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