3 # $Id: keitairc,v 1.29 2004-09-16 13:44:10 morimoto Exp $
5 # Copyright (c) 2003 Jun Morimoto <morimoto@xantia.citroen.org>
6 # This program is covered by the GNU General Public License 2
8 # Depends: libjcode-pm-perl, libpoe-component-irc-perl,
9 # liburi-perl, libwww-perl, libappconfig-perl
11 my $rcsid = q$Id: keitairc,v 1.29 2004-09-16 13:44:10 morimoto Exp $;
12 my ($version) = $rcsid =~ m#,v ([0-9.]+)#;
17 use POE::Component::Server::TCP;
18 use POE::Filter::HTTPD;
19 use POE::Component::IRC;
22 use AppConfig qw(:argcount);
24 use constant true => 1;
25 use constant false => 0;
26 use constant cookie_ttl => 86400*3; # 3 days
28 my $config = AppConfig->new(
32 ARGCOUNT => ARGCOUNT_ONE,
35 qw(irc_nick irc_username irc_desc
36 irc_server irc_port irc_password
37 au_subscriber_id use_cookie
38 web_port web_title web_lines web_root
39 web_username web_password show_newmsgonly)
42 $config->file('/etc/keitairc');
43 $config->file($ENV{'HOME'} . '/.keitairc');
47 if(defined $config->web_root){
48 $docroot = $config->web_root;
51 # join
\e$B$7$F$$$k%A%c%M%k$NL>>N$r5-O?$9$k%O%C%7%e
\e(B
54 # join
\e$B$7$F$$$k%A%c%M%k$NL>>N$r5-O?$9$k%O%C%7%e
\e(B
57 #
\e$B%A%c%M%k$N2qOCFbMF$r5-O?$9$k%O%C%7%e
\e(B
58 my (%channel_buffer, %channel_recent);
60 #
\e$B3F%A%c%M%k$N:G=*%"%/%;%9;~9o!":G?7H/8@;~9o
\e(B
72 POE::Component::IRC->new('keitairc');
74 _start => \&on_irc_start,
75 irc_join => \&on_irc_join,
76 irc_part => \&on_irc_part,
77 irc_public => \&on_irc_public,
78 irc_notice => \&on_irc_notice,
79 irc_topic => \&on_irc_topic,
80 irc_332 => \&on_irc_topicraw,
81 irc_ctcp_action => \&on_irc_ctcp_action,
84 # web server component
85 POE::Component::Server::TCP->new(
87 Port => $config->web_port,
88 ClientFilter => 'POE::Filter::HTTPD',
89 ClientInput => \&on_web_request
95 ################################################################
97 my $kernel = $_[KERNEL];
98 $kernel->post('keitairc' => 'register' => 'all');
99 $kernel->post('keitairc' => 'connect' => {
100 Nick => $config->irc_nick,
101 Username => $config->irc_username,
102 Ircname => $config->irc_desc,
103 Server => $config->irc_server,
104 Port => $config->irc_port,
105 Password => $config->irc_password
109 ################################################################
111 my ($kernel, $who, $channel) = @_[KERNEL, ARG0, ARG1];
114 # chop off after the gap (bug workaround of madoka)
116 my $canon_channel = &canon_name($channel);
118 $channel_name{$canon_channel} = $channel;
119 unless ($who eq $config->irc_nick) {
120 &add_message($channel, undef, "$who joined");
124 ################################################################
126 my ($kernel, $who, $channel) = @_[KERNEL, ARG0, ARG1];
129 # chop off after the gap (bug workaround of POE::Filter::IRC)
131 my $canon_channel = &canon_name($channel);
133 if ($who eq $config->irc_nick) {
134 delete $channel_name{$canon_channel};
136 &add_message($channel, undef, "$who leaves");
140 ################################################################
142 my ($kernel, $who, $channel, $msg) = @_[KERNEL, ARG0 .. ARG2];
144 $channel = $channel->[0];
145 $msg = Jcode->new($msg, 'jis')->euc;
146 &add_message($channel, $who, $msg);
149 ################################################################
151 my ($kernel, $who, $channel, $msg) = @_[KERNEL, ARG0 .. ARG2];
153 $channel = $channel->[0];
154 $msg = Jcode->new($msg, 'jis')->euc;
155 &add_message($channel, $who, $msg);
158 ################################################################
160 my ($kernel, $who, $channel, $topic) = @_[KERNEL, ARG0 .. ARG2];
162 $topic = Jcode->new($topic, 'jis')->euc;
163 &add_message($channel, undef, "$who set topic: $topic");
164 $topic{&canon_name($channel)} = $topic;
167 ################################################################
169 my ($kernel, $raw) = @_[KERNEL, ARG1];
170 my ($channel, $topic) = split(/ :/, $raw, 2);
171 $topic{&canon_name($channel)} = $topic;
174 ################################################################
175 sub on_irc_ctcp_action{
176 my ($kernel, $who, $channel, $msg) = @_[KERNEL, ARG0 .. ARG2];
178 $channel = $channel->[0];
179 $msg = sprintf('* %s %s', $who, Jcode->new($msg, 'jis')->euc);
180 &add_message($channel, '', $msg);
183 ################################################################
184 # $msg
\e$B$O
\e(B EUC
\e$B$K$J$C$F$$$k$O$:
\e(B
185 # $channel
\e$B$O
\e(B jis
\e$B$G$-$F$k$>
\e(B
187 my($channel, $who, $msg) = @_;
191 $message = sprintf('%s %s> %s', &now, $who, $msg);
193 $message = sprintf('%s %s', &now, $msg);
196 my $canon_channel = &canon_name($channel);
197 my @tmp = split("\n", $channel_buffer{$canon_channel});
200 my @tmp2 = split("\n", $channel_recent{$canon_channel});
201 push @tmp2, $message;
203 if(@tmp > $config->web_lines){
204 $channel_buffer{$canon_channel} =
205 join("\n", splice(@tmp, -$config->web_lines));
207 $channel_buffer{$canon_channel} = join("\n", @tmp);
210 if(@tmp2 > $config->web_lines){
211 $channel_recent{$canon_channel} =
212 join("\n", @tmp2[1 .. $config->web_lines]);
214 $channel_recent{$canon_channel} = join("\n", @tmp2);
217 $mtime{$canon_channel} = time;
220 $unread_lines{$canon_channel} = scalar(@tmp2);
222 if($unread_lines{$canon_channel} > $config->web_lines){
223 $unread_lines{$canon_channel} = $config->web_lines;
227 ################################################################
229 my ($sec,$min,$hour) = localtime(time);
230 sprintf('%02d:%02d', $hour, $min);
233 ################################################################
242 ################################################################
244 my $accesskey = shift;
247 sprintf('%d ', $accesskey);
253 ################################################################
259 for my $canon_channel (sort {
260 $mtime{$b} <=> $mtime{$a};
261 }(keys(%channel_name))){
262 $channel = $channel_name{$canon_channel};
264 $buf .= &label($accesskey);
267 $buf .= sprintf('<a accesskey="%1d" href="%s%s">%s</a>',
270 uri_escape($channel),
271 &compact_channel_name($channel));
273 $buf .= sprintf('<a href="%s%s">%s</a>',
275 uri_escape($channel),
276 &compact_channel_name($channel));
282 if($unread_lines{$canon_channel}){
283 $buf .= sprintf(' <a href="%s%s,recent">%s</a>',
285 uri_escape($channel),
286 $unread_lines{$canon_channel});
291 $buf .= qq(0 <a href="$docroot" accesskey="0">refresh list</a><br>);
293 if(grep($unread_lines{$_}, keys %unread_lines)){
294 $buf .= qq(* <a href="$docroot,recent" accesskey="*">recent</a><br>);
298 $buf .= qq(# <a href="$docroot,topics" accesskey="#">topics</a><br>);
301 $buf .= qq( - keitairc $version);
305 ################################################################
306 #
\e$B%A%c%M%kL>>N$rC;$+$/$9$k
\e(B
307 sub compact_channel_name{
310 # #name:*.jp
\e$B$r
\e(B %name
\e$B$K
\e(B
315 #
\e$BKvHx$NC1FH$N
\e(B @
\e$B$O<h$k
\e(B (for multicast.plm)
321 ################################################################
325 tr/A-Z[\\]^/a-z{|}~/;
330 ################################################################
335 my @src = (reverse(split("\n", shift)))[0 .. $config->web_lines];
343 unless(s,\b(https?://[!-;=-\177]+)\b,<a href="$1">$1</a>,g){
344 unless(s|\b(www\.[!-\177]+)\b|<a href="http://$1">$1</a>|g){
346 unless(s|\b(0\d{1,3})([-(]?)(\d{2,4})([-)]?)(\d{4})\b|<a href="tel:$1$3$5">$1$2$3$4$5</a>|g){
347 s|\b(\w[\w.+=-]+\@[\w.-]+[\w]\.[\w]{2,4})\b|<a href="mailto:$1">$1</a>|g;
357 '<pre>' . join("\n", @buf) . '</pre>';
360 ################################################################
362 my ($kernel, $heap, $request) = @_[KERNEL, HEAP, ARG0];
364 # Filter::HTTPD sometimes generates HTTP::Response objects.
365 # They indicate (and contain the response for) errors that occur
366 # while parsing the client's HTTP request. It's easiest to send
367 # the responses as they are and finish up.
368 if($request->isa('HTTP::Response')){
369 $heap->{client}->put($request);
370 $kernel->yield('shutdown');
375 my $cookie_authorized;
376 if($config->use_cookie){
378 for(split(/; */, $request->header('Cookie'))){
379 my ($name, $value) = split(/=/);
380 $value =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack('C', hex($1))/eg;
381 $cookie{$name} = $value;
384 if($cookie{username} eq $config->web_username &&
385 $cookie{passwd} eq $config->web_password){
386 $cookie_authorized = true;
391 unless($cookie_authorized){
392 unless(defined($config->au_subscriber_id) &&
393 $request->header('x-up-subno') eq $config->au_subscriber_id){
394 if(defined($config->web_username)){
395 unless($request->headers->authorization_basic eq
396 $config->web_username . ':' . $config->web_password){
397 my $response = HTTP::Response->new(401);
398 $response->push_header(WWW_Authenticate =>
399 qq(Basic Realm="keitairc"));
400 $heap->{client}->put($response);
401 $kernel->yield('shutdown');
408 my $uri = $request->uri;
409 my $content = '<html><head>';
410 $content .= '<meta http-equiv="Cache-Control" content="max-age=0" />';
412 # POST
\e$B$5$l$F$-$?$b$N$OH/8@
\e(B
413 if($request->method =~ /POST/i){
414 my $message = $request->content;
416 $message =~ s/\+/ /g;
417 $message = uri_unescape($message);
419 if(length($message)){
421 my $channel = uri_unescape($uri);
422 $poe_kernel->post('keitairc',
424 Jcode->new($channel)->jis,
425 Jcode->new($message)->jis);
426 &add_message($channel, $config->irc_nick,
427 Jcode->new($message)->euc);
428 $message_added = true;
432 # store and remove attached options from uri
435 my @opts = split(',', $uri);
437 grep($option{$_} = $_, @opts);
442 $content .= '<title>' . $config->web_title . '</title>';
443 $content .= '</head>';
444 $content .= '<body>';
447 # recent messages on every channel
448 for my $canon_channel (sort keys %channel_name){
449 my $channel = $channel_name{$canon_channel};
450 if(length($channel) &&
451 length($channel_recent{$canon_channel})){
452 $content .= '<b>' . Jcode->new($channel_name{$canon_channel})->euc . '</b>';
453 $content .= sprintf(' <a href="%s%s">more..</a><br>',
454 $docroot, uri_escape($channel));
455 $content .= &render($channel_recent{$canon_channel});
456 $unread_lines{$canon_channel} = 0;
457 $channel_recent{$canon_channel} = '';
461 $content .= qq(<a accesskey="8" href="$docroot">ch list[8]</a>);
462 }elsif($option{topics}){
463 # topic on every channel
464 for my $canon_channel (sort keys %channel_name){
465 my $channel = $channel_name{$canon_channel};
467 $content .= sprintf(' <a href="%s%s">%s</a><br>',
468 $docroot, uri_escape($channel),
469 Jcode->new($channel_name{$canon_channel})->euc);
470 $content .= &escape(Jcode->new($topic{$canon_channel})->euc);
474 $content .= qq(<br><a accesskey="8" href="$docroot">ch list[8]</a>);
477 $content .= &index_page;
480 # channel conversation
484 # Apart from the the requirement that the first character
485 # being either '&', '#', '+' or '!' (hereafter called "channel
486 # prefix"). The only restriction on a channel name is that it
487 # SHALL NOT contain any spaces (' '), a control G (^G or ASCII
488 # 7), a comma (',' which is used as a list item separator by
489 # the protocol). Also, a colon (':') is used as a delimiter
490 # for the channel mask. The exact syntax of a channel name is
491 # defined in "IRC Server Protocol" [IRC-SERVER].
493 # so we use white space as separator character of channel name
494 # and command argument.
496 my $channel = uri_unescape($uri);
498 $content .= '<title>' . $config->web_title . ": $channel</title>";
499 $content .= '</head>';
500 $content .= '<body>';
502 $content .= '<a name="1"></a>';
503 $content .= '<a accesskey="7" href="#1"></a>';
505 $content .= sprintf('<form action="%s%s" method="post">',
506 $docroot, uri_escape($channel));
507 $content .= '<input type="text" name="m" size="10">';
508 $content .= '<input type="submit" accesskey="1" value="OK[1]">';
509 $content .= qq(<a accesskey="8" href="$docroot">ch list[8]</a><br>);
510 $content .= '</form>';
512 my $canon_channel = &canon_name($channel);
513 if(defined($channel_name{$canon_channel})){
514 if(defined($channel_buffer{$canon_channel}) &&
515 length($channel_buffer{$canon_channel})){
516 $content .= '<a accesskey="9" href="#2"></a>';
517 if($option{recent} ||
518 (defined($config->show_newmsgonly) && $message_added)){
519 $content .= &render($channel_recent{$canon_channel});
520 $content .= sprintf('<a accesskey="5" href="%s%s">more[5]</a>',
521 $docroot, uri_escape($channel));
523 $content .= &render($channel_buffer{$canon_channel});
525 $content .= '<a accesskey="9" href="#2"></a>';
526 $content .= '<a name="2"></a>';
528 $content .= 'no message here yet';
531 $content .= 'no such channel';
535 $message_added = false;
537 # clear unread counter
538 $unread_lines{$canon_channel} = 0;
540 # clear recent messages buffer
541 $channel_recent{$canon_channel} = '';
544 $content .= '</body></html>';
546 my $response = HTTP::Response->new(200);
548 if($config->use_cookie){
549 my ($sec, $min, $hour, $mday, $mon, $year, $wday) =
550 localtime(time + cookie_ttl);
552 sprintf('%.3s, %.2d-%.3s-%.4s %.2d:%.2d:%.2d',
553 qw(Sun Mon Tue Wed Thu Fri Sat)[$wday],
555 qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$mon],
560 $response->push_header('Set-Cookie',
561 sprintf("username=%s; expires=%s; \n",
562 $config->web_username, $expiration));
563 $response->push_header('Set-Cookie',
564 sprintf("passwd=%s; expires=%s; \n",
565 $config->web_password, $expiration));
568 $response->push_header('Content-type', 'text/html; charset=Shift_JIS');
569 $response->content(Jcode->new($content)->sjis);
570 $heap->{client}->put($response);
571 $kernel->yield('shutdown');