dmc

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

commit b984c091502272e99581e830d365c3e982d3d451
parent e4be3727d9a3ba27dc2d67b7dcfdc07ae372e00d
Author: pancake@localhost.localdomain <unknown>
Date:   Sat, 10 Oct 2009 22:38:46 +0200

* Initial working shell for pop3
  - Simplified pipeline
Diffstat:
src/dmc | 5++++-
src/pop3.c | 52++++++++++++++++++++++++++++++++++++----------------
2 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/src/dmc b/src/dmc @@ -4,7 +4,10 @@ # FIFO=fifo-session -./dmc-pop3 $FIFO | nc localhost 110 | ./dmc-pop3 > $FIFO +./dmc-pop3 $FIFO | nc radare.org 110 > $FIFO +exit 0 ./dmc-pop3 | nc localhost 110 | ./dmc-pop3-post ./dmc-imap4 | openssl s_client -host radare.org -port 993 | ./dmc-imap4 + +mkdir -p ~/.dmc/mail diff --git a/src/pop3.c b/src/pop3.c @@ -8,6 +8,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> +#include <poll.h> static char word[1024]; @@ -21,23 +22,40 @@ static char *getword() { return word; } -static int waitreply() { - int ret = -1; +static int ready() { + struct pollfd fds[1]; + fds[0].fd = ff; + fds[0].events = POLLIN|POLLPRI; + fds[0].revents = POLLNVAL|POLLHUP|POLLERR; + return poll((struct pollfd *)&fds, 1, 10); +} + +static int waitreply(int lock) { + int ret, reply = -1; fflush(stdout); - fgets(word, sizeof(word), stdin); - if (!memcmp(word, "+OK", 3)) - ret = 1; - else - if (!memcmp(word, "-ERR", 4)) - ret = 0; - return ret; // 1 if true, 0 if false + while(lock || ready()) { + lock = 0; + ret = read(ff, word, 512); + if (ret<1) + break; + if (reply==-1) { + if (!memcmp(word, "+OK", 3)) + reply = 1; + else + if (!memcmp(word, "-ERR", 4)) + reply = 0; + } + fprintf(stderr, "\n\n-->(%d)(%s)<--\n\n", ret, word); + } + fprintf(stderr, "REPLY BOOL IS %d\n", reply); + return reply; } static int doword(char *word) { int ret = 1; if (*word == '\0' || !strcmp(word, "exit")) { printf("QUIT\n"); - waitreply(); + waitreply(1); ret = 0; } else if (!strcmp(word, "help") || !strcmp(word, "?")) { @@ -45,24 +63,25 @@ static int doword(char *word) { } else if (!strcmp(word, "ls")) { printf("LIST\n"); - waitreply(); + waitreply(1); } else if (!strcmp(word, "cat")) { printf("RETR %d\n", atoi(getword())); - waitreply(); + waitreply(1); } else if (!strcmp(word, "head")) { printf("TOP %d\n", atoi(getword())); - waitreply(); + waitreply(1); } else if (!strcmp(word, "rm")) { printf("DELE %d\n", atoi(getword())); - waitreply(); + waitreply(1); } else if (!strcmp(word, "login")) { printf("USER %s\n", getword()); + waitreply(1); printf("PASS %s\n", getword()); - waitreply(); + waitreply(1); } else printf("NOOP\n"); return ret; } @@ -85,7 +104,8 @@ int main(int argc, char **argv) { signal(SIGINT, cleanup); fifo = argv[1]; mkfifo(fifo, 0600); - ff = open(fifo, O_NONBLOCK|O_RDWR); + ff = open(fifo, O_RDONLY); //O_NONBLOCK|O_RDWR); + waitreply(1); while(doword(getword())); cleanup(0); } else parseoutput();