This is a cut down version of the first bot I ever experimented with (with some stuff changed to protect the innocent:

#!/usr/bin/perl -w use strict; use IO::Socket; my ( $server, $port, $my_nick, $channel, ) = ('irc.dal.net', 6667, 'foobot', '#hackerhost', ); my $sock = IO::Socket::INET->new(PeerAddr => $server, PeerPort => $port, Proto => "tcp", ); print $sock "USER $my_nick foo.wherever.com $server :Just another bot\ +n"; print $sock "NICK $my_nick\n"; $SIG{INT} = sub { print $sock "QUIT :Copped signint from owner\n"; close $sock; exit; }; SOCKLOOP: while (<$sock>) { if ( /^[A-Z]/ ) { if ( /^PING\s+:([\w.]+)/ ) { print $sock "PONG $1\n"; } next SOCKLOOP; #probably need to catch some other stuff but hey } else { my ($server,$action,$stuff) = /^:(\S+)\s+(\w+)\s+(.*)/; if ( $action =~ /\d+/ ) # Server response code { if ($action == 376 ) { print $sock "JOIN $channel\n"; } } } }
This works and has been tested. The way I have done it gives you an opportunity to expand its functionality later.

However having shown you that most people now will use Net::IRC or even better POE::Component::IRC because both of those will handle setting up the connection, joining the channel and answering the PINGs leaving you free to add the actual functionality.

Of course all of this moot because if you are intending to run this as a CGI then it isn't going to work because a CGI program is started as the result of a request from a browser and is intended to return some content and then terminate, it will not maintain a permanent connection for you.

/J\


In reply to Re: IRC bot in perl by gellyfish
in thread IRC bot in perl by Swordkeeper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.