fix all the <time.h> vs. <sys/time.h> issues, hopefully
[citadel.git] / citadel / 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 "citadel.h"
36 #include "server.h"
37 #include "sysdep_decls.h"
38 #include "citserver.h"
39 #include "support.h"
40 #include "config.h"
41 #include "dynloader.h"
42 #include "room_ops.h"
43 #include "user_ops.h"
44 #include "policy.h"
45 #include "database.h"
46 #include "msgbase.h"
47 #include "tools.h"
48 #include "internet_addressing.h"
49 #include "genstamp.h"
50 #include "domain.h"
51
52 void inetcfg_setTo(struct CtdlMessage *msg) {
53         char *conf;
54         char buf[SIZ];
55
56         if (msg->cm_fields['M']==NULL) return;
57         conf = strdoop(msg->cm_fields['M']);
58
59         if (conf != NULL) {
60                 do {
61                         extract_token(buf, conf, 0, '\n');
62                         strcpy(conf, &conf[strlen(buf)+1]);
63                 } while ( (strlen(conf)>0) && (strlen(buf)>0) );
64
65                 if (inetcfg != NULL) phree(inetcfg);
66                 inetcfg = conf;
67         }
68 }
69
70
71 /*
72  * This handler detects changes being made to the system's Internet
73  * configuration.
74  */
75 int inetcfg_aftersave(struct CtdlMessage *msg) {
76         char *ptr;
77         int linelen;
78
79         /* If this isn't the configuration room, or if this isn't a MIME
80          * message, don't bother.
81          */
82         if (strcasecmp(msg->cm_fields['O'], SYSCONFIGROOM)) return(0);
83         if (msg->cm_format_type != 4) return(0);
84
85         ptr = msg->cm_fields['M'];
86         while (ptr != NULL) {
87         
88                 linelen = strcspn(ptr, "\n");
89                 if (linelen == 0) return(0);    /* end of headers */    
90                 
91                 if ( (!strncasecmp(ptr, "Content-type: ", 14))
92                    && (!strncasecmp(&ptr[14], INTERNETCFG,
93                    strlen(INTERNETCFG) )) ) {
94                         /* Bingo!  The user is changing configs.
95                          */
96                         inetcfg_setTo(msg);
97                 }
98
99                 ptr = strchr((char *)ptr, '\n');
100                 if (ptr != NULL) ++ptr;
101         }
102
103         return(0);
104 }
105
106
107 void inetcfg_init_backend(long msgnum, void *userdata) {
108         struct CtdlMessage *msg;
109
110         msg = CtdlFetchMessage(msgnum);
111         if (msg != NULL) {
112                 inetcfg_setTo(msg);
113                 CtdlFreeMessage(msg);
114         }
115 }
116
117
118 void inetcfg_init(void) {
119         if (getroom(&CC->quickroom, SYSCONFIGROOM) != 0) return;
120         CtdlForEachMessage(MSGS_LAST, 1, (-127), INTERNETCFG, NULL,
121                 inetcfg_init_backend, NULL);
122 }
123
124
125
126
127 /*****************************************************************************/
128 /*                      MODULE INITIALIZATION STUFF                          */
129 /*****************************************************************************/
130
131
132 char *Dynamic_Module_Init(void)
133 {
134         CtdlRegisterMessageHook(inetcfg_aftersave, EVT_AFTERSAVE);
135         inetcfg_init();
136         return "$Id$";
137 }
138