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