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