Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Sending using Net::IRC

by deadpickle (Pilgrim)
on Mar 07, 2008 at 06:21 UTC ( [id://672689]=perlquestion: print w/replies, xml ) Need Help??

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

Yep I have been working on this forever and am very close to getting something that works correctly. Looks like Net::IRC was the golden module to use. Now I am wondering if there is a way to SEND PRIVMSG's to the channel (therefore making them public) using the module Net::IRC? This modeule does everything I need EXCEPT this, so if someone knows how to do this I would be forever greatful.
Here is the code I have so far. Nothing is printed to the textview since there is no point in going any farther until I'm able to send messages to the channel:
use strict; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; use Net::IRC; use threads; use threads::shared; my $irc = new Net::IRC; my $chat_state = 'Connect'; my $chat_entry; my $chat_send_sig; my $chat_textview; my $chat_button; my $die:shared = 0; my $chat_start:shared = 0; my $channel:shared = "#GRRUVI"; my $irc_server:shared = "irc.servercentral.net"; my $nick:shared = "MetBase"; my $msg:shared; ################################################# #Threads my $thread_chat = threads->new(\&chat); ################################################# #-------------------Main Loop------------------- &chat_build; Gtk2->main; ####################CHAT BLOCK#################### #-------------------chat Build------------------- sub chat_build { my $chat_window = Gtk2::Window->new('toplevel'); $chat_window->set_title('Chat Client'); $chat_window->set_position('center'); $chat_window->set_default_size( 300, 300 ); $chat_window->signal_connect(delete_event=> sub{ $die = 1; $thread_chat->join; Gtk2->main_quit; }); $chat_entry = Gtk2::Entry->new; my $chat_vbox = Gtk2::VBox->new; #*********Removed Menu and Shortcuts*********# my $chat_scroll = Gtk2::ScrolledWindow->new; $chat_scroll->set_shadow_type( 'etched-out'); $chat_textview = Gtk2::TextView->new; #$chat_entry = Gtk2::Entry->new; my $chat_buffer = $chat_textview->get_buffer; $chat_buffer->create_mark( 'end', $chat_buffer->get_end_iter, FALS +E ); $chat_buffer->signal_connect(insert_text => sub { $chat_textview->scroll_to_mark( $chat_buffer->get_mark('end'), + 0.0, TRUE, 0, 0.5 ); }); $chat_button = Gtk2::Button->new; $chat_button->set_label($chat_state); # allows for sending each line with an enter keypress $chat_send_sig = $chat_entry->signal_connect ('key-press-event' => + sub { my ($widget,$event)= @_; if( $event->keyval() == 65293){ # a return key press $msg = $chat_entry->get_text; # print $sock "PRIVMSG $channel :$text\r\n"; $chat_entry->set_text(''); $chat_entry->set_position(0); $chat_start = 2; print "$chat_start\n"; } }); $chat_entry->signal_handler_block($chat_send_sig); #not connected +yet $chat_entry->set_editable(0); $chat_textview->set_editable(0); $chat_textview->set_cursor_visible(0); $chat_textview->set_wrap_mode('word'); $chat_scroll->add($chat_textview); $chat_vbox->add($chat_scroll); $chat_vbox->pack_start( $chat_entry, FALSE, FALSE, 0 ); $chat_vbox->pack_start( $chat_button, FALSE, FALSE, 0 ); $chat_window->add($chat_vbox); $chat_window->show_all; $chat_button->signal_connect("clicked" => sub { if ($chat_state eq 'Connect') { $chat_button->set_label('Disconnect'); $chat_state='Disconnect'; $chat_start = 1; $chat_entry->set_editable(1); $chat_entry->grab_focus; $chat_entry->signal_handler_unblock ($chat_send_sig); #&connecting; } else { $chat_button->set_label('Connect'); $chat_state='Connect'; $chat_start = 0; } }); #*********Removed Keyboard shortcuts and Output File Creation***** +****# return 1; } ################################################## ####################Threads#################### sub chat{ #print "looped...\n"; while (1){ my $conn; goto END if $die == 1; if ($chat_start == 1){ print "starting...\n"; $conn = $irc->newconn(Server => $irc_server, Port => 6667, Nick => $nick, Ircname => "CoCoNUE Member $nick") or die "Can't connect to IRC server\n"; #Install Handlers $conn->add_handler('cping', \&on_ping); $conn->add_handler('crping', \&on_ping_reply); $conn->add_handler('msg', \&on_msg); $conn->add_handler('join', \&on_join); $conn->add_handler('part', \&on_part); $conn->add_global_handler([ 251,252,253,254,302,255 ], \&o +n_init); $conn->add_global_handler('disconnect', \&on_disconnect); $conn->add_global_handler(376, \&on_connect); $conn->add_global_handler(433, \&on_nick_taken); $conn->add_global_handler(353, \&on_names); for (;;){ $irc->do_one_loop; print "Looped: Reading\n"; if ($chat_start == 2){ print $conn "PRIVMSG #GRRUVI Hello\r\n"; $nick->privmsg($channel, "$msg"); #print $conn "Hello\r\n"; print "Looped: Sending\n"; $chat_start = 1; } goto END if $die == 1; last if $chat_start == 0; } } } END: } #-------------------Handlers------------------- # What to do when the bot successfully connects. sub on_connect { my $self = shift; print "Joining $channel...\n"; $self->join("$channel"); # $self->privmsg("#net-irc2", &pickrandom()); # $self->topic("#net-irc2"); } # Handles some messages you get when you connect sub on_init { my ($self, $event) = @_; my (@args) = ($event->args); shift (@args); print "*** @args\n"; } # What to do when someone leaves a channel the bot is on. sub on_part { my ($self, $event) = @_; my ($channel) = ($event->to)[0]; printf "*** %s has left channel %s\n", $event->nick, $channel; } # What to do when someone joins a channel the bot is on. sub on_join { my ($self, $event) = @_; my ($channel) = ($event->to)[0]; printf "*** %s (%s) has joined channel %s\n", $event->nick, $event->userhost, $channel; if ($event->userhost =~ /^corbeau\@.*execpc\.com/) { # Auto-ops a +nyone who $self->mode("#IRC.pm", "+o", $event->nick); # matches hostmas +k. } } # What to do when we receive a private PRIVMSG. sub on_msg { my ($self, $event) = @_; my ($nick) = $event->nick; print "*$nick* ", ($event->args), "\n"; # $self->privmsg($nick, &pickrandom()); # Say a Zippy quote. } # Prints the names of people in a channel when we enter. sub on_names { my ($self, $event) = @_; my (@list, $channel) = ($event->args); # eat yer heart out, mjd +! # splice() only works on real arrays. Sigh. ($channel, @list) = splice @list, 2; print "Users on $channel: @list\n"; } # Yells about incoming CTCP PINGs. sub on_ping { my ($self, $event) = @_; my $nick = $event->nick; $self->ctcp_reply($nick, join (' ', ($event->args))); print "*** CTCP PING request from $nick received\n"; } # Gives lag results for outgoing PINGs. sub on_ping_reply { my ($self, $event) = @_; my ($args) = ($event->args)[1]; my ($nick) = $event->nick; $args = time - $args; print "*** CTCP PING reply from $nick: $args sec.\n"; } # Change our nick if someone stole it. sub on_nick_taken { my ($self) = shift; $self->nick(substr($self->nick, -1) . substr($self->nick, 0, 8)); } # Reconnect to the server when we die. sub on_disconnect { my ($self, $event) = @_; print "Disconnected from ", $event->from(), " (", ($event->args())[0], "). Attempting to reconnect...\n"; $self->connect(); }

Replies are listed 'Best First'.
Re: Sending using Net::IRC
by moritz (Cardinal) on Mar 07, 2008 at 08:08 UTC
    The Net::IRC documentation has this example:
    $self->privmsg("#IRC.pm", "Hi there.");

    ... which should do exactly what you want: it sends a message to channel.

Re: Sending using Net::IRC
by grinder (Bishop) on Mar 07, 2008 at 13:26 UTC
    Looks like Net::IRC was the golden module to use.

    Uh, actually, no. Net::IRC is more or less deprecated. POE::Component::IRC is the recommended way for developing new IRC applications. There's a wealth of modules on CPAN that you can use to build all sorts of IRC bots.

    • another intruder with the mooring in the heart of the Perl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://672689]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2024-04-23 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found