* Attempting to add a smart host in webcit was instead adding it as an RBL host....
[citadel.git] / webcit / netconf.c
blobc8add93118d324e876e0a37d2fdce673ba9a5436
1 /*
2 * $Id$
3 */
4 /**
5 * \defgroup NetShareConf Functions which handle network and sharing configuration.
7 * \ingroup CitadelConfig
8 */
9 /*@{*/
10 #include "webcit.h"
12 void display_netconf(void);
14 /*----------------------------------------------------------------------*/
15 /* Business Logic */
16 /*----------------------------------------------------------------------*/
18 typedef struct _nodeconf {
19 int DeleteMe;
20 StrBuf *NodeName;
21 StrBuf *Secret;
22 StrBuf *Host;
23 StrBuf *Port;
24 }NodeConf;
26 void DeleteNodeConf(void *vNode)
28 NodeConf *Node = (NodeConf*) vNode;
29 FreeStrBuf(&Node->NodeName);
30 FreeStrBuf(&Node->Secret);
31 FreeStrBuf(&Node->Host);
32 FreeStrBuf(&Node->Port);
33 free(Node);
36 NodeConf *NewNode(StrBuf *SerializedNode)
38 NodeConf *Node;
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;
55 NodeConf *HttpGetNewNode(void)
57 NodeConf *Node;
59 if (!havebstr("node") ||
60 !havebstr("secret")||
61 !havebstr("host")||
62 !havebstr("port"))
63 return NULL;
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;
74 void SerializeNode(NodeConf *Node, StrBuf *Buf)
76 StrBufPrintf(Buf, "%s|%s|%s|%s",
77 ChrPtr(Node->NodeName),
78 ChrPtr(Node->Secret),
79 ChrPtr(Node->Host),
80 ChrPtr(Node->Port));
84 HashList *load_netconf(StrBuf *Target, WCTemplputParams *TP)
86 StrBuf *Buf;
87 HashList *Hash;
88 char nnn[64];
89 char buf[SIZ];
90 long len;
91 int nUsed;
92 NodeConf *Node;
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);
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);
109 FreeStrBuf(&Buf);
110 return Hash;
112 return NULL;
117 void save_net_conf(HashList *Nodelist)
119 char buf[SIZ];
120 StrBuf *Buf;
121 HashPos *where;
122 void *vNode;
123 NodeConf *Node;
124 const char *Key;
125 long KeyLen;
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);
140 FreeStrBuf(&Buf);
142 serv_puts("000");
148 /*----------------------------------------------------------------------*/
149 /* WEB Handlers */
150 /*----------------------------------------------------------------------*/
155 * \brief edit a network node
157 void edit_node(void) {
158 HashList *NodeConfig;
159 const StrBuf *Index;
160 NodeConf *NewNode;
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;
171 NodeConfig = load_netconf(NULL, &NoCtx);
172 Put(NodeConfig, ChrPtr(Index), StrLength(Index), NewNode, DeleteNodeConf);
173 save_net_conf(NodeConfig);
174 DeleteHash(&NodeConfig);
176 url_do_template();
181 * \brief modify an existing node
183 void display_edit_node(void)
185 WCTemplputParams SubTP;
186 HashList *NodeConfig;
187 const StrBuf *Index;
188 void *vNode;
189 const StrBuf *Tmpl;
191 Index = sbstr("index");
192 if (Index == NULL) {
193 sprintf(WC->ImportantMessage, _("Invalid Parameter"));
194 url_do_template();
195 return;
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;
207 memset(&SubTP, 0, sizeof(WCTemplputParams));
208 SVPutBuf("ITERATE:KEY", Index, 1);
209 SubTP.Filter.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);
222 * \brief display all configured nodes
224 void display_netconf(void)
226 wDumpContent(1);
230 * \brief display the dialog to verify the deletion
232 void display_confirm_delete_node(void)
234 wDumpContent(1);
239 * \brief actually delete the node
241 void delete_node(void)
243 HashList *NodeConfig;
244 const StrBuf *Index;
245 NodeConf *Node;
246 void *vNode;
248 Index = sbstr("index");
249 if (Index == NULL) {
250 sprintf(WC->ImportantMessage, _("Invalid Parameter"));
251 url_do_template();
252 return;
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;
263 Node = (NodeConf *) vNode;
264 Node->DeleteMe = 1;
265 save_net_conf(NodeConfig);
266 DeleteHash(&NodeConfig);
268 url_do_template();
273 void tmplput_NodeName(StrBuf *Target, WCTemplputParams *TP)
275 NodeConf *Node = (NodeConf*) CTX;
276 StrBufAppendTemplate(Target, TP, Node->NodeName, 0);
279 void tmplput_Secret(StrBuf *Target, WCTemplputParams *TP)
281 NodeConf *Node = (NodeConf*) CTX;
282 StrBufAppendTemplate(Target, TP, Node->Secret, 0);
285 void tmplput_Host(StrBuf *Target, WCTemplputParams *TP)
287 NodeConf *Node= (NodeConf*) CTX;
288 StrBufAppendTemplate(Target, TP, Node->Host, 0);
291 void tmplput_Port(StrBuf *Target, WCTemplputParams *TP)
293 NodeConf *Node= (NodeConf*) CTX;
294 StrBufAppendTemplate(Target, TP, Node->Port, 0);
297 void
298 InitModule_NETCONF
299 (void)
301 WebcitAddUrlHandler(HKEY("display_edit_node"), display_edit_node, 0);
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);
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);
314 RegisterIterator("NODECONFIG", 0, NULL, load_netconf, NULL, DeleteHash, CTX_NODECONF, CTX_NONE, IT_NOFLAG);
316 /*@}*/