-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolt38.pl
executable file
·430 lines (352 loc) · 11.9 KB
/
colt38.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/local/bin/perl -w
#
# colt38.pl - A program to retromoderate the mn.* hierarchy and with
# a minimal amount of finangling retromoderate any newsgroup with the
# appropriate rule set.
#
# steve ulrich <[email protected]>
#
#
# The term colt38 came from a truly braindead posting made by a paranoiac
# by the name of Mike Schneider.
#
# this was initially supposed to run as a daemon. much of this code has
# been removed due to some bugs that i ran across but never bothered to
# fix. it's operation as a command line client is fully functional though.
use News::NNTPClient;
use File::Path;
# Define a Variety of site specific stuff here
$| = 1; # No Buffering
$debug = 0;
$ConfigFile = $ARGV[0];
my $NewsClient = "";
my %Settings = ();
my %lastArtCount = ();
my %GroupLimit = ();
# Define the SIGNAL's that we'll pay attention to
$SIG{HUP} = \&catchHUP; # Reread our configuration file and start again
$SIG{QUIT} = \&dieWGrace;
$SIG{INT} = \&dieWGrace;
$SIG{TERM} = \&dieWGrace;
$SIG{ALRM} = \&dieWGrace;
$SIG{KILL} = \&dieWGrace;
#-------------------------------------------------------------------
# Here are the guts of things.
#
# Open and redirect STDERR and STDOUT
# open (STDOUT, ">>logs/colt38.out") || die "Cannot redirect STDOUT";
# open (STDERR, ">>logs/colt38.err") || die "Cannot redirect STDERR";
open (CANLOG, ">>$Settings{CANCELLOG}") || die "Cannot open Cancel Log";
&mainLoop;
#-------------------------------------------------------------------
#
# mainLoop()
# Where all the action is Jackson
#
sub mainLoop {
# while () {
&initClient;
&getNewArtCount;
foreach $CheckGroup (sort(keys(%GrpsToCheck))) {
print STDERR "----*** Group: ", $CheckGroup, "\n" if $debug > 0;
&checkGroup($CheckGroup);
&writeGroupList;
}
&writeGroupList;
# sleep($Settings{SLEEPTIME});
# }
} # End of sub mainLoop
#-------------------------------------------------------------------
#
# initClient()
# Initialize the various and unsundry variables that will make life
# happy.
#
sub initClient {
%GrpsToCheck = (); # intialize the Groups to check hash
%Settings = &getSettings($ConfigFile);
%lastArtCount = &getGroupList($Settings{GROUPFILE});
%GroupLimit = &getGroupLimit($Settings{GROUPFILE});
print STDERR "Reading Configuration File: ", `date` if $debug > 0;
# Get the Cancel Message Body
$Settings{MESSAGE} = &getTemplate($Settings{MESSAGEFILE});
# We also need to check to make sure that we have a connection
# to the news server. If there isn't a connection or our NewsClient
# isn't happy we should sleep for a little while and attempt to connect
# again.
$NewsClient = new News::NNTPClient("$Settings{NEWSSERVER}",
"$Settings{NEWSPORT}");
} # End of sub initClient
# -------------------------------------------------------------------
#
# catchHUP()
#
# Catch the HUP signal. Basically reinitialize the variables and
# run again
#
sub catchHUP {
print STDERR "\nRestarting colt38\n";
&initClient;
print STDERR "\n--Rereading configuration file $ConfigFile\n" if $debug > 0;
&mainLoop;
}
# -------------------------------------------------------------------
#
# checkGroup(GroupName)
# Cycle through the group and look at the articles that have been
# posted since the last time we looked at this group.
#
sub checkGroup {
my ($Group) = @_;
my ($GrpFirst, $GrpLast) = $NewsClient->group($Group);
# Go to the last article that we've seen
my $StatArtID = $NewsClient->stat($lastArtCount{$Group});
print STDERR "StatArtID: ", $StatArtID, "\n" if $debug > 0;
my $MessageCounter = $lastArtCount{$Group};
print STDERR "Starting Article:-",$lastArtCount{$Group},"-\n" if $debug > 0;
$lastArtCount{$Group} = $GrpLast;
print STDERR "--$Group: $lastArtCount{$Group}\n" if $debug > 0;
ARTICLE: while ($NewsClient->next) {
if ($debug > 1) {
print STDERR
"Message Retrieved\n -- Group: $Group\n -- $MessageCounter\n";
}
$MessageCounter++;
my %Headers = ();
my @ArtHead = $NewsClient->head();
foreach $_ (@ArtHead) {
my $FirstColon = index($_, ": ");
my $Header = substr($_, 0, $FirstColon);
my $Content = substr($_, $FirstColon + 2, length($_));
chop($Content) if ($Content ne "");
$Headers{$Header} = $Content;
}
my $ArtStatus = &parseNewsgroups($Headers{Newsgroups}, $Group);
next ARTICLE if ($ArtStatus == 0); # It's OK
# This baby needs to be cancelled.
&saveArticle($Group, $MessageCounter, $NewsClient->article);
my $CancelMsg = &createCMesg($Headers{'Newsgroups'},
$Headers{'Message-ID'},
$Headers{'From'},
$Headers{Subject});
# Post the Cancel Message, but first tokenize it into an array blech
my $currentTime = localtime(time);
print CANLOG "Cancelling: [$currentTime]\t$Headers{'Message-ID'}\t$Headers{'Newsgroups'}\t$Headers{'From'}\n";
my @CMesgArray = split('\n', $CancelMsg);
$NewsClient->post(@CMesgArray);
}
# We need to set the value of the last article for the group
} # End of sub checkGroup
# -------------------------------------------------------------------
#
# saveArticle(Article)
# If we're going to cancel a posting we should first commit the posting
# to disk. This will let us resurrect the posting at a later date.
# should we really screw something up. ;-)
#
sub saveArticle {
my ($Group, $MsgID, @Article) = @_;
$Group =~ s/\./\//goi;
mkpath("$Settings{BKUP_BASEDIR}/$Group", 1, 0755);
open (POOPCHUTE, ">$Settings{BKUP_BASEDIR}/$Group/$MsgID") ||
die "Error opening: $Settings{BKUP_BASEDIR}/$Group/$MsgID";
print POOPCHUTE @Article, "\n";
close(POOPCHUTE);
} # End of sub storeArticle
# -------------------------------------------------------------------
#
# parseNewsgroups(NewsGroups)
#
# This will look at the newsgroups header and check to see how many
# groups it's posted to. and specifically how many groups outside of
# the mn.* hierarchy.
#
sub parseNewsgroups {
my ($Header, $Group) = @_;
$Header =~ s/\s//g; # cleanout any whitespace
return 0 if ($Header !~ /\,/); # additional groups
my @GroupList = split(',', $Header);
# -----------------------------------------------------------------
#
# This section needs some serious attention - We want to have rulesets
# that understand what we want to accomplish in a more general fashion.
# This is crude and definitely needs to be fixed however it'll do in a
# pinch. ;-)
my $CrossCount = 0; # Initialize the crossposting counter
foreach $_ (@GroupList) {
$CrossCount++ if ( $_ !~ /^$Settings{GUARDEDHIERARCHY}\./ );
}
#
#
# -----------------------------------------------------------------
if ($CrossCount > $GroupLimit{$Group} ) {
print STDERR "Crosscount: $CrossCount\n" if $debug > 0;
return 1;
} else { return 0; }
} # End of sub parseNewsgroups
#-------------------------------------------------------------------
#
# getNewArtCount
#
# This will query the news server to get the message count for the
# various groups that its interested in. It will update a hash that
# we'll search through to do the cancellations. By doing this
# we only touch the groups that we're really interested in and
# we minimize our interaction with the news server.
#
sub getNewArtCount {
foreach $Group ( keys(%lastArtCount) ) {
my ($first, $last) = $NewsClient->group("$Group");
print STDERR "$Group: $first - $last\n" if $debug > 0;
if ($last > $lastArtCount{$Group}) {
print STDERR "Added $Group to CheckList: $last" if $debug > 1;
$GrpsToCheck{$Group} = $lastArtCount{$Group};
}
}
} # End of sub getNewArtCount
#-------------------------------------------------------------------
#
# createCMesg()
#
# Actually formulate the cancel message and return the thing.
#
sub createCMesg {
my ($Newsgroups, $MsgID, $PrevFrom, $PrevSubject) = @_;
$MsgID =~ s/[<|>]//goi;
my $CancelMessage = <<EOF;
Path: $Settings{NEWSPATH}
From: $PrevFrom
Message-ID: <cancel.$MsgID>
X-Cancelled-by: $Settings{XCANCELLEDBY}
Subject: cmsg cancel <$MsgID>
Newsgroups: $Newsgroups
Control: cancel <$MsgID>
User-Agent: $Settings{USERAGENT}
Summary: $Settings{SUMMARYTXT}
Approved: $Settings{APPROVED}
X-No-Archive: Yes
X-Was-From: $PrevFrom
X-Was-Subject: $PrevSubject
$Settings{MESSAGE}
EOF
if ($debug > 1) {
print STDERR
"\n------------------------------------------------------------\n";
print STDERR $CancelMessage, "\n";
print STDERR
"\n------------------------------------------------------------\n";
}
return $CancelMessage;
}
#-------------------------------------------------------------------
# writeGoupList()
#
# Write the most recent values of things to disk so we can
# use them later on in life.
#
sub writeGroupList {
open (GROUPLIST, ">$Settings{GROUPFILE}") ||
die "Cannot open $Settings{GROUPFILE}";
foreach $Group (sort(keys(%lastArtCount))) {
print GROUPLIST "$Group:$lastArtCount{$Group}:$GroupLimit{$Group}\n";
}
close(GROUPLIST);
} # End of sub writeVars
#-------------------------------------------------------------------
# dieWGrace()
#
# Catch the KILL signal and die with grace.
#
sub dieWGrace {
print STDERR "Dying off with Grace\n";
&writeGroupList;
close(CANLOG); # Close the cancel log
close(STDOUT); # Turn off the redirection of STDOUT
close(STDERR); # Turn off the redirection of STDERR
exit(0);
} # end of sub dieWGrace()
# -------------------------------------------------------------------
# getSettings(ConfigFile)
#
# Load the settings from a configuration file and return the values
# in the settings hash.
#
sub getSettings {
my ($ConfigFile ) = @_;
my $Settings = ();
open (CONF, "$ConfigFile") || die "Unable to open $ConfigFile";
while (<CONF>) {
next if /^\#/;
my ($Var, $WSpace, $WSpace,$Value) = /^(\S+)(\s+)=(\s+)\"(.+)\"$/;
# Let's Turn our Vars into UpperCase Vars for readability and
# make us fairly agnostic wrt the capitalization of the Vars.
$Var =~ tr/a-z/A-Z/;
$Settings{$Var} = $Value;
}
close(CONF);
if ($debug > 0) {
print STDERR "\n\n", '=' x 70, "\n";
foreach (keys(%Settings)) {
print STDERR "$_ - $Settings{$_}\n";
}
print STDERR '=' x 70, "\n";
}
return %Settings;
}
# -------------------------------------------------------------------
# getTemplate(TemplateFile)
#
sub getTemplate {
my $TemplateFile = $_[0];
my $Template = "";
open(TEMPLATE, $TemplateFile) || die "Error Opening: $TemplateFile";
while(<TEMPLATE>) { $Template .= $_; }
close(TEMPLATE);
return $Template;
}
# -------------------------------------------------------------------
# getGroupList(GroupList)
#
# Load the List of groups from a configuration file and return the
# values in the GroupList hash.
#
sub getGroupList{
my ($GroupFile ) = @_;
my %GroupList = ();
open (GROUPS, "$GroupFile") || die "Unable to open $GroupFile";
while (<GROUPS>) {
next if /^\#/;
next if /^$/;
print STDERR $_ if $debug > 1;
my $GroupLine = $_;
$GroupLine =~ s/\s//g;
my ($Group, $LastMsgNum) = split(/:/, $GroupLine);
$LastMsgNum =~ s/\r|\n|\s//g; # Rip out whitespace
$GroupList{$Group} = $LastMsgNum;
}
close(GROUPS);
return %GroupList;
}
#-------------------------------------------------------------------
# getGroupLimit(GroupList)
#
# Load the List of groups from a configuration file and return the
# values in the GroupLimit hash.
#
sub getGroupLimit{
my ( $GroupFile ) = @_;
my %GroupLimit = ();
open (GROUPS, "$GroupFile") || die "Unable to open $GroupFile";
while (<GROUPS>) {
next if /^\#/;
next if /^$/;
print STDERR $_ if $debug > 1;
my $GroupLine = $_;
$GroupLine =~ s/\s//g;
my ($Group, $LastMsgNum, $GroupLimit) = split(/:/, $GroupLine);
$LastMsgNum =~ s/\r|\n|\s//g; # Rip out whitespace
$GroupLimit{$Group} = $GroupLimit;
}
close(GROUPS);
return %GroupLimit;
}