in reply to Re^2: IRC Client (non-bot)
in thread IRC Client (non-bot)

OK. I added the IO watch but it gets hung up for some reason and stops receiving data and does not finish logging in. I cant seem to see where the problem is so heres the code:
#!/usr/local/bin/perl -w use strict; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; use IO::Socket; #-------------------Shared Variables------------------- my $server = "irc.freenode.net"; my $nick = "simple_bot"; my $login = "simple_bot"; my $channel = "#GRRUVI"; #-------------------Main Loop------------------- my $window = Gtk2::Window->new('toplevel'); $window->signal_connect( delete_event => sub { Gtk2->main_quit; }); $window->set_default_size( 300, 200 ); my $table = Gtk2::Table->new(2, 1, FALSE); my $scroller = Gtk2::ScrolledWindow->new; my $textview = Gtk2::TextView->new; my $entry = Gtk2::Entry->new; $scroller->add($textview); $table->attach_defaults($scroller, 0, 1, 0, 1); $table->attach_defaults($entry, 0, 1, 1, 2); $window->add($table); $window->show_all; # Connect to the IRC server. my $sock = new IO::Socket::INET( PeerAddr => $server, PeerPort => 6667, Proto => 'tcp' ) or die "Can't connect\n"; Glib::IO->add_watch( fileno $sock, [qw/in hup err/], \&incoming_data, +$sock ); # Log on to the server. print $sock "NICK $nick\r\n"; print $sock "USER $login 8 * :Perl IRC Hacks Robot\r\n"; Gtk2->main; #-------------------Incoming data------------------- sub incoming_data { my ( $fd, $condition, $fh ) = @_; if ( $condition eq 'in' ) { my $input = <$fh>; chop $input; # if ( defined $data ) { # # do something useful with the text. # my $buffer = $textview->get_buffer; # $buffer->insert( $buffer->get_end_iter, $data ); # } # Check the numerical responses from the server. if ($input =~ /004/) { # We are now logged in. # Join the channel. print $sock "JOIN $channel\r\n"; } elsif ($input =~ /433/) { die "Nickname is already in use."; } elsif ($input =~ /^PING(.*)$/i) { # We must respond to PINGs to avoid being disconnected. print $sock "PONG $1\r\n"; } else { # Print the raw line received by the bot. print "$input\n"; } } # if ( $condition >= 'hup' or $condition >= 'err' ) { # End Of File, Hang UP, or ERRor. that means # we're finished. # stop ability to send # $entry->set_editable(0); # $entry->signal_handler_block ($send_sig); # my $buffer = $textview->get_buffer; # $buffer->insert( $buffer->get_end_iter, "Server connection +lost !!\n" ); #close socket # $fh->close; # $fh = undef; #allow for new connection #$button->set_label('Connect'); #$button->set_sensitive(1); #$button->grab_focus; #Gtk2->main_iteration while Gtk2->events_pending; # } # if ($fh) { # the file handle is still open, so return TRUE to # stay installed and be called again. # print "still connected\n"; # possibly have a "connection alive" indicator # return TRUE; # } # else { # we're finished with this job. start another one, # if there are any, and uninstall ourselves. # return FALSE; # } }

Replies are listed 'Best First'.
Re^4: IRC Client (non-bot)
by pc88mxer (Vicar) on Feb 10, 2008 at 20:34 UTC
    Why are you writing:

    if ( $condition >= 'in' ) ...

    I would use $condition eq 'in'.

      I noticed a few things:
      I tested the program using mIRC. I logged into the channel with the client and under another name with mIRC. Logging in works fine, I can see the clients nickname in the channel. What the problem seems to be is when the MOTD is displayed. It seems that only a few lines of the MOTD are displayed before the 'in' condition is stopped and the message stops displaying in the terminal window. When I type in mIRC, a line of the MOTD is displayed in terminal window. The $input variable seems to be behind the real-time sending of the messages. I'm not sure how to remove this lag and I'm looking for ideas.
      #!/usr/local/bin/perl -w use strict; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; use IO::Socket; #-------------------Shared Variables------------------- my $server = "irc.freenode.net"; my $nick = "simple"; my $login = "simple"; my $channel = "#GRRUVI"; my $sock; #-------------------Main Loop------------------- my $window = Gtk2::Window->new('toplevel'); $window->signal_connect( delete_event => sub { close($sock); Gtk2->main_quit; }); $window->set_default_size( 300, 200 ); my $table = Gtk2::Table->new(2, 1, FALSE); my $scroller = Gtk2::ScrolledWindow->new; my $textview = Gtk2::TextView->new; my $entry = Gtk2::Entry->new; $scroller->add($textview); $table->attach_defaults($scroller, 0, 1, 0, 1); $table->attach_defaults($entry, 0, 1, 1, 2); $window->add($table); # allows for sending each line with an enter keypress my $send_sig = $entry->signal_connect ('key-press-event' => sub { my ($widget,$event)= @_; if( $event->keyval() == 65293){ # a return key press my $text = $entry->get_text; if(defined $sock){ print $sock "PRIVMSG $channel :$tex +t\r\n";} $entry->set_text(''); $entry->set_position(0); } }); $entry->signal_handler_block($send_sig); #not connected yet $entry->set_editable(0); $window->show_all; connecting(); Glib::IO->add_watch( fileno $sock, [qw/in hup err/], \&incoming_data, +$sock ); Gtk2->main; #-------------------Connect to Server------------------- sub connecting { # Connect to the IRC server. $sock = new IO::Socket::INET( PeerAddr => $server, PeerPort => 6667, Proto => 'tcp' ) or die "Can't connect\n"; # Log on to the server. print $sock "NICK $nick\r\n"; print $sock "USER $login 8 * :Perl IRC Hacks Robot\r\n"; # Read lines from the server until it tells us we have connected. while (my $input = <$sock>) { # Check the numerical responses from the server. if ($input =~ /004/) { # We are now logged in. last; } elsif ($input =~ /433/) { die "Nickname is already in use."; } } # Join the channel. print $sock "JOIN $channel\r\n"; $entry->set_editable(1); $entry->grab_focus; $entry->signal_handler_unblock ($send_sig); Gtk2->main_iteration while Gtk2->events_pending; } #-------------------Incoming data------------------- sub incoming_data { my ( $fd, $condition, $fh ) = @_; if ( $condition eq 'in' ) { my $input = scalar <$fh>; chop $input; if ($input =~ /^PING(.*)$/i) { # We must respond to PINGs to avoid being disconnected. print $sock "PONG $1\r\n"; } else { # Print the raw line received by the bot. print "$input\n"; } } return TRUE; }