]> code.citadel.org Git - citadel.git/blobdiff - webcit/msg_renderers.c
* make readloop long-controlled
[citadel.git] / webcit / msg_renderers.c
index f294c5e3537ab894babe9b943775687e508eb587..005288380fe1db905e0835e707f704e524210115 100644 (file)
@@ -68,6 +68,116 @@ void RegisterMimeRenderer(const char *HeaderName, long HdrNLen, RenderMimeFunc M
 
 /*----------------------------------------------------------------------------*/
 
+/*
+ * qsort() compatible function to compare two longs in descending order.
+ */
+int longcmp_r(const void *s1, const void *s2) {
+       long l1;
+       long l2;
+
+       l1 = *(long *)GetSearchPayload(s1);
+       l2 = *(long *)GetSearchPayload(s2);
+
+       if (l1 > l2) return(-1);
+       if (l1 < l2) return(+1);
+       return(0);
+}
+
+/*
+ * qsort() compatible function to compare two longs in descending order.
+ */
+int qlongcmp_r(const void *s1, const void *s2) {
+       long l1 = (long) s1;
+       long l2 = (long) s2;
+
+       if (l1 > l2) return(-1);
+       if (l1 < l2) return(+1);
+       return(0);
+}
+
+/*
+ * qsort() compatible function to compare two message summary structs by ascending subject.
+ */
+int summcmp_subj(const void *s1, const void *s2) {
+       message_summary *summ1;
+       message_summary *summ2;
+       
+       summ1 = (message_summary *)GetSearchPayload(s1);
+       summ2 = (message_summary *)GetSearchPayload(s2);
+       return strcasecmp(ChrPtr(summ1->subj), ChrPtr(summ2->subj));
+}
+
+/*
+ * qsort() compatible function to compare two message summary structs by descending subject.
+ */
+int summcmp_rsubj(const void *s1, const void *s2) {
+       message_summary *summ1;
+       message_summary *summ2;
+       
+       summ1 = (message_summary *)GetSearchPayload(s1);
+       summ2 = (message_summary *)GetSearchPayload(s2);
+       return strcasecmp(ChrPtr(summ2->subj), ChrPtr(summ1->subj));
+}
+
+/*
+ * qsort() compatible function to compare two message summary structs by ascending sender.
+ */
+int summcmp_sender(const void *s1, const void *s2) {
+       message_summary *summ1;
+       message_summary *summ2;
+       
+       summ1 = (message_summary *)GetSearchPayload(s1);
+       summ2 = (message_summary *)GetSearchPayload(s2);
+       return strcasecmp(ChrPtr(summ1->from), ChrPtr(summ2->from));
+}
+
+/*
+ * qsort() compatible function to compare two message summary structs by descending sender.
+ */
+int summcmp_rsender(const void *s1, const void *s2) {
+       message_summary *summ1;
+       message_summary *summ2;
+       
+       summ1 = (message_summary *)GetSearchPayload(s1);
+       summ2 = (message_summary *)GetSearchPayload(s2);
+       return strcasecmp(ChrPtr(summ2->from), ChrPtr(summ1->from));
+}
+
+/*
+ * qsort() compatible function to compare two message summary structs by ascending date.
+ */
+int summcmp_date(const void *s1, const void *s2) {
+       message_summary *summ1;
+       message_summary *summ2;
+       
+       summ1 = (message_summary *)GetSearchPayload(s1);
+       summ2 = (message_summary *)GetSearchPayload(s2);
+
+       if (summ1->date < summ2->date) return -1;
+       else if (summ1->date > summ2->date) return +1;
+       else return 0;
+}
+
+/*
+ * qsort() compatible function to compare two message summary structs by descending date.
+ */
+int summcmp_rdate(const void *s1, const void *s2) {
+       message_summary *summ1;
+       message_summary *summ2;
+       
+       summ1 = (message_summary *)GetSearchPayload(s1);
+       summ2 = (message_summary *)GetSearchPayload(s2);
+
+       if (summ1->date < summ2->date) return +1;
+       else if (summ1->date > summ2->date) return -1;
+       else return 0;
+}
+
+/*----------------------------------------------------------------------------*/
+
+
+
 
 void examine_nhdr(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
 {
@@ -94,10 +204,10 @@ void examine_from(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
        Msg->from = NewStrBufPlain(NULL, StrLength(HdrLine));
        StrBuf_RFC822_to_Utf8(Msg->from, HdrLine, WC->DefaultCharset, FoundCharset);
 }
-void tmplput_MAIL_SUMM_FROM(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_FROM(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->from, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->from, 0);
 }
 
 
@@ -108,10 +218,10 @@ void examine_subj(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
        Msg->subj = NewStrBufPlain(NULL, StrLength(HdrLine));
        StrBuf_RFC822_to_Utf8(Msg->subj, HdrLine, WC->DefaultCharset, FoundCharset);
 }
-void tmplput_MAIL_SUMM_SUBJECT(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_SUBJECT(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {/////TODO: Fwd: and RE: filter!!
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->subj, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->subj, 0);
 }
 
 
@@ -121,10 +231,10 @@ void examine_msgn(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
        Msg->reply_inreplyto = NewStrBufPlain(NULL, StrLength(HdrLine));
        StrBuf_RFC822_to_Utf8(Msg->reply_inreplyto, HdrLine, WC->DefaultCharset, FoundCharset);
 }
-void tmplput_MAIL_SUMM_INREPLYTO(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_INREPLYTO(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->reply_inreplyto, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->reply_inreplyto, 0);
 }
 
 int Conditional_MAIL_SUMM_UNREAD(WCTemplateToken *Tokens, void *Context, int ContextType)
@@ -139,10 +249,10 @@ void examine_wefw(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
        Msg->reply_references = NewStrBufPlain(NULL, StrLength(HdrLine));
        StrBuf_RFC822_to_Utf8(Msg->reply_references, HdrLine, WC->DefaultCharset, FoundCharset);
 }
-void tmplput_MAIL_SUMM_REFIDS(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_REFIDS(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->reply_references, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->reply_references, 0);
 }
 
 
@@ -158,10 +268,10 @@ void examine_cccc(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
        }
        StrBufAppendBuf(Msg->AllRcpt, Msg->cccc, 0);
 }
-void tmplput_MAIL_SUMM_CCCC(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_CCCC(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->cccc, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->cccc, 0);
 }
 
 
@@ -175,10 +285,10 @@ void examine_room(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
                Msg->Room = NewStrBufDup(HdrLine);              
        }
 }
-void tmplput_MAIL_SUMM_ORGROOM(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_ORGROOM(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->Room, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->Room, 0);
 }
 
 
@@ -187,10 +297,10 @@ void examine_rfca(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
        FreeStrBuf(&Msg->Rfca);
        Msg->Rfca = NewStrBufDup(HdrLine);
 }
-void tmplput_MAIL_SUMM_RFCA(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_RFCA(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->Rfca, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->Rfca, 0);
 }
 int Conditional_MAIL_SUMM_RFCA(WCTemplateToken *Tokens, void *Context, int ContextType)
 {
@@ -208,10 +318,10 @@ void examine_node(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
                Msg->OtherNode = NewStrBufDup(HdrLine);
        }
 }
-void tmplput_MAIL_SUMM_OTHERNODE(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_OTHERNODE(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->OtherNode, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->OtherNode, 0);
 }
 int Conditional_MAIL_SUMM_OTHERNODE(WCTemplateToken *Tokens, void *Context, int ContextType)
 {
@@ -232,15 +342,15 @@ void examine_rcpt(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
        }
        StrBufAppendBuf(Msg->AllRcpt, Msg->to, 0);
 }
-void tmplput_MAIL_SUMM_TO(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_TO(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->to, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->to, 0);
 }
-void tmplput_MAIL_SUMM_ALLRCPT(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_ALLRCPT(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->AllRcpt, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->AllRcpt, 0);
 }
 
 
@@ -249,14 +359,14 @@ void examine_time(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
 {
        Msg->date = StrTol(HdrLine);
 }
-void tmplput_MAIL_SUMM_DATE_STR(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_DATE_STR(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        char datebuf[64];
        message_summary *Msg = (message_summary*) Context;
        webcit_fmt_date(datebuf, Msg->date, 1);
        StrBufAppendBufPlain(Target, datebuf, -1, 0);
 }
-void tmplput_MAIL_SUMM_DATE_NO(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_DATE_NO(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
        StrBufAppendPrintf(Target, "%ld", Msg->date, 0);
@@ -325,7 +435,8 @@ void examine_mime_part(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundChars
        Buf = NewStrBuf();
 
        Mime->Name = NewStrBuf();
-       StrBufExtract_token(Mime->Name, HdrLine, 0, '|');
+       StrBufExtract_token(Buf, HdrLine, 0, '|');
+       StrBuf_RFC822_to_Utf8(Mime->Name, Buf, WC->DefaultCharset, FoundCharset);
        StrBufTrim(Mime->Name);
 
        StrBufExtract_token(Buf, HdrLine, 1, '|');
@@ -400,7 +511,8 @@ void evaluate_mime_part(message_summary *Msg, wc_mime_attachment *Mime)
                if (Msg->AttachLinks == NULL)
                        Msg->AttachLinks = NewHash(1,NULL);
                Put(Msg->AttachLinks, SKEY(Mime->PartNum), Mime, reference_free_handler);
-               if (strcasecmp(ChrPtr(Mime->ContentType), "application/octet-stream") == 0) {
+               if ((strcasecmp(ChrPtr(Mime->ContentType), "application/octet-stream") == 0) && 
+                   (StrLength(Mime->FileName) > 0)) {
                        FlushStrBuf(Mime->ContentType);
                        StrBufAppendBufPlain(Mime->ContentType,
                                             GuessMimeByFilename(SKEY(Mime->FileName)),
@@ -409,7 +521,7 @@ void evaluate_mime_part(message_summary *Msg, wc_mime_attachment *Mime)
        }
 }
 
-void tmplput_MAIL_SUMM_NATTACH(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_NATTACH(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
        StrBufAppendPrintf(Target, "%ld", GetCount(Msg->Attachments));
@@ -427,10 +539,10 @@ void examine_hnod(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
        Msg->hnod = NewStrBufPlain(NULL, StrLength(HdrLine));
        StrBuf_RFC822_to_Utf8(Msg->hnod, HdrLine, WC->DefaultCharset, FoundCharset);
 }
-void tmplput_MAIL_SUMM_H_NODE(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_H_NODE(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->hnod, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->hnod, 0);
 }
 int Conditional_MAIL_SUMM_H_NODE(WCTemplateToken *Tokens, void *Context, int ContextType)
 {
@@ -463,34 +575,57 @@ void examine_content_lengh(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundC
 }
 
 void examine_content_type(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
-{////TODO
-       int len, i;
+{
+       void *vHdr;
+       headereval *Hdr;
+       StrBuf *Token;
+       StrBuf *Value;
+       const char* sem;
+       const char *eq;
+       int len;
+       StrBufTrim(HdrLine);
        Msg->MsgBody->ContentType = NewStrBufDup(HdrLine);
-       StrBufTrim(Msg->MsgBody->ContentType);/////todo==striplt?
-       len = StrLength(Msg->MsgBody->ContentType);
-       for (i=0; i<len; ++i) {
-               if (!strncasecmp(ChrPtr(Msg->MsgBody->ContentType) + i, "charset=", 8)) {/// TODO: WHUT?
-//                     safestrncpy(mime_charset, &mime_content_type[i+8],
-                       ///                         sizeof mime_charset);
-               }
-       }/****
-       for (i=0; i<len; ++i) {
-               if (mime_content_type[i] == ';') {
-                       mime_content_type[i] = 0;
-                       len = i - 1;
-               }
-       }
-       len = strlen(mime_charset);
-       for (i=0; i<len; ++i) {
-               if (mime_charset[i] == ';') {
-                       mime_charset[i] = 0;
-                       len = i - 1;
+       sem = strchr(ChrPtr(HdrLine), ';');
+
+       if (sem != NULL) {
+               Token = NewStrBufPlain(NULL, StrLength(HdrLine));
+               Value = NewStrBufPlain(NULL, StrLength(HdrLine));
+               len = sem - ChrPtr(HdrLine);
+               StrBufCutAt(Msg->MsgBody->ContentType, len, NULL);
+               while (sem != NULL) {
+                       while (isspace(*(sem + 1)))
+                               sem ++;
+                       StrBufCutLeft(HdrLine, sem - ChrPtr(HdrLine));
+                       sem = strchr(ChrPtr(HdrLine), ';');
+                       if (sem != NULL)
+                               len = sem - ChrPtr(HdrLine);
+                       else
+                               len = StrLength(HdrLine);
+                       FlushStrBuf(Token);
+                       FlushStrBuf(Value);
+                       StrBufAppendBufPlain(Token, ChrPtr(HdrLine), len, 0);
+                       eq = strchr(ChrPtr(Token), '=');
+                       if (eq != NULL) {
+                               len = eq - ChrPtr(Token);
+                               StrBufAppendBufPlain(Value, eq + 1, StrLength(Token) - len - 1, 0); 
+                               StrBufCutAt(Token, len, NULL);
+                               StrBufTrim(Value);
+                       }
+                       StrBufTrim(Token);
+
+                       if (GetHash(MsgHeaderHandler, SKEY(Token), &vHdr) &&
+                           (vHdr != NULL)) {
+                               Hdr = (headereval*)vHdr;
+                               Hdr->evaluator(Msg, Value, FoundCharset);
+                       }
+                       else lprintf(1, "don't know how to handle content type sub-header[%s]\n", ChrPtr(Token));
                }
+               FreeStrBuf(&Token);
+               FreeStrBuf(&Value);
        }
-        */
 }
 
-void tmplput_MAIL_SUMM_N(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_SUMM_N(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
        StrBufAppendPrintf(Target, "%ld", Msg->msgnum);
@@ -525,17 +660,22 @@ int Conditional_MAIL_MIME_ATTACH(WCTemplateToken *Tokens, void *Context, int Con
 
 
 /*----------------------------------------------------------------------------*/
-void tmplput_QUOTED_MAIL_BODY(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_QUOTED_MAIL_BODY(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        long MsgNum;
-       MsgNum = LBstr(Token->Params[0]->Start, Token->Params[0]->len);
-       read_message(Target, HKEY("view_message_replyquote"), MsgNum, 0, NULL);
+       StrBuf *Buf;
+
+       MsgNum = LBstr(Tokens->Params[0]->Start, Tokens->Params[0]->len);
+       Buf = NewStrBuf();
+       read_message(Buf, HKEY("view_message_replyquote"), MsgNum, 0, NULL);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Buf, 1);
+       FreeStrBuf(&Buf);
 }
 
-void tmplput_MAIL_BODY(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MAIL_BODY(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        message_summary *Msg = (message_summary*) Context;
-       StrBufAppendBuf(Target, Msg->MsgBody->Data, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, Msg->MsgBody->Data, 0);
 }
 
 
@@ -553,10 +693,11 @@ void render_MAIL_text_plain(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *F
        StrBuf *cs = NULL;
        const char *ptr, *pte;
        const char *BufPtr = NULL;
-       StrBuf *Line = NewStrBuf();
-       StrBuf *Line1 = NewStrBuf();
-       StrBuf *Line2 = NewStrBuf();
-       StrBuf *Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
+       StrBuf *Line;
+       StrBuf *Line1;
+       StrBuf *Line2;
+       StrBuf *Target;
+
        int ConvertIt = 1;
        int bn = 0;
        int bq = 0;
@@ -586,7 +727,7 @@ void render_MAIL_text_plain(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *F
                        cs = FoundCharset;
                else if (StrLength(WC->DefaultCharset) > 0)
                        cs = WC->DefaultCharset;
-               if (cs == 0) {
+               if (cs == NULL) {
                        ConvertIt = 0;
                }
                else {
@@ -598,6 +739,10 @@ void render_MAIL_text_plain(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *F
                }
        }
 #endif
+       Line = NewStrBuf();
+       Line1 = NewStrBuf();
+       Line2 = NewStrBuf();
+       Target = NewStrBufPlain(NULL, StrLength(Mime->Data));
 
        while ((n = StrBufSipLine(Line, Mime->Data, &BufPtr), n >= 0) && !done)
        {
@@ -707,57 +852,63 @@ HashList *iterate_get_mime_Attachments(StrBuf *Target, int nArgs, WCTemplateToke
        return Msg->AllAttach;
 }
 
-void tmplput_MIME_Name(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MIME_Name(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_mime_attachment *mime = (wc_mime_attachment*) Context;
-       StrBufAppendBuf(Target, mime->Name, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, mime->Name, 0);
 }
 
-void tmplput_MIME_FileName(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MIME_FileName(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_mime_attachment *mime = (wc_mime_attachment*) Context;
-       StrBufAppendBuf(Target, mime->FileName, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, mime->FileName, 0);
 }
 
-void tmplput_MIME_PartNum(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MIME_PartNum(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_mime_attachment *mime = (wc_mime_attachment*) Context;
-       StrBufAppendBuf(Target, mime->PartNum, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, mime->PartNum, 0);
 }
 
-void tmplput_MIME_MsgNum(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MIME_MsgNum(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_mime_attachment *mime = (wc_mime_attachment*) Context;
        StrBufAppendPrintf(Target, "%ld", mime->msgnum);
 }
 
-void tmplput_MIME_Disposition(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MIME_Disposition(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_mime_attachment *mime = (wc_mime_attachment*) Context;
-       StrBufAppendBuf(Target, mime->Disposition, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, mime->Disposition, 0);
 }
 
-void tmplput_MIME_ContentType(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MIME_ContentType(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_mime_attachment *mime = (wc_mime_attachment*) Context;
-       StrBufAppendBuf(Target, mime->ContentType, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, mime->ContentType, 0);
 }
 
-void tmplput_MIME_Charset(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void examine_charset(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset)
+{
+       Msg->MsgBody->Charset = NewStrBufDup(HdrLine);
+}
+
+void tmplput_MIME_Charset(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_mime_attachment *mime = (wc_mime_attachment*) Context;
-       StrBufAppendBuf(Target, mime->Charset, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, mime->Charset, 0);
 }
 
-void tmplput_MIME_Data(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MIME_Data(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_mime_attachment *mime = (wc_mime_attachment*) Context;
        if (mime->Renderer != NULL)
                mime->Renderer(mime, NULL, NULL);
-       StrBufAppendBuf(Target, mime->Data, 0); /// TODO: check whether we need to load it now?
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, mime->Data, 0);
+ /// TODO: check whether we need to load it now?
 }
 
-void tmplput_MIME_Length(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_MIME_Length(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_mime_attachment *mime = (wc_mime_attachment*) Context;
        StrBufAppendPrintf(Target, "%ld", mime->length);
@@ -768,25 +919,63 @@ HashList *iterate_get_registered_Attachments(StrBuf *Target, int nArgs, WCTempla
        return WC->attachments;
 }
 
-void tmplput_ATT_Length(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_ATT_Length(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_attachment *att = (wc_attachment*) Context;
        StrBufAppendPrintf(Target, "%ld", att->length);
 }
 
-void tmplput_ATT_Contenttype(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_ATT_Contenttype(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_attachment *att = (wc_attachment*) Context;
-       StrBufAppendBuf(Target, att->content_type, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, att->content_type, 0);
 }
 
-void tmplput_ATT_FileName(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
+void tmplput_ATT_FileName(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
 {
        wc_attachment *att = (wc_attachment*) Context;
-       StrBufAppendBuf(Target, att->filename, 0);
+       StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, att->filename, 0);
+}
+
+
+void servcmd_do_search(char *buf, long bufsize)
+{
+       snprintf(buf, bufsize, "MSGS SEARCH|%s", bstr("query"));
 }
 
+void servcmd_headers(char *buf, long bufsize)
+{
+       snprintf(buf, bufsize, "MSGS ALL");
+}
 
+void servcmd_readfwd(char *buf, long bufsize)
+{
+       snprintf(buf, bufsize, "MSGS ALL");
+}
+
+void servcmd_readnew(char *buf, long bufsize)
+{
+       snprintf(buf, bufsize, "MSGS NEW");
+}
+
+void servcmd_readold(char *buf, long bufsize)
+{
+       snprintf(buf, bufsize, "MSGS OLD");
+}
+
+
+readloop_struct rlid[] = {
+       { {HKEY("do_search")}, servcmd_do_search},
+       { {HKEY("headers")},   servcmd_headers},
+       { {HKEY("readfwd")},   servcmd_readfwd},
+       { {HKEY("readnew")},   servcmd_readnew},
+       { {HKEY("readold")},   servcmd_readold}
+};
+
+
+
+
+               
 
 
 
@@ -795,6 +984,22 @@ void
 InitModule_MSGRENDERERS
 (void)
 {
+       RegisterSortFunc(HKEY("date"), 
+                        NULL, 0,
+                        summcmp_date,
+                        summcmp_rdate,
+                        CTX_MAILSUM);
+       RegisterSortFunc(HKEY("subject"), 
+                        NULL, 0,
+                        summcmp_subj,
+                        summcmp_rsubj,
+                        CTX_MAILSUM);
+       RegisterSortFunc(HKEY("sender"),
+                        NULL, 0,
+                        summcmp_sender,
+                        summcmp_rsender,
+                        CTX_MAILSUM);
+
        RegisterNamespace("MAIL:SUMM:DATESTR", 0, 0, tmplput_MAIL_SUMM_DATE_STR, CTX_MAILSUM);
        RegisterNamespace("MAIL:SUMM:DATENO",  0, 0, tmplput_MAIL_SUMM_DATE_NO,  CTX_MAILSUM);
        RegisterNamespace("MAIL:SUMM:N",       0, 0, tmplput_MAIL_SUMM_N,        CTX_MAILSUM);
@@ -880,4 +1085,5 @@ InitModule_MSGRENDERERS
        RegisterMsgHdr(HKEY("Content-type"), examine_content_type, 0);
        RegisterMsgHdr(HKEY("Content-length"), examine_content_lengh, 0);
        RegisterMsgHdr(HKEY("Content-transfer-encoding"), examine_content_encoding, 0);
+       RegisterMsgHdr(HKEY("charset"), examine_charset, 0);
 }