-
Notifications
You must be signed in to change notification settings - Fork 0
/
avantify.cgi
2990 lines (2595 loc) · 90.8 KB
/
avantify.cgi
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
############################################################################
# AvantSlash
my $version = "4.16";
# Copyright (c) Richard Lawrence and Han-Kwang Nienhuys 2000-2015
############################################################################
#
# 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
#
############################################################################
# For instructions please read the file called README.
# Settings should be changed within the file "config.local", using
# "config.dist" as an example.
#
# If the configuration and cache are not in the current directory,
# then delete the next line, remove the # from the line after it and
# set $path accordingly.
my $path = ""; # default: empty string
# my $path = "/home/richard/public_html/avantslash/";
#
# You should not need to change anything below this line.
############################################################################
use strict;
use CGI::Carp qw(fatalsToBrowser);
use POSIX;
use LWP;
use CGI qw(:param);
use URI::Escape;
use Cwd;
# Change directory
if ($path ne "") {
chdir($path) || die("Path '$path': $!");
}
# In case of any problems
$SIG{ALRM} = sub { exit };
alarm(35);
use constant ONE_MINUTE => 60;
my $self_url = $ENV{'SCRIPT_NAME'};
my $clean_html = "";
my $clean_done = 0;
my $thisyear = 1900 + (localtime(time))[5];
my $page = ""; # raw slashdot HTML code
my @missing_vars; # variables missing in configuration
my %fetch_info = qw(url undef
cache undef
pagedate 0
statuscode undef
);
# special categories of cache lifetime (in units of 1 minute)
my %cache_life_min = ("ckupdate", 7*24*60,
"ajax", 120,
"issue", 180,
"search", 180,
"css", 24*60,
"democalls", 8*60,
"bookmarks", 60*24*365, # 1 year
);
# Global variables
my $first_js = 1; # static variable for javascript output
my %timeouts = ("slashdot", 30, "avantslash", 8); # in seconds
# %prefs uses same keys as configuration file, but obligatory lowercase
my %prefs = qw(soylent 0
compressed_characters 100
threshold 4
mp_show_posted_by 1
mp_show_department 1
mp_show_blurb 1
mp_show_read_more 1
mp_show_poll 1
mp_max_stories 99
ap_show_posted_by 1
ap_show_department 1
ap_show_story 1
ap_threshold_options 1
max_comments 0
no_compressed_comments 0
extra_links 0
default_stylesheet mini_slashdot
timezone auto
time_fmt %F_%H:%M
cache_timeout 30
debugging 0
dump_reformatted_html 0
enable_local_cache 1
check_updates 1
demo_mode 0
);
my $as_user_agent = "Mozilla/5.0 (Linux; rv:53.0) Gecko/53.0 Firefox/53.0)";
# override settings in demo mode (also demo_mode!)
my %demo_settings = qw(
cache_timeout 360
max_comments 20
mp_max_stories 10
demo_mode 1
enable_local_cache 1
);
# these preferences can take non-numeric or negative values
my %prefs_non_numeric = qw(default_stylesheet 1 timezone 1 time_fmt 1);
# these url parameters are allowed in demo mode (regexp)
my $demo_cgi_whitelist = "ajax|base|ckupdate|comments|css|issue|setcss|showcredits|showprefs|style|threshold|ua|referer|xlinks|force_reload|return";
# Load in config file
@missing_vars = set_configuration();
# Process CGI parameters and proceed
process_cgi_params();
exit;
# sanitize and process CGI parameters
# argument: ref to sanitized parameter hash
sub process_cgi_params {
# check abuse in demo mode
$prefs{'demo_mode'} && demo_sanity_check();
# Allow people to override debugging values
if (cgi_param("debug")) {
$prefs{"debugging"} = cgi_param("debug");
$prefs{"debugging"} =~ s/[^0-9]/0/g;
}
if (cgi_param("xlinks")) {
$prefs{"extra_links"} = cgi_param("xlinks");
}
if ((my $issue = cgi_param("issue")) ne "") {
# older front page, 0=today, 1=yesterday, etc.
$issue =~ s/[^0-9]//g;
get_html(\$page, "issue", $issue);
parse_main_page("issue");
exit;
}
if ((my $issuedate = cgi_param("issuedate")) ne "") {
# older front page, 0=today, 1=yesterday, etc.
$issuedate =~ s/[^0-9]//g;
(length($issuedate) != 8) && fatal_error("Issue date '".cgi_param("issuedate")."' is not 8 digits.\n");
($issuedate < 19950101 || $issuedate > POSIX::strftime("%Y%m%d", gmtime())) &&
fatal_error("Issue date $issuedate out of range.\n");
get_html(\$page, "issuedate", $issuedate);
parse_main_page("issue");
exit;
}
if (cgi_param("showcredits") eq "1") {
show_credits(@missing_vars);
exit;
}
if (cgi_param("showprefs") eq "1") {
show_prefs(@missing_vars);
exit;
}
if (cgi_param("upgrade") ne "") {
run_upgrade(cgi_param("upgrade"))
}
# check threshold for comments
my $new_threshold = cgi_param("threshold");
if ($new_threshold ne "") {
$prefs{"threshold"} = $new_threshold;
}
if ((my $comments = cgi_param("comments")) ne "") {
# article/poll: comments
# should be something like /13/12/29/0416259 or poll/1234/
$comments =~ s![^\d/,pol]!!g;
$comments = sanitize_string_withslash($comments);
get_html(\$page, "comments", $comments);
parse_comments();
} elsif ((my $ajax = cgi_param("ajax")) ne "") {
# article / comments
$ajax = sanitize_string_withslash($ajax);
get_html(\$page, "ajax", $ajax);
parse_ajax_comment($ajax);
} elsif ((my $ckupdate = cgi_param("ckupdate")) ne "") {
# check for updates
$ckupdate = ($ckupdate ne "2") ? 1 : 2; # 1=page, 2=generate js
($ckupdate == 1) && purge_cache("ckupdate-*", 30); # force re-check
get_html(\$page, "ckupdate", "z");
parse_ckupdate($ckupdate);
} elsif ((my $css = cgi_param("css")) ne "") {
$css = sanitize_string_noslash($css);
generate_css($css);
} elsif ((my $search = cgi_param("search")) ne "") {
$search =~ s![^ a-zA-Z0-9]!!g;
get_html(\$page, "search", $search);
parse_main_page("search", $search);
} elsif ((my $rss = cgi_param("rss")) ne "") {
$rss = 1;
get_html(\$page, "rss", 0);
parse_rss();
} elsif (cgi_param("setcss") ne "") {
set_css_cookie();
} elsif (cgi_param("bookmark") ne "") {
set_bookmark();
} elsif (cgi_param("delbm") ne "") {
del_bookmark();
} elsif (cgi_param("showbm") ne "") {
show_bookmarks();
} elsif ($prefs{'demo_mode'}) {
# demo mode and nothing else: main page 3 days ago
get_html(\$page, "issue", 3);
parse_main_page("issue");
} else {
get_html(\$page, "main", "");
parse_main_page("main");
}
exit;
}
# parse the RSS page
sub parse_rss {
print("Content-Type: application/rss+xml\n\n");
# cleanup facebook/twitter/doubleclick stuff
$page =~ s/AVANTSLASH_URL=.*$//s;
my $remove_hrefs = join("|",
"twitter.com/home",
"feedads.",
"www.facebook.com/sharer");
$page =~ s!\<a href=\"http://($remove_hrefs).*?\</a\>!!g;
$page =~ s!\<p\>(\<br/\>)?\s*\</p\>!!gs;
$page =~ s!<image rdf:resource.*?/>!!g;
$page =~ s!<image [^>]*>.*?</image>!!gs;
$page =~ s!<link>.*?</link>!!g;
$page =~ s!<iframe.*?</iframe>!!g;
$page =~ s!<img.*?(</img|/)>!!g;
$page =~ s!(</a>) at Slashdot!$1 in AvantSlash!g;
$page =~ s!<feedburner:origLink>.*?</feedburner:origLink>!!g;
repoint_urls(\$page);
print $page;
}
# parse the main page
# uses global $page varibale for html code
# argument: 'search' (+search string) or 'main'
sub parse_main_page
{
return $prefs{soylent} ? parse_main_page_soylent(@_) : parse_main_page_slashdot(@_);
}
sub parse_main_page_soylent
{
my $mptype = shift;
my $search;
if ($mptype eq "search") {
$search = shift;
show_header("$search - Soylent search");
} elsif ($mptype eq "issue") {
show_header("Avantslash: older issue");
} else {
show_header("Avantslash: Soylent is People");
}
if ($#missing_vars >= 0)
{
print "<p><div class=\"error\">\n".
"Some configuration settings are missing which may affect your reading experience\n".
"[<a href=\"$self_url?showcredits=1\">more</a>]</div></p>";
}
if ($mptype eq "search") {
print("<div class='searchheader'>\n".
"Searched for '$search'.<br>\n".
search_form($search).
"</div>");
}
my ($title, $blurb, $posted, $dept, $more, $hiddenstory, $ncomments);
$title = $dept = $more = $posted = $blurb = $ncomments = "???";
$hiddenstory = "";
my $poll_story = "";
my $storycount = 0;
my @bits = split(/\n/, $page);
foreach (@bits)
{
# first, watch out for the small stories with no blurb
if ($prefs{"debugging"} >= 2) { dumpline(); }
# title
if (m!<div class=\"title\b.*<h3[^>]*>(.*?)</h3>!)
{
$title = remove_tags_except_basic($1);
# Strip all HTML tags from the line
$title =~ s/<.+?>//g;
debug_msg("title", $title) if ($prefs{"debugging"});
}
# search result
elsif (m!<div class=\"search-results\">.*?<a href=\".*?sid=([\d/]+)\">(.+?)</a>!) {
$more = $1;
$title = $2;
if ($prefs{"debugging"}) {
debug_msg("title", $title);
debug_msg("more", $more);
}
$posted="";
if ($more =~ m!^(\d\d)/(..)/(..)/!) {
$posted = "Posted on 20$1-$2-$3.";
}
$dept="";
}
# or poll
if ($prefs{"mp_show_poll"} && m!<div [^>]*id=\"poll-content\">(.*?)</form>!s) {
my $x = $1;
if ($x =~ m!<input[^>]+\"mainpage\">\s*<p><b>(.*?)</b>!) {
$title = "Poll: ".remove_tags($1);
}
my $qid = "??";
if ($x =~ m!<a href=[^>]+/pollBooth.pl\?qid=([0-9]+)!) {
$qid = $1;
$more="poll/$1";
}
($x =~ m!Comments:<b>([0-9]+)!) && ($ncomments = $1);
$blurb = "";
while ($x =~ s!<input type=\"radio\"[^>]*>(.*?)</li>!!) {
if ($blurb ne "") { $blurb .= " / "; }
$blurb .= "$1";
}
$blurb .= " [<a href=\"https://soylentnews.org/pollBooth.pl?qid=$qid\">cast your vote</a>]";
$posted = $dept = "";
$hiddenstory = " hiddenstory";
if ($prefs{"debugging"}) {
debug_msg("title", $title);
debug_msg("blurb", $blurb);
debug_msg("qid", $qid);
debug_msg("ncomments", $ncomments);
}
}
# posted
elsif (m!<div class="details"[^>]*>(.+?)<br>(.+?)</div>!)
{
$posted = remove_tags($1);
$dept = remove_tags($2);
# Remove any links in the line
$posted =~ s/<a href.+?>(.+?)<\/a>/$1/gi;
$posted =~ s/<.+?>//g;
# Remove any links in the line
$dept =~ s/<a href.+?>(.+?)<\/a>/$1/gi;
$dept =~ s/<.+?>//g;
debug_msg("posted", $posted) if ($prefs{"debugging"});
debug_msg("department", $dept) if ($prefs{"debugging"});
}
# the blurb
elsif (m!<div class=\"intro\">(.*?)</div>!)
{
$blurb = remove_tags_except_basic($1);
$blurb =~ s!<a [^>]+>original submission</a>!!i;
if ($prefs{"debugging"}) {
debug_msg("blurb", $blurb);
debug_msg("hiddenstory", $hiddenstory);
}
}
# link to more
elsif (m!<li class=.more.> <a [^>]*href=\"([^\"]+)\".*>([0-9]+)</a> comments!)
{
# full href is e.g "//soylentnews.org/articles/14/02/23/056215.shtml"
# or //soylentnews.org/article.pl?sid=14/02/27/2036233
$more = $1;
$ncomments = $2;
my $more_raw = $more;
$more =~ s!^.*(articles/|article.pl\?sid=)(\d+/\d+/\d+/\d+)\b.*$!$2!;
if ($prefs{"debugging"}) {
debug_msg("more-raw", $more_raw);
debug_msg("more", $more);
debug_msg("ncomments", $ncomments);
}
}
# display once the last item ($more) is found. This reduces the impact if
# slashdot changes only for one of the items.
if ($more && $more ne "???" && $blurb && $blurb ne "???")
{
debug_msg("Got everything...") if ($prefs{"debugging"});
# clean up any missing blockquotes and repoint external links
cleanup_message(\$blurb);
my $item = "<div class=\"item$hiddenstory\">\n".
"<h2><a href=\"$self_url?comments=$more\">$title</a></h2>\n";
if ($prefs{"mp_show_posted_by"} || $prefs{"mp_show_department"}) {
$item .= "<div class=\"subtitle\">\n";
if ($prefs{"mp_show_posted_by"}) {
$posted =~ s!( on )(.*[AP]M)!$1.reformat_time($2)!e;
$item .= "$posted\n";
}
$item .= "<div>$dept</div>\n" if ($prefs{"mp_show_department"});
$item .= "</div>\n";
}
$item .= "$blurb\n" if ($prefs{"mp_show_blurb"});
my $cmt_anchor = ($mptype eq "search") ?
"(Read more)" : "($ncomments comments)";
$item .= "<p><a href=\"$self_url?comments=$more\">$cmt_anchor</a></p>\n" if ($prefs{"mp_show_read_more"});
$item .= "</div>\n\n";
if ($title =~ /^Poll/) {
$poll_story = $item;
} else {
print($item);
}
$title = $dept = $more = $posted = $blurb = "???";
$hiddenstory = "";
++$storycount;
if ($storycount >= $prefs{'mp_max_stories'}) {
print("<p>(Showed only first $storycount stories.)\n");
last;
}
}
}
if ($prefs{"mp_show_poll"}) {
print($poll_story);
}
status_message(1);
print_ckupdate_js();
# need to see at least poll plus one other story
if ($storycount < 2 && $mptype ne "search") {
print_ckupdate_link("SoylentNews main page");
}
show_footer(1, "<br>".search_form($search));
}
sub parse_main_page_slashdot
{
my $mptype = shift;
my $search;
if ($mptype eq "search") {
$search = shift;
show_header("$search - Slashdot search");
} elsif ($mptype eq "issue") {
show_header("Avantslash: older issue");
} else {
show_header("Avantslash: News for nerds, stuff that matters");
}
if ($#missing_vars >= 0)
{
print "<p><div class=\"error\">\n".
"Some configuration settings are missing which may affect your reading experience\n".
"[<a href=\"$self_url?showcredits=1\">more</a>]</div></p>";
}
if ($mptype eq "search") {
print("<div class='searchheader'>\n".
"Searched Slashdot for '$search'.<br>\n".
search_form($search).
"</div>");
}
my ($title, $story_link,
$blurb, $posted, $dept, $more, $hiddenstory, $ncomments);
$title = $dept = $more = $posted = $blurb = $ncomments = "???";
$hiddenstory = $story_link = "";
my $storycount = 0;
my @bits = split(/\n/, $page);
foreach (@bits)
{
# first, watch out for the small stories with no blurb
if ($prefs{"debugging"} >= 2) { dumpline(); }
if (m!<div class="briefarticle".+?> ?<span class="section">(.+?)</span> ?<span class="storytitle"><a href=".+?sid=(\d\d/\d\d/\d\d/\d{5,7})">(.+?)</a> ?</span>!)
{
my $smsection = $1;
my $smmore = $2;
my $smtitle = $3;
# Remove any tags which may or may not be there
$smtitle =~ s/<.+?>//g;
$smsection =~ s/<.+?>//g;
print "<p><b>$smsection</b> <a href=\"$self_url?comments=$smmore\">$smtitle</a></p>";
}
# title & link to story page
if (m!<article.*?>.*?<span id=\"title-[^>]*> <a [^>]*href=\"([^\"]+)\"[^>]*>(.*?)</a!)
{
my $more_raw = remove_tags_except_basic($1);
$title = remove_tags_except_basic($2);
if (m!<a class="story-sourcelnk" href="(.*?)".*?>(.*?)</a!) {
$story_link =
"\n<div class='storylink'>[Story at <a href='$1'>$2</a>]</div>";
if ($prefs{'debugging'}) {
debug_msg("story_link", "$2 --> $1");
}
$story_link =~ s![\(\)]!!g;
}
$title =~ s/<.+?>//g;
$more = $more_raw;
$more =~ s!^.*/story/([0-9]+/[0-9]+/[0-9]+/[0-9]+)/.*$!$1!;
if (m!<span class=\"comments[^>]*>([0-9]+)</span>!) {
$ncomments = $1;
}
if ($prefs{'debugging'}) {
debug_msg("title", $title);
debug_msg("more-raw", $more);
debug_msg("more", $more);
debug_msg("ncomments", $ncomments);
}
}
# or poll
if ($prefs{"mp_show_poll"} && m!<form id="pollBooth"!) {
if (m!<h3>(.*?)</h3>!) {
$title = "Poll: ".remove_tags($1);
}
$blurb = "";
my $x = $_;
while ($x =~ s!<input type=\"radio\"[^>]+>(.*?)</label>!!) {
if ($blurb ne "") { $blurb .= " / "; }
$blurb .= remove_tags($1);
}
$posted = $dept = "";
if ($x =~ m!<input type="hidden".*?value="(\d+)">!) {
$more = "poll/$1/";
$blurb .= "<p><a href=\"https://slashdot.org/pollBooth.pl?qid=$1\">[Your vote]</a>"
}
if ($prefs{"debugging"}) {
debug_msg("title", $title);
debug_msg("blurb", $blurb);
debug_msg("more", $more);
}
}
# posted
elsif (m!<span class="story-byline">(.*Posted by.*)</time>.*?(from the .* dept.)!) {
$posted = remove_tags($1);
$dept = remove_tags($2);
# Remove any links in the line
$posted =~ s/<a href.+?>(.+?)<\/a>/$1/gi;
$posted =~ s/<.+?>//g;
# Remove any links in the line
$dept =~ s/<a href.+?>(.+?)<\/a>/$1/gi;
$dept =~ s/<.+?>//g;
debug_msg("posted", $posted) if ($prefs{"debugging"});
debug_msg("department", $dept) if ($prefs{"debugging"});
}
# the blurb
elsif (m!<div class=\"(body|hide)\"[^>]*>.*?<div id=\"text-[^>]+>(.+?)</div>!)
{
$blurb = remove_tags_except_basic($2);
$hiddenstory = ($1 eq "hide" && $mptype ne "search") ? " hiddenstory" : "";
if ($prefs{"debugging"}) {
debug_msg("blurb", $blurb);
debug_msg("hiddenstory", $hiddenstory);
}
}
# the blurb for video (not video bytes)
elsif (m!<div class="video-thumb">.*?</div>(.*?)</div>!)
{
$blurb = remove_tags_except_basic($1);
if ($prefs{"debugging"}) {
debug_msg("blurb", $blurb);
debug_msg("hiddenstory", $hiddenstory);
}
}
# display once the last item ($more) is found. This reduces the impact if
# slashdot changes only for one of the items.
if ($blurb && $blurb ne "???")
{
debug_msg("Got everything...") if ($prefs{"debugging"});
# remove the no ad tag
$blurb =~ s/<P> <!-- no ad \d+ -->//gi;
# clean up any missing blockquotes and repoint external links
cleanup_message(\$blurb);
# remove trailing <P> in blurb that suddenly started appearing one day
$blurb =~ s/\s<P>\s*$//i;
print "<div class=\"item$hiddenstory\">\n".
"<h2><a href=\"$self_url?comments=$more\">$title</a></h2>\n";
if ($prefs{"mp_show_posted_by"} || $prefs{"mp_show_department"}) {
print("<div class=\"subtitle\">\n");
if ($prefs{"mp_show_posted_by"}) {
$posted =~ s!( on )(.*[AP]M)!$1.reformat_time($2)!e;
print("$posted\n");
}
print("<div>$dept</div>\n") if ($prefs{"mp_show_department"});
print("</div>\n");
}
print("$blurb\n") if ($prefs{"mp_show_blurb"});
print("<br>$story_link\n".
"<a href=\"$self_url?comments=$more\">($ncomments comments)</a>\n") if ($prefs{"mp_show_read_more"});
print("</div>\n\n");
$title = $dept = $more = $posted = $blurb = "???";
$story_link = $hiddenstory = "";
++$storycount;
if ($storycount >= $prefs{'mp_max_stories'}) {
print("<p>(Showed only first $storycount stories.)\n");
last;
}
}
}
status_message(1);
print_ckupdate_js();
# need to see at least poll plus one other story
($storycount < 2) && print_ckupdate_link("Slashdot main page");
show_footer(1, "<br>".search_form($search));
}
# parse comments
sub parse_comments
{
return $prefs{soylent} ? parse_comments_soylent(@_) : parse_comments_slashdot(@_);
}
# parse comments
sub parse_comments_soylent
{
my ($title, $story, $posted, $dept, $header);
my ($i, $elements);
my $thread_mode = 0;
my $thereismore = 0;
my $shownblurb = 0;
my $thr_cid = "";
my $thr_title = "";
if (cgi_param("comments") =~ m!^\d+,(\d+)$!) {
$thread_mode = 1;
$thr_cid = $1;
}
# in debug mode, start with a standard header
if ($prefs{debugging}) {
show_header("AvantSlash comments debugging");
$header = 1;
}
my @bits = split(/\n/, $page);
$elements = scalar @bits;
my $commentscount = 0;
# comments page headers (soylent)
for ($i = 0; $i < $elements; $i++)
{
$_ = $bits[$i];
if ($prefs{"debugging"} >= 2) { dumpline(); }
# Title for story/thread page
if (!defined($title) && m!<div class=.title.*?<h3.*?>\s*(.*?)\s*</h3>!)
{
$title = remove_tags($1);
$prefs{'debugging'} && debug_msg("title", $title);
if ($thread_mode) {
$posted = "Showing comment thread. (<a href=\"$self_url?comments=$1\">original story</a>)";
$prefs{'debugging'} && debug_msg("posted", $posted);
$thr_title = "($title)";
}
}
# posted
if (m!<div class=\"details[^>]*>(.+?)<br>(.+?)</div>!)
{
$posted = remove_tags($1);
$dept = remove_tags($2);
debug_msg("posted", $posted) if ($prefs{"debugging"});
debug_msg("department", $dept) if ($prefs{"debugging"});
}
if (m!<div class=.intro\b!)
{
$story .= remove_tags_except_basic($_);
# book reviews have more stuff
if (m!<span class=\"bodytext\">(.*)</span>!) {
$story .= remove_tags_except_basic($1);
}
debug_msg("story", $story) if ($prefs{"debugging"});
}
# or poll results
elsif (m!Displaying poll results!) {
while (s!<caption class=\"barAnswer\">(.*?)</caption>.*?class=\"barPercent\">(\d+%)</td>!!) {
$story .= mk_pollbar((0+$2), remove_tags($1));
}
debug_msg("story", $story) if ($prefs{"debugging"});
}
# long stories: part 2
elsif (m!<div class=\"full\">(.*)</div>!) {
my $story2 = remove_tags_except_basic($1);
if ($prefs{'debugging'}) { debug_msg('story_pt2', $story2); }
$story .= $story2;
}
if (m!<div class=\"commentBoxForm\"!) {
debug_msg("Got everything...") if ($prefs{"debugging"});
# Display the header if we haven't already
if (!$header)
{
show_header($title);
$header = 1;
my $js = file_contents('expandmsg_soylent.js');
print("\n<script type=\"text/javascript\">\n".$js."</script>\n");
}
# clean up blockquotes and repoint external links
cleanup_message(\$story);
print "<div class=\"item\">\n";
print "<h2>$title</h2>\n";
if ($thread_mode) {
print "<div class=\"subtitle\">\n";
print("Showing comment thread.");
print("</div>\n");
} else {
print "<div class=\"subtitle\">\n";
if ($prefs{"ap_show_posted_by"}) {
$posted =~ s!( on )(.*[AP]M)!$1.reformat_time($2)!e;
print "$posted\n";
}
print "<div>$dept</div>\n" if ($prefs{"ap_show_department"});
print "</div>\n";
print "$story\n" if ($prefs{"ap_show_story"});
}
$shownblurb = 1;
# bail out to the comments part
last;
}
}
(!$header) && show_header("Comments [could not parse title]");
# Show the threshold change options
if ($prefs{"ap_threshold_options"} && $shownblurb)
{
print "<div class=\"subtitle toppad\">\nShowing comments at threshold " . $prefs{"threshold"};
if (!$prefs{"no_compressed_comments"} && $prefs{"compressed_characters"}) {
print " and summaries at " . ($prefs{"threshold"}-1);
}
print(".\n");
my $urldata = cgi_param("comments");
print("<form method='GET' action='$self_url'>\n".
"<input type=hidden name=comments value='$urldata'>\n".
"Change to: ".
"<select name='threshold' onchange='this.form.submit()'>\n");
foreach my $num (0..5) {
my $selected = ($prefs{"threshold"} == $num) ? " selected" : "";
print(" <option$selected value='$num'> $num </option>\n");
}
print "</select></form>\n</div>";
}
print("</div><!--item-->\n");
##### and now the comments (soylent) #####
print("\n\n");
my ($author, $score, $score_num, $message, $sid, $cid);
my $j;
my $uldepth = -1; # keep track of comment indentation
my $current_title = "<blah>";
$j=0;
for (; $i < $elements; $i++)
{
$_ = $bits[$i];
filterline();
if ($prefs{"debugging"} >= 2) { dumpline(); }
# keep track of UL indentation level
my $n_ul_open = () = $_ =~ m!<ul!gi;
my $n_ul_close = () = $_ =~ m!</ul!gi;
if ($n_ul_open || $n_ul_close) {
$uldepth += $n_ul_open - $n_ul_close;
$prefs{"debugging"} && debug_msg("uldepth", $uldepth);
}
if (m!<div class=\"title\"> <h4><a.+?>(.+?)</a> <span id=\"comment_score_\d+?\" class=\"score\"> ?\(Score:(.*?)\).*</h4>!)
{
$title = remove_tags($1);
$score = remove_tags($2);
$score_num = $score;
$score_num =~ s!,.*$!!; # keep the numeric value only
if ($prefs{"debugging"}) {
debug_msg("title", $title);
debug_msg("score", $score);
debug_msg("score_num", $score_num);
}
}
# <div class="details"> <><>by <><>NAME <>(12345)<><> ...
elsif (m!<div class="details">!)
{
if (m!sid=([0-9]+)[^\"]+cid=([0-9]+)!) {
$sid = $1;
$cid = $2;
debug_msg("cid,sid", "$cid,$sid") if ($prefs{"debugging"});
if ($cid == $thr_cid) {
$thr_title = "$title $thr_title";
}
}
s!<[^>]+>!!g;
s!([0-9][AP]M).*$!$1!g;
s!writes:.*on !on !g;
s!writes:!!g;
$author = "Posted $_";
debug_msg("author", $author) if ($prefs{"debugging"});
}
elsif (m!<div id=\"comment_body_\d+\">(.+)</div>!i) {
# Remove the "Read the rest of this comment" line and replace
# with generic message
$message = $1;
$message =~ s!<div [^>]*class="sig">.*!!;
$message = remove_tags_except_basic($message);
my $msg_length = length(remove_tags($message));
if ($prefs{"debugging"}) {
debug_msg("message", $message);
debug_msg("message-length", $msg_length);
}
# We have everything (i hope) so clean up message
debug_msg("Got everything...") if ($prefs{"debugging"});
++$commentscount;
cleanup_message(\$message);
if ($j <= $prefs{"max_comments"}) {
my $msg_html = "";
if ($score >= $prefs{"threshold"}) {
# heuristics to decide whether to add a '[thread]' link
my $title_nore = $title;
$title_nore =~ s!^Re:\s*!!;
my $add_thread_link = $prefs{'extra_links'};
if ($title_nore ne "" && $title_nore ne $current_title) {
$add_thread_link = 1;
$current_title = $title_nore;
}
$msg_html .= format_msg($title, $score, $author, $message,
$msg_length, 1, $cid, $sid, $add_thread_link);
} elsif ($prefs{'no_compressed_comments'}==0 && $score == $prefs{'threshold'}-1) {
$msg_html .= format_msg_abbrev_soylent($title, $score, $author, $message,
$msg_length, $cid, $sid);
}
if ($msg_html ne "") {
embed_div_inX($uldepth, \$msg_html);
print("$msg_html\n\n");
++$j;
}
} else {
if ($prefs{"debugging"} && $j > $prefs{"max_comments"}) {
debug_msg("Skipped due to $j > max_comments.\n");
}
elsif ($prefs{"debugging"} && $score_num < $prefs{"threshold"}-1) {
debug_msg("Skipped due to score $score_num < " . $prefs{"threshold"}-1);
}
elsif ($prefs{"debugging"}) {
debug_msg("Skipped.\n");
}
}
$author = $score = $author = $message = $cid = "";
}
} # for $i $elements
if ($j == 0) {
print "<p><small><b>There are no comments rated $prefs{threshold} or higher.</b></small></p>";
} elsif ($j >= $prefs{"max_comments"}) {
print "<p><small><b>Only the first " . $prefs{"max_comments"} . " comments are shown here.</b></small></p><hr>";
}
status_message(1);
if ($thread_mode) {
print_bookmark_link($thr_title);
}
my $extra_footer_html = "";
if (!$prefs{'extra_links'}) {
my $urldata = cgi_param("comments");
$extra_footer_html =
"<a href=\"$self_url?comments=$urldata\&threshold=$prefs{threshold}\&xlinks=1\">".
"[Show extra thread/reply links]</a><br>";
}
print_ckupdate_js();
(!$shownblurb || $commentscount == 0) && print_ckupdate_link("Soylentnews comments page");
show_footer(0, $extra_footer_html);
}
# parse comments
sub parse_comments_slashdot
{
my $thread_mode = 0;
my $is_poll = (cgi_param("comments") =~ m!poll!);
my ($title, $story, $posted, $dept, $header, $story_link);
my ($i, $elements);
my $thereismore = 0;
my $shownblurb = 0;
my $poll_item = '';
$story_link = "";
my $thr_cid = "";
my $thr_title = "";
if (cgi_param("comments") =~ m!^\d+,(\d+)$!) {
$thread_mode = 1;
$thr_cid = $1;
}
# in debug mode, start with a standard header
if ($prefs{debugging}) {
show_header("AvantSlash comments debugging");
$header = 1;
}
my @bits = split(/\n/, $page);
$elements = scalar @bits;
my $commentscount = 0;
for ($i = 0; $i < $elements; $i++)
{