Readloop remove special cases
[citadel.git] / webcit / siteconfig.c
index 1516e904463e7a71409b7a29eb5874de66aba4a0..bdc3cc20576c288af5bf7d66b9aa481891f9ecf3 100644 (file)
@@ -1,14 +1,20 @@
 /*
- * $Id$
- *
  * Administrative screen for site-wide configuration
+ *
+ * Copyright (c) 1996-2011 by the citadel.org team
+ *
+ * This program is open source software.  You can redistribute it and/or
+ * modify it under the terms of the GNU General Public License, version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  */
 
-
 #include "webcit.h"
 #include "webserver.h"
 
-
 HashList *ZoneHash = NULL;
 
 ConstStr ExpirePolicyString = {CStrOf(roompolicy)     };
@@ -37,8 +43,7 @@ void LoadExpirePolicy(GPEXWhichPolicy which)
                WCC->Policy[which].expire_value = StrBufExtractNext_long(Buf, &Pos, '|');
        }
        else if (State == 550)
-               StrBufAppendBufPlain(WCC->ImportantMsg,
-                                    _("Higher access is required to access this function."), -1, 0);
+               AppendImportantMessage(_("Higher access is required to access this function."), -1);
        FreeStrBuf(&Buf);
 }
 
@@ -56,8 +61,7 @@ void SaveExpirePolicyFromHTTP(GPEXWhichPolicy which)
        StrBuf_ServGetln(Buf);
        GetServerStatus(Buf, &State);
        if (State == 550)
-               StrBufAppendBufPlain(WC->ImportantMsg,
-                                    _("Higher access is required to access this function."), -1, 0);
+               AppendImportantMessage(_("Higher access is required to access this function."), -1);
        FreeStrBuf(&Buf);
 }
 
@@ -80,7 +84,7 @@ void tmplput_ExpireValue(StrBuf *Target, WCTemplputParams *TP)
        GPEXWhichPolicy which;
        wcsession *WCC = WC;
                
-       which = GetTemplateTokenNumber(Target, TP, 2, 0);
+       which = GetTemplateTokenNumber(Target, TP, 0, 0);
        if (WCC->Policy[which].loaded == 0) LoadExpirePolicy(which);
        StrBufAppendPrintf(Target, "%d", WCC->Policy[which].expire_value);
 }
@@ -97,7 +101,6 @@ void tmplput_ExpireMode(StrBuf *Target, WCTemplputParams *TP)
 }
 
 
-
 void LoadZoneFiles(void)
 {
        icalarray *zones;
@@ -111,7 +114,7 @@ void LoadZoneFiles(void)
        Put(ZoneHash, HKEY("UTC"), ZName, HFreeStrBuf);
        zones = icaltimezone_get_builtin_timezones();
        for (z = 0; z < zones->num_elements; ++z) {
-               /* lprintf(9, "Location: %-40s tzid: %s\n",
+               /* syslog(9, "Location: %-40s tzid: %s\n",
                        icaltimezone_get_location(icalarray_element_at(zones, z)),
                        icaltimezone_get_tzid(icalarray_element_at(zones, z))
                ); */
@@ -128,7 +131,7 @@ typedef struct _CfgMapping {
        int type;
        const char *Key;
        long len;
-}CfgMapping;
+} CfgMapping;
 
 #define CFG_STR 1
 #define CFG_YES 2
@@ -201,10 +204,12 @@ CfgMapping ServerConfig[] = {
        {CFG_STR, HKEY("c_xmpp_s2s_port")},
        {CFG_STR, HKEY("c_pop3_fetch")},
        {CFG_STR, HKEY("c_pop3_fastest")},
-       {CFG_YES , HKEY("c_spam_flag_only")}
+       {CFG_YES, HKEY("c_spam_flag_only")},
+       {CFG_YES, HKEY("c_guest_logins")}
 };
 
 
+
 /*
  *  display all configuration items
  */
@@ -213,33 +218,44 @@ void load_siteconfig(void)
        wcsession *WCC = WC;
        StrBuf *Buf;
        HashList *Cfg;
-       char buf[SIZ];
        long len;
-       int i;
+       int i, j;
        
        if (WCC->ServCfg == NULL)
                WCC->ServCfg = NewHash(1, NULL);
        Cfg = WCC->ServCfg;
 
-       serv_printf("CONF get");
-       serv_getln(buf, sizeof buf);
-       i = 0;
        Buf = NewStrBuf();
-       while ((sizeof(ServerConfig) / sizeof(CfgMapping)) &&
-              (len = StrBuf_ServGetln(Buf),
-               strcmp(ChrPtr(Buf), "000")) && 
-              (i <= sizeof(ServerConfig))) 
+
+       serv_printf("CONF get");
+       StrBuf_ServGetln(Buf);
+       if (GetServerStatus(Buf, NULL) != 1) {
+               StrBufCutLeft(Buf, 4);
+               AppendImportantMessage(SKEY(Buf));
+               FreeStrBuf(&Buf);
+               return;
+               
+       }
+       j = i = 0;
+       while (len = StrBuf_ServGetln(Buf),
+              (len >= 0) && 
+              ((len != 3) || strcmp(ChrPtr(Buf), "000")))
        {
-               Put(Cfg,
-                   ServerConfig[i].Key, 
-                   ServerConfig[i].len, 
-                   Buf, 
-                   HFreeStrBuf);
-               i++;
-               if (i <= sizeof(ServerConfig) / sizeof(CfgMapping))
+               if (i < (sizeof(ServerConfig) / sizeof(CfgMapping)))
+               {
+                       Put(Cfg,
+                           ServerConfig[i].Key, 
+                           ServerConfig[i].len, 
+                           Buf, 
+                           HFreeStrBuf);
+                       i++;
                        Buf = NewStrBuf();
-               else
-                       Buf = NULL;                     
+               }
+               else {
+                       if (j == 0)
+                               AppendImportantMessage(_("WARNING: Failed to parse Server Config; do you run a to new citserver?"), -1);
+                       j++;
+               }
        }
        FreeStrBuf(&Buf);
 
@@ -249,27 +265,30 @@ void load_siteconfig(void)
 
 
 
-/**
+/*
  * parse siteconfig changes 
  */
 void siteconfig(void)
 {
        wcsession *WCC = WC;
        int i;
-       char buf[256];
+       StrBuf *Line;
 
        if (strlen(bstr("ok_button")) == 0) {
                display_aide_menu();
                return;
        }
+       Line = NewStrBuf();
        serv_printf("CONF set");
-       serv_getln(buf, sizeof buf);
-       if (buf[0] != '4') {
-               safestrncpy(WCC->ImportantMessage, &buf[4], sizeof WCC->ImportantMessage);
+       StrBuf_ServGetln(Line);
+       if (GetServerStatusMsg(Line, NULL, 1, 4) != 4) {
                display_aide_menu();
+               FreeStrBuf(&Line);
                return;
        }
 
+       FreeStrBuf(&Line);
+
        for (i=0; i < (sizeof(ServerConfig) / sizeof(CfgMapping)); i ++)
        {
                switch (ServerConfig[i].type) {
@@ -297,8 +316,7 @@ void siteconfig(void)
        FreeStrBuf(&WCC->serv_info->serv_default_cal_zone);
        WCC->serv_info->serv_default_cal_zone = NewStrBufDup(sbstr("c_default_cal_zone"));
 
-       safestrncpy(WCC->ImportantMessage, _("Your system configuration has been updated."),
-               sizeof WCC->ImportantMessage);
+       AppendImportantMessage(_("Your system configuration has been updated."), -1);
        DeleteHash(&WCC->ServCfg);
        display_aide_menu();
 }
@@ -333,38 +351,42 @@ int ConditionalServCfg(StrBuf *Target, WCTemplputParams *TP)
                if (TP->Tokens->nParameters == 3) {
                        return 1;
                }
-               else if (TP->Tokens->Params[3]->Type == TYPE_STR)
-                       return (strcmp(TP->Tokens->Params[3]->Start, ChrPtr(Buf)) == 0);
-               else return (StrTol(Buf) == TP->Tokens->Params[3]->lvalue);
+               else if (IS_NUMBER(TP->Tokens->Params[3]->Type))
+                       return (StrTol(Buf) == GetTemplateTokenNumber (Target, TP, 3, 0));
+               else
+               {
+                       const char *pch;
+                       long len;
+                       
+                       GetTemplateTokenString(Target, TP, 3, &pch, &len);
+               
+                       return ((len == StrLength(Buf)) &&
+                               (strcmp(pch, ChrPtr(Buf)) == 0));
+               }
+
        }
        else return 0;
 }
 
-int ConditionalServCfgSubst(StrBuf *Target, WCTemplputParams *TP)
+int ConditionalServCfgCTXStrBuf(StrBuf *Target, WCTemplputParams *TP)
 {
        wcsession *WCC = WC;
        void *vBuf;
        StrBuf *Buf;
+       StrBuf *ZoneToCheck = (StrBuf*) CTX;
 
-       if (WCC->is_aide) {
+       if ((WCC->is_aide) || (ZoneToCheck == NULL)) {
                if (WCC->ServCfg == NULL)
                        load_siteconfig();
                GetHash(WCC->ServCfg, TKEY(2), &vBuf);
                if (vBuf == NULL) return 0;
                Buf = (StrBuf*) vBuf;
 
-               return CompareSubstToStrBuf(Buf, TP->Tokens->Params[3]);
+               return strcmp(ChrPtr(Buf), ChrPtr(ZoneToCheck)) == 0;
        }
        else return 0;
 }
 
-void CfgZoneTempl(StrBuf *TemplBuffer, WCTemplputParams *TP)
-{
-       StrBuf *Zone = (StrBuf*) CTX;
-
-       SVPutBuf("ZONENAME", Zone, 1);
-}
-
 void 
 InitModule_SITECONFIG
 (void)
@@ -373,8 +395,8 @@ InitModule_SITECONFIG
 
        RegisterNamespace("SERV:CFG", 1, 2, tmplput_servcfg, NULL, CTX_NONE);
        RegisterConditional(HKEY("COND:SERVCFG"), 3, ConditionalServCfg, CTX_NONE);
-       RegisterConditional(HKEY("COND:SERVCFG:SUBST"), 4, ConditionalServCfgSubst, CTX_NONE);
-       RegisterIterator("PREF:ZONE", 0, ZoneHash, NULL, CfgZoneTempl, NULL, CTX_PREF, CTX_NONE, IT_NOFLAG);
+       RegisterConditional(HKEY("COND:SERVCFG:CTXSTRBUF"), 4, ConditionalServCfgCTXStrBuf, CTX_STRBUF);
+       RegisterIterator("PREF:ZONE", 0, ZoneHash, NULL, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
 
        REGISTERTokenParamDefine(roompolicy);
        REGISTERTokenParamDefine(floorpolicy);