Handle hostnames with upper-case letters
[webmin.git] / procmail / save_recipe.cgi
1 #!/usr/local/bin/perl
2 # save_recipe.cgi
3 # Create, update or delete a procmail recipe
4
5 require './procmail-lib.pl';
6 &ReadParse();
7 &lock_file($procmailrc);
8 @conf = &get_procmailrc();
9 $rec = $conf[$in{'idx'}] if (!$in{'new'});
10
11 if ($in{'delete'}) {
12         # Just delete the recipe
13         &delete_recipe($rec);
14         }
15 else {
16         # Validate inputs
17         &error_setup($text{'save_err'});
18         if ($in{'block'}) {
19                 # Conditional code block
20                 $in{'bdata'} =~ s/\r//g;
21                 $rec->{'block'} = $in{'bdata'};
22                 }
23         else {
24                 # Normal action
25                 $in{'action'} =~ /\S/ ||
26                         &error($text{'save_eaction_'.$in{'amode'}});
27                 delete($rec->{'type'});
28                 if ($in{'amode'} == 0) {
29                         $rec->{'action'} = $in{'action'};
30                         }
31                 elsif ($in{'amode'} == 1) {
32                         $rec->{'action'} = $in{'action'}."/.";
33                         }
34                 elsif ($in{'amode'} == 2) {
35                         $rec->{'action'} = $in{'action'}."/";
36                         }
37                 elsif ($in{'amode'} == 3) {
38                         $rec->{'type'} = "!";
39                         $rec->{'action'} = $in{'action'};
40                         }
41                 elsif ($in{'amode'} == 6) {
42                         $rec->{'type'} = "=";
43                         $in{'action'} =~ /^(\S+)=(.*)$/ ||
44                                 &error($text{'save_eactionvar'});
45                         $rec->{'action'} = $in{'action'};
46                         }
47                 else {
48                         $rec->{'type'} = "|";
49                         $rec->{'action'} = $in{'action'};
50                         }
51                 }
52
53         map { $flag{$_}++ } split(/\0/, $in{'flag'});
54         @flags = @{$rec->{'flags'}};
55         foreach $f (@known_flags) {
56                 if ($flag{$f}) {
57                         push(@flags, $f);
58                         }
59                 else {
60                         @flags = grep { $_ ne $f } @flags;
61                         }
62                 }
63         $rec->{'flags'} = [ &unique(@flags) ];
64
65         if ($in{'lockfile_def'} == 1) {
66                 delete($rec->{'lockfile'});
67                 }
68         elsif ($in{'lockfile_def'} == 2) {
69                 $rec->{'lockfile'} = "";
70                 }
71         else {
72                 $in{'lockfile'} =~ /\S/ || &error($text{'save_elockfile'});
73                 $rec->{'lockfile'} = $in{'lockfile'};
74                 }
75
76         for($i=0; defined($m = $in{"cmode_$i"}); $i++) {
77                 next if ($m eq '-');
78                 $c = $in{"cond_$i"};
79                 if ($m eq '<' || $m eq '>') {
80                         $c =~ /^\d+$/ || &error(&text('save_esize', $i+1));
81                         }
82                 elsif ($m eq '$' || $m eq '?') {
83                         $c =~ /\S/ || &error(&text('save_eshell', $i+1));
84                         }
85                 else {
86                         $c =~ /\S/ || &error(&text('save_ere', $i+1));
87                         }
88                 push(@conds, [ $m, $c ]);
89                 }
90         $rec->{'conds'} = \@conds;
91
92         # Save the receipe
93         if ($in{'new'}) {
94                 if ($in{'before'} ne '') {
95                         $before = $conf[$in{'before'}];
96                         &create_recipe_before($rec, $before);
97                         }
98                 elsif ($in{'after'} ne '') {
99                         if ($in{'after'} == @conf-1) {
100                                 &create_recipe($rec);
101                                 }
102                         else {
103                                 $before = $conf[$in{'after'}+1];
104                                 &create_recipe_before($rec, $before);
105                                 }
106                         }
107                 else {
108                         &create_recipe($rec);
109                         }
110                 }
111         else {
112                 &modify_recipe($rec);
113                 }
114         }
115 &unlock_file($procmailrc);
116 &webmin_log($in{'delete'} ? "delete" : $in{'new'} ? "create" : "modify",
117             "recipe", undef, $rec);
118 &redirect("");
119