1 // Copyright (c) 1999-2004 by the citadel.org team
2 // This program is open source software. Use, duplication, or disclosure is subject to the GNU General Public License v3.
7 void display_netconf(void);
9 CtxType CTX_NODECONF = CTX_NONE;
10 /*----------------------------------------------------------------------*/
12 /*----------------------------------------------------------------------*/
14 typedef struct _nodeconf {
22 void DeleteNodeConf(void *vNode) {
23 NodeConf *Node = (NodeConf*) vNode;
24 FreeStrBuf(&Node->NodeName);
25 FreeStrBuf(&Node->Secret);
26 FreeStrBuf(&Node->Host);
27 FreeStrBuf(&Node->Port);
31 NodeConf *NewNode(StrBuf *SerializedNode) {
34 if (StrLength(SerializedNode) < 8)
35 return NULL; /* we need at least 4 pipes and some other text so its invalid. */
36 Node = (NodeConf *) malloc(sizeof(NodeConf));
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, '|');
49 NodeConf *HttpGetNewNode(void) {
52 if (!havebstr("node") ||
58 Node = (NodeConf *) malloc(sizeof(NodeConf));
60 Node->NodeName = NewStrBufDup(sbstr("node"));
61 Node->Secret = NewStrBufDup(sbstr("secret"));
62 Node->Host = NewStrBufDup(sbstr("host"));
63 Node->Port = NewStrBufDup(sbstr("port"));
67 void SerializeNode(NodeConf *Node, StrBuf *Buf) {
68 StrBufPrintf(Buf, "%s|%s|%s|%s",
69 ChrPtr(Node->NodeName),
76 HashList *load_netconf(StrBuf *Target, WCTemplputParams *TP) {
84 serv_puts("CONF getsys|application/x-citadel-ignet-config");
85 serv_getln(buf, sizeof buf);
87 Hash = NewHash(1, NULL);
90 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
93 nUsed = GetCount(Hash);
94 nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
95 Put(Hash, nnn, nUsed, Node, DeleteNodeConf);
106 void save_net_conf(HashList *Nodelist) {
115 serv_puts("CONF putsys|application/x-citadel-ignet-config");
116 serv_getln(buf, sizeof buf);
118 if ((Nodelist != NULL) && (GetCount(Nodelist) > 0)) {
119 where = GetNewHashPos(Nodelist, 0);
121 while (GetNextHashPos(Nodelist, where, &KeyLen, &Key, &vNode)) {
122 Node = (NodeConf*) vNode;
123 if (Node->DeleteMe==0) {
124 SerializeNode(Node, Buf);
129 DeleteHashPos(&where);
137 /*----------------------------------------------------------------------*/
139 /*----------------------------------------------------------------------*/
144 * edit a network node
146 void edit_node(void) {
147 HashList *NodeConfig;
151 if (havebstr("ok_button")) {
152 Index = sbstr("index");
153 NewNode = HttpGetNewNode();
154 if ((NewNode == NULL) || (Index == NULL)) {
155 AppendImportantMessage(_("Invalid Parameter"), -1);
160 NodeConfig = load_netconf(NULL, &NoCtx);
161 Put(NodeConfig, ChrPtr(Index), StrLength(Index), NewNode, DeleteNodeConf);
162 save_net_conf(NodeConfig);
163 DeleteHash(&NodeConfig);
170 * modify an existing node
172 void display_edit_node(void) {
173 WCTemplputParams SubTP;
174 HashList *NodeConfig;
179 Index = sbstr("index");
181 AppendImportantMessage(_("Invalid Parameter"), -1);
186 NodeConfig = load_netconf(NULL, &NoCtx);
187 if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) ||
189 AppendImportantMessage(_("Invalid Parameter"), -1);
191 DeleteHash(&NodeConfig);
194 StackContext(NULL, &SubTP, vNode, CTX_NODECONF, 0, NULL);
197 Tmpl = sbstr("template");
198 output_headers(1, 0, 0, 0, 1, 0);
199 DoTemplate(SKEY(Tmpl), NULL, &SubTP);
202 UnStackContext(&SubTP);
203 DeleteHash(&NodeConfig);
209 * display all configured nodes
211 void display_netconf(void) {
216 * display the dialog to verify the deletion
218 void display_confirm_delete_node(void) {
224 * actually delete the node
226 void delete_node(void) {
227 HashList *NodeConfig;
232 Index = sbstr("index");
234 AppendImportantMessage(_("Invalid Parameter"), -1);
239 NodeConfig = load_netconf(NULL, &NoCtx);
240 if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) ||
242 AppendImportantMessage(_("Invalid Parameter"), -1);
244 DeleteHash(&NodeConfig);
247 Node = (NodeConf *) vNode;
249 save_net_conf(NodeConfig);
250 DeleteHash(&NodeConfig);
257 void tmplput_NodeName(StrBuf *Target, WCTemplputParams *TP) {
258 NodeConf *Node = (NodeConf*) CTX(CTX_NODECONF);
259 StrBufAppendTemplate(Target, TP, Node->NodeName, 0);
262 void tmplput_Secret(StrBuf *Target, WCTemplputParams *TP) {
263 NodeConf *Node = (NodeConf*) CTX(CTX_NODECONF);
264 StrBufAppendTemplate(Target, TP, Node->Secret, 0);
267 void tmplput_Host(StrBuf *Target, WCTemplputParams *TP) {
268 NodeConf *Node= (NodeConf*) CTX(CTX_NODECONF);
269 StrBufAppendTemplate(Target, TP, Node->Host, 0);
272 void tmplput_Port(StrBuf *Target, WCTemplputParams *TP) {
273 NodeConf *Node= (NodeConf*) CTX(CTX_NODECONF);
274 StrBufAppendTemplate(Target, TP, Node->Port, 0);
281 RegisterCTX(CTX_NODECONF);
282 WebcitAddUrlHandler(HKEY("display_edit_node"), "", 0, display_edit_node, 0);
284 WebcitAddUrlHandler(HKEY("aide_ignetconf_edit_node"), "", 0, edit_node, 0);
285 WebcitAddUrlHandler(HKEY("display_netconf"), "", 0, display_netconf, 0);
286 WebcitAddUrlHandler(HKEY("display_confirm_delete_node"), "", 0, display_confirm_delete_node, 0);
287 WebcitAddUrlHandler(HKEY("delete_node"), "", 0, delete_node, 0);
290 RegisterNamespace("CFG:IGNET:NODE", 0, 1, tmplput_NodeName, NULL, CTX_NODECONF);
291 RegisterNamespace("CFG:IGNET:SECRET", 0, 1, tmplput_Secret, NULL, CTX_NODECONF);
292 RegisterNamespace("CFG:IGNET:HOST", 0, 1, tmplput_Host, NULL, CTX_NODECONF);
293 RegisterNamespace("CFG:IGNET:PORT", 0, 1, tmplput_Port, NULL, CTX_NODECONF);
295 RegisterIterator("NODECONFIG", 0, NULL, load_netconf, NULL, DeleteHash, CTX_NODECONF, CTX_NONE, IT_NOFLAG);