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