in reply to Re: A Quick OO Module for parsing lines from an IRC connection
in thread A Quick OO Module for parsing lines from an IRC connection

Yeah! I use IO::Socket for programming bots as well. I learned a lot about network socket programming that way and it was fun.

I like the way you wrote the code for that bot, as well.

The various bots I write all connect pretty much the same way. The problem I always had was getting mine to send an auth message to nickserv after it connected. That was a little tricky, bt I am getting better, slowly but surely ;)

Regards,
SoupNazi

Visit us at irc.crazylogic.org #apartment411
D A R K E M P I R E s u b n e t w o r k
  • Comment on Re^2: A Quick OO Module for parsing lines from an IRC connection

Replies are listed 'Best First'.
Re^3: A Quick OO Module for parsing lines from an IRC connection
by gargle (Chaplain) on Aug 27, 2005 at 12:31 UTC

    Just a litte fun:

    Ok, it needs a lot more, p.e. you could code the reading part in a thread to loose the blocking.

    Irc.pm
    package Irc; use strict; use warnings; use Carp; use IO::Socket; sub new { my $class = shift; my $server = shift; my $self = { SERVER => $server, MYSOCKET => undef, }; my $closure = sub { my $field = shift; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless ($closure, $class); return $closure; } # a public getter for the SERVER variable sub server { &{ $_[0] }("SERVER") } # a private getter/setter for the SOCKET variable sub socket { caller(0) eq __PACKAGE__ || confess "socket is a private method"; &{ $_[0] }( "MYSOCKET", @_[1..$#_] ); } # log on sub connect { my $self = shift; my $socket = IO::Socket::INET->new( $self->server() ); $self->socket($socket); } # logoff sub disconnect { my $self = shift; my $sock = $self->socket; print $sock "exit\r"; close $sock; } # send (and automatically append \r) sub send { my $self = shift; my $text = shift; my $sock = $self->socket; print $text . "\n"; print $sock $text . "\r"; } # receive something from the server sub read { my $self = shift; my $sock = $self->socket; PINGPONG: while (<$sock>) { print "$_"; # break after receiving PING if (/^PING :(.+)\r$/) { $self->send("PONG :$1"); last PINGPONG; } # break after receiving MOTD if (/ 376/) { last PINGPONG; } } } 1;
    irc.pl
    #!/usr/bin/perl use strict; use warnings; use Irc; my $session = Irc->new("localhost:6667"); $session->connect(); $session->send("nick whatever"); $session->read(); $session->send("user whatever ignored v1.0 :perl hack"); $session->read(); $session->send("join #room1"); # now keep on reading until receiving the special logoff code ;) $session->read(); $session->disconnect();

      I am sorry I didn't have a chance to get back to this sooner. That is amazing.

      I am going to noodle this tonight, and tomorrow.

      You seem to have solved a problem I had, ostensibly, how does one make an 'object' out of an irc bot. I was working on a 'personality' which was for this project innappropriate. You made an 'entity', which honestly, I like better. Why have just a method to parse lines when methods could also be created to handle the other aspects of the bot, connecting to the server, etc? Very nice indeed.

      Thank you, sincerely for your input.

      Oh, I was also working on closures, your addition of one helped me to learn a thing or two about that. Thank you for that as well :)

      Regards
      SoupNazi

      Visit us at irc.crazylogic.org #apartment411
      D A R K E M P I R E s u b n e t w o r k
Re^3: A Quick OO Module for parsing lines from an IRC connection
by gargle (Chaplain) on Aug 26, 2005 at 20:01 UTC

    Something like this?

    my $sock = IO::Socket::INET->new('localhost:6669'); print "pass this_is_the_passwd\n"; print $sock "pass this_is_the_pw\r"; print "nick whateverhack\n"; print $sock "nick whateverhack\r";

    later on I always to stuff like this to parse messages people send (here p.e. all messages from hitty:

    LOOP: while (<$sock>) { $line = $_; if ( $line =~ / 366 / ) { print "$line\r"; } if ( $line =~ /PRIVMSG/ ) { $line =~ /^:(.*)!(.*) PRIVMSG (.*) :(.*?)--(.*).$/; $from = $1; $room = $3; $cam = $4; if ($cam eq "") { next LOOP; } $comment = $5; if ( $cam =~ /hitty/ ) { print "<$from\@$cam> $comment\n"; } } }

      ...thats interesting yes, something exactly like that. On the server I frequent, NickServe likes for you to auth a certain way and it is peculiar, if you were in an irc client, you'd type:

      /msg nickserv@services.crazylogic.org AUTH yourpassword

      Getting the bot to send that message (as a raw irc message) at the proper time, man ... I'll tell you, it was frustrating.

      Your parser interests me, too. I especially like the:

      $line =~ /^:(.*)!(.*) PRIVMSG (.*) :(.*?)--(.*).$/;

      Because occasionally NOTICES and other such things get sent out, and the parser I wrote will do it's thing on those as best it can but I still get "Uninitialized value ... " warnings when it happens sometimes. I could use that, and build it into the parse_line() method, to check whether it is a PRIVMSG type line, or a NOTICE or something else and then have the method parse the line accordingly.

      ...if that makes any sense (I've been at work all day & I'm tired :))

      Thank you, I think I'll work on that this weekend.

      Regards
      SoupNazi

      Visit us at irc.crazylogic.org #apartment411
      D A R K E M P I R E s u b n e t w o r k