MSGS command can now do full text search on the room
[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 "serv_extensions.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 = strdup(msg->cm_fields['M']);
61
62         if (conf != NULL) {
63                 do {
64                         extract_token(buf, conf, 0, '\n', sizeof buf);
65                         strcpy(conf, &conf[strlen(buf)+1]);
66                 } while ( (strlen(conf)>0) && (strlen(buf)>0) );
67
68                 if (inetcfg != NULL) free(inetcfg);
69                 inetcfg = conf;
70         }
71 }
72
73
74 #ifdef ___NOT_CURRENTLY_IN_USE___
75 void spamstrings_setTo(struct CtdlMessage *msg) {
76         char buf[SIZ];
77         char *conf;
78         struct spamstrings_t *sptr;
79         int i, n;
80
81         /* Clear out the existing list */
82         while (spamstrings != NULL) {
83                 sptr = spamstrings;
84                 spamstrings = spamstrings->next;
85                 free(sptr->string);
86                 free(sptr);
87         }
88
89         /* Read in the new list */
90         if (msg->cm_fields['M']==NULL) return;
91         conf = strdup(msg->cm_fields['M']);
92         if (conf == NULL) return;
93
94         n = num_tokens(conf, '\n');
95         for (i=0; i<n; ++i) {
96                 extract_token(buf, conf, i, '\n', sizeof buf);
97                 sptr = malloc(sizeof(struct spamstrings_t));
98                 sptr->string = strdup(buf);
99                 sptr->next = spamstrings;
100                 spamstrings = sptr;
101         }
102
103 }
104 #endif
105
106
107 /*
108  * This handler detects changes being made to the system's Internet
109  * configuration.
110  */
111 int inetcfg_aftersave(struct CtdlMessage *msg) {
112         char *ptr;
113         int linelen;
114
115         /* If this isn't the configuration room, or if this isn't a MIME
116          * message, don't bother.
117          */
118         if (strcasecmp(msg->cm_fields['O'], SYSCONFIGROOM)) return(0);
119         if (msg->cm_format_type != 4) return(0);
120
121         ptr = msg->cm_fields['M'];
122         while (ptr != NULL) {
123         
124                 linelen = strcspn(ptr, "\n");
125                 if (linelen == 0) return(0);    /* end of headers */    
126                 
127                 if (!strncasecmp(ptr, "Content-type: ", 14)) {
128                         if (!strncasecmp(&ptr[14], INTERNETCFG,
129                            strlen(INTERNETCFG))) {
130                                 inetcfg_setTo(msg);     /* changing configs */
131                         }
132                 }
133
134                 ptr = strchr((char *)ptr, '\n');
135                 if (ptr != NULL) ++ptr;
136         }
137
138         return(0);
139 }
140
141
142 void inetcfg_init_backend(long msgnum, void *userdata) {
143         struct CtdlMessage *msg;
144
145         msg = CtdlFetchMessage(msgnum, 1);
146         if (msg != NULL) {
147                 inetcfg_setTo(msg);
148                 CtdlFreeMessage(msg);
149         }
150 }
151
152
153 #ifdef ___NOT_CURRENTLY_IN_USE___
154 void spamstrings_init_backend(long msgnum, void *userdata) {
155         struct CtdlMessage *msg;
156
157         msg = CtdlFetchMessage(msgnum, 1);
158         if (msg != NULL) {
159                 spamstrings_setTo(msg);
160                 CtdlFreeMessage(msg);
161         }
162 }
163 #endif
164
165
166 void inetcfg_init(void) {
167         if (getroom(&CC->room, SYSCONFIGROOM) != 0) return;
168         CtdlForEachMessage(MSGS_LAST, 1, NULL, INTERNETCFG, NULL,
169                 inetcfg_init_backend, NULL);
170 }
171
172
173
174
175 /*****************************************************************************/
176 /*                      MODULE INITIALIZATION STUFF                          */
177 /*****************************************************************************/
178
179
180 char *serv_inetcfg_init(void)
181 {
182         CtdlRegisterMessageHook(inetcfg_aftersave, EVT_AFTERSAVE);
183         inetcfg_init();
184         return "$Id$";
185 }
186