OSDN Git Service

document update
[keitairc/keitairc.git] / keitairc
index 458722e..285b844 100755 (executable)
--- a/keitairc
+++ b/keitairc
@@ -1,6 +1,6 @@
 #!/usr/bin/perl
 # keitairc
-# $Id: keitairc,v 1.6 2004-03-21 11:02:27 morimoto Exp $
+# $Id: keitairc,v 1.16 2004-03-23 16:12:56 ishikawa Exp $
 #
 # Copyright (c) 2003 Jun Morimoto <morimoto@xantia.citroen.org>
 # This program is covered by the GNU General Public License 2
@@ -8,15 +8,15 @@
 # Depends: libjcode-pm-perl, libpoe-component-irc-perl,
 #   liburi-perl, libwww-perl, libappconfig-perl
 
-my $rcsid = q$Id: keitairc,v 1.6 2004-03-21 11:02:27 morimoto Exp $;
+my $rcsid = q$Id: keitairc,v 1.16 2004-03-23 16:12:56 ishikawa Exp $;
 my ($version) = $rcsid =~ m#,v ([0-9.]+)#;
 
 use strict;
 use Jcode;
 use POE;
 use POE::Component::Server::TCP;
-use POE::Component::IRC;
 use POE::Filter::HTTPD;
+use POE::Component::IRC;
 use URI::Escape;
 use HTTP::Response;
 use AppConfig qw(:argcount);
@@ -47,10 +47,16 @@ if(defined $config->web_root){
 my %channel_name;
 
 # \e$B%A%c%M%k$N2qOCFbMF$r5-O?$9$k%O%C%7%e\e(B
-my %channel_buffer;
+my (%channel_buffer, %channel_recent);
 
 # \e$B3F%A%c%M%k$N:G=*%"%/%;%9;~9o!":G?7H/8@;~9o\e(B
-my (%atime, %mtime);
+my %mtime;
+
+# unread lines
+my %unread;
+
+# chk
+my ($send_chk, $update_chk);
 
 # irc component
 POE::Component::IRC->new('keitairc');
@@ -96,11 +102,16 @@ sub on_irc_join{
 ################################################################
 sub on_irc_part{
     my ($kernel, $who, $channel) = @_[KERNEL, ARG0, ARG1];
+    $who =~ s/!.*//;
 
     # chop off after the gap (bug workaround of POE::Filter::IRC)
     $channel =~ s/ .*//;
 
-    delete $channel_name{$channel};
+    if ($who eq $config->irc_nick) {
+       delete $channel_name{$channel};
+    } else {
+       &add_message($channel, 'SYSOP', $who . ' leaves');
+    }
 }
 
 ################################################################
@@ -127,16 +138,36 @@ sub on_irc_notice{
 sub add_message{
     my($channel, $who, $msg) = @_;
 
+    my $message = sprintf('%s %s> %s', &now, $who, $msg);
+
     my @tmp = split("\n", $channel_buffer{$channel});
-    push @tmp, sprintf('%s %s> %s', &now, $who, $msg);
+    push @tmp, $message;
+
+    my @tmp2 = split("\n", $channel_recent{$channel});
+    push @tmp2, $message;
 
     if(@tmp > $config->web_lines){
-       $channel_buffer{$channel} = join("\n", splice(@tmp, -$config->web_lines));
+       $channel_buffer{$channel} =
+               join("\n", splice(@tmp, -$config->web_lines));
     }else{
        $channel_buffer{$channel} = join("\n", @tmp);
     }
 
+    if(@tmp2 > $config->web_lines){
+       $channel_recent{$channel} =
+               join("\n", splice(@tmp2, -$config->web_lines));
+    }else{
+       $channel_recent{$channel} = join("\n", @tmp2);
+    }
+
     $mtime{$channel} = time;
+
+    # unread lines
+    $unread{$channel} = @tmp2;
+
+    if ($unread{$channel} > $config->web_lines) {
+       $unread{$channel} = $config->web_lines;
+    }
 }
 
 ################################################################
@@ -176,35 +207,33 @@ sub index_page{
 
        $buf .= &label($accesskey);
 
-       unless(defined($channel_buffer{$channel})){
-           $buf .= &compact_channel_name($channel);
-       }else{
-           if($accesskey < 10){
+       if($accesskey < 10){
                $buf .= sprintf('<a accesskey="%1d" href="%s%s">%s</a>',
-                               $accesskey,
+                               $accesskey,
                                $docroot,
                                uri_escape($channel),
                                &compact_channel_name($channel));
-           }else{
-               $buf .= sprintf('<a href="%docroot%s">%s</a>',
+       }else{
+               $buf .= sprintf('<a href="%s%s">%s</a>',
                                $docroot,
                                uri_escape($channel),
                                &compact_channel_name($channel));
-           }
        }
 
        $accesskey++;
 
-       if($atime{$channel} < $mtime{$channel}){
-           $buf .= '*';
+       # \e$BL$FI9T?t\e(B
+       if($unread{$channel} > 0){
+               $buf .= sprintf(' <a href="%s%s.update">%d</a>',
+                               $docroot,
+                               uri_escape($channel),
+                               $unread{$channel});
        }
-
        $buf .= '<br>';
     }
 
-    $buf .= qq(<a href="$docroot" accesskey="0"></a>);
+    $buf .= qq(0 <a href="$docroot" accesskey="0">refresh list</a><br>);
     $buf .= qq( - keitairc $version);
-
     $buf;
 }
 
@@ -283,6 +312,7 @@ sub on_web_request{
 
     my $uri = $request->uri;
     my $content = '<html><head>';
+    $content .= '<meta http-equiv="Cache-Control" content="max-age=0" />';
 
     # POST \e$B$5$l$F$-$?$b$N$OH/8@\e(B
     if($request->method =~ /POST/i){
@@ -300,18 +330,29 @@ sub on_web_request{
                              Jcode->new($message)->jis);
            &add_message($channel, $config->irc_nick,
                         Jcode->new($message)->euc);
+
+           # set flag for "send message"
+           $send_chk = 1;
        }
     }
 
     if($uri eq '/'){
        $content .= '<title>' . $config->web_title . '</title>';
+       $content .= '</head>';
        $content .= '<body>';
        $content .= &index_page;
     }else{
        $uri =~ s|^/||;
+
+       $update_chk = ($uri =~ /.*.update/);
+       if ($update_chk eq 1) {
+               $uri =~ s/.update//;
+       }
+
        my $channel = uri_unescape($uri);
 
        $content .= '<title>' . $config->web_title . ": $channel</title>";
+       $content .= '</head>';
        $content .= '<body>';
 
        $content .= '<a name="1"></a>';
@@ -320,16 +361,22 @@ sub on_web_request{
        $content .= sprintf('<form action="%s%s" method="post">',
                            $docroot, $uri);
        $content .= '<input type="text" name="m" size="10">';
-       $content .= '<input type="submit" accesskey="1" value="OK">';
+       $content .= '<input type="submit" accesskey="1" value="OK[1]">';
+        $content .= qq(<a accesskey="8" href="$docroot">back[8]</a><br>);
        # $content .= '<input type="submit" accesskey="1" value="&#63920;">';
        $content .= '</form>';
 
        if(defined($channel_name{$channel})){
            if(defined($channel_buffer{$channel}) &&
               length($channel_buffer{$channel})){
-               $content .= qq(<a accesskey="8" href="$docroot"></a>);
                $content .= '<a accesskey="9" href="#2"></a>';
-               $content .= &render($channel_buffer{$channel});
+               if (($update_chk eq 1)||($send_chk eq 1)) {
+                 $content .= &render($channel_recent{$channel});
+                 $content .= sprintf('<a accesskey="5" href="%s%s">
+                       ..more[5]</a>', $docroot, uri_escape($channel));
+               } else {
+                 $content .= &render($channel_buffer{$channel});
+               }
                $content .= '<a name="2"></a>';
            }else{
                $content .= 'no message here yet';
@@ -338,7 +385,14 @@ sub on_web_request{
            $content .= "no such channel";
        }
 
-       $atime{$channel} = time;
+       # clear check flags
+       $send_chk = 0;
+
+       # clear unread counter
+        $unread{$channel} = 0;
+
+       # clear recent messages buffer
+       $channel_recent{$channel} = '';
     }
 
     $content .= '</body></html>';