Back to index

webcit  8.12-dfsg
netconf.c
Go to the documentation of this file.
00001 #include "webcit.h"
00002 
00003 void display_netconf(void);
00004 
00005 /*----------------------------------------------------------------------*/
00006 /*              Business Logic                                          */
00007 /*----------------------------------------------------------------------*/
00008 
00009 typedef struct _nodeconf {
00010        int DeleteMe;
00011        StrBuf *NodeName;
00012        StrBuf *Secret;
00013        StrBuf *Host;
00014        StrBuf *Port;
00015 }NodeConf;
00016 
00017 void DeleteNodeConf(void *vNode)
00018 {
00019        NodeConf *Node = (NodeConf*) vNode;
00020        FreeStrBuf(&Node->NodeName);
00021        FreeStrBuf(&Node->Secret);
00022        FreeStrBuf(&Node->Host);
00023        FreeStrBuf(&Node->Port);
00024        free(Node);
00025 }
00026 
00027 NodeConf *NewNode(StrBuf *SerializedNode)
00028 {
00029        NodeConf *Node;
00030 
00031        if (StrLength(SerializedNode) < 8) 
00032               return NULL; 
00033        Node = (NodeConf *) malloc(sizeof(NodeConf));
00034        Node->DeleteMe = 0;
00035        Node->NodeName=NewStrBuf();
00036        StrBufExtract_token(Node->NodeName, SerializedNode, 0, '|');
00037        Node->Secret=NewStrBuf();
00038        StrBufExtract_token(Node->Secret, SerializedNode, 1, '|');
00039        Node->Host=NewStrBuf();
00040        StrBufExtract_token(Node->Host, SerializedNode, 2, '|');
00041        Node->Port=NewStrBuf();
00042        StrBufExtract_token(Node->Port, SerializedNode, 3, '|');
00043        return Node;
00044 }
00045 
00046 NodeConf *HttpGetNewNode(void)
00047 {
00048        NodeConf *Node;
00049 
00050        if (!havebstr("node") || 
00051            !havebstr("secret")||
00052            !havebstr("host")||
00053            !havebstr("port"))
00054               return NULL;
00055 
00056        Node = (NodeConf *) malloc(sizeof(NodeConf));
00057        Node->DeleteMe = 0;
00058        Node->NodeName = NewStrBufDup(sbstr("node"));
00059        Node->Secret = NewStrBufDup(sbstr("secret"));
00060        Node->Host = NewStrBufDup(sbstr("host"));
00061        Node->Port = NewStrBufDup(sbstr("port"));
00062        return Node;
00063 }
00064 
00065 void SerializeNode(NodeConf *Node, StrBuf *Buf)
00066 {
00067        StrBufPrintf(Buf, "%s|%s|%s|%s", 
00068                    ChrPtr(Node->NodeName),
00069                    ChrPtr(Node->Secret),
00070                    ChrPtr(Node->Host),
00071                    ChrPtr(Node->Port));
00072 }
00073 
00074 
00075 HashList *load_netconf(StrBuf *Target, WCTemplputParams *TP)
00076 {
00077        StrBuf *Buf;
00078        HashList *Hash;
00079        char nnn[64];
00080        char buf[SIZ];
00081        int nUsed;
00082        NodeConf *Node;
00083 
00084        serv_puts("CONF getsys|application/x-citadel-ignet-config");
00085        serv_getln(buf, sizeof buf);
00086        if (buf[0] == '1') {
00087               Hash = NewHash(1, NULL);
00088 
00089               Buf = NewStrBuf();
00090               while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
00091                      Node = NewNode(Buf);
00092                      if (Node != NULL) {
00093                             nUsed = GetCount(Hash);
00094                             nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
00095                             Put(Hash, nnn, nUsed, Node, DeleteNodeConf); 
00096                      }
00097               }
00098               FreeStrBuf(&Buf);
00099               return Hash;
00100        }
00101        return NULL;
00102 }
00103 
00104 
00105 
00106 void save_net_conf(HashList *Nodelist)
00107 {
00108        char buf[SIZ];
00109        StrBuf *Buf;
00110        HashPos *where;
00111        void *vNode;
00112        NodeConf *Node;
00113        const char *Key;
00114        long KeyLen;
00115 
00116        serv_puts("CONF putsys|application/x-citadel-ignet-config");
00117        serv_getln(buf, sizeof buf);
00118        if (buf[0] == '4') {
00119               if ((Nodelist != NULL) && (GetCount(Nodelist) > 0)) {
00120                      where = GetNewHashPos(Nodelist, 0);
00121                      Buf = NewStrBuf();
00122                      while (GetNextHashPos(Nodelist, where, &KeyLen, &Key, &vNode)) {
00123                             Node = (NodeConf*) vNode;
00124                             if (Node->DeleteMe==0) { 
00125                                    SerializeNode(Node, Buf);
00126                                    serv_putbuf(Buf);
00127                             }
00128                      }
00129                      FreeStrBuf(&Buf);
00130                      DeleteHashPos(&where);
00131               }
00132               serv_puts("000");
00133        }
00134 }
00135 
00136 
00137 
00138 /*----------------------------------------------------------------------*/
00139 /*              WEB Handlers                                            */
00140 /*----------------------------------------------------------------------*/
00141 
00142 
00143 
00144 /*
00145  * edit a network node
00146  */
00147 void edit_node(void) {
00148        HashList *NodeConfig;
00149        const StrBuf *Index;
00150        NodeConf *NewNode;
00151 
00152        if (havebstr("ok_button")) {
00153               Index = sbstr("index");
00154                NewNode = HttpGetNewNode();
00155               if ((NewNode == NULL) || (Index == NULL)) {
00156                      AppendImportantMessage(_("Invalid Parameter"), -1);
00157                      url_do_template();
00158                      return;
00159               }
00160                      
00161               NodeConfig = load_netconf(NULL, &NoCtx);
00162               Put(NodeConfig, ChrPtr(Index), StrLength(Index), NewNode, DeleteNodeConf);
00163               save_net_conf(NodeConfig);
00164               DeleteHash(&NodeConfig);
00165        }
00166        url_do_template();
00167 }
00168 
00169 
00170 /*
00171  * modify an existing node
00172  */
00173 void display_edit_node(void)
00174 {
00175        WCTemplputParams SubTP;
00176        HashList *NodeConfig;
00177        const StrBuf *Index;
00178        void *vNode;
00179        const StrBuf *Tmpl;
00180 
00181        Index = sbstr("index");
00182        if (Index == NULL) {
00183               AppendImportantMessage(_("Invalid Parameter"), -1);
00184               url_do_template();
00185               return;
00186        }
00187 
00188        NodeConfig = load_netconf(NULL, &NoCtx);
00189        if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || 
00190            (vNode == NULL)) {
00191               AppendImportantMessage(_("Invalid Parameter"), -1);
00192               url_do_template();
00193               DeleteHash(&NodeConfig);
00194               return;
00195        }
00196        
00197        memset(&SubTP, 0, sizeof(WCTemplputParams));
00198        SubTP.Filter.ContextType = CTX_NODECONF;
00199        SubTP.Context = vNode;
00200        begin_burst();
00201        Tmpl = sbstr("template");
00202         output_headers(1, 0, 0, 0, 1, 0);
00203         DoTemplate(SKEY(Tmpl), NULL, &SubTP);
00204         end_burst();                                                                               
00205        DeleteHash(&NodeConfig);
00206        
00207 }
00208 
00209 
00210 /*
00211  * display all configured nodes
00212  */
00213 void display_netconf(void)
00214 {
00215        wDumpContent(1);
00216 }
00217 
00218 /*
00219  * display the dialog to verify the deletion
00220  */
00221 void display_confirm_delete_node(void)
00222 {
00223        wDumpContent(1);
00224 }
00225 
00226 
00227 /*
00228  * actually delete the node
00229  */
00230 void delete_node(void)
00231 {
00232        HashList *NodeConfig;
00233        const StrBuf *Index;
00234        NodeConf *Node;
00235        void *vNode;
00236 
00237        Index = sbstr("index");
00238        if (Index == NULL) {
00239               AppendImportantMessage(_("Invalid Parameter"), -1);
00240               url_do_template();
00241               return;
00242        }
00243 
00244        NodeConfig = load_netconf(NULL, &NoCtx);
00245        if (!GetHash(NodeConfig, ChrPtr(Index), StrLength(Index), &vNode) || 
00246            (vNode == NULL)) {
00247               AppendImportantMessage(_("Invalid Parameter"), -1);
00248               url_do_template();
00249               DeleteHash(&NodeConfig);
00250               return;
00251        }
00252        Node = (NodeConf *) vNode;
00253        Node->DeleteMe = 1;
00254               save_net_conf(NodeConfig);
00255        DeleteHash(&NodeConfig);
00256        
00257        url_do_template();
00258 
00259 }
00260 
00261 
00262 void tmplput_NodeName(StrBuf *Target, WCTemplputParams *TP)
00263 {
00264        NodeConf *Node = (NodeConf*) CTX;  
00265        StrBufAppendTemplate(Target, TP, Node->NodeName, 0);
00266 }
00267 
00268 void tmplput_Secret(StrBuf *Target, WCTemplputParams *TP)
00269 {
00270        NodeConf *Node = (NodeConf*) CTX;
00271        StrBufAppendTemplate(Target, TP, Node->Secret, 0);
00272 }
00273 
00274 void tmplput_Host(StrBuf *Target, WCTemplputParams *TP) 
00275 {
00276        NodeConf *Node= (NodeConf*) CTX;
00277        StrBufAppendTemplate(Target, TP, Node->Host, 0);
00278 }
00279 
00280 void tmplput_Port(StrBuf *Target, WCTemplputParams *TP)
00281 {
00282        NodeConf *Node= (NodeConf*) CTX;
00283        StrBufAppendTemplate(Target, TP, Node->Port, 0);
00284 }
00285 
00286 void 
00287 InitModule_NETCONF
00288 (void)
00289 {
00290        WebcitAddUrlHandler(HKEY("display_edit_node"), "", 0, display_edit_node, 0);
00291 
00292        WebcitAddUrlHandler(HKEY("aide_ignetconf_edit_node"), "", 0, edit_node, 0);
00293        WebcitAddUrlHandler(HKEY("display_netconf"), "", 0, display_netconf, 0);
00294        WebcitAddUrlHandler(HKEY("display_confirm_delete_node"), "", 0, display_confirm_delete_node, 0);
00295        WebcitAddUrlHandler(HKEY("delete_node"), "", 0, delete_node, 0);
00296 
00297                                                                                           
00298         RegisterNamespace("CFG:IGNET:NODE", 0, 1, tmplput_NodeName, NULL, CTX_NODECONF);
00299         RegisterNamespace("CFG:IGNET:SECRET", 0, 1, tmplput_Secret, NULL, CTX_NODECONF);
00300         RegisterNamespace("CFG:IGNET:HOST", 0, 1, tmplput_Host, NULL, CTX_NODECONF);
00301         RegisterNamespace("CFG:IGNET:PORT", 0, 1, tmplput_Port, NULL, CTX_NODECONF);
00302 
00303        RegisterIterator("NODECONFIG", 0, NULL, load_netconf, NULL, DeleteHash, CTX_NODECONF, CTX_NONE, IT_NOFLAG);
00304 }