]> code.citadel.org Git - citadel.git/blob - citadel/server/modules/inboxrules/serv_inboxrules.c
serv_inboxrules.c: compound fields such as From now work with substring match
[citadel.git] / citadel / server / modules / inboxrules / serv_inboxrules.c
1 // Inbox handling rules
2 //
3 // Copyright (c) 1987-2023 by the citadel.org team
4 //
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 3.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12
13 #include "../../sysdep.h"
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <ctype.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <time.h>
23 #include <sys/wait.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <regex.h>
27 #include <libcitadel.h>
28 #include "../../citadel_defs.h"
29 #include "../../server.h"
30 #include "../../citserver.h"
31 #include "../../support.h"
32 #include "../../config.h"
33 #include "../../database.h"
34 #include "../../msgbase.h"
35 #include "../../internet_addressing.h"
36 #include "../../ctdl_module.h"
37
38
39 // The next sections are enums and keys that drive the serialize/deserialize functions for the inbox rules/state configuration.
40
41 // Fields to be compared
42 enum {
43         field_from,             
44         field_tocc,             
45         field_subject,  
46         field_replyto,  
47         field_sender,   
48         field_resentfrom,       
49         field_resentto, 
50         field_envfrom,  
51         field_envto,    
52         field_xmailer,  
53         field_xspamflag,        
54         field_xspamstatus,      
55         field_listid,   
56         field_size,             
57         field_all
58 };
59 char *field_keys[] = {
60         "from",
61         "tocc",
62         "subject",
63         "replyto",
64         "sender",
65         "resentfrom",
66         "resentto",
67         "envfrom",
68         "envto",
69         "xmailer",
70         "xspamflag",
71         "xspamstatus",
72         "listid",
73         "size",
74         "all"
75 };
76
77 // Field comparison operators
78 enum {
79         fcomp_contains,
80         fcomp_notcontains,
81         fcomp_is,
82         fcomp_isnot,
83         fcomp_matches,
84         fcomp_notmatches
85 };
86 char *fcomp_keys[] = {
87         "contains",
88         "notcontains",
89         "is",
90         "isnot",
91         "matches",
92         "notmatches"
93 };
94
95 // Actions
96 enum {
97         action_keep,
98         action_discard,
99         action_reject,
100         action_fileinto,
101         action_redirect,
102         action_vacation
103 };
104 char *action_keys[] = {
105         "keep",
106         "discard",
107         "reject",
108         "fileinto",
109         "redirect",
110         "vacation"
111 };
112
113 // Size comparison operators
114 enum {
115         scomp_larger,
116         scomp_smaller
117 };
118 char *scomp_keys[] = {
119         "larger",
120         "smaller"
121 };
122
123 // Final actions
124 enum {
125         final_continue,
126         final_stop
127 };
128 char *final_keys[] = {
129         "continue",
130         "stop"
131 };
132
133 // This data structure represents ONE inbox rule within the configuration.
134 struct irule {
135         int compared_field;
136         int field_compare_op;
137         char compared_value[128];
138         int size_compare_op;
139         long compared_size;
140         int action;
141         char file_into[ROOMNAMELEN];
142         char redirect_to[1024];
143         char autoreply_message[SIZ];
144         int final_action;
145 };
146
147 // This data structure represents the entire inbox rules configuration AND current state for a single user.
148 struct inboxrules {
149         long lastproc;
150         int num_rules;
151         struct irule *rules;
152 };
153
154
155 // Destructor for 'struct inboxrules'
156 void free_inbox_rules(struct inboxrules *ibr) {
157         free(ibr->rules);
158         free(ibr);
159 }
160
161
162 // Constructor for 'struct inboxrules' that deserializes the configuration from text input.
163 struct inboxrules *deserialize_inbox_rules(char *serialized_rules) {
164         int i;
165
166         if (!serialized_rules) {
167                 return NULL;
168         }
169
170         // Make a copy of the supplied buffer because we're going to shit all over it with strtok_r()
171         char *sr = strdup(serialized_rules);
172         if (!sr) {
173                 return NULL;
174         }
175
176         struct inboxrules *ibr = malloc(sizeof(struct inboxrules));
177         if (ibr == NULL) {
178                 return NULL;
179         }
180         memset(ibr, 0, sizeof(struct inboxrules));
181
182         char *token; 
183         char *rest = sr;
184         while ((token = strtok_r(rest, "\n", &rest))) {
185
186                 // For backwards compatibility, "# WEBCIT_RULE" is an alias for "rule".
187                 // Prior to version 930, WebCit converted its rules to Sieve scripts, but saved the rules as comments for
188                 // later re-editing.  Now, the rules hidden in the comments become the real rules.
189                 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
190                         strcpy(token, "rule|"); 
191                         strcpy(&token[5], &token[14]);
192                 }
193
194                 // Lines containing actual rules are double-serialized with Base64.  It seemed like a good idea at the time :(
195                 if (!strncasecmp(token, "rule|", 5)) {
196                         remove_token(&token[5], 0, '|');
197                         char *decoded_rule = malloc(strlen(token));
198                         CtdlDecodeBase64(decoded_rule, &token[5], strlen(&token[5]));
199                         ibr->num_rules++;
200                         ibr->rules = realloc(ibr->rules, (sizeof(struct irule) * ibr->num_rules));
201                         struct irule *new_rule = &ibr->rules[ibr->num_rules - 1];
202                         memset(new_rule, 0, sizeof(struct irule));
203
204                         // We have a rule , now parse it
205                         char rtoken[SIZ];
206                         int nt = num_tokens(decoded_rule, '|');
207                         int t = 0;
208                         for (t=0; t<nt; ++t) {
209                                 extract_token(rtoken, decoded_rule, t, '|', sizeof(rtoken));
210                                 string_trim(rtoken);
211                                 switch(t) {
212                                         case 1:                                                         // field to compare
213                                                 for (i=0; i<=field_all; ++i) {
214                                                         if (!strcasecmp(rtoken, field_keys[i])) {
215                                                                 new_rule->compared_field = i;
216                                                         }
217                                                 }
218                                                 break;
219                                         case 2:                                                         // field comparison operation
220                                                 for (i=0; i<=fcomp_notmatches; ++i) {
221                                                         if (!strcasecmp(rtoken, fcomp_keys[i])) {
222                                                                 new_rule->field_compare_op = i;
223                                                         }
224                                                 }
225                                                 break;
226                                         case 3:                                                         // field comparison value
227                                                 safestrncpy(new_rule->compared_value, rtoken, sizeof(new_rule->compared_value));
228                                                 break;
229                                         case 4:                                                         // size comparison operation
230                                                 for (i=0; i<=scomp_smaller; ++i) {
231                                                         if (!strcasecmp(rtoken, scomp_keys[i])) {
232                                                                 new_rule->size_compare_op = i;
233                                                         }
234                                                 }
235                                                 break;
236                                         case 5:                                                         // size comparison value
237                                                 new_rule->compared_size = atol(rtoken);
238                                                 break;
239                                         case 6:                                                         // action
240                                                 for (i=0; i<=action_vacation; ++i) {
241                                                         if (!strcasecmp(rtoken, action_keys[i])) {
242                                                                 new_rule->action = i;
243                                                         }
244                                                 }
245                                                 break;
246                                         case 7:                                                         // file into (target room)
247                                                 safestrncpy(new_rule->file_into, rtoken, sizeof(new_rule->file_into));
248                                                 break;
249                                         case 8:                                                         // redirect to (target address)
250                                                 safestrncpy(new_rule->redirect_to, rtoken, sizeof(new_rule->redirect_to));
251                                                 break;
252                                         case 9:                                                         // autoreply message
253                                                 safestrncpy(new_rule->autoreply_message, rtoken, sizeof(new_rule->autoreply_message));
254                                                 break;
255                                         case 10:                                                        // final_action;
256                                                 for (i=0; i<=final_stop; ++i) {
257                                                         if (!strcasecmp(rtoken, final_keys[i])) {
258                                                                 new_rule->final_action = i;
259                                                         }
260                                                 }
261                                                 break;
262                                         default:
263                                                 break;
264                                 }
265                         }
266                         free(decoded_rule);
267                 }
268
269                 // "lastproc" indicates the newest message number in the inbox that was previously processed by our inbox rules.
270                 // This is a legacy location for this value and will only be used if it's the only one present.
271                 else if (!strncasecmp(token, "lastproc|", 5)) {
272                         ibr->lastproc = atol(&token[9]);
273                 }
274
275                 // Lines which do not contain a recognizable token must be IGNORED.  These lines may be left over
276                 // from a previous version and will disappear when we rewrite the config.
277
278         }
279
280         free(sr);               // free our copy of the source buffer that has now been trashed with null bytes...
281         return(ibr);            // and return our complex data type to the caller.
282 }
283
284
285 // Perform the "fileinto" action (save the message in another room)
286 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
287 //
288 int inbox_do_fileinto(struct irule *rule, long msgnum) {
289         char *dest_folder = rule->file_into;
290         char original_room_name[ROOMNAMELEN];
291         char foldername[ROOMNAMELEN];
292         int c;
293
294         // Situations where we want to just keep the message in the inbox:
295         if (
296                 (IsEmptyStr(dest_folder))                       // no destination room was specified
297                 || (!strcasecmp(dest_folder, "INBOX"))          // fileinto inbox is the same as keep
298                 || (!strcasecmp(dest_folder, MAILROOM))         // fileinto "Mail" is the same as keep
299         ) {
300                 return(1);                                      // don't delete the inbox copy if this failed
301         }
302
303         // Remember what room we came from
304         safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
305
306         // First try a mailbox name match (check personal mail folders first)
307         strcpy(foldername, original_room_name);                                 // This keeps the user namespace of the inbox
308         snprintf(&foldername[10], sizeof(foldername)-10, ".%s", dest_folder);   // And this tacks on the target room name
309         c = CtdlGetRoom(&CC->room, foldername);
310
311         // Then a regular room name match (public and private rooms)
312         if (c != 0) {
313                 safestrncpy(foldername, dest_folder, sizeof foldername);
314                 c = CtdlGetRoom(&CC->room, foldername);
315         }
316
317         if (c != 0) {
318                 syslog(LOG_WARNING, "inboxrules: target <%s> does not exist", dest_folder);
319                 return(1);                                      // don't delete the inbox copy if this failed
320         }
321
322         // Yes, we actually have to go there
323         CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
324
325         c = CtdlSaveMsgPointersInRoom(NULL, &msgnum, 1, 0, NULL, 0);
326
327         // Go back to the room we came from
328         if (strcasecmp(original_room_name, CC->room.QRname)) {
329                 CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
330         }
331
332         return(0);                                              // delete the inbox copy
333 }
334
335
336 // Perform the "redirect" action (divert the message to another email address)
337 // Returns: 1 or 0 to tell the caller to keep (1) or delete (0) the inbox copy of the message.
338 //
339 int inbox_do_redirect(struct irule *rule, long msgnum) {
340         if (IsEmptyStr(rule->redirect_to)) {
341                 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
342                 return(1);                                      // don't delete the inbox copy if this failed
343         }
344
345         struct recptypes *valid = validate_recipients(rule->redirect_to, NULL, 0);
346         if (valid == NULL) {
347                 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
348                 return(1);                                      // don't delete the inbox copy if this failed
349         }
350         if (valid->num_error > 0) {
351                 free_recipients(valid);
352                 syslog(LOG_WARNING, "inboxrules: inbox_do_redirect() invalid recipient <%s>", rule->redirect_to);
353                 return(1);                                      // don't delete the inbox copy if this failed
354         }
355
356         struct CtdlMessage *msg = CtdlFetchMessage(msgnum, 1);
357         if (msg == NULL) {
358                 free_recipients(valid);
359                 syslog(LOG_WARNING, "inboxrules: cannot reload message %ld for forwarding", msgnum);
360                 return(1);                                      // don't delete the inbox copy if this failed
361         }
362
363         CtdlSubmitMsg(msg, valid, NULL);                        // send the message to the new recipient
364         free_recipients(valid);
365         CM_Free(msg);
366         return(0);                                              // delete the inbox copy
367 }
368
369
370 // Perform the "reject" action (delete the message, and tell the sender we deleted it)
371 void inbox_do_reject(struct irule *rule, struct CtdlMessage *msg) {
372         syslog(LOG_DEBUG, "inbox_do_reject: sender: <%s>, reject", msg->cm_fields[erFc822Addr]);
373
374         // If we can't determine who sent the message, reject silently.
375         char *sender;
376         if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
377                 sender = msg->cm_fields[eMessagePath];
378         }
379         else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
380                 sender = msg->cm_fields[erFc822Addr];
381         }
382         else {
383                 return;
384         }
385
386         // Assemble the reject message.
387         char *reject_text = malloc(strlen(rule->autoreply_message) + 1024);
388         if (reject_text == NULL) {
389                 return;
390         }
391         sprintf(reject_text, 
392                 "Content-type: text/plain\n"
393                 "\n"
394                 "The message was refused by the recipient's mail filtering program.\n"
395                 "The reason given was as follows:\n"
396                 "\n"
397                 "%s\n"
398                 "\n"
399         ,
400                 rule->autoreply_message
401         );
402
403         // Deliver the message
404         quickie_message(
405                 " ",
406                 msg->cm_fields[eenVelopeTo],
407                 sender,
408                 MAILROOM,
409                 reject_text,
410                 FMT_RFC822,
411                 "Delivery status notification"
412         );
413         free(reject_text);
414 }
415
416
417 // Perform the "vacation" action (send an automatic response)
418 void inbox_do_vacation(struct irule *rule, struct CtdlMessage *msg) {
419         syslog(LOG_DEBUG, "inbox_do_vacation: sender: <%s>, vacation", msg->cm_fields[erFc822Addr]);
420
421         // If we can't determine who sent the message, no auto-reply can be sent.
422         char *sender;
423         if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
424                 sender = msg->cm_fields[eMessagePath];
425         }
426         else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
427                 sender = msg->cm_fields[erFc822Addr];
428         }
429         else {
430                 return;
431         }
432
433         // Avoid repeatedly sending auto-replies to the same correspondent over and over again by creating
434         // a hash of the user, correspondent, and reply text, and hitting the S_USETABLE database.
435         StrBuf *u = NewStrBuf();
436         StrBufPrintf(u, "vacation/%x/%x/%x",
437                 HashLittle(sender, strlen(sender)),
438                 HashLittle(msg->cm_fields[eenVelopeTo], msg->cm_lengths[eenVelopeTo]),
439                 HashLittle(rule->autoreply_message, strlen(rule->autoreply_message))
440         );
441         int already_seen = CheckIfAlreadySeen(u);
442         FreeStrBuf(&u);
443
444         if (!already_seen) {
445                 // Assemble the auto-reply message.
446                 StrBuf *reject_text = NewStrBuf();
447                 if (reject_text == NULL) {
448                         return;
449                 }
450
451                 StrBufPrintf(reject_text, 
452                         "Content-type: text/plain\n"
453                         "\n"
454                         "%s\n"
455                         "\n"
456                 ,
457                         rule->autoreply_message
458                 );
459         
460                 // Deliver the auto-reply.
461                 quickie_message(
462                         "",
463                         msg->cm_fields[eenVelopeTo],
464                         sender,
465                         MAILROOM,
466                         ChrPtr(reject_text),
467                         FMT_RFC822,
468                         "Delivery status notification"
469                 );
470                 FreeStrBuf(&reject_text);
471         }
472 }
473
474
475 // Process a single message.  We know the room, the user, the rules, the message number, etc.
476 void inbox_do_msg(long msgnum, void *userdata) {
477         struct inboxrules *ii = (struct inboxrules *) userdata;
478         struct CtdlMessage *msg = NULL;         // If we are loading a message to process, put it here.
479         int headers_loaded = 0;                 // Did we load the headers yet?  Do it only once.
480         int body_loaded = 0;                    // Did we load the message body yet?  Do it only once.
481         int metadata_loaded = 0;                // Did we load the metadata yet?  Do it only once.
482         struct MetaData smi;                    // If we are loading the metadata to compare, put it here.
483         int rule_activated = 0;                 // On each rule, this is set if the compare succeeds and the rule activates.
484         char compare_me[SIZ];                   // On each rule, we will store the field to be compared here.
485         int compare_compound = 0;               // Set to 1 when we are comparing both display name and email address
486         int keep_message = 1;                   // Nonzero to keep the message in the inbox after processing, 0 to delete it.
487         int i;
488
489         syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
490
491         if (ii->num_rules <= 0) {
492                 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
493                 return;
494         }
495
496         for (i=0; i<ii->num_rules; ++i) {
497                 syslog(LOG_DEBUG, "inboxrules: processing rule: %d , field: %s", i, field_keys[ ii->rules[i].compared_field ]);
498                 rule_activated = 0;
499
500                 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
501
502                 switch(ii->rules[i].compared_field) {
503                         // These fields require loading only the top-level headers
504                         case field_from:                // From:
505                         case field_tocc:                // To: or Cc:
506                         case field_subject:             // Subject:
507                         case field_replyto:             // Reply-to:
508                         case field_listid:              // List-ID:
509                         case field_envto:               // Envelope-to:
510                         case field_envfrom:             // Return-path:
511                                 if (!headers_loaded) {
512                                         syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
513                                         msg = CtdlFetchMessage(msgnum, 0);
514                                         if (!msg) {
515                                                 return;
516                                         }
517                                         headers_loaded = 1;
518                                 }
519                                 break;
520                         // These fields are not stored as Citadel headers, and therefore require a full message load.
521                         case field_sender:
522                         case field_resentfrom:
523                         case field_resentto:
524                         case field_xmailer:
525                         case field_xspamflag:
526                         case field_xspamstatus:
527                                 if (!body_loaded) {
528                                         syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
529                                         if (msg != NULL) {
530                                                 CM_Free(msg);
531                                         }
532                                         msg = CtdlFetchMessage(msgnum, 1);
533                                         if (!msg) {
534                                                 return;
535                                         }
536                                         headers_loaded = 1;
537                                         body_loaded = 1;
538                                 }
539                                 break;
540                         case field_size:
541                                 if (!metadata_loaded) {
542                                         syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
543                                         GetMetaData(&smi, msgnum);
544                                         metadata_loaded = 1;
545                                 }
546                                 break;
547                         case field_all:
548                                 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
549                                 break;
550                         default:
551                                 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
552                 }
553
554                 // If the rule involves a field comparison, load the field to be compared.
555                 compare_me[0] = 0;
556                 compare_compound = 0;
557                 switch(ii->rules[i].compared_field) {
558                         case field_from:                // From:
559                                 if ( (!IsEmptyStr(msg->cm_fields[erFc822Addr])) && (!IsEmptyStr(msg->cm_fields[erFc822Addr])) ) {
560                                         snprintf(compare_me, sizeof compare_me, "%s|%s",
561                                                 msg->cm_fields[eAuthor],
562                                                 msg->cm_fields[erFc822Addr]
563                                         );
564                                         compare_compound = 1;           // there will be two fields to compare "name|address"
565                                 }
566                                 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
567                                         safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
568                                 }
569                                 else if (!IsEmptyStr(msg->cm_fields[eAuthor])) {
570                                         safestrncpy(compare_me, msg->cm_fields[eAuthor], sizeof compare_me);
571                                 }
572                                 break;
573                         case field_tocc:                // To: or Cc:
574                                 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
575                                         safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
576                                 }
577                                 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
578                                         if (!IsEmptyStr(compare_me)) {
579                                                 strcat(compare_me, ",");
580                                         }
581                                         safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
582                                 }
583                                 break;
584                         case field_subject:             // Subject:
585                                 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
586                                         safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
587                                 }
588                                 break;
589                         case field_replyto:             // Reply-to:
590                                 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
591                                         safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
592                                 }
593                                 break;
594                         case field_listid:              // List-ID:
595                                 if (!IsEmptyStr(msg->cm_fields[eListID])) {
596                                         safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
597                                 }
598                                 break;
599                         case field_envto:               // Envelope-to:
600                                 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
601                                         safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
602                                 }
603                                 break;
604                         case field_envfrom:             // Return-path:
605                                 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
606                                         safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
607                                 }
608                                 break;
609
610                         case field_sender:
611                         case field_resentfrom:
612                         case field_resentto:
613                         case field_xmailer:
614                         case field_xspamflag:
615                         case field_xspamstatus:
616
617                         default:
618                                 break;
619                 }
620
621                 // Message data to compare is loaded, now do something.
622                 switch(ii->rules[i].compared_field) {
623                         case field_from:                // From:
624                         case field_tocc:                // To: or Cc:
625                         case field_subject:             // Subject:
626                         case field_replyto:             // Reply-to:
627                         case field_listid:              // List-ID:
628                         case field_envto:               // Envelope-to:
629                         case field_envfrom:             // Return-path:
630                         case field_sender:
631                         case field_resentfrom:
632                         case field_resentto:
633                         case field_xmailer:
634                         case field_xspamflag:
635                         case field_xspamstatus:
636
637                                 // For all of the above fields, we can compare the field we've loaded into the buffer.
638                                 syslog(LOG_DEBUG, "Value of field to compare is: <%s>", compare_me);
639                                 int substring_match = 0;
640                                 int regex_match = 0;
641                                 int exact_match = 0;
642                                 if (compare_compound) {
643                                         char *sep = strchr(compare_me, '|');
644                                         if (sep) {
645                                                 *sep = 0;
646                                                 ++sep;
647                                                 exact_match =
648                                                         (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1)
649                                                         + (strcasecmp(sep, ii->rules[i].compared_value) ? 0 : 1)
650                                                 ;
651                                                 substring_match =
652                                                         (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0)
653                                                         + (bmstrcasestr(sep, ii->rules[i].compared_value) ? 1 : 0)
654                                                 ;
655                                         }
656                                 }
657                                 else {
658                                         exact_match = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
659                                         substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
660                                 }
661                                 syslog(LOG_DEBUG, "substring match: %d", substring_match);
662                                 syslog(LOG_DEBUG, "    regex match: %d", regex_match);
663                                 syslog(LOG_DEBUG, "    exact match: %d", exact_match);
664                                 switch(ii->rules[i].field_compare_op) {
665                                         case fcomp_contains:
666                                                 rule_activated = substring_match;
667                                                 break;
668                                         case fcomp_matches:
669                                                 rule_activated = regex_match;
670                                                 break;
671                                         case fcomp_notcontains:
672                                                 rule_activated = !substring_match;
673                                                 break;
674                                         case fcomp_notmatches:
675                                                 rule_activated = !regex_match;
676                                                 break;
677                                         case fcomp_is:
678                                                 rule_activated = exact_match;
679                                                 break;
680                                         case fcomp_isnot:
681                                                 rule_activated = !exact_match;
682                                                 break;
683                                 }
684                                 break;
685
686                         case field_size:
687                                 rule_activated = 0;
688                                 switch(ii->rules[i].field_compare_op) {
689                                         case scomp_larger:
690                                                 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
691                                                 break;
692                                         case scomp_smaller:
693                                                 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
694                                                 break;
695                                 }
696                                 break;
697                         case field_all:                 // The "all messages" rule ALWAYS triggers
698                                 rule_activated = 1;
699                                 break;
700                         default:                        // no matches, fall through and do nothing
701                                 syslog(LOG_WARNING, "inboxrules: an unknown field comparison was encountered");
702                                 rule_activated = 0;
703                                 break;
704                 }
705
706                 // If the rule matched, perform the requested action.
707                 if (rule_activated) {
708                         syslog(LOG_DEBUG, "inboxrules: rule activated");
709
710                         // Perform the requested action
711                         switch(ii->rules[i].action) {
712                                 case action_keep:
713                                         keep_message = 1;
714                                         break;
715                                 case action_discard:
716                                         keep_message = 0;
717                                         break;
718                                 case action_reject:
719                                         inbox_do_reject(&ii->rules[i], msg);
720                                         keep_message = 0;
721                                         break;
722                                 case action_fileinto:
723                                         keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
724                                         break;
725                                 case action_redirect:
726                                         keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
727                                         break;
728                                 case action_vacation:
729                                         inbox_do_vacation(&ii->rules[i], msg);
730                                         keep_message = 1;
731                                         break;
732                         }
733
734                         // Now perform the "final" action (anything other than "stop" means continue)
735                         if (ii->rules[i].final_action == final_stop) {
736                                 syslog(LOG_DEBUG, "inboxrules: stop processing");
737                                 i = ii->num_rules + 1;                                  // throw us out of scope to stop
738                         }
739
740
741                 }
742                 else {
743                         syslog(LOG_DEBUG, "inboxrules: rule not activated");
744                 }
745         }
746
747         if (msg != NULL) {              // Delete the copy of the message that is currently in memory.  We don't need it anymore.
748                 CM_Free(msg);
749         }
750
751         if (!keep_message) {            // Delete the copy of the message that is currently in the inbox, if rules dictated that.
752                 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
753                 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");                    // we're in the inbox already
754         }
755
756         ii->lastproc = msgnum;          // make note of the last message we processed, so we don't scan the whole inbox again
757 }
758
759
760 // A user account is identified as requring inbox processing.  Go ahead and do it.
761 void do_inbox_processing_for_user(long usernum) {
762         struct CtdlMessage *msg;
763         struct inboxrules *ii;
764         char roomname[ROOMNAMELEN];
765         char username[64];
766
767         if (CtdlGetUserByNumber(&CC->user, usernum) != 0) {     // grab the user record
768                 return;                                         // and bail out if we were given an invalid user
769         }
770
771         strcpy(username, CC->user.fullname);                    // save the user name so we can fetch it later and lock it
772
773         if (CC->user.msgnum_inboxrules <= 0) {
774                 return;                                         // this user has no inbox rules
775         }
776
777         msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
778         if (msg == NULL) {
779                 return;                                         // config msgnum is set but that message does not exist
780         }
781
782         ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
783         CM_Free(msg);
784
785         if (ii == NULL) {
786                 return;                                         // config message exists but body is null
787         }
788
789         if (ii->lastproc > CC->user.lastproc_inboxrules) {      // There might be a "last message processed" number left over
790                 CC->user.lastproc_inboxrules = ii->lastproc;    // in the ruleset from a previous version.  Use this if it is
791         }                                                       // a higher number.
792         else {
793                 ii->lastproc = CC->user.lastproc_inboxrules;
794         }
795
796         long original_lastproc = ii->lastproc;
797         syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc);
798
799         // Go to the user's inbox room and process all new messages
800         snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
801         if (CtdlGetRoom(&CC->room, roomname) == 0) {
802                 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
803         }
804
805         // Record the number of the last message we processed
806         if (ii->lastproc > original_lastproc) {
807                 CtdlGetUserLock(&CC->user, username);
808                 CC->user.lastproc_inboxrules = ii->lastproc;    // Avoid processing the entire inbox next time
809                 CtdlPutUserLock(&CC->user);
810         }
811
812         // And free the memory.
813         free_inbox_rules(ii);
814 }
815
816
817 // Here is an array of users (by number) who have received messages in their inbox and may require processing.
818 long *users_requiring_inbox_processing = NULL;
819 int num_urip = 0;
820 int num_urip_alloc = 0;
821
822
823 // Perform inbox processing for all rooms which require it
824 void perform_inbox_processing(void) {
825         int i = 0;
826
827         if (num_urip == 0) {
828                 return;                                                                                 // no action required
829         }
830
831         for (i=0; i<num_urip; ++i) {
832                 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
833         }
834
835         free(users_requiring_inbox_processing);
836         users_requiring_inbox_processing = NULL;
837         num_urip = 0;
838         num_urip_alloc = 0;
839 }
840
841
842 // This function is called after a message is saved to a room.  If it's someone's inbox, we have to check for inbox rules
843 int serv_inboxrules_roomhook(struct ctdlroom *room) {
844         int i = 0;
845
846         // Is this someone's inbox?
847         if (!strcasecmp(&room->QRname[11], MAILROOM)) {
848                 long usernum = atol(room->QRname);
849                 if (usernum > 0) {
850
851                         // first check to see if this user is already on the list
852                         if (num_urip > 0) {
853                                 for (i=0; i<=num_urip; ++i) {
854                                         if (users_requiring_inbox_processing[i] == usernum) {           // already on the list!
855                                                 return(0);
856                                         }
857                                 }
858                         }
859
860                         // make room if we need to
861                         if (num_urip_alloc == 0) {
862                                 num_urip_alloc = 100;
863                                 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
864                         }
865                         else if (num_urip >= num_urip_alloc) {
866                                 num_urip_alloc += 100;
867                                 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
868                         }
869                         
870                         // now add the user to the list
871                         users_requiring_inbox_processing[num_urip++] = usernum;
872                 }
873         }
874
875         // No errors are possible from this function.
876         return(0);
877 }
878
879
880
881 // Get InBox Rules
882 //
883 // This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
884 // 
885 // hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
886 void cmd_gibr(char *argbuf) {
887
888         if (CtdlAccessCheck(ac_logged_in)) return;
889
890         cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
891
892         struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
893         if (msg != NULL) {
894                 if (!CM_IsEmpty(msg, eMesageText)) {
895                         char *token; 
896                         char *rest = msg->cm_fields[eMesageText];
897                         while ((token = strtok_r(rest, "\n", &rest))) {
898
899                                 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule" 
900                                 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
901                                         strcpy(token, "rule|"); 
902                                         strcpy(&token[5], &token[14]);
903                                 }
904
905                                 // Output only lines containing rules.
906                                 if (!strncasecmp(token, "rule|", 5)) {
907                                         cprintf("%s\n", token); 
908                                 }
909                                 else {
910                                         cprintf("# invalid rule found : %s\n", token);
911                                 }
912                         }
913                 }
914                 CM_Free(msg);
915         }
916         cprintf("000\n");
917 }
918
919
920 // Rewrite the rule set to disk after it has been modified
921 // Called by cmd_pibr()
922 // Returns the msgnum of the new rules message
923 void rewrite_rules_to_disk(const char *new_config) {
924         long old_msgnum = CC->user.msgnum_inboxrules;
925         char userconfigroomname[ROOMNAMELEN];
926         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
927         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration");
928         CtdlGetUserLock(&CC->user, CC->curr_user);
929         CC->user.msgnum_inboxrules = new_msgnum;                // Now we know where to get the rules next time
930         CC->user.lastproc_inboxrules = new_msgnum;              // Avoid processing the entire inbox next time
931         CtdlPutUserLock(&CC->user);
932         if (old_msgnum > 0) {
933                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
934                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
935         }
936 }
937
938
939 // Put InBox Rules
940 // User transmits the new inbox rules for the account.  They are inserted into the account, replacing the ones already there.
941 void cmd_pibr(char *argbuf) {
942         if (CtdlAccessCheck(ac_logged_in)) return;
943
944         unbuffer_output();
945         cprintf("%d send new rules\n", SEND_LISTING);
946         char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
947         StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
948
949         char *token; 
950         char *rest = newrules;
951         while ((token = strtok_r(rest, "\n", &rest))) {
952                 // Accept only lines containing rules
953                 if (!strncasecmp(token, "rule|", 5)) {
954                         StrBufAppendBufPlain(NewConfig, token, -1, 0);
955                         StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
956                 }
957         }
958         free(newrules);
959         rewrite_rules_to_disk(ChrPtr(NewConfig));
960         FreeStrBuf(&NewConfig);
961 }
962
963
964 // Initialization function, called from modules_init.c
965 char *ctdl_module_init_sieve(void) {
966         if (!threading) {
967                 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
968                 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
969                 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
970                 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
971         }
972         
973         // return our module name for the log
974         return "inboxrules";
975 }