commit c3419cb5358fde81658eb5edb3fdb7ba4f8701f6
parent 10c8b6a8d65ae8c91677ec3a532416ab8f594058
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Tue, 5 Apr 2022 01:56:14 +0000
Fix message output
- Make the mbox output reasonably conforment to RFC 4155
- Fix external mailer with multiple messages
Diffstat:
M | pop3.c | | | 73 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------ |
1 file changed, 55 insertions(+), 18 deletions(-)
diff --git a/pop3.c b/pop3.c
@@ -1,4 +1,4 @@
-/* $Id: pop3.c,v 1.5 2022/04/04 14:59:27 jacob Exp $ */
+/* $Id: pop3.c,v 1.6 2022/04/05 01:56:14 jacob Exp $ */
/*
* Copyright (c) 2022 Jacob R. Edwards
@@ -364,22 +364,27 @@ pop_list(FILE *fp, int *_len)
}
int
-pop_retr(FILE *fp, char *msg)
+pop_readmsg(FILE *fp, char *buf)
{
- char buf[LINEMAX], *p;
+ int len;
- if (!pop_okay(pop_comd(fp, buf, sizeof(buf), "RETR", msg)))
- return 1;
+ if (!pop_read(fp, buf, LINEMAX))
+ return -1;
+ if (strcmp(buf, ".") == 0)
+ return 0;
- while ((p = pop_read(fp, buf, sizeof(buf))) && strcmp(p, ".") != 0) {
- if (*p == '.')
- ++p;
- if (puts(p) < 0)
- return 1;
- }
- if (p == NULL)
- return 1;
- return puts("") < 0;
+ len = strlen(buf);
+ if (*buf == '.')
+ memmove(buf, buf + 1, --len);
+ return len + 1;
+}
+
+int
+pop_retr(FILE *fp, char *msg)
+{
+ char buf[POPMAX];
+
+ return pop_okay(pop_comd(fp, buf, sizeof(buf), "RETR", msg)) == NULL;
}
int
@@ -391,6 +396,21 @@ pop_dele(FILE *fp, char *msg)
}
int
+writemsg(FILE *fp, char *msg, int out)
+{
+ char buf[LINEMAX];
+ int len;
+
+ if (pop_retr(fp, msg))
+ return 1;
+ while ((len = pop_readmsg(fp, buf)) > 0) {
+ if (dprintf(out, "%s\n", buf) < 0)
+ return 1;
+ }
+ return len != 0;
+}
+
+int
mail(FILE *fp, char *msg, char **mailer)
{
int fds[2];
@@ -412,14 +432,13 @@ mail(FILE *fp, char *msg, char **mailer)
_exit(127);
}
- if (close(fds[0]) < 0 || dup2(fds[1], 1) < 0 ||
- pop_retr(fp, msg) || fflush(stdout)) {
+ if (close(fds[0]) < 0 || writemsg(fp, msg, fds[1])) {
close(fds[1]);
kill(pid, SIGTERM);
return -1;
}
- if (close(fds[1]) < 0 || close(1) < 0) {
+ if (close(fds[1]) < 0) {
kill(pid, SIGTERM);
return -1;
}
@@ -432,7 +451,25 @@ mail(FILE *fp, char *msg, char **mailer)
int
mbox(FILE *fp, char *msg, char **unused)
{
- return pop_retr(fp, msg);
+ char buf[LINEMAX];
+ int escape;
+ int len;
+ time_t tim;
+
+ if (pop_retr(fp, msg))
+ return -1;
+
+ tim = time(NULL);
+ printf("From <unknown> %s", ctime(&tim));
+ while ((len = pop_readmsg(fp, buf)) > 0) {
+ escape = strncmp(buf, "From ", 5) == 0;
+ if (printf(escape ? ">%s\n" : "%s\n", buf) < 0)
+ return -1;
+ }
+
+ if (len != 0 || printf("\n") < 0)
+ return -1;
+ return 0;
}
int