-
Notifications
You must be signed in to change notification settings - Fork 1
/
lister.pl
executable file
·157 lines (136 loc) · 4.39 KB
/
lister.pl
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
155
156
157
#!/usr/bin/perl
## $Id: lister.pl,v 1.23 2006/04/13 14:39:41 netch Exp $
## Lister is called periodically from cron and send notifications for
## new articles to users
use strict;
sub BEGIN {
my $mn_prefix;
if( ( $mn_prefix = $ENV{'MN_PREFIX'} ) ) {
unshift @INC, "$mn_prefix/lib/mailnews", "$mn_prefix/etc";
}
}
use DBI;
use Fcntl ':flock';
use Sys::Syslog qw( :DEFAULT setlogsock );
use mn_config;
use mn_subs;
use mn_intl;
## lister reads list of notifications, groups it per user and sends
## these notifications
&main();
## UNREACHED
die;
################################################################
## main()
sub main {
## make `use strict' happy
my( $dbh, $rc, $sth, $sth2, $user, $mail_started, $groupname, $artnum,
$size, $sender, $subject, $arts_shown, $sendmail_cmd, $currgroup,
$sz_list_now, $entry, $uopt_lhelp, $uopt_lang );
my @row;
my %users;
setlogsock('unix');
## Get lock. If we can't get lock, exit immediately
#- print STDERR "Try to open: $mn_config::cf_lister_lock_path\n";
open( LOCK, ">>$mn_config::cf_lister_lock_path" ) ||
die 'cannot open lock';
unless( flock( LOCK, LOCK_EX|LOCK_NB ) ) {
syslog( 'notice', 'Another lock is in action' );
exit(0);
}
$dbh = &dbhandle();
die unless $dbh;
## Tag all current data with our tag.
## All records added by feeder will have tag 0.
## We work only with records retagged by lister. Together with lock for
## lister, this keeps constant set of records during all this work.
$rc = $dbh->do( 'UPDATE list SET tag=1' );
die unless $rc;
## Select users
$sth = $dbh->prepare( 'SELECT DISTINCT email FROM list WHERE tag=1' );
die $dbh->errstr unless $sth;
$rc = $sth->execute();
die $dbh->errstr unless $rc;
exit(0) if $rc == 0;
%users = ();
while( ( @row = $sth->fetchrow_array ) ) {
$user = $row[0];
$users{$user} = 1;
}
#- $sth->finish();
## Iterate users
$sth = $dbh->prepare(
'SELECT groupname,artnum,size,sender,subject FROM list ' .
'WHERE email=? AND tag=1 ORDER BY concat(groupname,artnum)' );
die unless $sth;
$sth2 = $dbh->prepare( 'SELECT lhelp,lang FROM users WHERE email=?' );
die unless $sth2;
foreach $user ( sort keys %users ) {
$uopt_lhelp = 1;
$rc = $sth2->execute( $user );
die $dbh->errstr unless $rc;
die if $rc > 1;
if( $rc > 0 ) {
( $uopt_lhelp, $uopt_lang ) = $sth2->fetchrow_array;
}
$rc = $sth->execute( $user );
die $dbh->errstr unless $rc;
$mail_started = $sz_list_now = 0;
while( $rc > 0 && ( @row = $sth->fetchrow_array ) ) {
($groupname,$artnum,$size,$sender,$subject) = @row;
if( $mail_started && ( $arts_shown >= 500 || $sz_list_now >= 64000 ) )
{
## Close current letter and set flag to begin new one
close MAIL;
$mail_started = 0;
}
if( !$mail_started ) {
$sendmail_cmd = sprintf( "/usr/sbin/sendmail -f%s -oi -t -odq -oee",
&mn_subs::shellparseable( $mn_config::cf_server_errors_to ) );
open( MAIL, "|$sendmail_cmd" ) or die;
&mn_subs::mn_list_print_header( {
'addr' => $user,
'handle' => \*MAIL,
'flag_lister' => 1,
'lhelp' => $uopt_lhelp,
'lang' => $uopt_lang
} );
$mail_started = 1;
$arts_shown = $sz_list_now = 0;
$currgroup = "";
}
if( $currgroup ne $groupname ) {
print MAIL "\n" if $currgroup;
print MAIL "GROUP $groupname\n";
$currgroup = $groupname;
}
## Print article annotation. Only 'new' format as for old mailnews
$subject = &mn_intl::convert_cp( $subject, 'utf-8', 'koi8-u' );
$entry = &mn_subs::mn_list_format_entry(
$artnum, $size, $sender, $subject );
$sz_list_now += length( $entry );
print MAIL $entry;
}
if( $mail_started ) {
close MAIL;
}
## As info for this user is sent, delete it from base
$sth = $dbh->prepare( 'DELETE FROM list WHERE email=? AND tag=1' );
die $dbh->errstr unless $sth;
$rc = $sth->execute( $user );
die $dbh->errstr unless $rc;
}
$sth->finish();
$sth2->finish();
$dbh->disconnect();
exit(0);
}
################################################################
## dbhandle()
sub dbhandle {
return DBI->connect(
$mn_config::cf_db_handler,
$mn_config::cf_db_lister_user,
$mn_config::cf_db_lister_password,
{AutoCommit=>1} );
}