summaryrefslogtreecommitdiffstats
path: root/util/waterball.pl
blob: 2cfa2a61f504b8e1a9152b3fbd7814825673e58c (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/perl
use lib '/home/bbs/bin/';
use LocalVars;
use Time::Local;
use POSIX;
use FileHandle;
use strict;
use Mail::Sender;

my($fndes, $fnsrc, $userid, $mailto, $outmode);
foreach $fndes ( <$JOBSPOOL/water.des.*> ){ #des: userid, mailto, outmode
    (open FH, "< $fndes") or next;
    chomp($userid = <FH>);
    chomp($mailto = <FH>);
    chomp($outmode= <FH>);
    close FH;
    next if( !$userid );
    print "$userid, $mailto, $outmode\n";
    `rm -Rf $TMP/water`;
    `mkdir $TMP/water`;

    $fnsrc = $fndes;
    $fnsrc =~ s/\.des\./\.src\./;
    eval{
    process($fnsrc, "$TMP/water/", $outmode, $userid);
    };
    if( $@ ){
    print "$@\n";
    }
    else{
    chdir "$TMP/water";
    if( $mailto eq '.' || $mailto =~ /\.bbs/ ){
        $mailto = "$userid.bbs\@$hostname" if( $mailto eq '.' );
        foreach my $fn ( <$TMP/water/*> ){
        my $who = substr($fn, rindex($fn, '/') + 1);
        my $content = '';
        open FH, "< $fn";while( <FH> ){chomp;$content .= "$_\n";}
        if( !MakeMail({mailto  => $mailto,
                   subject => "和 $who 的水球記錄",
                   body    => $content,
               }) ){ print "fault\n"; }
        }
        unlink $fnsrc;
        unlink $fndes;
    }
    else{
        if( MakeMail({tartarget => "$TMP/$userid.waterball.tgz",
              tarsource => "*",
              mailto    => "$userid <$mailto>",
              subject   => "水球紀錄",
              body      => 
                  "\n $hostname 站長群 ". POSIX::ctime(time())}
             ) ){
        unlink $fnsrc;
        unlink $fndes;
        }
    }
    }
}

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

    if( ! $FH{$who} ){
        $FH{$who} = new FileHandle "> $outdir/$who";
    }
    if( $outmode == 0 ){
        next if( $say =~ /<<下站通知>> -- 我走囉!/ ||
             $say =~ /<<上站通知>> -- 我來啦!/    );
        if( $time - $LAST{$who} > 1800 ){
        if( $LAST{$who} != 0 ){
            ($FH{$who})->print( POSIX::ctime($LAST{$who}) , "\n");
        }
        ($FH{$who})->print( POSIX::ctime($time) );
        $LAST{$who} = $time;
        }
        $len = (length($who) > length($me) ? length($who) : length($me))+1;
        ($FH{$who})->printf("%-${len}s %s\n", ($cmode?$who:$me).':', $say);
    }
    elsif( $outmode == 1 ){
        ($FH{$who})->print("$orig\n");
    }
    }
    if( $outmode == 0 ){
    foreach( keys %FH ){
        ($FH{$_})->print( POSIX::ctime($LAST{$_}) );
    }
    }
    foreach( keys %FH ){
    ($FH{$_})->close();
    }
    close DIN;
}

sub parse
{
    my $dat = $_[0];
    my($cmode, $who, $year, $month, $day, $hour, $min, $sec, $say);
    if( $dat =~ /^To/ ){
    $cmode = 0;
    ($who, $say, $month, $day, $year, $hour, $min, $sec) =
        $dat =~ m|^To (\w+):\s*(.*)\[(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)\]|;
    }
    else{
    $cmode = 1;
    ($who, $say, $month, $day, $year, $hour, $min, $sec) =
        $dat =~ m|(\w+?)\[37;45m\s*(.*)\[m \[0m\[(\w+)/(\w+)/(\w+) (\w+):(\w+):(\w+)\]|;

    }
#    $time = timelocal($sec,$min,$hours,$mday,$mon,$year);

    return undef if( $month == 0 );
    return ($cmode, $who, timelocal($sec, $min, $hour, $day, $month - 1, $year), $say, $_[0]);
}

sub MakeMail
{
    my($arg) = @_;
    my $sender;
    `$TAR zcf $arg->{tartarget} $arg->{tarsource}`
    if( $arg->{tarsource} );
    $sender = new Mail::Sender{smtp => $SMTPSERVER,
                   from => "$hostname水球整理程式 <$userid.bbs\@$MYHOSTNAME>"};
    foreach( 0..3 ){
    if( (!$arg->{tartarget} &&
         $sender->MailMsg({to      => $arg->{mailto},
                   subject => $arg->{subject},
                   msg     => $arg->{body}
               }) ) ||
        ($arg->{tartarget} && 
         $sender->MailFile({to      => $arg->{mailto},
                subject => $arg->{subject},
                msg     => $arg->{body},
                file    => $arg->{tartarget}})) ){
        unlink $arg->{tartarget} if( $arg->{tartarget} );
        return 1;
        }
    }
    $sender->MailMsg({to      => "$userid.bbs\@$MYHOSTNAME",
              subject => "無法寄出水球整理",
              msg     =>
              "親愛的使用者您好\n\n".
              "你的水球整理記錄無法寄達指定位置 $mailto \n\n".
              "$hostname站長群 敬上 ".POSIX::ctime(time())});
    unlink $arg->{tartarget} if( $arg->{tartarget} );
    return 1;
}