mblaze/mexport.c

157 lines
2.8 KiB
C
Raw Normal View History

2016-08-19 15:53:40 +00:00
#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"
2016-08-19 15:53:40 +00:00
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));
2016-08-19 15:53:40 +00:00
return;
}
2016-08-19 15:53:40 +00:00
char from[1024] = "nobody";
time_t date = 0;
2016-08-19 15:53:40 +00:00
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, '<');
2017-06-24 13:46:00 +00:00
if (s) {
char *e = strchr(s, '>');
if (e) {
s++;
snprintf(from, sizeof from, "%.*s",
2017-08-31 15:30:17 +00:00
(int)(e-s), s);
2017-06-24 13:46:00 +00:00
}
} else { // return-path without <>
snprintf(from, sizeof from, "%s", v);
}
2016-08-19 15:53:40 +00:00
}
if ((v = blaze822_hdr(msg, "date"))) {
date = blaze822_date(v);
}
blaze822_free(msg);
2016-08-19 15:53:40 +00:00
}
char *line = 0;
size_t linelen = 0;
2017-08-31 15:30:17 +00:00
printf("From %s %s", from, asctime(gmtime(&date)));
2016-08-19 15:53:40 +00:00
int in_header = 1;
int final_nl = 0;
while (1) {
errno = 0;
ssize_t rd = getdelim(&line, &linelen, '\n', infile);
2016-08-19 15:53:40 +00:00
if (rd == -1) {
if (errno == 0)
break;
fprintf(stderr, "mexport: error reading '%s': %s\n",
file, strerror(errno));
2016-08-19 15:53:40 +00:00
status = 1;
return;
}
2017-08-31 15:30:17 +00:00
2016-08-19 15:53:40 +00:00
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, '/');
2017-08-31 15:30:17 +00:00
if (!ee ||
2016-08-19 15:53:40 +00:00
!(ee >= file + 3 && ee[-3] == 'n' && ee[-2] == 'e' && ee[-1] == 'w'))
putchar('O');
putchar('\n');
2017-08-31 15:30:17 +00:00
2016-08-19 15:53:40 +00:00
fputs("X-Status: ", stdout);
if (strchr(flags, 'R')) putchar('A');
if (strchr(flags, 'T')) putchar('D');
if (strchr(flags, 'F')) putchar('F');
putchar('\n');
}
2017-08-31 15:30:17 +00:00
2016-08-19 15:53:40 +00:00
in_header = 0;
}
2017-08-31 15:30:17 +00:00
2016-08-19 15:53:40 +00:00
// 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');
// ensure empty line at end of message
putchar('\n');
2016-08-19 15:53:40 +00:00
fclose(infile);
}
int
main(int argc, char *argv[])
{
int c;
while ((c = getopt(argc, argv, "S")) != -1)
2017-08-31 15:30:17 +00:00
switch (c) {
2016-08-19 15:53:40 +00:00
case 'S': Sflag = 1; break;
default:
fprintf(stderr, "Usage: mexport [-S] [msgs...]\n");
exit(2);
}
status = 0;
xpledge("stdio rpath", "");
2016-08-19 15:53:40 +00:00
if (argc == optind && isatty(0))
blaze822_loop1(":", export);
else
blaze822_loop(argc-optind, argv+optind, export);
2017-08-31 15:30:17 +00:00
2016-08-19 15:53:40 +00:00
return status;
}