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 },
46 char *version=VERSION;
49 ** name of temporary file to store incoming message
51 static char *tmpfilenam = NULL;
54 static void fix_mbox(int out, char *ofile);
57 main(int argc, char **argv)
59 int publicsw = -1, zerosw = 0;
60 int create = 1, unseensw = 1;
63 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
64 char **argp, **arguments, *seqs[NUMATTRS+1];
68 if (atexit(unlink_done) != 0) {
69 adios(EX_OSERR, NULL, "atexit failed");
72 setlocale(LC_ALL, "");
73 invo_name = mhbasename(argv[0]);
75 /* read user profile/context */
78 arguments = getarguments(invo_name, argc, argv, 1);
82 while ((cp = *argp++)) {
84 switch (smatch(++cp, switches)) {
86 ambigsw(cp, switches);
89 adios(EX_USAGE, NULL, "-%s unknown", cp);
92 snprintf(buf, sizeof(buf),
93 "%s [+folder] [switches]",
95 print_help(buf, switches, 1);
96 exit(argc == 2 ? EX_OK : EX_USAGE);
98 print_version(invo_name);
99 exit(argc == 2 ? EX_OK : EX_USAGE);
102 if (!(cp = *argp++) || *cp == '-')
103 adios(EX_USAGE, NULL, "missing argument name to %s", argp[-2]);
105 /* check if too many sequences specified */
106 if (seqp >= NUMATTRS)
107 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
140 if (*cp == '+' || *cp == '@') {
142 adios(EX_USAGE, NULL, "only one folder at a time!");
144 folder = mh_xstrdup(expandfol(cp));
146 adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
151 seqs[seqp] = NULL; /* NULL terminate list of sequences */
153 /* if no folder is given, use default folder */
155 folder = getdeffol();
156 maildir = toabsdir(folder);
158 /* check if folder exists */
159 if (stat(maildir, &st) == NOTOK) {
161 adios(EX_IOERR, maildir, "error on folder");
163 adios(EX_USAGE, NULL, "folder %s doesn't exist", maildir);
164 if (!makedir(maildir))
165 adios(EX_CANTCREAT, NULL, "unable to create folder %s", maildir);
168 if (chdir(maildir) == NOTOK)
169 adios(EX_OSERR, maildir, "unable to change directory to");
171 /* ignore a few signals */
172 SIGNAL(SIGHUP, SIG_IGN);
173 SIGNAL(SIGINT, SIG_IGN);
174 SIGNAL(SIGQUIT, SIG_IGN);
175 SIGNAL(SIGTERM, SIG_IGN);
177 /* create a temporary file */
178 tmpfilenam = m_mktemp(invo_name, &fd, NULL);
179 if (tmpfilenam == NULL) {
180 adios(EX_CANTCREAT, "rcvstore", "unable to create temporary file");
182 chmod(tmpfilenam, m_gmprot());
184 /* check if incoming mail is in mbox-format */
185 fix_mbox(fd, tmpfilenam);
187 /* copy the message from stdin into temp file */
188 cpydata(fileno(stdin), fd, "standard input", tmpfilenam);
190 if (fstat(fd, &st) == NOTOK) {
192 adios(EX_IOERR, tmpfilenam, "unable to fstat");
194 if (close(fd) == NOTOK)
195 adios(EX_IOERR, tmpfilenam, "error closing");
197 /* don't add file if it is empty */
198 if (st.st_size == 0) {
200 advise(NULL, "empty file");
205 ** read folder and create message structure
207 if (!(mp = folder_read(folder)))
208 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
211 ** Link message into folder, and possibly add
212 ** to the Unseen-Sequence's.
214 if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL))
219 ** Add the message to any extra sequences
220 ** that have been specified.
222 for (seqp = 0; seqs[seqp]; seqp++) {
223 if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw))
227 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
228 seq_save(mp); /* synchronize and save message sequences */
229 folder_free(mp); /* free folder/message structure */
231 context_save(); /* save the global context file */
232 unlink(tmpfilenam); /* remove temporary file */
239 fix_mbox(int out, char *outfile)
244 if ((ret = read(fileno(stdin), mbox, sizeof(mbox))) != sizeof(mbox)) {
246 adios(EX_IOERR, "standard input", "error reading");
251 if (strncmp(mbox, "From ", sizeof(mbox))==0) {
253 if ((ret = read(fileno(stdin), mbox, 1)) != 1) {
255 adios(EX_IOERR, "standard input", "error reading");
259 } while (*mbox != '\n');
261 if (write(out, mbox, sizeof(mbox)) != sizeof(mbox)) {
262 adios(EX_IOERR, outfile, "error writing");
273 if (tmpfilenam && *tmpfilenam) {