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