Fix memory leaks; need to register lookups so their list is free'd afterwards.
[citadel.git] / webcit / inetconf.c
index c8e100d0a03af984a31888be70171f395c440530..07a7526a6d0a1d06becdf96ee5cab5936782925f 100644 (file)
@@ -1,6 +1,4 @@
 /* 
- * $Id$
- *
  * Functions which handle Internet domain configuration etc.
  */
 
@@ -12,6 +10,7 @@ typedef enum _e_cfg {
        ic_localhost,
        ic_directory,
        ic_smarthost,
+       ic_fallback,
        ic_rbl,
        ic_spamass,
        ic_masq,
@@ -20,17 +19,13 @@ typedef enum _e_cfg {
        ic_max
 } ECfg;
 
-typedef struct _ConstStrBuf {
-       const char *name;
-       size_t len;
-} ConstStrBuf;
-
 
   /* These are server config keywords; do not localize! */
-ConstStrBuf CfgNames[] = {
+ConstStr CfgNames[] = {
        { HKEY("localhost") },
        { HKEY("directory") },
        { HKEY("smarthost") },
+       { HKEY("fallbackhost") },
        { HKEY("rbl") },
        { HKEY("spamassassin") },
        { HKEY("masqdomain") },
@@ -55,9 +50,9 @@ void load_inetconf(void)
        
        WCC->InetCfg = NewHash(1, NULL);
 
-       for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStrBuf)); i++) {
+       for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStr)); i++) {
                Hash = NewHash(1, NULL);
-               Put(WCC->InetCfg, CfgNames[i].name, CfgNames[i].len, Hash, HDeleteHash);
+               Put(WCC->InetCfg, CKEY(CfgNames[i]), Hash, HDeleteHash);
        }
 
        serv_printf("CONF GETSYS|application/x-citadel-internet-config");
@@ -67,7 +62,10 @@ void load_inetconf(void)
        if (GetServerStatus(Buf, NULL) == 1) {
                CfgToken = NewStrBuf();
                while ((len = StrBuf_ServGetln(Buf),
-                       strcmp(ChrPtr(Buf), "000"))) {
+                       ((len >= 0) && 
+                        ((len != 3) ||
+                         strcmp(ChrPtr(Buf), "000")))))
+               {
                        Value = NewStrBuf();
  
                        StrBufExtract_token(CfgToken, Buf, 1, '|');
@@ -75,7 +73,7 @@ void load_inetconf(void)
                        GetHash(WCC->InetCfg, ChrPtr(CfgToken), StrLength(CfgToken), &vHash);
                        Hash = (HashList*) vHash;
                        if (Hash == NULL) {
-                               lprintf(1, "ERROR Loading inet config line: [%s]\n", 
+                               syslog(LOG_WARNING, "ERROR Loading inet config line: [%s]\n", 
                                        ChrPtr(Buf));
                                FreeStrBuf(&Value);
                                continue;
@@ -109,7 +107,7 @@ void new_save_inetconf(void) {
        GetHash(WCC->InetCfg, ChrPtr(eType), StrLength(eType), &vHash);
        Hash = (HashList*) vHash;
        if (Hash == NULL) {
-               StrBufPrintf(WCC->ImportantMsg, _("Invalid Parameter"));
+               AppendImportantMessage(_("Invalid Parameter"), -1);
                url_do_template();
                return;
        }
@@ -118,20 +116,21 @@ void new_save_inetconf(void) {
                eNum = sbstr("ename");
                if (!GetHash(Hash, ChrPtr(eNum), StrLength(eNum), &vStr) ||
                    (vStr == NULL)) {
-                       StrBufPrintf(WCC->ImportantMsg, _("Invalid Parameter"));
+                       AppendImportantMessage(_("Invalid Parameter"), -1);
                        url_do_template();
                        return;
                }
 
                Str = (StrBuf*)vStr;
-               StrBufPrintf(WCC->ImportantMsg, _("%s has been deleted."), ChrPtr(Str));
+               AppendImportantMessage(SKEY(Str));
+               AppendImportantMessage(_(" has been deleted."), -1);
                FlushStrBuf(Str);       
        }
        else if (!strcasecmp(bstr("oper"), "add")) {
                StrBuf *name;
                eName = sbstr("ename");
                if (eName == NULL) {
-                       StrBufPrintf(WCC->ImportantMsg, _("Invalid Parameter"));
+                       AppendImportantMessage(_("Invalid Parameter"), -1);
                        url_do_template();
                        return;
                }
@@ -141,24 +140,23 @@ void new_save_inetconf(void) {
                name = NewStrBufDup(eName);
                StrBufTrim(name);
                Put(Hash, nnn, nUsed, name, HFreeStrBuf); 
-               StrBufPrintf(WCC->ImportantMsg, "%s %s", 
-                            /*<domain> added status message*/ _("added."), 
-                            ChrPtr(eName));
+               AppendImportantMessage(SKEY(eName));
+               AppendImportantMessage( /*<domain> added status message*/ _(" added."), -1); 
        }
 
        Buf = NewStrBuf();
        serv_printf("CONF PUTSYS|application/x-citadel-internet-config");
        StrBuf_ServGetln(Buf);
        if (GetServerStatus(Buf, NULL) == 4) {
-               for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStrBuf)); i++) {
+               for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStr)); i++) {
                        HashPos *where;
                        const char *Key;
                        long KeyLen;
 
-                       GetHash(WCC->InetCfg, CfgNames[i].name, CfgNames[i].len, &vHash);
+                       GetHash(WCC->InetCfg, CKEY(CfgNames[i]), &vHash);
                        Hash = (HashList*) vHash;
                        if (Hash == NULL) {
-                               sprintf(WC->ImportantMessage, _("Invalid Parameter"));
+                               AppendImportantMessage(_("Invalid Parameter"), -1);
                                url_do_template();
                                return;
                        }
@@ -169,7 +167,7 @@ void new_save_inetconf(void) {
                                        if ((Str!= NULL) && (StrLength(Str) > 0))
                                                serv_printf("%s|%s", 
                                                            ChrPtr(Str),
-                                                           CfgNames[i].name); 
+                                                           CfgNames[i].Key); 
                                }
                                DeleteHashPos(&where);
                        }                       
@@ -181,11 +179,6 @@ void new_save_inetconf(void) {
        url_do_template();
 }
 
-void InetCfgSubst(StrBuf *TemplBuffer, WCTemplputParams *TP)
-{
-       SVPutBuf("SERVCFG:INET:HOSTNAME", CTX, 1);
-}
-
 void DeleteInetConfHash(StrBuf *Target, WCTemplputParams *TP)
 {
        wcsession *WCC = WC;
@@ -204,15 +197,59 @@ HashList *GetInetConfHash(StrBuf *Target, WCTemplputParams *TP)
        if (WCC->InetCfg == NULL)
                load_inetconf();
        GetHash(WCC->InetCfg, TKEY(5), &vHash);
-       svprintf(HKEY("SERVCFG:INET:TYPE"), WCS_STRING, TP->Tokens->Params[5]->Start);
+       PutBstr(HKEY("__SERVCFG:INET:TYPE"), NewStrBufPlain(TKEY(5)));
        return vHash;
 }
 
+
+HashList *GetValidDomainNames(StrBuf *Target, WCTemplputParams *TP) 
+{
+       StrBuf *Line;
+       HashList *ValidDomainNames = NULL;
+       long State;
+       int gvdnlevel = 0;
+       
+       serv_printf("GVDN %d", gvdnlevel);
+       Line = NewStrBuf();
+       StrBuf_ServGetln(Line);
+       if (GetServerStatus(Line, &State) == 1) 
+       {
+               int Done = 0;
+               int n = 0;
+
+               ValidDomainNames = NewHash(1, NULL);
+               while(!Done && (StrBuf_ServGetln(Line) >= 0))
+                       if ( (StrLength(Line)==3) && 
+                            !strcmp(ChrPtr(Line), "000"))
+                       {
+                               Done = 1;
+                       }
+                       else
+                       {
+                               Put(ValidDomainNames, 
+                                   IKEY(n),
+                                   NewStrBufDup(Line), 
+                                   HFreeStrBuf);
+                               n++; /* #0 is the type... */
+                       }
+       }
+       else if (State == 550)
+               AppendImportantMessage(_("Higher access is required to access this function."), -1);
+
+       FreeStrBuf(&Line);
+
+       return ValidDomainNames;
+}
+
+
+
 void 
 InitModule_INETCONF
 (void)
 {
-       WebcitAddUrlHandler(HKEY("save_inetconf"), new_save_inetconf, 0);
-       RegisterIterator("SERVCFG:INET", 1, NULL, GetInetConfHash, InetCfgSubst, NULL, CTX_INETCFG, CTX_NONE, IT_NOFLAG);
+       WebcitAddUrlHandler(HKEY("save_inetconf"), "", 0, new_save_inetconf, 0);
+       RegisterIterator("SERVCFG:INET", 1, NULL, GetInetConfHash, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
        RegisterNamespace("SERVCFG:FLUSHINETCFG",0, 0, DeleteInetConfHash, NULL, CTX_NONE);
+       RegisterIterator("ITERATE:VALID:DOMAINNAMES", 1, NULL, GetValidDomainNames, NULL, DeleteHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
+
 }