Handle hostnames with upper-case letters
[webmin.git] / filter / edit.cgi
1 #!/usr/local/bin/perl
2 # Show details of one filter
3
4 require './filter-lib.pl';
5 &foreign_require("mailbox", "mailbox-lib.pl");
6 &ReadParse();
7
8 # Show page header and get the filter
9 if ($in{'new'}) {
10         &ui_print_header(undef, $text{'edit_title1'}, "");
11         $filter = { 'actiondefault' => 1,
12                     'nobounce' => 1 };
13         if ($in{'header'}) {
14                 # Initial filter is based on URL params
15                 $filter->{'condheader'} = $in{'header'};
16                 $filter->{'condvalue'} = $in{'value'};
17                 }
18         }
19 else {
20         &ui_print_header(undef, $text{'edit_title2'}, "");
21         @filters = &list_filters();
22         ($filter) = grep { $_->{'index'} == $in{'idx'} } @filters;
23         }
24
25 @tds = ( "nowrap width=30%", "width=70%" );
26 print &ui_form_start("save.cgi", "post");
27 print &ui_hidden("new", $in{'new'});
28 print &ui_hidden("idx", $in{'idx'});
29
30 # Start of condition section
31 $cmode = $filter->{'condspam'} ? 5 :
32          $filter->{'condlevel'} ? 6 :
33          $filter->{'condheader'} ? 4 :
34          $filter->{'condtype'} eq '<' ? 3 :
35          $filter->{'condtype'} eq '>' ? 2 :
36          $filter->{'cond'} ? 1 : 0;
37 print &ui_table_start($text{'edit_header1'}, "width=100%", 2);
38
39 # Always do action
40 print &ui_table_row(
41         &ui_oneradio("cmode", 0, $text{'edit_cmode0'}, $cmode == 0),
42         "", undef, \@tds);
43
44 # Is spam
45 print &ui_table_row(
46         &ui_oneradio("cmode", 5, $text{'edit_cmode5'}, $cmode == 5),
47         "", undef, \@tds);
48
49 # Spam level is at or above
50 print &ui_table_row(
51         &ui_oneradio("cmode", 6, $text{'edit_cmode6'}, $cmode == 6),
52         &ui_textbox("condlevel", $filter->{'condlevel'}, 4, 0, undef,
53                     "onFocus='form.cmode[2].checked = true'"), undef, \@tds);
54
55 # Check some header
56 @headers = ( "From", "To", "Subject", "Cc", "Reply-To", "List-Id" );
57 $common = &indexoflc($filter->{'condheader'}, @headers) >= 0;
58 if ($filter->{'condvalue'} =~ /^\.\*(.*)\$$/) {
59         # Ends with
60         $condvalue = $1;
61         $condmode = 2;
62         }
63 elsif ($filter->{'condvalue'} =~ /^\.\*(.*)\.\*$/ ||
64        $filter->{'condvalue'} =~ /^\.\*(.*)$/) {
65         # Contains
66         $condvalue = $1;
67         $condmode = 1;
68         }
69 elsif ($filter->{'condvalue'} =~ /^(.*)\.\*$/ ||
70        $filter->{'condvalue'} =~ /^(.*)$/) {
71         # Starts with
72         $condvalue = $1;
73         $condmode = 0;
74         }
75 if ($condvalue =~ /^[a-zA-Z0-9_ ]*$/) {
76         # Contains no special chars, so not a regexp
77         $regexp = 0;
78         }
79 else {
80         # Has special chars .. but if they are all escaped, then not a regexp
81         $condre = $condvalue;
82         $condre =~ s/\\./x/g;
83         if ($condre =~ /^[a-zA-Z0-9_ ]*$/) {
84                 $condvalue =~ s/\\(.)/$1/g;
85                 $regexp = 0;
86                 }
87         else {
88                 $regexp = 1;
89                 }
90         }
91 print &ui_table_row(
92         &ui_oneradio("cmode", 4, $text{'edit_cmode4'}, $cmode == 4),
93         &text('edit_cheader2',
94               &ui_select("condmenu", $cmode != 4 ? "From" :
95                                      $common ? $filter->{'condheader'} : "",
96                          [ (map { [ $_ ] } @headers),
97                            [ "", $text{'edit_other'} ] ],
98                          1, 0, 0, 0,
99                          "onChange='form.condheader.disabled = (form.condmenu.value!=\"\")'"),
100               &ui_textbox("condheader",
101                           $common ? "" : $filter->{'condheader'}, 20,
102                           $cmode != 4 || $common),
103               &ui_select("condmode", $condmode,
104                          [ [ 0, $text{'edit_modestart'} ],
105                            [ 1, $text{'edit_modecont'} ],
106                            [ 2, $text{'edit_modeend'} ] ]),
107               &ui_textbox("condvalue", $condvalue, 40, 0, undef,
108                           "onFocus='form.cmode[3].checked = true'")." ".
109               &ui_checkbox("condregexp", 1, $text{'edit_regexp'}, $regexp)),
110         undef, \@tds);
111
112 # Smaller
113 print &ui_table_row(
114         &ui_oneradio("cmode", 3, $text{'edit_cmode3'}, $cmode == 3),
115         &ui_bytesbox("condsmall", $cmode == 3 ? $filter->{'cond'} : "",
116                      undef, 0, "onFocus='form.cmode[4].checked = true'"),
117         undef, \@tds);
118
119 # Larger
120 print &ui_table_row(
121         &ui_oneradio("cmode", 2, $text{'edit_cmode2'}, $cmode == 2),
122         &ui_bytesbox("condlarge", $cmode == 2 ? $filter->{'cond'} : "",
123                      undef, 0, "onFocus='form.cmode[5].checked = true'"),
124         undef, \@tds);
125
126 # Matches regexp
127 print &ui_table_row(
128         &ui_oneradio("cmode", 1, $text{'edit_cmode1'}, $cmode == 1),
129         &ui_textbox("cond", $cmode == 1 ? $filter->{'cond'} : "", 70, 0, undef,
130                     "onFocus='form.cmode[6].checked = true'")."<br>".
131         &ui_checkbox("body", 1, $text{'edit_cbody'}, $filter->{'body'}),
132         undef, \@tds);
133
134 print &ui_table_end();
135
136 # Start of action section
137 $amode = $filter->{'actionreply'} ? 6 :
138          $filter->{'actionspam'} ? 5 :
139          $filter->{'actionthrow'} ? 4 :
140          $filter->{'actiondefault'} ? 3 :
141          $filter->{'actionreply'} ? 2 :
142          $filter->{'actiontype'} eq '!' ? 1 : 0;
143 print &ui_table_start($text{'edit_header2'}, "width=100%", 2);
144
145 # Deliver normally
146 print &ui_table_row(
147         &ui_oneradio("amode", 3, $text{'edit_amode3'}, $amode == 3),
148         "", undef, \@tds);
149
150 if ($amode == 5 || &has_spamassassin()) {
151         # Run spamassassin
152         print &ui_table_row(
153                 &ui_oneradio("amode", 5, $text{'edit_amode5'}, $amode == 5),
154                 "", undef, \@tds);
155         }
156
157 # Throw away
158 print &ui_table_row(
159         &ui_oneradio("amode", 4, $text{'edit_amode4'}, $amode == 4),
160         "", undef, \@tds);
161
162 # Forward to some addresses
163 print &ui_table_row(
164         &ui_oneradio("amode", 1, $text{'edit_amode1'}, $amode == 1),
165         &ui_textarea("forward", $amode == 1 ?
166                 join("\n", split(/,/, $filter->{'action'})) : "", 3, 70).
167         "<br>\n".
168         &ui_checkbox("nobounce", 1, $text{'edit_nobounce'},
169                      $filter->{'nobounce'}),
170         undef, \@tds);
171
172 # Save to a folder or file
173 @folders = grep { $_->{'file'} } &mailbox::list_folders_sorted();
174 if ($amode == 0) {
175         $folder = &file_to_folder($filter->{'action'}, \@folders);
176         }
177 else {
178         $folder = $folders[0];
179         }
180 print &ui_table_row(
181         &ui_oneradio("amode", 0, $text{'edit_amode0'}, $amode == 0),
182         &ui_select("folder", $folder ? &mailbox::folder_name($folder) : "",
183                    [ (map { [ &mailbox::folder_name($_), $_->{'name'} ] }
184                           @folders),
185                      [ "", $text{'edit_file'} ] ],
186                    1, 0, 0, 0,
187                    "onChange='form.file.disabled = (form.folder.value!=\"\")'").
188                    "\n".
189         &ui_textbox("file", $folder ? "" : $filter->{'action'}, 50,
190                     $folder ? 1 : 0),
191         undef, \@tds);
192
193 # Save to a new folder
194 print &ui_table_row(
195         &ui_oneradio("amode", 7, $text{'edit_amode7'}, 0),
196         &ui_textbox("newfolder", undef, 20),
197         undef, \@tds);
198
199 # Send autoreply
200 if ($amode == 6) {
201         $r = $filter->{'reply'};
202         $period = $in{'new'} ? 60 :
203                   $r->{'replies'} && $r->{'period'} ? int($r->{'period'}/60) :
204                   $r->{'replies'} ? 60 : undef;
205         if ($r->{'autoreply_start'}) {
206                 @stm = localtime($r->{'autoreply_start'});
207                 $stm[4]++; $stm[5] += 1900;
208                 }
209         if ($r->{'autoreply_end'}) {
210                 @etm = localtime($r->{'autoreply_end'});
211                 $etm[4]++; $etm[5] += 1900;
212                 }
213         }
214 else {
215         $period = 60;
216         }
217 if ($config{'reply_force'}) {
218         $replyblock = "";
219         }
220 elsif ($config{'reply_min'}) {
221         $replyblock = "<tr> <td><b>$text{'index_period'}</b></td> ".
222                       "<td>".&ui_textbox("period", $period, 3)." ".
223                              $text{'index_mins'}."</td> </tr>\n";
224         }
225 else {
226         $replyblock = "<tr> <td><b>$text{'index_period'}</b></td> ".
227                       "<td>".&ui_opt_textbox("period", $period, 3,
228                           $text{'index_noperiod'})." ".$text{'index_mins'}.
229                       "</td> </tr>\n";
230         }
231 $cs = !$in{'new'} ? $r->{'charset'} :
232       &get_charset() eq $default_charset ? undef : &get_charset();
233 print &ui_table_row(
234         &ui_oneradio("amode", 6, $text{'edit_amode6'}, $amode == 6),
235         &ui_textarea("reply", $filter->{'reply'}->{'autotext'}, 5, 60)."<br>".
236         "<table>\n".
237         $replyblock.
238         "<tr> <td><b>$text{'index_astart'}</b></td> ".
239         "<td>".&ui_date_input($stm[3], $stm[4], $stm[5],
240                                "dstart", "mstart", "ystart")." ".
241             &date_chooser_button("dstart", "mstart", "ystart")."</td> </tr>\n".
242         "<tr> <td><b>$text{'index_aend'}</b></td> ".
243         "<td>".&ui_date_input($etm[3], $etm[4], $etm[5],
244                                "dend", "mend", "yend")." ".
245             &date_chooser_button("dend", "mend", "yend")."</td> </tr>\n".
246         "<tr> <td><b>$text{'index_charset'}</b></td> ".
247         "<td>".&ui_opt_textbox("charset", $cs, 20,
248                        $text{'default'}." ($default_charset)")."</td> </tr>\n".
249         "</table>",
250         undef, \@tds);
251
252 # Continue checkbox
253 print &ui_table_row(
254         undef, &ui_checkbox("continue", 1, $text{'edit_continue'},
255                             $filter->{'continue'}), 2);
256
257 print &ui_table_end();
258
259 # End of the form, with buttons
260 if ($in{'new'}) {
261         print &ui_form_end([ [ "create", $text{'create'} ] ]);
262         }
263 else {
264         print &ui_form_end([ [ "save", $text{'save'} ],
265                              [ "delete", $text{'delete'} ] ]);
266         }
267
268 # Show page footer
269 &ui_print_footer("", $text{'index_return'});
270