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