3 # $Id: keitairc,v 1.9 2004-03-21 11:03:19 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.9 2004-03-21 11:03:19 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 my $config = AppConfig->new(
28 ARGCOUNT => ARGCOUNT_ONE,
31 qw(irc_nick irc_username irc_desc
32 irc_server irc_port irc_password
33 web_port web_title web_lines web_root
34 web_username web_password)
37 $config->file('/etc/keitairc');
38 $config->file($ENV{'HOME'} . '/.keitairc');
42 if(defined $config->web_root){
43 $docroot = $config->web_root;
46 # join
\e$B$7$F$$$k%A%c%M%k$NL>>N$r5-O?$9$k%O%C%7%e
\e(B
49 #
\e$B%A%c%M%k$N2qOCFbMF$r5-O?$9$k%O%C%7%e
\e(B
52 #
\e$B3F%A%c%M%k$N:G=*%"%/%;%9;~9o!":G?7H/8@;~9o
\e(B
59 POE::Component::IRC->new('keitairc');
61 _start => \&on_irc_start,
62 irc_join => \&on_irc_join,
63 irc_part => \&on_irc_part,
64 irc_public => \&on_irc_public,
65 irc_notice => \&on_irc_notice,
68 # web server component
69 POE::Component::Server::TCP->new(
71 Port => $config->web_port,
72 ClientFilter => 'POE::Filter::HTTPD',
73 ClientInput => \&on_web_request
79 ################################################################
81 my $kernel = $_[KERNEL];
82 $kernel->post('keitairc' => 'register' => 'all');
83 $kernel->post('keitairc' => 'connect' => {
84 Nick => $config->irc_nick,
85 Username => $config->irc_username,
86 Ircname => $config->irc_desc,
87 Server => $config->irc_server,
88 Port => $config->irc_port,
89 Password => $config->irc_password
93 ################################################################
95 my ($kernel, $who, $channel) = @_[KERNEL, ARG0, ARG1];
96 $channel_name{$channel}++;
99 ################################################################
101 my ($kernel, $who, $channel) = @_[KERNEL, ARG0, ARG1];
103 # chop off after the gap (bug workaround of POE::Filter::IRC)
106 delete $channel_name{$channel};
109 ################################################################
111 my ($kernel, $who, $channel, $msg) = @_[KERNEL, ARG0 .. ARG2];
113 $channel = $channel->[0];
114 $msg = Jcode->new($msg, 'jis')->euc;
115 &add_message($channel, $who, $msg);
118 ################################################################
120 my ($kernel, $who, $channel, $msg) = @_[KERNEL, ARG0 .. ARG2];
122 $channel = $channel->[0];
123 $msg = Jcode->new($msg, 'jis')->euc;
124 &add_message($channel, $who, $msg);
127 ################################################################
128 # $msg
\e$B$O
\e(B EUC
\e$B$K$J$C$F$$$k$O$:
\e(B
129 # $channel
\e$B$O
\e(B jis
\e$B$G$-$F$k$>
\e(B
131 my($channel, $who, $msg) = @_;
133 my @tmp = split("\n", $channel_buffer{$channel});
134 push @tmp, sprintf('%s %s> %s', &now, $who, $msg);
136 if(@tmp > $config->web_lines){
137 $channel_buffer{$channel} = join("\n", splice(@tmp, -$config->web_lines));
139 $channel_buffer{$channel} = join("\n", @tmp);
142 $mtime{$channel} = time;
144 if ($unread{$channel} > $config->web_lines) {
145 $unread{$channel} = $config->web_lines;
149 ################################################################
151 my ($sec,$min,$hour) = localtime(time);
152 sprintf('%02d:%02d', $hour, $min);
155 ################################################################
164 ################################################################
166 my $accesskey = shift;
169 sprintf('%d ', $accesskey);
175 ################################################################
180 for my $channel (sort {
181 $mtime{$b} <=> $mtime{$a};
182 }(keys(%channel_name))){
184 $buf .= &label($accesskey);
187 $buf .= sprintf('<a accesskey="%1d" href="%s%s">%s</a>',
190 uri_escape($channel),
191 &compact_channel_name($channel));
193 $buf .= sprintf('<a href="%s%s">%s</a>',
195 uri_escape($channel),
196 &compact_channel_name($channel));
201 if($unread{$channel} > 0){
202 $buf .= sprintf(' (%d)', $unread{$channel});
208 $buf .= qq(<a href="$docroot" accesskey="0"></a>);
209 $buf .= qq( - keitairc $version +);
214 ################################################################
215 #
\e$B%A%c%M%kL>>N$rC;$+$/$9$k
\e(B
216 sub compact_channel_name{
219 # #name:*.jp
\e$B$r
\e(B %name
\e$B$K
\e(B
224 #
\e$BKvHx$NC1FH$N
\e(B @
\e$B$O<h$k
\e(B (for multicast.plm)
230 ################################################################
235 my @src = (reverse(split("\n", shift)))[0 .. $config->web_lines];
243 unless(s,(http://[!-;=-\177]+),<a href="$1">$1</a>,g){
244 unless(s|(www\.[!-\177]+)|<A HREF="http://$1">$1</A>|g){
246 unless(s|(0\d{1,3}[-(]?\d{2,4}[-)]?\d{4})|<a href="tel:$1">$1</a>|g){
247 s|(\w[\w.+=-]+\@[\w.-]+[\w])|<a href="mailto:$1">$1</a>|g;
257 '<pre>' . join("\n", @buf) . '</pre>';
260 ################################################################
262 my ($kernel, $heap, $request) = @_[KERNEL, HEAP, ARG0];
264 # Filter::HTTPD sometimes generates HTTP::Response objects.
265 # They indicate (and contain the response for) errors that occur
266 # while parsing the client's HTTP request. It's easiest to send
267 # the responses as they are and finish up.
268 if($request->isa('HTTP::Response')){
269 $heap->{client}->put($request);
270 $kernel->yield('shutdown');
274 if(defined($config->web_username)){
275 unless($request->headers->authorization_basic eq
276 $config->web_username . ':' . $config->web_password){
278 my $response = HTTP::Response->new(401);
279 $response->push_header(WWW_Authenticate =>
280 qq(Basic Realm="keitairc"));
281 $heap->{client}->put($response);
282 $kernel->yield('shutdown');
287 my $uri = $request->uri;
288 my $content = '<html><head>';
290 # POST
\e$B$5$l$F$-$?$b$N$OH/8@
\e(B
291 if($request->method =~ /POST/i){
292 my $message = $request->content;
294 $message =~ s/\+/ /g;
295 $message = uri_unescape($message);
297 if(length($message)){
299 my $channel = uri_unescape($uri);
300 $poe_kernel->post('keitairc',
302 Jcode->new($channel)->jis,
303 Jcode->new($message)->jis);
304 &add_message($channel, $config->irc_nick,
305 Jcode->new($message)->euc);
310 $content .= '<title>' . $config->web_title . '</title>';
311 $content .= '</head>';
312 $content .= '<body>';
313 $content .= &index_page;
316 my $channel = uri_unescape($uri);
318 $content .= '<title>' . $config->web_title . ": $channel</title>";
319 $content .= '<body>';
321 $content .= '<a name="1"></a>';
322 $content .= '<a accesskey="7" href="#1"></a>';
323 $content .= '<a accesskey="8" href="../"></a>';
325 $content .= sprintf('<form action="%s%s" method="post">',
327 $content .= '<input type="text" name="m" size="10">';
328 $content .= '<input type="submit" accesskey="1" value="OK">';
329 # $content .= '<input type="submit" accesskey="1" value="聆">';
330 $content .= '</form>';
331 $content .= '<a href="../">..back (push 8)</a><BR>';
333 if(defined($channel_name{$channel})){
334 if(defined($channel_buffer{$channel}) &&
335 length($channel_buffer{$channel})){
336 $content .= qq(<a accesskey="8" href="$docroot"></a>);
337 $content .= '<a accesskey="9" href="#2"></a>';
338 $content .= &render($channel_buffer{$channel});
339 $content .= '<a name="2"></a>';
341 $content .= 'no message here yet';
344 $content .= "no such channel";
347 $unread{$channel} = 0;
350 $content .= '</body></html>';
352 my $response = HTTP::Response->new(200);
353 $response->push_header('Content-type', 'text/html; charset=Shift_JIS');
354 $response->content(Jcode->new($content)->sjis);
355 $heap->{client}->put($response);
356 $kernel->yield('shutdown');