X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=uip%2Frcvstore.c;h=33f8f3bcedd3f0bc6ce4720cd52702dba1bdc791;hp=43d38892f4581b3d9285073330985996c2c2e2d5;hb=6e9577f324bef90765a5edc02044eb111ec48072;hpb=8563731b02ce9d750806f6b1769af8b399d964e8 diff --git a/uip/rcvstore.c b/uip/rcvstore.c index 43d3889..33f8f3b 100644 --- a/uip/rcvstore.c +++ b/uip/rcvstore.c @@ -1,235 +1,276 @@ - /* - * rcvstore.c -- asynchronously add mail to a folder - * - * $Id$ - */ +** rcvstore.c -- asynchronously add mail to a folder +** +** This code is Copyright (c) 2002, by the authors of nmh. See the +** COPYRIGHT file in the root directory of the nmh distribution for +** complete copyright information. +*/ #include +#include #include #include #include #include -#include +#include +#include +#include +#include static struct swit switches[] = { -#define CRETSW 0 - { "create", 0 }, -#define NCRETSW 1 - { "nocreate", 0 }, -#define UNSEENSW 2 - { "unseen", 0 }, -#define NUNSEENSW 3 - { "nounseen", 0 }, -#define PUBSW 4 - { "public", 0 }, -#define NPUBSW 5 - { "nopublic", 0 }, -#define ZEROSW 6 - { "zero", 0 }, -#define NZEROSW 7 - { "nozero", 0 }, -#define SEQSW 8 - { "sequence name", 0 }, -#define VERSIONSW 9 - { "version", 0 }, -#define HELPSW 10 - { "help", 4 }, - { NULL, 0 } +#define CRETSW 0 + { "create", 0 }, +#define NCRETSW 1 + { "nocreate", 2 }, +#define UNSEENSW 2 + { "unseen", 0 }, +#define NUNSEENSW 3 + { "nounseen", 2 }, +#define PUBSW 4 + { "public", 0 }, +#define NPUBSW 5 + { "nopublic", 2 }, +#define ZEROSW 6 + { "zero", 0 }, +#define NZEROSW 7 + { "nozero", 2 }, +#define SEQSW 8 + { "sequence name", 0 }, +#define VERSIONSW 9 + { "Version", 0 }, +#define HELPSW 10 + { "help", 0 }, + { NULL, 0 } }; -extern int errno; +char *version=VERSION; /* - * name of temporary file to store incoming message - */ +** name of temporary file to store incoming message +*/ static char *tmpfilenam = NULL; +void unlink_done(); +static void fix_mbox(int out, char *ofile); int -main (int argc, char **argv) +main(int argc, char **argv) +{ + int publicsw = -1, zerosw = 0; + int create = 1, unseensw = 1; + int fd, msgnum; + size_t seqp = 0; + char *cp, *maildir, *folder = NULL, buf[BUFSIZ]; + char **argp, **arguments, *seqs[NUMATTRS+1]; + struct msgs *mp; + struct stat st; + + if (atexit(unlink_done) != 0) { + adios(EX_OSERR, NULL, "atexit failed"); + } + + setlocale(LC_ALL, ""); + invo_name = mhbasename(argv[0]); + + /* read user profile/context */ + context_read(); + + arguments = getarguments(invo_name, argc, argv, 1); + argp = arguments; + + /* parse arguments */ + while ((cp = *argp++)) { + if (*cp == '-') { + switch (smatch(++cp, switches)) { + case AMBIGSW: + ambigsw(cp, switches); + exit(EX_USAGE); + case UNKWNSW: + adios(EX_USAGE, NULL, "-%s unknown", cp); + + case HELPSW: + snprintf(buf, sizeof(buf), + "%s [+folder] [switches]", + invo_name); + print_help(buf, switches, 1); + exit(argc == 2 ? EX_OK : EX_USAGE); + case VERSIONSW: + print_version(invo_name); + exit(argc == 2 ? EX_OK : EX_USAGE); + + case SEQSW: + if (!(cp = *argp++) || *cp == '-') + adios(EX_USAGE, NULL, "missing argument name to %s", argp[-2]); + + /* check if too many sequences specified */ + if (seqp >= NUMATTRS) + adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS); + seqs[seqp++] = cp; + continue; + + case UNSEENSW: + unseensw = 1; + continue; + case NUNSEENSW: + unseensw = 0; + continue; + + case PUBSW: + publicsw = 1; + continue; + case NPUBSW: + publicsw = 0; + continue; + + case ZEROSW: + zerosw++; + continue; + case NZEROSW: + zerosw = 0; + continue; + + case CRETSW: + create++; + continue; + case NCRETSW: + create = 0; + continue; + } + } + if (*cp == '+' || *cp == '@') { + if (folder) + adios(EX_USAGE, NULL, "only one folder at a time!"); + else + folder = mh_xstrdup(expandfol(cp)); + } else { + adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]", + invo_name); + } + } + + seqs[seqp] = NULL; /* NULL terminate list of sequences */ + + /* if no folder is given, use default folder */ + if (!folder) + folder = getdeffol(); + maildir = toabsdir(folder); + + /* check if folder exists */ + if (stat(maildir, &st) == NOTOK) { + if (errno != ENOENT) + adios(EX_IOERR, maildir, "error on folder"); + if (!create) + adios(EX_USAGE, NULL, "folder %s doesn't exist", maildir); + if (!makedir(maildir)) + adios(EX_CANTCREAT, NULL, "unable to create folder %s", maildir); + } + + if (chdir(maildir) == NOTOK) + adios(EX_OSERR, maildir, "unable to change directory to"); + + /* ignore a few signals */ + SIGNAL(SIGHUP, SIG_IGN); + SIGNAL(SIGINT, SIG_IGN); + SIGNAL(SIGQUIT, SIG_IGN); + SIGNAL(SIGTERM, SIG_IGN); + + /* create a temporary file */ + tmpfilenam = m_mktemp(invo_name, &fd, NULL); + if (tmpfilenam == NULL) { + 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(EX_IOERR, tmpfilenam, "unable to fstat"); + } + if (close(fd) == NOTOK) + 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(EX_OK); + } + + /* + ** read folder and create message structure + */ + if (!(mp = folder_read(folder))) + adios(EX_IOERR, NULL, "unable to read folder %s", folder); + + /* + ** Link message into folder, and possibly add + ** to the Unseen-Sequence's. + */ + if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL)) + == -1) + exit(EX_SOFTWARE); + + /* + ** Add the message to any extra sequences + ** that have been specified. + */ + for (seqp = 0; seqs[seqp]; seqp++) { + if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw)) + exit(EX_SOFTWARE); + } + + seq_setunseen(mp, 1); /* add new msgs to unseen sequences */ + seq_save(mp); /* synchronize and save message sequences */ + folder_free(mp); /* free folder/message structure */ + + context_save(); /* save the global context file */ + unlink(tmpfilenam); /* remove temporary file */ + tmpfilenam = NULL; + + return EX_OK; +} + +static void +fix_mbox(int out, char *outfile) { - int publicsw = -1, zerosw = 0; - int create = 1, unseensw = 1; - int fd, msgnum, seqp = 0; - char *cp, *maildir, *folder = NULL, buf[BUFSIZ]; - char **argp, **arguments, *seqs[NUMATTRS+1]; - struct msgs *mp; - struct stat st; - -#ifdef LOCALE - setlocale(LC_ALL, ""); -#endif - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); - - mts_init (invo_name); - arguments = getarguments (invo_name, argc, argv, 1); - argp = arguments; - - /* parse arguments */ - while ((cp = *argp++)) { - if (*cp == '-') { - switch (smatch (++cp, switches)) { - case AMBIGSW: - ambigsw (cp, switches); - done (1); - case UNKWNSW: - adios (NULL, "-%s unknown", cp); - - case HELPSW: - snprintf (buf, sizeof(buf), "%s [+folder] [switches]", - invo_name); - print_help (buf, switches, 1); - done (1); - case VERSIONSW: - print_version(invo_name); - done (1); - - case SEQSW: - if (!(cp = *argp++) || *cp == '-') - adios (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); - seqs[seqp++] = cp; - continue; - - case UNSEENSW: - unseensw = 1; - continue; - case NUNSEENSW: - unseensw = 0; - continue; - - case PUBSW: - publicsw = 1; - continue; - case NPUBSW: - publicsw = 0; - continue; - - case ZEROSW: - zerosw++; - continue; - case NZEROSW: - zerosw = 0; - continue; - - case CRETSW: - create++; - continue; - case NCRETSW: - create = 0; - continue; - } + 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 (*cp == '+' || *cp == '@') { - if (folder) - adios (NULL, "only one folder at a time!"); - else - folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF); + + 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 { - adios (NULL, "usage: %s [+folder] [switches]", invo_name); + if (write(out, mbox, sizeof(mbox)) != sizeof(mbox)) { + adios(EX_IOERR, outfile, "error writing"); + } } - } - - seqs[seqp] = NULL; /* NULL terminate list of sequences */ - - if (!context_find ("path")) - free (path ("./", TFOLDER)); - - /* if no folder is given, use default folder */ - if (!folder) - folder = getfolder (0); - maildir = m_maildir (folder); - - /* check if folder exists */ - if (stat (maildir, &st) == NOTOK) { - if (errno != ENOENT) - adios (maildir, "error on folder"); - if (!create) - adios (NULL, "folder %s doesn't exist", maildir); - if (!makedir (maildir)) - adios (NULL, "unable to create folder %s", maildir); - } - - if (chdir (maildir) == NOTOK) - adios (maildir, "unable to change directory to"); - - /* ignore a few signals */ - SIGNAL (SIGHUP, SIG_IGN); - SIGNAL (SIGINT, SIG_IGN); - SIGNAL (SIGQUIT, SIG_IGN); - SIGNAL (SIGTERM, SIG_IGN); - - /* create a temporary file */ - tmpfilenam = m_scratch ("", invo_name); - if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK) - adios (tmpfilenam, "unable to create"); - chmod (tmpfilenam, m_gmprot()); - - /* 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"); - } - if (close (fd) == NOTOK) - adios (tmpfilenam, "error closing"); - - /* don't add file if it is empty */ - if (st.st_size == 0) { - unlink (tmpfilenam); - advise (NULL, "empty file"); - done (0); - } - - /* - * read folder and create message structure - */ - if (!(mp = folder_read (folder))) - adios (NULL, "unable to read folder %s", folder); - - /* - * Link message into folder, and possibly add - * to the Unseen-Sequence's. - */ - if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0)) == -1) - done (1); - - /* - * Add the message to any extra sequences - * that have been specified. - */ - for (seqp = 0; seqs[seqp]; seqp++) { - if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw)) - done (1); - } - - seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */ - seq_save (mp); /* synchronize and save message sequences */ - folder_free (mp); /* free folder/message structure */ - - context_save (); /* save the global context file */ - unlink (tmpfilenam); /* remove temporary file */ - tmpfilenam = NULL; - - return done (0); } /* - * Clean up and exit - */ -int -done(int status) +** Clean up and exit +*/ +void +unlink_done() { - if (tmpfilenam && *tmpfilenam) - unlink (tmpfilenam); - exit (status); - return 1; /* dead code to satisfy the compiler */ + if (tmpfilenam && *tmpfilenam) { + unlink(tmpfilenam); + } }