]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
+ guess mimetype by template extension
[citadel.git] / webcit / subst.c
index 8dda6fb0d44124e4c3417060ba04565094d872c9..72659c9b2f351fa948dba0cbe4eaf78039b24f52 100644 (file)
@@ -48,6 +48,7 @@ typedef struct _WCTemplate {
        StrBuf *FileName;
        int nTokensUsed;
        int TokenSpace;
+       StrBuf *MimeType;
        WCTemplateToken **Tokens;
 } WCTemplate;
 
@@ -172,10 +173,16 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
                        ChrPtr(Error),
                        ChrPtr(TP->Tokens->FlatToken));
                */
+               StrBufPrintf(Info, "%s [%s]  %s; [%s]", 
+                            Type, 
+                            Err, 
+                            ChrPtr(Error), 
+                            ChrPtr(TP->Tokens->FlatToken));
+
 
                SerializeJson(Header, WildFireException(SKEY(TP->Tokens->FileName),
                                                        TP->Tokens->Line,
-                                                       Error,
+                                                       Info,
                                                        1), 1);
 /*
                SerializeJson(Header, WildFireMessage(SKEY(TP->Tokens->FileName),
@@ -194,10 +201,17 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
                        Type, 
                        ChrPtr(Error));
                */
-               SerializeJson(Header, WildFireException(HKEY(__FILE__), __LINE__, Error, 1), 1);
+               StrBufPrintf(Info, "%s [%s]  %s; [%s]", 
+                            Type, 
+                            Err, 
+                            ChrPtr(Error), 
+                            ChrPtr(TP->Tokens->FlatToken));
+               SerializeJson(Header, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
                WildFireSerializePayload(Header, WCC->HBuf, &WCC->nWildfireHeaders, NULL);
        }
        FreeStrBuf(&Header);
+       FreeStrBuf(&Info);
+       FreeStrBuf(&Error);
 /*
        if (dbg_bactrace_template_errors)
                wc_backtrace(); 
@@ -312,6 +326,7 @@ void FreeWCTemplate(void *vFreeMe)
        }
        FreeStrBuf(&FreeMe->FileName);
        FreeStrBuf(&FreeMe->Data);
+       FreeStrBuf(&FreeMe->MimeType);
        free(FreeMe);
 }
 
@@ -866,7 +881,6 @@ void StrBufAppendTemplate(StrBuf *Target,
                          const StrBuf *Source, int FormatTypeIndex)
 {
         wcsession *WCC;
-       StrBuf *Buf;
        char EscapeAs = ' ';
 
        if ((FormatTypeIndex < TP->Tokens->nParameters) &&
@@ -879,13 +893,7 @@ void StrBufAppendTemplate(StrBuf *Target,
        {
        case 'H':
                WCC = WC;
-               Buf = NewStrBufPlain(NULL, StrLength(Source));
-               StrBuf_RFC822_to_Utf8(Buf, 
-                                     Source, 
-                                     (WCC!=NULL)? WCC->DefaultCharset : NULL, 
-                                     NULL);
-               StrEscAppend(Target, Buf, NULL, 0, 0);
-               FreeStrBuf(&Buf);
+               StrEscAppend(Target, Source, NULL, 0, 2);
                break;
        case 'X':
                StrEscAppend(Target, Source, NULL, 0, 0);
@@ -1279,6 +1287,7 @@ void *load_template(StrBuf *filename, StrBuf *Key, HashList *PutThere)
        NewTemplate->nTokensUsed = 0;
        NewTemplate->TokenSpace = 0;
        NewTemplate->Tokens = NULL;
+       NewTemplate->MimeType = NewStrBufPlain(GuessMimeByFilename (SKEY(NewTemplate->FileName)), -1);
        if (StrBufReadBLOB(NewTemplate->Data, &fd, 1, statbuf.st_size, &Err) < 0) {
                close(fd);
                FreeWCTemplate(NewTemplate);
@@ -1498,7 +1507,7 @@ int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
 
 
 
-void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
+const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *CallingTP)
 {
        WCTemplate *pTmpl = Tmpl;
        int done = 0;
@@ -1523,7 +1532,7 @@ void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *Calling
                                Target, 
                                "<pre>\nError loading Template [%s]\n See Logfile for details\n</pre>\n", 
                                ChrPtr(Tmpl->FileName));
-                       return;
+                       return NULL;
                }
 
        }
@@ -1570,13 +1579,16 @@ void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams *Calling
        if (LoadTemplates != 0) {
                FreeWCTemplate(pTmpl);
        }
+       return Tmpl->MimeType;
+
 }
 
 /**
  * \brief Display a variable-substituted template
  * \param templatename template file to load
+ * \returns the mimetype of the template its doing
  */
-void DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP) 
+const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP) 
 {
        WCTemplputParams LocalTP;
        HashList *Static;
@@ -1603,7 +1615,7 @@ void DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputPa
        {
                lprintf (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;
+               return NULL;
        }
 
        if (!GetHash(StaticLocal, templatename, len, &vTmpl) &&
@@ -1616,11 +1628,12 @@ void DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputPa
                dbg_PrintHash(Static, PrintTemplate, NULL);
                PrintHash(Static, VarPrintTransition, PrintTemplate);
 #endif
-               return;
+               return NULL;
        }
        if (vTmpl == NULL) 
-               return;
-       ProcessTemplate(vTmpl, Target, TP);
+               return NULL;
+       return ProcessTemplate(vTmpl, Target, TP);
+
 }
 
 /*-----------------------------------------------------------------------------