Renamed all groupdav_() functions to dav_()
[citadel.git] / webcit / subst.c
index 49723377ae23f235662e81b9fb3e52a679063530..f1a01cf55807ce286e726afdbe55b60223d5e3bd 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);
 
 
@@ -109,6 +109,8 @@ const char *CtxNames[]  = {
        "Context DavNamespace",
        "Context TAB",
        "Context VCARD",
+       "Context SIEVE List",
+       "Context SIEVE Script",
        "Context UNKNOWN"
 };
 
@@ -390,35 +392,6 @@ void FreeWCTemplate(void *vFreeMe)
        free(FreeMe);
 }
 
-
-
-/**
- * \brief back end for print_value_of() ... does a server command
- * \param servcmd server command to execute on the citadel server
- */
-void pvo_do_cmd(StrBuf *Target, StrBuf *servcmd) {
-       char buf[SIZ];
-       int len;
-
-       serv_puts(ChrPtr(servcmd));
-       len = serv_getln(buf, sizeof buf);
-
-       switch(buf[0]) {
-               case '2':
-               case '3':
-               case '5':
-                       StrBufAppendPrintf(Target, "%s\n", &buf[4]);
-                       break;
-               case '1':
-                       _fmout(Target, "CENTER");
-                       break;
-               case '4':
-                       StrBufAppendPrintf(Target, "%s\n", &buf[4]);
-                       serv_puts("000");
-                       break;
-       }
-}
-
 int HaveTemplateTokenString(StrBuf *Target, 
                            WCTemplputParams *TP,
                            int N,
@@ -617,19 +590,19 @@ void StrBufAppendTemplate(StrBuf *Target,
                          WCTemplputParams *TP,
                          const StrBuf *Source, int FormatTypeIndex)
 {
-        wcsession *WCC;
+       const char *pFmt = NULL;
        char EscapeAs = ' ';
 
        if ((FormatTypeIndex < TP->Tokens->nParameters) &&
            (TP->Tokens->Params[FormatTypeIndex]->Type == TYPE_STR) &&
-           (TP->Tokens->Params[FormatTypeIndex]->len == 1)) {
-               EscapeAs = *TP->Tokens->Params[FormatTypeIndex]->Start;
+           (TP->Tokens->Params[FormatTypeIndex]->len >= 1)) {
+               pFmt = TP->Tokens->Params[FormatTypeIndex]->Start;
+               EscapeAs = *pFmt;
        }
 
        switch(EscapeAs)
        {
        case 'H':
-               WCC = WC;
                StrEscAppend(Target, Source, NULL, 0, 2);
                break;
        case 'X':
@@ -644,6 +617,12 @@ void StrBufAppendTemplate(StrBuf *Target,
        case 'U':
                StrBufUrlescAppend(Target, Source, NULL);
                break;
+       case 'F':
+               if (pFmt != NULL)       pFmt++;
+               else                    pFmt = "JUSTIFY";
+               if (*pFmt == '\0')      pFmt = "JUSTIFY";
+               FmOut(Target, pFmt, Source);
+               break;
        default:
                StrBufAppendBuf(Target, Source, 0);
        }
@@ -779,8 +758,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;
@@ -1037,6 +1018,25 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
        switch (NewToken->Flags) {
        case 0:
                /* If we're able to find out more about the token, do it now while its fresh. */
+               pch = NewToken->pName;
+               while (pch <  NewToken->pName + NewToken->NameEnd)
+               {
+                       if (((*pch >= 'A') && (*pch <= 'Z')) || 
+                           ((*pch >= '0') && (*pch <= '9')) ||
+                           (*pch == ':') || 
+                           (*pch == '-') ||
+                           (*pch == '_')) 
+                               pch ++;
+                       else
+                       {
+                               LogTemplateError(
+                                       NULL, "Token Name", ERR_NAME, &TP,
+                                       "contains illegal char: '%c'", 
+                                       *pch);
+                               pch++;
+                       }
+
+               }
                if (GetHash(GlobalNS, NewToken->pName, NewToken->NameEnd, &vVar)) {
                        HashHandler *Handler;
                        Handler = (HashHandler*) vVar;
@@ -1195,17 +1195,62 @@ 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;
        const char *pS, *pE, *pch, *Err;
        long Line;
-       int pos;
 
        fd = open(ChrPtr(NewTemplate->FileName), O_RDONLY);
        if (fd <= 0) {
@@ -1225,7 +1270,6 @@ void *load_template(WCTemplate *NewTemplate)
                close(fd);
                syslog(1, "ERROR: reading template '%s' - %s<br>\n",
                        ChrPtr(NewTemplate->FileName), strerror(errno));
-               //FreeWCTemplate(NewTemplate);/////tODO
                return NULL;
        }
        close(fd);
@@ -1237,12 +1281,10 @@ 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 <? > */
-               pos = (-1);
                for (; pch < pE; pch ++) {
                        if ((*pch=='<')&&(*(pch + 1)=='?') &&
                            !((pch == pS) && /* we must ommit a <?xml */
@@ -1258,13 +1300,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;
                        }
                }
@@ -1277,6 +1330,8 @@ void *load_template(WCTemplate *NewTemplate)
                        pch ++;
                }
        }
+
+       SanityCheckTemplate(NULL, NewTemplate);
        return NewTemplate;
 }
 
@@ -1323,7 +1378,7 @@ int LoadTemplateDir(const StrBuf *DirName, HashList *big, const StrBuf *BaseKey)
               (filedir_entry != NULL))
        {
                char *MinorPtr;
-               char *PStart;
+
 #ifdef _DIRENT_HAVE_D_NAMELEN
                d_namelen = filedir_entry->d_namelen;
                d_type = filedir_entry->d_type;
@@ -1400,7 +1455,6 @@ int LoadTemplateDir(const StrBuf *DirName, HashList *big, const StrBuf *BaseKey)
                            (strcmp(&filedir_entry->d_name[d_without_ext], ".orig") == 0) ||
                            (strcmp(&filedir_entry->d_name[d_without_ext], ".swp") == 0))
                                continue; /* Ignore backup files... */
-                       PStart = filedir_entry->d_name;
                        StrBufPrintf(FileName, "%s/%s", ChrPtr(DirName),  filedir_entry->d_name);
                        MinorPtr = strchr(filedir_entry->d_name, '.');
                        if (MinorPtr != NULL)
@@ -1476,7 +1530,7 @@ void InitTemplateCache(void)
                                              &vTemplate) && 
                               (vTemplate != NULL))
                        {
-                               load_template((WCTemplate *)vTemplate);
+                               load_template(NULL, (WCTemplate *)vTemplate);
                        }
                        DeleteHashPos(&At);
                }
@@ -1595,7 +1649,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", 
@@ -2164,9 +2218,9 @@ void tmpl_do_boxed(StrBuf *Target, WCTemplputParams *TP)
        memcpy (&SubTP, TP, sizeof(WCTemplputParams));
        SubTP.Context = Headline;
        SubTP.Filter.ContextType = CTX_STRBUF;
-       DoTemplate(HKEY("beginbox"), Target, &SubTP);
+       DoTemplate(HKEY("box_begin"), Target, &SubTP);
        DoTemplate(TKEY(0), Target, TP);
-       DoTemplate(HKEY("endbox"), Target, TP);
+       DoTemplate(HKEY("box_end"), Target, TP);
        FreeStrBuf(&Headline);
 }
 
@@ -2344,7 +2398,6 @@ CompareFunc RetrieveSort(WCTemplputParams *TP,
                         const char *OtherPrefix, long OtherPrefixLen,
                         const char *Default, long ldefault, long DefaultDirection)
 {
-       int isdefault = 0;
        const StrBuf *BSort = NULL;
        SortStruct *SortBy;
        void *vSortBy;
@@ -2378,7 +2431,6 @@ CompareFunc RetrieveSort(WCTemplputParams *TP,
 
        if (!GetHash(SortHash, SKEY(BSort), &vSortBy) || 
            (vSortBy == NULL)) {
-               isdefault = 1;
                if (!GetHash(SortHash, Default, ldefault, &vSortBy) || 
                    (vSortBy == NULL)) {
                        LogTemplateError(
@@ -2433,9 +2485,9 @@ enum {
 };
 
 ConstStr SortIcons[] = {
-       {HKEY("static/sort_none.gif")},
-       {HKEY("static/up_pointer.gif")},
-       {HKEY("static/down_pointer.gif")},
+       {HKEY("static/webcit_icons/sort_none.gif")},
+       {HKEY("static/webcit_icons/up_pointer.gif")},
+       {HKEY("static/webcit_icons/down_pointer.gif")},
 };
 
 ConstStr SortNextOrder[] = {