1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
# -----------------------------------------------------------------------------
# $Id: Logger.pm 31673 2009-03-29 12:24:29Z hio $
# -----------------------------------------------------------------------------
package Log::Logger;
use strict;
use warnings;
use Multicast;
our $MARKER = {
myself => {
PRIVMSG => ['>','<'],
NOTICE => [')','('],
},
priv => {
PRIVMSG => ['-','-'],
NOTICE => ['=','='],
},
channel => {
PRIVMSG => ['<','>'],
NOTICE => ['(',')'],
},
};
sub new {
my ($class,$enstringed_callback,$exception_object,@exceptions) = @_;
# enstringed_callback:
# メッセージをログ文字列化した時に呼ばれる関数。CODE型。
# 引数を二つ取り、一つ目はチャンネル名、二つ目はログ文字列。
# exception_object:
# exceptionsで指定されたメソッドを呼ぶとき、どのオブジェクトで呼ぶか。
# exceptions:
# 特定のメッセージのログ文字列化をオーバーライドする
# 'S_PRIVMSG'等。
# 引数は(Tiarra::IRC::Message,IrcIO)、戻り値は[チャンネル名,ログ文字列]の配列
my $this = {
enstringed => $enstringed_callback,
exception_object => $exception_object,
exceptions => do {
my %hash = map { $_ => 1 } @exceptions;
\%hash;
},
};
bless $this,$class;
}
sub log {
my ($this,$msg,$sender) = @_;
my $prefix = do {
if ($sender->isa('IrcIO::Server')) {
'S';
}
elsif ($sender->isa('IrcIO::Client')) {
'C';
}
};
my $method_name = "${prefix}_".$msg->command;
my @results;
# このメソッドはexceptionsで定義されているか?
if (defined $this->{exceptions}->{$method_name}) {
eval {
@results = $this->{exception_object}->$method_name($msg,$sender);
}; if ($@) {
RunLoop->shared->notify_error($@);
}
}
else {
# このクラスにメソッドはあるか?
if ($this->can($method_name)) {
eval {
@results = $this->$method_name($msg,$sender);
}; if ($@) {
RunLoop->shared->notify_error($@);
}
}
}
foreach (@results) {
$this->{enstringed}->($_->[0],$_->[1]);
}
}
sub S_JOIN {
my ($this,$msg,$sender) = @_;
$msg->param(0) =~ m/^([^\x07]+)(?:\x07(.*))?/;
my ($ch_name,$mode) = ($1,(defined $2 ? $2 : ''));
$mode =~ tr/ov/@+/;
[$msg->param(0),
sprintf('+ %s%s (%s) to %s',
$mode,$msg->nick,$msg->prefix,$msg->param(0))];
}
sub S_PART {
my ($this,$msg,$sender) = @_;
if (defined $msg->param(1)) {
[$msg->param(0),
sprintf('- %s from %s (%s)',
$msg->nick,$msg->param(0),$msg->param(1))];
} else {
[$msg->param(0),
sprintf('- %s from %s',
$msg->nick,$msg->param(0))];
}
}
sub S_KICK {
my ($this,$msg,$sender) = @_;
# RFC2812には、「サーバはクライアントに複数のチャンネルやユーザのKICKメッセージを
# 送っては「いけません」。これは、古いクライアントソフトウェアとの下位互換のためです。」とある。
[$msg->param(0),
sprintf('- %s by %s from %s (%s)',
$msg->param(1),$msg->nick,$msg->param(0),$msg->param(2))];
}
sub S_INVITE {
my ($this,$msg,$sender) = @_;
[$msg->param(1),
sprintf 'Invited by %s: %s',$msg->nick,$msg->param(1)];
}
sub S_MODE {
my ($this,$msg,$sender) = @_;
[$msg->param(0),
sprintf('Mode by %s: %s %s',
$msg->nick,
$msg->param(0),
join(' ',@{$msg->params}[1 .. ($msg->n_params - 1)]))];
}
sub S_NICK {
my ($this,$msg,$sender) = @_;
my $network_name = $sender->network_name;
my $line = do {
sprintf(
do {
if ($msg->param(0) eq $sender->current_nick) {
'My nick is changed (%s -> %s)';
}
else {
'%s -> %s';
}
},
$msg->nick,
$msg->param(0));
};
my @result;
if( my $ch_short_list = $msg->remark('affected-channels') ){
foreach my $ch_name (@$ch_short_list) {
push @result,[Multicast::attach($ch_name,$network_name),
$line];
}
}
@result;
}
{
no warnings 'once';
*S_KILL = \&S_QUIT;
}
sub S_QUIT {
my ($this,$msg,$sender) = @_;
my $network_name = $sender->network_name;
my @result;
if( my $ch_short_list = $msg->remark('affected-channels') ){
foreach my $ch_name (@$ch_short_list) {
push @result,[Multicast::attach($ch_name,$network_name),
sprintf '! %s (%s)',$msg->nick,$msg->param(0)];
}
}
@result;
}
sub S_TOPIC {
my ($this,$msg,$sender) = @_;
[$msg->param(0),
sprintf('Topic of channel %s by %s: %s',
$msg->param(0),
$msg->nick,
$msg->param(1))];
}
{
no warnings 'once';
*S_PRIVMSG = \&PRIVMSG_or_NOTICE;
*S_NOTICE = \&PRIVMSG_or_NOTICE;
*C_PRIVMSG = \&PRIVMSG_or_NOTICE;
*C_NOTICE = \&PRIVMSG_or_NOTICE;
}
sub PRIVMSG_or_NOTICE
{
my ($this,$msg,$sender) = @_;
my $line = $this->_build_message($msg, $sender);
my $channel = $line->{is_priv} ? 'priv' : $line->{ch_long};
[$channel, $line->{formatted}];
}
# -----------------------------------------------------------------------------
# $hashref = $obj->_build_message($msg, $sender).
# Log/Channel から拝借.
# ただ
# - distinguish_myself が省かれている.
# - PRIVでも相手の名前がchannel名として使われる.
# - 好きにformat出来るように解析した情報をHASHREFで返している.
# という点で変更されている.
#
sub _build_message
{
my ($this, $msg, $sender) = @_;
my $raw_target = $msg->param(0);
my ($target,$netname,$_explicit) = Multicast::detatch( $raw_target );
my $is_priv = Multicast::nick_p($target);
my $cmd = $msg->command;
my $marker_id;
if( $sender->isa('IrcIO::Client') )
{
$marker_id = 'myself';
}elsif( $is_priv )
{
$marker_id = 'priv';
}else
{
$marker_id = 'channel';
}
my $marker = $MARKER->{$marker_id}{$cmd};
$marker or die "no marker for $marker_id/$cmd";
my ($speaker, $ch_short);
if( $sender->isa('IrcIO::Client') )
{
# 自分の発言.
$speaker = RunLoop->shared_loop->network( $netname )->current_nick;
$ch_short = $target;
}else
{
# 相手の.
$speaker = $msg->nick || $sender->current_nick;
$ch_short = $is_priv ? $speaker : $target;
}
my $ch_long = Multicast::attach($ch_short, $netname);
my $line = sprintf(
'%s%s:%s%s %s',
$marker->[0],
$ch_long,
$speaker,
$marker->[1],
$msg->param(1),
);
+{
marker_id => $marker_id, # 'myself' / 'priv' / 'channel'
is_priv => $is_priv,
marker => $marker, # ['<', '>'], etc.
speaker => $speaker,
ch_long => $ch_long,
ch_short => $ch_short,
netname => $netname,
msg => $msg->param(1),
command => $msg->command(),
time => $msg->time(),
#msg_orig => $msg,
formatted => $line,
};
}
1;
|