summaryrefslogtreecommitdiffstats
path: root/util/waterball.pl
blob: 3f9f8f2f88ded6fb968c11c9caca6adb0ddaebcc (plain) (blame)
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
#!/usr/bin/perl
# $Id$
use lib '/home/bbs/bin/';
use Time::Local;
use LocalVars;
use Mail::Sender;
use IO::All;
use strict;

my(%water);
sub main
{
    my($fndes, $userid, $mailto, $outmode, $fnsrc);
    chdir($BBSHOME);
    foreach $fndes ( <$JOBSPOOL/water.des.*> ){
    ($userid, $mailto, $outmode, $fnsrc) = parsedes($fndes);
    next if( !$userid || !-e $fnsrc );

    $mailto = "$userid.bbs\@$MYHOSTNAME" if( $mailto eq '.');
    undef %water;
    process($fnsrc, "$TMP/water/", $outmode, $userid);
    output("$userid.bbs\@$MYHOSTNAME", $mailto, $mailto =~ /\.bbs/);
    unlink($fndes, $fnsrc);
    }
}

sub parsedes($)
{
    my $t < io($_[0]);
    my $fnsrc = $_[0];
    $fnsrc =~ s/\.des\./\.src\./;
    return (split("\n", $t), $fnsrc);
}

sub process($$$$)
{
    my($fn, $outdir, $outmode, $me) = @_;
    my($cmode, $who, $time, $say, $orig, %LAST, $len) = ();
    open DIN, "<$fn";
    while( <DIN> ){
    next if( !(($cmode, $who, $time, $say, $orig) = parse($_)) || !$who );

    if( $outmode ){
        $water{$who} .= $orig;
    } else {
        next if( $say =~ /<<(上|下)站通知>> -- 我(走|來)囉!/ );
        if( $time - $LAST{$who} > 1800 ){
        $water{$who} .= (scalar localtime($LAST{$who}))."\n\n"
            if( $LAST{$who} );
        $water{$who} .= scalar localtime($time) . "\n";
        }
        
        $len = max(length($who), length($me)) + 1;
        $water{$who} .= sprintf("%-${len}s %s\n",
                    ($cmode ? $who : $me).':' ,
                    $say);
        $LAST{$who} = $time;
    }
    }
    if( $outmode == 0 ){
    $water{$_} .= scalar localtime($LAST{$_})
        foreach( keys %LAST );
    }
}

sub parse($)
{
    my($str) = @_;
    my($cmode, $who, $year, $month, $day, $hour, $min, $sec, $say);
    $cmode = ($str =~ /^To/) ? 0 : 1;
    ($who, $say, $month, $day, $year, $hour, $min, $sec) =
    $cmode ?
    $str =~ m|(.+?)\[37;45m\s*(.*).*?\[(\w+)/(\w+)/(\w+) (\w+):(\w+):(\w+)\]| :
    $str =~ m|^To (.+?):\s*(.*)\[(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)\]|;
    return ( !(1 <= $month && $month <= 12 &&
           1 <= $day   && $day   <= 31 &&
           0 <= $hour  && $hour  <= 23 &&
           0 <= $min   && $min   <= 59 &&
           0 <= $sec   && $sec   <= 59 &&
           1970 <= $year && $year <= 2038) ?
         () :
         ($cmode, $who,
          timelocal($sec, $min, $hour, $day, $month - 1, $year),
          $say, $_[0]) );
}

sub output
{
    my($from, $tomail, $bbsmail) = @_;
    my $ms = new Mail::Sender{smtp    => $SMTPSERVER,
                  from    => $from,
                  charset => 'big5'};

    foreach( keys %water ){
    $ms->MailMsg({to      => $tomail,
              subject => "和 $_ 的水球記錄",
              msg     => $water{$_}});
    }
}

sub max
{
    return $_[0] > $_[1] ? $_[0] : $_[1];
}

main();
1;