forked from jasontibbitts/majordomo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convertlist.pl
executable file
·439 lines (358 loc) · 10.4 KB
/
convertlist.pl
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/perl
use strict;
use Getopt::Std;
use Fcntl;
use Data::Dumper;
require "./setup/query_util.pl";
require "./setup/setup_func.pl";
use vars qw(%opts $mjcfg);
$|=1;
$SIG{__WARN__} = sub {print STDERR "--== $_[0]"};
my %delete = (
announcements => 1,
approve_passwd => 1,
date_info => 1,
date_intro => 1,
digest_archive => 1,
digest_issue => 1,
digest_maxdays => 1,
digest_maxlines => 1,
digest_name => 1,
digest_rm_footer => 1,
digest_rm_fronter => 1,
digest_volume => 1,
digest_work_dir => 1,
filedir => 1,
mungedomain => 1,
strip => 1,
);
# Pull in configuration
$mjcfg = read_mj_config();
unless ($mjcfg) {
print STDERR "This program should be run after Majordomo has been installed.\n";
exit 1;
}
# Check args for directory and list name to convert.
getopts('cd:gm:o:', \%opts);
if ($opts{g}) {
print STDERR "Conversion of global configuration parameters not yet supported.";
exit 1;
}
convert_some_lists();
exit 0;
#---- Subroutines
sub convert_some_lists {
# No parameters; %opts and $mjcfg are globals
my ($list);
$opts{d} ||= $mjcfg->{domains}[0];
$opts{o} ||= $mjcfg->{domain}{$opts{d}}{old_lists_dir};
unless ($opts{o} && -d $opts{o}) {
print STDERR "Can't find directory containing old lists.\n";
exit 1;
}
unless (@ARGV) {
print STDERR "No lists specified for conversion.\n";
exit 1;
}
for $list (@ARGV) {
unless (-f "$opts{o}/$list") {
print STDERR "Can't find list $list to convert.\n";
exit 1;
}
unless (-r "$opts{o}/$list") {
print STDERR "Found old list $list but can't read it.\n";
exit 1;
}
}
for $list (@ARGV) {
convert_list($list);
}
}
sub convert_list {
my $list = shift;
my(%config, @args, @rest, $aux, $digest, $editor, @editor, $err, $file,
$filecount, $i, $id, $j, $msg, $owner, $pid, $pw, $tmp, $val, $var);
$filecount = 1;
print "Converting $list\n";
# Check to see if list exists already. Prompt to exit or create it.
# (Don't want to overwrite existing lists.) Note that we do the bad
# thing and just poke inside the list directories directly.
if (-d "$mjcfg->{lists_dir}/$opts{d}/$list") {
$msg = <<EOM;
The list $list already exists. This process can continue, but note that
existing configuration changes may be overwritten.
Continue?
EOM
return unless get_bool($msg);
}
$msg = <<EOM;
What is the email address of the owner of $list?
In Majordomo1 this was written into the aliases but under Majordomo2 it is
simply a configuration variable.
EOM
$owner = get_str($msg);
# Locate and load old config file. Convert variables where necessary.
load_old("$opts{o}/$list.config", \%config);
# Iterate over the keys, performing conversion or deletion if necessary.
for $i (sort keys %config) {
if ($delete{$i}) {
delete $config{$i};
}
}
munge(\%config);
# Figure out what default_flags should be based on reply_to and
# subject_prefix.
push @{$config{default_flags}}, 'selfcopy';
if (length $config{reply_to} > 0) {
push @{$config{default_flags}}, 'replyto';
}
else {
$msg = <<EOM;
You do not have the reply_to variable set. Majordomo2 allows users to
choose to receive messages with a Reply-To: header. Would you like the
default settings to enable you to turn on reply_to and then allow users to
choose to begin receiving it if they so desire?
EOM
unless (get_bool($msg, 1)) {
push @{$config{default_flags}}, 'replyto';
}
}
if (length $config{subject_prefix} > 0) {
push @{$config{default_flags}}, 'prefix';
}
else {
$msg = <<EOM;
You do not have the subject_prefix variable set. Majordomo2 allows users
to choose to receive messages with a subject prefix. Would you like the
default settings to enable you to set a subject prefix and then allow users
to choose to begin receiving it if they so desire?
EOM
unless (get_bool($msg, 1)) {
push @{$config{default_flags}}, 'prefix';
}
}
if (length $config{mungedomain}) {
$msg = <<EOM;
The list $list has the mungedomain variable set. Majordomo2 supports a
mechanism for performing the equivalent functionality and more through the
addr_xforms variable, but note that this variable is set globally, not per
list and so it will not be set here.
Press enter to continue.
EOM
get_str($msg);
}
# Open the command file
$file = "$mjcfg->{wtmpdir}/convertlist.$$";
sysopen CMD, $file, O_RDWR|O_CREAT|O_EXCL, 0600
or die "Can't open $file, $!";
# Initial arguments
@args = ("$mjcfg->{'install_dir'}/bin/mj_shell", "-d", "$opts{d}", "-F",
"-");
# Pull out the password or prompt for it if necessary
$pw = $mjcfg->{'site_password'};
unless ($pw) {
$msg = "What is the site password?\n";
$pw = get_str($msg);
}
print CMD "default password $pw\n";
if ($config{master_password}) {
print CMD "createlist-pass $list $config{master_password} $owner\n";
delete $config{master_password};
}
else {
print CMD "createlist $list $owner\n";
}
# Pick apart the restrict_post variable and build auxlists.
if (length $config{restrict_post}) {
@rest = split(/[:,\s]/, $config{restrict_post});
$config{restrict_post} = [];
for $i (@rest) {
# Name of this list: just put it in
if ($i eq $list) {
push @{$config{restrict_post}}, $i;
next;
}
# Something that looks like a digest list; ignore it for now
if ($i eq "$list-digest") {
next;
}
# Something that doesn't look like a separate list. It might be a
# file that "lives" under another list, but it's easier to ignore
# that fact for now.
if ($i =~ /\.(.*)$/) {
$msg = <<EOM;
The old restrict_post variable includes: $i.
This does not look like the name of another list, so we can set up an
auxiliary list and add the contents of this file to it.
Do you want to do this?
EOM
next unless get_bool($msg, 1);
$msg = <<EOM;
What do you want to name this auxiliary list?
EOM
$aux = get_str($msg, $1);
print CMD "subscribe $list:$aux <\@$filecount\n";
$filecount++;
push @args, ('-f', "$opts{o}/$i");
push @{$config{restrict_post}}, $aux;
next;
}
# Anything else references another list. Just warn about it.
$msg = <<EOM;
The old restrict_post variable includes: $i.
This looks like a separate list, so it will not be automatically placed in
the new restrict_post variable. If you had created a separate list for
this solely to enable the list owner to manage the restriction list
remotely, then you can just as well use an auxiliary list for that
purpose.
You will need to do this manually. Press enter to continue.
EOM
get_str($msg);
next;
}
}
# check for and convert digest subscribers
if ((-f "$opts{o}/$list-digest") && (-r "$opts{o}/$list-digest")) {
$msg = <<EOM;
You appear to have a digest for this list as $list-digest. If any list
settings are different from the main list, they will be lost, however, we
can import the subscriber list. A digest called "daily" will be created
and the $list-digest subscribers will be set up in 'digest-daily' mode.
Would you list to import the subscribers from $list-digest?
EOM
if (get_bool($msg, 1)) {
$digest = 1;
$tmp = <<EOD;
daily
times=5
minsize=20
minmsg=5
maxsize=40
maxmsg=10
maxage=3d
minage=1d
separate=0
newmsg=0
desc=The daily digest for $list
EOD
push @{$config{digests}}, $tmp;
}
}
# Now dump out the configuration.
$id = 'AA';
for $i (sort keys %config) {
if (ref($config{$i}) eq 'ARRAY') {
print CMD "configset $list $i << END$id\n";
for $j (@{$config{$i}}) {
print CMD "$j\n";
}
print CMD "END$id\n\n";
$id++;
}
else {
print CMD "configset $list $i = $config{$i}\n";
}
}
if (-r "$opts{o}/$list" and -s "$opts{o}/$list") {
print CMD "\nsubscribe-noinform-nowelcome $list <\@$filecount\n\n";
push @args, "-f", "$opts{o}/$list";
$filecount++;
}
if ($digest and (-r "$opts{o}/$list-digest" and
-s "$opts{o}/$list-digest"))
{
print CMD "subscribe-set-noinform-nowelcome $list digest-daily <\@$filecount\n\n";
push @args, "-f", "$opts{o}/$list-digest";
$filecount++;
}
print CMD "# mj_shell will be called with the following arguments:\n";
print CMD "# @args\n\n";
close CMD;
# Offer to edit it
$editor = $ENV{EDITOR} || $ENV{VISUAL} || '/bin/vi';
@editor = split(' ',$editor);
if (get_bool("Do you want to edit the command file before executing it?\n", 1))
{
$err = system(@editor, $file);
}
return if $err && !get_bool("The editor indicated an error; continue?\n", 1);
get_str("Ready to execute script; press enter\n");
# Pass it to mj_shell.
$pid = open(SHELL, "|-");
if ($pid) {
# in parent
open CMD, "<$file";
while (defined($_ = <CMD>)) {
print SHELL $_;
}
close CMD;
close SHELL;
waitpid $pid, 0;
}
else {
# in child
exec (@args) or die "Error executing $args[0], $!";
}
unlink $file;
# Done.
}
sub load_old {
my($file, $hash) = @_;
my ($key, $op, $val);
print "Loading $file\n";
unless (-r $file) {
print STDERR "$file does not exist or is unreadable; using default configuration.\n";
return;
}
open CF, $file or die "Can't open $file: $!";
while (defined ($_ = <CF>)) {
next if /^\s*($|\#)/;
chomp;
s/#.*//;
s/\s+$//;
($key, $op, $val) = split(" ", $_, 3);
if (!defined $val) { $val = "" }
$key = lc($key);
if ($op eq "\<\<") {
$hash->{$key} = [];
while (defined($_ = <CF>)) {
chomp;
next unless $_;
# s/^-//;
last if $_ eq $val;
push @{$hash->{$key}}, $_;
}
}
else {
$hash->{$key} = $val;
}
}
1;
}
sub munge {
my $cf = shift;
if (exists $cf->{debug}) {
$a = $cf->{debug};
if (lc($a) eq 'no') {
$cf->{debug} = 0;
}
else {
$cf->{debug} = 500;
}
}
if (exists $cf->{admin_passwd}) {
$cf->{master_password} = $cf->{admin_passwd};
delete $cf->{admin_passwd};
}
if (exists $cf->{moderator}) {
$cf->{moderators} = $cf->{moderator};
delete $cf->{moderator};
}
if (exists $cf->{mungedomain}) {
$a = $cf->{mungedomain};
if (lc($a) eq 'yes') {
push @{$cf->{addr_xforms}}, 'mungedomain';
}
delete $cf->{mungedomain};
}
}