Integrated the DKIM signer into serv_smtpclient, but disabled it
[citadel.git] / webcit / netconf.c
1 // Copyright (c) 1999-2004 by the citadel.org team
2 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
3
4
5 #include "webcit.h"
6
7 void display_netconf(void);
8
9 CtxType CTX_NODECONF = CTX_NONE;
10 /*----------------------------------------------------------------------*/
11 /*              Business Logic                                          */
12 /*----------------------------------------------------------------------*/
13
14 typedef struct _nodeconf {
15         int DeleteMe;
16         StrBuf *NodeName;
17         StrBuf *Secret;
18         StrBuf *Host;
19         StrBuf *Port;
20 } NodeConf;
21
22 void DeleteNodeConf(void *vNode) {
23         NodeConf *Node = (NodeConf*) vNode;
24         FreeStrBuf(&Node->NodeName);
25         FreeStrBuf(&Node->Secret);
26         FreeStrBuf(&Node->Host);
27         FreeStrBuf(&Node->Port);
28         free(Node);
29 }
30
31 NodeConf *NewNode(StrBuf *SerializedNode) {
32         NodeConf *Node;
33
34         if (StrLength(SerializedNode) < 8) 
35                 return NULL; /* we need at least 4 pipes and some other text so its invalid. */
36         Node = (NodeConf *) malloc(sizeof(NodeConf));
37         Node->DeleteMe = 0;
38         Node->NodeName=NewStrBuf();
39         StrBufExtract_token(Node->NodeName, SerializedNode, 0, '|');
40         Node->Secret=NewStrBuf();
41         StrBufExtract_token(Node->Secret, SerializedNode, 1, '|');
42         Node->Host=NewStrBuf();
43         StrBufExtract_token(Node->Host, SerializedNode, 2, '|');
44         Node->Port=NewStrBuf();
45         StrBufExtract_token(Node->Port, SerializedNode, 3, '|');
46         return Node;
47 }
48
49 NodeConf *HttpGetNewNode(void) {
50         NodeConf *Node;
51
52         if (!havebstr("node") || 
53             !havebstr("secret")||
54             !havebstr("host")||
55             !havebstr("port"))
56                 return NULL;
57
58         Node = (NodeConf *) malloc(sizeof(NodeConf));
59         Node->DeleteMe = 0;
60         Node->NodeName = NewStrBufDup(sbstr("node"));
61         Node->Secret = NewStrBufDup(sbstr("secret"));
62         Node->Host = NewStrBufDup(sbstr("host"));
63         Node->Port = NewStrBufDup(sbstr("port"));
64         return Node;
65 }
66
67 void SerializeNode(NodeConf *Node, StrBuf *Buf) {
68         StrBufPrintf(Buf, "%s|%s|%s|%s", 
69                      ChrPtr(Node->NodeName),
70                      ChrPtr(Node->Secret),
71                      ChrPtr(Node->Host),
72                      ChrPtr(Node->Port));
73 }
74
75
76 HashList *load_netconf(StrBuf *Target, WCTemplputParams *TP) {
77         StrBuf *Buf;
78         HashList *Hash;
79         char nnn[64];
80         char buf[SIZ];
81         int nUsed;
82         NodeConf *Node;
83
84         serv_puts("CONF getsys|application/x-citadel-ignet-config");
85         serv_getln(buf, sizeof buf);
86         if (buf[0] == '1') {
87                 Hash = NewHash(1, NULL);
88
89                 Buf = NewStrBuf();
90                 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
91                         Node = NewNode(Buf);
92                         if (Node != NULL) {
93                                 nUsed = GetCount(Hash);
94                                 nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
95                                 Put(Hash, nnn, nUsed, Node, DeleteNodeConf); 
96                         }
97                 }
98                 FreeStrBuf(&Buf);
99                 return Hash;
100         }
101         return NULL;
102 }
103
104
105
106 void save_net_conf(HashList *Nodelist) {
107         char buf[SIZ];
108         StrBuf *Buf;
109         HashPos *where;
110         void *vNode;
111         NodeConf *Node;
112         const char *Key;
113         long KeyLen;
114
115         serv_puts("CONF putsys|application/x-citadel-ignet-config");
116         serv_getln(buf, sizeof buf);
117         if (buf[0] == '4') {
118                 if ((Nodelist != NULL) && (GetCount(Nodelist) > 0)) {
119                         where = GetNewHashPos(Nodelist, 0);
120                         Buf = NewStrBuf();
121                         while (GetNextHashPos(Nodelist, where, &KeyLen, &Key, &vNode)) {
122                                 Node = (NodeConf*) vNode;
123                                 if (Node->DeleteMe==0) { 
124                                         SerializeNode(Node, Buf);
125                                         serv_putbuf(Buf);
126                                 }
127                         }
128                         FreeStrBuf(&Buf);
129                         DeleteHashPos(&where);
130                 }
131                 serv_puts("000");
132         }
133 }
134
135
136
137 /*----------------------------------------------------------------------*/
138 /*              WEB Handlers                                            */
139 /*----------------------------------------------------------------------*/
140
141
142
143 /*
144  * edit a network node
145  */
146 void edit_node(void) {
147         HashList *NodeConfig;
148         const StrBuf *Index;
149         NodeConf *NewNode;
150
151         if (havebstr("ok_button")) {
152                 Index = sbstr("index");
153                 NewNode = HttpGetNewNode();
154                 if ((NewNode == NULL) || (Index == NULL)) {
155                         AppendImportantMessage(_("Invalid Parameter"), -1);
156                         url_do_template();
157                         return;
158                 }
159                         
160                 NodeConfig = load_netconf(NULL, &NoCtx);
161                 Put(NodeConfig, ChrPtr(Index), StrLength(Index), NewNode, DeleteNodeConf);
162                 save_net_conf(NodeConfig);
163                 DeleteHash(&NodeConfig);
164         }
165         url_do_template();
166 }
167
168
169 /*
170  * modify an existing node
171  */
172 void display_edit_node(void) {
173         WCTemplputParams SubTP;
174         HashList *NodeConfig;
175         const StrBuf *Index;
176         void *vNode;
177         const StrBuf *Tmpl;
178
179         Index = sbstr("index");
180         if (Index == NULL) {
181                 AppendImportantMessage(_("Invalid Parameter"), -1);
182                 url_do_template();
183                 return;
184         }
185
186         NodeConfig = load_netconf(NULL, &NoCtx);
187         if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || 
188             (vNode == NULL)) {
189                 AppendImportantMessage(_("Invalid Parameter"), -1);
190                 url_do_template();
191                 DeleteHash(&NodeConfig);
192                 return;
193         }
194         StackContext(NULL, &SubTP, vNode, CTX_NODECONF, 0, NULL);
195         {
196                 begin_burst();
197                 Tmpl = sbstr("template");
198                 output_headers(1, 0, 0, 0, 1, 0);
199                 DoTemplate(SKEY(Tmpl), NULL, &SubTP);
200                 end_burst();
201         }
202         UnStackContext(&SubTP);
203         DeleteHash(&NodeConfig);
204         
205 }
206
207
208 /*
209  * display all configured nodes
210  */
211 void display_netconf(void) {
212         wDumpContent(1);
213 }
214
215 /*
216  * display the dialog to verify the deletion
217  */
218 void display_confirm_delete_node(void) {
219         wDumpContent(1);
220 }
221
222
223 /*
224  * actually delete the node
225  */
226 void delete_node(void) {
227         HashList *NodeConfig;
228         const StrBuf *Index;
229         NodeConf *Node;
230         void *vNode;
231
232         Index = sbstr("index");
233         if (Index == NULL) {
234                 AppendImportantMessage(_("Invalid Parameter"), -1);
235                 url_do_template();
236                 return;
237         }
238
239         NodeConfig = load_netconf(NULL, &NoCtx);
240         if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || 
241             (vNode == NULL)) {
242                 AppendImportantMessage(_("Invalid Parameter"), -1);
243                 url_do_template();
244                 DeleteHash(&NodeConfig);
245                 return;
246         }
247         Node = (NodeConf *) vNode;
248         Node->DeleteMe = 1;
249         save_net_conf(NodeConfig);
250         DeleteHash(&NodeConfig);
251         
252         url_do_template();
253
254 }
255
256
257 void tmplput_NodeName(StrBuf *Target, WCTemplputParams *TP) {
258         NodeConf *Node = (NodeConf*) CTX(CTX_NODECONF); 
259         StrBufAppendTemplate(Target, TP, Node->NodeName, 0);
260 }
261
262 void tmplput_Secret(StrBuf *Target, WCTemplputParams *TP) {
263         NodeConf *Node = (NodeConf*) CTX(CTX_NODECONF);
264         StrBufAppendTemplate(Target, TP, Node->Secret, 0);
265 }
266
267 void tmplput_Host(StrBuf *Target, WCTemplputParams *TP) {
268         NodeConf *Node= (NodeConf*) CTX(CTX_NODECONF);
269         StrBufAppendTemplate(Target, TP, Node->Host, 0);
270 }
271
272 void tmplput_Port(StrBuf *Target, WCTemplputParams *TP) {
273         NodeConf *Node= (NodeConf*) CTX(CTX_NODECONF);
274         StrBufAppendTemplate(Target, TP, Node->Port, 0);
275 }
276
277 void 
278 InitModule_NETCONF
279 (void)
280 {
281         RegisterCTX(CTX_NODECONF);
282         WebcitAddUrlHandler(HKEY("display_edit_node"), "", 0, display_edit_node, 0);
283
284         WebcitAddUrlHandler(HKEY("aide_ignetconf_edit_node"), "", 0, edit_node, 0);
285         WebcitAddUrlHandler(HKEY("display_netconf"), "", 0, display_netconf, 0);
286         WebcitAddUrlHandler(HKEY("display_confirm_delete_node"), "", 0, display_confirm_delete_node, 0);
287         WebcitAddUrlHandler(HKEY("delete_node"), "", 0, delete_node, 0);
288
289                                                                                           
290         RegisterNamespace("CFG:IGNET:NODE", 0, 1, tmplput_NodeName, NULL, CTX_NODECONF);
291         RegisterNamespace("CFG:IGNET:SECRET", 0, 1, tmplput_Secret, NULL, CTX_NODECONF);
292         RegisterNamespace("CFG:IGNET:HOST", 0, 1, tmplput_Host, NULL, CTX_NODECONF);
293         RegisterNamespace("CFG:IGNET:PORT", 0, 1, tmplput_Port, NULL, CTX_NODECONF);
294
295         RegisterIterator("NODECONFIG", 0, NULL, load_netconf, NULL, DeleteHash, CTX_NODECONF, CTX_NONE, IT_NOFLAG);
296 }