diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-08-29 10:04:38 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-08-29 10:04:38 +0300 |
commit | 99b2c31936f74efccd19e44147992dc0674f87e4 (patch) | |
tree | 493150dd274441beb35300f3d458257789be4daa | |
parent | afbdbd89e1edd58916bf504ffc41f0884a6992c9 (diff) | |
download | mailutils-99b2c31936f74efccd19e44147992dc0674f87e4.tar.gz mailutils-99b2c31936f74efccd19e44147992dc0674f87e4.tar.bz2 |
pop3d: cleanup
* pop3d/pop3d.h: Include filter.h.
(istream, ostream): New externs.
* pop3d/extra.c (pop3d_setio): Set a crlf filter on the
output stream. This allows to forget about \r in the rest of
the code.
* pop3d/retr.c (pop3d_retr): Use mu_stream_copy instead of
copying streams manually.
* pop3d/top.c (pop3d_top): Rewrite using streamrefs and
mu_stream_copy.
* all sources: Use \n instead of \r\n in output strings.
-rw-r--r-- | pop3d/capa.c | 24 | ||||
-rw-r--r-- | pop3d/dele.c | 2 | ||||
-rw-r--r-- | pop3d/extra.c | 20 | ||||
-rw-r--r-- | pop3d/list.c | 8 | ||||
-rw-r--r-- | pop3d/logindelay.c | 2 | ||||
-rw-r--r-- | pop3d/noop.c | 2 | ||||
-rw-r--r-- | pop3d/pop3d.c | 4 | ||||
-rw-r--r-- | pop3d/pop3d.h | 2 | ||||
-rw-r--r-- | pop3d/quit.c | 2 | ||||
-rw-r--r-- | pop3d/retr.c | 39 | ||||
-rw-r--r-- | pop3d/rset.c | 2 | ||||
-rw-r--r-- | pop3d/stat.c | 2 | ||||
-rw-r--r-- | pop3d/stls.c | 2 | ||||
-rw-r--r-- | pop3d/top.c | 84 | ||||
-rw-r--r-- | pop3d/uidl.c | 8 | ||||
-rw-r--r-- | pop3d/user.c | 4 |
16 files changed, 81 insertions, 126 deletions
diff --git a/pop3d/capa.c b/pop3d/capa.c index 688b09d6c..ca58512e6 100644 --- a/pop3d/capa.c +++ b/pop3d/capa.c @@ -35,30 +35,30 @@ pop3d_capa (char *arg) if (state != initial_state && state != TRANSACTION) return ERR_WRONG_STATE; - pop3d_outf ("+OK Capability list follows\r\n"); - pop3d_outf ("TOP\r\n"); - pop3d_outf ("USER\r\n"); - pop3d_outf ("UIDL\r\n"); - pop3d_outf ("RESP-CODES\r\n"); - pop3d_outf ("PIPELINING\r\n"); + pop3d_outf ("+OK Capability list follows\n"); + pop3d_outf ("TOP\n"); + pop3d_outf ("USER\n"); + pop3d_outf ("UIDL\n"); + pop3d_outf ("RESP-CODES\n"); + pop3d_outf ("PIPELINING\n"); #ifdef WITH_TLS if (tls_available && tls_done == 0) - pop3d_outf ("STLS\r\n"); + pop3d_outf ("STLS\n"); #endif /* WITH_TLS */ login_delay_capa (); /* This can be implemented by setting an header field on the message. */ if (expire == EXPIRE_NEVER) - pop3d_outf ("EXPIRE NEVER\r\n"); + pop3d_outf ("EXPIRE NEVER\n"); else - pop3d_outf ("EXPIRE %s\r\n", mu_umaxtostr (0, expire)); + pop3d_outf ("EXPIRE %s\n", mu_umaxtostr (0, expire)); if (state == INITIAL) - pop3d_outf ("XTLSREQUIRED\r\n"); + pop3d_outf ("XTLSREQUIRED\n"); if (state == TRANSACTION) /* let's not advertise to just anyone */ - pop3d_outf ("IMPLEMENTATION %s\r\n", PACKAGE_STRING); - pop3d_outf (".\r\n"); + pop3d_outf ("IMPLEMENTATION %s\n", PACKAGE_STRING); + pop3d_outf (".\n"); return OK; } diff --git a/pop3d/dele.c b/pop3d/dele.c index f838f59b9..08e3ac193 100644 --- a/pop3d/dele.c +++ b/pop3d/dele.c @@ -39,6 +39,6 @@ pop3d_dele (char *arg) mu_message_get_attribute (msg, &attr); pop3d_mark_deleted (attr); - pop3d_outf ("+OK Message %s marked\r\n", mu_umaxtostr (0, num)); + pop3d_outf ("+OK Message %s marked\n", mu_umaxtostr (0, num)); return OK; } diff --git a/pop3d/extra.c b/pop3d/extra.c index 72f0e67b4..ca5089e25 100644 --- a/pop3d/extra.c +++ b/pop3d/extra.c @@ -20,7 +20,7 @@ #include "pop3d.h" #include "mailutils/libargp.h" -static mu_stream_t istream, ostream; +mu_stream_t istream, ostream; void pop3d_parse_command (char *cmd, char **pcmd, char **parg) @@ -61,7 +61,7 @@ pop3d_abquit (int reason) { case ERR_NO_MEM: code = EX_SOFTWARE; - pop3d_outf ("-ERR %s\r\n", pop3d_error_string (reason)); + pop3d_outf ("-ERR %s\n", pop3d_error_string (reason)); mu_diag_output (MU_DIAG_ERROR, _("not enough memory")); break; @@ -77,7 +77,7 @@ pop3d_abquit (int reason) case ERR_TIMEOUT: code = EX_TEMPFAIL; - pop3d_outf ("-ERR %s\r\n", pop3d_error_string (reason)); + pop3d_outf ("-ERR %s\n", pop3d_error_string (reason)); if (state == TRANSACTION) mu_diag_output (MU_DIAG_INFO, _("session timed out for user: %s"), username); @@ -111,12 +111,12 @@ pop3d_abquit (int reason) _("mailbox was updated by other party: %s"), username); pop3d_outf - ("-ERR [OUT-SYNC] Mailbox updated by other party or corrupt\r\n"); + ("-ERR [OUT-SYNC] Mailbox updated by other party or corrupt\n"); break; default: code = EX_SOFTWARE; - pop3d_outf ("-ERR Quitting: %s\r\n", pop3d_error_string (reason)); + pop3d_outf ("-ERR Quitting: %s\n", pop3d_error_string (reason)); mu_diag_output (MU_DIAG_ERROR, _("quitting (numeric reason %d)"), reason); break; @@ -129,6 +129,8 @@ pop3d_abquit (int reason) void pop3d_setio (FILE *in, FILE *out) { + mu_stream_t str; + if (!in) pop3d_abquit (ERR_NO_IFILE); if (!out) @@ -136,9 +138,13 @@ pop3d_setio (FILE *in, FILE *out) if (mu_stdio_stream_create (&istream, fileno (in), MU_STREAM_NO_CLOSE)) pop3d_abquit (ERR_NO_IFILE); - if (mu_stdio_stream_create (&ostream, fileno (out), MU_STREAM_NO_CLOSE)) - pop3d_abquit (ERR_NO_OFILE); mu_stream_set_buffer (istream, mu_buffer_line, 1024); + + if (mu_stdio_stream_create (&str, fileno (out), MU_STREAM_NO_CLOSE)) + pop3d_abquit (ERR_NO_OFILE); + if (mu_filter_create (&ostream, str, "rfc822", MU_FILTER_ENCODE, + MU_STREAM_WRITE|MU_STREAM_NO_CLOSE)) + pop3d_abquit (ERR_NO_IFILE); mu_stream_set_buffer (ostream, mu_buffer_line, 1024); } diff --git a/pop3d/list.c b/pop3d/list.c index b1928750b..a212e4146 100644 --- a/pop3d/list.c +++ b/pop3d/list.c @@ -37,7 +37,7 @@ pop3d_list (char *arg) if (strlen (arg) == 0) { size_t total = 0; - pop3d_outf ("+OK\r\n"); + pop3d_outf ("+OK\n"); mu_mailbox_messages_count (mbox, &total); for (mesgno = 1; mesgno <= total; mesgno++) { @@ -47,12 +47,12 @@ pop3d_list (char *arg) { mu_message_size (msg, &size); mu_message_lines (msg, &lines); - pop3d_outf ("%s %s\r\n", + pop3d_outf ("%s %s\n", mu_umaxtostr (0, mesgno), mu_umaxtostr (1, size + lines)); } } - pop3d_outf (".\r\n"); + pop3d_outf (".\n"); } else { @@ -64,7 +64,7 @@ pop3d_list (char *arg) return ERR_MESG_DELE; mu_message_size (msg, &size); mu_message_lines (msg, &lines); - pop3d_outf ("+OK %s %s\r\n", + pop3d_outf ("+OK %s %s\n", mu_umaxtostr (0, mesgno), mu_umaxtostr (1, size + lines)); } diff --git a/pop3d/logindelay.c b/pop3d/logindelay.c index 9c8020d9c..5cab22c50 100644 --- a/pop3d/logindelay.c +++ b/pop3d/logindelay.c @@ -126,7 +126,7 @@ login_delay_capa () if (login_delay && open_stat_db (&db, MU_STREAM_RDWR) == 0) { - pop3d_outf ("LOGIN-DELAY %s\r\n", mu_umaxtostr (0, login_delay)); + pop3d_outf ("LOGIN-DELAY %s\n", mu_umaxtostr (0, login_delay)); mu_dbm_close (db); } } diff --git a/pop3d/noop.c b/pop3d/noop.c index a3d8e6450..55ecf18db 100644 --- a/pop3d/noop.c +++ b/pop3d/noop.c @@ -26,6 +26,6 @@ pop3d_noop (char *arg) return ERR_BAD_ARGS; if (state != TRANSACTION) return ERR_WRONG_STATE; - pop3d_outf ("+OK\r\n"); + pop3d_outf ("+OK\n"); return OK; } diff --git a/pop3d/pop3d.c b/pop3d/pop3d.c index 62c871b1f..5a09efc68 100644 --- a/pop3d/pop3d.c +++ b/pop3d/pop3d.c @@ -248,7 +248,7 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile) } /* Lets boogie. */ - pop3d_outf ("+OK POP3 Ready %s\r\n", md5shared); + pop3d_outf ("+OK POP3 Ready %s\n", md5shared); while (state != UPDATE && state != ABORT) { @@ -291,7 +291,7 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile) status = ERR_BAD_CMD; if (status != OK) - pop3d_outf ("-ERR %s\r\n", pop3d_error_string (status)); + pop3d_outf ("-ERR %s\n", pop3d_error_string (status)); } pop3d_bye (); diff --git a/pop3d/pop3d.h b/pop3d/pop3d.h index ad580b7f2..b7e593b9e 100644 --- a/pop3d/pop3d.h +++ b/pop3d/pop3d.h @@ -125,6 +125,7 @@ extern int expire_on_exit; #include <mailutils/md5.h> #include <mailutils/acl.h> #include <mailutils/server.h> +#include <mailutils/filter.h> /* For Berkley DB2 APOP password file */ #ifdef HAVE_DB_H @@ -188,6 +189,7 @@ struct pop3d_command pop3d_command_handler_t handler; }; +extern mu_stream_t istream, ostream; extern mu_pop_server_t pop3srv; extern mu_mailbox_t mbox; extern int state; diff --git a/pop3d/quit.c b/pop3d/quit.c index 94857cb83..0b0ca15e7 100644 --- a/pop3d/quit.c +++ b/pop3d/quit.c @@ -53,7 +53,7 @@ pop3d_quit (char *arg) free (md5shared); if (err == OK) - pop3d_outf ("+OK\r\n"); + pop3d_outf ("+OK\n"); return err; } diff --git a/pop3d/retr.c b/pop3d/retr.c index c5e44f501..53b25c090 100644 --- a/pop3d/retr.c +++ b/pop3d/retr.c @@ -22,12 +22,10 @@ int pop3d_retr (char *arg) { - size_t mesgno, n; - char buf[BUFFERSIZE]; + size_t mesgno; mu_message_t msg = NULL; mu_attribute_t attr = NULL; - mu_stream_t stream = NULL; - int prev_nl; + mu_stream_t stream; if ((strlen (arg) == 0) || (strchr (arg, ' ') != NULL)) return ERR_BAD_ARGS; @@ -44,42 +42,19 @@ pop3d_retr (char *arg) if (pop3d_is_deleted (attr)) return ERR_MESG_DELE; - /* FIXME: Use crlf filter + mu_stream_copy instead of the loop below */ - if (mu_message_get_stream (msg, &stream) - || mu_stream_seek (stream, 0, MU_SEEK_SET, NULL)) + if (mu_message_get_streamref (msg, &stream)) return ERR_UNKNOWN; - pop3d_outf ("+OK\r\n"); - - prev_nl = 1; - while (mu_stream_readline (stream, buf, sizeof(buf), &n) == 0 - && n > 0) - { - if (prev_nl && buf[0] == '.') - pop3d_outf ("."); - - if (buf[n - 1] == '\n') - { - buf[n - 1] = '\0'; - pop3d_outf ("%s\r\n", buf); - prev_nl = 1; - } - else - { - pop3d_outf ("%s", buf); - prev_nl = 0; - } - } - - if (!prev_nl) - pop3d_outf ("\r\n"); + pop3d_outf ("+OK\n"); + mu_stream_copy (ostream, stream, 0); + mu_stream_destroy (&stream); if (!mu_attribute_is_read (attr)) mu_attribute_set_read (attr); pop3d_mark_retr (attr); - pop3d_outf (".\r\n"); + pop3d_outf (".\n"); return OK; } diff --git a/pop3d/rset.c b/pop3d/rset.c index 4a03acf48..48df589a7 100644 --- a/pop3d/rset.c +++ b/pop3d/rset.c @@ -41,6 +41,6 @@ pop3d_rset (char *arg) mu_message_get_attribute (msg, &attr); pop3d_unset_deleted (attr); } - pop3d_outf ("+OK\r\n"); + pop3d_outf ("+OK\n"); return OK; } diff --git a/pop3d/stat.c b/pop3d/stat.c index 0b4699c9d..9daf3ed16 100644 --- a/pop3d/stat.c +++ b/pop3d/stat.c @@ -57,7 +57,7 @@ pop3d_stat (char *arg) num++; } } - pop3d_outf ("+OK %s %s\r\n", mu_umaxtostr (0, num), mu_umaxtostr (1, tsize)); + pop3d_outf ("+OK %s %s\n", mu_umaxtostr (0, num), mu_umaxtostr (1, tsize)); return OK; } diff --git a/pop3d/stls.c b/pop3d/stls.c index 0e35ba7c1..189bc7cf5 100644 --- a/pop3d/stls.c +++ b/pop3d/stls.c @@ -32,7 +32,7 @@ pop3d_stls (char *arg) if (tls_done) return ERR_TLS_ACTIVE; - pop3d_outf ("+OK Begin TLS negotiation\r\n"); + pop3d_outf ("+OK Begin TLS negotiation\n"); pop3d_flush_output (); tls_done = pop3d_init_tls_server (); diff --git a/pop3d/top.c b/pop3d/top.c index c51356d84..bab0583ac 100644 --- a/pop3d/top.c +++ b/pop3d/top.c @@ -23,16 +23,14 @@ int pop3d_top (char *arg) { size_t mesgno; - int lines; + unsigned long lines; mu_message_t msg; mu_attribute_t attr; mu_header_t hdr; mu_body_t body; mu_stream_t stream; - char *mesgc, *linesc; - char buf[BUFFERSIZE]; - size_t n; - + char *mesgc, *linesc, *p; + if (strlen (arg) == 0) return ERR_BAD_ARGS; @@ -40,11 +38,14 @@ pop3d_top (char *arg) return ERR_WRONG_STATE; pop3d_parse_command (arg, &mesgc, &linesc); + if (linesc[0] == 0) + return ERR_BAD_ARGS; - mesgno = strtoul (mesgc, NULL, 10); - lines = *linesc ? strtol (linesc, NULL, 10) : -1; - - if (lines < 0) + mesgno = strtoul (mesgc, &p, 10); + if (*p) + return ERR_BAD_ARGS; + lines = strtoul (linesc, &p, 10); + if (*p) return ERR_BAD_ARGS; if (mu_mailbox_get_message (mbox, mesgno, &msg) != 0) @@ -55,63 +56,34 @@ pop3d_top (char *arg) return ERR_MESG_DELE; pop3d_mark_retr (attr); - pop3d_outf ("+OK\r\n"); - /* Header. */ mu_message_get_header (msg, &hdr); - /* FIXME: Use crlf filter + mu_stream_copy instead of the below loop */ - if (mu_header_get_stream (hdr, &stream) - || mu_stream_seek (stream, 0, MU_SEEK_SET, NULL)) + if (mu_header_get_streamref (hdr, &stream)) return ERR_UNKNOWN; - n = 0; - while (mu_stream_readline (stream, buf, sizeof(buf), &n) == 0 - && n > 0) - { - /* Nuke the trainline newline. */ - if (buf[n - 1] == '\n') - { - buf[n - 1] = '\0'; - pop3d_outf ("%s\r\n", buf); - } - else - pop3d_outf ("%s", buf); - } + pop3d_outf ("+OK\n"); - /* Lines of body. */ - if (lines) + mu_stream_copy (ostream, stream, 0); + pop3d_outf ("\n"); + mu_stream_destroy (&stream); + + mu_message_get_body (msg, &body); + if (mu_body_get_streamref (body, &stream) == 0) { - int prev_nl = 1; - - mu_message_get_body (msg, &body); - /* FIXME: Use the crlf filter + mu_stream_copy */ - if (mu_body_get_stream (body, &stream) - || mu_stream_seek (stream, 0, MU_SEEK_SET, NULL)) - return ERR_UNKNOWN; - n = 0; - while (mu_stream_readline (stream, buf, sizeof(buf), &n) == 0 - && n > 0 && lines > 0) + char *buf = NULL; + size_t size = 0, n; + while (lines > 0 && + mu_stream_getline (stream, &buf, &size, &n) == 0 && + n > 0) { - if (prev_nl && buf[0] == '.') + if (buf[0] == '.') pop3d_outf ("."); - - if (buf[n - 1] == '\n') - { - buf[n - 1] = '\0'; - pop3d_outf ("%s\r\n", buf); - prev_nl = 1; - lines--; - } - else - { - pop3d_outf ("%s", buf); - prev_nl = 0; - } + pop3d_outf ("%s\n", buf); } - if (!prev_nl) - pop3d_outf ("\r\n"); + mu_stream_destroy (&stream); + free (buf); } - pop3d_outf (".\r\n"); + pop3d_outf (".\n"); return OK; } diff --git a/pop3d/uidl.c b/pop3d/uidl.c index d52cc4134..fa3e24812 100644 --- a/pop3d/uidl.c +++ b/pop3d/uidl.c @@ -34,7 +34,7 @@ pop3d_uidl (char *arg) if (strlen (arg) == 0) { size_t total = 0; - pop3d_outf ("+OK\r\n"); + pop3d_outf ("+OK\n"); mu_mailbox_messages_count (mbox, &total); for (mesgno = 1; mesgno <= total; mesgno++) { @@ -43,10 +43,10 @@ pop3d_uidl (char *arg) if (!pop3d_is_deleted (attr)) { mu_message_get_uidl (msg, uidl, sizeof (uidl), NULL); - pop3d_outf ("%s %s\r\n", mu_umaxtostr (0, mesgno), uidl); + pop3d_outf ("%s %s\n", mu_umaxtostr (0, mesgno), uidl); } } - pop3d_outf (".\r\n"); + pop3d_outf (".\n"); } else { @@ -57,7 +57,7 @@ pop3d_uidl (char *arg) if (pop3d_is_deleted (attr)) return ERR_MESG_DELE; mu_message_get_uidl (msg, uidl, sizeof (uidl), NULL); - pop3d_outf ("+OK %s %s\r\n", mu_umaxtostr (0, mesgno), uidl); + pop3d_outf ("+OK %s %s\n", mu_umaxtostr (0, mesgno), uidl); } return OK; diff --git a/pop3d/user.c b/pop3d/user.c index 6cb130ce3..6d0ef3ff1 100644 --- a/pop3d/user.c +++ b/pop3d/user.c @@ -63,7 +63,7 @@ pop3d_begin_session () pop3d_abquit (ERR_NO_MEM); state = TRANSACTION; - pop3d_outf ("+OK opened mailbox for %s\r\n", username); + pop3d_outf ("+OK opened mailbox for %s\n", username); if (undelete_on_startup) pop3d_undelete_all (); @@ -98,7 +98,7 @@ pop3d_user (char *arg) if ((strlen (arg) == 0) || (strchr (arg, ' ') != NULL)) return ERR_BAD_ARGS; - pop3d_outf ("+OK\r\n"); + pop3d_outf ("+OK\n"); pop3d_flush_output (); buf = pop3d_readline (buffer, sizeof (buffer)); |