summaryrefslogtreecommitdiff
blob: e2697687ab322e40ecaccf841985e4b81823cef4 (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
#!/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
# - 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;

  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>.*$//;

  return ($from, $subject);
};


# 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;


# 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";
  } else {
    print "  Data not yet available, fetching it\n";
    my ($from, $subject) = getdata($msglist, $msghash);
    $messagefrom{$msg}=$from;
    $messagesubject{$msg}=$subject;
    print "    Sender  is \"$messagefrom{$msg}\"\n";
    print "    Subject is \"$messagesubject{$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 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";

close $mld;