e38f38696015217175a806b42a0616a6a7115178
[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         const char *ptr,*ptr1;                  // Temporary variables to get X-Spam-Status
489         int len;
490
491         syslog(LOG_DEBUG, "inboxrules: processing message #%ld which is higher than %ld, we are in %s", msgnum, ii->lastproc, CC->room.QRname);
492
493         if (ii->num_rules <= 0) {
494                 syslog(LOG_DEBUG, "inboxrules: rule set is empty");
495                 return;
496         }
497
498         for (i=0; i<ii->num_rules; ++i) {
499                 syslog(LOG_DEBUG, "inboxrules: processing rule: %d , field: %s", i, field_keys[ ii->rules[i].compared_field ]);
500                 rule_activated = 0;
501
502                 // Before doing a field compare, check to see if we have the correct parts of the message in memory.
503
504                 switch(ii->rules[i].compared_field) {
505                         // These fields require loading only the top-level headers
506                         case field_from:                // From:
507                         case field_tocc:                // To: or Cc:
508                         case field_subject:             // Subject:
509                         case field_replyto:             // Reply-to:
510                         case field_listid:              // List-ID:
511                         case field_envto:               // Envelope-to:
512                         case field_envfrom:             // Return-path:
513                                 if (!headers_loaded) {
514                                         syslog(LOG_DEBUG, "inboxrules: loading headers for message %ld", msgnum);
515                                         msg = CtdlFetchMessage(msgnum, 0);
516                                         if (!msg) {
517                                                 return;
518                                         }
519                                         headers_loaded = 1;
520                                 }
521                                 break;
522                         // These fields are not stored as Citadel headers, and therefore require a full message load.
523                         case field_sender:
524                         case field_resentfrom:
525                         case field_resentto:
526                         case field_xmailer:
527                         case field_xspamflag:
528                         case field_xspamstatus:
529                                 if (!body_loaded) {
530                                         syslog(LOG_DEBUG, "inboxrules: loading all of message %ld", msgnum);
531                                         if (msg != NULL) {
532                                                 CM_Free(msg);
533                                         }
534                                         msg = CtdlFetchMessage(msgnum, 1);
535                                         if (!msg) {
536                                                 return;
537                                         }
538                                         headers_loaded = 1;
539                                         body_loaded = 1;
540                                 }
541                                 break;
542                         case field_size:
543                                 if (!metadata_loaded) {
544                                         syslog(LOG_DEBUG, "inboxrules: loading metadata for message %ld", msgnum);
545                                         GetMetaData(&smi, msgnum);
546                                         metadata_loaded = 1;
547                                 }
548                                 break;
549                         case field_all:
550                                 syslog(LOG_DEBUG, "inboxrules: this is an always-on rule");
551                                 break;
552                         default:
553                                 syslog(LOG_DEBUG, "inboxrules: unknown rule key");
554                 }
555
556                 // If the rule involves a field comparison, load the field to be compared.
557                 compare_me[0] = 0;
558                 compare_compound = 0;
559                 switch(ii->rules[i].compared_field) {
560                         case field_from:                // From:
561                                 if ( (!IsEmptyStr(msg->cm_fields[erFc822Addr])) && (!IsEmptyStr(msg->cm_fields[erFc822Addr])) ) {
562                                         snprintf(compare_me, sizeof compare_me, "%s|%s",
563                                                 msg->cm_fields[eAuthor],
564                                                 msg->cm_fields[erFc822Addr]
565                                         );
566                                         compare_compound = 1;           // there will be two fields to compare "name|address"
567                                 }
568                                 else if (!IsEmptyStr(msg->cm_fields[erFc822Addr])) {
569                                         safestrncpy(compare_me, msg->cm_fields[erFc822Addr], sizeof compare_me);
570                                 }
571                                 else if (!IsEmptyStr(msg->cm_fields[eAuthor])) {
572                                         safestrncpy(compare_me, msg->cm_fields[eAuthor], sizeof compare_me);
573                                 }
574                                 break;
575                         case field_tocc:                // To: or Cc:
576                                 if (!IsEmptyStr(msg->cm_fields[eRecipient])) {
577                                         safestrncpy(compare_me, msg->cm_fields[eRecipient], sizeof compare_me);
578                                 }
579                                 if (!IsEmptyStr(msg->cm_fields[eCarbonCopY])) {
580                                         if (!IsEmptyStr(compare_me)) {
581                                                 strcat(compare_me, ",");
582                                         }
583                                         safestrncpy(&compare_me[strlen(compare_me)], msg->cm_fields[eCarbonCopY], (sizeof compare_me - strlen(compare_me)));
584                                 }
585                                 break;
586                         case field_subject:             // Subject:
587                                 if (!IsEmptyStr(msg->cm_fields[eMsgSubject])) {
588                                         safestrncpy(compare_me, msg->cm_fields[eMsgSubject], sizeof compare_me);
589                                 }
590                                 break;
591                         case field_replyto:             // Reply-to:
592                                 if (!IsEmptyStr(msg->cm_fields[eReplyTo])) {
593                                         safestrncpy(compare_me, msg->cm_fields[eReplyTo], sizeof compare_me);
594                                 }
595                                 break;
596                         case field_listid:              // List-ID:
597                                 if (!IsEmptyStr(msg->cm_fields[eListID])) {
598                                         safestrncpy(compare_me, msg->cm_fields[eListID], sizeof compare_me);
599                                 }
600                                 break;
601                         case field_envto:               // Envelope-to:
602                                 if (!IsEmptyStr(msg->cm_fields[eenVelopeTo])) {
603                                         safestrncpy(compare_me, msg->cm_fields[eenVelopeTo], sizeof compare_me);
604                                 }
605                                 break;
606                         case field_envfrom:             // Return-path:
607                                 if (!IsEmptyStr(msg->cm_fields[eMessagePath])) {
608                                         safestrncpy(compare_me, msg->cm_fields[eMessagePath], sizeof compare_me);
609                                 }
610                                 break;
611
612                         case field_xspamstatus:
613                                 if (!IsEmptyStr(msg->cm_fields[eMesageText])) {
614                                         if ((ptr=strstr(msg->cm_fields[eMesageText],"X-Spam-Status"))!=NULL) {
615                                                 len=0;
616                                                 ptr1=ptr;
617                                                 while (*ptr1 && (*ptr1!='\r') && (*ptr1!='\n')) {
618                                                         ptr1++;
619                                                         len++;
620                                                 }
621                                                 if (len && (len<SIZ)) {
622                                                         memcpy(compare_me, ptr, len);
623                                                         compare_me[len]='\0';
624                                                 }
625                                         }
626                                 }
627                         break;
628
629                         case field_sender:
630                         case field_resentfrom:
631                         case field_resentto:
632                         case field_xmailer:
633                         case field_xspamflag:
634
635                         default:
636                                 break;
637                 }
638
639                 // Message data to compare is loaded, now do something.
640                 switch(ii->rules[i].compared_field) {
641                         case field_from:                // From:
642                         case field_tocc:                // To: or Cc:
643                         case field_subject:             // Subject:
644                         case field_replyto:             // Reply-to:
645                         case field_listid:              // List-ID:
646                         case field_envto:               // Envelope-to:
647                         case field_envfrom:             // Return-path:
648                         case field_sender:
649                         case field_resentfrom:
650                         case field_resentto:
651                         case field_xmailer:
652                         case field_xspamflag:
653                         case field_xspamstatus:
654
655                                 // For all of the above fields, we can compare the field we've loaded into the buffer.
656                                 syslog(LOG_DEBUG, "inboxrules: rule %d: field \"%s\" is \"%s\", looking for \"%s\"",
657                                         i,
658                                         field_keys[ii->rules[i].compared_field],
659                                         compare_me,
660                                         ii->rules[i].compared_value
661                                 );
662                                 int substring_match = 0;
663                                 int regex_match = 0;
664                                 int exact_match = 0;
665                                 int rc = 0;
666
667                                 regex_t regex;
668                                 
669                                 rc = regcomp(&regex, ii->rules[i].compared_value, (REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE));
670                                 if (rc) {
671                                         syslog(LOG_ERR, "inboxrules: regcomp: error %d trying to compile \"%s\"", rc, ii->rules[i].compared_value);
672                                 }
673
674                                 if (compare_compound) {                                 // comparing a compound field such as name+address
675                                         char *sep = strchr(compare_me, '|');
676                                         if (sep) {
677                                                 *sep = 0;
678                                                 ++sep;
679                                                 exact_match =
680                                                         (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1)
681                                                         + (strcasecmp(sep, ii->rules[i].compared_value) ? 0 : 1)
682                                                 ;
683                                                 substring_match =
684                                                         (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0)
685                                                         + (bmstrcasestr(sep, ii->rules[i].compared_value) ? 1 : 0)
686                                                 ;
687                                                 if (!rc) regex_match =
688                                                         (regexec(&regex, compare_me, 0, 0, 0) ? 0 : 1)
689                                                         + (regexec(&regex, sep, 0, 0, 0) ? 0 : 1)
690                                                 ;
691                                         }
692                                 }
693                                 else {                                                  // comparing a single field only
694                                         exact_match = (strcasecmp(compare_me, ii->rules[i].compared_value) ? 0 : 1);
695                                         substring_match = (bmstrcasestr(compare_me, ii->rules[i].compared_value) ? 1 : 0);
696                                         if (!rc) regex_match = (regexec(&regex, compare_me, 0, 0, 0) ? 0 : 1);
697                                 }
698
699                                 regfree(&regex);
700                                 syslog(LOG_DEBUG, "inboxrules: substring match: %d", substring_match);
701                                 syslog(LOG_DEBUG, "inboxrules:     regex match: %d", regex_match);
702                                 syslog(LOG_DEBUG, "inboxrules:     exact match: %d", exact_match);
703                                 switch(ii->rules[i].field_compare_op) {
704                                         case fcomp_contains:
705                                                 rule_activated = substring_match;
706                                                 break;
707                                         case fcomp_matches:
708                                                 rule_activated = regex_match;
709                                                 break;
710                                         case fcomp_notcontains:
711                                                 rule_activated = !substring_match;
712                                                 break;
713                                         case fcomp_notmatches:
714                                                 rule_activated = !regex_match;
715                                                 break;
716                                         case fcomp_is:
717                                                 rule_activated = exact_match;
718                                                 break;
719                                         case fcomp_isnot:
720                                                 rule_activated = !exact_match;
721                                                 break;
722                                 }
723                                 break;
724
725                         case field_size:
726                                 rule_activated = 0;
727                                 switch(ii->rules[i].field_compare_op) {
728                                         case scomp_larger:
729                                                 rule_activated = ((smi.meta_rfc822_length > ii->rules[i].compared_size) ? 1 : 0);
730                                                 break;
731                                         case scomp_smaller:
732                                                 rule_activated = ((smi.meta_rfc822_length < ii->rules[i].compared_size) ? 1 : 0);
733                                                 break;
734                                 }
735                                 break;
736                         case field_all:                 // The "all messages" rule ALWAYS triggers
737                                 rule_activated = 1;
738                                 break;
739                         default:                        // no matches, fall through and do nothing
740                                 syslog(LOG_WARNING, "inboxrules: an unknown field comparison was encountered");
741                                 rule_activated = 0;
742                                 break;
743                 }
744
745                 // If the rule matched, perform the requested action.
746                 if (rule_activated) {
747                         syslog(LOG_DEBUG, "inboxrules: rule activated");
748
749                         // Perform the requested action
750                         switch(ii->rules[i].action) {
751                                 case action_keep:
752                                         keep_message = 1;
753                                         break;
754                                 case action_discard:
755                                         keep_message = 0;
756                                         break;
757                                 case action_reject:
758                                         inbox_do_reject(&ii->rules[i], msg);
759                                         keep_message = 0;
760                                         break;
761                                 case action_fileinto:
762                                         keep_message = inbox_do_fileinto(&ii->rules[i], msgnum);
763                                         break;
764                                 case action_redirect:
765                                         keep_message = inbox_do_redirect(&ii->rules[i], msgnum);
766                                         break;
767                                 case action_vacation:
768                                         inbox_do_vacation(&ii->rules[i], msg);
769                                         keep_message = 1;
770                                         break;
771                         }
772
773                         // Now perform the "final" action (anything other than "stop" means continue)
774                         if (ii->rules[i].final_action == final_stop) {
775                                 syslog(LOG_DEBUG, "inboxrules: stop processing");
776                                 i = ii->num_rules + 1;                                  // throw us out of scope to stop
777                         }
778
779
780                 }
781                 else {
782                         syslog(LOG_DEBUG, "inboxrules: rule not activated");
783                 }
784         }
785
786         if (msg != NULL) {              // Delete the copy of the message that is currently in memory.  We don't need it anymore.
787                 CM_Free(msg);
788         }
789
790         if (!keep_message) {            // Delete the copy of the message that is currently in the inbox, if rules dictated that.
791                 syslog(LOG_DEBUG, "inboxrules: delete %ld from inbox", msgnum);
792                 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");                    // we're in the inbox already
793         }
794
795         ii->lastproc = msgnum;          // make note of the last message we processed, so we don't scan the whole inbox again
796 }
797
798
799 // A user account is identified as requring inbox processing.  Go ahead and do it.
800 void do_inbox_processing_for_user(long usernum) {
801         struct CtdlMessage *msg;
802         struct inboxrules *ii;
803         char roomname[ROOMNAMELEN];
804         char username[64];
805
806         if (CtdlGetUserByNumber(&CC->user, usernum) != 0) {     // grab the user record
807                 return;                                         // and bail out if we were given an invalid user
808         }
809
810         strcpy(username, CC->user.fullname);                    // save the user name so we can fetch it later and lock it
811
812         if (CC->user.msgnum_inboxrules <= 0) {
813                 return;                                         // this user has no inbox rules
814         }
815
816         msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
817         if (msg == NULL) {
818                 return;                                         // config msgnum is set but that message does not exist
819         }
820
821         ii = deserialize_inbox_rules(msg->cm_fields[eMesageText]);
822         CM_Free(msg);
823
824         if (ii == NULL) {
825                 return;                                         // config message exists but body is null
826         }
827
828         if (ii->lastproc > CC->user.lastproc_inboxrules) {      // There might be a "last message processed" number left over
829                 CC->user.lastproc_inboxrules = ii->lastproc;    // in the ruleset from a previous version.  Use this if it is
830         }                                                       // a higher number.
831         else {
832                 ii->lastproc = CC->user.lastproc_inboxrules;
833         }
834
835         long original_lastproc = ii->lastproc;
836         syslog(LOG_DEBUG, "inboxrules: for %s, messages newer than %ld", CC->user.fullname, original_lastproc);
837
838         // Go to the user's inbox room and process all new messages
839         snprintf(roomname, sizeof roomname, "%010ld.%s", usernum, MAILROOM);
840         if (CtdlGetRoom(&CC->room, roomname) == 0) {
841                 CtdlForEachMessage(MSGS_GT, ii->lastproc, NULL, NULL, NULL, inbox_do_msg, (void *) ii);
842         }
843
844         // Record the number of the last message we processed
845         if (ii->lastproc > original_lastproc) {
846                 CtdlGetUserLock(&CC->user, username);
847                 CC->user.lastproc_inboxrules = ii->lastproc;    // Avoid processing the entire inbox next time
848                 CtdlPutUserLock(&CC->user);
849         }
850
851         // And free the memory.
852         free_inbox_rules(ii);
853 }
854
855
856 // Here is an array of users (by number) who have received messages in their inbox and may require processing.
857 long *users_requiring_inbox_processing = NULL;
858 int num_urip = 0;
859 int num_urip_alloc = 0;
860
861
862 // Perform inbox processing for all rooms which require it
863 void perform_inbox_processing(void) {
864         int i = 0;
865
866         if (num_urip == 0) {
867                 return;                                                                                 // no action required
868         }
869
870         for (i=0; i<num_urip; ++i) {
871                 do_inbox_processing_for_user(users_requiring_inbox_processing[i]);
872         }
873
874         free(users_requiring_inbox_processing);
875         users_requiring_inbox_processing = NULL;
876         num_urip = 0;
877         num_urip_alloc = 0;
878 }
879
880
881 // 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
882 int serv_inboxrules_roomhook(struct ctdlroom *room) {
883         int i = 0;
884
885         // Is this someone's inbox?
886         if (!strcasecmp(&room->QRname[11], MAILROOM)) {
887                 long usernum = atol(room->QRname);
888                 if (usernum > 0) {
889
890                         // first check to see if this user is already on the list
891                         if (num_urip > 0) {
892                                 for (i=0; i<=num_urip; ++i) {
893                                         if (users_requiring_inbox_processing[i] == usernum) {           // already on the list!
894                                                 return(0);
895                                         }
896                                 }
897                         }
898
899                         // make room if we need to
900                         if (num_urip_alloc == 0) {
901                                 num_urip_alloc = 100;
902                                 users_requiring_inbox_processing = malloc(sizeof(long) * num_urip_alloc);
903                         }
904                         else if (num_urip >= num_urip_alloc) {
905                                 num_urip_alloc += 100;
906                                 users_requiring_inbox_processing = realloc(users_requiring_inbox_processing, (sizeof(long) * num_urip_alloc));
907                         }
908                         
909                         // now add the user to the list
910                         users_requiring_inbox_processing[num_urip++] = usernum;
911                 }
912         }
913
914         // No errors are possible from this function.
915         return(0);
916 }
917
918
919
920 // Get InBox Rules
921 //
922 // This is a client-facing function which fetches the user's inbox rules -- it omits all lines containing anything other than a rule.
923 // 
924 // hmmmmm ... should we try to rebuild this in terms of deserialize_inbox_rules() instread?
925 void cmd_gibr(char *argbuf) {
926
927         if (CtdlAccessCheck(ac_logged_in)) return;
928
929         cprintf("%d inbox rules for %s\n", LISTING_FOLLOWS, CC->user.fullname);
930
931         struct CtdlMessage *msg = CtdlFetchMessage(CC->user.msgnum_inboxrules, 1);
932         if (msg != NULL) {
933                 if (!CM_IsEmpty(msg, eMesageText)) {
934                         char *token; 
935                         char *rest = msg->cm_fields[eMesageText];
936                         while ((token = strtok_r(rest, "\n", &rest))) {
937
938                                 // for backwards compatibility, "# WEBCIT_RULE" is an alias for "rule" 
939                                 if (!strncasecmp(token, "# WEBCIT_RULE|", 14)) {
940                                         strcpy(token, "rule|"); 
941                                         strcpy(&token[5], &token[14]);
942                                 }
943
944                                 // Output only lines containing rules.
945                                 if (!strncasecmp(token, "rule|", 5)) {
946                                         cprintf("%s\n", token); 
947                                 }
948                                 else {
949                                         cprintf("# invalid rule found : %s\n", token);
950                                 }
951                         }
952                 }
953                 CM_Free(msg);
954         }
955         cprintf("000\n");
956 }
957
958
959 // Rewrite the rule set to disk after it has been modified
960 // Called by cmd_pibr()
961 // Returns the msgnum of the new rules message
962 void rewrite_rules_to_disk(const char *new_config) {
963         long old_msgnum = CC->user.msgnum_inboxrules;
964         char userconfigroomname[ROOMNAMELEN];
965         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
966         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, new_config, FMT_RFC822, "inbox rules configuration");
967         CtdlGetUserLock(&CC->user, CC->curr_user);
968         CC->user.msgnum_inboxrules = new_msgnum;                // Now we know where to get the rules next time
969         CC->user.lastproc_inboxrules = new_msgnum;              // Avoid processing the entire inbox next time
970         CtdlPutUserLock(&CC->user);
971         if (old_msgnum > 0) {
972                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
973                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
974         }
975 }
976
977
978 // Put InBox Rules
979 // User transmits the new inbox rules for the account.  They are inserted into the account, replacing the ones already there.
980 void cmd_pibr(char *argbuf) {
981         if (CtdlAccessCheck(ac_logged_in)) return;
982
983         unbuffer_output();
984         cprintf("%d send new rules\n", SEND_LISTING);
985         char *newrules = CtdlReadMessageBody(HKEY("000"), CtdlGetConfigLong("c_maxmsglen"), NULL, 0);
986         StrBuf *NewConfig = NewStrBufPlain("Content-type: application/x-citadel-sieve-config; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
987
988         char *token; 
989         char *rest = newrules;
990         while ((token = strtok_r(rest, "\n", &rest))) {
991                 // Accept only lines containing rules
992                 if (!strncasecmp(token, "rule|", 5)) {
993                         StrBufAppendBufPlain(NewConfig, token, -1, 0);
994                         StrBufAppendBufPlain(NewConfig, HKEY("\n"), 0);
995                 }
996         }
997         free(newrules);
998         rewrite_rules_to_disk(ChrPtr(NewConfig));
999         FreeStrBuf(&NewConfig);
1000 }
1001
1002
1003 // Initialization function, called from modules_init.c
1004 char *ctdl_module_init_sieve(void) {
1005         if (!threading) {
1006                 CtdlRegisterProtoHook(cmd_gibr, "GIBR", "Get InBox Rules");
1007                 CtdlRegisterProtoHook(cmd_pibr, "PIBR", "Put InBox Rules");
1008                 CtdlRegisterRoomHook(serv_inboxrules_roomhook);
1009                 CtdlRegisterSessionHook(perform_inbox_processing, EVT_HOUSE, PRIO_HOUSE + 10);
1010         }
1011         
1012         // return our module name for the log
1013         return "inboxrules";
1014 }