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