Add a 'go' value to the 'edit room configuration' tab so it automatically selects...
[citadel.git] / webcit / subst.c
index 54a59a425487e7ec8c45c9a5997dfb5c367fc922..1a8d029f0eef959e6f96c4ae65bd6d7b6219c1d9 100644 (file)
@@ -71,7 +71,7 @@ typedef struct _HashHandler {
        WCHandlerFunc HandlerFunc;
 }HashHandler;
 
-void *load_template(WCTemplate *NewTemplate);
+void *load_template(StrBuf *Target, WCTemplate *NewTemplate);
 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP);
 
 
@@ -111,6 +111,9 @@ const char *CtxNames[]  = {
        "Context VCARD",
        "Context SIEVE List",
        "Context SIEVE Script",
+       "Context MailQ-Item",
+       "Context MailQ-Recipient",
+       "Context ServLogStatus",
        "Context UNKNOWN"
 };
 
@@ -581,10 +584,10 @@ long GetTemplateTokenNumber(StrBuf *Target, WCTemplputParams *TP, int N, long df
 }
 
 
-/**
- * \brief puts string into the template and computes which escape methon we should use
- * \param Source the string we should put into the template
- * \param FormatTypeIndex where should we look for escape types if?
+/*
+ * puts string into the template and computes which escape methon we should use
+ * Source = the string we should put into the template
+ * FormatTypeIndex = where should we look for escape types if?
  */
 void StrBufAppendTemplate(StrBuf *Target, 
                          WCTemplputParams *TP,
@@ -758,8 +761,10 @@ int GetNextParameter(StrBuf *Buf,
                else {
                        StrBufPeek(Buf, pch, -1, '\0');         
                        if (LoadTemplates > 1) {                        
-                               syslog(1, "DBG: got param [%s] %ld %ld\n", 
-                                       pchs, pche - pchs, strlen(pchs));
+                               syslog(1,
+                                       "DBG: got param [%s] %d %d\n", 
+                                       pchs, pche - pchs, strlen(pchs)
+                               );
                        }
                        Parm->Start = pchs;
                        Parm->len = pche - pchs;
@@ -1193,11 +1198,57 @@ void *duplicate_template(WCTemplate *OldTemplate)
        return NewTemplate;
 }
 
+
+void SanityCheckTemplate(StrBuf *Target, WCTemplate *CheckMe)
+{
+       int i = 0;
+       int j;
+       int FoundConditionalEnd;
+
+       for (i = 0; i < CheckMe->nTokensUsed; i++)
+       {
+               switch(CheckMe->Tokens[i]->Flags)
+               {
+               case SV_CONDITIONAL:
+               case SV_NEG_CONDITIONAL:
+                       FoundConditionalEnd = 0;
+                       if ((CheckMe->Tokens[i]->Params[0]->len == 1) && 
+                           (CheckMe->Tokens[i]->Params[0]->Start[0] == 'X'))
+                               break;
+                       for (j = i + 1; j < CheckMe->nTokensUsed; j++)
+                       {
+                               if (((CheckMe->Tokens[j]->Flags == SV_CONDITIONAL) ||
+                                    (CheckMe->Tokens[j]->Flags == SV_NEG_CONDITIONAL)) && 
+                                   (CheckMe->Tokens[i]->Params[1]->lvalue == 
+                                    CheckMe->Tokens[j]->Params[1]->lvalue))
+                               {
+                                       FoundConditionalEnd = 1;
+                                       break;
+                               }
+
+                       }
+                       if (!FoundConditionalEnd)
+                       {
+                               WCTemplputParams TP;
+                               memset(&TP, 0, sizeof(WCTemplputParams));
+                               TP.Tokens = CheckMe->Tokens[i];
+                               LogTemplateError(
+                                       Target, "Token", ERR_PARM1, &TP,
+                                       "Conditional without Endconditional"
+                                       );
+                       }
+                       break;
+               default:
+                       break;
+               }
+       }
+}
+
 /**
  * \brief Display a variable-substituted template
  * \param templatename template file to load
  */
-void *load_template(WCTemplate *NewTemplate)
+void *load_template(StrBuf *Target, WCTemplate *NewTemplate)
 {
        int fd;
        struct stat statbuf;
@@ -1233,8 +1284,7 @@ void *load_template(WCTemplate *NewTemplate)
        pE = pS + StrLength(NewTemplate->Data);
        while (pch < pE) {
                const char *pts, *pte;
-               int InQuotes = 0;
-               int InDoubleQuotes = 0;
+               char InQuotes = '\0';
                void *pv;
 
                /** Find one <? > */
@@ -1253,13 +1303,24 @@ void *load_template(WCTemplate *NewTemplate)
 
                /** Found one? parse it. */
                for (; pch <= pE - 1; pch ++) {
-                       if (*pch == '"')
-                               InDoubleQuotes = ! InDoubleQuotes;
-                       else if (*pch == '\'')
-                               InQuotes = ! InQuotes;
-                       else if ((!InQuotes  && !InDoubleQuotes) &&
-                                ((*pch!='\\')&&(*(pch + 1)=='>'))) {
-                               pch ++;
+                       if ((!InQuotes) &&
+                           ((*pch == '\'') || (*pch == '"')))
+                       {
+                               InQuotes = *pch;
+                       }
+                       else if (InQuotes && (InQuotes == *pch))
+                       {
+                               InQuotes = '\0';
+                       }
+                       else if ((InQuotes) &&
+                                (*pch == '\\') &&
+                                (*(pch + 1) == InQuotes))
+                       {
+                               pch++;
+                       }
+                       else if ((!InQuotes) && 
+                                (*pch == '>'))
+                       {
                                break;
                        }
                }
@@ -1272,6 +1333,8 @@ void *load_template(WCTemplate *NewTemplate)
                        pch ++;
                }
        }
+
+       SanityCheckTemplate(NULL, NewTemplate);
        return NewTemplate;
 }
 
@@ -1470,7 +1533,7 @@ void InitTemplateCache(void)
                                              &vTemplate) && 
                               (vTemplate != NULL))
                        {
-                               load_template((WCTemplate *)vTemplate);
+                               load_template(NULL, (WCTemplate *)vTemplate);
                        }
                        DeleteHashPos(&At);
                }
@@ -1589,7 +1652,7 @@ const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams
                        syslog(1, "DBG: ----- loading:  [%s] ------ \n", 
                                ChrPtr(Tmpl->FileName));
                pTmpl = duplicate_template(Tmpl);
-               if(load_template(pTmpl) == NULL) {
+               if(load_template(Target, pTmpl) == NULL) {
                        StrBufAppendPrintf(
                                Target, 
                                "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n",