Created stub for REPORT method
[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         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 {
108         char buf[SIZ];
109         StrBuf *Buf;
110         HashPos *where;
111         void *vNode;
112         NodeConf *Node;
113         const char *Key;
114         long KeyLen;
115
116         serv_puts("CONF putsys|application/x-citadel-ignet-config");
117         serv_getln(buf, sizeof buf);
118         if (buf[0] == '4') {
119                 if ((Nodelist != NULL) && (GetCount(Nodelist) > 0)) {
120                         where = GetNewHashPos(Nodelist, 0);
121                         Buf = NewStrBuf();
122                         while (GetNextHashPos(Nodelist, where, &KeyLen, &Key, &vNode)) {
123                                 Node = (NodeConf*) vNode;
124                                 if (Node->DeleteMe==0) { 
125                                         SerializeNode(Node, Buf);
126                                         serv_putbuf(Buf);
127                                 }
128                         }
129                         FreeStrBuf(&Buf);
130                         DeleteHashPos(&where);
131                 }
132                 serv_puts("000");
133         }
134 }
135
136
137
138 /*----------------------------------------------------------------------*/
139 /*              WEB Handlers                                            */
140 /*----------------------------------------------------------------------*/
141
142
143
144 /*
145  * edit a network node
146  */
147 void edit_node(void) {
148         HashList *NodeConfig;
149         const StrBuf *Index;
150         NodeConf *NewNode;
151
152         if (havebstr("ok_button")) {
153                 Index = sbstr("index");
154                 NewNode = HttpGetNewNode();
155                 if ((NewNode == NULL) || (Index == NULL)) {
156                         AppendImportantMessage(_("Invalid Parameter"), -1);
157                         url_do_template();
158                         return;
159                 }
160                         
161                 NodeConfig = load_netconf(NULL, &NoCtx);
162                 Put(NodeConfig, ChrPtr(Index), StrLength(Index), NewNode, DeleteNodeConf);
163                 save_net_conf(NodeConfig);
164                 DeleteHash(&NodeConfig);
165         }
166         url_do_template();
167 }
168
169
170 /*
171  * modify an existing node
172  */
173 void display_edit_node(void)
174 {
175         WCTemplputParams SubTP;
176         HashList *NodeConfig;
177         const StrBuf *Index;
178         void *vNode;
179         const StrBuf *Tmpl;
180
181         Index = sbstr("index");
182         if (Index == NULL) {
183                 AppendImportantMessage(_("Invalid Parameter"), -1);
184                 url_do_template();
185                 return;
186         }
187
188         NodeConfig = load_netconf(NULL, &NoCtx);
189         if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || 
190             (vNode == NULL)) {
191                 AppendImportantMessage(_("Invalid Parameter"), -1);
192                 url_do_template();
193                 DeleteHash(&NodeConfig);
194                 return;
195         }
196         
197         memset(&SubTP, 0, sizeof(WCTemplputParams));
198         SubTP.Filter.ContextType = CTX_NODECONF;
199         SubTP.Context = vNode;
200         begin_burst();
201         Tmpl = sbstr("template");
202         output_headers(1, 0, 0, 0, 1, 0);
203         DoTemplate(SKEY(Tmpl), NULL, &SubTP);
204         end_burst();                                                                               
205         DeleteHash(&NodeConfig);
206         
207 }
208
209
210 /*
211  * display all configured nodes
212  */
213 void display_netconf(void)
214 {
215         wDumpContent(1);
216 }
217
218 /*
219  * display the dialog to verify the deletion
220  */
221 void display_confirm_delete_node(void)
222 {
223         wDumpContent(1);
224 }
225
226
227 /*
228  * actually delete the node
229  */
230 void delete_node(void)
231 {
232         HashList *NodeConfig;
233         const StrBuf *Index;
234         NodeConf *Node;
235         void *vNode;
236
237         Index = sbstr("index");
238         if (Index == NULL) {
239                 AppendImportantMessage(_("Invalid Parameter"), -1);
240                 url_do_template();
241                 return;
242         }
243
244         NodeConfig = load_netconf(NULL, &NoCtx);
245         if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || 
246             (vNode == NULL)) {
247                 AppendImportantMessage(_("Invalid Parameter"), -1);
248                 url_do_template();
249                 DeleteHash(&NodeConfig);
250                 return;
251         }
252         Node = (NodeConf *) vNode;
253         Node->DeleteMe = 1;
254         save_net_conf(NodeConfig);
255         DeleteHash(&NodeConfig);
256         
257         url_do_template();
258
259 }
260
261
262 void tmplput_NodeName(StrBuf *Target, WCTemplputParams *TP)
263 {
264         NodeConf *Node = (NodeConf*) CTX;       
265         StrBufAppendTemplate(Target, TP, Node->NodeName, 0);
266 }
267
268 void tmplput_Secret(StrBuf *Target, WCTemplputParams *TP)
269 {
270         NodeConf *Node = (NodeConf*) CTX;
271         StrBufAppendTemplate(Target, TP, Node->Secret, 0);
272 }
273
274 void tmplput_Host(StrBuf *Target, WCTemplputParams *TP) 
275 {
276         NodeConf *Node= (NodeConf*) CTX;
277         StrBufAppendTemplate(Target, TP, Node->Host, 0);
278 }
279
280 void tmplput_Port(StrBuf *Target, WCTemplputParams *TP)
281 {
282         NodeConf *Node= (NodeConf*) CTX;
283         StrBufAppendTemplate(Target, TP, Node->Port, 0);
284 }
285
286 void 
287 InitModule_NETCONF
288 (void)
289 {
290         WebcitAddUrlHandler(HKEY("display_edit_node"), "", 0, display_edit_node, 0);
291
292         WebcitAddUrlHandler(HKEY("aide_ignetconf_edit_node"), "", 0, edit_node, 0);
293         WebcitAddUrlHandler(HKEY("display_netconf"), "", 0, display_netconf, 0);
294         WebcitAddUrlHandler(HKEY("display_confirm_delete_node"), "", 0, display_confirm_delete_node, 0);
295         WebcitAddUrlHandler(HKEY("delete_node"), "", 0, delete_node, 0);
296
297                                                                                           
298         RegisterNamespace("CFG:IGNET:NODE", 0, 1, tmplput_NodeName, NULL, CTX_NODECONF);
299         RegisterNamespace("CFG:IGNET:SECRET", 0, 1, tmplput_Secret, NULL, CTX_NODECONF);
300         RegisterNamespace("CFG:IGNET:HOST", 0, 1, tmplput_Host, NULL, CTX_NODECONF);
301         RegisterNamespace("CFG:IGNET:PORT", 0, 1, tmplput_Port, NULL, CTX_NODECONF);
302
303         RegisterIterator("NODECONFIG", 0, NULL, load_netconf, NULL, DeleteHash, CTX_NODECONF, CTX_NONE, IT_NOFLAG);
304 }