in reply to Re^2: IRC Client (non-bot)
in thread IRC Client (non-bot)
#!/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 | |
by deadpickle (Pilgrim) on Feb 12, 2008 at 05:16 UTC |