Mailing list header changes (fuck you Google)
[citadel.git] / webcit / netconf.c
1 #include "webcit.h"
2
3 void display_netconf(void);
4
5 CtxType CTX_NODECONF = CTX_NONE;
6 /*----------------------------------------------------------------------*/
7 /*              Business Logic                                          */
8 /*----------------------------------------------------------------------*/
9
10 typedef struct _nodeconf {
11         int DeleteMe;
12         StrBuf *NodeName;
13         StrBuf *Secret;
14         StrBuf *Host;
15         StrBuf *Port;
16 }NodeConf;
17
18 void DeleteNodeConf(void *vNode)
19 {
20         NodeConf *Node = (NodeConf*) vNode;
21         FreeStrBuf(&Node->NodeName);
22         FreeStrBuf(&Node->Secret);
23         FreeStrBuf(&Node->Host);
24         FreeStrBuf(&Node->Port);
25         free(Node);
26 }
27
28 NodeConf *NewNode(StrBuf *SerializedNode)
29 {
30         NodeConf *Node;
31
32         if (StrLength(SerializedNode) < 8) 
33                 return NULL; /** we need at least 4 pipes and some other text so its invalid. */
34         Node = (NodeConf *) malloc(sizeof(NodeConf));
35         Node->DeleteMe = 0;
36         Node->NodeName=NewStrBuf();
37         StrBufExtract_token(Node->NodeName, SerializedNode, 0, '|');
38         Node->Secret=NewStrBuf();
39         StrBufExtract_token(Node->Secret, SerializedNode, 1, '|');
40         Node->Host=NewStrBuf();
41         StrBufExtract_token(Node->Host, SerializedNode, 2, '|');
42         Node->Port=NewStrBuf();
43         StrBufExtract_token(Node->Port, SerializedNode, 3, '|');
44         return Node;
45 }
46
47 NodeConf *HttpGetNewNode(void)
48 {
49         NodeConf *Node;
50
51         if (!havebstr("node") || 
52             !havebstr("secret")||
53             !havebstr("host")||
54             !havebstr("port"))
55                 return NULL;
56
57         Node = (NodeConf *) malloc(sizeof(NodeConf));
58         Node->DeleteMe = 0;
59         Node->NodeName = NewStrBufDup(sbstr("node"));
60         Node->Secret = NewStrBufDup(sbstr("secret"));
61         Node->Host = NewStrBufDup(sbstr("host"));
62         Node->Port = NewStrBufDup(sbstr("port"));
63         return Node;
64 }
65
66 void SerializeNode(NodeConf *Node, StrBuf *Buf)
67 {
68         StrBufPrintf(Buf, "%s|%s|%s|%s", 
69                      ChrPtr(Node->NodeName),
70                      ChrPtr(Node->Secret),
71                      ChrPtr(Node->Host),
72                      ChrPtr(Node->Port));
73 }
74
75
76 HashList *load_netconf(StrBuf *Target, WCTemplputParams *TP)
77 {
78         StrBuf *Buf;
79         HashList *Hash;
80         char nnn[64];
81         char buf[SIZ];
82         int nUsed;
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 (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         StackContext(NULL, &SubTP, vNode, CTX_NODECONF, 0, NULL);
198         {
199                 begin_burst();
200                 Tmpl = sbstr("template");
201                 output_headers(1, 0, 0, 0, 1, 0);
202                 DoTemplate(SKEY(Tmpl), NULL, &SubTP);
203                 end_burst();
204         }
205         UnStackContext(&SubTP);
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(CTX_NODECONF); 
266         StrBufAppendTemplate(Target, TP, Node->NodeName, 0);
267 }
268
269 void tmplput_Secret(StrBuf *Target, WCTemplputParams *TP)
270 {
271         NodeConf *Node = (NodeConf*) CTX(CTX_NODECONF);
272         StrBufAppendTemplate(Target, TP, Node->Secret, 0);
273 }
274
275 void tmplput_Host(StrBuf *Target, WCTemplputParams *TP) 
276 {
277         NodeConf *Node= (NodeConf*) CTX(CTX_NODECONF);
278         StrBufAppendTemplate(Target, TP, Node->Host, 0);
279 }
280
281 void tmplput_Port(StrBuf *Target, WCTemplputParams *TP)
282 {
283         NodeConf *Node= (NodeConf*) CTX(CTX_NODECONF);
284         StrBufAppendTemplate(Target, TP, Node->Port, 0);
285 }
286
287 void 
288 InitModule_NETCONF
289 (void)
290 {
291         RegisterCTX(CTX_NODECONF);
292         WebcitAddUrlHandler(HKEY("display_edit_node"), "", 0, display_edit_node, 0);
293
294         WebcitAddUrlHandler(HKEY("aide_ignetconf_edit_node"), "", 0, edit_node, 0);
295         WebcitAddUrlHandler(HKEY("display_netconf"), "", 0, display_netconf, 0);
296         WebcitAddUrlHandler(HKEY("display_confirm_delete_node"), "", 0, display_confirm_delete_node, 0);
297         WebcitAddUrlHandler(HKEY("delete_node"), "", 0, delete_node, 0);
298
299                                                                                           
300         RegisterNamespace("CFG:IGNET:NODE", 0, 1, tmplput_NodeName, NULL, CTX_NODECONF);
301         RegisterNamespace("CFG:IGNET:SECRET", 0, 1, tmplput_Secret, NULL, CTX_NODECONF);
302         RegisterNamespace("CFG:IGNET:HOST", 0, 1, tmplput_Host, NULL, CTX_NODECONF);
303         RegisterNamespace("CFG:IGNET:PORT", 0, 1, tmplput_Port, NULL, CTX_NODECONF);
304
305         RegisterIterator("NODECONFIG", 0, NULL, load_netconf, NULL, DeleteHash, CTX_NODECONF, CTX_NONE, IT_NOFLAG);
306 }