An irc connection abstraction
use AnyEvent; use AnyEvent::IRC::Connection; my $c = AnyEvent->condvar; my $con = new AnyEvent::IRC::Connection; $con->connect ("localhost", 6667); $con->reg_cb ( connect => sub { my ($con) = @_; $con->send_msg (NICK => 'testbot'); $con->send_msg (USER => 'testbot', '*', '0', 'testbot'); }, irc_001 => sub { my ($con) = @_; print "$_[1]->{prefix} says I'm in the IRC: $_[1]->{params}->[-1]!\n"; $c->broadcast; } ); $c->wait;
The connection class. Here the actual interesting stuff can be done, such as sending and receiving \s-1IRC\s0 messages. And it also handles \s-1TCP\s0 connecting and even enabling of \s-1TLS\s0.
Please note that \s-1CTCP\s0 support is available through the functions \*(C`encode_ctcp\*(C' and \*(C`decode_ctcp\*(C' provided by AnyEvent::IRC::Util.
This constructor doesn't take any arguments. \s-1NOTE:\s0 You are free to use the hash member \*(C`heap\*(C' (which contains a hash) to store any associated data with this object. For example retry timers or anything else. You can also access that member via the \*(C`heap\*(C' method. Tries to open a socket to the host $host and the port $port. If an error occurred it will die (use eval to catch the exception). If you want to connect via \s-1TLS/SSL\s0 you have to call the \*(C`enable_ssl\*(C' method before to enable it. $prepcb_or_timeout can either be a callback with the semantics of a prepare callback for the function \*(C`tcp_connect\*(C' in AnyEvent::Socket or a simple number which stands for a timeout. This method will enable \s-1SSL\s0 for new connections that are initiated by \*(C`connect\*(C'. Unregisters the connection in the main AnyEvent::IRC object, closes the sockets and send a 'disconnect' event with $reason as argument. Returns true when this connection is connected. Otherwise false. Returns the hash reference stored in the \*(C`heap\*(C' member, that is local to this connection object that lets you store any information you want. This method sends $ircline straight to the server without any further processing done. This function sends a message to the server. @ircmsg is the argument list for \*(C`AnyEvent::IRC::Util::mk_msg (undef, $command, @params)\*(C'.
Following events are emitted by this module and shouldn't be emitted from a module user call to \*(C`event\*(C'. See also the documents Object::Event about registering event callbacks. This event is generated when the socket was successfully connected or an error occurred while connecting. The error is given as second argument ($error) to the callback then. This event will be generated if the connection is somehow terminated. It will also be emitted when \*(C`disconnect\*(C' is called. The second argument to the callback is $reason, a string that contains a clue about why the connection terminated. If you want to reestablish a connection, call \*(C`connect\*(C' again. Emitted when a message is about to be sent. $ircmsg is an array reference to the arguments of \*(C`mk_msg\*(C' (see AnyEvent::IRC::Util). You may modify the array reference to change the message or even intercept it completely by calling \*(C`stop_event\*(C' (see Object::Event \s-1API\s0): $con->reg_cb ( send => sub { my ($con, $ircmsg) = @_;
if ($ircmsg->[1] eq 'NOTICE') { $con->stop_event; # prevent any notices from being sent.
} elsif ($ircmsg->[1] eq 'PRIVMSG') { $ircmsg->[-1] =~ s/sex/XXX/i; # censor any outgoing private messages. } } ); Emitted when a message (@ircmsg) was sent to the server. @ircmsg are the arguments to \*(C`AnyEvent::IRC::Util::mk_msg\*(C'. Emitted when a message ($msg) was read from the server. $msg is the hash reference returned by \*(C`AnyEvent::IRC::Util::parse_irc_msg\*(C'; Note: '<lowercase command>' stands for the command of the message in (\s-1ASCII\s0) lower case.
This event is emitted when the write buffer of the underlying connection is empty and all data has been given to the kernel. See also \*(C`samples/notify\*(C' about a usage example. Please note that this buffer is \s-1NOT\s0 the queue mentioned in AnyEvent::IRC::Client!
Robin Redeker, \*(C`<[email protected]>\*(C'
AnyEvent::IRC
AnyEvent::IRC::Client
Copyright 2006-2009 Robin Redeker, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.