dmc

dynamic mail client
git clone git://git.suckless.org/dmc
Log | Files | Refs | README | LICENSE

commit e1fb9e1b5253424e644fcfb1863ba2059c584957
parent cee1b1129d8a33ab3ae0c318f13a37eb05185389
Author: pancake@localhost.localdomain <unknown>
Date:   Sun,  8 Nov 2009 22:27:54 +0100

* Various fixes in dmc -c
* Filter output of the mail body in imap4
Diffstat:
dmc | 7++++---
imap4.c | 41+++++++++++++++++++++++++++++------------
sock.c | 1+
3 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/dmc b/dmc @@ -23,13 +23,15 @@ acc_daemon () { rm -f "${LOCK}" "${INPUT}" "${OUTPUT}" mkfifo "${LOCK}" "${INPUT}" - ( head -n 1 ${LOCK} ; dmc_cmd "login ${USER} ${PASS}" ) & + # wait 1 connect message and 1 login message + ( head -n 2 ${LOCK} ; dmc_cmd "login ${USER} ${PASS}" ) & (while : ; do cat ${INPUT} 2> /dev/null ; done) | \ dmc-${PROTOCOL} ${HOST} ${PORT} ${SSL} 2> ${OUTPUT} > ${LOCK} rm -f "${LOCK}" "${INPUT}" "${OUTPUT}" } dmc_cmd () { + [ -z "$1" ] && return echo "$@" > ~/.dmc/tmp/${NAME}.input head -n 1 ~/.dmc/tmp/${NAME}.lock > /dev/stderr cat ~/.dmc/tmp/${NAME}.output @@ -219,11 +221,10 @@ case "$1" in ;; "-c"|"--cmd") if [ -z "$2" ]; then - while : ; do + while [ ! "$A" = "exit" ] ; do printf "> " read A dmc_cmd "$A" - [ "$A" = "exit" ] && exit 0 done else shift diff --git a/imap4.c b/imap4.c @@ -14,11 +14,10 @@ static char *cmd = NULL; static char word[4096]; static int ctr = 1; +static int catmode = 0; static char *dir; -/* TODO: make getword() ready() and cleanup() shared between smtp,pop,imap? */ - -/* XXX full of bugs and ugly code */ +/* TODO clean this ugly code */ static char *getword() { char *p = NULL; char *str = word; @@ -58,13 +57,14 @@ static int waitreply() { int lock = 1; int line = 0; int reply = -1; - char result[256]; + char result[1024]; ftruncate (2, 0); lseek (2, 0, SEEK_SET); word[0] = result[0] = '\0'; while(lock || sock_ready ()) { lock = 0; + memset (word, 0, 4096); if (sock_read (word, 4095) <1) break; if (line++ == 0) { @@ -81,25 +81,39 @@ static int waitreply() { else // TODO: Do 'BAD' should be -1 ? if (!memcmp (ptr+1, "BAD", 3)) reply = 0; - else - reply = -1; + else reply = -1; } else reply = -1; - ptr = strchr (word, '\r'); + ptr = strstr (word, "\r\n"); if (!ptr) ptr = strchr (word, '\n'); if (ptr) { + char oldptr = *ptr; *ptr = '\0'; - snprintf (result, 254, "### %s %d \"%s\"\n", cmd, reply, word); - *ptr = '\n'; - if (reply != -1) - strcpy (word, ptr+1); + snprintf (result, 1023, "### %s %d \"%s\"\n", cmd, reply, word); + *ptr = oldptr; + if (catmode) { + if (oldptr=='\r') + strcpy (word, ptr+2); + else strcpy (word, ptr+1); + if (reply != 0) + lock = 1; + } } + } else if (catmode) { + ptr = strstr (word, "\r\n)"); + if (ptr == NULL) + ptr = strstr (word, "\n)"); + if (ptr) { + ptr[0] = '\n'; + ptr[1] = 0; + lock = 0; + } else lock = 1; } write (2, word, strlen (word)); } - write (1, result, strlen(result)); + write (1, result, strlen (result)); return reply; } @@ -114,6 +128,7 @@ RECENT - show the number of recent messages #endif static int doword(char *word) { int ret = 1; + catmode = 0; free (cmd); cmd = strdup (word); if (*word == '\0') { @@ -147,11 +162,13 @@ static int doword(char *word) { waitreply (); } else if (!strcmp (word, "cat")) { + catmode = 1; sock_printf ("%d FETCH %d body[]\n", ctr++, atoi (getword ())); waitreply (); } else if (!strcmp (word, "head")) { + catmode = 1; sock_printf ("%d FETCH %d body[header]\n", ctr++, atoi (getword ())); waitreply (); diff --git a/sock.c b/sock.c @@ -102,6 +102,7 @@ int sock_printf(const char *fmt, ...) { int sock_read (char *buf, int len) { int ret; + memset (buf, 0, len+1); #if HAVE_SSL if (ssl) ret = SSL_read (sfd, buf, len);