rssclient.c - dont compare null strings
[citadel.git] / citadel / modules / inetcfg / serv_inetcfg.c
1 /*
2  * This module handles the loading/saving and maintenance of the
3  * system's Internet configuration.  It's not an optional component; I
4  * wrote it as a module merely to keep things as clean and loosely coupled
5  * as possible.
6  *
7  * Copyright (c) 1987-2012 by the citadel.org team
8  *
9  *  This program is open source software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 3.
11  *  
12  *  
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  
20  *  
21  *  
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "user_ops.h"
55 #include "database.h"
56 #include "msgbase.h"
57 #include "internet_addressing.h"
58 #include "genstamp.h"
59 #include "domain.h"
60
61
62 #include "ctdl_module.h"
63
64
65 void inetcfg_setTo(struct CtdlMessage *msg) {
66         char *conf;
67         char buf[SIZ];
68         
69         if (CM_IsEmpty(msg, eMesageText)) return;
70         conf = strdup(msg->cm_fields[eMesageText]);
71
72         if (conf != NULL) {
73                 do {
74                         extract_token(buf, conf, 0, '\n', sizeof buf);
75                         strcpy(conf, &conf[strlen(buf)+1]);
76                 } while ( (!IsEmptyStr(conf)) && (!IsEmptyStr(buf)) );
77
78                 if (inetcfg != NULL) free(inetcfg);
79                 inetcfg = conf;
80         }
81 }
82
83
84 /*
85  * This handler detects changes being made to the system's Internet
86  * configuration.
87  */
88 int inetcfg_aftersave(struct CtdlMessage *msg, recptypes *recp) {
89         char *ptr;
90         int linelen;
91
92         /* If this isn't the configuration room, or if this isn't a MIME
93          * message, don't bother.
94          */
95         if ((msg->cm_fields[eOriginalRoom]) && (strcasecmp(msg->cm_fields[eOriginalRoom], SYSCONFIGROOM)))
96         {
97                 return(0);
98         }
99         if (msg->cm_format_type != 4) return(0);
100
101         ptr = msg->cm_fields[eMesageText];
102         while (ptr != NULL) {
103         
104                 linelen = strcspn(ptr, "\n");
105                 if (linelen == 0) return(0);    /* end of headers */    
106                 
107                 if (!strncasecmp(ptr, "Content-type: ", 14)) {
108                         if (!strncasecmp(&ptr[14], INTERNETCFG,
109                            strlen(INTERNETCFG))) {
110                                 inetcfg_setTo(msg);     /* changing configs */
111                         }
112                 }
113
114                 ptr = strchr((char *)ptr, '\n');
115                 if (ptr != NULL) ++ptr;
116         }
117
118         return(0);
119 }
120
121
122 void inetcfg_init_backend(long msgnum, void *userdata) {
123         struct CtdlMessage *msg;
124
125         msg = CtdlFetchMessage(msgnum, 1, 1);
126         if (msg != NULL) {
127                 inetcfg_setTo(msg);
128                 CM_Free(msg);
129         }
130 }
131
132
133 void inetcfg_init(void) {
134         if (CtdlGetRoom(&CC->room, SYSCONFIGROOM) != 0) return;
135         CtdlForEachMessage(MSGS_LAST, 1, NULL, INTERNETCFG, NULL,
136                 inetcfg_init_backend, NULL);
137 }
138
139
140
141
142 /*****************************************************************************/
143 /*                      MODULE INITIALIZATION STUFF                          */
144 /*****************************************************************************/
145 void clenaup_inetcfg(void)
146 {
147         char *buf;
148
149         buf = inetcfg;
150         inetcfg = NULL;
151         if (buf != NULL)
152                 free(buf);
153 }
154
155 CTDL_MODULE_INIT(inetcfg)
156 {
157         if (!threading)
158         {
159                 CtdlRegisterMessageHook(inetcfg_aftersave, EVT_AFTERSAVE);
160                 inetcfg_init();
161                 CtdlRegisterCleanupHook(clenaup_inetcfg);
162         }
163         
164         /* return our module name for the log */
165         return "inetcfg";
166 }
167