X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=webcit%2Fnetconf.c;h=5f9087228076de8e9abdd5957c8759f4049839ae;hb=808f3be91dd6b6677e380695e2f16e6473141a7e;hp=ee2095031028c24f7700ec29c4bb12a9654bb78b;hpb=fcf62632708da42a9fd1a603dc7742426aed039b;p=citadel.git diff --git a/webcit/netconf.c b/webcit/netconf.c index ee2095031..5f9087228 100644 --- a/webcit/netconf.c +++ b/webcit/netconf.c @@ -1,368 +1,316 @@ - - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +/* + * $Id$ + */ +/** + * \defgroup NetShareConf Functions which handle network and sharing configuration. + * + * \ingroup CitadelConfig + */ +/*@{*/ #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; +} -struct sharelist { - struct sharelist *next; - char shname[256]; -}; +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)); +} -void display_edit_node(void) + +HashList *load_netconf(StrBuf *Target, WCTemplputParams *TP) { - char buf[256]; - char node[256]; - char sroom[256]; - - strcpy(node, bstr("node")); - - output_headers(1); - wprintf("
"); - wprintf("Edit share list for "); - escputs(node); - wprintf("\n"); - wprintf("
\n"); - - wprintf("
\n"); - wprintf("Add a shared room
\n"); - - wprintf("Return to network configuration screen
\n"); - - serv_printf("NSET roomlist|%s", node); - serv_gets(buf); + 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') { - wprintf("\n"); - while (serv_gets(buf), strcmp(buf, "000")) { - extract(sroom, buf, 0); - wprintf(""); - wprintf(""); - wprintf("\n"); + 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); } - wprintf("
"); - escputs(sroom); - wprintf("(UnShare)
\n"); + FreeStrBuf(&Buf); + return Hash; } - wDumpContent(1); + return NULL; } -void display_netconf(void) +void save_net_conf(HashList *Nodelist) { - char buf[256]; - char node[256]; - - output_headers(1); - wprintf("
"); - wprintf("Network configuration\n"); - wprintf("
\n"); - - wprintf("
"); - wprintf(""); - wprintf("Add a new node
\n"); - wprintf("
"); - - wprintf("
"); - wprintf("Currently configured nodes\n"); - wprintf("
\n"); - serv_puts("NSET nodelist"); - serv_gets(buf); - if (buf[0] == '1') { - wprintf("
\n"); - while (serv_gets(buf), strcmp(buf, "000")) { - extract(node, buf, 0); - wprintf(""); - wprintf(""); - wprintf(""); - wprintf("\n"); + 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(Nodelist, 0); + Buf = NewStrBuf(); + while (GetNextHashPos(Nodelist, where, &KeyLen, &Key, &vNode)) { + Node = (NodeConf*) vNode; + if (Node->DeleteMe==0) { + SerializeNode(Node, Buf); + serv_putbuf(Buf); + } + } + FreeStrBuf(&Buf); } - wprintf("
"); - escputs(node); - wprintf("(Edit)(Delete)
\n"); + serv_puts("000"); } - wDumpContent(1); } -void display_confirm_unshare(void) -{ - char node[256]; - char sroom[256]; - - output_headers(1); - wprintf("
"); - wprintf("Confirm unshare\n"); - wprintf("
\n"); - - strcpy(node, bstr("node")); - strcpy(sroom, bstr("sroom")); - wprintf("
Are you sure you want to unshare "); - escputs(sroom); - wprintf("?
\n"); - wprintf("Yes   "); - wprintf("No
\n"); - wDumpContent(1); -} +/*----------------------------------------------------------------------*/ +/* WEB Handlers */ +/*----------------------------------------------------------------------*/ -void display_confirm_delete_node(void) -{ - char node[256]; - - output_headers(1); - wprintf("
"); - wprintf("Confirm delete\n"); - wprintf("
\n"); - - strcpy(node, bstr("node")); - wprintf("
Are you sure you want to delete "); - escputs(node); - wprintf("?
\n"); - wprintf("Yes   "); - wprintf("No
\n"); - wDumpContent(1); -} -void delete_node(void) -{ - char node[256]; - char buf[256]; +/** + * \brief edit a network node + */ +void edit_node(void) { + HashList *NodeConfig; + const StrBuf *Index; + NodeConf *NewNode; - strcpy(node, bstr("node")); - sprintf(buf, "NSET deletenode|%s", node); - serv_puts(buf); - serv_gets(buf); - if (buf[0] == '1') { - output_headers(1); - server_to_text(); - wprintf("Back to menu\n"); - wDumpContent(1); - } else { - display_error(&buf[4]); + 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, &NoCtx); + Put(NodeConfig, ChrPtr(Index), StrLength(Index), NewNode, DeleteNodeConf); + save_net_conf(NodeConfig); + DeleteHash(&NodeConfig); } + url_do_template(); } -void unshare(void) +/** + * \brief modify an existing node + */ +void display_edit_node(void) { - char node[256]; - char sroom[256]; - char buf[256]; - - strcpy(node, bstr("node")); - strcpy(sroom, bstr("sroom")); - sprintf(buf, "NSET unshare|%s|%s", node, sroom); - serv_puts(buf); - serv_gets(buf); - if (buf[0] == '1') { - output_headers(1); - server_to_text(); - wprintf("Back to menu\n"); - wDumpContent(1); - } else { - display_error(&buf[4]); + WCTemplputParams SubTP; + HashList *NodeConfig; + const StrBuf *Index; + void *vNode; + const StrBuf *Tmpl; + + Index = sbstr("index"); + if (Index == NULL) { + sprintf(WC->ImportantMessage, _("Invalid Parameter")); + url_do_template(); + return; } -} + NodeConfig = load_netconf(NULL, &NoCtx); + if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || + (vNode == NULL)) { + sprintf(WC->ImportantMessage, _("Invalid Parameter")); + url_do_template(); + DeleteHash(&NodeConfig); + return; + } + + memset(&SubTP, 0, sizeof(WCTemplputParams)); + SVPutBuf("ITERATE:KEY", Index, 1); + SubTP.ContextType = CTX_NODECONF; + SubTP.Context = vNode; + begin_burst(); + Tmpl = sbstr("template"); + output_headers(1, 0, 0, 0, 1, 0); + DoTemplate(SKEY(Tmpl), NULL, &SubTP); + end_burst(); + DeleteHash(&NodeConfig); + +} -void display_add_node(void) +/** + * \brief display all configured nodes + */ +void display_netconf(void) { - - output_headers(1); - wprintf("
"); - wprintf("Add a new node\n"); - wprintf("
\n"); - - wprintf("
"); - wprintf("
\n"); - - wprintf("Enter name of new node: "); - wprintf("
\n"); - - wprintf(""); - wprintf(""); - - wprintf("
\n"); wDumpContent(1); } - - -void add_node(void) +/** + * \brief display the dialog to verify the deletion + */ +void display_confirm_delete_node(void) { - char node[256]; - char buf[256]; - char sc[256]; - - strcpy(node, bstr("node")); - strcpy(sc, bstr("sc")); - - if (!strcmp(sc, "Add")) { - sprintf(buf, "NSET addnode|%s", node); - serv_puts(buf); - serv_gets(buf); - if (buf[0] == '1') { - output_headers(1); - server_to_text(); - wprintf("Back to menu\n"); - wDumpContent(1); - } else { - display_error(&buf[4]); - } - } + wDumpContent(1); } - -void display_share(void) +/** + * \brief actually delete the node + */ +void delete_node(void) { - char buf[256]; - char node[256]; - char sroom[256]; - struct sharelist *shlist = NULL; - struct sharelist *shptr; - int already_shared; - - strcpy(node, bstr("node")); - - output_headers(1); - wprintf("
"); - wprintf("Add a shared room\n"); - wprintf("
\n"); - - wprintf("
"); - wprintf("
\n"); - wprintf("\n"); - - sprintf(buf, "NSET roomlist|%s", node); - serv_puts(buf); - serv_gets(buf); - if (buf[0] == '1') { - while (serv_gets(buf), strcmp(buf, "000")) { - shptr = (struct sharelist *) - malloc(sizeof(struct sharelist)); - shptr->next = shlist; - extract(shptr->shname, buf, 0); - shlist = shptr; - } + HashList *NodeConfig; + const StrBuf *Index; + NodeConf *Node; + void *vNode; + + Index = sbstr("index"); + if (Index == NULL) { + sprintf(WC->ImportantMessage, _("Invalid Parameter")); + url_do_template(); + return; } - wprintf("\n"); - wprintf("
\n"); + Node = (NodeConf *) vNode; + Node->DeleteMe = 1; + save_net_conf(NodeConfig); + DeleteHash(&NodeConfig); + + url_do_template(); - wprintf(""); - wprintf(""); +} - wprintf("
\n"); - wDumpContent(1); - /* free the list */ - while (shlist != NULL) { - shptr = shlist->next; - free(shlist); - shlist = shptr; - } +void tmplput_NodeName(StrBuf *Target, WCTemplputParams *TP) +{ + NodeConf *Node = (NodeConf*) CTX; + StrBufAppendTemplate(Target, TP, Node->NodeName, 0); +} +void tmplput_Secret(StrBuf *Target, WCTemplputParams *TP) +{ + NodeConf *Node = (NodeConf*) CTX; + StrBufAppendTemplate(Target, TP, Node->Secret, 0); } +void tmplput_Host(StrBuf *Target, WCTemplputParams *TP) +{ + NodeConf *Node= (NodeConf*) CTX; + StrBufAppendTemplate(Target, TP, Node->Host, 0); +} +void tmplput_Port(StrBuf *Target, WCTemplputParams *TP) +{ + NodeConf *Node= (NodeConf*) CTX; + StrBufAppendTemplate(Target, TP, Node->Port, 0); +} -void share(void) +void +InitModule_NETCONF +(void) { - char node[256]; - char buf[256]; - char sc[256]; - char sroom[256]; - - strcpy(node, bstr("node")); - strcpy(sc, bstr("sc")); - strcpy(sroom, bstr("sroom")); - - if (!strcmp(sc, "Share")) { - sprintf(buf, "NSET share|%s|%s", node, sroom); - serv_puts(buf); - serv_gets(buf); - if (buf[0] == '1') { - output_headers(1); - server_to_text(); - wprintf("Back to menu\n"); - wDumpContent(1); - } else { - display_error(&buf[4]); - } + 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); + + + RegisterNamespace("CFG:IGNET:NODE", 0, 1, tmplput_NodeName, CTX_NODECONF); + RegisterNamespace("CFG:IGNET:SECRET", 0, 1, tmplput_Secret, CTX_NODECONF); + RegisterNamespace("CFG:IGNET:HOST", 0, 1, tmplput_Host, CTX_NODECONF); + RegisterNamespace("CFG:IGNET:PORT", 0, 1, tmplput_Port, CTX_NODECONF); + + RegisterIterator("NODECONFIG", 0, NULL, load_netconf, NULL, DeleteHash, CTX_NODECONF, CTX_NONE, IT_NOFLAG); } +/*@}*/