Add async message reading
[citadel.git] / citadel / msgbase.c
index a2224a8b5edcdf552afc328b344ea5629173ac0e..13e1b125806bc2c888ebac3da01f056a806708ec 100644 (file)
@@ -1427,16 +1427,27 @@ void choose_preferred(char *name, char *filename, char *partnum, char *disp,
 /*
  * Now that we've chosen our preferred part, output it.
  */
-void output_preferred(char *name, char *filename, char *partnum, char *disp,
-               void *content, char *cbtype, char *cbcharset, size_t length,
-               char *encoding, char *cbid, void *cbuserdata)
+void output_preferred(char *name, 
+                     char *filename, 
+                     char *partnum, 
+                     char *disp,
+                     void *content, 
+                     char *cbtype, 
+                     char *cbcharset, 
+                     size_t length,
+                     char *encoding, 
+                     char *cbid, 
+                     void *cbuserdata)
 {
        int i;
        char buf[128];
        int add_newline = 0;
        char *text_content;
        struct ma_info *ma;
-       
+       char *decoded = NULL;
+       size_t bytes_decoded;
+       int rc = 0;
+
        ma = (struct ma_info *)cbuserdata;
 
        /* This is not the MIME part you're looking for... */
@@ -1449,8 +1460,21 @@ void output_preferred(char *name, char *filename, char *partnum, char *disp,
                extract_token(buf, CC->preferred_formats, i, '|', sizeof buf);
                if (!strcasecmp(buf, cbtype)) {
                        /* Yeah!  Go!  W00t!! */
+                       if (ma->dont_decode == 0) 
+                               rc = mime_decode_now (content, 
+                                                     length,
+                                                     encoding,
+                                                     &decoded,
+                                                     &bytes_decoded);
+                       if (rc < 0)
+                               break; /* Give us the chance, maybe theres another one. */
+
+                       if (rc == 0) text_content = (char *)content;
+                       else {
+                               text_content = decoded;
+                               length = bytes_decoded;
+                       }
 
-                       text_content = (char *)content;
                        if (text_content[length-1] != '\n') {
                                ++add_newline;
                        }
@@ -1468,16 +1492,34 @@ void output_preferred(char *name, char *filename, char *partnum, char *disp,
                        }
                        cprintf("X-Citadel-MSG4-Partnum: %s\n", partnum);
                        cprintf("\n");
-                       client_write(content, length);
+                       client_write(text_content, length);
                        if (add_newline) cprintf("\n");
+                       if (decoded != NULL) free(decoded);
                        return;
                }
        }
 
        /* No translations required or possible: output as text/plain */
        cprintf("Content-type: text/plain\n\n");
-       fixed_output(name, filename, partnum, disp, content, cbtype, cbcharset,
+       rc = 0;
+       if (ma->dont_decode == 0)
+               rc = mime_decode_now (content, 
+                                     length,
+                                     encoding,
+                                     &decoded,
+                                     &bytes_decoded);
+       if (rc < 0)
+               return; /* Give us the chance, maybe theres another one. */
+       
+       if (rc == 0) text_content = (char *)content;
+       else {
+               text_content = decoded;
+               length = bytes_decoded;
+       }
+
+       fixed_output(name, filename, partnum, disp, text_content, cbtype, cbcharset,
                        length, encoding, cbid, cbuserdata);
+       if (decoded != NULL) free(decoded);
 }
 
 
@@ -2292,7 +2334,7 @@ START_TEXT:
                                (do_proto ? *list_this_part : NULL),
                                (do_proto ? *list_this_pref : NULL),
                                (do_proto ? *list_this_suff : NULL),
-                               (void *)&ma, 0);
+                               (void *)&ma, 1);
                }
                else if (mode == MT_RFC822) {   /* unparsed RFC822 dump */
                        Dump_RFC822HeadersBody(
@@ -2347,11 +2389,12 @@ START_TEXT:
                        ma.use_fo_hooks = 0;
                        strcpy(ma.chosen_part, "1");
                        ma.chosen_pref = 9999;
+                       ma.dont_decode = CC->msg4_dont_decode;
                        mime_parser(mptr, NULL,
                                *choose_preferred, *fixed_output_pre,
-                               *fixed_output_post, (void *)&ma, 0);
+                               *fixed_output_post, (void *)&ma, 1);
                        mime_parser(mptr, NULL,
-                               *output_preferred, NULL, NULL, (void *)&ma, CC->msg4_dont_decode);
+                               *output_preferred, NULL, NULL, (void *)&ma, 1);
                }
                else {
                        ma.use_fo_hooks = 1;
@@ -2582,7 +2625,7 @@ int CtdlSaveMsgPointersInRoom(char *roomname, long newmsgidlist[], int num_newms
                }
        }
 
-       CtdlLogPrintf(9, "%d unique messages to be merged\n", num_msgs_to_be_merged);
+       CtdlLogPrintf(CTDL_DEBUG, "%d unique messages to be merged\n", num_msgs_to_be_merged);
 
        /*
         * Now merge the new messages
@@ -2884,7 +2927,7 @@ void ReplicationChecks(struct CtdlMessage *msg) {
  */
 long CtdlSubmitMsg(struct CtdlMessage *msg,    /* message to save */
                   struct recptypes *recps,     /* recipients (if mail) */
-                  char *force,                 /* force a particular room? */
+                  const char *force,           /* force a particular room? */
                   int flags                    /* should the message be exported clean? */
 ) {
        char submit_filename[128];
@@ -3398,7 +3441,7 @@ StrBuf *CtdlReadMessageBodyBuf(char *terminator,  /* token signalling EOT */
        /* read in the lines of message text one by one */
        do {
                if (sock != NULL) {
-                       if ((CtdlSockGetLine(sock, LineBuf) < 0) ||
+                       if ((CtdlSockGetLine(sock, LineBuf, 5) < 0) ||
                            (*sock == -1))
                                finished = 1;
                }
@@ -3436,6 +3479,123 @@ StrBuf *CtdlReadMessageBodyBuf(char *terminator,        /* token signalling EOT */
        return Message;
 }
 
+void DeleteAsyncMsg(ReadAsyncMsg **Msg)
+{
+       if (*Msg == NULL)
+               return;
+       FreeStrBuf(&(*Msg)->MsgBuf);
+
+       free(*Msg);
+       *Msg = NULL;
+}
+
+ReadAsyncMsg *NewAsyncMsg(const char *terminator,      /* token signalling EOT */
+                         long tlen,
+                         size_t maxlen,                /* maximum message length */
+                         size_t expectlen,             /* if we expect a message, how long should it be? */
+                         char *exist,                  /* if non-null, append to it;
+                                                          exist is ALWAYS freed  */
+                         long eLen,                    /* length of exist */
+                         int crlf                      /* CRLF newlines instead of LF */
+       )
+{
+       ReadAsyncMsg *NewMsg;
+
+       NewMsg = (ReadAsyncMsg *)malloc(sizeof(ReadAsyncMsg));
+       memset(NewMsg, 0, sizeof(ReadAsyncMsg));
+
+       if (exist == NULL) {
+               long len;
+
+               if (expectlen == 0) {
+                       len = 4 * SIZ;
+               }
+               else {
+                       len = expectlen + 10;
+               }
+               NewMsg->MsgBuf = NewStrBufPlain(NULL, len);
+       }
+       else {
+               NewMsg->MsgBuf = NewStrBufPlain(exist, eLen);
+               free(exist);
+       }
+       /* Do we need to change leading ".." to "." for SMTP escaping? */
+       if ((tlen == 1) && (*terminator == '.')) {
+               NewMsg->dodot = 1;
+       }
+
+       NewMsg->terminator = terminator;
+       NewMsg->tlen = tlen;
+
+       NewMsg->maxlen = maxlen;
+
+       NewMsg->crlf = crlf;
+
+       return NewMsg;
+}
+
+/*
+ * Back end function used by CtdlMakeMessage() and similar functions
+ */
+eReadState CtdlReadMessageBodyAsync(AsyncIO *IO)
+{
+       ReadAsyncMsg *ReadMsg;
+       int MsgFinished = 0;
+       eReadState Finished = eMustReadMore;
+
+       ReadMsg = IO->ReadMsg;
+
+       /* read in the lines of message text one by one */
+       do {
+               Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
+               
+               switch (Finished) {
+               case eMustReadMore: /// read new from socket... 
+                   return Finished;
+                   break;
+               case eBufferNotEmpty: /* shouldn't happen... */
+               case eReadSuccess: /// done for now...
+                   break;
+               case eReadFail: /// WHUT?
+                   ///todo: shut down! 
+                       break;
+               }
+           
+
+               if ((StrLength(IO->IOBuf) == ReadMsg->tlen) && 
+                   (!strcmp(ChrPtr(IO->IOBuf), ReadMsg->terminator))) {
+                       MsgFinished = 1;
+               }
+               else if (!ReadMsg->flushing) {
+                       /* Unescape SMTP-style input of two dots at the beginning of the line */
+                       if ((ReadMsg->dodot) &&
+                           (StrLength(IO->IOBuf) == 2) &&  /* TODO: do we just unescape lines with two dots or any line? */
+                           (!strcmp(ChrPtr(IO->IOBuf), "..")))
+                       {
+                               StrBufCutLeft(IO->IOBuf, 1);
+                       }
+
+                       if (ReadMsg->crlf) {
+                               StrBufAppendBufPlain(IO->IOBuf, HKEY("\r\n"), 0);
+                       }
+                       else {
+                               StrBufAppendBufPlain(IO->IOBuf, HKEY("\n"), 0);
+                       }
+
+                       StrBufAppendBuf(ReadMsg->MsgBuf, IO->IOBuf, 0);
+               }
+
+               /* if we've hit the max msg length, flush the rest */
+               if (StrLength(ReadMsg->MsgBuf) >= ReadMsg->maxlen) ReadMsg->flushing = 1;
+
+       } while (!MsgFinished);
+
+       if (MsgFinished)
+               return eReadSuccess;
+       else 
+               return eAbort;
+}
+
 
 /*
  * Back end function used by CtdlMakeMessage() and similar functions
@@ -3489,6 +3649,8 @@ struct CtdlMessage *CtdlMakeMessage(
        char dest_node[256];
        char buf[1024];
        struct CtdlMessage *msg;
+       StrBuf *FakeAuthor;
+       StrBuf *FakeEncAuthor = NULL;
 
        msg = malloc(sizeof(struct CtdlMessage));
        memset(msg, 0, sizeof(struct CtdlMessage));
@@ -3518,11 +3680,14 @@ struct CtdlMessage *CtdlMakeMessage(
        msg->cm_fields['T'] = strdup(buf);
 
        if ((fake_name != NULL) && (fake_name[0])) {            /* author */
-               msg->cm_fields['A'] = strdup(fake_name);
+               FakeAuthor = NewStrBufPlain (fake_name, -1);
        }
        else {
-               msg->cm_fields['A'] = strdup(author->fullname);
+               FakeAuthor = NewStrBufPlain (author->fullname, -1);
        }
+       StrBufRFC2047encode(&FakeEncAuthor, FakeAuthor);
+       msg->cm_fields['A'] = SmashStrBuf(&FakeEncAuthor);
+       FreeStrBuf(&FakeAuthor);
 
        if (CC->room.QRflags & QR_MAILBOX) {            /* room */
                msg->cm_fields['O'] = strdup(&CC->room.QRname[11]);