-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetdiary.FINAL
More file actions
executable file
·397 lines (339 loc) · 9.17 KB
/
netdiary.FINAL
File metadata and controls
executable file
·397 lines (339 loc) · 9.17 KB
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
#!/usr/bin/perl -w
# Netdiary, a script to manage progressive entries in a web diary
$editor = 'gvim -f';
$alt_editor = 'vim';
$name = "Pat Gunn";
$includefile = M_gethome() . "/.ndinclude"; # Customize this
$diarydir = M_gethome() . "/netdiary";
my @topush; # List of summary files to push to the site
use POSIX;
my $do_ticons = 1; # Time icons
$nd_ver = "2.2";
$threshold = 10; # If nonzero, how many messages should fit into each page
main();
####################################################################
sub main
{
my $toedit;
my $do_edit = 1;
my $do_post = 1;
my @args = @ARGV;
while($_ = pop @args)
{
if( /-u/i) {$do_edit = 0;next;} # Only post (Upload)
if( /-x/i) {$editor = $alt_editor;next;} # Swap editors
if( /-n/i) {$do_post = 0;next;} # Only edit new entry (New)
if( /-b/i) {$do_edit = $do_post = 0;next;} # Only rebuild indices
if( /-h/i) {die "netdiary [-u][-n]\n\t-u upload only\n\t-n edit only\n";}
die "usage: netdiary [-u][-n]\n";
}
if($do_edit)
{
$toedit = new_tfile(); # Create a new file, prepare for editing
system("$editor $toedit"); # And edit it.
if( -z $toedit)
{
unlink ($toedit);
die "Empty file!"; # Reject empty entries
}
}
buildsummary();
if($do_post) {postbuild();} # Do whatever the user wants with the summary
}
#### YOU WILL PROBABLY WANT TO CUSTOMIZE THIS FUNCTION
sub postbuild
{
my $jstring = join " ", @topush;
print `scp -C -v $jstring pgunn\@www.evilnet.net:~pgunn/public_html/diary/`;
#foreach $file (@topush)
# {
#print `ncftpput -u pgunn -p 0001539980 www.evilnet.net ~pgunn/public_html/diary $file` . "\n";
# }
}
#### YOU CUSTOMIZED THE ABOVE FUNCTION, RIGHT? RIGHT?!?!?!
sub buildsummary
# Build the html file from all the entries
{
my $idx; # Index for for-loop
opendir(SUMDIR, $diarydir);
my @files = # The list of files is
sort {$a <=> $b} # A numerically sorted list
grep /^\d+$/i, # We only want 'number' files
readdir(SUMDIR); # From all the files in the diary dir
closedir(SUMDIR);
$num_ofiles = ($threshold == 0)? 1 : POSIX::ceil( scalar(@files) / $threshold );
# print "Calculated number of files as " . scalar(@files) . " / $threshold = $num_ofiles\n";
# While we're not at the last file
# {
# build a summary$number file with increasing $number, telling it if
# there's another summary or the main summary in front of it
# }
# Build the main summary. Might overlap with the last numbered summary.
my $files_iter = 0;
my $t_files_iter = 0;
my $ofile_iter = 0;
for(my $fv = 0; $fv < $threshold ; $fv++)
{push(@lfiles, $files[$fv]);}
#my @lfiles = @files[0 .. $threshold];
pos_update(0);
sumfile("summary0");
sum_childhead(0);
while($ofile_iter < ($num_ofiles - 1)) # Not at the last file
{
while(scalar(@lfiles))
{ parsefile(pop @lfiles); }
#else need to change files
{
sum_tail();
close(SUM);
$ofile_iter++;
pos_update($ofile_iter);
sumfile("summary" . $ofile_iter);
sum_childhead($ofile_iter);
for(my $fv = $threshold * $ofile_iter; $fv < ($threshold*($ofile_iter+1)) ; $fv++)
{push(@lfiles, $files[$fv]);}
# @lfiles = @files[($threshold * $ofile_iter) .. ($threshold * ($ofile_iter+1))]; # Assign the next batch
}
}
pos_update($num_ofiles - 1);
sumfile("summary");
sum_head();
$t_files_iter = (($num_ofiles==1) || (scalar(@files) < $threshold))? 0 : (scalar(@files) - $threshold);
my @final_files_to_parse;
while($t_files_iter < scalar(@files) )
{
unshift(@final_files_to_parse, $files[$t_files_iter++]);
}
foreach my $ffp (@final_files_to_parse)
{
parsefile($ffp);
}
sum_tail();
close(SUM);
}
sub sumfile
# Create a new summary file, open it
{
my $sumname = shift;
my $FN = "$diarydir/$sumname" . ".html";
push(@topush, $FN); # add to list of things to push
unlink($FN); # Clear it out if it exists
if( -e $FN ) {die "Could not clear summary file $FN: $!\n";} # Permissions problem?
open(SUM,">$FN") || die "Cannot open summary file $FN: $!\n"; # Permissions problem
}
sub sum_head
# Prints HTML header to filehandle <SUM>
{
print SUM <<EOS;
<HEAD><TITLE>Net Diary</TITLE></HEAD>
<BODY BGCOLOR="#AAACCC">
<H3>This is the netdiary of $name</H3>
It is managed with software written by Pat Gunn,
available at <A HREF="http://junior.apk.net/~qc">his webpage</A><br>
</H3>
EOS
if( -e $includefile)
{
local $/;
open(NDI, $includefile);
my $nddata = <NDI>;
close(NDI);
print SUM $nddata;
}
}
sub sum_childhead
# Non-mainpage HTML header
{
my $page = shift;
my $displaypage = $page + 1;
print SUM <<EOSI;
<HEAD><TITLE>Net Diary (Page $displaypage)</TITLE></HEAD>
<BODY BGCOLOR="#AAACCC">
<H3>Netdiary of $name, page $displaypage</H3>
EOSI
}
sub sum_tail
# Prints footer to filehandle <SUM>
{
print SUM $postmagic;
print SUM <<EOTA;
<HR><HR><B>Generated with Netdiary $nd_ver</B></BODY></HTML>
EOTA
close(SUM);
}
sub parsefile
# Parses a netdiary entry, outputs to <SUM>
{
my $fn_toparse = shift; # Filename to parse
my $entfile;
if(! defined($fn_toparse) )
{
die "Asked to parse a nonexistant file in $diarydir\n";
}
open(ENTFILE, "$diarydir/$fn_toparse") || die "Cannot open file $fn_toparse!\n";
undef $/; # We want to parse it all as a single huge string
$entfile = <ENTFILE>; # Swallow it
do_filter(\$entfile);
my $title = get_title(\$entfile);
my ($tcolor, $bgcolor) = get_colors(\$entfile);
my @topics = get_topics(\$entfile);
if(is_private($entfile))
{$entfile = "<B>Entry is Private</B>";}
my $datestr = niceformat($fn_toparse);
my $iconstr = ticon($fn_toparse);
write_entry($title, $entfile, $datestr, $iconstr, $bgcolor, $tcolor, \@topics);
#print "Writing entry $fn_toparse\n";
close(ENTFILE);
}
sub write_entry
{
my($title, $entfile, $datestr, $iconstr, $bgcolor, $textcolor, $topics) = @_;
print SUM "<TABLE BORDER=1 BGCOLOR=\"grey\">\n";
print SUM "<TR><TD>$iconstr<TD>$datestr</TR>\n";
print SUM "<TR><TD colspan=\"2\">$title</TR>\n";
if(defined($$topics[0]))
{
print SUM "<TR><TD>Topics:<TD>" . join(', ', @$topics) . "</TR>\n";
}
print SUM "<TR><TD colspan=\"2\" BGCOLOR=\"$bgcolor\"><FONT COLOR=\"$textcolor\"><PRE>$entfile</PRE></FONT></TR>\n";
print SUM "</TABLE><br>\n";
}
sub pos_update
# Generates the needed HTML for position-dependent journal stuff
# It might be interesting, but it would complicate the code too much,
# to display titles for entries in the next and previous page.
{
my $pindex = shift;
if($pindex < 0) {die "Internal error in pos_update";}
$postmagic = "";
if($num_ofiles == 1) {return;} # Don't bother if this is the only file
if($pindex > 0) # Setup the pre-link, if valid
{
$postmagic = "[<A HREF=\"" . # A link to
"summary" .
(($pindex-1 == 1)? "" : $pindex-1) . # Handle position two
".html" . # next file entry
"\">Past</A>]";
}
if($pindex < ($num_ofiles - 1)) # And the post-link, if valid
{
$postmagic .= "[<A HREF=\"" . # A link to
"summary" . ($pindex+1) . ".html" . # next file entry
"\">Future</A>]";
}
}
sub niceformat
# Nicely parses Unix time into a pretty string
{
my $ut = shift;
my $rner = POSIX::ctime($ut);
$rner =~ s/\n$//;
return $rner;
}
sub ticon
{ # Print to the SUM filehandle the appropriate HTML for the time icon
if(! $do_ticons) {return;}
my ($dstr) = @_;
my(undef,$mn,$hr)=localtime($dstr);
my $mins=$mn+(60*$hr);
if(
($mins == 0) # before 03:00
|| ($mins > 1080) # After 18:00
)
{
return q{<IMG SRC="time_dusk.jpg" ALT="dusk">};
}
elsif(
($mins > 0) # after 03:00
&& ($mins < 360) # Before 09:00
)
{
return q{<IMG SRC="time_dawn.jpg" ALT="dawn">};
}
elsif
(
($mins >= 360)
&& ($mins < 720)
)
{
return q{<IMG SRC="time_morning.jpg" ALT="morning">};
}
else
{
return q{<IMG SRC="time_evening.jpg" ALT="evening">};
}
}
############################################
# File stuff grabbed from psmail
############################################
sub M_gethome
{
return ( (getpwuid($<))[7] );
}
sub new_tfile
# creates file netdiary/$FILE, where $FILE's name
# is determined by current unix time
{
my $FN = "$diarydir/" . time();
if( -e $FN )
{
sleep(2); # Sleep so clock will increment
$FN = "$diarydir/" . time();
}
if( -e $FN ) # This should never happen
{
die "Bailing because $FN already exists!\n";
}
`touch $FN`;
if(! POSIX::access($FN, &POSIX::W_OK) )
{
die "Cannot write to $FN!\n";
}
return($FN);
}
sub is_private
# Passed the contents of a message, determines if it has the needed
# tokens that mark it as private
{
my $content = shift;
if($content =~ /^CONTENT: Private/m){return 1;}
else {return 0;}
}
sub get_title
{
my ($entfile) = @_;
$$entfile =~ s/\A([^\n]*)\n\n//; # What's on the first line?
my $returner = $1;
if((! defined($returner)) || ($returner !~ /\w/) )
{$returner = "No Title";}
return $returner;
}
sub get_colors
{
my ($entfile) = @_;
if($$entfile =~ s/^COLOR: *([A-Za-z]+)\/([A-Za-z]+)//m)
{return ($1, $2);}
else
{return ("green", "black");}
}
sub get_topics
{
my ($entfile) = @_;
if($$entfile =~ s/^TOPICS?: *([^\n]*)\n//m) # Note that . does NOT match newline here
{return split /,/, $1;}
else
{return ();}
}
sub do_filter
{
my($entfile_r) = @_;
local $_ = $$entfile_r;
s/^[\n ]+//s; # Remove initial newlines
s/[\n ]+$//s; # and trailing newlines
# s/([^-!])-- /$1 ߞ /g; # Most browsers don't support this. Alas.
#s/ae/æ/g; # fun, but a no-go
#s/ss/ß/g;
s/EURO/€/g;
s/<PRIVATE>.*?<\/PRIVATE>/<br><br>-PRIVATE_SECTION NOT SHOWN-<br><br>/msg;
$$entfile_r = $_;
}