]> code.citadel.org Git - citadel.git/blobdiff - webcit/msg_renderers.c
* make readloop long-controlled
[citadel.git] / webcit / msg_renderers.c
index 2f72a575462cd4d9133e9e0a8f594fd21ce94c46..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)
 {
@@ -401,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)),
@@ -464,31 +575,54 @@ 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 *Tokens, void *Context, int ContextType)
@@ -559,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;
@@ -592,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 {
@@ -604,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)
        {
@@ -749,6 +888,11 @@ void tmplput_MIME_ContentType(StrBuf *Target, int nArgs, WCTemplateToken *Tokens
        StrBufAppendTemplate(Target, nArgs, Tokens, Context, ContextType, mime->ContentType, 0);
 }
 
+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;
@@ -794,6 +938,44 @@ void tmplput_ATT_FileName(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, vo
 }
 
 
+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}
+};
+
+
+
+
+               
 
 
 
@@ -802,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);
@@ -887,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);
 }