* add contexttype, so the subst can precheck the context pointer for us, and bad...
[citadel.git] / webcit / netconf.c
index bfb9388708df9a24f15290e24daff3974ed741e6..521c765a78ed0dadd892d9e8ea222d4476d9132a 100644 (file)
 /*@{*/
 #include "webcit.h"
 
+void display_netconf(void);
+
+/*----------------------------------------------------------------------*/
+/*              Business Logic                                          */
+/*----------------------------------------------------------------------*/
+
+typedef struct _nodeconf {
+       int DeleteMe;
+       StrBuf *NodeName;
+       StrBuf *Secret;
+       StrBuf *Host;
+       StrBuf *Port;
+}NodeConf;
+
+void DeleteNodeConf(void *vNode)
+{
+       NodeConf *Node = (NodeConf*) vNode;
+       FreeStrBuf(&Node->NodeName);
+       FreeStrBuf(&Node->Secret);
+       FreeStrBuf(&Node->Host);
+       FreeStrBuf(&Node->Port);
+       free(Node);
+}
+
+NodeConf *NewNode(StrBuf *SerializedNode)
+{
+       NodeConf *Node;
+
+       if (StrLength(SerializedNode) < 8) 
+               return NULL; /** we need at least 4 pipes and some other text so its invalid. */
+       Node = (NodeConf *) malloc(sizeof(NodeConf));
+       Node->DeleteMe = 0;
+       Node->NodeName=NewStrBuf();
+       StrBufExtract_token(Node->NodeName, SerializedNode, 0, '|');
+       Node->Secret=NewStrBuf();
+       StrBufExtract_token(Node->Secret, SerializedNode, 1, '|');
+       Node->Host=NewStrBuf();
+       StrBufExtract_token(Node->Host, SerializedNode, 2, '|');
+       Node->Port=NewStrBuf();
+       StrBufExtract_token(Node->Port, SerializedNode, 3, '|');
+       return Node;
+}
+
+NodeConf *HttpGetNewNode(void)
+{
+       NodeConf *Node;
+
+       if (!havebstr("node") || 
+           !havebstr("secret")||
+           !havebstr("host")||
+           !havebstr("port"))
+               return NULL;
+
+       Node = (NodeConf *) malloc(sizeof(NodeConf));
+       Node->DeleteMe = 0;
+       Node->NodeName = NewStrBufDup(sbstr("node"));
+       Node->Secret = NewStrBufDup(sbstr("secret"));
+       Node->Host = NewStrBufDup(sbstr("host"));
+       Node->Port = NewStrBufDup(sbstr("port"));
+       return Node;
+}
+
+void SerializeNode(NodeConf *Node, StrBuf *Buf)
+{
+       StrBufPrintf(Buf, "%s|%s|%s|%s", 
+                    ChrPtr(Node->NodeName),
+                    ChrPtr(Node->Secret),
+                    ChrPtr(Node->Host),
+                    ChrPtr(Node->Port));
+}
+
+
+HashList *load_netconf(WCTemplateToken *Token)
+{
+       StrBuf *Buf;
+       HashList *Hash;
+       char nnn[64];
+       char buf[SIZ];
+       long len;
+       int nUsed;
+       NodeConf *Node;
+
+       serv_puts("CONF getsys|application/x-citadel-ignet-config");
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '1') {
+               Hash = NewHash(1, NULL);
+
+               Buf = NewStrBuf();
+               while ((len = StrBuf_ServGetln(Buf),
+                       strcmp(ChrPtr(Buf), "000"))) {
+                       Node = NewNode(Buf);
+                       if (Node == NULL)
+                               continue;
+                       nUsed = GetCount(Hash);
+                       nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
+                       Put(Hash, nnn, nUsed, Node, DeleteNodeConf); 
+               }
+               FreeStrBuf(&Buf);
+               return Hash;
+       }
+       return NULL;
+}
+
+
+void NodeCfgSubst(StrBuf *TemplBuffer, void *vContext, WCTemplateToken *Token)
+{
+       NodeConf *Node= (NodeConf*)vContext;
+
+       SVPutBuf("CFG:IGNET:NODE", Node->NodeName, 1);
+       SVPutBuf("CFG:IGNET:SECRET", Node->Secret, 1);
+       SVPutBuf("CFG:IGNET:HOST", Node->Host, 1);
+       SVPutBuf("CFG:IGNET:PORT", Node->Port, 1);
+}
+
+
+void save_net_conf(HashList *Nodelist)
+{
+       char buf[SIZ];
+       StrBuf *Buf;
+       HashPos *where;
+       void *vNode;
+       NodeConf *Node;
+       const char *Key;
+       long KeyLen;
+
+       serv_puts("CONF putsys|application/x-citadel-ignet-config");
+       serv_getln(buf, sizeof buf);
+       if (buf[0] == '4') {
+               if ((Nodelist != NULL) && (GetCount(Nodelist) > 0)) {
+                       where = GetNewHashPos();
+                       Buf = NewStrBuf();
+                       while (GetNextHashPos(Nodelist, where, &KeyLen, &Key, &vNode)) {
+                               Node = (NodeConf*) vNode;
+                               if (Node->DeleteMe==0) { 
+                                       SerializeNode(Node, Buf);
+                                       serv_putbuf(Buf);
+                               }
+                       }
+                       FreeStrBuf(&Buf);
+               }
+               serv_puts("000");
+       }
+}
+
+
+
+/*----------------------------------------------------------------------*/
+/*              WEB Handlers                                            */
+/*----------------------------------------------------------------------*/
+
+
+
 /**
  * \brief edit a network node
  */
 void edit_node(void) {
+       HashList *NodeConfig;
+       const StrBuf *Index;
+       NodeConf *NewNode;
+/*
        char buf[SIZ];
        char node[SIZ];
        char cnode[SIZ];
        FILE *fp;
-
-       if (!IsEmptyStr(bstr("ok_button"))) {
+*/
+       if (havebstr("ok_button")) {
+               Index = sbstr("index");
+               NewNode = HttpGetNewNode();
+               if ((NewNode == NULL) || (Index == NULL)) {
+                       sprintf(WC->ImportantMessage, _("Invalid Parameter"));
+                       url_do_template();
+                       return;
+               }
+                       
+               NodeConfig = load_netconf(NULL);
+               Put(NodeConfig, ChrPtr(Index), StrLength(Index), NewNode, DeleteNodeConf);
+               save_net_conf(NodeConfig);
+               DeleteHash(&NodeConfig);
+/*
                strcpy(node, bstr("node") );
                fp = tmpfile();
                if (fp != NULL) {
@@ -31,28 +200,34 @@ void edit_node(void) {
                                                fprintf(fp, "%s\n", buf);
                                        }
                                }
-                       fprintf(fp, "%s|%s|%s|%s\n", 
-                               bstr("node"),
-                               bstr("secret"),
-                               bstr("host"),
-                               bstr("port") );
-                       }
                        rewind(fp);
+               }
 
-                       serv_puts("CONF putsys|application/x-citadel-ignet-config");
-                       serv_getln(buf, sizeof buf);
-                       if (buf[0] == '4') {
+               serv_puts("CONF putsys|application/x-citadel-ignet-config");
+               serv_getln(buf, sizeof buf);
+               if (buf[0] == '4') {
+                       if (fp != NULL) {
                                while (fgets(buf, sizeof buf, fp) != NULL) {
                                        buf[strlen(buf)-1] = 0;
-                                       serv_puts(buf);
+                                       if (buf[0] != 0) {
+                                               serv_puts(buf);
+                                       }
                                }
-                               serv_puts("000");
+                               fclose(fp);
+                       }
+                       serv_printf("%s|%s|%s|%s", 
+                               bstr("node"),
+                               bstr("secret"),
+                               bstr("host"),
+                               bstr("port") );
                        }
-                       fclose(fp);
+                       serv_puts("000");
                }
+*/
        }
 
-       display_netconf();
+       //display_netconf();
+       url_do_template();
 }
 
 
@@ -60,18 +235,18 @@ void edit_node(void) {
  * \brief add a node
  */
 void display_add_node(void)
-{
+{/*
        output_headers(1, 1, 2, 0, 0, 0);
        wprintf("<div id=\"banner\">\n");
-       wprintf("<TABLE class=\"netconf_banner\"><TR><TD>");
-       wprintf("<SPAN CLASS=\"titlebar\">");
+       wprintf("<h1>");
        wprintf(_("Add a new node"));
-       wprintf("</SPAN>");
-       wprintf("</TD></TR></TABLE>\n");
-       wprintf("</div>\n<div id=\"content\">\n");
+       wprintf("</h1>");
+       wprintf("</div>\n");
+
+       wprintf("<div id=\"content\" class=\"service\">\n");
 
        wprintf("<FORM METHOD=\"POST\" action=\"edit_node\">\n");
-       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
+       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
        wprintf("<CENTER><TABLE border=0>\n");
        wprintf("<TR><TD>%s</TD>", _("Node name"));
        wprintf("<TD><INPUT TYPE=\"text\" NAME=\"node\" MAXLENGTH=\"16\"></TD></TR>\n");
@@ -80,7 +255,7 @@ void display_add_node(void)
        wprintf("<TR><TD>%s</TD>", _("Host or IP address"));
        wprintf("<TD><INPUT TYPE=\"text\" NAME=\"host\" MAXLENGTH=\"64\"></TD></TR>\n");
        wprintf("<TR><TD>%s</TD>", _("Port number"));
-       wprintf("<TD><INPUT TYPE=\"text\" NAME=\"port\" MAXLENGTH=\"8\"></TD></TR>\n");
+       wprintf("<TD><INPUT TYPE=\"text\" NAME=\"port\" VALUE=\"504\" MAXLENGTH=\"8\"></TD></TR>\n");
        wprintf("</TABLE><br />");
                wprintf("<INPUT TYPE=\"submit\" NAME=\"ok_button\" VALUE=\"%s\">", _("Add node"));
        wprintf("&nbsp;");
@@ -88,6 +263,7 @@ void display_add_node(void)
        wprintf("</CENTER></FORM>\n");
 
        wDumpContent(1);
+ */
 }
 
 /**
@@ -95,6 +271,33 @@ void display_add_node(void)
  */
 void display_edit_node(void)
 {
+       HashList *NodeConfig;
+       const StrBuf *Index;
+       void *vNode;
+
+       Index = sbstr("index");
+       if (Index == NULL) {
+               sprintf(WC->ImportantMessage, _("Invalid Parameter"));
+               url_do_template();
+               return;
+       }
+
+       NodeConfig = load_netconf(NULL);
+       if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || 
+           (vNode == NULL)) {
+               sprintf(WC->ImportantMessage, _("Invalid Parameter"));
+               url_do_template();
+               DeleteHash(&NodeConfig);
+               return;
+       }
+       
+       NodeCfgSubst(NULL, vNode, NULL);
+       SVPutBuf("ITERATE:KEY", Index, 1);
+       url_do_template();
+
+       DeleteHash(&NodeConfig);
+       
+/*
        char buf[512];
        char node[256];
        char cnode[256];
@@ -106,13 +309,13 @@ void display_edit_node(void)
 
        output_headers(1, 1, 2, 0, 0, 0);
        wprintf("<div id=\"banner\">\n");
-       wprintf("<TABLE class=\"netconf_banner\"><TR><TD>");
-       wprintf("<SPAN CLASS=\"titlebar\">");
+       wprintf("<h1>");
        wprintf(_("Edit node configuration for "));
        escputs(node);
-       wprintf("</SPAN>\n");
-       wprintf("</TD></TR></TABLE>\n");
-       wprintf("</div>\n<div id=\"content\">\n");
+       wprintf("</h1>");
+       wprintf("</div>\n");
+
+       wprintf("<div id=\"content\" class=\"service\">\n");
 
        serv_puts("CONF getsys|application/x-citadel-ignet-config");
        serv_getln(buf, sizeof buf);
@@ -125,7 +328,7 @@ void display_edit_node(void)
 
                        if (!strcasecmp(node, cnode)) {
                                wprintf("<FORM METHOD=\"POST\" action=\"edit_node\">\n");
-                               wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
+                               wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
                                wprintf("<CENTER><TABLE border=0>\n");
                                wprintf("<TR><TD>");
                                wprintf(_("Node name"));
@@ -155,30 +358,34 @@ void display_edit_node(void)
                }
        }
 
-       else {          /** command error getting configuration */
+       else {          / ** command error getting configuration * /
                wprintf("%s<br />\n", &buf[4]);
        }
 
        wDumpContent(1);
+*/
 }
 
 
+
+
+//CFG:IGNET:NODE
 /**
  * \brief display all configured nodes
  */
 void display_netconf(void)
 {
+/*
        char buf[SIZ];
        char node[SIZ];
-
        output_headers(1, 1, 2, 0, 0, 0);
        wprintf("<div id=\"banner\">\n");
-       wprintf("<TABLE class=\"netconf_banner\"><TR><TD>");
-       wprintf("<SPAN CLASS=\"titlebar\">");
+       wprintf("<h1>");
        wprintf(_("Network configuration"));
-       wprintf("</SPAN>\n");
-       wprintf("</TD></TR></TABLE>\n");
-       wprintf("</div>\n<div id=\"content\">\n");
+       wprintf("</h1>");
+       wprintf("</div>\n");
+
+       wprintf("<div id=\"content\" class=\"service\">\n");
 
        wprintf("<CENTER>");
        wprintf("<a href=\"display_add_node\">");
@@ -214,6 +421,7 @@ void display_netconf(void)
                }
                wprintf("</TABLE></CENTER>\n");
        }
+*/
        wDumpContent(1);
 }
 
@@ -222,16 +430,16 @@ void display_netconf(void)
  */
 void display_confirm_delete_node(void)
 {
+/*
        char node[SIZ];
-
        output_headers(1, 1, 2, 0, 0, 0);
        wprintf("<div id=\"banner\">\n");
-       wprintf("<TABLE class=\"netconf_banner\"><TR><TD>");
-       wprintf("<SPAN CLASS=\"titlebar\">");
+       wprintf("<h1>");
        wprintf(_("Confirm delete"));
-       wprintf("</SPAN>\n");
-       wprintf("</TD></TR></TABLE>\n");
-       wprintf("</div>\n<div id=\"content\">\n");
+       wprintf("</h1>");
+       wprintf("</div>\n");
+
+       wprintf("<div id=\"content\" class=\"service\" >\n");
 
        strcpy(node, bstr("node"));
        wprintf("<CENTER>");
@@ -247,14 +455,44 @@ void display_confirm_delete_node(void)
        wprintf("<a href=\"display_netconf\">");
        wprintf(_("No"));
        wprintf("</A><br />\n");
+*/
        wDumpContent(1);
 }
 
+
 /**
  * \brief actually delete the node
  */
 void delete_node(void)
 {
+       HashList *NodeConfig;
+       const StrBuf *Index;
+       NodeConf *Node;
+       void *vNode;
+
+       Index = sbstr("index");
+       if (Index == NULL) {
+               sprintf(WC->ImportantMessage, _("Invalid Parameter"));
+               url_do_template();
+               return;
+       }
+
+       NodeConfig = load_netconf(NULL);
+       if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || 
+           (vNode == NULL)) {
+               sprintf(WC->ImportantMessage, _("Invalid Parameter"));
+               url_do_template();
+               DeleteHash(&NodeConfig);
+               return;
+       }
+       Node = (NodeConf *) vNode;
+       Node->DeleteMe = 1;
+               save_net_conf(NodeConfig);
+       DeleteHash(&NodeConfig);
+       
+       url_do_template();
+
+/*
        char buf[SIZ];
        char node[SIZ];
        char cnode[SIZ];
@@ -288,35 +526,20 @@ void delete_node(void)
        }
 
        display_netconf();
+*/
 }
 
-/**
- * \brief add a new node
- */
-void add_node(void)
+void 
+InitModule_NETCONF
+(void)
 {
-       char node[SIZ];
-       char buf[SIZ];
-
-       strcpy(node, bstr("node"));
-
-       if (!IsEmptyStr(bstr("add_button")))  {
-               sprintf(buf, "NSET addnode|%s", node);
-               serv_puts(buf);
-               serv_getln(buf, sizeof buf);
-               if (buf[0] == '1') {
-                       output_headers(1, 1, 0, 0, 0, 0);
-                       server_to_text();
-                       wprintf("<a href=\"display_netconf\">");
-                       wprintf(_("Back to menu"));
-                       wprintf("</A>\n");
-                       wDumpContent(1);
-               } else {
-                       strcpy(WC->ImportantMessage, &buf[4]);
-                       display_netconf();
-               }
-       }
+       WebcitAddUrlHandler(HKEY("display_edit_node"), display_edit_node, 0);
+
+       WebcitAddUrlHandler(HKEY("edit_node"), edit_node, 0);
+       WebcitAddUrlHandler(HKEY("display_netconf"), display_netconf, 0);
+       WebcitAddUrlHandler(HKEY("display_confirm_delete_node"), display_confirm_delete_node, 0);
+       WebcitAddUrlHandler(HKEY("delete_node"), delete_node, 0);
+       WebcitAddUrlHandler(HKEY("display_add_node"), display_add_node, 0);
+       RegisterIterator("NODECONFIG", 0, NULL, load_netconf, NodeCfgSubst, DeleteHash, CTX_NODECONF);
 }
-
-
 /*@}*/