X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=uip%2Frcvstore.c;h=33f8f3bcedd3f0bc6ce4720cd52702dba1bdc791;hp=fbddda7faba9d0fc51df3d34e004c157df56513a;hb=6e9577f324bef90765a5edc02044eb111ec48072;hpb=fede6e42d81ce34fd5c1bbe7fb2757b281c2573a diff --git a/uip/rcvstore.c b/uip/rcvstore.c index fbddda7..33f8f3b 100644 --- a/uip/rcvstore.c +++ b/uip/rcvstore.c @@ -7,10 +7,15 @@ */ #include +#include #include #include #include #include +#include +#include +#include +#include static struct swit switches[] = { #define CRETSW 0 @@ -38,13 +43,15 @@ static struct swit switches[] = { { NULL, 0 } }; +char *version=VERSION; /* ** name of temporary file to store incoming message */ static char *tmpfilenam = NULL; -static void unlink_done(); +void unlink_done(); +static void fix_mbox(int out, char *ofile); int main(int argc, char **argv) @@ -58,7 +65,9 @@ main(int argc, char **argv) struct msgs *mp; struct stat st; - atexit(unlink_done); + if (atexit(unlink_done) != 0) { + adios(EX_OSERR, NULL, "atexit failed"); + } setlocale(LC_ALL, ""); invo_name = mhbasename(argv[0]); @@ -75,27 +84,27 @@ main(int argc, char **argv) switch (smatch(++cp, switches)) { case AMBIGSW: ambigsw(cp, switches); - exit(1); + exit(EX_USAGE); case UNKWNSW: - adios(NULL, "-%s unknown", cp); + adios(EX_USAGE, NULL, "-%s unknown", cp); case HELPSW: snprintf(buf, sizeof(buf), "%s [+folder] [switches]", invo_name); print_help(buf, switches, 1); - exit(1); + exit(argc == 2 ? EX_OK : EX_USAGE); case VERSIONSW: print_version(invo_name); - exit(1); + exit(argc == 2 ? EX_OK : EX_USAGE); case SEQSW: if (!(cp = *argp++) || *cp == '-') - adios(NULL, "missing argument name to %s", argp[-2]); + adios(EX_USAGE, NULL, "missing argument name to %s", argp[-2]); /* check if too many sequences specified */ if (seqp >= NUMATTRS) - adios(NULL, "too many sequences (more than %d) specified", NUMATTRS); + adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS); seqs[seqp++] = cp; continue; @@ -130,11 +139,11 @@ main(int argc, char **argv) } if (*cp == '+' || *cp == '@') { if (folder) - adios(NULL, "only one folder at a time!"); + adios(EX_USAGE, NULL, "only one folder at a time!"); else - folder = getcpy(expandfol(cp)); + folder = mh_xstrdup(expandfol(cp)); } else { - adios(NULL, "usage: %s [+folder] [switches]", + adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]", invo_name); } } @@ -149,15 +158,15 @@ main(int argc, char **argv) /* check if folder exists */ if (stat(maildir, &st) == NOTOK) { if (errno != ENOENT) - adios(maildir, "error on folder"); + adios(EX_IOERR, maildir, "error on folder"); if (!create) - adios(NULL, "folder %s doesn't exist", maildir); + adios(EX_USAGE, NULL, "folder %s doesn't exist", maildir); if (!makedir(maildir)) - adios(NULL, "unable to create folder %s", maildir); + adios(EX_CANTCREAT, NULL, "unable to create folder %s", maildir); } if (chdir(maildir) == NOTOK) - adios(maildir, "unable to change directory to"); + adios(EX_OSERR, maildir, "unable to change directory to"); /* ignore a few signals */ SIGNAL(SIGHUP, SIG_IGN); @@ -168,32 +177,35 @@ main(int argc, char **argv) /* create a temporary file */ tmpfilenam = m_mktemp(invo_name, &fd, NULL); if (tmpfilenam == NULL) { - adios("rcvstore", "unable to create temporary file"); + adios(EX_CANTCREAT, "rcvstore", "unable to create temporary file"); } chmod(tmpfilenam, m_gmprot()); + /* check if incoming mail is in mbox-format */ + fix_mbox(fd, tmpfilenam); + /* copy the message from stdin into temp file */ cpydata(fileno(stdin), fd, "standard input", tmpfilenam); if (fstat(fd, &st) == NOTOK) { unlink(tmpfilenam); - adios(tmpfilenam, "unable to fstat"); + adios(EX_IOERR, tmpfilenam, "unable to fstat"); } if (close(fd) == NOTOK) - adios(tmpfilenam, "error closing"); + adios(EX_IOERR, tmpfilenam, "error closing"); /* don't add file if it is empty */ if (st.st_size == 0) { unlink(tmpfilenam); advise(NULL, "empty file"); - exit(0); + exit(EX_OK); } /* ** read folder and create message structure */ if (!(mp = folder_read(folder))) - adios(NULL, "unable to read folder %s", folder); + adios(EX_IOERR, NULL, "unable to read folder %s", folder); /* ** Link message into folder, and possibly add @@ -201,7 +213,7 @@ main(int argc, char **argv) */ if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL)) == -1) - exit(1); + exit(EX_SOFTWARE); /* ** Add the message to any extra sequences @@ -209,7 +221,7 @@ main(int argc, char **argv) */ for (seqp = 0; seqs[seqp]; seqp++) { if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw)) - exit(1); + exit(EX_SOFTWARE); } seq_setunseen(mp, 1); /* add new msgs to unseen sequences */ @@ -220,13 +232,42 @@ main(int argc, char **argv) unlink(tmpfilenam); /* remove temporary file */ tmpfilenam = NULL; - return 0; + return EX_OK; +} + +static void +fix_mbox(int out, char *outfile) +{ + char mbox[5]; + int ret; + + if ((ret = read(fileno(stdin), mbox, sizeof(mbox))) != sizeof(mbox)) { + if (ret == -1) { + adios(EX_IOERR, "standard input", "error reading"); + } + return; + } + + if (strncmp(mbox, "From ", sizeof(mbox))==0) { + do { + if ((ret = read(fileno(stdin), mbox, 1)) != 1) { + if (ret == -1) { + adios(EX_IOERR, "standard input", "error reading"); + } + return; + } + } while (*mbox != '\n'); + } else { + if (write(out, mbox, sizeof(mbox)) != sizeof(mbox)) { + adios(EX_IOERR, outfile, "error writing"); + } + } } /* ** Clean up and exit */ -static void +void unlink_done() { if (tmpfilenam && *tmpfilenam) {