sieve.c

00001 /* 
00002  * $Id: sieve.c 5147 2007-05-08 15:36:22Z ajc $
00003  */
00009 #include "webcit.h"
00010 
00011 #define MAX_SCRIPTS     100
00012 #define MAX_RULES       25
00013 #define RULES_SCRIPT    "__WebCit_Generated_Script__"
00014 
00018 void display_sieve(void)
00019 {
00020         char script_names[MAX_SCRIPTS][64];
00021         int num_scripts = 0;
00022         int active_script = (-1);
00023         char buf[256];
00024         int i;
00025         int rules_script_is_active = 0;
00026         
00027 
00028         memset(script_names, 0, sizeof script_names);
00029 
00030         serv_puts("MSIV listscripts");
00031         serv_getln(buf, sizeof(buf));
00032         if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
00033                 if (num_scripts < MAX_SCRIPTS) {
00034                         extract_token(script_names[num_scripts], buf, 0, '|', 64);
00035                         if (extract_int(buf, 1) > 0) {
00036                                 active_script = num_scripts;
00037                                 if (!strcasecmp(script_names[num_scripts], RULES_SCRIPT)) {
00038                                         rules_script_is_active = 1;
00039                                 }
00040                         }
00041                         ++num_scripts;
00042                 }
00043         }
00044 
00045         output_headers(1, 1, 2, 0, 0, 0);
00046 
00047         wprintf("<script type=\"text/javascript\">                                      \n"
00048                 "                                                                       \n"
00049                 "var previously_active_script;                                          \n"
00050                 "                                                                       \n"
00051                 "function ToggleSievePanels() {                                         \n"
00052                 " d = ($('sieveform').bigaction.options[$('sieveform').bigaction.selectedIndex].value); \n"
00053                 " for (i=0; i<3; ++i) {                                                 \n"
00054                 "  if (i == d) {                                                        \n"
00055                 "   $('sievediv' + i).style.display = 'block';                          \n"
00056                 "  }                                                                    \n"
00057                 "  else {                                                               \n"
00058                 "   $('sievediv' + i).style.display = 'none';                           \n"
00059                 "  }                                                                    \n"
00060                 " }                                                                     \n"
00061                 "}                                                                      \n"
00062                 "                                                                       \n"
00063                 "function ToggleScriptPanels() {                                        \n"
00064                 " d = ($('sieveform').active_script.options[$('sieveform').active_script.selectedIndex].value); \n"
00065                 " if ($('script_' + previously_active_script)) {                        \n"
00066                 "  $('script_' + previously_active_script).style.display = 'none';      \n"
00067                 " }                                                                     \n"
00068                 " $('script_' + d).style.display = 'block';                             \n"
00069                 " previously_active_script = d;                                         \n"
00070                 "}                                                                      \n"
00071                 "                                                                       \n"
00072                 "</script>                                                              \n"
00073         );
00074 
00075         wprintf("<div id=\"banner\">\n");
00076         wprintf("<TABLE class=\"sieve_banner\"><TR><TD>");
00077         wprintf("<SPAN CLASS=\"titlebar\">"
00078                 "<img src=\"static/advanpage2_48x.gif\">");
00079         wprintf(_("View/edit server-side mail filters"));
00080         wprintf("</SPAN>\n");
00081         wprintf("</TD></TR></TABLE>\n");
00082         wprintf("</div>\n<div id=\"content\">\n");
00083 
00084         wprintf("<div class=\"fix_scrollbar_bug\">"
00085                 "<table class=\"sieve_background\">"
00086                 "<tr><td valign=top>\n");
00087 
00088 
00089         wprintf("<form id=\"sieveform\" method=\"post\" action=\"save_sieve\">\n");
00090 
00091         wprintf(_("When new mail arrives: "));
00092         wprintf("<select name=\"bigaction\" size=1 onChange=\"ToggleSievePanels();\">\n");
00093 
00094         wprintf("<option %s value=\"0\">", ((active_script < 0) ? "selected" : ""));
00095         wprintf(_("Leave it in my inbox without filtering"));
00096         wprintf("</option>\n");
00097 
00098         wprintf("<option %s value=\"1\">", ((rules_script_is_active) ? "selected" : ""));
00099         wprintf(_("Filter it according to rules selected below"));
00100         wprintf("</option>\n");
00101 
00102         wprintf("<option %s value=\"2\">",
00103                         (((active_script >= 0) && (!rules_script_is_active)) ? "selected" : ""));
00104         wprintf(_("Filter it through a manually edited script (advanced users only)"));
00105         wprintf("</option>\n");
00106 
00107         wprintf("</select>");
00108 
00109 
00110 
00111         /* The "no filtering" div */
00112 
00113         wprintf("<div id=\"sievediv0\" style=\"display:none\">\n");
00114         wprintf("<div align=\"center\"><br /><br />");
00115         wprintf(_("Your incoming mail will not be filtered through any scripts."));
00116         wprintf("<br /><br /></div>\n");
00117         wprintf("</div>\n");
00118 
00119         /* The "webcit managed scripts" div */
00120 
00121         wprintf("<div id=\"sievediv1\" style=\"display:none\">\n");
00122         display_rules_editor_inner_div();
00123         wprintf("</div>\n");
00124 
00125         /* The "I'm smart and can write my own Sieve scripts" div */
00126 
00127         wprintf("<div id=\"sievediv2\" style=\"display:none\">\n");
00128 
00129         if (num_scripts > 0) {
00130                 wprintf(_("The currently active script is: "));
00131                 wprintf("<select name=\"active_script\" size=1 onChange=\"ToggleScriptPanels();\">\n");
00132                 for (i=0; i<num_scripts; ++i) {
00133                         if (strcasecmp(script_names[i], RULES_SCRIPT)) {
00134                                 wprintf("<option %s value=\"%s\">%s</option>\n",
00135                                         ((active_script == i) ? "selected" : ""),
00136                                         script_names[i],
00137                                         script_names[i]
00138                                 );
00139                         }
00140                 }
00141                 wprintf("</select>\n");
00142         }
00143 
00144         wprintf("&nbsp;&nbsp;&nbsp;");
00145         wprintf("<a href=\"display_add_remove_scripts\">%s</a>\n", _("Add or delete scripts"));
00146 
00147         wprintf("<br />\n");
00148 
00149         if (num_scripts > 0) {
00150                 for (i=0; i<num_scripts; ++i) {
00151                         if (strcasecmp(script_names[i], RULES_SCRIPT)) {
00152                                 wprintf("<div id=\"script_%s\" style=\"display:none\">\n", script_names[i]);
00153                                 wprintf("<textarea name=\"text_%s\" wrap=soft rows=20 cols=80 width=80>\n",
00154                                         script_names[i]);
00155                                 serv_printf("MSIV getscript|%s", script_names[i]);
00156                                 serv_getln(buf, sizeof buf);
00157                                 if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
00158                                         wprintf("%s\n", buf);
00159                                 }
00160                                 wprintf("</textarea>\n");
00161                                 wprintf("</div>\n");
00162                         }
00163                 }
00164         }
00165 
00166         wprintf("<script type=\"text/javascript\">      \n"
00167                 "ToggleScriptPanels();                  \n"
00168                 "</script>                              \n"
00169         );
00170 
00171         wprintf("</div>\n");
00172 
00173 
00174         /* The rest of this is common for all panels... */
00175 
00176         wprintf("<div align=\"center\"><br>");
00177         wprintf("<input type=\"submit\" name=\"save_button\" value=\"%s\">", _("Save changes"));
00178         wprintf("&nbsp;");
00179         wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">\n", _("Cancel"));
00180         wprintf("</div></form>\n");
00181 
00182         wprintf("</td></tr></table></div>\n");
00183 
00184         wprintf("<script type=\"text/javascript\">      \n"
00185                 "ToggleSievePanels();                   \n"
00186                 "</script>                              \n"
00187         );
00188 
00189         wDumpContent(1);
00190 
00191 }
00192 
00193 
00194 
00198 void osr_sanitize(char *str) {
00199         int i;
00200 
00201         if (str == NULL) return;
00202         for (i=0; i<strlen(str); ++i) {
00203                 if (str[i]=='\"') {
00204                         str[i] = '\'' ;
00205                 }
00206                 else if (isspace(str[i])) {
00207                         str[i] = ' ';
00208                 }
00209         }
00210 }
00211 
00212 
00216 void output_sieve_rule(char *hfield, char *compare, char *htext, char *sizecomp, int sizeval,
00217                         char *action, char *fileinto, char *redirect, char *automsg, char *final)
00218 {
00219         char *comp1 = "";
00220         char *comp2 = "";
00221 
00222         osr_sanitize(htext);
00223         osr_sanitize(fileinto);
00224         osr_sanitize(redirect);
00225         osr_sanitize(automsg);
00226 
00227         /* Prepare negation and match operators that will be used iff we apply a conditional */
00228 
00229         if (!strcasecmp(compare, "contains")) {
00230                 comp1 = "";
00231                 comp2 = ":contains";
00232         }
00233         else if (!strcasecmp(compare, "notcontains")) {
00234                 comp1 = "not";
00235                 comp2 = ":contains";
00236         }
00237         else if (!strcasecmp(compare, "is")) {
00238                 comp1 = "";
00239                 comp2 = ":is";
00240         }
00241         else if (!strcasecmp(compare, "isnot")) {
00242                 comp1 = "not";
00243                 comp2 = ":is";
00244         }
00245         else if (!strcasecmp(compare, "matches")) {
00246                 comp1 = "";
00247                 comp2 = ":matches";
00248         }
00249         else if (!strcasecmp(compare, "notmatches")) {
00250                 comp1 = "not";
00251                 comp2 = ":matches";
00252         }
00253 
00254         /* Now do the conditional */
00255 
00256         if (!strcasecmp(hfield, "from")) {
00257                 serv_printf("if%s header %s \"From\" \"%s\"",
00258                         comp1, comp2,
00259                         htext
00260                 );
00261         }
00262 
00263         else if (!strcasecmp(hfield, "tocc")) {
00264                 serv_printf("if%s header %s [\"To\", \"Cc\"] \"%s\"",
00265                         comp1, comp2,
00266                         htext
00267                 );
00268         }
00269 
00270         else if (!strcasecmp(hfield, "subject")) {
00271                 serv_printf("if%s header %s \"Subject\" \"%s\"",
00272                         comp1, comp2,
00273                         htext
00274                 );
00275         }
00276 
00277         else if (!strcasecmp(hfield, "replyto")) {
00278                 serv_printf("if%s header %s \"Reply-to\" \"%s\"",
00279                         comp1, comp2,
00280                         htext
00281                 );
00282         }
00283 
00284         else if (!strcasecmp(hfield, "sender")) {
00285                 serv_printf("if%s header %s \"Sender\" \"%s\"",
00286                         comp1, comp2,
00287                         htext
00288                 );
00289         }
00290 
00291         else if (!strcasecmp(hfield, "resentfrom")) {
00292                 serv_printf("if%s header %s \"Resent-from\" \"%s\"",
00293                         comp1, comp2,
00294                         htext
00295                 );
00296         }
00297 
00298         else if (!strcasecmp(hfield, "resentto")) {
00299                 serv_printf("if%s header %s \"Resent-to\" \"%s\"",
00300                         comp1, comp2,
00301                         htext
00302                 );
00303         }
00304 
00305         else if (!strcasecmp(hfield, "xmailer")) {
00306                 serv_printf("if%s header %s \"X-Mailer\" \"%s\"",
00307                         comp1, comp2,
00308                         htext
00309                 );
00310         }
00311 
00312         else if (!strcasecmp(hfield, "xspamflag")) {
00313                 serv_printf("if%s header %s \"X-Spam-Flag\" \"%s\"",
00314                         comp1, comp2,
00315                         htext
00316                 );
00317         }
00318 
00319         else if (!strcasecmp(hfield, "xspamstatus")) {
00320                 serv_printf("if%s header %s \"X-Spam-Status\" \"%s\"",
00321                         comp1, comp2,
00322                         htext
00323                 );
00324         }
00325 
00326         else if (!strcasecmp(hfield, "envfrom")) {
00327                 serv_printf("if%s envelope %s \"From\" \"%s\"",
00328                         comp1, comp2,
00329                         htext
00330                 );
00331         }
00332 
00333         else if (!strcasecmp(hfield, "envto")) {
00334                 serv_printf("if%s envelope %s \"To\" \"%s\"",
00335                         comp1, comp2,
00336                         htext
00337                 );
00338         }
00339 
00340         else if (!strcasecmp(hfield, "size")) {
00341                 if (!strcasecmp(sizecomp, "larger")) {
00342                         serv_printf("if size :over %d", sizeval);
00343                 }
00344                 else if (!strcasecmp(sizecomp, "smaller")) {
00345                         serv_printf("if size :under %d", sizeval);
00346                 }
00347                 else {  /* failsafe - should never get here, but just in case... */
00348                         serv_printf("if size :over 1");
00349                 }
00350         }
00351 
00352         /* Open braces if we're in a conditional loop */
00353 
00354         if (strcasecmp(hfield, "all")) {
00355                 serv_printf("{");
00356         }
00357 
00358 
00359         /* Do action */
00360 
00361         if (!strcasecmp(action, "keep")) {
00362                 serv_printf("keep;");
00363         }
00364 
00365         else if (!strcasecmp(action, "discard")) {
00366                 serv_printf("discard;");
00367         }
00368 
00369         else if (!strcasecmp(action, "reject")) {
00370                 serv_printf("reject \"%s\";", automsg);
00371         }
00372 
00373         else if (!strcasecmp(action, "fileinto")) {
00374                 serv_printf("fileinto \"%s\";", fileinto);
00375         }
00376 
00377         else if (!strcasecmp(action, "redirect")) {
00378                 serv_printf("redirect \"%s\";", redirect);
00379         }
00380 
00381         else if (!strcasecmp(action, "vacation")) {
00382                 serv_printf("vacation \"%s\";", automsg);
00383         }
00384 
00385 
00386         /* Do 'final' action */
00387 
00388         if (!strcasecmp(final, "stop")) {
00389                 serv_printf("stop;");
00390         }
00391 
00392 
00393         /* Close the braces if we're in a conditional loop */
00394 
00395         if (strcasecmp(hfield, "all")) {
00396                 serv_printf("}");
00397         }
00398 
00399 
00400         /* End of rule. */
00401 }
00402 
00403 
00404 
00408 void parse_fields_from_rule_editor(void) {
00409 
00410         int active;
00411         char hfield[256];
00412         char compare[32];
00413         char htext[256];
00414         char sizecomp[32];
00415         int sizeval;
00416         char action[32];
00417         char fileinto[128];
00418         char redirect[256];
00419         char automsg[1024];
00420         char final[32];
00421 
00422         int i;
00423         char buf[256];
00424         char fname[256];
00425         char rule[2048];
00426         char encoded_rule[4096];
00427 
00428         serv_printf("MSIV putscript|%s|", RULES_SCRIPT);
00429         serv_getln(buf, sizeof buf);
00430         if (buf[0] != '4') {
00431                 return;
00432         }
00433 
00434         serv_puts("# THIS SCRIPT WAS AUTOMATICALLY GENERATED BY WEBCIT.");
00435         serv_puts("# ");
00436         serv_puts("# Do not attempt to manually edit it.  If you do so,");
00437         serv_puts("# your changes will be overwritten the next time WebCit");
00438         serv_puts("# saves its mail filtering rule set.  If you really want");
00439         serv_puts("# to use these rules as the basis for another script,");
00440         serv_puts("# copy them to another script and save that instead.");
00441         serv_puts("");
00442         serv_puts("require \"fileinto\";");
00443         serv_puts("require \"reject\";");
00444         serv_puts("require \"vacation\";");
00445         serv_puts("require \"envelope\";");
00446         serv_puts("");
00447 
00448         for (i=0; i<MAX_RULES; ++i) {
00449                 
00450                 strcpy(rule, "");
00451 
00452                 sprintf(fname, "active%d", i);
00453                 active = !strcasecmp(bstr(fname), "on") ;
00454 
00455                 if (active) {
00456 
00457                         sprintf(fname, "hfield%d", i);
00458                         safestrncpy(hfield, bstr(fname), sizeof hfield);
00459         
00460                         sprintf(fname, "compare%d", i);
00461                         safestrncpy(compare, bstr(fname), sizeof compare);
00462         
00463                         sprintf(fname, "htext%d", i);
00464                         safestrncpy(htext, bstr(fname), sizeof htext);
00465         
00466                         sprintf(fname, "sizecomp%d", i);
00467                         safestrncpy(sizecomp, bstr(fname), sizeof sizecomp);
00468         
00469                         sprintf(fname, "sizeval%d", i);
00470                         sizeval = atoi(bstr(fname));
00471         
00472                         sprintf(fname, "action%d", i);
00473                         safestrncpy(action, bstr(fname), sizeof action);
00474         
00475                         sprintf(fname, "fileinto%d", i);
00476                         safestrncpy(fileinto, bstr(fname), sizeof fileinto);
00477         
00478                         sprintf(fname, "redirect%d", i);
00479                         safestrncpy(redirect, bstr(fname), sizeof redirect);
00480         
00481                         sprintf(fname, "automsg%d", i);
00482                         safestrncpy(automsg, bstr(fname), sizeof automsg);
00483         
00484                         sprintf(fname, "final%d", i);
00485                         safestrncpy(final, bstr(fname), sizeof final);
00486         
00487                         snprintf(rule, sizeof rule, "%d|%s|%s|%s|%s|%d|%s|%s|%s|%s|%s",
00488                                 active, hfield, compare, htext, sizecomp, sizeval, action, fileinto,
00489                                 redirect, automsg, final
00490                         );
00491         
00492                         CtdlEncodeBase64(encoded_rule, rule, strlen(rule)+1, 0);
00493                         serv_printf("# WEBCIT_RULE|%d|%s|", i, encoded_rule);
00494                         output_sieve_rule(hfield, compare, htext, sizecomp, sizeval,
00495                                         action, fileinto, redirect, automsg, final);
00496                         serv_printf("");
00497                 }
00498 
00499 
00500         }
00501 
00502         serv_puts("stop;");
00503         serv_puts("000");
00504 
00505 }
00506 
00507 
00508 
00512 void save_sieve(void) {
00513         int bigaction;
00514         char script_names[MAX_SCRIPTS][64];
00515         int num_scripts = 0;
00516         int active_script = (-1);
00517         int i;
00518         char this_name[64];
00519         char buf[256];
00520 
00521         if (strlen(bstr("save_button")) == 0) {
00522                 strcpy(WC->ImportantMessage,
00523                         _("Cancelled.  Changes were not saved."));
00524                 display_main_menu();
00525                 return;
00526         }
00527 
00528         parse_fields_from_rule_editor();
00529 
00530         serv_puts("MSIV listscripts");
00531         serv_getln(buf, sizeof(buf));
00532         if (buf[0] == '1') while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
00533                 if (num_scripts < MAX_SCRIPTS) {
00534                         extract_token(script_names[num_scripts], buf, 0, '|', 64);
00535                         if (extract_int(buf, 1) > 0) {
00536                                 active_script = num_scripts;
00537                         }
00538                         ++num_scripts;
00539                 }
00540         }
00541 
00542         bigaction = atoi(bstr("bigaction"));
00543 
00544         if (bigaction == 0) {
00545                 serv_puts("MSIV setactive||");
00546                 serv_getln(buf, sizeof buf);
00547         }
00548 
00549         else if (bigaction == 1) {
00550                 serv_printf("MSIV setactive|%s|", RULES_SCRIPT);
00551                 serv_getln(buf, sizeof buf);
00552         }
00553 
00554         else if (bigaction == 2) {
00555                 serv_printf("MSIV setactive|%s|", bstr("active_script"));
00556                 serv_getln(buf, sizeof buf);
00557         }
00558 
00559         if (num_scripts > 0) {
00560                 for (i=0; i<num_scripts; ++i) {
00561                         /*
00562                          * We only want to save the scripts from the "manually edited scripts"
00563                          * screen.  The script that WebCit generates from its ruleset will be
00564                          * auto-generated by parse_fields_from_rule_editor() and saved there.
00565                          */
00566                         if (strcasecmp(script_names[i], RULES_SCRIPT)) {
00567                                 serv_printf("MSIV putscript|%s|", script_names[i]);
00568                                 serv_getln(buf, sizeof buf);
00569                                 if (buf[0] == '4') {
00570                                         snprintf(this_name, sizeof this_name, "text_%s", script_names[i]);
00571                                         striplt(bstr(this_name));
00572                                         serv_printf("%s", bstr(this_name));
00573                                         serv_puts("000");
00574                                 }
00575                         }
00576                 }
00577         }
00578 
00579         strcpy(WC->ImportantMessage, _("Your changes have been saved."));
00580         display_main_menu();
00581         return;
00582 }
00583 
00584 
00588 void display_add_remove_scripts(char *message)
00589 {
00590         char buf[256];
00591         char script_name[256];
00592 
00593         output_headers(1, 1, 2, 0, 0, 0);
00594         wprintf("<div id=\"banner\">\n");
00595         wprintf("<table class=\"sieve_banner\"><tr>"
00596                 "<td>"
00597                 "<span class=\"titlebar\">"
00598                 "<img src=\"static/advanpage2_48x.gif\">");
00599         wprintf(_("Add or delete scripts"));
00600         wprintf("</span></td></tr></table>\n"
00601                 "</div>\n<div id=\"content\">\n"
00602         );
00603 
00604         if (message != NULL) wprintf(message);
00605 
00606         wprintf("<table border=0 cellspacing=10><tr valign=top><td>\n");
00607 
00608         svprintf("BOXTITLE", WCS_STRING, _("Add a new script"));
00609         do_template("beginbox");
00610 
00611         wprintf(_("To create a new script, enter the desired "
00612                 "script name in the box below and click 'Create'."));
00613         wprintf("<br /><br />");
00614 
00615         wprintf("<center><form method=\"POST\" action=\"create_script\">\n");
00616         wprintf(_("Script name: "));
00617         wprintf("<input type=\"text\" name=\"script_name\"><br />\n"
00618                 "<input type=\"submit\" name=\"create_button\" value=\"%s\">"
00619                 "</form></center>\n", _("Create"));
00620 
00621         do_template("endbox");
00622 
00623         svprintf("BOXTITLE", WCS_STRING, _("Edit scripts"));
00624         do_template("beginbox");
00625         wprintf("<br /><div align=center><a href=\"display_sieve\">%s</a><br /><br />\n",
00626                 _("Return to the script editing screen")
00627         );
00628         do_template("endbox");
00629 
00630         wprintf("</td><td>");
00631 
00632         svprintf("BOXTITLE", WCS_STRING, _("Delete scripts"));
00633         do_template("beginbox");
00634 
00635         wprintf(_("To delete an existing script, select the script "
00636                 "name from the list and click 'Delete'."));
00637         wprintf("<br /><br />");
00638         
00639         wprintf("<center>"
00640                 "<form method=\"POST\" action=\"delete_script\">\n");
00641         wprintf("<select name=\"script_name\" size=10 style=\"width:100%%\">\n");
00642 
00643         serv_puts("MSIV listscripts");
00644         serv_getln(buf, sizeof buf);
00645         if (buf[0] == '1') {
00646                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00647                         extract_token(script_name, buf, 0, '|', sizeof script_name);
00648                         if ( (extract_int(buf, 1) == 0) && (strcasecmp(script_name, RULES_SCRIPT)) ) {
00649                                 wprintf("<option>");
00650                                 escputs(script_name);
00651                                 wprintf("</option>\n");
00652                         }
00653                 }
00654         }
00655         wprintf("</select><br />\n");
00656 
00657         wprintf("<input type=\"submit\" name=\"delete_button\" value=\"%s\" "
00658                 "onClick=\"return confirm('%s');\">", _("Delete script"), _("Delete this script?"));
00659         wprintf("</form></center>\n");
00660         do_template("endbox");
00661 
00662         wprintf("</td></tr></table>\n");
00663 
00664         wDumpContent(1);
00665 }
00666 
00667 
00668 
00672 void delete_script(void) {
00673         char buf[256];
00674 
00675         serv_printf("MSIV deletescript|%s", bstr("script_name"));
00676         serv_getln(buf, sizeof buf);
00677         display_add_remove_scripts(&buf[4]);
00678 }
00679                 
00680 
00681 
00686 void create_script(void) {
00687         char buf[256];
00688 
00689         serv_printf("MSIV getscript|%s", bstr("script_name"));
00690         serv_getln(buf, sizeof buf);
00691         if (buf[0] == '1') {
00692                 while (serv_getln(buf, sizeof(buf)), strcmp(buf, "000")) {
00693                         /* flush */
00694                 }
00695                 display_add_remove_scripts(_("A script by that name already exists."));
00696                 return;
00697         }
00698         
00699         serv_printf("MSIV putscript|%s", bstr("script_name"));
00700         serv_getln(buf, sizeof buf);
00701         if (buf[0] == '4') {
00702                 serv_puts("keep;");
00703                 serv_puts("000");
00704                 display_add_remove_scripts(_("A new script has been created.  Return to the script editing screen to edit and activate it."));
00705                 return;
00706         }
00707 
00708         display_add_remove_scripts(&buf[4]);
00709 }
00710 
00711 
00712 
00713 
00714 void display_rules_editor_inner_div(void) {
00715         int i, j;
00716         char buf[4096];
00717         char rules[MAX_RULES][2048];
00718 
00719         struct {
00720                 char name[128];
00721         } *rooms = NULL;
00722         int num_roomnames = 0;
00723         int num_roomnames_alloc = 0;
00724 
00725         int active;
00726         char hfield[256];
00727         char compare[32];
00728         char htext[256];
00729         char sizecomp[32];
00730         int sizeval;
00731         char action[32];
00732         char fileinto[128];
00733         char redirect[256];
00734         char automsg[1024];
00735         char final[32];
00736 
00737         /* load the rules */
00738         memset(rules, 0, sizeof rules);
00739         serv_printf("MSIV getscript|%s", RULES_SCRIPT);
00740         serv_getln(buf, sizeof buf);
00741         if (buf[0] == '1') while(serv_getln(buf, sizeof (buf)), strcmp(buf, "000")) {
00742                 if (!strncasecmp(buf, "# WEBCIT_RULE|", 14)) {
00743                         j = extract_int(buf, 1);
00744                         remove_token(buf, 0, '|');
00745                         remove_token(buf, 0, '|');
00746                         CtdlDecodeBase64(rules[j], buf, strlen(buf));
00747                 }
00748         }
00749 
00750         /* load the roomnames */
00751         serv_puts("LKRA");
00752         serv_getln(buf, sizeof buf);
00753         if (buf[0] == '1') {
00754                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
00755                         ++num_roomnames;
00756                         if (num_roomnames > num_roomnames_alloc) {
00757                                 num_roomnames_alloc += 250;
00758                                 rooms = realloc(rooms, (num_roomnames_alloc * 128));
00759                         }
00760                         extract_token(rooms[num_roomnames-1].name, buf, 0, '|', 128);
00761                 }
00762         }
00763 
00764 
00765 /*
00766  * This script should get called by every onChange event...
00767  *
00768  */
00769         wprintf("<script type=\"text/javascript\">                                      \n"
00770                 "                                                                       \n"
00771                 "var highest_active_rule = (-1);                                        \n"
00772                 "                                                                       \n"
00773                 "function UpdateRules() {                                               \n");
00774 /*
00775  * Show only the active rows...
00776  */
00777         wprintf("  highest_active_rule = (-1);                                          \n");
00778         wprintf("  for (i=0; i<%d; ++i) {                                               \n", MAX_RULES);
00779         wprintf("   if ($('active'+i).checked) {                                        \n"
00780                 "     $('rule' + i).style.display = 'block';                            \n"
00781                 "     highest_active_rule = i;                                          \n"
00782                 "   }                                                                   \n"
00783                 "   else {                                                              \n"
00784                 "     $('rule' + i).style.display = 'none';                             \n"
00785                 "   }                                                                   \n"
00786                 "  }                                                                    \n");
00787 /*
00788  * Show only the fields relevant to the rules...
00789  */
00790         wprintf("  for (i=0; i<=highest_active_rule; ++i) {                             \n"
00791                 "    d = ($('movedown'+i));                                             \n"
00792                 "    if (i < highest_active_rule) {                                     \n"
00793                 "      d.style.display = 'block';                                       \n"
00794                 "    }                                                                  \n"
00795                 "    else {                                                             \n"
00796                 "      d.style.display = 'none';                                        \n"
00797                 "    }                                                                  \n"
00798                 "    d = ($('hfield'+i).options[$('hfield'+i).selectedIndex].value);    \n"
00799                 "    if (d == 'all') {                                                  \n"
00800                 "      $('div_size'+i).style.display = 'none';                          \n"
00801                 "      $('div_compare'+i).style.display = 'none';                       \n"
00802                 "      $('div_nocompare'+i).style.display = 'block';                    \n"
00803                 "    }                                                                  \n"
00804                 "    else if (d == 'size') {                                            \n"
00805                 "      $('div_size'+i).style.display = 'block';                         \n"
00806                 "      $('div_compare'+i).style.display = 'none';                       \n"
00807                 "      $('div_nocompare'+i).style.display = 'none';                     \n"
00808                 "    }                                                                  \n"
00809                 "    else {                                                             \n"
00810                 "      $('div_size'+i).style.display = 'none';                          \n"
00811                 "      $('div_compare'+i).style.display = 'block';                      \n"
00812                 "      $('div_nocompare'+i).style.display = 'none';                     \n"
00813                 "    }                                                                  \n"
00814                 "    d = ($('action'+i).options[$('action'+i).selectedIndex].value);    \n"
00815                 "    if (d == 'fileinto') {                                             \n"
00816                 "      $('div_fileinto'+i).style.display = 'block';                     \n"
00817                 "      $('div_redirect'+i).style.display = 'none';                      \n"
00818                 "      $('div_automsg'+i).style.display = 'none';                       \n"
00819                 "    } else if (d == 'redirect') {                                      \n"
00820                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
00821                 "      $('div_redirect'+i).style.display = 'block';                     \n"
00822                 "      $('div_automsg'+i).style.display = 'none';                       \n"
00823                 "    } else if ((d == 'reject') || (d == 'vacation'))  {                \n"
00824                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
00825                 "      $('div_redirect'+i).style.display = 'none';                      \n"
00826                 "      $('div_automsg'+i).style.display = 'block';                      \n"
00827                 "    } else {                                                           \n"
00828                 "      $('div_fileinto'+i).style.display = 'none';                      \n"
00829                 "      $('div_redirect'+i).style.display = 'none';                      \n"
00830                 "      $('div_automsg'+i).style.display = 'none';                       \n"
00831                 "    }                                                                  \n"
00832                 "    if (highest_active_rule < %d) {                                    \n", MAX_RULES-1 );
00833         wprintf("      $('div_addrule').style.display = 'block';                        \n"
00834                 "    } else {                                                           \n"
00835                 "      $('div_addrule').style.display = 'none';                         \n"
00836                 "    }                                                                  \n"
00837                 "  }                                                                    \n"
00838                 "}                                                                      \n"
00839 /*
00840  * Add a rule (really, just un-hide it)
00841  */
00842                 "function AddRule() {                                                   \n"
00843                 "  highest_active_rule = highest_active_rule + 1;                       \n"
00844                 "  $('active'+highest_active_rule).checked = true;                      \n"
00845                 "  UpdateRules();                                                       \n"
00846                 "}                                                                      \n"
00847 /*
00848  * Swap two rules
00849  */
00850                 "function SwapRules(ra, rb) {                                           \n"
00851                 "                                                                       \n"
00852                 "  var things = new Array();                                            \n"
00853                 "  things[0] = 'hfield';                                                \n"
00854                 "  things[1] = 'compare';                                               \n"
00855                 "  things[2] = 'htext';                                                 \n"
00856                 "  things[3] = 'action';                                                \n"
00857                 "  things[4] = 'fileinto';                                              \n"
00858                 "  things[5] = 'redirect';                                              \n"
00859                 "  things[6] = 'final';                                                 \n"
00860                 "  things[7] = 'sizecomp';                                              \n"
00861                 "  things[8] = 'sizeval';                                               \n"
00862                 "  things[9] = 'automsg';                                               \n"
00863                 "                                                                       \n"
00864                 "  for (i=0; i<=9; ++i) {                                               \n"
00865                 "    tempval=$(things[i]+ra).value;                                     \n"
00866                 "    $(things[i]+ra).value = $(things[i]+rb).value;                     \n"
00867                 "    $(things[i]+rb).value = tempval;                                   \n"
00868                 "  }                                                                    \n"
00869                 "}                                                                      \n"
00870 /*
00871  * Delete a rule (percolate the deleted rule out to the end, then deactivate it)
00872  */
00873                 "function DeleteRule(rd) {                                              \n"
00874                 "  for (j=rd; j<=highest_active_rule; ++j) {                            \n"
00875                 "    SwapRules(j, (j+1));                                               \n"
00876                 "  }                                                                    \n"
00877                 "  $('active'+highest_active_rule).checked = false;                     \n"
00878                 "}                                                                      \n"
00879                 "</script>                                                              \n"
00880         );
00881 
00882 
00883         wprintf("<br />");
00884 
00885         wprintf("<table cellpadding=2 width=100%%>");
00886 
00887         for (i=0; i<MAX_RULES; ++i) {
00888 
00889                 /* Grab our existing values to populate */
00890                 active = extract_int(rules[i], 0);
00891                 extract_token(hfield, rules[i], 1, '|', sizeof hfield);
00892                 extract_token(compare, rules[i], 2, '|', sizeof compare);
00893                 extract_token(htext, rules[i], 3, '|', sizeof htext);
00894                 extract_token(sizecomp, rules[i], 4, '|', sizeof sizecomp);
00895                 sizeval = extract_int(rules[i], 5);
00896                 extract_token(action, rules[i], 6, '|', sizeof action);
00897                 extract_token(fileinto, rules[i], 7, '|', sizeof fileinto);
00898                 extract_token(redirect, rules[i], 8, '|', sizeof redirect);
00899                 extract_token(automsg, rules[i], 9, '|', sizeof automsg);
00900                 extract_token(final, rules[i], 10, '|', sizeof final);
00901                 
00902                 /* now generate the table row */
00903 
00904                 wprintf("<tr id=\"rule%d\" bgcolor=\"#%s\">",
00905                         i,
00906                         ((i%2) ? "DDDDDD" : "FFFFFF")
00907                 );
00908 
00909                 wprintf("<td width=5%% align=\"center\">");
00910 
00911                 wprintf("<div style=\"display:none\">");
00912                 wprintf("<input type=\"checkbox\" name=\"active%d\" id=\"active%d\" %s>",
00913                         i, i,
00914                         (active ? "checked" : "")
00915                 );
00916                 wprintf("</div>");
00917 
00918                 if (i>0) wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
00919                         "<img border=\"0\" src=\"static/up_pointer.gif\" "
00920                         "title=\"%s\"/></a>",
00921                         i-1, i, _("Move rule up") );
00922 
00923                 wprintf("<a href=\"javascript:SwapRules(%d,%d);UpdateRules();\">"
00924                         "<img id=\"movedown%d\" border=\"0\" src=\"static/down_pointer.gif\" "
00925                         "title=\"%s\"/></a>",
00926                         i, i+1, i, _("Move rule down") );
00927 
00928                 wprintf("<a href=\"javascript:DeleteRule(%d);UpdateRules();\">"
00929                         "<img id=\"delete%d\" border=\"0\" src=\"static/delete.gif\" "
00930                         "title=\"%s\"/></a>",
00931                         i, i, _("Delete rule") );
00932 
00933                 wprintf("</td>");
00934 
00935                 wprintf("<td width=5%% align=\"center\">");
00936                 wprintf("<font size=+2>%d</font>", i+1);
00937                 wprintf("</td>");
00938 
00939                 wprintf("<td width=20%%>%s ", _("If") );
00940 
00941                 char *hfield_values[14][2] = {
00942                         {       "from",         _("From")               },
00943                         {       "tocc",         _("To or Cc")           },
00944                         {       "subject",      _("Subject")            },
00945                         {       "replyto",      _("Reply-to")           },
00946                         {       "sender",       _("Sender")             },
00947                         {       "resentfrom",   _("Resent-From")        },
00948                         {       "resentto",     _("Resent-To")          },
00949                         {       "envfrom",      _("Envelope From")      },
00950                         {       "envto",        _("Envelope To")        },
00951                         {       "xmailer",      _("X-Mailer")           },
00952                         {       "xspamflag",    _("X-Spam-Flag")        },
00953                         {       "xspamstatus",  _("X-Spam-Status")      },
00954                         {       "size",         _("Message size")       },
00955                         {       "all",          _("All")                }
00956                 };
00957 
00958                 wprintf("<select id=\"hfield%d\" name=\"hfield%d\" size=1 onChange=\"UpdateRules();\">",
00959                         i, i);
00960                 for (j=0; j<14; ++j) {
00961                         wprintf("<option %s value=\"%s\">%s</option>",
00962                                 ( (!strcasecmp(hfield, hfield_values[j][0])) ? "selected" : ""),
00963                                 hfield_values[j][0],
00964                                 hfield_values[j][1]
00965                         );
00966                 }
00967 
00968                 wprintf("</select>");
00969                 wprintf("</td>");
00970 
00971                 wprintf("<td width=20%%>");
00972 
00973                 char *compare_values[6][2] = {
00974                         {       "contains",     _("contains")           },
00975                         {       "notcontains",  _("does not contain")   },
00976                         {       "is",           _("is")                 },
00977                         {       "isnot",        _("is not")             },
00978                         {       "matches",      _("matches")            },
00979                         {       "notmatches",   _("does not match")     }
00980                 };
00981 
00982                 wprintf("<div id=\"div_compare%d\">", i);
00983                 wprintf("<select id=\"compare%d\" name=\"compare%d\" size=1 onChange=\"UpdateRules();\">",
00984                         i, i);
00985                 for (j=0; j<6; ++j) {
00986                         wprintf("<option %s value=\"%s\">%s</option>",
00987                                 ( (!strcasecmp(compare, compare_values[j][0])) ? "selected" : ""),
00988                                 compare_values[j][0],
00989                                 compare_values[j][1]
00990                         );
00991                 }
00992                 wprintf("</select>");
00993 
00994                 wprintf("<input type=\"text\" id=\"htext%d\" name=\"htext%d\" value=\"", i, i);
00995                 escputs(htext);
00996                 wprintf("\"></div>");
00997 
00998                 wprintf("<div id=\"div_nocompare%d\">", i);
00999                 wprintf("%s", _("(All messages)"));
01000                 wprintf("</div>");
01001 
01002                 char *sizecomp_values[2][2] = {
01003                         {       "larger",       _("is larger than")     },
01004                         {       "smaller",      _("is smaller than")    }
01005                 };
01006 
01007                 wprintf("<div id=\"div_size%d\">", i);
01008                 wprintf("<select id=\"sizecomp%d\" name=\"sizecomp%d\" size=1 onChange=\"UpdateRules();\">",
01009                         i, i);
01010                 for (j=0; j<2; ++j) {
01011                         wprintf("<option %s value=\"%s\">%s</option>",
01012                                 ( (!strcasecmp(sizecomp, sizecomp_values[j][0])) ? "selected" : ""),
01013                                 sizecomp_values[j][0],
01014                                 sizecomp_values[j][1]
01015                         );
01016                 }
01017                 wprintf("</select>");
01018 
01019                 wprintf("<input type=\"text\" id=\"sizeval%d\" name=\"sizeval%d\" value=\"%d\">",
01020                         i, i, sizeval);
01021                 wprintf("bytes");
01022                 wprintf("</div>");
01023 
01024                 wprintf("</td>");
01025 
01026                 char *action_values[6][2] = {
01027                         {       "keep",         _("Keep")               },
01028                         {       "discard",      _("Discard silently")   },
01029                         {       "reject",       _("Reject")             },
01030                         {       "fileinto",     _("Move message to")    },
01031                         {       "redirect",     _("Forward to")         },
01032                         {       "vacation",     _("Vacation")           }
01033                 };
01034 
01035                 wprintf("<td width=20%%>");
01036                 wprintf("<select id=\"action%d\" name=\"action%d\" size=1 onChange=\"UpdateRules();\">",
01037                         i, i);
01038                 for (j=0; j<6; ++j) {
01039                         wprintf("<option %s value=\"%s\">%s</option>",
01040                                 ( (!strcasecmp(action, action_values[j][0])) ? "selected" : ""),
01041                                 action_values[j][0],
01042                                 action_values[j][1]
01043                         );
01044                 }
01045                 wprintf("</select>");
01046 
01047                 wprintf("<div id=\"div_fileinto%d\">", i);
01048                 wprintf("<select name=\"fileinto%d\" id=\"fileinto%d\">", i, i);
01049                 for (j=0; j<num_roomnames; ++j) {
01050                         wprintf("<option ");
01051                         if (!strcasecmp(rooms[j].name, fileinto)) {
01052                                 wprintf("selected ");
01053                         }
01054                         wprintf("value=\"");
01055                         escputs(rooms[j].name);
01056                         wprintf("\">");
01057                         escputs(rooms[j].name);
01058                         wprintf("</option>\n");
01059                 }
01060                 wprintf("</select>\n");
01061                 wprintf("</div>");
01062 
01063                 wprintf("<div id=\"div_redirect%d\">", i);
01064                 wprintf("<input type=\"text\" id=\"redirect%d\" name=\"redirect%d\" value=\"", i, i);
01065                 escputs(redirect);
01066                 wprintf("\"></div>");
01067 
01068                 wprintf("<div id=\"div_automsg%d\">", i);
01069                 wprintf(_("Message:"));
01070                 wprintf("<br />");
01071                 wprintf("<textarea name=\"automsg%d\" id=\"automsg%d\" wrap=soft rows=5>\n", i, i);
01072                 escputs(automsg);
01073                 wprintf("</textarea>");
01074                 wprintf("</div>");
01075 
01076                 wprintf("</td>");
01077 
01078                 char *final_values[2][2] = {
01079                         {       "continue",     _("continue processing")        },
01080                         {       "stop",         _("stop")                       }
01081                 };
01082 
01083                 wprintf("<td width=10%% align=\"center\">%s</td>", _("and then") );
01084 
01085                 wprintf("<td width=20%%>");
01086                 wprintf("<select name=\"final%d\" id=\"final%d\" size=1 onChange=\"UpdateRules();\">",
01087                         i, i);
01088                 for (j=0; j<2; ++j) {
01089                         wprintf("<option %s value=\"%s\">%s</option>",
01090                                 ( (!strcasecmp(final, final_values[j][0])) ? "selected" : ""),
01091                                 final_values[j][0],
01092                                 final_values[j][1]
01093                         );
01094                 }
01095                 wprintf("</select>");
01096                 wprintf("</td>");
01097 
01098                 wprintf("</tr>\n");
01099 
01100         }
01101 
01102         wprintf("</table>");
01103         wprintf("<div id=\"div_addrule\"><a href=\"javascript:AddRule();\">%s</a><br /></div>\n",
01104                 _("Add rule")
01105         );
01106 
01107         wprintf("<script type=\"text/javascript\">                                      \n");
01108         wprintf("UpdateRules();                                                         \n");
01109         wprintf("</script>                                                              \n");
01110 
01111         free(rooms);
01112 }
01113 
01114 
01115 
01116 
01117 

Generated on Wed Jun 20 23:13:10 2007 for webcit by  doxygen 1.5.2