2 ** rcvstore.c -- asynchronously add mail to a folder
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
12 #include <h/signals.h>
20 static struct swit switches[] = {
38 { "sequence name", 0 },
48 ** name of temporary file to store incoming message
50 static char *tmpfilenam = NULL;
53 static void fix_mbox(int out, char *ofile);
56 main(int argc, char **argv)
58 int publicsw = -1, zerosw = 0;
59 int create = 1, unseensw = 1;
62 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
63 char **argp, **arguments, *seqs[NUMATTRS+1];
67 if (atexit(unlink_done) != 0) {
68 adios(EX_OSERR, NULL, "atexit failed");
71 setlocale(LC_ALL, "");
72 invo_name = mhbasename(argv[0]);
74 /* read user profile/context */
77 arguments = getarguments(invo_name, argc, argv, 1);
81 while ((cp = *argp++)) {
83 switch (smatch(++cp, switches)) {
85 ambigsw(cp, switches);
88 adios(EX_USAGE, NULL, "-%s unknown", cp);
91 snprintf(buf, sizeof(buf),
92 "%s [+folder] [switches]",
94 print_help(buf, switches, 1);
95 exit(argc == 2 ? EX_OK : EX_USAGE);
97 print_version(invo_name);
98 exit(argc == 2 ? EX_OK : EX_USAGE);
101 if (!(cp = *argp++) || *cp == '-')
102 adios(EX_USAGE, NULL, "missing argument name to %s", argp[-2]);
104 /* check if too many sequences specified */
105 if (seqp >= NUMATTRS)
106 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
139 if (*cp == '+' || *cp == '@') {
141 adios(EX_USAGE, NULL, "only one folder at a time!");
143 folder = mh_xstrdup(expandfol(cp));
145 adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
150 seqs[seqp] = NULL; /* NULL terminate list of sequences */
152 /* if no folder is given, use default folder */
154 folder = getdeffol();
155 maildir = toabsdir(folder);
157 /* check if folder exists */
158 if (stat(maildir, &st) == NOTOK) {
160 adios(EX_IOERR, maildir, "error on folder");
162 adios(EX_USAGE, NULL, "folder %s doesn't exist", maildir);
163 if (!makedir(maildir))
164 adios(EX_CANTCREAT, NULL, "unable to create folder %s", maildir);
167 if (chdir(maildir) == NOTOK)
168 adios(EX_OSERR, maildir, "unable to change directory to");
170 /* ignore a few signals */
171 SIGNAL(SIGHUP, SIG_IGN);
172 SIGNAL(SIGINT, SIG_IGN);
173 SIGNAL(SIGQUIT, SIG_IGN);
174 SIGNAL(SIGTERM, SIG_IGN);
176 /* create a temporary file */
177 tmpfilenam = m_mktemp(invo_name, &fd, NULL);
178 if (tmpfilenam == NULL) {
179 adios(EX_CANTCREAT, "rcvstore", "unable to create temporary file");
181 chmod(tmpfilenam, m_gmprot());
183 /* check if incoming mail is in mbox-format */
184 fix_mbox(fd, tmpfilenam);
186 /* copy the message from stdin into temp file */
187 cpydata(fileno(stdin), fd, "standard input", tmpfilenam);
189 if (fstat(fd, &st) == NOTOK) {
191 adios(EX_IOERR, tmpfilenam, "unable to fstat");
193 if (close(fd) == NOTOK)
194 adios(EX_IOERR, tmpfilenam, "error closing");
196 /* don't add file if it is empty */
197 if (st.st_size == 0) {
199 advise(NULL, "empty file");
204 ** read folder and create message structure
206 if (!(mp = folder_read(folder)))
207 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
210 ** Link message into folder, and possibly add
211 ** to the Unseen-Sequence's.
213 if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL))
218 ** Add the message to any extra sequences
219 ** that have been specified.
221 for (seqp = 0; seqs[seqp]; seqp++) {
222 if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw))
226 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
227 seq_save(mp); /* synchronize and save message sequences */
228 folder_free(mp); /* free folder/message structure */
230 context_save(); /* save the global context file */
231 unlink(tmpfilenam); /* remove temporary file */
238 fix_mbox(int out, char *outfile)
243 if ((ret = read(fileno(stdin), mbox, sizeof(mbox))) != sizeof(mbox)) {
245 adios(EX_IOERR, "standard input", "error reading");
250 if (strncmp(mbox, "From ", sizeof(mbox))==0) {
252 if ((ret = read(fileno(stdin), mbox, 1)) != 1) {
254 adios(EX_IOERR, "standard input", "error reading");
258 } while (*mbox != '\n');
260 if (write(out, mbox, sizeof(mbox)) != sizeof(mbox)) {
261 adios(EX_IOERR, outfile, "error writing");
272 if (tmpfilenam && *tmpfilenam) {