HUGE PATCH. This moves all of mime_parser.c and all
[citadel.git] / citadel / modules / inetcfg / 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 <libcitadel.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "citserver.h"
39 #include "support.h"
40 #include "config.h"
41 #include "room_ops.h"
42 #include "user_ops.h"
43 #include "policy.h"
44 #include "database.h"
45 #include "msgbase.h"
46 #include "internet_addressing.h"
47 #include "genstamp.h"
48 #include "domain.h"
49
50
51 #include "ctdl_module.h"
52
53
54 void inetcfg_setTo(struct CtdlMessage *msg) {
55         char *conf;
56         char buf[SIZ];
57         
58         if (msg->cm_fields['M']==NULL) return;
59         conf = strdup(msg->cm_fields['M']);
60
61         if (conf != NULL) {
62                 do {
63                         extract_token(buf, conf, 0, '\n', sizeof buf);
64                         strcpy(conf, &conf[strlen(buf)+1]);
65                 } while ( (!IsEmptyStr(conf)) && (!IsEmptyStr(buf)) );
66
67                 if (inetcfg != NULL) free(inetcfg);
68                 inetcfg = conf;
69         }
70 }
71
72
73 #ifdef ___NOT_CURRENTLY_IN_USE___
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                 free(sptr->string);
85                 free(sptr);
86         }
87
88         /* Read in the new list */
89         if (msg->cm_fields['M']==NULL) return;
90         conf = strdup(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', sizeof buf);
96                 sptr = malloc(sizeof(struct spamstrings_t));
97                 sptr->string = strdup(buf);
98                 sptr->next = spamstrings;
99                 spamstrings = sptr;
100         }
101
102 }
103 #endif
104
105
106 /*
107  * This handler detects changes being made to the system's Internet
108  * configuration.
109  */
110 int inetcfg_aftersave(struct CtdlMessage *msg) {
111         char *ptr;
112         int linelen;
113
114         /* If this isn't the configuration room, or if this isn't a MIME
115          * message, don't bother.
116          */
117         if (strcasecmp(msg->cm_fields['O'], SYSCONFIGROOM)) return(0);
118         if (msg->cm_format_type != 4) return(0);
119
120         ptr = msg->cm_fields['M'];
121         while (ptr != NULL) {
122         
123                 linelen = strcspn(ptr, "\n");
124                 if (linelen == 0) return(0);    /* end of headers */    
125                 
126                 if (!strncasecmp(ptr, "Content-type: ", 14)) {
127                         if (!strncasecmp(&ptr[14], INTERNETCFG,
128                            strlen(INTERNETCFG))) {
129                                 inetcfg_setTo(msg);     /* changing configs */
130                         }
131                 }
132
133                 ptr = strchr((char *)ptr, '\n');
134                 if (ptr != NULL) ++ptr;
135         }
136
137         return(0);
138 }
139
140
141 void inetcfg_init_backend(long msgnum, void *userdata) {
142         struct CtdlMessage *msg;
143
144         msg = CtdlFetchMessage(msgnum, 1);
145         if (msg != NULL) {
146                 inetcfg_setTo(msg);
147                 CtdlFreeMessage(msg);
148         }
149 }
150
151
152 #ifdef ___NOT_CURRENTLY_IN_USE___
153 void spamstrings_init_backend(long msgnum, void *userdata) {
154         struct CtdlMessage *msg;
155
156         msg = CtdlFetchMessage(msgnum, 1);
157         if (msg != NULL) {
158                 spamstrings_setTo(msg);
159                 CtdlFreeMessage(msg);
160         }
161 }
162 #endif
163
164
165 void inetcfg_init(void) {
166         if (getroom(&CC->room, SYSCONFIGROOM) != 0) return;
167         CtdlForEachMessage(MSGS_LAST, 1, NULL, INTERNETCFG, NULL,
168                 inetcfg_init_backend, NULL);
169 }
170
171
172
173
174 /*****************************************************************************/
175 /*                      MODULE INITIALIZATION STUFF                          */
176 /*****************************************************************************/
177
178
179 CTDL_MODULE_INIT(inetcfg)
180 {
181         CtdlRegisterMessageHook(inetcfg_aftersave, EVT_AFTERSAVE);
182         inetcfg_init();
183
184         /* return our Subversion id for the Log */
185         return "$Id$";
186 }
187