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

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

    print "($userid, $mailto, $outmode, $fnsrc)\n";
    undef %water;
    process($fnsrc, "$TMP/water/", $outmode, $userid);
    output($mailto eq '.' ? "$userid.bbs\@$MYHOSTNAME" : $mailto,
           $mailto eq '.' || $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) = @_;
    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|(\w+?)\[37;45m\s*(.*).*?\[(\w+)/(\w+)/(\w+) (\w+):(\w+):(\w+)\]| :
    $str =~ m|^To (\w+):\s*(.*)\[(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)\]|;
    return (!$month ? () :
        ($cmode, $who,
         timelocal($sec, $min, $hour, $day, $month - 1, $year),
         $say, $_[0]));
}

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

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

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

main();
1;