List.pm 192 KB
Newer Older
root's avatar
root committed
1
2
3
4
5
# This module is part of ML and does all list processing functions

package List;

use strict;
6
7
8
9
require Exporter;
require 'tools.pl';
my @ISA = qw(Exporter);
my @EXPORT = qw(%list_of_lists);
root's avatar
root committed
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

=head1 CONSTRUCTOR

=item new( [PHRASE] )

 List->new();

Creates a new object which will be used for a list and
eventually loads the list if a name is given. Returns
a List object.

=back

=head1 METHODS

=over 4

=item load ( LIST )

Loads the indicated list into the object.

=item save ( LIST )

Saves the indicated list object to the disk files.

=item savestats ()

Saves updates the statistics file on disk.

=item update_stats( BYTES )

Updates the stats, argument is number of bytes, returns the next
sequence number. Does nothing if no stats.

=item send_sub_to_owner ( WHO, COMMENT )
Send a message to the list owners telling that someone
wanted to subscribe to the list.

=item send_to_editor ( MSG )

Send a Mail::Internet type object to the editor (for approval).

=item send_msg ( MSG )

Sends the Mail::Internet message to the list.

=item send_file ( FILE, USER, GECOS )

Sends the file to the USER. FILE may only be welcome for now.

=item delete_user ( ARRAY )

Delete the indicated users from the list.
 
=item get_cookie ()

Returns the cookie for a list, if available.

=item get_max_size ()

Returns the maximum allowed size for a message.

=item get_reply_to ()

Returns an array with the Reply-To values.

=item get_default_user_options ()

Returns a default option of the list for subscription.

=item get_total ()

Returns the number of subscribers to the list.

=item get_user ( USER )

Returns a hash with the informations regarding the indicated
user.

=item get_first_user ()

Returns a hash to the first user on the list.

=item get_next_user ()

Returns a hash to the next users, until we reach the end of
the list.

=item update_user ( USER, HASHPTR )

Sets the new values given in the hash for the user.

=item add_user ( USER, HASHPTR )

Adds a new user to the list. May overwrite existing
entries.

=item is_user ( USER )

Returns true if the indicated user is member of the list.
 
=item am_i ( FUNCTION, USER )

Returns true is USER has FUNCTION (owner, editor) on the
list.

=item get_state ( FLAG )

Returns the value for a flag : sig or sub.

=item may_do ( ACTION, USER )

Chcks is USER may do the ACTION for the list. ACTION can be
one of following : send, review, index, getm add, del,
reconfirm, purge.

=item is_moderated ()

Returns true if the list is moderated.

=item archive_exist ( FILE )

Returns true if the indicated file exists.

=item archive_send ( WHO, FILE )

Send the indicated archive file to the user, if it exists.

=item archive_ls ()

Returns the list of available files, if any.

=item archive_msg ( MSG )

Archives the Mail::Internet message given as argument.

=item is_archived ()

Returns true is the list is configured to keep archives of
its messages.

=item get_stats ( OPTION )

Returns either a formatted printable strings or an array whith
the statistics. OPTION can be 'text' or 'array'.

=item print_info ( FDNAME )

Print the list informations to the given file descriptor, or the
currently selected descriptor.

=cut

use Carp;

use Mail::Header;
use Mail::Internet;
use Archive;
use Language;
use Log;
use Conf;
salaun's avatar
salaun committed
171
use mail;
root's avatar
root committed
172
173
174
175
176
177
178
use Time::Local;
use MIME::Entity;
use MIME::Words;
use MIME::Parser;

## Database and SQL statement handlers
my ($dbh, $sth, @sth_stack, $use_db);
179
180

my %list_cache;
salaun's avatar
salaun committed
181
my %persistent_cache;
182

root's avatar
root committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
my %date_format = (
		   'read' => {
		       'Pg' => 'date_part(\'epoch\',%s)',
		       'mysql' => 'UNIX_TIMESTAMP(%s)',
		       'Oracle' => '((to_number(to_char(%s,\'J\')) - to_number(to_char(to_date(\'01/01/1970\',\'dd/mm/yyyy\'), \'J\'))) * 86400) +to_number(to_char(%s,\'SSSSS\'))',
		       'Sybase' => 'datediff(second, "01/01/1970",%s)'
		       },
		   'write' => {
		       'Pg' => '\'epoch\'::datetime + \'%d sec\'',
		       'mysql' => 'FROM_UNIXTIME(%d)',
		       'Oracle' => 'to_date(to_char(round(%s/86400) + to_number(to_char(to_date(\'01/01/1970\',\'dd/mm/yyyy\'), \'J\'))) || \':\' ||to_char(mod(%s,86400)), \'J:SSSSS\')',
		       'Sybase' => 'dateadd(second,%s,"01/01/1970")'
		       }
	       );

## Regexps for list params
my %regexp = ('email' => '(\S+|\".*\")(@\S+)',
	      'host' => '[\w\.\-]+',
	      'listname' => '[a-z0-9][a-z0-9\-\._]+',
	      'sql_query' => 'SELECT.*',
203
204
	      'scenario' => '[\w,\.\-]+',
	      'task' => '\w+'
root's avatar
root committed
205
206
207
208
209
210
211
212
213
	      );

## List parameters defaults
my %default = ('occurrence' => '0-1',
	       'length' => 25
	       );

my @param_order = qw (subject visibility info subscribe add unsubscribe del owner send editor 
		      account topics 
salaun's avatar
salaun committed
214
		      host lang web_archive archive digest available_user_options 
215
		      default_user_options reply_to_header reply_to forced_reply_to * 
root's avatar
root committed
216
		      welcome_return_path remind_return_path user_data_source include_file 
217
		      include_list include_ldap_query include_ldap_2level_query include_sql_query ttl creation update 
root's avatar
root committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
		      status serial);

## List parameters aliases
my %alias = ('reply-to' => 'reply_to',
	     'replyto' => 'reply_to',
	     'forced_replyto' => 'forced_reply_to',
	     'forced_reply-to' => 'forced_reply_to',
	     'custom-subject' => 'custom_subject',
	     'custom-header' => 'custom_header',
	     'subscription' => 'subscribe',
	     'unsubscription' => 'unsubscribe',
	     'max-size' => 'max_size');

##############################################################
salaun's avatar
salaun committed
232
## This hash COMPLETELY defines ALL list parameters     
root's avatar
root committed
233
234
235
236
237
238
239
## It is then used to load, save, view, edit list config files
##############################################################
## List parameters format accepts the following keywords :
## format :      Regexp aplied to the configuration file entry; 
##               some common regexps are defined in %regexp
## file_format : Config file format of the parameter might not be
##               the same in memory
240
## split_char:   Character used to separate multiple parameters 
root's avatar
root committed
241
242
243
244
245
246
247
## length :      Length of a scalar variable ; used in web forms
## scenario :    tells that the parameter is a scenario, providing its name
## default :     Default value for the param ; may be a configuration parameter (conf)
## synonym :     Defines synonyms for parameter values (for compatibility reasons)
## unit :        Unit of the parameter ; this is used in web forms
## occurrence :  Occurerence of the parameter in the config file
##               possible values: 0-1 | 1 | 0-n | 1-n
248
##               example : a list may have multiple owner 
root's avatar
root committed
249
## title_id :    Title reference in NLS catalogues
salaun's avatar
salaun committed
250
## group :       Group of parameters
251
252
253
## obsolete :    Obsolete parameter ; should not be displayed 
##               nor saved
## order :       Order of parameters within paragraph
root's avatar
root committed
254
255
256
###############################################################
%::pinfo = ('account' => {'format' => '\S+',
			  'length' => 10,
salaun's avatar
salaun committed
257
258
			  'title_id' => 1,
			  'group' => 'other'
root's avatar
root committed
259
260
			  },
	    'add' => {'scenario' => 'add',
salaun's avatar
salaun committed
261
262
		      'title_id' => 2,
		      'group' => 'command'
root's avatar
root committed
263
264
		      },
	    'anonymous_sender' => {'format' => '.+',
salaun's avatar
salaun committed
265
266
				   'title_id' => 3,
				   'group' => 'tuning'
root's avatar
root committed
267
268
269
				   },
	    'archive' => {'format' => {'period' => {'format' => ['day','week','month','quarter','year'],
						    'synonym' => {'weekly' => 'week'},
270
271
						    'title_id' => 5,
						    'order' => 1
root's avatar
root committed
272
273
274
						},
				       'access' => {'format' => ['open','private','public','owner','closed'],
						    'synonym' => {'open' => 'public'},
275
276
						    'title_id' => 6,
						    'order' => 2
root's avatar
root committed
277
278
						}
				   },
salaun's avatar
salaun committed
279
280
			  'title_id' => 4,
			  'group' => 'archives'
root's avatar
root committed
281
		      },
282
	    'available_user_options' => {'format' => {'reception' => {'format' => ['mail','notice','digest','summary','nomail','txt','html','urlize','not_me'],
283
284
								      'occurrence' => '1-n',
								      'split_char' => ',',
285
								      'default' => 'mail,notice,digest,summary,nomail,txt,html,urlize,not_me',
286
								      'title_id' => 89
287
288
								      }
						  },
289
					 'title_id' => 88
290
				     },
root's avatar
root committed
291
292
293
294
295

	    'bounce' => {'format' => {'warn_rate' => {'format' => '\d+',
						      'length' => 3,
						      'unit' => '%',
						      'default' => {'conf' => 'bounce_warn_rate'},
296
297
						      'title_id' => 8,
						      'order' => 1
root's avatar
root committed
298
299
300
301
302
						  },
				      'halt_rate' => {'format' => '\d+',
						      'length' => 3,
						      'unit' => '%',
						      'default' => {'conf' => 'bounce_halt_rate'},
303
304
						      'title_id' => 9,
						      'order' => 2
root's avatar
root committed
305
306
						  }
				  },
salaun's avatar
salaun committed
307
308
			 'title_id' => 7,
			 'group' => 'bounces'
root's avatar
root committed
309
310
311
312
313
		     },
	    'clean_delay_queuemod' => {'format' => '\d+',
				       'length' => 3,
				       'unit' => 'days',
				       'default' => {'conf' => 'clean_delay_queuemod'},
salaun's avatar
salaun committed
314
315
				       'title_id' => 10,
				       'group' => 'other'
root's avatar
root committed
316
317
318
319
				       },
	    'cookie' => {'format' => '\S+',
			 'length' => 15,
			 'default' => {'conf' => 'cookie'},
salaun's avatar
salaun committed
320
321
			 'title_id' => 11,
			 'group' => 'other'
root's avatar
root committed
322
323
324
		     },
	    'creation' => {'format' => {'date_epoch' => {'format' => '\d+',
							 'occurrence' => '1',
325
326
							 'title_id' => 13,
							 'order' => 3
root's avatar
root committed
327
328
						     },
					'date' => {'format' => '.+',
329
330
						   'title_id' => 14,
						   'order' => 2
root's avatar
root committed
331
332
333
						   },
					'email' => {'format' => $regexp{'email'},
						    'occurrence' => '1',
334
335
						    'title_id' => 15,
						    'order' => 1
root's avatar
root committed
336
337
						    }
				    },
salaun's avatar
salaun committed
338
339
			   'title_id' => 12,
			   'group' => 'other'
root's avatar
root committed
340
341
342
343
344

		       },
	    'custom_header' => {'format' => '\S+:\s+.*',
				'length' => 30,
				'occurrence' => '0-n',
salaun's avatar
salaun committed
345
346
				'title_id' => 16,
				'group' => 'tuning'
root's avatar
root committed
347
348
349
				},
	    'custom_subject' => {'format' => '.*',
				 'length' => 15,
salaun's avatar
salaun committed
350
351
				 'title_id' => 17,
				 'group' => 'tuning'
root's avatar
root committed
352
				 },
353
	    'default_user_options' => {'format' => {'reception' => {'format' => ['digest','mail','nomail','summary','notice','txt','html','urlize','not_me'],
root's avatar
root committed
354
								    'default' => 'mail',
355
356
								    'title_id' => 19,
								    'order' => 1
root's avatar
root committed
357
358
359
								    },
						    'visibility' => {'format' => ['conceal','noconceal'],
								     'default' => 'noconceal',
360
361
								     'title_id' => 20,
								     'order' => 2
root's avatar
root committed
362
363
								     }
						},
salaun's avatar
salaun committed
364
365
				       'title_id' => 18,
				       'group' => 'other'
root's avatar
root committed
366
367
				   },
	    'del' => {'scenario' => 'del',
salaun's avatar
salaun committed
368
369
		      'title_id' => 21,
		      'group' => 'command'
root's avatar
root committed
370
371
372
		      },
	    'digest' => {'file_format' => '\d+(\s*,\s*\d+)*\s+\d+:\d+',
			 'format' => {'days' => {'format' => [1..7],
salaun's avatar
salaun committed
373
						 'file_format' => '1|2|3|4|5|6|7',
root's avatar
root committed
374
						 'occurrence' => '1-n',
375
376
						 'title_id' => 23,
						 'order' => 1
root's avatar
root committed
377
378
379
380
						 },
				      'hour' => {'format' => '\d+',
						 'length' => 2,
						 'occurrence' => '1',
381
382
						 'title_id' => 24,
						 'order' => 2
root's avatar
root committed
383
384
385
386
						 },
				      'minute' => {'format' => '\d+',
						   'length' => 2,
						   'occurrence' => '1',
387
388
						   'title_id' => 25,
						   'order' => 3
root's avatar
root committed
389
390
						   }
				  },
salaun's avatar
salaun committed
391
392
			 'title_id' => 22,
			 'group' => 'tuning'
root's avatar
root committed
393
		     },
salaun's avatar
salaun committed
394

root's avatar
root committed
395
396
397
	    'editor' => {'format' => {'email' => {'format' => $regexp{'email'},
						  'length' => 30,
						  'occurrence' => '1',
398
399
						  'title_id' => 27,
						  'order' => 1
root's avatar
root committed
400
401
402
						  },
				      'reception' => {'format' => ['mail','nomail'],
						      'default' => 'mail',
403
404
						      'title_id' => 28,
						      'order' => 4
root's avatar
root committed
405
406
407
						      },
				      'gecos' => {'format' => '.+',
						  'length' => 30,
408
409
						  'title_id' => 29,
						  'order' => 2
root's avatar
root committed
410
411
412
						  },
				      'info' => {'format' => '.+',
						 'length' => 30,
413
414
						 'title_id' => 30,
						 'order' => 3
root's avatar
root committed
415
416
417
						 }
				  },
			 'occurrence' => '0-n',
salaun's avatar
salaun committed
418
419
			 'title_id' => 26,
			 'group' => 'description'
root's avatar
root committed
420
			 },
421
422
423
	    'expire_task' => {'task' => 'expire',
			      'title_id' => 95
			 },
root's avatar
root committed
424
425
	    'footer_type' => {'format' => ['mime','append'],
			      'default' => 'mime',
salaun's avatar
salaun committed
426
427
			      'title_id' => 31,
			      'group' => 'tuning'
root's avatar
root committed
428
429
			      },
	    'forced_reply_to' => {'format' => '\S+',
salaun's avatar
salaun committed
430
				  'title_id' => 32,
431
432
				  'group' => 'tuning',
				  'obsolete' => 1
root's avatar
root committed
433
434
435
			 },
	    'host' => {'format' => $regexp{'host'},
		       'length' => 20,
salaun's avatar
salaun committed
436
437
		       'title_id' => 33,
		       'group' => 'description'
root's avatar
root committed
438
439
440
441
		   },
	    'include_file' => {'format' => '\S+',
			       'length' => 20,
			       'occurrence' => '0-n',
salaun's avatar
salaun committed
442
443
			       'title_id' => 34,
			       'group' => 'data_source'
root's avatar
root committed
444
445
446
447
448
449
450
451
			       },

#	    'include_admin' => {'format' => ['owners','editors','privileged_owners'],
#				 'occurrence' => '0-n'
#				 },

	    'include_ldap_query' => {'format' => {'host' => {'format' => $regexp{'host'},
							     'occurrence' => '1',
452
453
							     'title_id' => 36,
							     'order' => 1
root's avatar
root committed
454
455
456
457
							     },
						  'port' => {'format' => '\d+',
							     'default' => 389,
							     'length' => 4,
458
459
							     'title_id' => 37,
							     'order' => 2
root's avatar
root committed
460
461
							     },
						  'user' => {'format' => '.*',
462
463
							     'title_id' => 38,
							     'order' => 3
root's avatar
root committed
464
465
466
							     },
						  'passwd' => {'format' => '.*',
							       'length' => 10,
467
468
							       'title_id' => 39,
							       'order' => 3
root's avatar
root committed
469
470
							       },
						  'suffix' => {'format' => '.*',
471
472
							       'title_id' => 40,
							       'order' => 4
root's avatar
root committed
473
474
475
476
							       },
						  'filter' => {'format' => '.*',
							       'length' => 50,
							       'occurrence' => '1',
477
							       'title_id' => 41,
478
							       'order' => 7
root's avatar
root committed
479
480
481
482
							       },
						  'attrs' => {'format' => '\w+',
							      'length' => 15,
							      'default' => 'mail',
483
							      'title_id' => 42,
484
							      'order' => 8
root's avatar
root committed
485
486
487
							      },
						  'select' => {'format' => ['all','first'],
							       'default' => 'first',
488
							       'title_id' => 43,
489
490
491
492
493
494
495
496
497
498
499
500
501
							       'order' => 9
							       },
					          'scope' => {'format' => ['base','one','sub'],
							      'default' => 'sub',
							      'title_id' => 97,
							      'order' => 5
							      },
						  'timeout' => {'format' => '\w+',
								'default' => 30,
								'unit' => 'seconds',
								'title_id' => 98,
								'order' => 6
								}
root's avatar
root committed
502
503
					      },
				     'occurrence' => '0-n',
salaun's avatar
salaun committed
504
505
				     'title_id' => 35,
				     'group' => 'data_source'
root's avatar
root committed
506
				     },
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
	    'include_ldap_2level_query' => {'format' => {'host' => {'format' => $regexp{'host'},
							     'occurrence' => '1',
							     'title_id' => 136,
							     'order' => 1
							     },
						  'port' => {'format' => '\d+',
							     'default' => 389,
							     'length' => 4,
							     'title_id' => 137,
							     'order' => 2
							     },
						  'user' => {'format' => '.*',
							     'title_id' => 138,
							     'order' => 3
							     },
						  'passwd' => {'format' => '.*',
							       'length' => 10,
							       'title_id' => 139,
							       'order' => 3
							       },
						  'suffix1' => {'format' => '.*',
							       'title_id' => 140,
							       'order' => 4
							       },
						  'filter1' => {'format' => '.*',
							       'length' => 50,
							       'occurrence' => '1',
							       'title_id' => 141,
							       'order' => 7
							       },
						  'attrs1' => {'format' => '\w+',
							      'length' => 15,
							      'title_id' => 142,
							      'order' => 8
							      },
						  'select1' => {'format' => ['all','first','regex'],
							       'default' => 'first',
							       'title_id' => 143,
							       'order' => 9
							       },
					          'scope1' => {'format' => ['base','one','sub'],
							      'default' => 'sub',
							      'title_id' => 197,
							      'order' => 5
							      },
						  'timeout1' => {'format' => '\w+',
								'default' => 30,
								'unit' => 'seconds',
								'title_id' => 198,
								'order' => 6
								},
						  'regex1' => {'format' => '.+',
								'length' => 50,
								'default' => '',
								'title_id' => 201,
								'order' => 10
								},
						  'suffix2' => {'format' => '.*',
							       'title_id' => 144,
							       'order' => 11
							       },
						  'filter2' => {'format' => '.*',
							       'length' => 50,
							       'occurrence' => '1',
							       'title_id' => 145,
							       'order' => 14
							       },
						  'attrs2' => {'format' => '\w+',
							      'length' => 15,
							      'default' => 'mail',
							      'title_id' => 146,
							      'order' => 15
							      },
						  'select2' => {'format' => ['all','first','regex'],
							       'default' => 'first',
							       'title_id' => 147,
							       'order' => 16
							       },
					          'scope2' => {'format' => ['base','one','sub'],
							      'default' => 'sub',
							      'title_id' => 199,
							      'order' => 12
							      },
						  'timeout2' => {'format' => '\w+',
								'default' => 30,
								'unit' => 'seconds',
								'title_id' => 200,
								'order' => 13
								},
						  'regex2' => {'format' => '.+',
								'length' => 50,
								'default' => '',
aumont's avatar
aumont committed
599
								'title_id' => 202,
600
601
602
603
604
605
606
								'order' => 17
								}
					      },
				     'occurrence' => '0-n',
				     'title_id' => 135,
				     'group' => 'data_source'
				     },
root's avatar
root committed
607
608
	    'include_list' => {'format' => $regexp{'listname'},
			       'occurrence' => '0-n',
salaun's avatar
salaun committed
609
610
			       'title_id' => 44,
			       'group' => 'data_source'
root's avatar
root committed
611
612
613
			       },
	    'include_sql_query' => {'format' => {'db_type' => {'format' => ['mysql','Pg','Oracle','Sybase'],
							       'occurrence' => '1',
614
615
							       'title_id' => 46,
							       'order' => 1
root's avatar
root committed
616
617
618
							       },
						 'host' => {'format' => $regexp{'host'},
							    'occurrence' => '1',
619
620
							    'title_id' => 47,
							    'order' => 2
root's avatar
root committed
621
							    },
622
						 'db_name' => {'format' => '\S+',
root's avatar
root committed
623
							       'occurrence' => '1',
624
625
							       'title_id' => 48,
							       'order' => 3 
root's avatar
root committed
626
							       },
627
628
629
630
						 'connect_options' => {'format' => '.+',
								       'title_id' => 94,
								       'order' => 4
								       },
631
						 'user' => {'format' => '\S+',
root's avatar
root committed
632
							    'occurrence' => '1',
633
							    'title_id' => 49,
634
							    'order' => 5
root's avatar
root committed
635
636
							    },
						 'passwd' => {'format' => '.+',
637
							      'title_id' => 50,
638
							      'order' => 6
root's avatar
root committed
639
640
641
642
							      },
						 'sql_query' => {'format' => $regexp{'sql_query'},
								 'length' => 50,
								 'occurrence' => '1',
643
								 'title_id' => 51,
644
								 'order' => 7
root's avatar
root committed
645
646
								 },
						 'f_dir' => {'format' => '.+',
647
							     'title_id' => 52,
648
							     'order' => 8
root's avatar
root committed
649
650
651
							     }
					     },
				    'occurrence' => '0-n',
salaun's avatar
salaun committed
652
653
				    'title_id' => 45,
				    'group' => 'data_source'
root's avatar
root committed
654
655
				    },
	    'info' => {'scenario' => 'info',
salaun's avatar
salaun committed
656
657
		       'title_id' => 53,
		       'group' => 'command'
root's avatar
root committed
658
659
		       },
	    'invite' => {'scenario' => 'invite',
salaun's avatar
salaun committed
660
661
			 'title_id' => 54,
			 'group' => 'command'
root's avatar
root committed
662
			 },
663
	    'lang' => {'format' => ['fr','us','de','it','fi','es','tw','cn','pl','cz','hu'],
root's avatar
root committed
664
		       'default' => {'conf' => 'lang'},
salaun's avatar
salaun committed
665
666
		       'title_id' => 55,
		       'group' => 'description'
root's avatar
root committed
667
668
669
670
671
		   },
	    'max_size' => {'format' => '\d+',
			   'length' => 8,
			   'unit' => 'bytes',
			   'default' => {'conf' => 'max_size'},
salaun's avatar
salaun committed
672
673
			   'title_id' => 56,
			   'group' => 'tuning'
root's avatar
root committed
674
675
676
677
		       },
	    'owner' => {'format' => {'email' => {'format' => $regexp{'email'},
						 'length' =>30,
						 'occurrence' => '1',
678
679
						 'title_id' => 58,
						 'order' => 1
root's avatar
root committed
680
681
682
						 },
				     'reception' => {'format' => ['mail','nomail'],
						     'default' => 'mail',
683
684
						     'title_id' => 59,
						     'order' =>5
root's avatar
root committed
685
686
687
						     },
				     'gecos' => {'format' => '.+',
						 'length' => 30,
688
689
						 'title_id' => 60,
						 'order' => 2
root's avatar
root committed
690
691
692
						 },
				     'info' => {'format' => '.+',
						'length' => 30,
693
694
						'title_id' => 61,
						'order' => 3
root's avatar
root committed
695
696
697
						},
				     'profile' => {'format' => ['privileged','normal'],
						   'default' => 'normal',
698
699
						   'title_id' => 62,
						   'order' => 4
root's avatar
root committed
700
701
702
						   }
				 },
			'occurrence' => '1-n',
salaun's avatar
salaun committed
703
704
			'title_id' => 57,
			'group' => 'description'
root's avatar
root committed
705
706
707
708
			},
	    'priority' => {'format' => [0..9,'z'],
			   'length' => 1,
			   'default' => {'conf' => 'default_list_priority'},
salaun's avatar
salaun committed
709
710
			   'title_id' => 63,
			   'group' => 'tuning'
root's avatar
root committed
711
712
		       },
	    'remind' => {'scenario' => 'remind',
salaun's avatar
salaun committed
713
714
			 'title_id' => 64,
			 'group' => 'command'
root's avatar
root committed
715
716
717
			  },
	    'remind_return_path' => {'format' => ['unique','owner'],
				     'default' => {'conf' => 'remind_return_path'},
salaun's avatar
salaun committed
718
719
				     'title_id' => 65,
				     'group' => 'bounces'
root's avatar
root committed
720
				 },
721
722
723
	    'remind_task' => {'task' => 'remind',
			      'tilte-id' => 96
			      },
root's avatar
root committed
724
725
	    'reply_to' => {'format' => '\S+',
			   'default' => 'sender',
salaun's avatar
salaun committed
726
			   'title_id' => 66,
727
728
			   'group' => 'tuning',
			   'obsolete' => 1
root's avatar
root committed
729
			   },
730
	    'reply_to_header' => {'format' => {'value' => {'format' => ['sender','list','all','other_email'],
731
732
							   'default' => 'sender',
							   'title_id' => 91,
aumont's avatar
aumont committed
733
							   'occurrence' => '1',
734
							   'order' => 1
735
736
							   },
					       'other_email' => {'format' => $regexp{'email'},
737
738
								 'title_id' => 92,
								 'order' => 2
739
740
741
								 },
					       'apply' => {'format' => ['forced','respect'],
							   'default' => 'respect',
742
743
							   'title_id' => 93,
							   'order' => 3
744
745
746
747
748
							   }
					   },
				  'title_id' => 90,
				  'group' => 'tuning'
				  },		
root's avatar
root committed
749
	    'review' => {'scenario' => 'review',
salaun's avatar
salaun committed
750
			 'synonym' => {'open' => 'public'},
salaun's avatar
salaun committed
751
752
			 'title_id' => 67,
			 'group' => 'command'
root's avatar
root committed
753
754
755
756
757
758
759
			 },
	    'send' => {'scenario' => 'send',
		       'title_id' => 68
		       },
	    'serial' => {'format' => '\d+',
			 'default' => 0,
			 'length' => 3,
salaun's avatar
salaun committed
760
			 'default' => 0,
salaun's avatar
salaun committed
761
762
			 'title_id' => 69,
			 'group' => 'other'
root's avatar
root committed
763
764
			 },
	    'shared_doc' => {'format' => {'d_read' => {'scenario' => 'd_read',
765
766
						       'title_id' => 86,
						       'order' => 1
root's avatar
root committed
767
768
						       },
					  'd_edit' => {'scenario' => 'd_edit',
769
770
						       'title_id' => 87,
						       'order' => 2
root's avatar
root committed
771
772
						       }
				      },
salaun's avatar
salaun committed
773
774
			     'title_id' => 70,
			     'group' => 'command'
root's avatar
root committed
775
776
777
			 },
	    'status' => {'format' => ['open','closed','pending'],
			 'default' => 'open',
salaun's avatar
salaun committed
778
779
			 'title_id' => 71,
			 'group' => 'other'
root's avatar
root committed
780
781
782
783
			  },
	    'subject' => {'format' => '.+',
			  'length' => 50,
			  'occurrence' => '1',
salaun's avatar
salaun committed
784
785
			  'title_id' => 72,
			  'group' => 'description'
root's avatar
root committed
786
787
			   },
	    'subscribe' => {'scenario' => 'subscribe',
salaun's avatar
salaun committed
788
789
			    'title_id' => 73,
			    'group' => 'command'
root's avatar
root committed
790
			    },
791
	    'topics' => {'format' => '\w+(\/\w+)?',
792
			 'split_char' => ',',
root's avatar
root committed
793
			 'occurrence' => '0-n',
salaun's avatar
salaun committed
794
795
			 'title_id' => 74,
			 'group' => 'description'
root's avatar
root committed
796
797
798
799
800
			 },
	    'ttl' => {'format' => '\d+',
		      'length' => 6,
		      'unit' => 'seconds',
		      'default' => 3600,
salaun's avatar
salaun committed
801
802
		      'title_id' => 75,
		      'group' => 'data_source'
root's avatar
root committed
803
804
		      },
	    'unsubscribe' => {'scenario' => 'unsubscribe',
salaun's avatar
salaun committed
805
806
			      'title_id' => 76,
			      'group' => 'command'
root's avatar
root committed
807
808
809
810
			      },
	    'update' => {'format' => {'date_epoch' => {'format' => '\d+',
						       'length' => 8,
						       'occurrence' => '1',
811
812
						       'title_id' => 78,
						       'order' => 3
root's avatar
root committed
813
814
815
						       },
				      'date' => {'format' => '.+',
						 'length' => 30,
816
817
						 'title_id' => 79,
						 'order' => 2
root's avatar
root committed
818
819
820
821
						 },
				      'email' => {'format' => $regexp{'email'},
						  'length' => 30,
						  'occurrence' => '1',
822
823
						  'title_id' => 80,
						  'order' => 1
root's avatar
root committed
824
825
						  }
				  },
salaun's avatar
salaun committed
826
827
			 'title_id' => 77,
			 'group' => 'other'
root's avatar
root committed
828
829
830
		     },
	    'user_data_source' => {'format' => ['database','file','include'],
				   'default' => 'file',
salaun's avatar
salaun committed
831
832
				   'title_id' => 81,
				   'group' => 'data_source'
root's avatar
root committed
833
834
				   },
	    'visibility' => {'scenario' => 'visibility',
salaun's avatar
salaun committed
835
			     'synonym' => {'public' => 'noconceal'},
salaun's avatar
salaun committed
836
837
			     'title_id' => 82,
			     'group' => 'description'
root's avatar
root committed
838
839
840
841
842
			     },
	    'web_archive'  => {'format' => {'access' => {'scenario' => 'access_web_archive',
							 'title_id' => 84
							 }
					},
salaun's avatar
salaun committed
843
844
			       'title_id' => 83,
			       'group' => 'archives'
root's avatar
root committed
845
846
847
848

			   },
	    'welcome_return_path' => {'format' => ['unique','owner'],
				      'default' => {'conf' => 'welcome_return_path'},
salaun's avatar
salaun committed
849
850
				      'title_id' => 85,
				      'group' => 'bounces'
salaun's avatar
salaun committed
851
852
853
854
855
856
857
858
				  },
	    
	    'export' => {'format' => '\w+',
			 'split_char' => ',',
			 'occurrence' => '0-n',
			 'default' =>'',
		     }
	    
root's avatar
root committed
859
860
861
862
	    );

## This is the generic hash which keeps all lists in memory.
my %list_of_lists = ();
salaun's avatar
salaun committed
863
my %list_of_robots = ();
root's avatar
root committed
864
my %list_of_topics = ();
salaun's avatar
salaun committed
865
866
867
868
my %edit_list_conf = ();

## Last modification times
my %mtime;
root's avatar
root committed
869
870
871
872
873
874

use Fcntl;
use DB_File;

$DB_BTREE->{compare} = '_compare_addresses';

salaun's avatar
salaun committed
875
876
877
878
879
sub LOCK_SH {1};
sub LOCK_EX {2};
sub LOCK_NB {4};
sub LOCK_UN {8};

root's avatar
root committed
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
## Connect to Database
sub db_connect {
    do_log('debug2', 'List::db_connect');

    my $connect_string;

    unless (require DBI) {
	do_log ('info',"Unable to use DBI library, install DBI (CPAN) first");
	return undef;
    }

    ## Do we have db_xxx required parameters
    foreach my $db_param ('db_type','db_name','db_host','db_user') {
	unless ($Conf{$db_param}) {
	    do_log ('info','Missing parameter %s for DBI connection', $db_param);
	    return undef;
	}
    }

899
900
901
902
903
904
905
    ## Used by Oracle (ORACLE_HOME)
    if ($Conf{'db_env'}) {
	foreach my $env (keys %{$Conf{'db_env'}}) {
	    $ENV{$env} = $Conf{'db_env'}{$env};
	}
    }

root's avatar
root committed
906
907
908
909
910
911
912
913
914
915
916
    if ($Conf{'db_type'} eq 'Oracle') {
	## Oracle uses sids instead of dbnames
	$connect_string = sprintf 'DBI:%s:sid=%s;host=%s', $Conf{'db_type'}, $Conf{'db_name'}, $Conf{'db_host'};

    }elsif ($Conf{'db_type'} eq 'Sybase') {
	$connect_string = sprintf 'DBI:%s:dbname=%s;server=%s', $Conf{'db_type'}, $Conf{'db_name'}, $Conf{'db_host'};

    }else {
	$connect_string = sprintf 'DBI:%s:dbname=%s;host=%s', $Conf{'db_type'}, $Conf{'db_name'}, $Conf{'db_host'};
    }

917
918
919
920
    if ($Conf{'db_port'}) {
	$connect_string .= ';port=' . $Conf{'db_port'};
    }

921
922
923
924
    if ($Conf{'db_options'}) {
	$connect_string .= ';' . $Conf{'db_options'};
    }

root's avatar
root committed
925
    unless ( $dbh = DBI->connect($connect_string, $Conf{'db_user'}, $Conf{'db_passwd'}) ) {
salaun's avatar
salaun committed
926
	do_log ('err','Can\'t connect to Database %s as %s', $connect_string, $Conf{'db_user'});
root's avatar
root committed
927

928
	&send_notify_to_listmaster('no_db', $Conf{'domain'});
root's avatar
root committed
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
	&fatal_err('Sympa cannot connect to database %s, dying', $Conf{'db_name'});

#	return undef;
    }

    if ($Conf{'db_type'} eq 'Pg') { # Configure Postgres to use ISO format dates
       $dbh->do ("SET DATESTYLE TO 'ISO';");
    }

    ## added sybase support
    if ($Conf{'db_type'} eq 'Sybase') { # Configure to use sympa database 
	my $dbname;
	$dbname="use $Conf{'db_name'}";
        $dbh->do ($dbname);
    }

    do_log('debug','Connected to Database %s',$Conf{'db_name'});

    return 1;
}

## Disconnect from Database
sub db_disconnect {
    do_log('debug2', 'List::db_disconnect');

    unless ($dbh->disconnect()) {
	do_log ('notice','Can\'t disconnect from Database %s : %s',$Conf{'db_name'}, $dbh->errstr);
	return undef;
    }

    return 1;
}

salaun's avatar
salaun committed
962
963
964
965
966
967
968
969
## Get database handler
sub db_get_handler {
    do_log('debug2', 'List::db_get_handler');


    return $dbh;
}

root's avatar
root committed
970
971
## Creates an object.
sub new {
972
    my($pkg, $name, $robot) = @_;
root's avatar
root committed
973
    my $liste={};
974
975
976
977
978
979
980
981
    do_log('debug2', 'List::new(%s,%s)', $name, $robot);
    
    ## Allow robot in the name
    if ($name =~ /\@/) {
	my @parts = split /\@/, $name;
	$robot ||= $parts[1];
	$name = $parts[0];
    }
root's avatar
root committed
982
983
984
985
986
987
988
989
990
991
992
993
994

    ## Only process the list if the name is valid.
    unless ($name and ($name =~ /^[a-z0-9][a-z0-9\-\+\._]*$/io) ) {
	&do_log('info', 'Incorrect listname "%s"',  $name);
	return undef;
    }
    ## Lowercase the list name.
    $name =~ tr/A-Z/a-z/;
    
    if ($list_of_lists{$name}){
	# use the current list in memory and update it
	$liste=$list_of_lists{$name};
    }else{
salaun's avatar
salaun committed
995
	do_log('debug', 'List object %s creation', $name) if $main::options{'debug'}; ##TEMP
root's avatar
root committed
996
997
998
999

	# create a new object list
	bless $liste, $pkg;
    }
1000
    return undef unless ($liste->load($name, $robot));