Completed a new queue (sieve_list) which will instantly
[citadel.git] / citadel / serv_sieve.c
1 /*
2  * $Id: serv_test.c 3850 2005-09-13 14:00:24Z ajc $
3  *
4  *
5  */
6
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <pwd.h>
14 #include <errno.h>
15 #include <sys/types.h>
16
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
19 # include <time.h>
20 #else
21 # if HAVE_SYS_TIME_H
22 #  include <sys/time.h>
23 # else
24 #  include <time.h>
25 # endif
26 #endif
27
28 #include <sys/wait.h>
29 #include <string.h>
30 #include <limits.h>
31 #include "citadel.h"
32 #include "server.h"
33 #include "sysdep_decls.h"
34 #include "citserver.h"
35 #include "support.h"
36 #include "config.h"
37 #include "serv_extensions.h"
38 #include "room_ops.h"
39 #include "policy.h"
40 #include "database.h"
41 #include "msgbase.h"
42 #include "tools.h"
43
44 #ifdef HAVE_LIBSIEVE
45
46 #include <sieve2.h>
47 #include <sieve2_error.h>
48 #include "serv_sieve.h"
49
50 struct RoomProcList *sieve_list = NULL;
51
52 /*
53  * Add a room to the list of those rooms which potentially require sieve processing
54  */
55 void sieve_queue_room(struct ctdlroom *which_room) {
56         struct RoomProcList *ptr;
57
58         ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
59         if (ptr == NULL) return;
60
61         safestrncpy(ptr->name, which_room->QRname, sizeof ptr->name);
62         begin_critical_section(S_SIEVELIST);
63         ptr->next = sieve_list;
64         sieve_list = ptr;
65         end_critical_section(S_SIEVELIST);
66 }
67
68
69 /*
70  * Perform sieve processing for a single room
71  */
72 void sieve_do_room(char *roomname) {
73         lprintf(CTDL_DEBUG, "Performing Sieve processing for <%s>\n", roomname);
74         /* FIXME ... actually do this instead of just talking about it */
75 }
76
77
78 /*
79  * Perform sieve processing for all rooms which require it
80  */
81 void perform_sieve_processing(void) {
82         struct RoomProcList *ptr = NULL;
83
84         if (sieve_list != NULL) {
85                 lprintf(CTDL_DEBUG, "Begin Sieve processing\n");
86                 while (sieve_list != NULL) {
87                         char spoolroomname[ROOMNAMELEN];
88                         safestrncpy(spoolroomname, sieve_list->name, sizeof spoolroomname);
89                         begin_critical_section(S_SIEVELIST);
90
91                         /* pop this record off the list */
92                         ptr = sieve_list;
93                         sieve_list = sieve_list->next;
94                         free(ptr);
95
96                         /* invalidate any duplicate entries to prevent double processing */
97                         for (ptr=sieve_list; ptr!=NULL; ptr=ptr->next) {
98                                 if (!strcasecmp(ptr->name, spoolroomname)) {
99                                         ptr->name[0] = 0;
100                                 }
101                         }
102
103                         end_critical_section(S_SIEVELIST);
104                         if (spoolroomname[0] != 0) {
105                                 sieve_do_room(spoolroomname);
106                         }
107                 }
108         }
109 }
110
111
112
113 /**
114  *      We don't really care about dumping the entire credits to the log
115  *      every time the server is initialized.  The documentation will suffice
116  *      for that purpose.  We are making a call to sieve2_credits() in order
117  *      to demonstrate that we have successfully linked in to libsieve.
118  */
119 void log_the_sieve2_credits(void) {
120         char *cred = NULL;
121
122         cred = strdup(sieve2_credits());
123         if (cred == NULL) return;
124
125         if (strlen(cred) > 60) {
126                 strcpy(&cred[55], "...");
127         }
128
129         lprintf(CTDL_INFO, "%s\n",cred);
130         free(cred);
131 }
132
133
134 char *serv_sieve_init(void)
135 {
136         log_the_sieve2_credits();
137         return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
138 }
139
140 #else   /* HAVE_LIBSIEVE */
141
142 char *serv_sieve_init(void)
143 {
144         lprintf(CTDL_INFO, "This server is missing libsieve.  Mailbox filtering will be disabled.\n");
145         return "$Id: serv_sieve.c 3850 2005-09-13 14:00:24Z ajc $";
146 }
147
148 #endif  /* HAVE_LIBSIEVE */