put standard handlers into their own enum-lookupable list.
[citadel.git] / webcit / messages.c
index 405656eab269ec22794ea6c7c5b5d080fc9726c1..732ef03b8093686eecdac97baf7a9d3a21b60a9e 100644 (file)
@@ -17,7 +17,6 @@
 #include "dav.h"
 #include "calendar.h"
 
-HashList *MsgHeaderHandler = NULL;
 HashList *MimeRenderHandler = NULL;
 HashList *ReadLoopHandler = NULL;
 int dbg_analyze_msg = 0;
@@ -45,12 +44,11 @@ int load_message(message_summary *Msg,
 {
        StrBuf *Buf;
        StrBuf *HdrToken;
-       headereval *Hdr;
-       void *vHdr;
        char buf[SIZ];
        int Done = 0;
        int state=0;
-       
+       int rc;
+
        Buf = NewStrBuf();
        if (Msg->PartNum != NULL) {
                serv_printf("MSG4 %ld|%s", Msg->msgnum, ChrPtr(Msg->PartNum));
@@ -97,14 +95,11 @@ int load_message(message_summary *Msg,
                        StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
                        
                        /* look up one of the examine_* functions to parse the content */
-                       if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
-                           (vHdr != NULL)) {
-                               Hdr = (headereval*)vHdr;
-                               Hdr->evaluator(Msg, Buf, FoundCharset);
-                               if (Hdr->Type == 1) {
-                                       state++;
-                               }
-                       }/* TODO: 
+                       rc =  EvaluateMsgHdr(SKEY(HdrToken), Msg, Buf, FoundCharset);
+                       if (rc == 1) {
+                               state++;
+                       }
+                       /* TODO: 
                        else LogError(Target, 
                                      __FUNCTION__,  
                                      "don't know how to handle message header[%s]\n", 
@@ -125,11 +120,8 @@ int load_message(message_summary *Msg,
                                if (StrLength(HdrToken) > 0) {
                                        StrBufCutLeft(Buf, StrLength(HdrToken) + 1);
                                        /* the examine*'s know how to do with mime headers too... */
-                                       if (GetHash(MsgHeaderHandler, SKEY(HdrToken), &vHdr) &&
-                                           (vHdr != NULL)) {
-                                               Hdr = (headereval*)vHdr;
-                                               Hdr->evaluator(Msg, Buf, FoundCharset);
-                                       }
+                                       EvaluateMsgHdr(SKEY(HdrToken), Msg, Buf, FoundCharset);
+                                       
                                        break;
                                }
                        }
@@ -171,7 +163,7 @@ int load_message(message_summary *Msg,
  * printable_view      Nonzero to display a printable view
  * section             Optional for encapsulated message/rfc822 submessage
  */
-int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime) 
+int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime, WCTemplputParams *TP
 {
        StrBuf *Buf;
        StrBuf *FoundCharset;
@@ -208,7 +200,7 @@ int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, co
        StrBufTrim(Buf);
        StrBufLowerCase(Buf);
 
-       StackContext(NULL, &SuperTP, Msg, CTX_MAILSUM, 0, NULL);
+       StackContext(TP, &SuperTP, Msg, CTX_MAILSUM, 0, NULL);
        {
                /* Locate a renderer capable of converting this MIME part into HTML */
                if (GetHash(MimeRenderHandler, SKEY(Buf), &vHdr) &&
@@ -357,9 +349,9 @@ void handle_one_message(void)
        case ePOST:
                Tmpl = sbstr("template");
                if (StrLength(Tmpl) > 0) 
-                       read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
+                       read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime, NULL);
                else 
-                       read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
+                       read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
                http_transmit_thing(ChrPtr(Mime), 0);
                break;
        case eDELETE:
@@ -416,9 +408,9 @@ void embed_message(void) {
        case ePOST:
                Tmpl = sbstr("template");
                if (StrLength(Tmpl) > 0) 
-                       read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
+                       read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime, NULL);
                else 
-                       read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
+                       read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
                http_transmit_thing(ChrPtr(Mime), 0);
                break;
        case eDELETE:
@@ -455,7 +447,7 @@ void print_message(void) {
 
        begin_burst();
 
-       read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime);
+       read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime, NULL);
 
        wDumpContent(0);
 }
@@ -787,13 +779,13 @@ void readloop(long oper, eCustomRoomRenderer ForceRenderer)
                
                Foo = NewStrBuf ();
                StrBufPrintf(Foo, "%ld", Stat.nummsgs);
-               PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo)); // keep Foo!
+               PutBstr(HKEY("__READLOOP:TOTALMSGS"), NewStrBufDup(Foo)); /* keep Foo! */
 
                StrBufPrintf(Foo, "%ld", Stat.numNewmsgs);
-               PutBstr(HKEY("__READLOOP:NEWMSGS"), NewStrBufDup(Foo)); // keep Foo!
+               PutBstr(HKEY("__READLOOP:NEWMSGS"), NewStrBufDup(Foo)); /* keep Foo! */
 
                StrBufPrintf(Foo, "%ld", Stat.startmsg);
-               PutBstr(HKEY("__READLOOP:STARTMSG"), Foo); // store Foo elsewhere, descope it here.
+               PutBstr(HKEY("__READLOOP:STARTMSG"), Foo); /* store Foo elsewhere, descope it here. */
        }
 
        /*
@@ -1310,33 +1302,6 @@ void remove_attachment(void) {
 }
 
 
-long FourHash(const char *key, long length) 
-{
-        int i;
-        long ret = 0;
-        const unsigned char *ptr = (const unsigned char*)key;
-
-        for (i = 0; i < 4; i++, ptr ++) 
-                ret = (ret << 8) | 
-                        ( ((*ptr >= 'a') &&
-                           (*ptr <= 'z'))? 
-                          *ptr - 'a' + 'A': 
-                          *ptr);
-
-        return ret;
-}
-
-long l_subj;
-long l_wefw;
-long l_msgn;
-long l_from;
-long l_rcpt;
-long l_cccc;
-long l_replyto;
-long l_node;
-long l_rfca;
-long l_nvto;
-
 const char *ReplyToModeStrings [3] = {
        "reply",
        "replyall",
@@ -1366,6 +1331,10 @@ void display_enter(void)
        int i = 0;
        long replying_to;
 
+       int prefer_md;
+
+       get_pref_yesno("markdown", &prefer_md, 0);
+
        if (havebstr("force_room")) {
                gotoroom(sbstr("force_room"));
        }
@@ -1465,95 +1434,112 @@ void display_enter(void)
                               ((len != 3)  ||
                                strcmp(ChrPtr(Line), "000")))
                        {
-                               long which = 0;
+                               eMessageField which;
                                if ((StrLength(Line) > 4) && 
-                                   (ChrPtr(Line)[4] == '='))
-                                       which = FourHash(ChrPtr(Line), 4);
-
-                               if (which == l_subj)
-                               {
-                                       StrBuf *subj = NewStrBuf();
-                                       StrBuf *FlatSubject;
-
-                                       if (ReplyMode == eForward) {
-                                               if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
-                                                       StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
+                                   (ChrPtr(Line)[4] == '=') &&
+                                   GetFieldFromMnemonic(&which, ChrPtr(Line)))
+                                       switch (which) {
+                                       case eMsgSubject: {
+                                               StrBuf *subj = NewStrBuf();
+                                               StrBuf *FlatSubject;
+
+                                               if (ReplyMode == eForward) {
+                                                       if (strncasecmp(ChrPtr(Line) + 5, "Fw:", 3)) {
+                                                               StrBufAppendBufPlain(subj, HKEY("Fw: "), 0);
+                                                       }
                                                }
-                                       }
-                                       else {
-                                               if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
-                                                       StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
+                                               else {
+                                                       if (strncasecmp(ChrPtr(Line) + 5, "Re:", 3)) {
+                                                               StrBufAppendBufPlain(subj, HKEY("Re: "), 0);
+                                                       }
                                                }
-                                       }
-                                       StrBufAppendBufPlain(subj, 
-                                                            ChrPtr(Line) + 5, 
-                                                            StrLength(Line) - 5, 0);
-                                       FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
-                                       StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
+                                               StrBufAppendBufPlain(subj, 
+                                                                    ChrPtr(Line) + 5, 
+                                                                    StrLength(Line) - 5, 0);
+                                               FlatSubject = NewStrBufPlain(NULL, StrLength(subj));
+                                               StrBuf_RFC822_to_Utf8(FlatSubject, subj, NULL, NULL);
 
-                                       PutBstr(HKEY("subject"), FlatSubject);
-                               }
+                                               PutBstr(HKEY("subject"), FlatSubject);
+                                       }
+                                               break;
 
-                               else if (which == l_wefw)
-                               {
-                                       int rrtok;
-                                       int rrlen;
+                                       case eWeferences:
+                                       {
+                                               int rrtok;
+                                               int rrlen;
 
-                                       wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
+                                               wefw = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
                                        
-                                       /* Trim down excessively long lists of thread references.  We eliminate the
-                                        * second one in the list so that the thread root remains intact.
-                                        */
-                                       rrtok = num_tokens(ChrPtr(wefw), '|');
-                                       rrlen = StrLength(wefw);
-                                       if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
-                                               StrBufRemove_token(wefw, 1, '|');
+                                               /* Trim down excessively long lists of thread references.  We eliminate the
+                                                * second one in the list so that the thread root remains intact.
+                                                */
+                                               rrtok = num_tokens(ChrPtr(wefw), '|');
+                                               rrlen = StrLength(wefw);
+                                               if ( ((rrtok >= 3) && (rrlen > 900)) || (rrtok > 10) ) {
+                                                       StrBufRemove_token(wefw, 1, '|');
+                                               }
+                                               break;
                                        }
-                               }
-
-                               else if (which == l_msgn) {
-                                       msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
-                               }
 
-                               else if (which == l_from) {
-                                       StrBuf *FlatFrom;
-                                       from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
-                                       FlatFrom = NewStrBufPlain(NULL, StrLength(from));
-                                       StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
-                                       FreeStrBuf(&from);
-                                       from = FlatFrom;
-                                       for (i=0; i<StrLength(from); ++i) {
-                                               if (ChrPtr(from)[i] == ',')
-                                                       StrBufPeek(from, NULL, i, ' ');
+                                       case emessageId:
+                                               msgn = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
+                                               break;
+
+                                       case eAuthor: {
+                                               StrBuf *FlatFrom;
+                                               from = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
+                                               FlatFrom = NewStrBufPlain(NULL, StrLength(from));
+                                               StrBuf_RFC822_to_Utf8(FlatFrom, from, NULL, NULL);
+                                               FreeStrBuf(&from);
+                                               from = FlatFrom;
+                                               for (i=0; i<StrLength(from); ++i) {
+                                                       if (ChrPtr(from)[i] == ',')
+                                                               StrBufPeek(from, NULL, i, ' ');
+                                               }
+                                               break;
                                        }
-                               }
                                
-                               else if (which == l_rcpt) {
-                                       rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
-                               }
+                                       case eRecipient:
+                                               rcpt = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
+                                               break;
+                       
                                
-                               else if (which == l_cccc) {
-                                       cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
-                               }
+                                       case eCarbonCopY:
+                                               cccc = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
+                                               break;
+
                                
-                               else if (which == l_node) {
-                                       node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
-                               }
-                               else if (which == l_replyto) {
-                                       replyto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
-                               }
-                               else if (which == l_rfca) {
-                                       StrBuf *FlatRFCA;
-                                       rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
-                                       FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
-                                       StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
-                                       FreeStrBuf(&rfca);
-                                       rfca = FlatRFCA;
-                               }
-                               else if (which == l_nvto) {
-                                       nvto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
-                                       putbstr("nvto", nvto);
-                               }
+                                       case eNodeName:
+                                               node = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
+                                               break;
+                                       case eReplyTo:
+                                               replyto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
+                                               break;
+                                       case erFc822Addr: {
+                                               StrBuf *FlatRFCA;
+                                               rfca = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
+                                               FlatRFCA = NewStrBufPlain(NULL, StrLength(rfca));
+                                               StrBuf_RFC822_to_Utf8(FlatRFCA, rfca, NULL, NULL);
+                                               FreeStrBuf(&rfca);
+                                               rfca = FlatRFCA;
+                                               break;
+                                       }
+                                       case eenVelopeTo:
+                                               nvto = NewStrBufPlain(ChrPtr(Line) + 5, StrLength(Line) - 5);
+                                               putbstr("nvto", nvto);
+                                               break;
+                                       case eXclusivID:
+                                       case eHumanNode:
+                                       case eJournal:
+                                       case eListID:
+                                       case eMesageText:
+                                       case eOriginalRoom:
+                                       case eMessagePath:
+                                       case eSpecialField:
+                                       case eTimestamp:
+                                               break;
+
+                                       }
                        }
 
 
@@ -1695,7 +1681,7 @@ void display_enter(void)
 
        begin_burst();
        output_headers(1, 0, 0, 0, 1, 0);
-       if (WCC->CurRoom.defview == VIEW_WIKIMD) 
+       if ((WCC->CurRoom.defview == VIEW_WIKIMD) || prefer_md)
                DoTemplate(HKEY("edit_markdown_epic"), NULL, &NoCtx);
        else
                DoTemplate(HKEY("edit_message"), NULL, &NoCtx);
@@ -2042,7 +2028,7 @@ InitModule_MSG
                           PRF_YESNO, 
                           NULL);
        RegisterPreference("signature", _("Use this signature:"), PRF_QP_STRING, NULL);
-       RegisterPreference("default_header_charset", 
+       RegisterPreference("default_header_charset",
                           _("Default character set for email headers:"), 
                           PRF_STRING, 
                           NULL);
@@ -2056,6 +2042,8 @@ InitModule_MSG
                           PRF_STRING, 
                           NULL);
        RegisterPreference("mailbox",_("Mailbox view mode"), PRF_STRING, NULL);
+       RegisterPreference("markdown",_("Prefer markdown editing"), PRF_YESNO, NULL);
+
 
        WebcitAddUrlHandler(HKEY("readnew"), "", 0, h_readnew, ANONYMOUS|NEED_URL);
        WebcitAddUrlHandler(HKEY("readold"), "", 0, h_readold, ANONYMOUS|NEED_URL);
@@ -2083,19 +2071,6 @@ InitModule_MSG
 
        /* json */
        WebcitAddUrlHandler(HKEY("roommsgs"), "", 0, jsonMessageList,0);
-
-       l_subj = FourHash("subj", 4);
-       l_wefw = FourHash("wefw", 4);
-       l_msgn = FourHash("msgn", 4);
-       l_from = FourHash("from", 4);
-       l_rcpt = FourHash("rcpt", 4);
-       l_cccc = FourHash("cccc", 4);
-       l_replyto = FourHash("rep2", 4);
-       l_node = FourHash("node", 4);
-       l_rfca = FourHash("rfca", 4);
-       l_nvto = FourHash("nvto", 4);
-
-       return ;
 }
 
 void