1 # -*- mode: perl; coding: utf-8 -*-
2 # Keitairc::IrcCallback
4 # Copyright (c) 2008 Jun Morimoto <morimoto@mrmt.net>
5 # This program is covered by the GNU General Public License 2
7 package Keitairc::IrcCallback;
13 ################################################################
16 $heap->{Irc}->yield(register => 'all');
19 ################################################################
22 $heap->{Irc}->yield(connect => {});
25 ################################################################
27 my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER];
28 my $ib = $heap->{IrcBuffer};
29 my $cf = $heap->{Config};
30 my $msg = 'Connected to irc server!';
32 if ($cf->show_console()) {
33 $ib->add_message($ib->name2cid('*Console*'), $msg);
35 for my $cid ($ib->channels()) {
36 $ib->add_message($cid, $msg);
39 $heap->{disconnect_msg} = 1;
40 $kernel->delay(autoping => $cf->ping_delay());
43 ################################################################
45 my ($kernel, $heap, $who, $channel, $msg) = @_[KERNEL, HEAP, ARG0 .. ARG2];
46 my $ib = $heap->{IrcBuffer};
47 my $cf = $heap->{Config};
49 $msg = decode($cf->irc_charset(), $msg);
50 my $cid = $ib->name2cid(decode($cf->irc_charset(), $channel->[0]));
51 $heap->{IrcBuffer}->add_message($cid, $msg, $who);
52 $heap->{seen_traffic} = 1;
53 $heap->{disconnect_msg} = 1;
56 ################################################################
58 my ($kernel, $heap, $who, $channel) = @_[KERNEL, HEAP, ARG0, ARG1];
59 my $ib = $heap->{IrcBuffer};
60 my $cf = $heap->{Config};
64 $channel = decode($cf->irc_charset(), $channel);
66 # chop off after the gap to workaround madoka's bug
70 if($who ne $cf->irc_nick() && $cf->show_joinleave()) {
71 my $cid = $ib->name2cid($channel);
72 if ($cf->show_console()) {
73 my $ch = $ib->compact_channel_name($cid);
74 $ib->add_message($ib->name2cid('*Console*'),
75 "$who joined to $ch");
77 $ib->add_message($cid, "$who joined");
81 if($cf->follow_nick()){
82 $heap->{Irc}->yield(who => $channel);
84 $heap->{seen_traffic} = 1;
85 $heap->{disconnect_msg} = 1;
88 ################################################################
90 my ($kernel, $heap, $who, $channel) = @_[KERNEL, HEAP, ARG0, ARG1];
91 my $ib = $heap->{IrcBuffer};
92 my $cf = $heap->{Config};
96 $channel = decode($cf->irc_charset(), $channel);
97 # chop off after the gap (bug workaround of POE::Filter::IRC)
100 my $cid = $ib->name2cid($channel);
101 if($who eq $cf->irc_nick()){
103 } elsif ($cf->show_joinleave()) {
104 if ($cf->show_console()) {
105 my $ch = $ib->compact_channel_name($cid);
106 $ib->add_message($ib->name2cid('*Console*'),
107 "$who leaves from $ch");
109 $ib->add_message($cid, "$who leaves");
112 $ib->remove_nick($cid, $who);
113 $heap->{seen_traffic} = 1;
114 $heap->{disconnect_msg} = 1;
117 ################################################################
119 my ($kernel, $heap, $who, $message) = @_[KERNEL, HEAP, ARG0, ARG1];
120 my $ib = $heap->{IrcBuffer};
121 my $cf = $heap->{Config};
124 $message = decode($cf->irc_charset(), $message);
126 for my $cid ($ib->channels()) {
127 for my $name ($ib->list_nick($cid)) {
131 if($who eq $cf->irc_nick()){
133 } elsif (!$cf->show_console() &&
134 $cf->show_joinleave()) {
135 $ib->add_message($cid,
136 "$who leaves from IRC ($message)");
138 $ib->remove_nick($cid, $who);
143 if ($cf->show_console() && $cf->show_joinleave()) {
144 $ib->add_message($ib->name2cid('*Console*'),
145 "$who leaves from IRC ($message)");
148 $heap->{seen_traffic} = 1;
149 $heap->{disconnect_msg} = 1;
152 ################################################################
154 my ($kernel, $heap, $arg) = @_[KERNEL, HEAP, ARG1];
155 my ($channel, $user, $host, $server, $nick, $hg, $hop, @realname) =
157 my $ib = $heap->{IrcBuffer};
158 my $cf = $heap->{Config};
159 my $op = ($hg =~ /@/ ? 1 : 0);
160 my $cid = $ib->name2cid(decode($cf->irc_charset(), $channel));
161 # @realname may be irc channel's charset
162 $ib->add_nick($cid, $nick, $op, decode($cf->irc_charset(), join(' ', @realname)));
165 ################################################################
167 my ($kernel, $heap, $from, $channel, $mode, @args) =
168 @_[KERNEL, HEAP, ARG0, ARG1, ARG2, ARG3 .. $#_];
170 my $ib = $heap->{IrcBuffer};
171 my $cf = $heap->{Config};
172 my $cid = $ib->name2cid(decode($cf->irc_charset(), $channel));
173 if($mode =~ s/^\+//){
174 while($mode =~ s/^o//){
175 my $nick = shift @args;
176 $ib->op_nick($cid, $nick);
178 }elsif($mode =~ s/^-//){
179 while($mode =~ s/^o//){
180 my $nick = shift @args;
181 $ib->deop_nick($cid, $nick);
186 ################################################################
188 my ($kernel, $heap, $who, $nick) = @_[KERNEL, HEAP, ARG0, ARG1];
189 my $ib = $heap->{IrcBuffer};
190 my $cf = $heap->{Config};
194 if($who eq $cf->irc_nick()) {
195 $cf->irc_nick($nick);
198 for my $cid ($ib->channels()) {
199 for my $name ($ib->list_nick($cid)) {
203 my $realname = $ib->get_nick_realname($cid, $who);
204 my $op = $ib->get_nick_op($cid, $who);
205 $ib->remove_nick($cid, $who);
206 $ib->add_nick($cid, $nick, $op, $realname);
208 if (!$cf->show_console() && $cf->show_joinleave()) {
209 $ib->add_message($cid, "$who -> $nick");
216 if ($cf->show_console() && $cf->show_joinleave()) {
217 $ib->add_message($ib->name2cid('*Console*'), "$who -> $nick");
221 ################################################################
223 my ($kernel, $heap, $who, $channel, $msg) = @_[KERNEL, HEAP, ARG0 .. ARG2];
224 my $ib = $heap->{IrcBuffer};
225 my $cf = $heap->{Config};
228 my $cid = $ib->name2cid(decode($cf->irc_charset(), $channel->[0]));
229 $msg = decode($cf->irc_charset(), $msg);
231 $ib->add_message($cid, $msg, $who);
232 $heap->{seen_traffic} = 1;
233 $heap->{disconnect_msg} = 1;
236 ################################################################
238 my ($kernel, $heap, $who, $channel, $topic) = @_[KERNEL, HEAP, ARG0 .. ARG2];
239 my $ib = $heap->{IrcBuffer};
240 my $cf = $heap->{Config};
241 my $cid = $ib->name2cid(decode($cf->irc_charset(), $channel));
242 $topic = decode($cf->irc_charset(), $topic);
243 $ib->topic($cid, $topic);
245 $ib->add_message($cid, "$who set topic: $topic");
246 $heap->{seen_traffic} = 1;
247 $heap->{disconnect_msg} = 1;
250 ################################################################
252 my ($kernel, $heap, $raw) = @_[KERNEL, HEAP, ARG1];
253 my ($channel, $topic) = ($raw =~ m/(\S+)\s+:?(.*)$/);
254 my $ib = $heap->{IrcBuffer};
255 my $cf = $heap->{Config};
256 my $cid = $ib->name2cid(decode($cf->irc_charset(), $channel));
257 $ib->topic($cid, decode($cf->irc_charset(), $topic));
258 $heap->{seen_traffic} = 1;
259 $heap->{disconnect_msg} = 1;
262 ################################################################
264 my ($kernel, $heap, $who, $channel, $msg) = @_[KERNEL, HEAP, ARG0 .. ARG2];
265 my $ib = $heap->{IrcBuffer};
266 my $cf = $heap->{Config};
271 if ($who eq $cf->irc_nick()) {
272 # XXX: $channel may contain two or more nicks.
273 $buddy = $channel->[0];
277 $buddy = decode($cf->irc_charset(), $buddy);
278 $ib->join($buddy); # XXX: join $buddy if not joined
279 my $cid = $ib->name2cid($buddy);
280 $msg = decode($cf->irc_charset(), $msg);
281 $ib->add_message($cid, $msg, $who);
282 $heap->{seen_traffic} = 1;
283 $heap->{disconnect_msg} = 1;
286 ################################################################
288 my ($kernel, $heap, $who, $channel, $msg) = @_[KERNEL, HEAP, ARG0 .. ARG2];
289 my $ib = $heap->{IrcBuffer};
290 my $cf = $heap->{Config};
293 my $cid = $ib->name2cid(decode($cf->irc_charset(), $channel->[0]));
294 $msg = decode($cf->irc_charset(), $msg);
295 $ib->add_message($cid, sprintf('* %s %s', $who, $msg));
296 $heap->{seen_traffic} = 1;
297 $heap->{disconnect_msg} = 1;
300 ################################################################
302 my ($kernel, $heap) = @_[KERNEL, HEAP];
303 my $ib = $heap->{IrcBuffer};
304 my $cf = $heap->{Config};
305 my $msg = 'Disconnected from irc server, trying to reconnect...';
307 if ($heap->{disconnect_msg}) {
308 if ($cf->show_console()) {
309 $ib->add_message($ib->name2cid('*Console*'), $msg);
311 for my $cid ($ib->channels()) {
312 $ib->add_message($cid, $msg);
316 $heap->{disconnect_msg} = 0;
317 $kernel->delay(connect => $cf->reconnect_delay());
320 ################################################################
322 my ($kernel, $heap) = @_[KERNEL, HEAP];
323 $kernel->post(keitairc_irc => connect => {});
326 ################################################################
328 my ($kernel, $heap) = @_[KERNEL, HEAP];
329 my $cf = $heap->{Config};
331 $kernel->post(keitairc_irc => time) unless $heap->{seen_traffic};
332 $heap->{seen_traffic} = 0;
333 $kernel->delay(autoping => $cf->ping_delay());