* add contexttype, so the subst can precheck the context pointer for us, and bad...
[citadel.git] / webcit / siteconfig.c
index aa034ca83a49e790ebe4a7d57343718591bf7cf1..1a760033570b3befe85ae9801ed1243e8439d553 100644 (file)
@@ -18,21 +18,18 @@ void LoadZoneFiles(void)
        icalarray *zones;
        int z;
        long len;
-       char this_zone[128];
-       char *ZName;
+       const char *this_zone;
+       StrBuf *ZName;
        
        ZoneHash = NewHash(1, NULL);
-       len = sizeof("UTC") + 1;
-       ZName = malloc(len + 1);
-       memcpy(ZName, "UTC", len + 1);
-       Put(ZoneHash, ZName, len, ZName, NULL);
+       ZName = NewStrBufPlain(HKEY("UTC"));
+       Put(ZoneHash, HKEY("UTC"), ZName, HFreeStrBuf);
        zones = icaltimezone_get_builtin_timezones();
        for (z = 0; z < zones->num_elements; ++z) {
-               strcpy(this_zone, icaltimezone_get_location(icalarray_element_at(zones, z)));
+               this_zone = icaltimezone_get_location(icalarray_element_at(zones, z));
                len = strlen(this_zone);
-               ZName = (char*)malloc(len +1);
-               memcpy(ZName, this_zone, len + 1);
-               Put(ZoneHash, ZName, len, ZName, NULL);
+               ZName = NewStrBufPlain(this_zone, len);
+               Put(ZoneHash, this_zone, len, ZName, HFreeStrBuf);
        }
        SortByHashKey(ZoneHash, 0);
 }
@@ -883,19 +880,28 @@ void load_siteconfig(void)
        serv_printf("CONF get");
        serv_getln(buf, sizeof buf);
        i = 0;
-       while (len = serv_getln(buf, sizeof buf), 
-              strcmp(buf, "000") && 
-              (i < sizeof(ServerConfig))) 
+       Buf = NewStrBuf();
+       while ((sizeof(ServerConfig) / sizeof(CfgMapping)) &&
+              (len = StrBuf_ServGetln(Buf),
+               strcmp(ChrPtr(Buf), "000")) && 
+              (i <= sizeof(ServerConfig))) 
        {
                Put(Cfg,
                    ServerConfig[i].Key, 
                    ServerConfig[i].len, 
-                   NewStrBufPlain(buf, len)
+                   Buf
                    HFreeStrBuf);
                i++;
+               if (i <= sizeof(ServerConfig) / sizeof(CfgMapping))
+                       Buf = NewStrBuf();
+               else
+                       Buf = NULL;                     
        }
+       FreeStrBuf(&Buf);
 
        serv_puts("GPEX site");
+       Buf = NewStrBuf();
+       Token = NULL;
        StrBuf_ServGetln(Buf);
        if (ChrPtr(Buf)[0] == '2') {
                StrBufCutLeft(Buf, 4);
@@ -930,6 +936,7 @@ void load_siteconfig(void)
  */
 void siteconfig(void)
 {
+       struct wcsession *WCC = WC;
        int i;
        char buf[256];
 
@@ -940,13 +947,12 @@ void siteconfig(void)
        serv_printf("CONF set");
        serv_getln(buf, sizeof buf);
        if (buf[0] != '4') {
-               safestrncpy(WC->ImportantMessage, &buf[4], sizeof WC->ImportantMessage);
+               safestrncpy(WCC->ImportantMessage, &buf[4], sizeof WCC->ImportantMessage);
                display_aide_menu();
                return;
        }
 
-       i = sizeof(ServerConfig);
-       for (i=0; i < sizeof(ServerConfig); i ++)
+       for (i=0; i < (sizeof(ServerConfig) / sizeof(CfgMapping)); i ++)
        {
                switch (ServerConfig[i].type) {
                default:
@@ -965,6 +971,7 @@ void siteconfig(void)
                        break;
                }
        }
+        serv_puts("000");
 
        serv_printf("SPEX site|%d|%d", ibstr("sitepolicy"), ibstr("sitevalue"));
        serv_getln(buf, sizeof buf);
@@ -973,12 +980,13 @@ void siteconfig(void)
 
        strcpy(serv_info.serv_default_cal_zone, bstr("c_default_cal_zone"));
 
-       safestrncpy(WC->ImportantMessage, _("Your system configuration has been updated."),
-               sizeof WC->ImportantMessage);
+       safestrncpy(WCC->ImportantMessage, _("Your system configuration has been updated."),
+               sizeof WCC->ImportantMessage);
+       DeleteHash(&WCC->ServCfg);
        display_aide_menu();
 }
 
-void tmplput_servcfg(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context)
+void tmplput_servcfg(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        struct wcsession *WCC = WC;
        void *vBuf;
@@ -996,7 +1004,7 @@ void tmplput_servcfg(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *C
        }
 }
 
-int ConditionalServCfg(WCTemplateToken *Tokens, void *Context)
+int ConditionalServCfg(WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        struct wcsession *WCC = WC;
        void *vBuf;
@@ -1006,15 +1014,38 @@ int ConditionalServCfg(WCTemplateToken *Tokens, void *Context)
                if (WCC->ServCfg == NULL)
                        load_siteconfig();
                GetHash(WCC->ServCfg, 
-                       Tokens->Params[0]->Start,
-                       Tokens->Params[0]->len, 
+                       Tokens->Params[2]->Start,
+                       Tokens->Params[2]->len, 
                        &vBuf);
                if (vBuf == NULL) return 0;
                Buf = (StrBuf*) vBuf;
-               if (Tokens->nParameters == 1)
+               if (Tokens->nParameters == 3) {
                        return 1;
-               else 
-                       return (strcmp(Tokens->Params[0]->Start, ChrPtr(Buf)) == 0);
+               }
+               else if (Tokens->Params[3]->Type == TYPE_STR)
+                       return (strcmp(Tokens->Params[3]->Start, ChrPtr(Buf)) == 0);
+               else return (StrTol(Buf) == Tokens->Params[3]->lvalue);
+       }
+       else return 0;
+}
+
+int ConditionalServCfgSubst(WCTemplateToken *Tokens, void *Context, int ContextType)
+{
+       struct wcsession *WCC = WC;
+       void *vBuf;
+       StrBuf *Buf;
+
+       if (WCC->is_aide) {
+               if (WCC->ServCfg == NULL)
+                       load_siteconfig();
+               GetHash(WCC->ServCfg, 
+                       Tokens->Params[2]->Start,
+                       Tokens->Params[2]->len, 
+                       &vBuf);
+               if (vBuf == NULL) return 0;
+               Buf = (StrBuf*) vBuf;
+
+               return CompareSubstToStrBuf(Buf, Tokens->Params[3]);
        }
        else return 0;
 }
@@ -1023,10 +1054,11 @@ void
 InitModule_SITECONFIG
 (void)
 {
-       WebcitAddUrlHandler(HKEY("display_siteconfig"), display_siteconfig, 0);
-       WebcitAddUrlHandler(HKEY("siteconfig"), siteconfig, 0);
+       WebcitAddUrlHandler(HKEY("display_siteconfig"), display_siteconfig, CTX_NONE);
+       WebcitAddUrlHandler(HKEY("siteconfig"), siteconfig, CTX_NONE);
 
-       RegisterNamespace("SERV:CFG", 1, 1, tmplput_servcfg);
-       RegisterConditional(HKEY("COND:SERVCFG"), 1, ConditionalServCfg);
+       RegisterNamespace("SERV:CFG", 1, 1, tmplput_servcfg, CTX_SITECFG);
+       RegisterConditional(HKEY("COND:SERVCFG"), 3, ConditionalServCfg, CTX_NONE);
+       RegisterConditional(HKEY("COND:SERVCFG:SUBST"), 4, ConditionalServCfgSubst, CTX_NONE);
 }
 /*@}*/