-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathautofox.pl
More file actions
executable file
·2187 lines (1832 loc) · 77 KB
/
autofox.pl
File metadata and controls
executable file
·2187 lines (1832 loc) · 77 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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# AutoFox 2.5
# Copyright (c) 2003-2008 Nicholas "Tegeran" Knight <nknight@runawaynet.com>
# Copyright (c) 2003-2024 Nicholas "CaptainSpam" Killewald <captainspam@exclaimindustries.net>
# See "LICENSE" file at the toplevel for your daily dose of 3-clause BSD.
# This is 2000-ish lines of semi-(readable|maintainable) Perl. It ain't pretty,
# but by some miracle it works, and it's surprisingly fast. I should really
# think about breaking this up into modules at some point.
# It might be possible to clean AutoFox up quite a bit by using more of the
# standard modules. Unfortunately my familiarity with things outside the core
# language is quite limited, and I also harbour a fear that the surprisingly
# good performance seen in AF may be adversely affected.
use strict;
use warnings;
use File::Copy;
use POSIX;
use JSON;
use Digest::SHA;
use HTML::Entities ();
#use local::lib;
my $afversion = "AutoFox 2.5.7.2";
#=======================================================================
# Why am I counting from 1? Because it simplifies things when dealing
# with user-supplied dates (which is about all AutoFox does) and saves
# many an addition operation (yeah, I know, I'm not supposed to try and
# optimize Perl code like that ;)). -Teg
my @mnames = ("Error", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December");
my @mshortnames = ("Error", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec");
my @dnames = ("Error", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday");
my @dshortnames = ("Error", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
#=======================================================================
# Read in the config file in a totally unsafe and error-prone way. -Teg
my %conf = (
url => "about:blank",
updatetime => 2300,
updateday => "same",
timezone => strftime("%z",localtime),
captionold => 1,
captionsfile => "captions.txt",
comicsdir => "comics/",
imagedir => "images/",
dailydir => "d/",
uploaddir => "comics/",
sitedir => "public_html/",
workdir => "workspace/",
parsedir => "pages/",
datadir => "data/",
storyfile => "storyline.txt",
logfile => "autofox.log",
indexfile => "index.html",
storylinebanners => "storylinebanners.txt",
dailyext => ".html",
use_css_navbuttons => 0,
last_day => "",
first_day => "",
previous_day => "",
next_day => "",
last_day_ghosted => "",
first_day_ghosted => "",
previous_day_ghosted => "",
next_day_ghosted => "",
storystart => "storystart.gif",
dailytemplate => "dailytemplate.html",
js_prefix => "af_",
storyline_use_date => 0,
storyline_use_javascript => 1,
# Plain is unused for now
storyline_use_plain => 1,
bigcalwidth => 3,
ddredirect => "",
calbacka => "#d0d0d0",
calbackb => "#b0b0b0",
calhighlight => "#ffffff",
calnolink => "#000000",
# The RSS options
# Base functionality
rss_full_generate => 0,
rss_full_filename => "comicfeed.rdf",
rss_lite_generate => 0,
rss_lite_filename => "comicfeedlite.rdf",
rss_limit => 10,
# Customization
rss_title => "DEFAULT TITLE",
rss_link => "http://localhost/",
rss_description => "Edit this in autofox.cfg!",
rss_copyright => "Copyright sometime... by someone... doing something",
# Image; assume title and link are the same as the base.
rss_image_url => "",
rss_image_width => "",
rss_image_height => "",
# JSON generation options
json_generate => 0,
json_index_filename => "comic",
json_suffix => ".0.json",
json_digest => 1,
);
my $config = "autofox.cfg";
if (defined $ARGV[0]) {
$config = $ARGV[0];
}
open CONFIG, $config or die "Exiting: Can't open configuration file '$config'. $!\n";
while (<CONFIG>) {
chomp;
next if /^#/; next if /^\s*$/;
s/\s*$//;
/(\S*)\s*=\s*(.*)/;
$conf{$1} = $2;
}
close CONFIG;
open(LOGFILE, ">>$conf{logfile}") or print "Can't open '$conf{logfile}' for logging: $!\nNon-fatal, but you're about to get a bunch of warning messages from Perl and\nyou won't have a nice log to look at.\n";
my $calbacka = $conf{calbacka};
my $calbackb = $conf{calbackb};
my $calhighlight = $conf{calhighlight};
my $calnolink = $conf{calnolink};
# Now we horribly and dangerously abuse the properties of a hash.
# FIXME: did some quick'n'dirty stuff here 20031104. Needs cleaned up. -Teg
# FIXME (2021-12-31): Maybe keeping everything in %conf is a cleaner idea? -Nick
# 2004-01-03 (Spirit to land tonight!) Just noticed this FAQ entry:
# http://www.perldoc.com/perl5.8.0/pod/perlfaq4.html#How-do-I-process-an-entire-hash-
# Modified code accordingly. Methinks it's faster, not that it's likely to make
# a whole lot of difference... -Teg
my $basedir = $conf{basedir};
my $sitedir = $conf{sitedir};
my $dailydir = $conf{dailydir};
my $imagedir = $conf{imagedir};
my $comicsdir = $conf{comicsdir};
my $workdir = $conf{workdir};
my $parsedir = $conf{parsedir};
my $datadir = $conf{datadir};
my $uploaddir = $conf{uploaddir};
my $rss_full_generate = $conf{rss_full_generate};
my $rss_full_filename = $conf{rss_full_filename};
my $rss_lite_generate = $conf{rss_lite_generate};
my $rss_lite_filename = $conf{rss_lite_filename};
my $rss_limit = $conf{rss_limit};
my $rss_title = $conf{rss_title};
my $rss_link = $conf{rss_link};
my $rss_description = $conf{rss_description};
my $rss_copyright = $conf{rss_copyright};
my $rss_image_url = $conf{rss_image_url};
my $rss_image_width = $conf{rss_image_width};
my $rss_image_height = $conf{rss_image_height};
my $json_generate = $conf{json_generate};
my $json_index_filename = $conf{json_index_filename};
my $json_suffix = $conf{json_suffix};
my $json_digest = $conf{json_digest};
# basedir CAN be relative to the execution path. You shouldn't do that, but
# you can if you so wish. However, for path-assembling purposes, it must end
# with a forward slash. We can attach that as need be.
print "basedir doesn't start with a slash; this isn't fatal, and it'll be relative to\nwherever you executed the script, but chances are you didn't want that.\n" unless $basedir =~ /^\//;
$basedir = "$basedir/" unless $basedir =~ /\/$/;
# Both sitedir and workdir CAN refer to absolute locations, if they start with a
# forward slash. If not, they get basedir stapled onto the front of them.
foreach ($sitedir, $workdir) {
$_ = "$basedir$_" unless /^\//;
$_ = "$_/" unless /\/$/;
}
# All other dirs should also end in a slash. Starting with a slash is
# irrelevant; they'll all go under their respective directories no matter what
# (unless, say, the user puts .. in the path to break out, but if that's the
# case, it's their own fault).
foreach ($dailydir, $imagedir, $comicsdir, $workdir, $parsedir, $datadir, $uploaddir) {
$_ = "$_/" unless /\/$/;
}
while (my ($key, $val) = each %conf) {
if ($key =~ /(?:_day$|^storystart$)/ and $val =~ /^$/) {
foreach my $fn (<$sitedir$imagedir$key.*>) {
$conf{$key} = "$fn";
last if $fn =~ /\.png$/; # Favouristic fiat.
}
}
}
my $storyfile = $conf{storyfile};
my $dailytemplate = $conf{dailytemplate};
my $dailyext = $conf{dailyext};
my ($updatehour, $updatemin) =
($conf{updatetime} =~ /(\d\d).*(\d\d)/);
my $timezone = $conf{timezone};
my $updateday = $conf{updateday};
my $js_prefix = $conf{js_prefix};
my $storyline_use_date = $conf{storyline_use_date};
my $storyline_use_javascript = $conf{storyline_use_javascript};
### FIXME: This is currently ignored (if javascript is false,
### this is implied)!
my $storyline_use_plain = $conf{storyline_use_plain};
my $ddredirect = $conf{ddredirect};
my $use_css_navbuttons = $conf{use_css_navbuttons};
my $first_day = $conf{first_day};
my $last_day = $conf{last_day};
my $previous_day = $conf{previous_day};
my $next_day = $conf{next_day};
my $first_day_ghosted = $conf{first_day_ghosted};
my $last_day_ghosted = $conf{last_day_ghosted};
my $previous_day_ghosted = $conf{previous_day_ghosted};
my $next_day_ghosted = $conf{next_day_ghosted};
my $storystart = $conf{storystart};
my $captionsfile = $conf{captionsfile};
my $storylinebanners = $conf{storylinebanners};
my $url = $conf{url};
$url .= "/" unless($url =~ /\/$/);
my $bigcalwidth = $conf{bigcalwidth};
my $dir = "";
# Declares whether the Javascript headers have been called yet.
# This gets reset on EVERY new page, but since I don't think it's
# prudent to either keep passing it to each header-using function or
# have the parse function deal with it, it's global.
my $headers_placed = 0;
# Declares whether the Javascript story dropdown has been created
# yet. The problem is that in order for the Javascript to work,
# the dropdown needs its own object name. If the story dropdown
# is called twice, both will have the same name, causing conflicts
# in Javascript parsing that I'd rather not deal with. This also
# applies to the full storyline dropdown, given it's patently stupid
# to put both on the same page.
# Like $headers_placed, it's global and is reset per-page.
my $js_story_placed = 0;
# How many includes deep we are. In the case of zero, we're resetting the
# header data. Declared up here because we need it now, I guess.
my $includecount = 0;
#=======================================================================
# Version string and startup log message. -Teg
#
# I moved the version string up to immediately after the use statements.
# -Spam
aflog("AutoFox $afversion running for $url...");
# Let's do some directory sanity checking first!
sub checkdirectoryexists($$) {
my $varname = shift;
my $dir = shift;
(-d $dir) or affatal("$varname ($dir) isn't a directory!");
}
sub checkdirectoryread($$) {
my $varname = shift;
my $dir = shift;
checkdirectoryexists($varname, $dir);
(-r $dir) or affatal("$varname ($dir) isn't readable!");
}
sub checkdirectoryreadwrite($$) {
my $varname = shift;
my $dir = shift;
checkdirectoryread($varname, $dir);
(-w $dir) or affatal("$varname ($dir) isn't writeable!");
}
# basedir needs to be there and readable. We attempt to chdir into it first
# thing, after all.
checkdirectoryread("basedir", $basedir);
# workdir doesn't really need to be readable on its own. We directly look at
# the subdirs and shouldn't have reason to explicitly read from workdir.
checkdirectoryexists("workdir", $workdir);
# sitedir, however, DOES need to be explicitly writeable. That's where we're
# going to dump the final products, after all.
checkdirectoryreadwrite("sitedir", $sitedir);
# uploaddir and comicsdir need to be writeable (the former to remove comics, the
# latter to add them in).
checkdirectoryreadwrite("comicsdir", $sitedir . $comicsdir);
checkdirectoryreadwrite("uploaddir", $workdir . $uploaddir);
# dailydir also needs to be writeable (that's where the archive is built up).
checkdirectoryreadwrite("dailydir", $sitedir . $dailydir);
# imagedir, parsedir, and datadir only need read access. Those just contain
# image filenames (for URLs), templates (for building the site), and data files
# (for other parsing bits).
checkdirectoryread("imagedir", $sitedir . $imagedir);
checkdirectoryread("parsedir", $workdir . $parsedir);
checkdirectoryread("datadir", $workdir . $datadir);
aflog("Entering $basedir...");
chdir($basedir) or affatal("Couldn't chdir into $basedir: $!");
aflog("Using $sitedir as the site directory...");
aflog("Using $workdir as the workspace directory...");
#=======================================================================
# Time zone hackery. Do you know how many time zones there are in the
# Soviet Union? -Spam
# The server's time zone. Useful in the upcoming calculations with the user's
# inputted time zone.
my $servertimezone = strftime("%z",localtime);
# Time zone's formats are, in general, the +/-nnnn format.
# But, since I'm feeling nice, the initial number can be ignored in the case
# of less-than-10-hours-off. As in, EST can be either -500 or -0500.
# If this turns out to be a bogus format, we default it to the server's time
# zone.
if($timezone =~ /^(\+|\-)/) {
$timezone =~ /^(\+|\-)(\d\d\d\d|\d\d\d)/;
my $temp = $1 . $2;
# Basic sanity check
if($temp < -1200 or $temp > 1200 or abs($temp) % 100 >= 60) {
aflog("Config error: timezone is not valid (needs to be in the +/-XXXX hours format), reverting to system time zone...");
$timezone = $servertimezone;
}
# If the format matches, then $timezone is set properly. Done!
} else {
aflog("Config error: timezone is not valid (needs to be in the +/-XXXX hours format), reverting to system time zone...");
$timezone = $servertimezone;
}
#=======================================================================
# Moving stuff from $uploaddir to $comicsdir. -Teg
# Also creates $curdate.
my %strips;
my $curdate;
# Get the current date as per GMT, then convert it to whatever time zone the
# user may or may not have requested. Yes, this depends on the server knowing
# what time zone it's in. I hereby declare servers that do not know this as
# "braindead".
my $tzoffset;
{
my $houroffset = (int ($timezone / 100)) * 3600;
my $minuteoffset = (abs($timezone) % 100) * 60;
if($timezone < 0) { $minuteoffset *= -1 };
$tzoffset = $houroffset + $minuteoffset;
}
# Right here, we get $curdate set. For the purposes of execution, $curdate
# will always be adjusted for the current time zone.
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time + $tzoffset);
$mon++;
$year += 1900;
if ($conf{updateday} eq "previous") {
if ($hour > $updatehour || ($hour == $updatehour && $min >= $updatemin)) {
my $mmon = $mon+1;
my $mmday = $mday+1;
fdn($mon, $mmon, $mmday);
if ($mday == getndays($year, $mon)) {
$curdate = $year . $mmon . "01";
} else {
$curdate = $year . $mon . $mmday;
}
} else {
fdn($mon, $mday);
$curdate = $year . $mon . $mday;
}
} elsif ($updateday eq "same") {
my $mmday = $mday-1;
fdn($mmday, $mon, $mday);
if ($hour > $updatehour || ($hour == $updatehour && $min >= $updatemin)) {
$curdate = $year . $mon . $mday;
} else {
$curdate = $year . $mon . $mmday;
}
} else {
affatal("Invalid value for 'updateday', must be either 'previous' or 'same'. Check the docs again.");
}
foreach (<$workdir/$uploaddir/*>) {
if (/(\d{8})/ and $1 <= $curdate) {
move($_, "$sitedir$comicsdir") ? aflog("moved $_ to $sitedir$comicsdir") : aflog("FAILED TO MOVE $_ TO $sitedir$comicsdir: $!");
}
}
#=======================================================================
# Storyline dropdown box.
# Let's dance. -Spam
my @sdrop;
if (open SDROP, "$workdir$datadir$storyfile") {
@sdrop = <SDROP>;
close SDROP;
}
my %storylinebanners;
if (open STORYBANNERS, "$workdir$datadir$storylinebanners") {
while (<STORYBANNERS>) {
next if /^$|^\s*#/;
my ($name, $url) = /(.*?),(?!.*")(.*?)(?:\r|\n)/;
$name =~ s/^"(.*?)"$/$1/;
$storylinebanners{$name} = $url;
}
close STORYBANNERS;
}
# The storyline preparser.
# This will make it easier to just grab this preparsed data and throw
# it together later on. Trust me.
# The @stories array will be a list of anonymous hashes. Each hash has
# four keys:
# name: The storyline name
# path: The URL of the storyline
# date: The date in yyyymmdd format
# (yes, I know that's not the format it's in in the storyline file)
# depth: How many layers deep the storyline is
# I have no idea why I did it like this. I must've been REALLY tired.
my @stories;
{
my $index = 0;
foreach (@sdrop) {
next if /^$|^\s*#/;
my ($name, $path, $date) = /(.*?)\s*,\s*(?!.*")(.*?)(?:\s*,\s*(.*?))?(?:\r\n|\r|\n)/;
# Catch quotes
$name =~ s/^(\s*)"(.*?)"$/$1$2/;
my ($cname) = ($name =~ /^\s*(.*)/); # "c"lean name
# Any space that PRECEDES the name of a storyline (i.e.
# collapseable level indicators) are replaced by a
# DOUBLE .
my ($tempspaces) = ($name =~ /^( *)/);
my $depth = length $tempspaces;
$tempspaces =~ s/ / /g;
$name =~ s/^( *)/$tempspaces/;
# Compatibility with Keenspace: Knock out leading @ symbol
# (don't ask, I don't get why they made it so you need an @ to make an
# external URL)
$path =~ s/^\@//;
# Make date in yyyymmdd format
my $pdate;
if (defined($date)) {
# Hi. Undocumented bit. The $storyline_use_date variable
# determines whether to use the actual date field (true) or
# Keenspace-like interpretation (false, default).
#
# I actually think it ought to be documented. I was also
# thinking about reversing it to make the AutoKeen behaviour
# the non-default. -Teg
my ($mon,$day,$year);
if ($storyline_use_date) {
# This way, people can use either YYYYMMDD or MM/DD/YYYY. I'm
# also going to add a config option to switch between American
# and standard XX/XX/YYYY formats, I think. -Teg
if ($date =~ /\//) {
($mon, $day, $year) = ($date =~ /(\d\d|\d)\/(\d\d|\d)\/(\d\d\d\d|\d\d)/);
fdn($mon, $day);
# Now, catch two-digit years, Y2K be damned!
if($year < 100) {
# I figure 1981 is safe enough as a lower bound
# of two-digit dates. And coders of yore figured
# two-digit dates were safe bounds, too.
if($year >= 80 and $year <= 99) {
$year += 1900;
} else {
$year += 2000;
}
}
$date = "$year$mon$day";
} else {
# Sanity check of the weakest order.
($date) = ($date =~ /(\d{8})/);
}
} else {
($date) = ($path =~ /(\d{8})/);
}
} elsif ($path =~ /(\d{8})/) {
# Magic bit to make SELECTED work on storylines with no date field.
# See poorly-formatted code in storylinesubcheck() for rest of magic.
# -Teg
$pdate = $1;
}
# At this point, we have either $date or $pdate, I think. If
# either are beyond the current date, we ignore this entry, as
# it hasn't happened yet and we'd be linking to a non-existant
# file.
next if((defined($date) and $date > $curdate) or (defined($pdate) and $pdate > $curdate));
my $banner = exists $storylinebanners{$cname} ? $storylinebanners{$cname} : '';
# Now throw it all together
$stories[$index++] = {
name => $name,
cname => $cname,
path => $path,
date => $date,
pdate => $pdate,
banner => $banner,
depth => $depth
}
}
}
# The stories array is made. That's done, at least.
# Now, the subordinate array.
### There was a reason I made @subordinate list all the subordinates to each
### entry as opposed to just making it a list of flags (i.e. does this entry
### have ANY subordinates), but I can't remember what it was. I kept the code
### in, but commented out. It also threw a compile error.
#
# Said error might have had something to do with the reference to '@$subordinate' :) -Teg
# (later) Or I might be an idiot that never read part of the docs correctly. :P -Teg
my @subordinate;
for(my $i = 0; $i <= $#stories; $i++) {
# Starting with the first story, we check anything after it to make an
# array of what's underneath each story. Really.
$subordinate[$i] = 0;
my $startdepth = $stories[$i]->{depth};
for(my $j = ($i + 1); $j <= $#stories; $j++) {
# ASSUMPTION: We only go down one depth at a time!
# (I think it would be paradoxial to go down more than one at a time; I can't
# even begin to imagine how it might be defined. -Teg)
# If this is directly subordinate, add it to the fray.
$subordinate[$i] = 1 if ($stories[$j]->{depth} == ($startdepth + 1));
# If this is more than one level subordinate, skip it.
next if($stories[$j]->{depth} > ($startdepth + 1));
# If this is equal or higher, terminate the loop.
last if($stories[$j]->{depth} <= $startdepth);
}
}
# The big block of storyline subroutines. If given the time to reorganize
# everything, these would go in a separate file. We have GOT to get back to
# work on Autofox, you know that?
# Now, storyline() needs an argument, the current date. This is used to check
# where we are in the story so we know where the collapseables should go to
# and what should be selected. It IS always defined, right, Teg? :-)
#
# Yes, though parsetags() doesn't actually check to make sure it is. It just
# relies on us not screwing up in calling it. ;) -Teg
sub storyline {
my $fulldate = shift;
my $line;
my @remainingstories;
my $selected = 0;
my $no_this_is_cgi = 0;
# If this is Javascripty AND the headers have been called,
# set it up as a Javascripty bit. Otherwise, fall to CGI.
### FIXME: The CGI interface uses "dropdown" as the parameter name,
### but the Javascript one uses "page".
if($storyline_use_javascript) {
# Error checking
if($headers_placed and !$js_story_placed) {
$line = qq(<select name="$js_prefix).qq(story">\n);
} else {
if(!$headers_placed) {
$line = "ERROR: Config says we want the Javascript dropdown, but the Javascript header hasn't been declared for this page yet! Defaulting to CGI dropdown...<br>\n";
$line = qq(<form method="post" action="$ddredirect"><select name="dropdown">\n);
$no_this_is_cgi = 1;
}
if($js_story_placed) {
$line = "ERROR: Config says we want the Javascript dropdown, but it's already been used on this page! Ignoring this tag to avoid Javascript namespace collisions...<br>\n";
return $line;
}
}
} else {
# If it's not set at all, we can safely assume CGI.
$line = qq(<form method="post" action="$ddredirect"><select name="dropdown">\n);
}
# One recurse later, we'll be in business.
$selected = storylinesubcheck(0,$fulldate,\@remainingstories);
# We now have @remainingstories! And the crowd goes wild.
# We also have $selected, so we know what's selected. Booyaa!
for (my $i = 0; $i <= $#remainingstories; $i++) {
my $link;
my $selectedtext;
if ($remainingstories[$i]->{path} eq "NULL" or $remainingstories[$i]->{path} eq "") {
$link = "NULL";
} elsif ($remainingstories[$i]->{path} =~ /^http\:\/\//) {
$link = $remainingstories[$i]->{path};
} else {
$link = $url.$remainingstories[$i]->{path};
}
# Hey! Catch initial double-slashes!
# Smallish problem if the URL ends with a slash and the storyline
# URL starts with a slash... realistically, it doesn't matter in a
# practical sense (any sane webserver should know what to do with it),
# but it looks ugly. So there.
$link =~ s/^(http\:\/\/(.*?)\/)\//$1/;
if ($i == $selected) {
$selectedtext = qq( selected="selected");
} else {
$selectedtext = "";
}
# A NULL entry is disabled. Unclickable. Ignored by the Javascripty bit.
if ($link eq "NULL" or $link eq "") {
$selectedtext .= qq( disabled="disabled");
}
$line .= qq(<option value="$link"$selectedtext>$remainingstories[$i]->{name}</option>\n);
}
if($storyline_use_javascript and !$no_this_is_cgi) {
$line .= qq(</select> <input type="button" value="Go!" onclick=").$js_prefix.qq(story_go();" />\n);
} else {
$line .= qq(</select> <input type="submit" value="Go!" /></form>\n);
}
return $line;
}
# Basically, I just ripped the code out of the top of storylinestart()
# to make getstoryline() to make a quick and easy way to get at the
# storyline for any given date from anywhere in the code. It takes
# the normal fulldate argument and returns the hash for the appropriate
# storyline.
# -Teg 2004-01-11
sub getstoryline {
my $fulldate = shift;
my @remainingstories;
my $selected = 0;
# Same deal as in the main storyline checker, but with a twist at the end
$selected = storylinesubcheck(0, $fulldate, \@remainingstories);
return $remainingstories[$selected];
}
sub storylinebanner {
my $fulldate = shift;
my $storyline = getstoryline($fulldate);
my $line = qq(<a href="$url$storyline->{path}">);
unless ($storyline->{banner} eq '') {
$line .= qq(<img src="$storyline->{banner}" alt="$storyline->{cname}" title="$storyline->{cname}">)
} else {
$line .= qq($storyline->{cname});
}
$line .= qq(</a>);
return $line;
}
sub storylinestart {
# Return a link to the start of the current storyline.
my $fulldate = shift;
my $link;
# Now, all we need is the path of the resulting story. And because I want
# to be fancy, I'll alt/title tag it, too.
my $storyline = getstoryline($fulldate);
my $name = $storyline->{cname};
if ($storyline->{path} =~ /^http\:\/\//) {
$link = $storyline->{path};
} else {
$link = $url.$storyline->{path};
}
# Same deal as in storyline().
$link =~ s/^(http\:\/\/(.*?)\/)\//$1/;
if (-e ("$sitedir$imagedir$storystart")) {
return qq(<a href="$link"><img src="$url$imagedir$storystart" alt="Start of $name" title="Start of $name" border=0></a>);
} else {
return qq(<a href="$link">Start of $name</a>);
}
}
sub storylinesubcheck {
my $startline = shift;
my $fulldate = shift;
my $remainingstories = shift;
my $selected = 0;
my $tempselected;
my $firstdepth = $stories[$startline]->{depth};
# Now then.
# Now. Then.
# Take a look at each storyline past where we are now. We're looking for
# the next storyline of the same depth, if one exists. After this, we
# check to see if the current date falls between the two (or is greater
# than the first one if no more of the same depth exist). If it does, or
# if the storyline has no date declared for it, we dive into the next
# depth level and check all THOSE for similar conditions until we run out
# of storylines at that depth. Of course, once depth zero terminates, we
# return back to storyline() to dump it all out.
# As we come across each valid storyline, we push its full $stories data
# into the @remainingstories hash, via reference passed between each call
# to this recursive curse.
for (my $i = $startline; $i <= $#stories; $i++) {
# We're only checking one depth at a time.
next if ($stories[$i]->{depth} > $firstdepth);
# If this is ABOVE the starting depth, we're out of the current depth
# check and can thus stop here.
last if ($stories[$i]->{depth} < $firstdepth);
# This storyline gets added in.
push @$remainingstories, $stories[$i];
# If the date of this one is less than the current date, bump up the
# SELECTED variable to this entry.
$selected = (scalar @$remainingstories - 1) if (
(defined($stories[$i]->{date}) and $stories[$i]->{date} <= $fulldate)
or # (The rest of the magic. -Teg)
(defined($stories[$i]->{pdate}) and $stories[$i]->{pdate} <= $fulldate)
);
# Safely skip this if there's no subordinates to check.
next if ($subordinate[$i] == 0);
my $j;
# Seek the next same-level storyline.
for ($j = ($i + 1); $j <= ($#stories + 1); $j++) {
# We've fallen out of the array. Thus, this depth is unmatched
# and this terminates the storyline array in general.
last if ($j > $#stories);
# Jackpot. Bingo. Yahtzee. We have a winner.
last if ($stories[$j]->{depth} <= $firstdepth);
# Anything below this depth is skipped.
next if ($stories[$j]->{depth} > $firstdepth);
}
# Okay. We've got the end of the depth. $i is the start, $j is the
# end. Simple.
# Main case: If the current storyline has an undefined date, proceed
# to the next depth.
if (!defined($stories[$i]->{date})) {
$tempselected = storylinesubcheck($i + 1, $fulldate, $remainingstories);
$selected = $tempselected if(defined($tempselected) and $tempselected > $selected);
} elsif ($j > $#stories) {
# Off the end
if ($stories[$i]->{date} <= $fulldate) {
# The date falls here, spit out the next chunk.
$tempselected = storylinesubcheck($i + 1, $fulldate, $remainingstories);
$selected = $tempselected if(defined($tempselected) and $tempselected > $selected);
} else {
next;
}
} elsif ($stories[$i]->{date} <= $fulldate and $stories[$j]->{date} > $fulldate) {
# It falls in here somewhere.
$tempselected = storylinesubcheck($i + 1, $fulldate, $remainingstories);
$selected = $tempselected if(defined($tempselected) and $tempselected > $selected);
} else {
next;
}
}
return $selected;
}
sub storylinefull {
# Returns the entire storyline dropdown with no collapsing.
# Still takes a fulldate so we select the proper default.
my $fulldate = shift;
my $selected = 0;
my $line;
my $no_this_is_cgi = 0;
# If this is Javascripty AND the headers have been called,
# set it up as a Javascripty bit. Otherwise, fall to CGI.
### FIXME: The CGI interface uses "dropdown" as the parameter name,
### but the Javascript one uses "page".
if($storyline_use_javascript) {
# Error checking
if($headers_placed and !$js_story_placed) {
$line = qq(<select name="$js_prefix).qq(story">\n);
} else {
if(!$headers_placed) {
$line = "ERROR: Config says we want the Javascript dropdown, but the Javascript header hasn't been declared for this page yet! Defaulting to CGI dropdown...<br>\n";
$no_this_is_cgi = 1;
$line = qq(<form method="post" action="$ddredirect"><select name="dropdown">\n);
}
if($js_story_placed) {
$line = "ERROR: Config says we want the Javascript dropdown, but it's already been placed on this page! Ignoring this tag to avoid Javascript namespace collisions...<br>\n";
return $line;
}
}
} else {
# If it's not set at all, we can safely assume CGI.
$line = qq(<form method="post" action="$ddredirect"><select name="dropdown">\n);
}
# Since we're not doing any collapse logic, all we really need to do is
# grab the storyline array and chuck out EVERYTHING. Except we do need
# to make sure we stop on the right entry for default purposes. Or
# default porpoises. That's all this loop does.
for (my $i = 0; $i <= $#stories; $i++) {
next unless(defined($stories[$i]->{date}));
if($stories[$i]->{date} <= $fulldate) {
$selected = $i;
}
}
# $selected is now set. Loop again through the hash.
# Yes, this DOES look familiar. From storyline() above.
for (my $i = 0; $i <= $#stories; $i++) {
my $link;
my $selectedtext;
# To avoid confusion for now, translate ALL dropdown
# destinations to absolute paths.
if ($stories[$i]->{path} eq "NULL" or $stories[$i]->{path} eq "") {
$link = "NULL";
} elsif ($stories[$i]->{path} =~ /^http\:\/\//) {
$link = $stories[$i]->{path};
} else {
$link = $url.$stories[$i]->{path};
}
$link =~ s/^(http\:\/\/(.*?)\/)\//$1/;
if ($i == $selected) {
$selectedtext = " SELECTED";
} else {
$selectedtext = "";
}
if ($link eq "NULL") {
$selectedtext .= " DISABLED";
}
$line .= qq(<option value="$link"$selectedtext>$stories[$i]->{name}</option>\n);
}
if($storyline_use_javascript and !$no_this_is_cgi) {
$line .= qq(</select> <input type="button" value="Go!" onClick=").$js_prefix.qq(story_go();">\n);
} else {
$line .= qq(</select> <input type="submit" value="Go!"></form>\n);
}
return $line;
}
#=======================================================================
# Pulls in filenames from $comicsdir and loads them into various hashes
# for use later on (in the case of .tag, .cap, and .alt files, it
# actually opens the files and reads in the contents). -Teg
my %dayhasstrip;
my %monthhasstrip;
my %captions;
my %alts;
while (my $nextname = <$sitedir$comicsdir*>) {
$nextname =~ s/.*\///;
if ($nextname =~ /((\d\d\d\d)(\d\d)(\d\d)).*\.(?:gif|jpg|jpeg|png|bmp|tiff|txt|html|htm)$/i) {
push (@{$strips{$1}}, $nextname);
$dayhasstrip{$2}[$3][$4] = 1;
$monthhasstrip{$2}{$3} = 1;
} elsif ($nextname =~ /((\d\d\d\d)(\d\d)(\d\d)).*\.(tag|cap)$/i) {
open (CAPFILE, "$sitedir$comicsdir$nextname") or die "Can't open caption file $sitedir$comicsdir$nextname: $!";
my $caption = join '', <CAPFILE>;
$caption =~ s/^\s+|\s+$//g;
close CAPFILE;
my ($filename) = ($nextname =~ /(^(.*)\.(?:gif|jpg|jpeg|png|bmp))/);
if($filename eq "") {
aflog("WARNING: Caption file $nextname does not appear to be associated with any file!");
next;
}
if (defined $captions{$filename}) {
aflog("WARNING: More than one entry in captions list for $filename !! Later entries take precedence!");
}
$captions{$filename} = $caption;
} elsif ($nextname =~ /((\d\d\d\d)(\d\d)(\d\d)).*\.alt$/i) {
# Remember, alt-text is NOT the same as title text!
open (ALTFILE, "$sitedir$comicsdir$nextname") or die "Can't open alt-text file $sitedir$comicsdir$nextname: $!";
my $alts = join '', <ALTFILE>;
$alts =~ s/^\s+|\s+$//g;
close ALTFILE;
my ($filename) = ($nextname =~ /(^(.*)\.(?:gif|jpg|jpeg|png|bmp))/);
if($filename eq "") {
aflog("WARNING: Alt-text file $nextname does not appear to be associated with any file!");
next;
}
if (defined $alts{$filename}) {
aflog("WARNING: More than one entry in alt-text list for $filename !! Later entries take precedence!");
}
$alts{$filename} = $alts;
} else {
aflog("WARNING: Don't know how to handle $nextname, skipping...");
}
}
#=======================================================================
# Pulls in captions from captions.txt. I don't have a counterpart for
# alt-texts; I'm just going with the per-file system there.
# -Teg and/or Spam
if (open (CAPTIONS, $captionsfile)) {
foreach (<CAPTIONS>) {
chomp;
my ($filename, $caption) = ($_ =~ /(\S*) (.*)/);
if (defined $captions{$filename}) {
aflog("WARNING: More than one caption for $filename !! Later entries take precedence!");
}
$caption =~ s/^\s+|\s+$//g;
$captions{$filename} = $caption;
}
close CAPTIONS;
}
#=======================================================================
my @daylist = sort keys %strips;
my ($fstrip, $fyear, $fmonth, $fday) =
($daylist[0] =~ /((\d\d\d\d)(\d\d)(\d\d))/);
my ($lstrip, $lyear, $lmonth, $lday) =
($daylist[$#daylist] =~ /((\d\d\d\d)(\d\d)(\d\d))/);
aflog("(re)generating pages...");