Handle hostnames with upper-case letters
[webmin.git] / spam / save_procmail.cgi
1 #!/usr/local/bin/perl
2 # Change the procmail rule that delivers spam
3
4 require './spam-lib.pl';
5 &error_setup($text{'procmail_err'});
6 &ReadParse();
7 &set_config_file_in(\%in);
8 &can_use_check("procmail");
9
10 # Validate inputs
11 $type = undef;
12 if ($in{'to'} == 0) {
13         $file = "/dev/null";
14         }
15 elsif ($in{'to'} == 1) {
16         $in{'mbox'} =~ /^\S+$/ || &error($text{'setup_efile'});
17         $file = $in{'mbox'};
18         }
19 elsif ($in{'to'} == 2) {
20         $in{'maildir'} =~ /^\S+$/ || &error($text{'setup_emaildir'});
21         $file = "$in{'maildir'}/";
22         }
23 elsif ($in{'to'} == 3) {
24         $in{'mhdir'} =~ /^\S+$/ || &error($text{'setup_emhdir'});
25         $file = "$in{'mhdir'}/.";
26         }
27 elsif ($in{'to'} == 4) {
28         $file = "\$DEFAULT";
29         }
30 elsif ($in{'to'} == 5) {
31         $in{'email'} =~ /^\S+$/ || &error($text{'setup_eemail'});
32         $file = $in{'email'};
33         $type = "!";
34         }
35
36 # Find the existing recipe
37 &foreign_require("procmail", "procmail-lib.pl");
38 @pmrcs = &get_procmailrc();
39 $pmrc = $pmrcs[$#pmrcs];
40 @recipes = &procmail::parse_procmail_file($pmrc);
41 $spamrec = &find_file_recipe(\@recipes);
42
43 &lock_file($pmrc);
44 if ($file) {
45         if ($spamrec) {
46                 # Update the recipe
47                 $spamrec->{'action'} = $file;
48                 $spamrec->{'type'} = $type;
49                 &procmail::modify_recipe($spamrec);
50                 }
51         else {
52                 # Add a new recipe
53                 $spamrec = { 'flags' => [ ],
54                              'conds' => [ [ '', '^X-Spam-Status: Yes' ] ],
55                              'type' => $type,
56                              'action' => $file };
57                 &procmail::create_recipe($spamrec, $pmrc);
58                 }
59         }
60 elsif ($spamrec) {
61         # Remove the recipe, to fall back to default delivery
62         &procmail::delete_recipe($spamrec);
63         }
64 &unlock_file($pmrc);
65
66 if ($module_info{'usermin'} && $file ne "/dev/null") {
67         # Remember spam mail file
68         $userconfig{'spam_file'} = $file;
69         &write_file("$user_module_config_directory/config", \%userconfig);
70         }
71
72 # All done!
73 &webmin_log("procmail");
74 &redirect($redirect_url);
75