Skip to content

Instantly share code, notes, and snippets.

@kanetann
Created November 7, 2011 09:48
Show Gist options
  • Save kanetann/1344571 to your computer and use it in GitHub Desktop.
Save kanetann/1344571 to your computer and use it in GitHub Desktop.
AnyEvent::IRC::Client Sample
#!/usr/bin/perl
use strict;
use warnings;
use AnyEvent;
use AnyEvent::IRC::Client;
use Encode qw /encode decode_utf8/;
# 接続設定
my $channel = '#somechannel';
my %config = (
server => '192.168.0.1',
port => 6667,
info => {
nick => 'testbot',
real => 'testbot',
}
);
# 状態変数生成
my $condvar = AnyEvent->condvar;
# IRC イベント
my $irc = AnyEvent::IRC::Client->new;
$irc->reg_cb(
# 接続時に呼び出されるイベント
connect => sub {
my ($irc, $err) = @_;
if (defined $err) {
print "connect error: $err\n";
return;
}
print "connected.\n";
},
# 接続成功時に呼び出されるイベント
registered => sub {
print "registered.\n";
},
# 切断時に呼び出されるイベント
disconnect => sub {
print "disconnected.\n";
},
);
$irc->reg_cb(
# $channel に発言があったとき _public() を実行
publicmsg => sub {
my ($irc, $chan, $msg) = @_;
_public($msg->{params}[1]);
}
);
sub _public {
my $msg = shift;
$msg = $msg . 'だと!?';
Encode::from_to($msg,"utf8","iso-2022-jp");
$irc->send_chan($channel, "PRIVMSG", $channel, $msg);
}
# IRC サーバに接続
$irc->connect($config{server}, $config{port}, $config{info});
# $channel に join
$irc->send_srv("JOIN", $channel);
# $channel に notice メッセージを送る
$irc->send_chan($channel, "NOTICE", $channel, "Hello, $channel!");
# イベント待ち
$condvar->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment