Skip to content

Latest commit

 

History

History
executable file
·
433 lines (363 loc) · 15 KB

IcculusFinger_archiveplans.pl

File metadata and controls

executable file
·
433 lines (363 loc) · 15 KB
 
Jan 11, 2002
Jan 11, 2002
1
#!/usr/bin/perl -w
Jan 29, 2002
Jan 29, 2002
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#-----------------------------------------------------------------------------
#
# Copyright (C) 2002 Ryan C. Gordon (icculus@icculus.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#-----------------------------------------------------------------------------
Jan 11, 2002
Jan 11, 2002
21
22
23
24
25
26
use strict;
use warnings;
use DBI;
use File::Basename;
Jan 29, 2002
Jan 29, 2002
27
28
29
30
31
#-----------------------------------------------------------------------------#
# CONFIGURATION VARIABLES: Change to suit your needs... #
#-----------------------------------------------------------------------------#
Jan 11, 2002
Jan 11, 2002
32
33
34
35
36
# The ever important debug-spew-enabler...
my $debug = 0;
# File must have not been touch within this many minutes to be archived.
# This prevents archiving of a file that is in the middle of being edited.
Feb 28, 2002
Feb 28, 2002
37
my $update_delay = 60 * 24; # 24 hours old.
Jan 11, 2002
Jan 11, 2002
38
Jul 1, 2002
Jul 1, 2002
39
40
my $use_homedir = 0;
my $fingerspace = '/fingerspace';
Jan 11, 2002
Jan 11, 2002
41
42
43
44
my $dbhost = 'localhost';
my $dbuser = 'fingermgr';
May 11, 2002
May 11, 2002
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# This is the maximum amount of data that IcculusFinger will read from a
# planfile. If this cuts off in the middle of an opened formatting tag,
# tough luck. IcculusFinger reads the entire planfile (up to the max you
# specify here) into memory before processing tags. Theoretically, this could
# be changed to handle tags on the fly, in which case users that would
# otherwise be over the limit might not be after processing sections, etc,
# but this is not the case here and now.
# Note that images specified inside [img] tags, etc are not counted towards
# this limit (since the web browser would be requesting those images
# separately), and only applies to the max bytes to be read directly from the
# planfile. This is merely here to prevent someone from symlinking their
# planfile to /dev/zero or something that could fill all available memory.
# This value is specified in bytes. Data read from the database has no
# limit, but theoretically, if it had to pass through this script to get into
# the database, this is a practical limit for that, too.
my $max_plan_size = (100 * 1024);
Jan 11, 2002
Jan 11, 2002
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# The password can be entered in three ways: Either hardcode it into $dbpass,
# (which is a security risk, but is faster if you have a completely closed
# system), or leave $dbpass set to undef, in which case this script will try
# to read the password from the file specified in $dbpassfile (which means
# that this script and the database can be touched by anyone with read access
# to that file), or leave both undef to have DBI get the password from the
# DBI_PASS environment variable, which is the most secure, but least
# convenient.
my $dbpass = undef;
my $dbpassfile = '/etc/IcculusFinger_dbpass.txt';
my $dbname = 'IcculusFinger';
my $dbtable_archive = 'finger_archive';
my $post_to_icculusnews = 1;
Jun 14, 2002
Jun 14, 2002
77
78
79
80
81
my $newshost = 'localhost';
my $newsauthor = 'fingermaster';
my $newsposter = '/usr/local/bin/IcculusNews_post.pl';
my $newspass = undef;
my $newspassfile = '/etc/IcculusFinger_newspass.txt';
Jan 11, 2002
Jan 11, 2002
82
Jan 29, 2002
Jan 29, 2002
83
84
85
86
#-----------------------------------------------------------------------------#
# The rest is probably okay without you laying yer dirty mits on it. #
#-----------------------------------------------------------------------------#
May 9, 2002
May 9, 2002
87
my $force_archive = undef;
Feb 5, 2008
Feb 5, 2008
88
my $replace_archive = 0;
Jan 11, 2002
Jan 11, 2002
89
Mar 14, 2016
Mar 14, 2016
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
sub strip_tags {
my $t = shift;
1 while ($t =~ s/\&(?!amp)/&/s);
1 while ($t =~ s/</&lt;/s);
1 while ($t =~ s/>/&gt;/s);
1 while ($t =~ s/\[title\](.*?)\[\/title\](\n|\r\n|\b)//is);
1 while ($t =~ s/\[wittyremark\](.*?)\[\/wittyremark\](\n|\r\n|\b)//is);
1 while ($t =~ s/\[b\](.*?)\[\/b\]/<b>$1<\/b>/is);
1 while ($t =~ s/\[u\](.*?)\[\/u\]/<u>$1<\/u>/is);
1 while ($t =~ s/\[i\](.*?)\[\/i\]/<i>$1<\/i>/is);
1 while ($t =~ s/\[center\](.*?)\[\/center\]/<center>$1<\/center>/is);
1 while ($t =~ s/\[img=(".*?")\].*?\[\/img\]/<img src=$1>/is);
1 while ($t =~ s/\[link=(".*?")\](.*?)\[\/link\]/<a href=$1>$2<\/a>/is);
1 while ($t =~ s/\[defaultsection=".*?"\](\n|\r\n|\b)//is);
1 while ($t =~ s/\[section=".*?"\](\n|\r\n|\b)(.*?)\[\/section\](\n|\r\n|\b)/$2/is);
1 while ($t =~ s/\[font(.*?)\](.*?)\[\/font\]/<font $1>$2<\/font>/is);
1 while ($t =~ s/\[noarchive\](.*?)\[\/noarchive\]/$1/is);
1 while ($t =~ s/\[markdown\](.*?)\[\/markdown\]/$1/is);
1 while ($t =~ s/\[metadata=\].*?\[\/metadata\]//is);
return $t;
}
Jan 11, 2002
Jan 11, 2002
112
113
114
115
116
117
118
sub run_external_updater {
my $u = shift;
my $d = shift;
my $t = shift;
if ($post_to_icculusnews) {
print(" posting to IcculusNews's submission queue...\n") if $debug;
Mar 14, 2016
Mar 14, 2016
119
$t = strip_tags($t);
Jan 11, 2002
Jan 11, 2002
120
121
print(" parsed markup tags...\n") if $debug;
my $newssubj = "Notable .plan update from $u";
Jun 14, 2002
Jun 14, 2002
122
123
$ENV{'ICCNEWS_POST_PASS'} = $newspass if defined $newspass;
my $rc = open(PH, "|$newsposter '$newshost' '$newsauthor' '1' '$newssubj' -");
Jan 11, 2002
Jan 11, 2002
124
125
126
127
128
129
130
131
132
133
134
135
if (not $rc) {
print(" No pipe to IcculusNews: $!\n");
} else {
print PH "<i>$u updated his .plan file at $d" .
" with the following info:</i>\n<p>\n<pre>$t</pre>\n</p>\n";
close(PH);
print(" posted to submission queue.\n") if $debug;
}
}
}
Jul 1, 2002
Jul 1, 2002
136
137
138
sub enumerate_planfiles {
my $dirname = (($use_homedir) ? '/home' : $fingerspace);
opendir(DIRH, $dirname) or return(undef);
Jun 3, 2006
Jun 3, 2006
139
my @dirents = sort readdir(DIRH);
Jan 11, 2002
Jan 11, 2002
140
closedir(DIRH);
Jul 1, 2002
Jul 1, 2002
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
my @retval;
if ($use_homedir) {
foreach (@dirents) {
next if (($_ eq '.') or ($_ eq '..'));
push @retval, "/home/$_/.plan";
}
} else {
foreach (@dirents) {
next if (($_ eq '.') or ($_ eq '..'));
push @retval, "$fingerspace/$_";
}
}
Jan 11, 2002
Jan 11, 2002
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
return(@retval);
}
sub get_sqldate {
my $mtime = shift;
my @t = localtime($mtime);
$t[5] = "0000" . ($t[5] + 1900);
$t[5] =~ s/.*?(\d\d\d\d)\Z/$1/;
$t[4] = "00" . ($t[4] + 1);
$t[4] =~ s/.*?(\d\d)\Z/$1/;
$t[3] = "00" . $t[3];
$t[3] =~ s/.*?(\d\d)\Z/$1/;
$t[2] = "00" . $t[2];
$t[2] =~ s/.*?(\d\d)\Z/$1/;
$t[1] = "00" . $t[1];
$t[1] =~ s/.*?(\d\d)\Z/$1/;
$t[0] = "00" . $t[0];
$t[0] =~ s/.*?(\d\d)\Z/$1/;
return('' .
($t[5]) . '-' . ($t[4]) . '-' . ($t[3]) . ' ' .
($t[2]) . ':' . ($t[1]) . ':' . ($t[0])
);
}
sub read_plantext {
my $link = shift;
my $filename = shift;
my $retval = '';
May 11, 2002
May 11, 2002
188
189
190
open(PLAN_IN, '<', $filename) or die("Can't open $filename: $!\n");
if (not defined read(PLAN_IN, $retval, $max_plan_size)) {
die("Couldn't read planfile: $!");
Jan 11, 2002
Jan 11, 2002
191
192
193
}
close(PLAN_IN);
Jun 3, 2006
Jun 3, 2006
194
return($retval);
Jan 11, 2002
Jan 11, 2002
195
196
197
198
199
200
201
202
203
204
205
206
207
208
}
sub update_planfile {
my $link = shift;
my $filename = shift;
if (-d $filename) {
return;
}
my $modtime = (stat($filename))[9];
my $fdate = get_sqldate($modtime);
my $sql = '';
May 4, 2002
May 4, 2002
209
my $plantext = undef;
Jan 11, 2002
Jan 11, 2002
210
Jul 1, 2002
Jul 1, 2002
211
212
213
214
215
216
217
my $user = $filename;
if ($use_homedir) {
$user =~ s#\A/home/(.*?)/\.plan\Z#$1#;
} else {
$user = basename($filename);
}
Feb 5, 2008
Feb 5, 2008
218
my $replace = 0;
May 9, 2002
May 9, 2002
219
220
221
if ((defined $force_archive) and ($force_archive eq $user)) {
print(" * Forcing archival of $user\'s .planfile...\n") if $debug;
$user = $link->quote($user);
Feb 5, 2008
Feb 5, 2008
222
$replace = $replace_archive;
May 9, 2002
May 9, 2002
223
224
} else {
print(" * Examining $user\'s .planfile (modtime $fdate)...\n") if $debug;
Jan 11, 2002
Jan 11, 2002
225
May 9, 2002
May 9, 2002
226
$user = $link->quote($user);
Jan 11, 2002
Jan 11, 2002
227
May 9, 2002
May 9, 2002
228
229
230
231
232
# ... get date of the latest archived .plan ...
$sql = "select postdate, text from $dbtable_archive where username=$user" .
" order by postdate desc limit 1";
my $sth = $link->prepare($sql);
$sth->execute() or die "can't execute the query: $sth->errstr";
Jan 11, 2002
Jan 11, 2002
233
May 9, 2002
May 9, 2002
234
my @row = $sth->fetchrow_array();
Jun 3, 2006
Jun 3, 2006
235
$sth->finish();
May 9, 2002
May 9, 2002
236
237
if (not @row) {
print(" Don't seem to have a previous entry ...\n") if $debug;
Jun 14, 2002
Jun 14, 2002
238
239
240
241
if ( (stat($filename))[7] == 0 ) {
print(" ...but the .plan is empty. Skipping.\n") if $debug;
return;
}
Jan 11, 2002
Jan 11, 2002
242
} else {
May 9, 2002
May 9, 2002
243
244
245
my $t = time();
if ($t < $modtime) {
print(" WARNING: file timestamp is in the future!\n") if $debug;
Jun 3, 2006
Jun 3, 2006
246
247
`touch -c -m '$filename'`; # force to now, retest next time for content change.
return;
May 9, 2002
May 9, 2002
248
249
250
251
252
253
254
255
} else {
if ( ($t - $modtime) < ($update_delay * 60) ) {
if ($debug) {
my $oktime = int($update_delay - (($t - $modtime) / 60));
print(" File update is too new to archive.\n");
print(" (Try again in $oktime minutes.)\n");
}
return;
Jan 11, 2002
Jan 11, 2002
256
257
258
}
}
May 9, 2002
May 9, 2002
259
260
261
if ($debug) {
my $x = $row[0];
print(" dates: [$x] [$fdate]\n");
Jun 3, 2006
Jun 3, 2006
262
}
Jan 11, 2002
Jan 11, 2002
263
Jun 3, 2006
Jun 3, 2006
264
if ($row[0] eq $fdate) {
May 9, 2002
May 9, 2002
265
print(" Matches archive timestamp. Skipping.\n") if $debug;
May 4, 2002
May 4, 2002
266
return;
May 9, 2002
May 9, 2002
267
268
269
270
} else {
$plantext = read_plantext($link, $filename);
my $plancpy = $plantext;
# Ditch [noarchive][/noarchive] tag blocks and strcmp rest.
Jun 3, 2006
Jun 3, 2006
271
272
1 while ($row[1] =~ s/\[noarchive\].*?\[\/noarchive\]//is);
1 while ($plancpy =~ s/\[noarchive\].*?\[\/noarchive\]//is);
May 9, 2002
May 9, 2002
273
if ($row[1] ne $plancpy) {
Jun 3, 2006
Jun 3, 2006
274
print(" Newer revision needs archiving.\n") if $debug;
May 9, 2002
May 9, 2002
275
} else {
Jun 3, 2006
Jun 3, 2006
276
print(" Newer revision only changed [noarchive] section(s). Skipping.\n") if ($debug);
May 9, 2002
May 9, 2002
277
278
279
280
return;
}
}
}
Jan 11, 2002
Jan 11, 2002
281
282
}
May 4, 2002
May 4, 2002
283
$plantext = read_plantext($link, $filename) if (not defined $plantext);
Mar 14, 2016
Mar 14, 2016
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
my $metadatastr = $plantext;
my %metadata = ();
1 while ($metadatastr =~ s/\r\n/\n/s);
1 while ($metadatastr =~ s/\r/\n/s);
while ($metadatastr =~ s/\[metadata=\"(.*?)\"\](\n|\b)(.*?)\[\/metadata\](\n|\b)//) {
my $k = $1;
my $v = $3;
$k =~ tr/A-Z/a-z/;
$metadata{$k} = $v;
}
my $summary = $metadata{'summary'};
if (not defined $summary) {
my $str = $metadatastr;
1 while ($str =~ s/\[title\](.*?)\[\/title\](\n|\r\n|\b)//is);
1 while ($str =~ s/\[wittyremark\](.*?)\[\/wittyremark\](\n|\r\n|\b)//is);
1 while ($str =~ s/\[b\](.*?)\[\/b\]/$1/is);
1 while ($str =~ s/\[u\](.*?)\[\/u\]/$1/is);
1 while ($str =~ s/\[i\](.*?)\[\/i\]/$1/is);
1 while ($str =~ s/\[center\](.*?)\[\/center\]/$1/is);
1 while ($str =~ s/\[img=(".*?")\].*?\[\/img\]//is);
1 while ($str =~ s/\[link=(".*?")\](.*?)\[\/link\]/$2/is);
1 while ($str =~ s/\[defaultsection=".*?"\](\n|\r\n|\b)//is);
1 while ($str =~ s/\[section=".*?"\](\n|\r\n|\b)(.*?)\[\/section\](\n|\r\n|\b)/$2/is);
1 while ($str =~ s/\[font(.*?)\](.*?)\[\/font\]/$2/is);
1 while ($str =~ s/\[noarchive\](.*?)\[\/noarchive\]/$1/is);
1 while ($str =~ s/\[markdown\](.*?)\[\/markdown\]/$1/is);
1 while ($str =~ s/\[metadata=".*?"\](\n|\r\n|\b).*?\[\/metadata\](\n|\r\n|\b)//is);
$str =~ s/\A\s+//;
$str =~ s/\s+\Z//;
$str =~ s/\A(.*?)[\r\n].*\Z/$1/s;
$str =~ s/\A\s+//;
$str =~ s/\s+\Z//;
$summary = $str;
Mar 22, 2016
Mar 22, 2016
322
323
$summary =~ s/\A(.{1,80})\b.*\Z/$1/;
$summary =~ s/\A(.{15,}?[.?!]).*\Z/$1/; # gross.
Mar 14, 2016
Mar 14, 2016
324
325
326
327
328
}
1 while ($summary =~ s/\r\n/ /s);
1 while ($summary =~ s/\r/ /s);
1 while ($summary =~ s/\n/ /s);
Mar 22, 2016
Mar 22, 2016
329
330
331
332
333
334
335
if (length($summary) > 80) {
$summary =~ s/\A(.{1,80})\b.*\Z/$1/;
$summary =~ s/\A(.{15,}?[.?!]).*\Z/$1/; # gross.
}
#print("SUMMARY: $summary\n"); exit(0);
Mar 14, 2016
Mar 14, 2016
336
337
338
$summary = $link->quote($summary);
May 4, 2002
May 4, 2002
339
my $ftext = $link->quote($plantext);
Feb 5, 2008
Feb 5, 2008
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
my $lastpost = undef;
if ($replace) {
# ... get date of the latest archived .plan ...
$sql = "select postdate from $dbtable_archive where username=$user" .
" order by postdate desc limit 1";
my $sth = $link->prepare($sql);
$sth->execute() or die "can't execute the query: $sth->errstr";
my @row = $sth->fetchrow_array();
$sth->finish();
if (not @row) {
$replace = 0; # no previous entry.
} else {
$lastpost = $row[0];
}
}
if ($replace) {
Mar 14, 2016
Mar 14, 2016
359
$sql = "update $dbtable_archive set text=$ftext, summary=$summary" .
Feb 5, 2008
Feb 5, 2008
360
361
362
363
" where $user=$user and postdate='$lastpost'";
$lastpost =~ s/\d\d\d\d\-(\d\d)-(\d\d) (\d\d)\:(\d\d)\:(\d\d)/$1$2$3$4.$5/;
`touch -c -m -t '$lastpost' '$filename'`; # force file to this date.
} else {
Mar 14, 2016
Mar 14, 2016
364
365
$sql = "insert into $dbtable_archive (username, postdate, summary, text)" .
" values ($user, '$fdate', $summary, $ftext)";
Feb 5, 2008
Feb 5, 2008
366
}
Jan 11, 2002
Jan 11, 2002
367
368
369
370
$link->do($sql) or die "can't execute the query: $link->errstr";
print(" Revision added to archives.\n") if $debug;
May 4, 2002
May 4, 2002
371
run_external_updater(basename($filename), $fdate, $plantext);
Jan 11, 2002
Jan 11, 2002
372
373
374
375
376
}
# the mainline.
May 9, 2002
May 9, 2002
377
378
379
for (my $i = 0; $i < scalar(@ARGV); $i++) {
my $arg = $ARGV[$i];
$debug = 1, next if ($arg eq '--debug');
Feb 5, 2008
Feb 5, 2008
380
$replace_archive = 1, next if ($arg eq '--replace');
May 9, 2002
May 9, 2002
381
382
383
384
385
$force_archive = $ARGV[++$i], next if ($arg eq '--force');
#$username = $arg, next if (not defined $username);
#$subject = $arg, next if (not defined $subject);
#$text = $arg, next if (not defined $text);
Jan 11, 2002
Jan 11, 2002
386
#etc.
May 9, 2002
May 9, 2002
387
388
print("Unknown argument \"$arg\".\n");
Jan 11, 2002
Jan 11, 2002
389
390
}
Feb 5, 2008
Feb 5, 2008
391
392
393
394
if (($replace_archive) and (not defined $force_archive)) {
die("--replace without --force")
}
Jul 1, 2002
Jul 1, 2002
395
396
my @planfiles = enumerate_planfiles();
die("Failed to enumerate planfiles: $!\n") if not @planfiles;
Jan 11, 2002
Jan 11, 2002
397
398
399
400
401
402
403
404
405
406
407
408
if (not defined $dbpass) {
if (defined $dbpassfile) {
open(FH, $dbpassfile) or die("failed to open $dbpassfile: $!\n");
$dbpass = <FH>;
chomp($dbpass);
$dbpass =~ s/\A\s*//;
$dbpass =~ s/\s*\Z//;
close(FH);
}
}
Jun 14, 2002
Jun 14, 2002
409
410
411
412
413
414
415
416
417
418
419
if ((not defined $newspass) && (not defined $ENV{'ICCNEWS_POST_PASS'})) {
if (defined $newspassfile) {
open(FH, $newspassfile) or die("failed to open $newspassfile: $!\n");
$newspass = <FH>;
chomp($newspass);
$newspass =~ s/\A\s*//;
$newspass =~ s/\s*\Z//;
close(FH);
}
}
Jan 11, 2002
Jan 11, 2002
420
421
422
423
424
425
my $dsn = "DBI:mysql:database=$dbname;host=$dbhost";
print(" * Connecting to [$dsn] ...\n") if $debug;
my $link = DBI->connect($dsn, $dbuser, $dbpass, {'RaiseError' => 1});
foreach (@planfiles) {
Jul 1, 2002
Jul 1, 2002
426
update_planfile($link, "$_");
Jan 11, 2002
Jan 11, 2002
427
428
429
430
431
432
}
$link->disconnect();
exit 0;
# end of IcculusFinger_archiveplans.pl ...