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