Remove $Id$ tags from most of webcit
[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 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("fallbackhost") },
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 DeleteInetConfHash(StrBuf *Target, WCTemplputParams *TP)
185 {
186         wcsession *WCC = WC;
187
188         if (WCC->InetCfg != NULL)
189                 DeleteHash(&WCC->InetCfg);
190
191 }
192
193
194 HashList *GetInetConfHash(StrBuf *Target, WCTemplputParams *TP)
195 {
196         wcsession *WCC = WC;
197         void *vHash;
198
199         if (WCC->InetCfg == NULL)
200                 load_inetconf();
201         GetHash(WCC->InetCfg, TKEY(5), &vHash);
202         PutBstr(HKEY("__SERVCFG:INET:TYPE"), NewStrBufPlain(TKEY(5)));
203         return vHash;
204 }
205
206 void 
207 InitModule_INETCONF
208 (void)
209 {
210         WebcitAddUrlHandler(HKEY("save_inetconf"), "", 0, new_save_inetconf, 0);
211         RegisterIterator("SERVCFG:INET", 1, NULL, GetInetConfHash, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
212         RegisterNamespace("SERVCFG:FLUSHINETCFG",0, 0, DeleteInetConfHash, NULL, CTX_NONE);
213 }