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
7 use open ":std", ":encoding(UTF-8)";
12 use File::Temp qw(tempfile);
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
40 my $threads_per_page = 100;
44 return $a unless defined $b;
45 return $a gt $b ? $a : $b;
50 return $a unless defined $b;
51 return $a lt $b ? $a : $b;
56 return $i == 1 && "index.html" || "$i.html";
60 say $pfh '</ul></div>';
62 push @pages, "$from_day - $to_day";
66 endfile if defined $pfh;
70 ($pfh, $path) = tempfile "/tmp/gotmarc.index.XXXXXXXXXX";
71 binmode($pfh, ':utf8');
73 say $pfh "<div class='thread'><ul>";
78 my ($first, $last) = (pagename(1), pagename($page));
79 my ($next, $prev) = (pagename($n+1), pagename($n-1));
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;
93 <form method="get" action="/search">
94 <label>Search: <input type="search" name="q" /></label>
95 <button type="submit">search</button>
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>;
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;
127 nav $pfh, $i if $page > 1;
129 copyfrom($path, $pfh);
130 nav $pfh, $i if $page > 1;
141 say $tfh "</ul></li>" x $last_level;
142 say $tfh "</ul>\n</div>\n";
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, undef, undef, ["Thread: $subj"];
156 print $tfh "<div class='thread'><ul class='mails'>\n";
160 my ($fh, $mid, $date, $from, $subj) = @_;
162 # synthetic mail hash
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: $!";
187 if ($mail->{level} == 0) {
188 nextthread $mail->{mid}, $mail->{subj};
191 if ($threads > $threads_per_page) {
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;
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;
226 endthread if $threads_seen;