+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;
+
+#ifdef BIGBAD_IODBG
+ char fn [SIZ];
+ FILE *fd;
+ const char *pch = ChrPtr(IO->SendBuf.Buf);
+ const char *pchh = IO->SendBuf.ReadWritePointer;
+ long nbytes;
+
+ if (pchh == NULL)
+ pchh = pch;
+
+ nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
+ snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
+ ((CitContext*)(IO->CitContext))->ServiceName,
+ IO->SendBuf.fd);
+
+ fd = fopen(fn, "a+");
+#endif
+
+ 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...
+#ifdef BIGBAD_IODBG
+ if (IO->RecvBuf.ReadWritePointer != NULL) {
+ nbytes = StrLength(IO->RecvBuf.Buf) - (IO->RecvBuf.ReadWritePointer - ChrPtr(IO->RecvBuf.Buf));
+ fprintf(fd, "Read; Line unfinished: %ld Bytes still in buffer [", nbytes);
+
+ fwrite(IO->RecvBuf.ReadWritePointer, nbytes, 1, fd);
+
+ fprintf(fd, "]\n");
+ } else {
+ fprintf(fd, "BufferEmpty! \n");
+ }
+ fclose(fd);
+#endif
+ 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;
+#ifdef BIGBAD_IODBG
+ fprintf(fd, "found Terminator; Message Size: %d\n", StrLength(ReadMsg->MsgBuf));
+#endif
+ }
+ else if (!ReadMsg->flushing) {
+
+#ifdef BIGBAD_IODBG
+ fprintf(fd, "Read Line: [%d][%s]\n", StrLength(IO->IOBuf), ChrPtr(IO->IOBuf));
+#endif
+
+ /* 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), "..")))
+ {
+#ifdef BIGBAD_IODBG
+ fprintf(fd, "UnEscaped!\n");
+#endif
+ 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);
+
+#ifdef BIGBAD_IODBG
+ fprintf(fd, "Done with reading; %s.\n, ",
+ (MsgFinished)?"Message Finished": "FAILED");
+ fclose(fd);
+#endif
+ if (MsgFinished)
+ return eReadSuccess;
+ else
+ return eAbort;
+}
+