Some ui-lib conversion
authorJamie Cameron <jcameron@webmin.com>
Fri, 23 Jan 2009 18:59:09 +0000 (18:59 +0000)
committerJamie Cameron <jcameron@webmin.com>
Fri, 23 Jan 2009 18:59:09 +0000 (18:59 +0000)
spam/edit_report.cgi
spam/edit_score.cgi
spam/spam-lib.pl

index e6c25a4..33b39a5 100755 (executable)
@@ -16,121 +16,112 @@ if (&version_atleast(3.0)) {
        # New version can replace subject, from and to headers
        @rheader = &find("rewrite_header", $conf);
        foreach $h ("subject", "from", "to") {
-               print "<tr> <td><b>",$text{'report_r'.$h},
-                     "</b></td> <td nowrap>";
                ($hn) = grep { lc($_->{'words'}->[0]) eq $h } @rheader;
-               &opt_field("rewrite_header_${h}",
-                          $hn ? join(" ", @{$hn->{'words'}}[1..@{$hn->{'words'}}-1]) : undef, 15);
+               print &ui_table_row($text{'report_r'.$h},
+                       &opt_field("rewrite_header_${h}",
+                                  $hn ? join(" ", @{$hn->{'words'}}[1..@{$hn->{'words'}}-1]) : undef, 15));
                }
        }
 else {
        # Older versions can only replace subject header
-       print "<tr> <td><b>$text{'report_rewrite'}</b></td> <td nowrap>";
        $rewrite = &find("rewrite_subject", $conf);
-       &yes_no_field("rewrite_subject", $rewrite, 1);
-       print "</td> </tr>\n";
+       print &ui_table_row($text{'report_rewrite'},
+               &yes_no_field("rewrite_subject", $rewrite, 1));
 
-       print "<tr> <td><b>$text{'report_subject'}</b></td> <td nowrap>";
        $subject = &find("subject_tag", $conf);
-       &opt_field("subject_tag", $subject, 15, "*****SPAM*****");
-       print "</td> </tr>\n";
+       print &ui_table_row($text{'report_subject'},
+               &opt_field("subject_tag", $subject, 15, "*****SPAM*****"));
        }
 
-print "<tr> <td><b>$text{'report_rheader'}</b></td> <td nowrap>";
+# Include report in headers
 $header = &find("report_header", $conf);
-&yes_no_field("report_header", $header, 0);
-print "</td> </tr>\n";
+print &ui_table_row($text{'report_rheader'},
+       &yes_no_field("report_header", $header, 0));
 
-print "<tr> <td><b>$text{'report_useterse'}</b></td> <td nowrap>";
+# Terse report mode
 $terse = &find("use_terse_report", $conf);
-&yes_no_field("use_terse_report", $terse, 0);
-print "</td> </tr>\n";
+print &ui_table_row($text{'report_useterse'},
+       &yes_no_field("use_terse_report", $terse, 0));
 
-print "<tr> <td><b>$text{'report_fold'}</b></td> <td nowrap>";
+# Split status header?
 $fold = &find("fold_headers", $conf);
-&yes_no_field("fold_headers", $fold, 1);
-print "</td> </tr>\n";
+print &ui_table_row($text{'report_fold'},
+       &yes_no_field("fold_headers", $fold, 1));
 
-print "<tr> <td><b>$text{'report_detail'}</b></td> <td nowrap>";
+# Include details of spam phrases
 $detail = &find("detailed_phrase_score", $conf);
-&yes_no_field("detailed_phrase_score", $detail, 0);
-print "</td> </tr>\n";
+print &ui_table_row($text{'report_detail'},
+       &yes_no_field("detailed_phrase_score", $detail, 0));
 
 if (!&version_atleast(3.0)) {
-       print "<tr> <td><b>$text{'report_stars'}</b></td> <td nowrap>";
+       # Include stars header
        $stars = &find("spam_level_stars", $conf);
-       &yes_no_field("spam_level_stars", $stars, 1);
-       print "</td> </tr>\n";
+       print &ui_table_row($text{'report_stars'},
+               &yes_no_field("spam_level_stars", $stars, 1));
        }
 
-print "<tr> <td><b>$text{'report_char'}</b></td> <td nowrap>";
+# Character for stars
 $char = &find("spam_level_char", $conf);
-&opt_field("spam_level_char", $char, 2, "*");
-print "</td> </tr>\n";
+print &ui_table_row($text{'report_char'},
+       &opt_field("spam_level_char", $char, 2, "*"));
 
-print "<tr> <td><b>$text{'report_defang'}</b></td> <td nowrap>";
+# Remove MIME blocks
 $defang = &find("defang_mime", $conf);
-&yes_no_field("defang_mime", $defang, 1);
-print "</td> </tr>\n";
+print &ui_table_row($text{'report_defang'},
+       &yes_no_field("defang_mime", $defang, 1));
 
 if (&version_atleast(2.6)) {
-       print "<tr> <td><b>$text{'report_safe'}</b></td> <td nowrap>";
        $safe = &find("report_safe", $conf);
-       #&yes_no_field("report_safe", $safe, 0);
-       &option_field("report_safe", $safe, 1,
-                     [ [ 0, $text{'no'} ],
-                       [ 1, $text{'yes'} ],
-                       [ 2, $text{'report_safe2'} ] ]);
-       print "</td> </tr>\n";
+       print &ui_table_row($text{'report_safe'},
+               &option_field("report_safe", $safe, 1,
+                             [ [ 0, $text{'no'} ],
+                               [ 1, $text{'yes'} ],
+                               [ 2, $text{'report_safe2'} ] ]));
        }
 
+print &ui_table_hr();
+
+# Extra report to attach to spam messages
+@report = &find_value("report", $conf);
+$clear = &find("clear_report_template", $conf);
+print &ui_table_row($text{'report_report'},
+       &ui_radio("clear_report", $clear ? 1 : 0,
+                 [ [ 0, $text{'report_noclear'} ],
+                   [ 1, $text{'report_clear'} ] ])."<br>\n".
+       &ui_textarea("report", join("\n", @report), 5, 80));
+
+# Extra report to attach to spam messages, for terse mode
+@report = &find_value("terse_report", $conf);
+$clear = &find("clear_terse_report_template", $conf);
+print &ui_table_row($text{'report_terse'},
+       &ui_radio("clear_terse", $clear ? 1 : 0,
+                 [ [ 0, $text{'report_noclear'} ],
+                   [ 1, $text{'report_clear'} ] ])."<br>\n".
+       &ui_textarea("terse", join("\n", @report), 5, 80));
+
+# Additional headers to add
 if (&version_atleast(3)) {
-       print "<tr> <td colspan=4><hr></td> </tr>\n";
-       print "<tr> <td colspan=2><b>$text{'report_adds'}</b></td> </tr>\n";
-       print "<tr> <td colspan=2>\n";
-       print &ui_columns_start([ $text{'report_addfor'},
+       print &ui_table_hr();
+       $table = &ui_columns_start([ $text{'report_addfor'},
                                  $text{'report_addheader'},
                                  $text{'report_addtext'} ]);
        $i = 0;
        foreach $a (&find_value("add_header", $conf), undef) {
                local ($for, $header, $text) = split(/\s+/, $a, 3);
-               print &ui_columns_row([
+               $table .= &ui_columns_row([
                        &ui_select("addfor_$i", $for,
                                   [ [ "", "&nbsp;" ],
                                     map { [ $_, $text{'report_add'.$_} ] }
                                         ( "spam", "ham", "all" ) ]),
                        &ui_textbox("addheader_$i", $header, 30),
-                       &ui_textbox("addtext_$i", $text, 50)
+                       &ui_textbox("addtext_$i", $text, 30)
                                ]);
                $i++;
                }
-       print &ui_columns_end();
-       print "</td> </tr>\n";
+       $table .= &ui_columns_end();
+       print &ui_table_row($text{'report_adds'}, $table);
        }
 
-print "<tr> <td colspan=4><hr></td> </tr>\n";
-
-@report = &find_value("report", $conf);
-$clear = &find("clear_report_template", $conf);
-print "<tr> <td colspan=2><b>$text{'report_report'}</b></td> </tr>\n";
-printf "<tr> <td colspan=2><input type=radio name=clear_report value=0 %s> %s\n",
-       $clear ? "" : "checked", $text{'report_noclear'};
-printf "<input type=radio name=clear_report value=1 %s> %s<br>\n",
-       $clear ? "checked" : "", $text{'report_clear'};
-print "<textarea name=report rows=5 cols=80>",
-      join("\n", @report),"</textarea></td> </tr>\n";
-
-@report = &find_value("terse_report", $conf);
-$clear = &find("clear_terse_report_template", $conf);
-print "<tr> <td colspan=2><b>$text{'report_terse'}</b></td> </tr>\n";
-printf "<tr> <td colspan=2><input type=radio name=clear_terse value=0 %s> %s\n",
-       $clear ? "" : "checked", $text{'report_noclear'};
-printf "<input type=radio name=clear_terse value=1 %s> %s<br>\n",
-       $clear ? "checked" : "", $text{'report_clear'};
-print "<textarea name=terse rows=5 cols=80>",
-      join("\n", @report),"</textarea></td> </tr>\n";
-
-print "</tr>\n";
 
 &end_form(undef, $text{'save'});
 &ui_print_footer($redirect_url, $text{'index_return'});
index 73508a8..f60726c 100755 (executable)
@@ -12,90 +12,74 @@ $conf = &get_config();
 print "$text{'score_desc'}<p>\n";
 &start_form("save_score.cgi", $text{'score_header'});
 
+# Required score before considering spam
 $hits_param = &version_atleast(3.0) ? "required_score" : "required_hits";
-print "<tr> <td><b>$text{'score_hits'}</b></td> <td nowrap colspan=2>";
 $hits = &find($hits_param, $conf);
-&opt_field($hits_param, $hits, 5, "5");
-print "</td> </tr>\n";
+print &ui_table_row($text{'score_hits'},
+       &opt_field($hits_param, $hits, 5, "5"));
 
-print "<tr> <td><b>$text{'score_auto'}</b></td> <td nowrap colspan=2>";
+# Auto-whitelist factor
 $auto = &find("auto_whitelist_factor", $conf);
-&opt_field("auto_whitelist_factor", $auto, 5, "0.5");
-print "</td> </tr>\n";
+print &ui_table_row($text{'score_auto'},
+       &opt_field("auto_whitelist_factor", $auto, 5, "0.5"));
 
-print "<tr> <td><b>$text{'score_bayes'}</b></td> <td nowrap colspan=2>";
+# Enable bayesian learning
 $bayes = &find("use_bayes", $conf);
-&yes_no_field("use_bayes", $bayes, 1);
-print "</td> </tr>\n";
+print &ui_table_row($text{'score_bayes'},
+       &yes_no_field("use_bayes", $bayes, 1));
 
-print "<tr> <td><b>$text{'score_mx'}</b></td> <td nowrap colspan=2>";
+# MX check tries
 $mx = &find("check_mx_attempts", $conf);
-&opt_field("check_mx_attempts", $mx, 4, "2");
-print "</td> </tr>\n";
+print &ui_table_row($text{'score_mx'},
+       &opt_field("check_mx_attempts", $mx, 4, "2"));
 
-print "<tr> <td><b>$text{'score_mxdelay'}</b></td> <td nowrap colspan=2>";
+# Delay between MX checks
 $mxdelay = &find("check_mx_delay", $conf);
-&opt_field("check_mx_delay", $mxdelay, 4, "2");
-print "</td> </tr>\n";
+print &ui_table_row($text{'score_mxdelay'},
+       &opt_field("check_mx_delay", $mxdelay, 4, "2"));
 
-print "<tr> <td><b>$text{'score_rbl'}</b></td> <td nowrap colspan=2>";
+# Check RBLs?
 $rbl = &find("skip_rbl_checks", $conf);
-&yes_no_field("skip_rbl_checks", $rbl, 0);
-print "</td> </tr>\n";
+print &ui_table_row($text{'score_rbl'},
+       &yes_no_field("skip_rbl_checks", $rbl, 0));
 
-print "<tr> <td><b>$text{'score_timeout'}</b></td> <td nowrap colspan=2>";
+# RBL timeout
 $timeout = &find("rbl_timeout", $conf);
-&opt_field("rbl_timeout", $timeout, 5, "30");
-print "</td> </tr>\n";
+print &ui_table_row($text{'score_timeout'},
+       &opt_field("rbl_timeout", $timeout, 5, "30"));
 
-print "<tr> <td><b>$text{'score_received'}</b></td> <td nowrap colspan=2>";
+# Received headers to check
 $received = &find("num_check_received", $conf);
-&opt_field("num_check_received", $received, 5, 2);
-print "</td> </tr>\n";
+print &ui_table_row($text{'score_received'},
+       &opt_field("num_check_received", $received, 5, 2));
 
-print "<tr> <td colspan=4><hr></td> </tr>\n";
+print &ui_table_hr();
 
+# Acceptable languages
 @langs = &find_value("ok_languages", $conf);
 %langs = map { $_, 1 } split(/\s+/, join(" ", @langs));
 $lmode = !@langs ? 2 : $langs{'all'} ? 1 : 0;
-print "<tr> <td valign=top><b>$text{'score_langs'}</b></td> <td valign=top>\n";
-printf "<input type=radio name=langs_def value=2 %s> %s (%s)<br>\n",
-       $lmode == 2 ? 'checked' : '', $text{'default'}, $text{'score_langsall'};
-printf "<input type=radio name=langs_def value=1 %s> %s<br>\n",
-       $lmode == 1 ? 'checked' : '', $text{'score_langsall'};
-printf "<input type=radio name=langs_def value=0 %s> %s<br>\n",
-       $lmode == 0 ? 'checked' : '', $text{'score_langssel'};
-print "</td> <td><select name=langs multiple size=5>\n";
-open(LANGS, "$module_root_directory/langs");
-while(<LANGS>) {
-       if (/^(\S+)\s+(.*)/) {
-               printf "<option value=%s %s>%s\n",
-                       $1, $langs{$1} ? "selected" : "", $2;
-               }
-       }
-close(LANGS);
-print "</select></td> </tr>\n";
+delete($langs{'all'});
+print &ui_table_row($text{'score_langs'},
+       &ui_radio("langs_def", $lmode,
+                 [ [ 2, $text{'default'}." (".$text{'score_langsall'}.")" ],
+                   [ 1, $text{'score_langsall'} ],
+                   [ 0, $text{'score_langssel'} ] ])."<br>\n".
+       &ui_select("langs", [ keys %langs ],
+                  [ &list_spamassassin_languages() ], 10, 1, 1));
 
+# Acceptable locales
 @locales = &find_value("ok_locales", $conf);
 %locales = map { $_, 1 } split(/\s+/, join(" ", @locales));
 $lmode = !@locales ? 2 : $locales{'all'} ? 1 : 0;
-print "<tr> <td valign=top><b>$text{'score_locales'}</b></td> <td valign=top>\n";
-printf "<input type=radio name=locales_def value=2 %s> %s (%s)<br>\n",
-       $lmode == 2 ? 'checked' : '', $text{'default'},$text{'score_localesall'};
-printf "<input type=radio name=locales_def value=1 %s> %s<br>\n",
-       $lmode == 1 ? 'checked' : '', $text{'score_localesall'};
-printf "<input type=radio name=locales_def value=0 %s> %s<br>\n",
-       $lmode == 0 ? 'checked' : '', $text{'score_localessel'};
-print "</td> <td><select name=locales multiple size=5>\n";
-open(LANGS, "$module_root_directory/locales");
-while(<LANGS>) {
-       if (/^(\S+)\s+(.*)/) {
-               printf "<option value=%s %s>%s\n",
-                       $1, $locales{$1} ? "selected" : "", $2;
-               }
-       }
-close(LANGS);
-print "</select></td> </tr>\n";
+delete($locales{'all'});
+print &ui_table_row($text{'score_locales'},
+       &ui_radio("locales_def", $lmode,
+                 [ [ 2, $text{'default'}." (".$text{'score_localesall'}.")" ],
+                   [ 1, $text{'score_localesall'} ],
+                   [ 0, $text{'score_localessel'} ] ])."<br>\n".
+       &ui_select("locales", [ keys %locales ],
+                  [ &list_spamassassin_locales() ], 5, 1, 1));
 
 &end_form(undef, $text{'save'});
 &ui_print_footer($redirect_url, $text{'index_return'});
index 1ac9e56..b23f427 100644 (file)
@@ -422,31 +422,23 @@ return $_[1] ? join(" ", @_[1..$#_]) : undef;
 # start_form(cgi, header)
 sub start_form
 {
-print "<form action=$_[0] method=post>\n";
-print "<table border width=100%>\n";
-print "<tr $tb> <td><b>$_[1]</b></td> </tr>\n";
-print "<tr $cb> <td><table width=100%>\n";
+local ($cgi, $header) = @_;
+print &ui_form_start($cgi, "post");
+print &ui_table_start($header, "width=100%", 2);
 print $form_hiddens;
 }
 
 # end_form(buttonname, buttonvalue, ...)
 sub end_form
 {
-print "</table></td></tr></table>\n";
-if (@_) {
-       local $p = int(200 / scalar(@_));
-       print "<table width=100%><tr>\n";
-       local $i;
-       for($i=0; $i<@_; $i+=2 ) {
-               local $al = $i == 0 ? "align=left" :
-                           $i == @_-2 ? "align=right" : "align=center";
-               local $n = $_[$i] ? "name='$_[$i]'" : "";
-               local $v = &html_escape($_[$i+1]);
-               print "<td width=$p% $al><input type=submit $n value='$v'></td>\n";
-               }
-       print "</table>\n";
-       }
-print "</form>\n";
+print &ui_table_end();
+local @buts;
+for(my $i=0; $i<@_; $i+=2 ) {
+       local $al = $i == 0 ? "align=left" :
+                   $i == @_-2 ? "align=right" : "align=center";
+       push(@buts, [ $_[$i], $_[$i+1] ]);
+       }
+print &ui_form_end(\@buts);
 }
 
 # yes_no_field(name, value, default)
@@ -454,12 +446,9 @@ sub yes_no_field
 {
 local $v = !$_[1] ? -1 : $_[1]->{'value'};
 local $def = &find_default($_[0], $_[2]) ? $text{'yes'} : $text{'no'};
-printf "<input type=radio name=$_[0] value=1 %s> %s\n",
-       $v == 1 ? "checked" : "", $text{'yes'};
-printf "<input type=radio name=$_[0] value=0 %s> %s\n",
-       $v == 0 ? "checked" : "", $text{'no'};
-printf "<input type=radio name=$_[0] value=-1 %s> %s (%s)\n",
-       $v == -1 ? "checked" : "", $text{'default'}, $def;
+return &ui_radio($_[0], $v,
+                [ [ 1, $text{'yes'} ], [ 0, $text{'no'} ],
+                  [ -1, $text{'default'}." (".$def.")" ] ]);
 }
 
 # parse_yes_no(&config, name)
@@ -475,8 +464,8 @@ sub option_field
 local $v = !$_[1] ? -1 : $_[1]->{'value'};
 local $def = &find_default($_[0], $_[2]);
 local ($defopt) = grep { $_->[0] eq $def } @{$_[3]};
-print &ui_radio($_[0], $v,
-               [ @{$_[3]}, [ -1, "$text{'default'} ($defopt->[1])" ] ]);
+return &ui_radio($_[0], $v,
+                [ @{$_[3]}, [ -1, "$text{'default'} ($defopt->[1])" ] ]);
 }
 
 sub parse_option
@@ -488,12 +477,9 @@ sub parse_option
 sub opt_field
 {
 local $def = &find_default($_[0], $_[3]) if ($_[3]);
-printf "<input type=radio name=$_[0]_def value=1 %s> %s %s\n",
-       $_[1] ? "" : "checked", $text{'default'}, $_[3] ? " ($def)" : "";
-printf "<input type=radio name=$_[0]_def value=0 %s>\n",
-       $_[1] ? "checked" : "";
-printf "<input name=$_[0] size=$_[2] value='%s'>\n",
-       $_[1] ? &html_escape(ref($_[1]) ? $_[1]->{'value'} : $_[1]) : "";
+return &ui_opt_textbox($_[0],
+       !$_[1] ? undef : ref($_[1]) ? $_[1]->{'value'} : $_[1],
+       $_[2], $text{'default'}.($_[3] ? " ($def)" : ""));
 }
 
 # parse_opt(&config, name, [&checkfunc])
@@ -512,11 +498,7 @@ else {
 # edit_textbox(name, &values, width, height)
 sub edit_textbox
 {
-print "<textarea name=$_[0] cols=$_[2] rows=$_[3]>";
-foreach $v (@{$_[1]}) {
-       print "$v\n";
-       }
-print "</textarea>\n";
+return &ui_textarea($_[0], join("\n", @{$_[1]}), $_[3], $_[2]);
 }
 
 # parse_textbox(&config, name)
@@ -1078,5 +1060,35 @@ else {
        }
 }
 
+# list_spamassassin_languages()
+# Returns a list of language codes and descriptions
+sub list_spamassassin_languages
+{
+local @rv;
+open(LANGS, "$module_root_directory/langs");
+while(<LANGS>) {
+       if (/^(\S+)\s+(.*)/) {
+               push(@rv, [ $1, $2 ]);
+               }
+       }
+close(LANGS);
+return @rv;
+}
+
+# list_spamassassin_locales()
+# Returns a list of locale codes and descriptions
+sub list_spamassassin_locales
+{
+local @rv;
+open(LANGS, "$module_root_directory/locales");
+while(<LANGS>) {
+       if (/^(\S+)\s+(.*)/) {
+               push(@rv, [ $1, $2 ]);
+               }
+       }
+close(LANGS);
+return @rv;
+}
+
 1;