Handle hostnames with upper-case letters
[webmin.git] / spam / save_white.cgi
1 #!/usr/local/bin/perl
2 # save_white.cgi
3 # Save white and black lists of to and from addresses
4
5 require './spam-lib.pl';
6 &error_setup($text{'white_err'});
7 &ReadParseMime();
8 &set_config_file_in(\%in);
9 &can_use_check("white");
10 &execute_before("white");
11 &lock_spam_files();
12 $conf = &get_config();
13
14 &parse_textbox($conf, "whitelist_from");
15
16 &parse_textbox($conf, 'unwhitelist_from');
17
18 @rcvd = &parse_table("whitelist_from_rcvd", \&rcvd_parser);
19 &save_directives($conf, 'whitelist_from_rcvd', \@rcvd, 1);
20
21 &parse_textbox($conf, 'blacklist_from');
22
23 &parse_textbox($conf, 'unblacklist_from');
24
25 @to = &parse_table("whitelist_to", \&to_parser);
26 @oldto = ( &find("whitelist_to", $conf),
27            &find("more_spam_to", $conf),
28            &find("all_spam_to", $conf) );
29 &save_directives($conf, \@oldto, \@to, 0);
30
31 # Add any imported addresses
32 if ($in{'import'}) {
33         @addrs = ( );
34         while($in{'import'} =~ s/((([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+([a-zA-Z]{2,})+)))//) {
35                 push(@addrs, $1);
36                 }
37         @addrs || &error($text{'import_enone'});
38         @addrs = &unique(@addrs);
39
40         @from = map { @{$_->{'words'}} } &find("whitelist_from", $conf);
41         %already = map { lc($_), 1 } @from;
42         @newaddrs = grep { !$already{lc($_)} } @addrs;
43         push(@from, @newaddrs);
44         if ($in{'sort'}) {
45                 @from = sort { ($ua, $da) = split(/\@/, $a);
46                                ($ub, $db) = split(/\@/, $b);
47                                lc($da) cmp lc($db) || lc($ua) cmp lc($ub) }
48                              @from;
49                 }
50         &save_directives($conf, 'whitelist_from', \@from, 1);
51         }
52
53 &flush_file_lines();
54 &unlock_spam_files();
55 &execute_after("white");
56 &webmin_log("white");
57 &redirect($redirect_url);
58
59 sub from_parser
60 {
61 return undef if (!$_[1]);
62 $_[1] =~ /^\S+$/ || &error(&text('white_efrom', $_[1]));
63 return $_[1];
64 }
65
66 sub rcvd_parser
67 {
68 local $a = &from_parser($_[0], $_[1]);
69 return undef if (!$a);
70 $_[2] =~ /^[A-Za-z0-9\.\-]+$/ || &error(&text('white_ercvd', $_[2]));
71 return "$a $_[2]";
72 }
73
74 sub to_parser
75 {
76 local $a = &from_parser($_[0], $_[1]);
77 return undef if (!$a);
78 return { 'name' => $_[2] == 0 ? 'whitelist_to' :
79                    $_[2] == 1 ? 'more_spam_to' : 'all_spam_to',
80          'value' => $a };
81 }
82