1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "blaze822.h"
#include "xpledge.h"
static int Sflag;
static int status;
void
export(char *file)
{
struct message *msg;
while (*file == ' ' || *file == '\t')
file++;
FILE *infile = fopen(file, "r");
if (!infile) {
status = 1;
fprintf(stderr, "mexport: error opening '%s': %s\n",
file, strerror(errno));
return;
}
char from[1024] = "nobody";
time_t date = -1;
if (fseek(infile, 0L, SEEK_SET) && errno == ESPIPE) {
date = time(0);
memcpy(from, "stdin", 6);
} else {
msg = blaze822(file);
if (!msg)
return;
char *v;
if ((v = blaze822_hdr(msg, "return-path")) ||
(v = blaze822_hdr(msg, "x-envelope-from"))) {
char *s = strchr(v, '<');
if (s) {
char *e = strchr(s, '>');
if (e) {
s++;
snprintf(from, sizeof from, "%.*s",
(int)(e-s), s);
}
} else { // return-path without <>
snprintf(from, sizeof from, "%s", v);
}
}
if ((v = blaze822_hdr(msg, "date"))) {
date = blaze822_date(v);
}
blaze822_free(msg);
}
char *line = 0;
size_t linelen = 0;
printf("From %s %s", from, ctime(&date));
int in_header = 1;
int final_nl = 0;
while (1) {
errno = 0;
ssize_t rd = getdelim(&line, &linelen, '\n', infile);
if (rd == -1) {
if (errno == 0)
break;
fprintf(stderr, "mexport: error reading '%s': %s\n",
file, strerror(errno));
status = 1;
return;
}
if (in_header && line[0] == '\n' && !line[1]) {
if (Sflag) {
char *flags = strstr(file, ":2,");
if (!flags)
flags = "";
fputs("Status: ", stdout);
if (strchr(flags, 'S'))
putchar('R');
char *ee = strrchr(file, '/');
if (!ee ||
!(ee >= file + 3 && ee[-3] == 'n' && ee[-2] == 'e' && ee[-1] == 'w'))
putchar('O');
putchar('\n');
fputs("X-Status: ", stdout);
if (strchr(flags, 'R')) putchar('A');
if (strchr(flags, 'T')) putchar('D');
if (strchr(flags, 'F')) putchar('F');
putchar('\n');
}
in_header = 0;
}
// MBOXRD: add first > to >>..>>From
char *s = line;
while (*s == '>')
s++;
if (strncmp("From ", s, 5) == 0)
putchar('>');
fputs(line, stdout);
final_nl = (line[rd-1] == '\n');
}
// ensure trailing newline
if (!final_nl)
putchar('\n');
fclose(infile);
}
int
main(int argc, char *argv[])
{
int c;
while ((c = getopt(argc, argv, "S")) != -1)
switch (c) {
case 'S': Sflag = 1; break;
default:
fprintf(stderr, "Usage: mexport [-S] [msgs...]\n");
exit(2);
}
status = 0;
xpledge("stdio rpath", "");
if (argc == optind && isatty(0))
blaze822_loop1(":", export);
else
blaze822_loop(argc-optind, argv+optind, export);
return status;
}
|