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