Blob


1 #!/usr/bin/env perl
2 #
3 # mkindex was written by Omar Polo <op@openbsd.org> and is placed in
4 # the public domain. The author hereby disclaims copyright to this
5 # source code.
7 use open ":std", ":encoding(UTF-8)";
8 use utf8;
9 use strict;
10 use warnings;
11 use v5.32;
12 use File::Temp qw(tempfile);
14 use OpenBSD::Pledge;
15 use OpenBSD::Unveil;
17 use lib ".";
18 use GotMArc qw(parse san urlencode initpage endpage index_header
19 thread_header threntry);
21 my $outdir = $ENV{'OUTDIR'};
22 die 'Set $OUTDIR' unless defined $outdir;
24 my $tfh; # thread file handle
25 my $pfh; # page file handle
26 my $page = 0;
27 my @pages;
28 my @files;
29 my $from_day;
30 my $to_day;
31 my $threads_seen = 0;
33 my $last_level = 0;
34 my $last_tid;
35 my $last_date;
36 my $last_from;
37 my $last_subj;
39 my $threads = 0;
40 my $threads_per_page = 100;
42 sub maxs {
43 my ($a, $b) = @_;
44 return $a unless defined $b;
45 return $a gt $b ? $a : $b;
46 }
48 sub mins {
49 my ($a, $b) = @_;
50 return $a unless defined $b;
51 return $a lt $b ? $a : $b;
52 }
54 sub pagename {
55 my $i = shift;
56 return $i == 1 && "index.html" || "$i.html";
57 }
59 sub endfile {
60 say $pfh '</ul></div>';
61 close($pfh);
62 push @pages, "$from_day - $to_day";
63 }
65 sub nextfile {
66 endfile if defined $pfh;
67 $page += 1;
69 my $path;
70 ($pfh, $path) = tempfile "/tmp/gotmarc.index.XXXXXXXXXX";
71 binmode($pfh, ':utf8');
72 push @files, $path;
73 say $pfh "<div class='thread'><ul>";
74 }
76 sub nav {
77 my ($pfh, $n) = @_;
78 my ($first, $last) = (pagename(1), pagename($page));
79 my ($next, $prev) = (pagename($n+1), pagename($n-1));
81 say $pfh "<nav>";
82 say $pfh "<a href='$first'>First</a>" if $n > 2;
83 say $pfh "<a href='$prev'>Prev</a>" if $n > 1;
84 say $pfh "<a href='$next'>Next</a>" if $n < $page;
85 say $pfh "<a href='$last'>Last</a>" if $n < $page - 1;
86 say $pfh "</nav>";
87 }
89 sub search {
90 my $pfh = shift;
92 say $pfh <<'EOF' ;
93 <form method="get" action="/search">
94 <label>Search: <input type="search" name="q" /></label>
95 <button type="submit">search</button>
96 </form>
97 EOF
98 }
100 sub copyfrom {
101 my ($path, $fh) = @_;
103 # there are probably faster ways to do this like File::Copy,
104 # but it bypasses the bufio cache...
105 open(my $pfh, '<', $path) or die "can't open $path: $!";
106 print $fh $_ while <$pfh>;
109 sub renderpages {
110 close($pfh);
112 for (my $i = 1; $i <= $page; $i++) {
113 my $name = pagename($i);
114 my $path = shift @files;
115 my $dest = "$outdir/$name";
117 open(my $pfh, '>', $dest)
118 or die "can't open $dest for writing: $!";
120 my $title = "Game of Trees Mail Archive | page $i";
121 my $subtitle = $pages[$i-1];
123 initpage($pfh, $title);
124 index_header $pfh, $i, $subtitle;
125 say $pfh "<main>";
127 nav $pfh, $i if $page > 1;
128 search $pfh;
129 copyfrom($path, $pfh);
130 nav $pfh, $i if $page > 1;
132 say $pfh "</main>";
133 endpage($pfh);
135 close($pfh);
136 unlink $path;
140 sub endthread {
141 say $tfh "</ul></li>" x $last_level;
142 say $tfh "</ul>\n</div>\n";
143 endpage($tfh);
144 close($tfh);
146 $last_level = 0;
149 sub nextthread {
150 endthread if defined $tfh;
151 my ($mid, $subj) = @_;
152 my $dest = "$outdir/thread/$mid.html";
153 open($tfh, '>', $dest) or die "can't open $dest: $!";
154 initpage($tfh, $subj);
155 thread_header $tfh, ["Thread: $subj"];
156 print $tfh "<div class='thread'><ul class='mails'>\n";
159 sub index_entry {
160 my ($fh, $mid, $date, $from, $subj) = @_;
162 # synthetic mail hash
163 my $mail = {
164 mid => $mid,
165 level => 0,
166 date => $date,
167 from => $from,
168 subj => $subj,
169 };
171 threntry $fh, "thread", 0, 0, $mail;
174 unveil($outdir, "rwc") or die "unveil $outdir: $!";
176 # can't use tmppath because File::Temp checks wether /tmp exists.
177 unveil("/tmp", "rwc") or die "unveil /tmp: $!";
179 # fattr for File::Temp
180 pledge("stdio rpath wpath cpath fattr") or die "pledge: $!";
182 nextfile;
184 while (<>) {
185 my $mail = parse $_;
187 if ($mail->{level} == 0) {
188 nextthread $mail->{mid}, $mail->{subj};
190 $threads++;
191 if ($threads > $threads_per_page) {
192 nextfile;
193 $threads = 0;
194 $to_day = undef;
195 $from_day = undef;
198 my $day = $mail->{date} =~ s/ .*//r;
199 $to_day = mins $day, $to_day;
200 $from_day = maxs $day, $from_day;
203 $last_level = threntry $tfh, "mail", 0, $last_level, $mail;
204 $threads_seen = 1;
206 index_entry $pfh, $last_tid, $last_date, $last_from, $last_subj
207 if defined $last_tid && $mail->{level} == 0;
209 # `gt' on dates works because the format used allow for
210 # lexicographic comparisons.
211 if ($mail->{level} == 0 || $mail->{date} gt $last_date) {
212 $last_date = $mail->{date};
213 $last_from = $mail->{from};
216 if ($mail->{level} == 0) {
217 $last_tid = $mail->{mid};
218 $last_subj = $mail->{subj};
222 index_entry $pfh, $last_tid, $last_date, $last_from, $last_subj
223 if defined $last_tid;
225 endfile;
226 endthread if $threads_seen;
227 renderpages;