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