Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

how do I get my irc bot to recieve commands from the server? I have the following code but I don't get any responses from the server btw it's a unrealIRCd server and Im not using nickserv. is there any errors?
use Socket; use IO::Select; sub connec { my ($server,$port)=@_; $iaddr = inet_aton($server); $paddr = sockaddr_in($port,$iaddr); $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket failed: $! +"; if (connect(SOCK,$paddr)) { print "Connected!\n"; } select(SOCK); $|=1; select(STDOUT); print SOCK "USER $botnick buf buf :$name\r\n"; print SOCK "NICK $botnick\r\n"; } sub getline { my $sck = IO::Select->new(); $sck->add(\*SOCK); while(1) { if ((@ready = $sck->can_read(5)) != 0) { foreach $handle (@ready) { if ($handle == \*SOCK) { if (sysread(SOCK,$inchar,1) > 0 ) { if ($inchar eq "\n") { chop $inline; return $inline; } } } } } } } while ($ircline=&getline){ ($pingchk,$rest)=split(/ /,$ircline,2); if ($pingchk eq 'PING') { print SOCK "PONG $rest"; } }
I need help bad :( I just want to make the bot issue USER cmd NICK cmd and join a channel then just reply to ping with pong that's it :( no luck will continue my search until I get some help....

Replies are listed 'Best First'.
Re: perl bot problems
by abstracts (Hermit) on Aug 10, 2001 at 23:37 UTC
    Hello

    There are several problems in your code. I'll list them below. But first, why are you not using Net::IRC? If you want to reinvent the wheel, then why are you not using IO::Socket::INET? Why are you doing all the dirty work yourself?

    Here are the things that are causing the problems:

    1. $botnick is used but never defined.
    2. $name also is used but never defined.
    3. You are using sysread to read from the socket, however, IRC is not a binary protocol. You should use <SOCK> instead.
    4. You are reading one char at a time which is not what you should do.
    5. This segment will always return an empty string:
       if ($inchar eq "\n") # $inchar is one char long
              {
                chop $inline; # remove it
                return $inline; # return empty.
              }
    6. You are defining the function connec which does the connect but never actually calling it.
    7. It works after fixing all the above points.

    Now back to using IO::Socket::INET, the above code can be shortened to:

    use IO::Socket; my $server = 'irc.openprojects.net'; my $port = 6667; my $nick = 'aziz'; my $name = 'Abdulaziz Ghuloum'; my $sock = IO::Socket::INET->new( PeerAddr => $server, PeerPort => $port) or die "Cannot connect: $!\n"; print $sock "USER $nick buf buf :$name\r\n"; print $sock "NICK $nick\r\n"; while(<$sock>){ print; my ($pingchk,$rest)=split(/ /,$_,2); if ($pingchk eq 'PING') { print $sock "PONG $rest\r\n"; } }
    Hope this helps,,,

    Aziz,,,

      Isn't there another way using vec() select()? I found some other perl irc bots and they all seem to use some form of select() or IO::Select??
        Hello again

        I suggest that you start with the basics first. IO::Select and select are only used when you need to handle multiple connections at the same time. Write your simple bot that does one thing first, add features as you go. Read the docs carefully. Just because someone used select does not mean that you should. Know why you need it before deciding that you do. After reading all the docs, understanding them, hanging them in your bathroom, then if you get stumbled, ask again :-) I think it's simple :-)

        Aziz,,,

Re: perl bot problems
by Bucket (Beadle) on Aug 10, 2001 at 23:36 UTC
    I'm assuming this is your entire script.
    1st, you never call connec, which won't connect you to the server and second, $botnick and $name are not defined anywhere, so they won't get sent to the server correctly.

    Also, you could look into the Net::IRC module, which I have sucessfully used in the past.

    HTH

    -- Bucket