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