chiark / gitweb /
a3754375ba105b019309117db186015661ad2f53
[d.git] / evade-mail-pregen
1 #!/usr/bin/perl -w
2 use strict;
3 use Carp;
4 our $us = $0; $us =~ s#.*/##;
5 our $usage = <<'END';
6 usage:
7  $us [<opts>] update <number>|<localpart>@[domain] [<reason>] [<comment>]
8  $us [<opts>] assign <number>|<localpart>@[domain] <comment>
9  $us [<opts>] list
10  $us [<opts>] some <count>                     } will generate aliases
11  $us [<opts>] range <min>-<max>|<min>+<count>  }  as necessary
12 opts:
13  -G<generator>           generator/lister program
14  -N                      do not generate more aliases
15  -l.., -d.., -F.., -m..  passed to generator
16 notes:
17  Always use $us to edit the comment of a pregen alias;
18   otherwise we may lose track of the next number alias to pregenerate
19   and reuse numbers.
20  <comment> must always start with #, and you will need to quote the
21   whole comment to protect it from your shell
22 END
23
24 sub fail { die "$us: @_\n"; }
25 sub badusage { fail "bad usage: @_"; }
26 sub failstdout () { fail "stdout: $!"; }
27
28 our $generator;
29 our $no_generate=0;
30 our @genopts;
31
32 our %by_localpart;
33 our @by_number;
34 our $min_number;
35
36 our $comment_pattern = '### PREGEN <number>';
37 our $comment_re;
38
39 sub comment_make ($) {
40     my ($num) = @_;
41     my $r = $comment_pattern;
42     ($r =~ s/\<number\>/$num/) == 1 or confess "$r ?";
43     return $r;
44 }
45
46 sub run_generator {
47     my @genargs = @_;
48     open P, "-!", $generator, @genargs or fail "fork $generator: $!";
49     while (<P>) {
50         my ($alias,$comment);
51         if (m/^\# user/) {
52             next;
53         } elsif (m/^\# reject (\S+) (\#.*)$/) {
54             ($alias,$comment) = ($1,$2);
55         } elsif (m/^\#/) {
56             next; # future extension
57         } elsif (m/^([^#: ])\: [^#]* (\#.*)$/) {
58             ($alias,$comment) = ($1,$2);
59         } else {
60             die "generator output $_ ?";
61         }
62         my $localpart = $alias;
63         $localpart =~ s/\@.*//;
64         my $row = { Alias => $alias, LocalPart => $localpart };
65         if ($comment =~ m/^$comment_re$/o) {
66             $row->{Number}= $1;
67         }
68         $by_localpart{$row->{LocalPart}} = $row;
69         if (defined $row->{Number}) {
70             $by_number[$row->{Number}] = $row;
71             $min_number = $row->{Number} if
72                 (!defined $min_number) || $min_number > $row->{Number};
73     }
74     $?=0; $!=0; close P or fail "$generator $genargs[0] failed ($! $?)\n";
75 }
76
77 sub fetch_list () {
78     run_generator qw(list);
79 }
80
81 sub perhaps_generate ($) {
82     my ($num) = @_;
83     return undef if $no_generate;
84     my $alias = $by_number[$num];
85     return $alias if $alias;
86     return undef unless $num > $#by_number;
87     run_generator qw(create), (comment_make $num);
88     $alias = $by_number[$num];
89     confess "$num ?" unless $alias;
90     return $alias;
91 }
92
93 sub parse_target {
94     @ARGV or badusage "missing specification for alias to change";
95     my $spec = shift @ARGV;
96     my $target;
97     if ($spec =~ m/^(\d{1,9})$/) {
98         $target = $by_number[$spec];
99         fail "no alias number $target" unless $target;
100     } elsif ($spec =~ m/^(.*)\@[^\@]*/) {
101         $target = $by_localpart{$1};
102         fail "no alias with local part \`$1'" unless $target;
103         fail "incorrect or unchecked domain: \`$target->{Alias}' != \`$spec'"
104             unless $spec eq $target->{Alias} ||
105                    $spec eq $target->{LocalPart}.'@';
106     } else {
107         badusage "bad specification for alias to change";
108     }
109 }
110
111 sub report ($) {
112     my ($alias) = @_;
113     confess unless $alias;
114     print $alias->{Number}, ' ', $alias->{LocalPart}, "\n"
115         or failstdout;
116 }
117
118 sub action_update {
119     my $target = parse_target;
120     @ARGV or badusage "missing update info\n";
121     if (defined $target->{Number} && $target->{Number} == $#by_number) {
122         my $wanted = $#by_number + 1;
123         perhaps_generate $wanted
124             or print STDERR <<END or fail "stderr: $!"
125 $us: Losing track of next number ($wanted), due to use of -N;
126 $us: will start next alias at #0, unless you say (e.g.) "range $wanted".
127 END
128     }
129     exec $generator, qw(update), $target->{Alias}, @ARGV;
130     fail "exec $generator: $!";
131 }
132
133 sub action_assign {
134     (@ARGV==2 &&
135      $ARGV[1] =~ m/^#/) or
136      badusage "invalid arguments to assign - forgot to quote it properly?";
137     action_update;
138 }
139
140 sub action_list {
141     @ARGV==0 or
142         badusage "invalid arguments to list";
143     my $num = $min_number;
144     while ($num <= $#by_number) {
145         my $alias = $by_number[$num];
146         report $alias if $alias;
147     }
148 }
149
150 sub action_some {
151     (@ARGV==1 &&
152      $ARGV[0] =~ m/^(\d+)$/s) or
153      badusage "invalid arguments to some";
154     my ($count) = $1;
155     my $num = $min_number;
156     while ($count > 0) {
157         my $alias = generate $num;
158         if ($alias) {
159             report $alias;
160             $count--;
161         } else {
162             last if $num > $#by_number; # -N
163             $num++;
164         }
165     }
166 }
167
168 sub action_range {
169     (@ARGV==1 &&
170      $ARGV[0] =~ m/^(\d+)(?:([-+])(\d+))?$/s) or
171      badusage "invalid arguments to range";
172     my ($num,$op,$rarg) = ($1,$2,$3);
173     my $limit =
174         $op eq '+' ? $num+$rarg-1 :
175         $op eq '-' ? $rarg :
176         confess "$op ??";
177     for (; $num<=$limit; $num++) {
178         my $alias = generate $num;
179         report $alias if $alias;
180     }
181 }    
182
183 for (;;) {
184     last unless @ARGV;
185     last unless $ARGV[0] =~ m/^-/;
186     my $arg = shift @ARGV;
187     last if $arg =~ m/^--?$/;
188     while ($arg !~ m/^-$/) {
189         if ($arg =~ s/^-[ldFm]/-/) {
190             push @genopts, $arg;
191         } elsif ($arg =~ s/^-G//) {
192             $generator = $'; #';
193         } elsif ($arg =~ s/^-N//) {
194             $no_generate = 1;
195         } else {
196             badusage "unknown option \`$arg'";
197         }
198     }
199 }
200
201 if (!defined $generator) {
202     $generator = $us;
203     $generator =~ s/-pregen$//
204         or fail "unable to calculate default generator".
205             " (want to strip \`-pregen' from our program name, \`$us')";
206 }
207
208 $comment_pattern =~ m/^#/s
209     or badusage "comment pattern must start with \`#'";
210 $comment_re = $comment_pattern;
211 $comment_re =~ s/\W/\\$&/g;
212 ($comment_re =~ s'\<number\>'([1-9]\d{0,8})'g) == 1 #'
213     or badusage "comment pattern must contain \`<number>' exactly once";
214
215 @ARGV or badusage "missing action\n";
216 my $action = shift @ARGV;
217
218 fetch_list();
219
220 $action =~ y/-/_/;
221 my $actionsub = { no strict qw(refs); \&{"action_$action"}; }
222 defined $actionsub or badusage "unknown action $action";
223 $actionsub->();
224
225 close STDOUT or failstdout;