* Configuration for spam filter
[citadel.git] / citadel / serv_inetcfg.c
1 /*
2  * $Id$ 
3  *
4  * This module handles the loading/saving and maintenance of the
5  * system's Internet configuration.  It's not an optional component; I
6  * wrote it as a module merely to keep things as clean and loosely coupled
7  * as possible.
8  *
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <sys/types.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <sys/wait.h>
33 #include <string.h>
34 #include <limits.h>
35 #include "citadel.h"
36 #include "server.h"
37 #include "sysdep_decls.h"
38 #include "citserver.h"
39 #include "support.h"
40 #include "config.h"
41 #include "dynloader.h"
42 #include "room_ops.h"
43 #include "user_ops.h"
44 #include "policy.h"
45 #include "database.h"
46 #include "msgbase.h"
47 #include "tools.h"
48 #include "internet_addressing.h"
49 #include "genstamp.h"
50 #include "domain.h"
51
52
53
54
55 void inetcfg_setTo(struct CtdlMessage *msg) {
56         char *conf;
57         char buf[SIZ];
58         
59         if (msg->cm_fields['M']==NULL) return;
60         conf = strdoop(msg->cm_fields['M']);
61
62         if (conf != NULL) {
63                 do {
64                         extract_token(buf, conf, 0, '\n');
65                         strcpy(conf, &conf[strlen(buf)+1]);
66                 } while ( (strlen(conf)>0) && (strlen(buf)>0) );
67
68                 if (inetcfg != NULL) phree(inetcfg);
69                 inetcfg = conf;
70         }
71 }
72
73
74 void spamstrings_setTo(struct CtdlMessage *msg) {
75         char buf[SIZ];
76         char *conf;
77         struct spamstrings_t *sptr;
78         int i, n;
79
80         /* Clear out the existing list */
81         while (spamstrings != NULL) {
82                 sptr = spamstrings;
83                 spamstrings = spamstrings->next;
84                 phree(sptr->string);
85                 phree(sptr);
86         }
87
88         /* Read in the new list */
89         if (msg->cm_fields['M']==NULL) return;
90         conf = strdoop(msg->cm_fields['M']);
91         if (conf == NULL) return;
92
93         n = num_tokens(conf, '\n');
94         for (i=0; i<n; ++i) {
95                 extract_token(buf, conf, i, '\n');
96                 sptr = mallok(sizeof(struct spamstrings_t));
97                 sptr->string = strdoop(buf);
98                 sptr->next = spamstrings;
99                 spamstrings = sptr;
100         }
101
102 }
103
104
105 /*
106  * This handler detects changes being made to the system's Internet
107  * configuration.
108  */
109 int inetcfg_aftersave(struct CtdlMessage *msg) {
110         char *ptr;
111         int linelen;
112
113         /* If this isn't the configuration room, or if this isn't a MIME
114          * message, don't bother.
115          */
116         if (strcasecmp(msg->cm_fields['O'], SYSCONFIGROOM)) return(0);
117         if (msg->cm_format_type != 4) return(0);
118
119         ptr = msg->cm_fields['M'];
120         while (ptr != NULL) {
121         
122                 linelen = strcspn(ptr, "\n");
123                 if (linelen == 0) return(0);    /* end of headers */    
124                 
125                 if (!strncasecmp(ptr, "Content-type: ", 14)) {
126                         if (!strncasecmp(&ptr[14], INTERNETCFG,
127                            strlen(INTERNETCFG))) {
128                                 inetcfg_setTo(msg);     /* changing configs */
129                         }
130                         if (!strncasecmp(&ptr[14], SPAMSTRINGS,
131                            strlen(INTERNETCFG))) {
132                                 spamstrings_setTo(msg); /* changing configs */
133                         }
134                 }
135
136                 ptr = strchr((char *)ptr, '\n');
137                 if (ptr != NULL) ++ptr;
138         }
139
140         return(0);
141 }
142
143
144 void inetcfg_init_backend(long msgnum, void *userdata) {
145         struct CtdlMessage *msg;
146
147         msg = CtdlFetchMessage(msgnum);
148         if (msg != NULL) {
149                 inetcfg_setTo(msg);
150                 CtdlFreeMessage(msg);
151         }
152 }
153
154
155 void spamstrings_init_backend(long msgnum, void *userdata) {
156         struct CtdlMessage *msg;
157
158         msg = CtdlFetchMessage(msgnum);
159         if (msg != NULL) {
160                 spamstrings_setTo(msg);
161                 CtdlFreeMessage(msg);
162         }
163 }
164
165
166 void inetcfg_init(void) {
167         if (getroom(&CC->quickroom, SYSCONFIGROOM) != 0) return;
168         CtdlForEachMessage(MSGS_LAST, 1, INTERNETCFG, NULL,
169                 inetcfg_init_backend, NULL);
170         CtdlForEachMessage(MSGS_LAST, 1, SPAMSTRINGS, NULL,
171                 spamstrings_init_backend, NULL);
172 }
173
174
175
176
177 /*****************************************************************************/
178 /*                      MODULE INITIALIZATION STUFF                          */
179 /*****************************************************************************/
180
181
182 char *Dynamic_Module_Init(void)
183 {
184         CtdlRegisterMessageHook(inetcfg_aftersave, EVT_AFTERSAVE);
185         inetcfg_init();
186         return "$Id$";
187 }
188