* add custom notification targets config interface
[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                 eName = sbstr("ename");
132                 if (eName == NULL) {
133                         StrBufPrintf(WCC->ImportantMsg, _("Invalid Parameter"));
134                         url_do_template();
135                         return;
136                 }
137
138                 nUsed = GetCount(Hash);
139                 nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
140         
141                 Put(Hash, nnn, nUsed, NewStrBufDup(eName), HFreeStrBuf); 
142                 StrBufPrintf(WCC->ImportantMsg, "%s %s", 
143                              /*<domain> added status message*/ _("added."), 
144                              ChrPtr(eName));
145         }
146
147         Buf = NewStrBuf();
148         serv_printf("CONF PUTSYS|application/x-citadel-internet-config");
149         StrBuf_ServGetln(Buf);
150         if (GetServerStatus(Buf, NULL) == 4) {
151                 for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStrBuf)); i++) {
152                         HashPos *where;
153                         const char *Key;
154                         long KeyLen;
155
156                         GetHash(WCC->InetCfg, CfgNames[i].name, CfgNames[i].len, &vHash);
157                         Hash = (HashList*) vHash;
158                         if (Hash == NULL) {
159                                 sprintf(WC->ImportantMessage, _("Invalid Parameter"));
160                                 url_do_template();
161                                 return;
162                         }
163                         if (GetCount(Hash) > 0) {
164                                 where = GetNewHashPos(Hash, 0);
165                                 while (GetNextHashPos(Hash, where, &KeyLen, &Key, &vStr)) {
166                                         Str = (StrBuf*) vStr;
167                                         if ((Str!= NULL) && (StrLength(Str) > 0))
168                                                 serv_printf("%s|%s", 
169                                                             ChrPtr(Str),
170                                                             CfgNames[i].name); 
171                                 }
172                                 DeleteHashPos(&where);
173                         }                       
174                 }
175                 serv_puts("000");
176                 DeleteHash(&WCC->InetCfg);
177         }
178         FreeStrBuf(&Buf);
179         url_do_template();
180 }
181
182 void InetCfgSubst(StrBuf *TemplBuffer, WCTemplputParams *TP)
183 {
184         SVPutBuf("SERVCFG:INET:HOSTNAME", CTX, 1);
185 }
186
187 void DeleteInetConfHash(StrBuf *Target, WCTemplputParams *TP)
188 {
189         wcsession *WCC = WC;
190
191         if (WCC->InetCfg != NULL)
192                 DeleteHash(&WCC->InetCfg);
193
194 }
195
196
197 HashList *GetInetConfHash(StrBuf *Target, WCTemplputParams *TP)
198 {
199         wcsession *WCC = WC;
200         void *vHash;
201
202         if (WCC->InetCfg == NULL)
203                 load_inetconf();
204         GetHash(WCC->InetCfg, TKEY(5), &vHash);
205         svprintf(HKEY("SERVCFG:INET:TYPE"), WCS_STRING, TP->Tokens->Params[5]->Start);
206         return vHash;
207 }
208
209 void 
210 InitModule_INETCONF
211 (void)
212 {
213         WebcitAddUrlHandler(HKEY("save_inetconf"), new_save_inetconf, 0);
214         RegisterIterator("SERVCFG:INET", 1, NULL, GetInetConfHash, InetCfgSubst, NULL, CTX_INETCFG, CTX_NONE, IT_NOFLAG);
215         RegisterNamespace("SERVCFG:FLUSHINETCFG",0, 0, DeleteInetConfHash, CTX_NONE);
216 }