ed69de8587f600d5cb00a265cc63d3085a7f9b32
[citadel.git] / citadel / modules / sieve / serv_sieve.c
1 /*
2  * This module glues libSieve to the Citadel server in order to implement
3  * the Sieve mailbox filtering language (RFC 3028).
4  *
5  * Copyright (c) 1987-2012 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "sysdep.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <ctype.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25
26 #if TIME_WITH_SYS_TIME
27 # include <sys/time.h>
28 # include <time.h>
29 #else
30 # if HAVE_SYS_TIME_H
31 #  include <sys/time.h>
32 # else
33 #  include <time.h>
34 # endif
35 #endif
36
37 #include <sys/wait.h>
38 #include <string.h>
39 #include <limits.h>
40 #include <libcitadel.h>
41 #include "citadel.h"
42 #include "server.h"
43 #include "citserver.h"
44 #include "support.h"
45 #include "config.h"
46 #include "database.h"
47 #include "msgbase.h"
48 #include "internet_addressing.h"
49 #include "ctdl_module.h"
50 #include "serv_sieve.h"
51
52 struct RoomProcList *sieve_list = NULL;
53 char *msiv_extensions = NULL;
54 int SieveDebugEnable = 0;
55
56 #define DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (SieveDebugEnable != 0))
57
58 #define SV_syslog(LEVEL, FORMAT, ...)                           \
59         DBGLOG(LEVEL) syslog(LEVEL,                             \
60                              "Sieve: " FORMAT, __VA_ARGS__)
61
62 #define SVM_syslog(LEVEL, FORMAT)               \
63         DBGLOG(LEVEL) syslog(LEVEL,             \
64                              "Sieve: " FORMAT);
65
66
67 /*
68  * Callback function to send libSieve trace messages to Citadel log facility
69  */
70 int ctdl_debug(sieve2_context_t *s, void *my)
71 {
72         SV_syslog(LOG_DEBUG, "%s", sieve2_getvalue_string(s, "message"));
73         return SIEVE2_OK;
74 }
75
76
77 /*
78  * Callback function to log script parsing errors
79  */
80 int ctdl_errparse(sieve2_context_t *s, void *my)
81 {
82         SV_syslog(LOG_WARNING, "Error in script, line %d: %s",
83                   sieve2_getvalue_int(s, "lineno"),
84                   sieve2_getvalue_string(s, "message")
85         );
86         return SIEVE2_OK;
87 }
88
89
90 /*
91  * Callback function to log script execution errors
92  */
93 int ctdl_errexec(sieve2_context_t *s, void *my)
94 {
95         SV_syslog(LOG_WARNING, "Error executing script: %s",
96                   sieve2_getvalue_string(s, "message")
97                 );
98         return SIEVE2_OK;
99 }
100
101
102 /*
103  * Callback function to redirect a message to a different folder
104  */
105 int ctdl_redirect(sieve2_context_t *s, void *my)
106 {
107         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
108         struct CtdlMessage *msg = NULL;
109         recptypes *valid = NULL;
110         char recp[256];
111
112         safestrncpy(recp, sieve2_getvalue_string(s, "address"), sizeof recp);
113
114         SV_syslog(LOG_DEBUG, "Action is REDIRECT, recipient <%s>", recp);
115
116         valid = validate_recipients(recp, NULL, 0);
117         if (valid == NULL) {
118                 SV_syslog(LOG_WARNING, "REDIRECT failed: bad recipient <%s>", recp);
119                 return SIEVE2_ERROR_BADARGS;
120         }
121         if (valid->num_error > 0) {
122                 SV_syslog(LOG_WARNING, "REDIRECT failed: bad recipient <%s>", recp);
123                 free_recipients(valid);
124                 return SIEVE2_ERROR_BADARGS;
125         }
126
127         msg = CtdlFetchMessage(cs->msgnum, 1);
128         if (msg == NULL) {
129                 SV_syslog(LOG_WARNING, "REDIRECT failed: unable to fetch msg %ld", cs->msgnum);
130                 free_recipients(valid);
131                 return SIEVE2_ERROR_BADARGS;
132         }
133
134         CtdlSubmitMsg(msg, valid, NULL, 0);
135         cs->cancel_implicit_keep = 1;
136         free_recipients(valid);
137         CM_Free(msg);
138         return SIEVE2_OK;
139 }
140
141
142 /*
143  * Callback function to indicate that a message *will* be kept in the inbox
144  */
145 int ctdl_keep(sieve2_context_t *s, void *my)
146 {
147         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
148         
149         SVM_syslog(LOG_DEBUG, "Action is KEEP");
150
151         cs->keep = 1;
152         cs->cancel_implicit_keep = 1;
153         return SIEVE2_OK;
154 }
155
156
157 /*
158  * Callback function to file a message into a different mailbox
159  */
160 int ctdl_fileinto(sieve2_context_t *s, void *my)
161 {
162         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
163         const char *dest_folder = sieve2_getvalue_string(s, "mailbox");
164         int c;
165         char foldername[256];
166         char original_room_name[ROOMNAMELEN];
167
168         SV_syslog(LOG_DEBUG, "Action is FILEINTO, destination is <%s>", dest_folder);
169
170         /* FILEINTO 'INBOX' is the same thing as KEEP */
171         if ( (!strcasecmp(dest_folder, "INBOX")) || (!strcasecmp(dest_folder, MAILROOM)) ) {
172                 cs->keep = 1;
173                 cs->cancel_implicit_keep = 1;
174                 return SIEVE2_OK;
175         }
176
177         /* Remember what room we came from */
178         safestrncpy(original_room_name, CC->room.QRname, sizeof original_room_name);
179
180         /* First try a mailbox name match (check personal mail folders first) */
181         snprintf(foldername, sizeof foldername, "%010ld.%s", cs->usernum, dest_folder);
182         c = CtdlGetRoom(&CC->room, foldername);
183
184         /* Then a regular room name match (public and private rooms) */
185         if (c != 0) {
186                 safestrncpy(foldername, dest_folder, sizeof foldername);
187                 c = CtdlGetRoom(&CC->room, foldername);
188         }
189
190         if (c != 0) {
191                 SV_syslog(LOG_WARNING, "FILEINTO failed: target <%s> does not exist", dest_folder);
192                 return SIEVE2_ERROR_BADARGS;
193         }
194
195         /* Yes, we actually have to go there */
196         CtdlUserGoto(NULL, 0, 0, NULL, NULL, NULL, NULL);
197
198         c = CtdlSaveMsgPointersInRoom(NULL, &cs->msgnum, 1, 0, NULL, 0);
199
200         /* Go back to the room we came from */
201         if (strcasecmp(original_room_name, CC->room.QRname)) {
202                 CtdlUserGoto(original_room_name, 0, 0, NULL, NULL, NULL, NULL);
203         }
204
205         if (c == 0) {
206                 cs->cancel_implicit_keep = 1;
207                 return SIEVE2_OK;
208         }
209         else {
210                 return SIEVE2_ERROR_BADARGS;
211         }
212 }
213
214
215 /*
216  * Callback function to indicate that a message should be discarded.
217  */
218 int ctdl_discard(sieve2_context_t *s, void *my)
219 {
220         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
221
222         SVM_syslog(LOG_DEBUG, "Action is DISCARD");
223
224         /* Cancel the implicit keep.  That's all there is to it. */
225         cs->cancel_implicit_keep = 1;
226         return SIEVE2_OK;
227 }
228
229
230
231 /*
232  * Callback function to indicate that a message should be rejected
233  */
234 int ctdl_reject(sieve2_context_t *s, void *my)
235 {
236         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
237         char *reject_text = NULL;
238
239         SVM_syslog(LOG_DEBUG, "Action is REJECT");
240
241         /* If we don't know who sent the message, do a DISCARD instead. */
242         if (IsEmptyStr(cs->sender)) {
243                 SVM_syslog(LOG_INFO, "Unknown sender.  Doing DISCARD instead of REJECT.");
244                 return ctdl_discard(s, my);
245         }
246
247         /* Assemble the reject message. */
248         reject_text = malloc(strlen(sieve2_getvalue_string(s, "message")) + 1024);
249         if (reject_text == NULL) {
250                 return SIEVE2_ERROR_FAIL;
251         }
252
253         sprintf(reject_text, 
254                 "Content-type: text/plain\n"
255                 "\n"
256                 "The message was refused by the recipient's mail filtering program.\n"
257                 "The reason given was as follows:\n"
258                 "\n"
259                 "%s\n"
260                 "\n"
261         ,
262                 sieve2_getvalue_string(s, "message")
263         );
264
265         quickie_message(        /* This delivers the message */
266                 NULL,
267                 cs->envelope_to,
268                 cs->sender,
269                 NULL,
270                 reject_text,
271                 FMT_RFC822,
272                 "Delivery status notification"
273         );
274
275         free(reject_text);
276         cs->cancel_implicit_keep = 1;
277         return SIEVE2_OK;
278 }
279
280
281
282 /*
283  * Callback function to indicate that a vacation message should be generated
284  */
285 int ctdl_vacation(sieve2_context_t *s, void *my)
286 {
287         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
288         struct sdm_vacation *vptr;
289         int days = 1;
290         const char *message;
291         char *vacamsg_text = NULL;
292         char vacamsg_subject[1024];
293
294         SVM_syslog(LOG_DEBUG, "Action is VACATION");
295
296         message = sieve2_getvalue_string(s, "message");
297         if (message == NULL) return SIEVE2_ERROR_BADARGS;
298
299         if (sieve2_getvalue_string(s, "subject") != NULL) {
300                 safestrncpy(vacamsg_subject, sieve2_getvalue_string(s, "subject"), sizeof vacamsg_subject);
301         }
302         else {
303                 snprintf(vacamsg_subject, sizeof vacamsg_subject, "Re: %s", cs->subject);
304         }
305
306         days = sieve2_getvalue_int(s, "days");
307         if (days < 1) days = 1;
308         if (days > MAX_VACATION) days = MAX_VACATION;
309
310         /* Check to see whether we've already alerted this sender that we're on vacation. */
311         for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
312                 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
313                         if ( (time(NULL) - vptr->timestamp) < (days * 86400) ) {
314                                 SV_syslog(LOG_DEBUG, "Already alerted <%s> recently.", cs->sender);
315                                 return SIEVE2_OK;
316                         }
317                 }
318         }
319
320         /* Assemble the reject message. */
321         vacamsg_text = malloc(strlen(message) + 1024);
322         if (vacamsg_text == NULL) {
323                 return SIEVE2_ERROR_FAIL;
324         }
325
326         sprintf(vacamsg_text, 
327                 "Content-type: text/plain charset=utf-8\n"
328                 "\n"
329                 "%s\n"
330                 "\n"
331         ,
332                 message
333         );
334
335         quickie_message(        /* This delivers the message */
336                 NULL,
337                 cs->envelope_to,
338                 cs->sender,
339                 NULL,
340                 vacamsg_text,
341                 FMT_RFC822,
342                 vacamsg_subject
343         );
344
345         free(vacamsg_text);
346
347         /* Now update the list to reflect the fact that we've alerted this sender.
348          * If they're already in the list, just update the timestamp.
349          */
350         for (vptr = cs->u->first_vacation; vptr != NULL; vptr = vptr->next) {
351                 if (!strcasecmp(vptr->fromaddr, cs->sender)) {
352                         vptr->timestamp = time(NULL);
353                         return SIEVE2_OK;
354                 }
355         }
356
357         /* If we get to this point, create a new record.
358          */
359         vptr = malloc(sizeof(struct sdm_vacation));
360         memset(vptr, 0, sizeof(struct sdm_vacation));
361         vptr->timestamp = time(NULL);
362         safestrncpy(vptr->fromaddr, cs->sender, sizeof vptr->fromaddr);
363         vptr->next = cs->u->first_vacation;
364         cs->u->first_vacation = vptr;
365
366         return SIEVE2_OK;
367 }
368
369
370 /*
371  * Callback function to parse addresses per local system convention
372  * It is disabled because we don't support subaddresses.
373  */
374 #if 0
375 int ctdl_getsubaddress(sieve2_context_t *s, void *my)
376 {
377         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
378
379         /* libSieve does not take ownership of the memory used here.  But, since we
380          * are just pointing to locations inside a struct which we are going to free
381          * later, we're ok.
382          */
383         sieve2_setvalue_string(s, "user", cs->recp_user);
384         sieve2_setvalue_string(s, "detail", "");
385         sieve2_setvalue_string(s, "localpart", cs->recp_user);
386         sieve2_setvalue_string(s, "domain", cs->recp_node);
387         return SIEVE2_OK;
388 }
389 #endif
390
391
392 /*
393  * Callback function to parse message envelope
394  */
395 int ctdl_getenvelope(sieve2_context_t *s, void *my)
396 {
397         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
398
399         SVM_syslog(LOG_DEBUG, "Action is GETENVELOPE");
400         SV_syslog(LOG_DEBUG, "EnvFrom: %s", cs->envelope_from);
401         SV_syslog(LOG_DEBUG, "EnvTo: %s", cs->envelope_to);
402
403         if (cs->envelope_from != NULL) {
404                 if ((cs->envelope_from[0] != '@')&&(cs->envelope_from[strlen(cs->envelope_from)-1] != '@')) {
405                         sieve2_setvalue_string(s, "from", cs->envelope_from);
406                 }
407                 else {
408                         sieve2_setvalue_string(s, "from", "invalid_envelope_from@example.org");
409                 }
410         }
411         else {
412                 sieve2_setvalue_string(s, "from", "null_envelope_from@example.org");
413         }
414
415
416         if (cs->envelope_to != NULL) {
417                 if ((cs->envelope_to[0] != '@') && (cs->envelope_to[strlen(cs->envelope_to)-1] != '@')) {
418                         sieve2_setvalue_string(s, "to", cs->envelope_to);
419                 }
420                 else {
421                         sieve2_setvalue_string(s, "to", "invalid_envelope_to@example.org");
422                 }
423         }
424         else {
425                 sieve2_setvalue_string(s, "to", "null_envelope_to@example.org");
426         }
427
428         return SIEVE2_OK;
429 }
430
431
432 /*
433  * Callback function to fetch message body
434  * (Uncomment the code if we implement this extension)
435  *
436 int ctdl_getbody(sieve2_context_t *s, void *my)
437 {
438         return SIEVE2_ERROR_UNSUPPORTED;
439 }
440  *
441  */
442
443
444 /*
445  * Callback function to fetch message size
446  */
447 int ctdl_getsize(sieve2_context_t *s, void *my)
448 {
449         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
450         struct MetaData smi;
451
452         GetMetaData(&smi, cs->msgnum);
453         
454         if (smi.meta_rfc822_length > 0L) {
455                 sieve2_setvalue_int(s, "size", (int)smi.meta_rfc822_length);
456                 return SIEVE2_OK;
457         }
458
459         return SIEVE2_ERROR_UNSUPPORTED;
460 }
461
462
463 /*
464  * Return a pointer to the active Sieve script.
465  * (Caller does NOT own the memory and should not free the returned pointer.)
466  */
467 char *get_active_script(struct sdm_userdata *u) {
468         struct sdm_script *sptr;
469
470         for (sptr=u->first_script; sptr!=NULL; sptr=sptr->next) {
471                 if (sptr->script_active > 0) {
472                         SV_syslog(LOG_DEBUG, "get_active_script() is using script '%s'", sptr->script_name);
473                         return(sptr->script_content);
474                 }
475         }
476
477         SVM_syslog(LOG_DEBUG, "get_active_script() found no active script");
478         return(NULL);
479 }
480
481
482 /*
483  * Callback function to retrieve the sieve script
484  */
485 int ctdl_getscript(sieve2_context_t *s, void *my) {
486         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
487
488         char *active_script = get_active_script(cs->u);
489         if (active_script != NULL) {
490                 sieve2_setvalue_string(s, "script", active_script);
491                 return SIEVE2_OK;
492         }
493
494         return SIEVE2_ERROR_GETSCRIPT;
495 }
496
497 /*
498  * Callback function to retrieve message headers
499  */
500 int ctdl_getheaders(sieve2_context_t *s, void *my) {
501
502         struct ctdl_sieve *cs = (struct ctdl_sieve *)my;
503
504         SVM_syslog(LOG_DEBUG, "ctdl_getheaders() was called");
505         sieve2_setvalue_string(s, "allheaders", cs->rfc822headers);
506         return SIEVE2_OK;
507 }
508
509
510
511 /*
512  * Add a room to the list of those rooms which potentially require sieve processing
513  */
514 void sieve_queue_room(struct ctdlroom *which_room) {
515         struct RoomProcList *ptr;
516
517         ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
518         if (ptr == NULL) return;
519
520         safestrncpy(ptr->name, which_room->QRname, sizeof ptr->name);
521         begin_critical_section(S_SIEVELIST);
522         ptr->next = sieve_list;
523         sieve_list = ptr;
524         end_critical_section(S_SIEVELIST);
525         SV_syslog(LOG_DEBUG, "<%s> queued for Sieve processing", which_room->QRname);
526 }
527
528
529
530 /*
531  * Perform sieve processing for one message (called by sieve_do_room() for each message)
532  */
533 void sieve_do_msg(long msgnum, void *userdata) {
534         struct sdm_userdata *u = (struct sdm_userdata *) userdata;
535         sieve2_context_t *sieve2_context;
536         struct ctdl_sieve my;
537         int res;
538         struct CtdlMessage *msg;
539         int i;
540         size_t headers_len = 0;
541         int len = 0;
542
543         if (u == NULL)
544         {
545                 SV_syslog(LOG_EMERG, "Can't process message <%ld> without userdata!", msgnum);
546                 return;
547         }
548
549         sieve2_context = u->sieve2_context;
550
551         SV_syslog(LOG_DEBUG, "Performing sieve processing on msg <%ld>", msgnum);
552
553         /*
554          * Make sure you include message body so you can get those second-level headers ;)
555          */
556         msg = CtdlFetchMessage(msgnum, 1);
557         if (msg == NULL) return;
558
559         /*
560          * Grab the message headers so we can feed them to libSieve.
561          * Use HEADERS_ONLY rather than HEADERS_FAST in order to include second-level headers.
562          */
563         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
564         CtdlOutputPreLoadedMsg(msg, MT_RFC822, HEADERS_ONLY, 0, 1, 0);
565         headers_len = StrLength(CC->redirect_buffer);
566         my.rfc822headers = SmashStrBuf(&CC->redirect_buffer);
567
568         /*
569          * libSieve clobbers the stack if it encounters badly formed
570          * headers.  Sanitize our headers by stripping nonprintable
571          * characters.
572          */
573         for (i=0; i<headers_len; ++i) {
574                 if (!isascii(my.rfc822headers[i])) {
575                         my.rfc822headers[i] = '_';
576                 }
577         }
578
579         my.keep = 0;                            /* Set to 1 to declare an *explicit* keep */
580         my.cancel_implicit_keep = 0;            /* Some actions will cancel the implicit keep */
581         my.usernum = atol(CC->room.QRname);     /* Keep track of the owner of the room's namespace */
582         my.msgnum = msgnum;                     /* Keep track of the message number in our local store */
583         my.u = u;                               /* Hand off a pointer to the rest of this info */
584
585         /* Keep track of the recipient so we can do handling based on it later */
586         process_rfc822_addr(msg->cm_fields[eRecipient], my.recp_user, my.recp_node, my.recp_name);
587
588         /* Keep track of the sender so we can use it for REJECT and VACATION responses */
589         if (!CM_IsEmpty(msg, erFc822Addr)) {
590                 safestrncpy(my.sender, msg->cm_fields[erFc822Addr], sizeof my.sender);
591         }
592         else if ( (!CM_IsEmpty(msg, eAuthor)) && (!CM_IsEmpty(msg, eNodeName)) ) {
593                 snprintf(my.sender, sizeof my.sender, "%s@%s", msg->cm_fields[eAuthor], msg->cm_fields[eNodeName]);
594         }
595         else if (!CM_IsEmpty(msg, eAuthor)) {
596                 safestrncpy(my.sender, msg->cm_fields[eAuthor], sizeof my.sender);
597         }
598         else {
599                 strcpy(my.sender, "");
600         }
601
602         /* Keep track of the subject so we can use it for VACATION responses */
603         if (!CM_IsEmpty(msg, eMsgSubject)) {
604                 safestrncpy(my.subject, msg->cm_fields[eMsgSubject], sizeof my.subject);
605         }
606         else {
607                 strcpy(my.subject, "");
608         }
609
610         /* Keep track of the envelope-from address (use body-from if not found) */
611         if (!CM_IsEmpty(msg, eMessagePath)) {
612                 safestrncpy(my.envelope_from, msg->cm_fields[eMessagePath], sizeof my.envelope_from);
613                 stripallbut(my.envelope_from, '<', '>');
614         }
615         else if (!CM_IsEmpty(msg, erFc822Addr)) {
616                 safestrncpy(my.envelope_from, msg->cm_fields[erFc822Addr], sizeof my.envelope_from);
617                 stripallbut(my.envelope_from, '<', '>');
618         }
619         else {
620                 strcpy(my.envelope_from, "");
621         }
622
623         len = strlen(my.envelope_from);
624         for (i=0; i<len; ++i) {
625                 if (isspace(my.envelope_from[i])) my.envelope_from[i] = '_';
626         }
627         if (haschar(my.envelope_from, '@') == 0) {
628                 strcat(my.envelope_from, "@");
629                 strcat(my.envelope_from, config.c_fqdn);
630         }
631
632         /* Keep track of the envelope-to address (use body-to if not found) */
633         if (!CM_IsEmpty(msg, eenVelopeTo)) {
634                 safestrncpy(my.envelope_to, msg->cm_fields[eenVelopeTo], sizeof my.envelope_to);
635                 stripallbut(my.envelope_to, '<', '>');
636         }
637         else if (!CM_IsEmpty(msg, eRecipient)) {
638                 safestrncpy(my.envelope_to, msg->cm_fields[eRecipient], sizeof my.envelope_to);
639                 if (!CM_IsEmpty(msg, eDestination)) {
640                         strcat(my.envelope_to, "@");
641                         strcat(my.envelope_to, msg->cm_fields[eDestination]);
642                 }
643                 stripallbut(my.envelope_to, '<', '>');
644         }
645         else {
646                 strcpy(my.envelope_to, "");
647         }
648
649         len = strlen(my.envelope_to);
650         for (i=0; i<len; ++i) {
651                 if (isspace(my.envelope_to[i])) my.envelope_to[i] = '_';
652         }
653         if (haschar(my.envelope_to, '@') == 0) {
654                 strcat(my.envelope_to, "@");
655                 strcat(my.envelope_to, config.c_fqdn);
656         }
657
658         CM_Free(msg);
659         
660         SVM_syslog(LOG_DEBUG, "Calling sieve2_execute()");
661         res = sieve2_execute(sieve2_context, &my);
662         if (res != SIEVE2_OK) {
663                 SV_syslog(LOG_CRIT, "sieve2_execute() returned %d: %s", res, sieve2_errstr(res));
664         }
665
666         free(my.rfc822headers);
667         my.rfc822headers = NULL;
668
669         /*
670          * Delete the message from the inbox unless either we were told not to, or
671          * if no other action was successfully taken.
672          */
673         if ( (!my.keep) && (my.cancel_implicit_keep) ) {
674                 SVM_syslog(LOG_DEBUG, "keep is 0 -- deleting message from inbox");
675                 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
676         }
677
678         SV_syslog(LOG_DEBUG, "Completed sieve processing on msg <%ld>", msgnum);
679         u->lastproc = msgnum;
680
681         return;
682 }
683
684
685
686 /*
687  * Given the on-disk representation of our Sieve config, load
688  * it into an in-memory data structure.
689  */
690 void parse_sieve_config(char *conf, struct sdm_userdata *u) {
691         char *ptr;
692         char *c, *vacrec;
693         char keyword[256];
694         struct sdm_script *sptr;
695         struct sdm_vacation *vptr;
696
697         ptr = conf;
698         while (c = ptr, ptr = bmstrcasestr(ptr, CTDLSIEVECONFIGSEPARATOR), ptr != NULL) {
699                 *ptr = 0;
700                 ptr += strlen(CTDLSIEVECONFIGSEPARATOR);
701
702                 extract_token(keyword, c, 0, '|', sizeof keyword);
703
704                 if (!strcasecmp(keyword, "lastproc")) {
705                         u->lastproc = extract_long(c, 1);
706                 }
707
708                 else if (!strcasecmp(keyword, "script")) {
709                         sptr = malloc(sizeof(struct sdm_script));
710                         extract_token(sptr->script_name, c, 1, '|', sizeof sptr->script_name);
711                         sptr->script_active = extract_int(c, 2);
712                         remove_token(c, 0, '|');
713                         remove_token(c, 0, '|');
714                         remove_token(c, 0, '|');
715                         sptr->script_content = strdup(c);
716                         sptr->next = u->first_script;
717                         u->first_script = sptr;
718                 }
719
720                 else if (!strcasecmp(keyword, "vacation")) {
721
722                         if (c != NULL) while (vacrec=c, c=strchr(c, '\n'), (c != NULL)) {
723
724                                 *c = 0;
725                                 ++c;
726
727                                 if (strncasecmp(vacrec, "vacation|", 9)) {
728                                         vptr = malloc(sizeof(struct sdm_vacation));
729                                         extract_token(vptr->fromaddr, vacrec, 0, '|', sizeof vptr->fromaddr);
730                                         vptr->timestamp = extract_long(vacrec, 1);
731                                         vptr->next = u->first_vacation;
732                                         u->first_vacation = vptr;
733                                 }
734                         }
735                 }
736
737                 /* ignore unknown keywords */
738         }
739 }
740
741 /*
742  * We found the Sieve configuration for this user.
743  * Now do something with it.
744  */
745 void get_sieve_config_backend(long msgnum, void *userdata) {
746         struct sdm_userdata *u = (struct sdm_userdata *) userdata;
747         struct CtdlMessage *msg;
748         char *conf;
749         long conflen;
750
751         u->config_msgnum = msgnum;
752         msg = CtdlFetchMessage(msgnum, 1);
753         if (msg == NULL) {
754                 u->config_msgnum = (-1) ;
755                 return;
756         }
757
758         CM_GetAsField(msg, eMesageText, &conf, &conflen);
759
760         CM_Free(msg);
761
762         if (conf != NULL) {
763                 parse_sieve_config(conf, u);
764                 free(conf);
765         }
766
767 }
768
769
770 /* 
771  * Write our citadel sieve config back to disk
772  * 
773  * (Set yes_write_to_disk to nonzero to make it actually write the config;
774  * otherwise it just frees the data structures.)
775  */
776 void rewrite_ctdl_sieve_config(struct sdm_userdata *u, int yes_write_to_disk) {
777         StrBuf *text;
778         struct sdm_script *sptr;
779         struct sdm_vacation *vptr;
780         
781         text = NewStrBufPlain(NULL, SIZ);
782         StrBufPrintf(text,
783                      "Content-type: application/x-citadel-sieve-config\n"
784                      "\n"
785                      CTDLSIEVECONFIGSEPARATOR
786                      "lastproc|%ld"
787                      CTDLSIEVECONFIGSEPARATOR
788                      ,
789                      u->lastproc
790                 );
791
792         while (u->first_script != NULL) {
793                 StrBufAppendPrintf(text,
794                                    "script|%s|%d|%s" CTDLSIEVECONFIGSEPARATOR,
795                                    u->first_script->script_name,
796                                    u->first_script->script_active,
797                                    u->first_script->script_content
798                         );
799                 sptr = u->first_script;
800                 u->first_script = u->first_script->next;
801                 free(sptr->script_content);
802                 free(sptr);
803         }
804
805         if (u->first_vacation != NULL) {
806
807                 StrBufAppendPrintf(text, "vacation|\n");
808                 while (u->first_vacation != NULL) {
809                         if ( (time(NULL) - u->first_vacation->timestamp) < (MAX_VACATION * 86400)) {
810                                 StrBufAppendPrintf(text, "%s|%ld\n",
811                                                    u->first_vacation->fromaddr,
812                                                    u->first_vacation->timestamp
813                                         );
814                         }
815                         vptr = u->first_vacation;
816                         u->first_vacation = u->first_vacation->next;
817                         free(vptr);
818                 }
819                 StrBufAppendPrintf(text, CTDLSIEVECONFIGSEPARATOR);
820         }
821
822         if (yes_write_to_disk)
823         {
824                 /* Save the config */
825                 quickie_message("Citadel", NULL, NULL, u->config_roomname,
826                                 ChrPtr(text),
827                                 4,
828                                 "Sieve configuration"
829                 );
830                 
831                 /* And delete the old one */
832                 if (u->config_msgnum > 0) {
833                         CtdlDeleteMessages(u->config_roomname, &u->config_msgnum, 1, "");
834                 }
835         }
836
837         FreeStrBuf (&text);
838
839 }
840
841
842 /*
843  * This is our callback registration table for libSieve.
844  */
845 sieve2_callback_t ctdl_sieve_callbacks[] = {
846         { SIEVE2_ACTION_REJECT,         ctdl_reject             },
847         { SIEVE2_ACTION_VACATION,       ctdl_vacation           },
848         { SIEVE2_ERRCALL_PARSE,         ctdl_errparse           },
849         { SIEVE2_ERRCALL_RUNTIME,       ctdl_errexec            },
850         { SIEVE2_ACTION_FILEINTO,       ctdl_fileinto           },
851         { SIEVE2_ACTION_REDIRECT,       ctdl_redirect           },
852         { SIEVE2_ACTION_DISCARD,        ctdl_discard            },
853         { SIEVE2_ACTION_KEEP,           ctdl_keep               },
854         { SIEVE2_SCRIPT_GETSCRIPT,      ctdl_getscript          },
855         { SIEVE2_DEBUG_TRACE,           ctdl_debug              },
856         { SIEVE2_MESSAGE_GETALLHEADERS, ctdl_getheaders         },
857         { SIEVE2_MESSAGE_GETSIZE,       ctdl_getsize            },
858         { SIEVE2_MESSAGE_GETENVELOPE,   ctdl_getenvelope        },
859 /*
860  * These actions are unsupported by Citadel so we don't declare them.
861  *
862         { SIEVE2_ACTION_NOTIFY,         ctdl_notify             },
863         { SIEVE2_MESSAGE_GETSUBADDRESS, ctdl_getsubaddress      },
864         { SIEVE2_MESSAGE_GETBODY,       ctdl_getbody            },
865  *
866  */
867         { 0 }
868 };
869
870
871 /*
872  * Perform sieve processing for a single room
873  */
874 void sieve_do_room(char *roomname) {
875         
876         struct sdm_userdata u;
877         sieve2_context_t *sieve2_context = NULL;        /* Context for sieve parser */
878         int res;                                        /* Return code from libsieve calls */
879         long orig_lastproc = 0;
880
881         memset(&u, 0, sizeof u);
882
883         /* See if the user who owns this 'mailbox' has any Sieve scripts that
884          * require execution.
885          */
886         snprintf(u.config_roomname, sizeof u.config_roomname, "%010ld.%s", atol(roomname), USERCONFIGROOM);
887         if (CtdlGetRoom(&CC->room, u.config_roomname) != 0) {
888                 SV_syslog(LOG_DEBUG, "<%s> does not exist.  No processing is required.", u.config_roomname);
889                 return;
890         }
891
892         /*
893          * Find the sieve scripts and control record and do something
894          */
895         u.config_msgnum = (-1);
896         CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
897                 get_sieve_config_backend, (void *)&u );
898
899         if (u.config_msgnum < 0) {
900                 SVM_syslog(LOG_DEBUG, "No Sieve rules exist.  No processing is required.");
901                 return;
902         }
903
904         /*
905          * Check to see whether the script is empty and should not be processed.
906          * A script is considered non-empty if it contains at least one semicolon.
907          */
908         if (
909                 (get_active_script(&u) == NULL)
910                 || (strchr(get_active_script(&u), ';') == NULL)
911         ) {
912                 SVM_syslog(LOG_DEBUG, "Sieve script is empty.  No processing is required.");
913                 return;
914         }
915
916         SV_syslog(LOG_DEBUG, "Rules found.  Performing Sieve processing for <%s>", roomname);
917
918         if (CtdlGetRoom(&CC->room, roomname) != 0) {
919                 SV_syslog(LOG_CRIT, "ERROR: cannot load <%s>", roomname);
920                 return;
921         }
922
923         /* Initialize the Sieve parser */
924         
925         res = sieve2_alloc(&sieve2_context);
926         if (res != SIEVE2_OK) {
927                 SV_syslog(LOG_CRIT, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
928                 return;
929         }
930
931         res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
932         if (res != SIEVE2_OK) {
933                 SV_syslog(LOG_CRIT, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
934                 goto BAIL;
935         }
936
937         /* Validate the script */
938
939         struct ctdl_sieve my;           /* dummy ctdl_sieve struct just to pass "u" slong */
940         memset(&my, 0, sizeof my);
941         my.u = &u;
942         res = sieve2_validate(sieve2_context, &my);
943         if (res != SIEVE2_OK) {
944                 SV_syslog(LOG_CRIT, "sieve2_validate() returned %d: %s", res, sieve2_errstr(res));
945                 goto BAIL;
946         }
947
948         /* Do something useful */
949         u.sieve2_context = sieve2_context;
950         orig_lastproc = u.lastproc;
951         CtdlForEachMessage(MSGS_GT, u.lastproc, NULL, NULL, NULL,
952                 sieve_do_msg,
953                 (void *) &u
954         );
955
956 BAIL:
957         res = sieve2_free(&sieve2_context);
958         if (res != SIEVE2_OK) {
959                 SV_syslog(LOG_CRIT, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
960         }
961
962         /* Rewrite the config if we have to */
963         rewrite_ctdl_sieve_config(&u, (u.lastproc > orig_lastproc) ) ;
964 }
965
966
967 /*
968  * Perform sieve processing for all rooms which require it
969  */
970 void perform_sieve_processing(void) {
971         struct RoomProcList *ptr = NULL;
972
973         if (sieve_list != NULL) {
974                 SVM_syslog(LOG_DEBUG, "Begin Sieve processing");
975                 while (sieve_list != NULL) {
976                         char spoolroomname[ROOMNAMELEN];
977                         safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
978                         begin_critical_section(S_SIEVELIST);
979
980                         /* pop this record off the list */
981                         ptr = sieve_list;
982                         sieve_list = sieve_list->next;
983                         free(ptr);
984
985                         /* invalidate any duplicate entries to prevent double processing */
986                         for (ptr=sieve_list; ptr!=NULL; ptr=ptr->next) {
987                                 if (!strcasecmp(ptr->name, spoolroomname)) {
988                                         ptr->name[0] = 0;
989                                 }
990                         }
991
992                         end_critical_section(S_SIEVELIST);
993                         if (spoolroomname[0] != 0) {
994                                 sieve_do_room(spoolroomname);
995                         }
996                 }
997         }
998 }
999
1000
1001 void msiv_load(struct sdm_userdata *u) {
1002         char hold_rm[ROOMNAMELEN];
1003
1004         strcpy(hold_rm, CC->room.QRname);       /* save current room */
1005
1006         /* Take a spin through the user's personal address book */
1007         if (CtdlGetRoom(&CC->room, USERCONFIGROOM) == 0) {
1008         
1009                 u->config_msgnum = (-1);
1010                 strcpy(u->config_roomname, CC->room.QRname);
1011                 CtdlForEachMessage(MSGS_LAST, 1, NULL, SIEVECONFIG, NULL,
1012                         get_sieve_config_backend, (void *)u );
1013
1014         }
1015
1016         if (strcmp(CC->room.QRname, hold_rm)) {
1017                 CtdlGetRoom(&CC->room, hold_rm);    /* return to saved room */
1018         }
1019 }
1020
1021 void msiv_store(struct sdm_userdata *u, int yes_write_to_disk) {
1022 /*
1023  * Initialise the sieve configs last processed message number.
1024  * We don't need to get the highest message number for the users inbox since the systems
1025  * highest message number will be higher than that and loer than this scripts message number
1026  * This prevents this new script from processing any old messages in the inbox.
1027  * Most importantly it will prevent vacation messages being sent to lots of old messages
1028  * in the inbox.
1029  */
1030         u->lastproc = CtdlGetCurrentMessageNumber();
1031         rewrite_ctdl_sieve_config(u, yes_write_to_disk);
1032 }
1033
1034
1035 /*
1036  * Select the active script.
1037  * (Set script_name to an empty string to disable all scripts)
1038  * 
1039  * Returns 0 on success or nonzero for error.
1040  */
1041 int msiv_setactive(struct sdm_userdata *u, char *script_name) {
1042         int ok = 0;
1043         struct sdm_script *s;
1044
1045         /* First see if the supplied value is ok */
1046
1047         if (IsEmptyStr(script_name)) {
1048                 ok = 1;
1049         }
1050         else {
1051                 for (s=u->first_script; s!=NULL; s=s->next) {
1052                         if (!strcasecmp(s->script_name, script_name)) {
1053                                 ok = 1;
1054                         }
1055                 }
1056         }
1057
1058         if (!ok) return(-1);
1059
1060         /* Now set the active script */
1061         for (s=u->first_script; s!=NULL; s=s->next) {
1062                 if (!strcasecmp(s->script_name, script_name)) {
1063                         s->script_active = 1;
1064                 }
1065                 else {
1066                         s->script_active = 0;
1067                 }
1068         }
1069         
1070         return(0);
1071 }
1072
1073
1074 /*
1075  * Fetch a script by name.
1076  *
1077  * Returns NULL if the named script was not found, or a pointer to the script
1078  * if it was found.   NOTE: the caller does *not* own the memory returned by
1079  * this function.  Copy it if you need to keep it.
1080  */
1081 char *msiv_getscript(struct sdm_userdata *u, char *script_name) {
1082         struct sdm_script *s;
1083
1084         for (s=u->first_script; s!=NULL; s=s->next) {
1085                 if (!strcasecmp(s->script_name, script_name)) {
1086                         if (s->script_content != NULL) {
1087                                 return (s->script_content);
1088                         }
1089                 }
1090         }
1091
1092         return(NULL);
1093 }
1094
1095
1096 /*
1097  * Delete a script by name.
1098  *
1099  * Returns 0 if the script was deleted.
1100  *       1 if the script was not found.
1101  *       2 if the script cannot be deleted because it is active.
1102  */
1103 int msiv_deletescript(struct sdm_userdata *u, char *script_name) {
1104         struct sdm_script *s = NULL;
1105         struct sdm_script *script_to_delete = NULL;
1106
1107         for (s=u->first_script; s!=NULL; s=s->next) {
1108                 if (!strcasecmp(s->script_name, script_name)) {
1109                         script_to_delete = s;
1110                         if (s->script_active) {
1111                                 return(2);
1112                         }
1113                 }
1114         }
1115
1116         if (script_to_delete == NULL) return(1);
1117
1118         if (u->first_script == script_to_delete) {
1119                 u->first_script = u->first_script->next;
1120         }
1121         else for (s=u->first_script; s!=NULL; s=s->next) {
1122                 if (s->next == script_to_delete) {
1123                         s->next = s->next->next;
1124                 }
1125         }
1126
1127         free(script_to_delete->script_content);
1128         free(script_to_delete);
1129         return(0);
1130 }
1131
1132
1133 /*
1134  * Add or replace a new script.  
1135  * NOTE: after this function returns, "u" owns the memory that "script_content"
1136  * was pointing to.
1137  */
1138 void msiv_putscript(struct sdm_userdata *u, char *script_name, char *script_content) {
1139         int replaced = 0;
1140         struct sdm_script *s, *sptr;
1141
1142         for (s=u->first_script; s!=NULL; s=s->next) {
1143                 if (!strcasecmp(s->script_name, script_name)) {
1144                         if (s->script_content != NULL) {
1145                                 free(s->script_content);
1146                         }
1147                         s->script_content = script_content;
1148                         replaced = 1;
1149                 }
1150         }
1151
1152         if (replaced == 0) {
1153                 sptr = malloc(sizeof(struct sdm_script));
1154                 safestrncpy(sptr->script_name, script_name, sizeof sptr->script_name);
1155                 sptr->script_content = script_content;
1156                 sptr->script_active = 0;
1157                 sptr->next = u->first_script;
1158                 u->first_script = sptr;
1159         }
1160 }
1161
1162
1163
1164 /*
1165  * Citadel protocol to manage sieve scripts.
1166  * This is basically a simplified (read: doesn't resemble IMAP) version
1167  * of the 'managesieve' protocol.
1168  */
1169 void cmd_msiv(char *argbuf) {
1170         char subcmd[256];
1171         struct sdm_userdata u;
1172         char script_name[256];
1173         char *script_content = NULL;
1174         struct sdm_script *s;
1175         int i;
1176         int changes_made = 0;
1177
1178         memset(&u, 0, sizeof(struct sdm_userdata));
1179
1180         if (CtdlAccessCheck(ac_logged_in)) return;
1181         extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1182         msiv_load(&u);
1183
1184         if (!strcasecmp(subcmd, "putscript")) {
1185                 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1186                 if (!IsEmptyStr(script_name)) {
1187                         cprintf("%d Transmit script now\n", SEND_LISTING);
1188                         script_content = CtdlReadMessageBody(HKEY("000"), config.c_maxmsglen, NULL, 0, 0);
1189                         msiv_putscript(&u, script_name, script_content);
1190                         changes_made = 1;
1191                 }
1192                 else {
1193                         cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
1194                 }
1195         }       
1196         
1197         else if (!strcasecmp(subcmd, "listscripts")) {
1198                 cprintf("%d Scripts:\n", LISTING_FOLLOWS);
1199                 for (s=u.first_script; s!=NULL; s=s->next) {
1200                         if (s->script_content != NULL) {
1201                                 cprintf("%s|%d|\n", s->script_name, s->script_active);
1202                         }
1203                 }
1204                 cprintf("000\n");
1205         }
1206
1207         else if (!strcasecmp(subcmd, "setactive")) {
1208                 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1209                 if (msiv_setactive(&u, script_name) == 0) {
1210                         cprintf("%d ok\n", CIT_OK);
1211                         changes_made = 1;
1212                 }
1213                 else {
1214                         cprintf("%d Script '%s' does not exist.\n",
1215                                 ERROR + ILLEGAL_VALUE,
1216                                 script_name
1217                         );
1218                 }
1219         }
1220
1221         else if (!strcasecmp(subcmd, "getscript")) {
1222                 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1223                 script_content = msiv_getscript(&u, script_name);
1224                 if (script_content != NULL) {
1225                         int script_len;
1226
1227                         cprintf("%d Script:\n", LISTING_FOLLOWS);
1228                         script_len = strlen(script_content);
1229                         client_write(script_content, script_len);
1230                         if (script_content[script_len-1] != '\n') {
1231                                 cprintf("\n");
1232                         }
1233                         cprintf("000\n");
1234                 }
1235                 else {
1236                         cprintf("%d Invalid script name.\n", ERROR + ILLEGAL_VALUE);
1237                 }
1238         }
1239
1240         else if (!strcasecmp(subcmd, "deletescript")) {
1241                 extract_token(script_name, argbuf, 1, '|', sizeof script_name);
1242                 i = msiv_deletescript(&u, script_name);
1243                 if (i == 0) {
1244                         cprintf("%d ok\n", CIT_OK);
1245                         changes_made = 1;
1246                 }
1247                 else if (i == 1) {
1248                         cprintf("%d Script '%s' does not exist.\n",
1249                                 ERROR + ILLEGAL_VALUE,
1250                                 script_name
1251                         );
1252                 }
1253                 else if (i == 2) {
1254                         cprintf("%d Script '%s' is active and cannot be deleted.\n",
1255                                 ERROR + ILLEGAL_VALUE,
1256                                 script_name
1257                         );
1258                 }
1259                 else {
1260                         cprintf("%d unknown error\n", ERROR);
1261                 }
1262         }
1263
1264         else {
1265                 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1266         }
1267
1268         msiv_store(&u, changes_made);
1269 }
1270
1271
1272
1273 void ctdl_sieve_init(void) {
1274         char *cred = NULL;
1275         sieve2_context_t *sieve2_context = NULL;
1276         int res;
1277
1278         /*
1279          *      We don't really care about dumping the entire credits to the log
1280          *      every time the server is initialized.  The documentation will suffice
1281          *      for that purpose.  We are making a call to sieve2_credits() in order
1282          *      to demonstrate that we have successfully linked in to libsieve.
1283          */
1284         cred = strdup(sieve2_credits());
1285         if (cred == NULL) return;
1286
1287         if (strlen(cred) > 60) {
1288                 strcpy(&cred[55], "...");
1289         }
1290
1291         SV_syslog(LOG_INFO, "%s",cred);
1292         free(cred);
1293
1294         /* Briefly initialize a Sieve parser instance just so we can list the
1295          * extensions that are available.
1296          */
1297         res = sieve2_alloc(&sieve2_context);
1298         if (res != SIEVE2_OK) {
1299                 SV_syslog(LOG_CRIT, "sieve2_alloc() returned %d: %s", res, sieve2_errstr(res));
1300                 return;
1301         }
1302
1303         res = sieve2_callbacks(sieve2_context, ctdl_sieve_callbacks);
1304         if (res != SIEVE2_OK) {
1305                 SV_syslog(LOG_CRIT, "sieve2_callbacks() returned %d: %s", res, sieve2_errstr(res));
1306                 goto BAIL;
1307         }
1308
1309         msiv_extensions = strdup(sieve2_listextensions(sieve2_context));
1310         SV_syslog(LOG_INFO, "Extensions: %s", msiv_extensions);
1311
1312 BAIL:   res = sieve2_free(&sieve2_context);
1313         if (res != SIEVE2_OK) {
1314                 SV_syslog(LOG_CRIT, "sieve2_free() returned %d: %s", res, sieve2_errstr(res));
1315         }
1316
1317 }
1318
1319 void cleanup_sieve(void)
1320 {
1321         struct RoomProcList *ptr, *ptr2;
1322
1323         if (msiv_extensions != NULL)
1324                 free(msiv_extensions);
1325         msiv_extensions = NULL;
1326
1327         begin_critical_section(S_SIEVELIST);
1328         ptr=sieve_list;
1329         while (ptr != NULL) {
1330                 ptr2 = ptr->next;
1331                 free(ptr);
1332                 ptr = ptr2;
1333         }
1334         sieve_list = NULL;
1335         end_critical_section(S_SIEVELIST);
1336 }
1337
1338 int serv_sieve_room(struct ctdlroom *room)
1339 {
1340         if (!strcasecmp(&room->QRname[11], MAILROOM)) {
1341                 sieve_queue_room(room);
1342         }
1343         return 0;
1344 }
1345
1346 void LogSieveDebugEnable(const int n)
1347 {
1348         SieveDebugEnable = n;
1349 }
1350 CTDL_MODULE_INIT(sieve)
1351 {
1352         if (!threading)
1353         {
1354                 CtdlRegisterDebugFlagHook(HKEY("sieve"), LogSieveDebugEnable, &SieveDebugEnable);
1355                 ctdl_sieve_init();
1356                 CtdlRegisterProtoHook(cmd_msiv, "MSIV", "Manage Sieve scripts");
1357                 CtdlRegisterRoomHook(serv_sieve_room);
1358                 CtdlRegisterSessionHook(perform_sieve_processing, EVT_HOUSE, PRIO_HOUSE + 10);
1359                 CtdlRegisterCleanupHook(cleanup_sieve);
1360         }
1361         
1362         /* return our module name for the log */
1363         return "sieve";
1364 }
1365