summaryrefslogtreecommitdiff
blob: 78cafe867c7c5de70ca9983a96225555a4091af7 (plain)
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
#!/usr/bin/perl

use List::MoreUtils qw(uniq sort_by);
use LaTeX::Encode qw(latex_encode);
use Data::Dumper;

# four files: 
# - mlr: input file, unsorted list of messages, format "hash:listname"
#        generated by LaTeX, can contain duplicates
# - mlf: cache of message senders; format: one message per two lines
#        * first line "hash:listname"
#        * second line message sender
#        maintained by this script, can be deleted any time but needs a.g.o
#        access for recreation
# - mls: cache of message subjects; format: one message per two lines
#        * first line "hash:listname"
#        * second line message subject
#        maintained by this script, can be deleted any time but needs a.g.o
#        access for recreation
# - mlt: cache of message dates; format: one message per two lines
#        * first line "hash:listname"
#        * second line message date
#        maintained by this script, can be deleted any time but needs a.g.o
#        access for recreation
# - mld: output file, LaTeX fragment that can be sourced


sub getdata {
  my $list=shift;
  my $hash=shift;
  
  open (my $web, '-|:encoding(UTF-8)', "wget -O - https://archives.gentoo.org/$list/message/$hash");

  my $line, $from, $subject, $date;

  until ($line=~/From:/) {
    $line=<$web>; chomp $line;
  }

  $from=<$web>;
  chomp $from;
  $from=~s/^.*<td>//;
  $from=~s/ &lt;.*$//;
  $from=~s/&quot;//g;
  
  until ($line=~/>Subject:</) {
    $line=<$web>; chomp $line;
  }

  $subject=<$web>;
  chomp $subject;
  $subject=~s/^.*<td><strong>//;
  $subject=~s/<\/strong><\/td>.*$//;
  $subject=~s/\[$list\] ?//;

  until ($line=~/>Date:</) {
    $line=<$web>; chomp $line;
  }

  $date=<$web>;
  chomp $date;
  $date=~s/^.*<td>//;
  $date=~s/<\/td>.*$//;

  return ($from, $subject, $date);
};


# Main code start

# Read the list of messages referenced in the LaTeX file.
open my $mlr, '<', "decisions.mlr";
my @mlrlist = <$mlr>;
close $mlr;
chomp @mlrlist;

my @messages=uniq sort_by { $_ } @mlrlist;

# Read the cache of message senders; this file can be deleted, but recreating or
# updating it requires internet access.
open my $mlf, '<', "decisions.mlf";
my @mlflist = <$mlf>;
close $mlf;
chomp @mlflist;

my %messagefrom=@mlflist;

# Read the cache of message subjects; this file can be deleted, but recreating or
# updating it requires internet access.
open my $mls, '<', "decisions.mls";
my @mlslist = <$mls>;
close $mls;
chomp @mlslist;

my %messagesubject=@mlslist;

# Read the cache of message times; this file can be deleted, but recreating or
# updating it requires internet access.
open my $mlt, '<', "decisions.mlt";
my @mltlist = <$mlt>;
close $mlt;
chomp @mltlist;

my %messagedate=@mltlist;


# Loop through the referenced messages, check if we already have the data,
# and if not fetch and add it.
foreach(@messages) {
  my $msg=$_;
  my ($msghash, $msglist) = split /:/,$msg,2;
  
  print "List $msglist, hash $msghash\n";
  if ($messagesubject{$msg}) {
    print "  Sender  is \"$messagefrom{$msg}\"\n";
    print "  Subject is \"$messagesubject{$msg}\"\n";
    print "  Date    is \"$messagedate{$msg}\"\n";
  } else {
    print "  Data not yet available, fetching it\n";
    my ($from, $subject, $date) = getdata($msglist, $msghash);
    $messagefrom{$msg}=$from;
    $messagesubject{$msg}=$subject;
    $messagedate{$msg}=$date;
    print "    Sender  is \"$messagefrom{$msg}\"\n";
    print "    Subject is \"$messagesubject{$msg}\"\n";
    print "    Date    is \"$messagedate{$msg}\"\n";
  };
};

# Write out the from cache again.
my @mlflistnew= map { $_ => $messagefrom{$_} } sort keys %messagefrom;
open my $mlf, '>', "decisions.mlf";
print $mlf "$_\n" for @mlflistnew;
close $mlf;

# Write out the subject cache again.
my @mlslistnew= map { $_ => $messagesubject{$_} } sort keys %messagesubject;
open my $mls, '>', "decisions.mls";
print $mls "$_\n" for @mlslistnew;
close $mls;

# Write out the date cache again.
my @mltlistnew= map { $_ => $messagedate{$_} } sort keys %messagedate;
open my $mlt, '>', "decisions.mlt";
print $mlt "$_\n" for @mltlistnew;
close $mlt;

# Write out the TeX input file
open my $mld, '>', "decisions.mld";

print $mld '\renewcommand{\gentoomailfrom}[1]{%'."\n";
foreach(@messages) {
  my $msg=$_;
  my ($msghash, $msglist) = split /:/,$msg,2;

  my $from=$messagefrom{$msg};
  print $mld '\ifthenelse{\equal{#1}{'.$msghash.'}}{{'.$from.'}}{}%'."\n";
};
print $mld '}'."\n";

print $mld '\renewcommand{\gentoomailsubject}[1]{%'."\n";
foreach(@messages) {
  my $msg=$_;
  my ($msghash, $msglist) = split /:/,$msg,2;

  my $subject=latex_encode($messagesubject{$msg});
  print $mld '\ifthenelse{\equal{#1}{'.$msghash.'}}{{'.$subject.'}}{}%'."\n";
};
print $mld '}'."\n";

print $mld '\renewcommand{\gentoomaildate}[1]{%'."\n";
foreach(@messages) {
  my $msg=$_;
  my ($msghash, $msglist) = split /:/,$msg,2;

  my $date=latex_encode($messagedate{$msg});
  print $mld '\ifthenelse{\equal{#1}{'.$msghash.'}}{{'.$date.'}}{}%'."\n";
};
print $mld '}'."\n";

close $mld;