1 1bcb9899 2022-08-29 op # GotMArc was written by Omar Polo <op@openbsd.org> and is placed in
2 1bcb9899 2022-08-29 op # the public domain. The author hereby disclaims copyright to this
5 04eab9af 2022-08-25 op package GotMArc;
10 8a090a17 2022-08-30 op use File::Basename;
12 04eab9af 2022-08-25 op our @ISA = qw(Exporter);
13 f0fb0f08 2022-08-30 op our @EXPORT_OK = qw(san urlencode parse initpage endpage index_header thread_header);
16 04eab9af 2022-08-25 op my $str = shift;
17 04eab9af 2022-08-25 op $str =~ s/&/\&/g;
18 04eab9af 2022-08-25 op $str =~ s/</\</g;
19 04eab9af 2022-08-25 op $str =~ s/>/\>/g;
23 f0fb0f08 2022-08-30 op sub urlencode {
24 f0fb0f08 2022-08-30 op my $str = shift;
25 f0fb0f08 2022-08-30 op unless (defined($str)) {
26 f0fb0f08 2022-08-30 op my ($pkg, $file, $line) = caller 1;
27 f0fb0f08 2022-08-30 op die "bad $pkg / $file:$line";
29 f0fb0f08 2022-08-30 op $str =~ s/([^-_~.A-Za-z0-9])/sprintf("%%%2X", ord($1))/ge;
34 de557185 2022-08-26 op my $str = shift;
35 de557185 2022-08-26 op $str =~ s/\s+/ /g;
36 de557185 2022-08-26 op $str =~ s/\s+$//;
37 de557185 2022-08-26 op return san($str);
41 8a090a17 2022-08-30 op my ($indent, $fname, $date, $from, $subj) = m{
42 de557185 2022-08-26 op ^([^-]*)- # the indent level
43 de557185 2022-08-26 op ([^ ]+)\s # filename
44 de557185 2022-08-26 op (\d{4}-\d\d-\d\d[ ]\d\d:\d\d) # date
45 de557185 2022-08-26 op <([^>]+)> # from
46 de557185 2022-08-26 op (.*) # subject
47 de557185 2022-08-26 op }x or die "can't parse: $_";
49 de557185 2022-08-26 op my $level = length($indent);
50 de557185 2022-08-26 op $level = 10 if $indent =~ m/\.\.\d+\.\./;
52 de557185 2022-08-26 op $from = ssan($from);
53 de557185 2022-08-26 op $subj = ssan($subj);
55 8a090a17 2022-08-30 op my ($time, $id) = split /\./, basename($fname);
56 8a090a17 2022-08-30 op my $mid = "$time.$id";
58 de557185 2022-08-26 op return ($level, $fname, $mid, $date, $from, $subj);
62 4ad24540 2022-08-27 op my $path = shift;
63 04eab9af 2022-08-25 op local $/ = undef;
64 4ad24540 2022-08-27 op open my $fh, "<", $path or die "can't open $path: $!";
68 4ad24540 2022-08-27 op my $small_logo = readall "logo-small.html";
69 4ad24540 2022-08-27 op my $hdr = readall "head.html";
70 4ad24540 2022-08-27 op my $foot = readall "foot.html";
71 9ec6c848 2022-08-27 op my $idxhdr = readall "index-header.html";
73 04eab9af 2022-08-25 op sub initpage {
74 04eab9af 2022-08-25 op my ($fh, $title) = @_;
75 04eab9af 2022-08-25 op say $fh $hdr =~ s/TITLE/$title/r;
79 04eab9af 2022-08-25 op my $fh = shift;
80 04eab9af 2022-08-25 op say $fh $foot;
83 9ec6c848 2022-08-27 op sub index_header {
84 9ec6c848 2022-08-27 op my ($fh, $page, $subtitle) = @_;
85 9ec6c848 2022-08-27 op my $html = $idxhdr =~ s/PAGE/$page/r;
86 9ec6c848 2022-08-27 op $html =~ s/SUBTITLE/$subtitle/;
87 9ec6c848 2022-08-27 op print $fh $html;
90 9d8482ab 2022-08-27 op sub thread_header {
91 9d8482ab 2022-08-27 op my ($fh, $tid, $mid, $e) = @_;
92 9d8482ab 2022-08-27 op my @entries = @$e;
94 f0fb0f08 2022-08-30 op my $enctid = urlencode $tid if defined $tid;
95 f0fb0f08 2022-08-30 op my $encmid = urlencode $mid if defined $mid;
97 9d8482ab 2022-08-27 op print $fh "<header class='mail-header'>\n";
99 9d8482ab 2022-08-27 op print $fh "<p>";
100 9d8482ab 2022-08-27 op print $fh $small_logo;
101 9d8482ab 2022-08-27 op print $fh "<a href='/'>Index</a>";
102 f0fb0f08 2022-08-30 op print $fh " | <a href='/thread/$enctid.html#$encmid'>Thread</a>"
103 f0fb0f08 2022-08-30 op if defined $enctid;
104 9d8482ab 2022-08-27 op print $fh "</p>\n";
106 9d8482ab 2022-08-27 op say $fh "<dl>";
107 9d8482ab 2022-08-27 op foreach my $entry (@entries) {
108 de498c0c 2022-08-27 op my ($k, $v) = split /: /, $entry, 2;
110 9d8482ab 2022-08-27 op say $fh "<dt>$k:</dt><dd>$v</dd>";
112 9d8482ab 2022-08-27 op say $fh "</dl>";
114 f0fb0f08 2022-08-30 op say $fh "<p>Download raw <a href='/text/$encmid.txt'>body</a>.</p>"
115 f0fb0f08 2022-08-30 op if defined $encmid;
117 9d8482ab 2022-08-27 op say $fh "</header>\n";