9e9a73e7737f1442229eef93c38baa003388a15c
[citadel.git] / webcit / inetconf.c
1 /* 
2  * $Id$
3  *
4  * Functions which handle Internet domain configuration etc.
5  */
6
7 #include "webcit.h"
8 #include "webserver.h"
9
10
11 typedef enum _e_cfg {
12         ic_localhost,
13         ic_directory,
14         ic_smarthost,
15         ic_fallback,
16         ic_rbl,
17         ic_spamass,
18         ic_masq,
19         ic_clamav,
20         ic_notify,
21         ic_max
22 } ECfg;
23
24 typedef struct _ConstStrBuf {
25         const char *name;
26         size_t len;
27 } ConstStrBuf;
28
29
30   /* These are server config keywords; do not localize! */
31 ConstStrBuf CfgNames[] = {
32         { HKEY("localhost") },
33         { HKEY("directory") },
34         { HKEY("smarthost") },
35         { HKEY("fallbackhost") },
36         { HKEY("rbl") },
37         { HKEY("spamassassin") },
38         { HKEY("masqdomain") },
39         { HKEY("clamav") },
40         { HKEY("notify") }
41 };
42
43         
44
45
46 /*
47  * display the inet config dialog 
48  */
49 void load_inetconf(void)
50 {
51         wcsession *WCC = WC;
52         StrBuf *Buf, *CfgToken, *Value;
53         void *vHash;
54         HashList *Hash;
55         char nnn[64];
56         int i, len, nUsed;
57         
58         WCC->InetCfg = NewHash(1, NULL);
59
60         for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStrBuf)); i++) {
61                 Hash = NewHash(1, NULL);
62                 Put(WCC->InetCfg, CfgNames[i].name, CfgNames[i].len, Hash, HDeleteHash);
63         }
64
65         serv_printf("CONF GETSYS|application/x-citadel-internet-config");
66         Buf = NewStrBuf();
67         StrBuf_ServGetln(Buf);
68                 
69         if (GetServerStatus(Buf, NULL) == 1) {
70                 CfgToken = NewStrBuf();
71                 while ((len = StrBuf_ServGetln(Buf),
72                         ((len >= 0) && 
73                          ((len != 3) ||
74                           strcmp(ChrPtr(Buf), "000")))))
75                 {
76                         Value = NewStrBuf();
77  
78                         StrBufExtract_token(CfgToken, Buf, 1, '|');
79                         StrBufExtract_token(Value, Buf, 0, '|');
80                         GetHash(WCC->InetCfg, ChrPtr(CfgToken), StrLength(CfgToken), &vHash);
81                         Hash = (HashList*) vHash;
82                         if (Hash == NULL) {
83                                 lprintf(1, "ERROR Loading inet config line: [%s]\n", 
84                                         ChrPtr(Buf));
85                                 FreeStrBuf(&Value);
86                                 continue;
87                         }
88                         nUsed = GetCount(Hash);
89                         nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
90                         Put(Hash, nnn, nUsed, Value, HFreeStrBuf); 
91                 }
92                 FreeStrBuf(&CfgToken);
93         }
94         FreeStrBuf(&Buf);
95 }
96
97
98 /*
99  * save changes to the inet config
100  */
101 void new_save_inetconf(void) {
102         wcsession *WCC = WC;
103         HashList *Hash;
104         StrBuf *Str;
105         StrBuf *Buf;
106         const StrBuf *eType, *eNum, *eName;
107         char nnn[64];
108         void *vHash, *vStr;
109         int i, nUsed;
110
111         load_inetconf();
112         eType = sbstr("etype");
113
114         GetHash(WCC->InetCfg, ChrPtr(eType), StrLength(eType), &vHash);
115         Hash = (HashList*) vHash;
116         if (Hash == NULL) {
117                 StrBufPrintf(WCC->ImportantMsg, _("Invalid Parameter"));
118                 url_do_template();
119                 return;
120         }
121
122         if (strcasecmp(bstr("oper"), "delete") == 0) {
123                 eNum = sbstr("ename");
124                 if (!GetHash(Hash, ChrPtr(eNum), StrLength(eNum), &vStr) ||
125                     (vStr == NULL)) {
126                         StrBufPrintf(WCC->ImportantMsg, _("Invalid Parameter"));
127                         url_do_template();
128                         return;
129                 }
130
131                 Str = (StrBuf*)vStr;
132                 StrBufPrintf(WCC->ImportantMsg, _("%s has been deleted."), ChrPtr(Str));
133                 FlushStrBuf(Str);       
134         }
135         else if (!strcasecmp(bstr("oper"), "add")) {
136                 StrBuf *name;
137                 eName = sbstr("ename");
138                 if (eName == NULL) {
139                         StrBufPrintf(WCC->ImportantMsg, _("Invalid Parameter"));
140                         url_do_template();
141                         return;
142                 }
143
144                 nUsed = GetCount(Hash);
145                 nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
146                 name = NewStrBufDup(eName);
147                 StrBufTrim(name);
148                 Put(Hash, nnn, nUsed, name, HFreeStrBuf); 
149                 StrBufPrintf(WCC->ImportantMsg, "%s %s", 
150                              /*<domain> added status message*/ _("added."), 
151                              ChrPtr(eName));
152         }
153
154         Buf = NewStrBuf();
155         serv_printf("CONF PUTSYS|application/x-citadel-internet-config");
156         StrBuf_ServGetln(Buf);
157         if (GetServerStatus(Buf, NULL) == 4) {
158                 for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStrBuf)); i++) {
159                         HashPos *where;
160                         const char *Key;
161                         long KeyLen;
162
163                         GetHash(WCC->InetCfg, CfgNames[i].name, CfgNames[i].len, &vHash);
164                         Hash = (HashList*) vHash;
165                         if (Hash == NULL) {
166                                 sprintf(WC->ImportantMessage, _("Invalid Parameter"));
167                                 url_do_template();
168                                 return;
169                         }
170                         if (GetCount(Hash) > 0) {
171                                 where = GetNewHashPos(Hash, 0);
172                                 while (GetNextHashPos(Hash, where, &KeyLen, &Key, &vStr)) {
173                                         Str = (StrBuf*) vStr;
174                                         if ((Str!= NULL) && (StrLength(Str) > 0))
175                                                 serv_printf("%s|%s", 
176                                                             ChrPtr(Str),
177                                                             CfgNames[i].name); 
178                                 }
179                                 DeleteHashPos(&where);
180                         }                       
181                 }
182                 serv_puts("000");
183                 DeleteHash(&WCC->InetCfg);
184         }
185         FreeStrBuf(&Buf);
186         url_do_template();
187 }
188
189 void InetCfgSubst(StrBuf *TemplBuffer, WCTemplputParams *TP)
190 {
191         SVPutBuf("SERVCFG:INET:HOSTNAME", CTX, 1);
192 }
193
194 void DeleteInetConfHash(StrBuf *Target, WCTemplputParams *TP)
195 {
196         wcsession *WCC = WC;
197
198         if (WCC->InetCfg != NULL)
199                 DeleteHash(&WCC->InetCfg);
200
201 }
202
203
204 HashList *GetInetConfHash(StrBuf *Target, WCTemplputParams *TP)
205 {
206         wcsession *WCC = WC;
207         void *vHash;
208
209         if (WCC->InetCfg == NULL)
210                 load_inetconf();
211         GetHash(WCC->InetCfg, TKEY(5), &vHash);
212         svprintf(HKEY("SERVCFG:INET:TYPE"), WCS_STRING, TP->Tokens->Params[5]->Start);
213         return vHash;
214 }
215
216 void 
217 InitModule_INETCONF
218 (void)
219 {
220         WebcitAddUrlHandler(HKEY("save_inetconf"), "", 0, new_save_inetconf, 0);
221         RegisterIterator("SERVCFG:INET", 1, NULL, GetInetConfHash, InetCfgSubst, NULL, CTX_INETCFG, CTX_NONE, IT_NOFLAG);
222         RegisterNamespace("SERVCFG:FLUSHINETCFG",0, 0, DeleteInetConfHash, NULL, CTX_NONE);
223 }