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