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