]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
Fix our problems with strings like this: "abc'def" in template token strings.
[citadel.git] / webcit / subst.c
index bec748d833ee4fb9b81f22774f62f615038fb432..907e1a34f289f3ed9f57239a9daab6da7e005306 100644 (file)
@@ -109,6 +109,8 @@ const char *CtxNames[]  = {
        "Context DavNamespace",
        "Context TAB",
        "Context VCARD",
+       "Context SIEVE List",
+       "Context SIEVE Script",
        "Context UNKNOWN"
 };
 
@@ -157,7 +159,7 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
        }
        if (TP->Tokens != NULL) 
        {
-               lprintf(1, "%s [%s]  (in '%s' line %ld); %s; [%s]\n", 
+               syslog(1, "%s [%s]  (in '%s' line %ld); %s; [%s]\n", 
                        Type, 
                        Err, 
                        ChrPtr(TP->Tokens->FileName),
@@ -167,7 +169,7 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
        }
        else 
        {
-               lprintf(1, "%s: %s;\n", 
+               syslog(1, "%s: %s;\n", 
                        Type, 
                        ChrPtr(Error));
        }
@@ -256,7 +258,7 @@ void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
        StrBufVAppendPrintf(Error, Format, arg_ptr);
        va_end(arg_ptr);
 
-       lprintf(1, ChrPtr(Error));
+       syslog(1, "%s", ChrPtr(Error));
 
        WCC = WC;
        if (WCC->WFBuf == NULL) WCC->WFBuf = NewStrBuf();
@@ -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);
        }
@@ -765,7 +744,7 @@ int GetNextParameter(StrBuf *Buf,
                }
                pche = pch;
                if (*pch != quote) {
-                       lprintf(1, "Error (in '%s' line %ld); "
+                       syslog(1, "Error (in '%s' line %ld); "
                                "evaluating template param [%s] in Token [%s]\n",
                                ChrPtr(pTmpl->FileName),
                                Tokens->Line,
@@ -779,7 +758,7 @@ int GetNextParameter(StrBuf *Buf,
                else {
                        StrBufPeek(Buf, pch, -1, '\0');         
                        if (LoadTemplates > 1) {                        
-                               lprintf(1, "DBG: got param [%s] %ld %ld\n", 
+                               syslog(1, "DBG: got param [%s] %ld %ld\n", 
                                        pchs, pche - pchs, strlen(pchs));
                        }
                        Parm->Start = pchs;
@@ -809,7 +788,7 @@ int GetNextParameter(StrBuf *Buf,
                else {
                        Parm->lvalue = 0;
 /* TODO whUT?
-                       lprintf(1, "Error (in '%s' line %ld); "
+                       syslog(1, "Error (in '%s' line %ld); "
                                "evaluating long template param [%s] in Token [%s]\n",
                                ChrPtr(pTmpl->FileName),
                                Tokens->Line,
@@ -1003,7 +982,7 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                                                     &NewToken->Params[NewToken->nParameters - 1]))
                                {
                                        NewToken->HaveParameters = 1;
-                                       if (NewToken->nParameters > MAXPARAM) {
+                                       if (NewToken->nParameters >= MAXPARAM) {
                                                LogTemplateError(
                                                        NULL, "Parseerror", ERR_NAME, &TP,
                                                        "only [%d] Params allowed in Tokens",
@@ -1037,6 +1016,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;
@@ -1205,17 +1203,16 @@ void *load_template(WCTemplate *NewTemplate)
        struct stat statbuf;
        const char *pS, *pE, *pch, *Err;
        long Line;
-       int pos;
 
        fd = open(ChrPtr(NewTemplate->FileName), O_RDONLY);
        if (fd <= 0) {
-               lprintf(1, "ERROR: could not open template '%s' - %s\n",
+               syslog(1, "ERROR: could not open template '%s' - %s\n",
                        ChrPtr(NewTemplate->FileName), strerror(errno));
                return NULL;
        }
 
        if (fstat(fd, &statbuf) == -1) {
-               lprintf(1, "ERROR: could not stat template '%s' - %s\n",
+               syslog(1, "ERROR: could not stat template '%s' - %s\n",
                        ChrPtr(NewTemplate->FileName), strerror(errno));
                return NULL;
        }
@@ -1223,9 +1220,8 @@ void *load_template(WCTemplate *NewTemplate)
        NewTemplate->Data = NewStrBufPlain(NULL, statbuf.st_size + 1);
        if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
                close(fd);
-               lprintf(1, "ERROR: reading template '%s' - %s<br>\n",
+               syslog(1, "ERROR: reading template '%s' - %s<br>\n",
                        ChrPtr(NewTemplate->FileName), strerror(errno));
-               //FreeWCTemplate(NewTemplate);/////tODO
                return NULL;
        }
        close(fd);
@@ -1237,11 +1233,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 */
@@ -1257,22 +1252,35 @@ 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;
                        }
                }
                if (pch + 1 > pE)
                        continue;
                pte = pch;
-               PutNewToken(NewTemplate, 
-                           NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate));
-               pch ++;
+               pv = NewTemplateSubstitute(NewTemplate->Data, pS, pts, pte, Line, NewTemplate);
+               if (pv != NULL) {
+                       PutNewToken(NewTemplate, pv);
+                       pch ++;
+               }
        }
        return NewTemplate;
 }
@@ -1320,7 +1328,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;
@@ -1397,7 +1405,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)
@@ -1411,7 +1418,7 @@ int LoadTemplateDir(const StrBuf *DirName, HashList *big, const StrBuf *BaseKey)
                        StrBufAppendBufPlain(Key, filedir_entry->d_name, MinorPtr - filedir_entry->d_name, 0);
 
                        if (LoadTemplates >= 1)
-                               lprintf(1, "%s %s\n", ChrPtr(FileName), ChrPtr(Key));
+                               syslog(1, "%s %s\n", ChrPtr(FileName), ChrPtr(Key));
                        prepare_template(FileName, Key, big);
                default:
                        break;
@@ -1505,7 +1512,7 @@ int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
        void *vVar;
        
 /* much output, since pName is not terminated...
-       lprintf(1,"Doing token: %s\n",Token->pName);
+       syslog(1,"Doing token: %s\n",Token->pName);
 */
 
        switch (TP->Tokens->Flags) {
@@ -1589,7 +1596,7 @@ const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams
 
        if (LoadTemplates != 0) {                       
                if (LoadTemplates > 1)
-                       lprintf(1, "DBG: ----- loading:  [%s] ------ \n", 
+                       syslog(1, "DBG: ----- loading:  [%s] ------ \n", 
                                ChrPtr(Tmpl->FileName));
                pTmpl = duplicate_template(Tmpl);
                if(load_template(pTmpl) == NULL) {
@@ -1673,14 +1680,14 @@ const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCT
 
        if (len == 0)
        {
-               lprintf (1, "Can't to load a template with empty name!\n");
+               syslog(1, "Can't to load a template with empty name!\n");
                StrBufAppendPrintf(Target, "<pre>\nCan't to load a template with empty name!\n</pre>");
                return NULL;
        }
 
        if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
            !GetHash(Static, templatename, len, &vTmpl)) {
-               lprintf (1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
+               syslog(1, "didn't find Template [%s] %ld %ld\n", templatename, len , (long)strlen(templatename));
                StrBufAppendPrintf(Target, "<pre>\ndidn't find Template [%s] %ld %ld\n</pre>", 
                                   templatename, len, 
                                   (long)strlen(templatename));
@@ -1976,6 +1983,8 @@ int conditional_ITERATE_LASTN(StrBuf *Target, WCTemplputParams *TP)
 int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
 {
        ConditionalStruct *Cond;
+       int rc = 0;
+       int res;
 
        if ((TP->Tokens->Params[0]->len == 1) &&
            (TP->Tokens->Params[0]->Start[0] == 'X'))
@@ -1992,10 +2001,14 @@ int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP
        if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
                return 0;
        }
-
-       if (Cond->CondF(Target, TP) == Neg)
-               return TP->Tokens->Params[1]->lvalue;
-       return 0;
+       res = Cond->CondF(Target, TP);
+       if (res == Neg)
+               rc = TP->Tokens->Params[1]->lvalue;
+       if (LoadTemplates > 5) 
+               syslog(1, "<%s> : %d %d==%d\n", 
+                       ChrPtr(TP->Tokens->FlatToken), 
+                       rc, res, Neg);
+       return rc;
 }
 
 void RegisterConditional(const char *Name, long len, 
@@ -2155,9 +2168,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);
 }
 
@@ -2324,7 +2337,7 @@ void RegisterSortFunc(const char *name, long len,
        NewSort->GroupChange = GroupChange;
        NewSort->ContextType = ContextType;
        if (ContextType == CTX_NONE) {
-               lprintf(1, "sorting requires a context. CTX_NONE won't make it.\n");
+               syslog(1, "sorting requires a context. CTX_NONE won't make it.\n");
                exit(1);
        }
                
@@ -2335,7 +2348,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;
@@ -2369,7 +2381,6 @@ CompareFunc RetrieveSort(WCTemplputParams *TP,
 
        if (!GetHash(SortHash, SKEY(BSort), &vSortBy) || 
            (vSortBy == NULL)) {
-               isdefault = 1;
                if (!GetHash(SortHash, Default, ldefault, &vSortBy) || 
                    (vSortBy == NULL)) {
                        LogTemplateError(
@@ -2424,9 +2435,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[] = {
@@ -2620,7 +2631,7 @@ void dbg_print_longvector(long *LongVector)
                        StrBufAppendPrintf(Buf, "%d: %ld]\n", i, LongVector[i]);
 
        }
-       lprintf(1, ChrPtr(Buf));
+       syslog(1, "%s", ChrPtr(Buf));
        FreeStrBuf(&Buf);
 }