简体   繁体   中英

How can I implement a simple IRC client in Perl?

I'm working on a tool that needs to send IRC messages to an internal IRC channel. This isn't a constantly running program, but rather a tool that will be invoked occasionally and needs to be able to notify the channel with a couple of messages when it's invoked.

I looked at Net::IRC , but it's been dead since 2004. So I looked at the alternatives that it lists ( Bot::BasicBot and POE::Component::IRC ) but both of those need to be run under POE and its event loop. The same problem occurs for something like Net::Async::IRC since it needs to be run in the IO::Async event loop.

I'm not writing a full fledged bot that needs to interact with anything, I just want to login to an irc server, join a channel, post some quick messages and then leave. I don't want to have to re-write this whole program to fit inside of some framework's event loop just to do this.

So, any recommendations for a library for a simple IRC client that won't make me rewrite my whole app?

Use AnyEvent::IRC::Client , while it is using the AnyEvent 'event loop', you don't have to rewrite your app to use it, you can do something like this:

use AnyEvent;
use AnyEvent::IRC::Client;

with the rest of your module use lines; and something like this

sub do_irc {
    my $log_chan = '#foobar';
    my $timer; 
    my $c = AnyEvent->condvar;
    my $con = new AnyEvent::IRC::Client;

    $con->reg_cb( join => sub {
        my ($con, $nick, $channel, $is_myself) = @_; 
        if ($is_myself && $channel eq $log_chan) {
           $con->send_chan( $channel, PRIVMSG => ($channel, 'my information') );
           $timer = AnyEvent->timer ( 
                after => 1,
                cb => sub {
                    undef $timer;
                    $con->disconnect('done');
                });
        }
    });

    $con->connect($irc_serv, 6667, { nick => $your_nick } );
    $con->send_srv( JOIN => ($log_chan) );
    $c->wait;
    $con->disconnect;

}

to do the connection, it will only return once it is done (error handling left out for brevity). Nothing else in your app needs to use AnyEvent.

Net::IRC is still perfectly usable, if all you want to do is something as simple as you're describing, I think it would do just fine.

That said, getting used to POE isn't a bad idea at all, especially since IRC bots tend to grow in functionality over time :)

Note that there's actually nothing stopping you from using PoCo-IRC or BasicBot. Yes, they run under POE, but POE doesn't have to have control over your entire app.

If you're just connecting, doing some stuff on IRC, disconnecting, and doing other things, you can just make sure that the IRC session destroys itself when it's done -- when there are no sessions remaining, POE::Kernel->run will return control right back to your program.

If you're dealing with a longer-lived connection, but you still want to turn the control around, POE provides run_one_timeslice and run_while methods that give you fine control over when and where POE runs. Of course, you have to arrange for it to run at least often enough to respond to any server PINGs and keep the socket buffers from filling up.

Actually, Net::IRC does exactly the same thing in taking over your app with its own event loop -- except it's not a named thing like POE or AnyEvent -- it's just "the Net::IRC event loop". It doesn't mean you need a total rewrite to work with Net::IRC, and it doesn't mean you need a total rewrite to work with POE :)

Using the answer from MkV, I was able to create a working command-line script:

#!/usr/bin/perl

use strict;
use warnings;
use AnyEvent;
use AnyEvent::IRC::Client;
use Data::Dumper ();
use Getopt::Long;

my %opt = (
    channel => '#ircmsgtest',
    nick    => "ircmsg$$",
    port    => 6667,
    server  => 'irc.freenode.net',
    verbose => undef,
);

GetOptions(\%opt,'channel','nick', 'port', 'server', 'verbose|v');
my $message = shift() || "test message @{[ scalar localtime() ]}";
if ($opt{verbose}) {
    warn "message is: '$message'";
    warn Data::Dumper->Dump([\%opt], [qw(*opt)]);
}

my $c = AnyEvent->condvar;
my $con = AnyEvent::IRC::Client->new;

$con->reg_cb(
    join => sub {
        my ($con, $nick, $channel, $is_myself) = @_; 
        if ($is_myself && $channel eq $opt{channel}) {
            $con->send_chan($channel, PRIVMSG => $channel, $message);
            $c->send;
        }
    }
);

$con->connect($opt{server}, $opt{port}, { nick => $opt{nick} } );
$con->send_srv(JOIN => $opt{channel});
$c->wait;
$con->disconnect;

It connects, sends the message, and then disconnects, which is perfect for my needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM