4adb07e09cdf685feac1b0849ba40eeb7b5b0444
[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                         strcmp(ChrPtr(Buf), "000"))) {
73                         Value = NewStrBuf();
74  
75                         StrBufExtract_token(CfgToken, Buf, 1, '|');
76                         StrBufExtract_token(Value, Buf, 0, '|');
77                         GetHash(WCC->InetCfg, ChrPtr(CfgToken), StrLength(CfgToken), &vHash);
78                         Hash = (HashList*) vHash;
79                         if (Hash == NULL) {
80                                 lprintf(1, "ERROR Loading inet config line: [%s]\n", 
81                                         ChrPtr(Buf));
82                                 FreeStrBuf(&Value);
83                                 continue;
84                         }
85                         nUsed = GetCount(Hash);
86                         nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
87                         Put(Hash, nnn, nUsed, Value, HFreeStrBuf); 
88                 }
89                 FreeStrBuf(&CfgToken);
90         }
91         FreeStrBuf(&Buf);
92 }
93
94
95 /*
96  * save changes to the inet config
97  */
98 void new_save_inetconf(void) {
99         wcsession *WCC = WC;
100         HashList *Hash;
101         StrBuf *Str;
102         StrBuf *Buf;
103         const StrBuf *eType, *eNum, *eName;
104         char nnn[64];
105         void *vHash, *vStr;
106         int i, nUsed;
107
108         load_inetconf();
109         eType = sbstr("etype");
110
111         GetHash(WCC->InetCfg, ChrPtr(eType), StrLength(eType), &vHash);
112         Hash = (HashList*) vHash;
113         if (Hash == NULL) {
114                 StrBufPrintf(WCC->ImportantMsg, _("Invalid Parameter"));
115                 url_do_template();
116                 return;
117         }
118
119         if (strcasecmp(bstr("oper"), "delete") == 0) {
120                 eNum = sbstr("ename");
121                 if (!GetHash(Hash, ChrPtr(eNum), StrLength(eNum), &vStr) ||
122                     (vStr == NULL)) {
123                         StrBufPrintf(WCC->ImportantMsg, _("Invalid Parameter"));
124                         url_do_template();
125                         return;
126                 }
127
128                 Str = (StrBuf*)vStr;
129                 StrBufPrintf(WCC->ImportantMsg, _("%s has been deleted."), ChrPtr(Str));
130                 FlushStrBuf(Str);       
131         }
132         else if (!strcasecmp(bstr("oper"), "add")) {
133                 StrBuf *name;
134                 eName = sbstr("ename");
135                 if (eName == NULL) {
136                         StrBufPrintf(WCC->ImportantMsg, _("Invalid Parameter"));
137                         url_do_template();
138                         return;
139                 }
140
141                 nUsed = GetCount(Hash);
142                 nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
143                 name = NewStrBufDup(eName);
144                 StrBufTrim(name);
145                 Put(Hash, nnn, nUsed, name, HFreeStrBuf); 
146                 StrBufPrintf(WCC->ImportantMsg, "%s %s", 
147                              /*<domain> added status message*/ _("added."), 
148                              ChrPtr(eName));
149         }
150
151         Buf = NewStrBuf();
152         serv_printf("CONF PUTSYS|application/x-citadel-internet-config");
153         StrBuf_ServGetln(Buf);
154         if (GetServerStatus(Buf, NULL) == 4) {
155                 for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStrBuf)); i++) {
156                         HashPos *where;
157                         const char *Key;
158                         long KeyLen;
159
160                         GetHash(WCC->InetCfg, CfgNames[i].name, CfgNames[i].len, &vHash);
161                         Hash = (HashList*) vHash;
162                         if (Hash == NULL) {
163                                 sprintf(WC->ImportantMessage, _("Invalid Parameter"));
164                                 url_do_template();
165                                 return;
166                         }
167                         if (GetCount(Hash) > 0) {
168                                 where = GetNewHashPos(Hash, 0);
169                                 while (GetNextHashPos(Hash, where, &KeyLen, &Key, &vStr)) {
170                                         Str = (StrBuf*) vStr;
171                                         if ((Str!= NULL) && (StrLength(Str) > 0))
172                                                 serv_printf("%s|%s", 
173                                                             ChrPtr(Str),
174                                                             CfgNames[i].name); 
175                                 }
176                                 DeleteHashPos(&where);
177                         }                       
178                 }
179                 serv_puts("000");
180                 DeleteHash(&WCC->InetCfg);
181         }
182         FreeStrBuf(&Buf);
183         url_do_template();
184 }
185
186 void DeleteInetConfHash(StrBuf *Target, WCTemplputParams *TP)
187 {
188         wcsession *WCC = WC;
189
190         if (WCC->InetCfg != NULL)
191                 DeleteHash(&WCC->InetCfg);
192
193 }
194
195
196 HashList *GetInetConfHash(StrBuf *Target, WCTemplputParams *TP)
197 {
198         wcsession *WCC = WC;
199         void *vHash;
200
201         if (WCC->InetCfg == NULL)
202                 load_inetconf();
203         GetHash(WCC->InetCfg, TKEY(5), &vHash);
204         PutBstr(HKEY("__SERVCFG:INET:TYPE"), NewStrBufPlain(TKEY(5)));
205         return vHash;
206 }
207
208 void 
209 InitModule_INETCONF
210 (void)
211 {
212         WebcitAddUrlHandler(HKEY("save_inetconf"), "", 0, new_save_inetconf, 0);
213         RegisterIterator("SERVCFG:INET", 1, NULL, GetInetConfHash, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
214         RegisterNamespace("SERVCFG:FLUSHINETCFG",0, 0, DeleteInetConfHash, NULL, CTX_NONE);
215 }