051aae3bcb024e3619daa6bdb0a2e164d0b142ce
[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  * Copyright (c) 1987-2009 by the citadel.org team
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 3 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #include "sysdep.h"
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <errno.h>
34 #include <sys/types.h>
35
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
38 # include <time.h>
39 #else
40 # if HAVE_SYS_TIME_H
41 #  include <sys/time.h>
42 # else
43 #  include <time.h>
44 # endif
45 #endif
46
47 #include <sys/wait.h>
48 #include <string.h>
49 #include <limits.h>
50 #include <libcitadel.h>
51 #include "citadel.h"
52 #include "server.h"
53 #include "citserver.h"
54 #include "support.h"
55 #include "config.h"
56 #include "user_ops.h"
57 #include "database.h"
58 #include "msgbase.h"
59 #include "internet_addressing.h"
60 #include "genstamp.h"
61 #include "domain.h"
62
63
64 #include "ctdl_module.h"
65
66
67 void inetcfg_setTo(struct CtdlMessage *msg) {
68         char *conf;
69         char buf[SIZ];
70         
71         if (msg->cm_fields['M']==NULL) return;
72         conf = strdup(msg->cm_fields['M']);
73
74         if (conf != NULL) {
75                 do {
76                         extract_token(buf, conf, 0, '\n', sizeof buf);
77                         strcpy(conf, &conf[strlen(buf)+1]);
78                 } while ( (!IsEmptyStr(conf)) && (!IsEmptyStr(buf)) );
79
80                 if (inetcfg != NULL) free(inetcfg);
81                 inetcfg = conf;
82         }
83 }
84
85
86 #ifdef ___NOT_CURRENTLY_IN_USE___
87 void spamstrings_setTo(struct CtdlMessage *msg) {
88         char buf[SIZ];
89         char *conf;
90         struct spamstrings_t *sptr;
91         int i, n;
92
93         /* Clear out the existing list */
94         while (spamstrings != NULL) {
95                 sptr = spamstrings;
96                 spamstrings = spamstrings->next;
97                 free(sptr->string);
98                 free(sptr);
99         }
100
101         /* Read in the new list */
102         if (msg->cm_fields['M']==NULL) return;
103         conf = strdup(msg->cm_fields['M']);
104         if (conf == NULL) return;
105
106         n = num_tokens(conf, '\n');
107         for (i=0; i<n; ++i) {
108                 extract_token(buf, conf, i, '\n', sizeof buf);
109                 sptr = malloc(sizeof(struct spamstrings_t));
110                 sptr->string = strdup(buf);
111                 sptr->next = spamstrings;
112                 spamstrings = sptr;
113         }
114
115 }
116 #endif
117
118
119 /*
120  * This handler detects changes being made to the system's Internet
121  * configuration.
122  */
123 int inetcfg_aftersave(struct CtdlMessage *msg) {
124         char *ptr;
125         int linelen;
126
127         /* If this isn't the configuration room, or if this isn't a MIME
128          * message, don't bother.
129          */
130         if (strcasecmp(msg->cm_fields['O'], SYSCONFIGROOM)) return(0);
131         if (msg->cm_format_type != 4) return(0);
132
133         ptr = msg->cm_fields['M'];
134         while (ptr != NULL) {
135         
136                 linelen = strcspn(ptr, "\n");
137                 if (linelen == 0) return(0);    /* end of headers */    
138                 
139                 if (!strncasecmp(ptr, "Content-type: ", 14)) {
140                         if (!strncasecmp(&ptr[14], INTERNETCFG,
141                            strlen(INTERNETCFG))) {
142                                 inetcfg_setTo(msg);     /* changing configs */
143                         }
144                 }
145
146                 ptr = strchr((char *)ptr, '\n');
147                 if (ptr != NULL) ++ptr;
148         }
149
150         return(0);
151 }
152
153
154 void inetcfg_init_backend(long msgnum, void *userdata) {
155         struct CtdlMessage *msg;
156
157         msg = CtdlFetchMessage(msgnum, 1);
158         if (msg != NULL) {
159                 inetcfg_setTo(msg);
160                 CtdlFreeMessage(msg);
161         }
162 }
163
164
165 #ifdef ___NOT_CURRENTLY_IN_USE___
166 void spamstrings_init_backend(long msgnum, void *userdata) {
167         struct CtdlMessage *msg;
168
169         msg = CtdlFetchMessage(msgnum, 1);
170         if (msg != NULL) {
171                 spamstrings_setTo(msg);
172                 CtdlFreeMessage(msg);
173         }
174 }
175 #endif
176
177
178 void inetcfg_init(void) {
179         if (CtdlGetRoom(&CC->room, SYSCONFIGROOM) != 0) return;
180         CtdlForEachMessage(MSGS_LAST, 1, NULL, INTERNETCFG, NULL,
181                 inetcfg_init_backend, NULL);
182 }
183
184
185
186
187 /*****************************************************************************/
188 /*                      MODULE INITIALIZATION STUFF                          */
189 /*****************************************************************************/
190
191
192 CTDL_MODULE_INIT(inetcfg)
193 {
194         if (!threading)
195         {
196                 CtdlRegisterMessageHook(inetcfg_aftersave, EVT_AFTERSAVE);
197                 inetcfg_init();
198         }
199         
200         /* return our Subversion id for the Log */
201         return "$Id$";
202 }
203