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