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.
11 #include <h/signals.h>
15 static struct swit switches[] = {
33 { "sequence name", 0 },
43 ** name of temporary file to store incoming message
45 static char *tmpfilenam = NULL;
50 main(int argc, char **argv)
52 int publicsw = -1, zerosw = 0;
53 int create = 1, unseensw = 1;
56 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
57 char **argp, **arguments, *seqs[NUMATTRS+1];
61 if (atexit(unlink_done) != 0) {
62 adios(NULL, "atexit failed");
65 setlocale(LC_ALL, "");
66 invo_name = mhbasename(argv[0]);
68 /* read user profile/context */
71 arguments = getarguments(invo_name, argc, argv, 1);
75 while ((cp = *argp++)) {
77 switch (smatch(++cp, switches)) {
79 ambigsw(cp, switches);
82 adios(NULL, "-%s unknown", cp);
85 snprintf(buf, sizeof(buf),
86 "%s [+folder] [switches]",
88 print_help(buf, switches, 1);
91 print_version(invo_name);
95 if (!(cp = *argp++) || *cp == '-')
96 adios(NULL, "missing argument name to %s", argp[-2]);
98 /* check if too many sequences specified */
100 adios(NULL, "too many sequences (more than %d) specified", NUMATTRS);
133 if (*cp == '+' || *cp == '@') {
135 adios(NULL, "only one folder at a time!");
137 folder = getcpy(expandfol(cp));
139 adios(NULL, "usage: %s [+folder] [switches]",
144 seqs[seqp] = NULL; /* NULL terminate list of sequences */
146 /* if no folder is given, use default folder */
148 folder = getdeffol();
149 maildir = toabsdir(folder);
151 /* check if folder exists */
152 if (stat(maildir, &st) == NOTOK) {
154 adios(maildir, "error on folder");
156 adios(NULL, "folder %s doesn't exist", maildir);
157 if (!makedir(maildir))
158 adios(NULL, "unable to create folder %s", maildir);
161 if (chdir(maildir) == NOTOK)
162 adios(maildir, "unable to change directory to");
164 /* ignore a few signals */
165 SIGNAL(SIGHUP, SIG_IGN);
166 SIGNAL(SIGINT, SIG_IGN);
167 SIGNAL(SIGQUIT, SIG_IGN);
168 SIGNAL(SIGTERM, SIG_IGN);
170 /* create a temporary file */
171 tmpfilenam = m_mktemp(invo_name, &fd, NULL);
172 if (tmpfilenam == NULL) {
173 adios("rcvstore", "unable to create temporary file");
175 chmod(tmpfilenam, m_gmprot());
177 /* copy the message from stdin into temp file */
178 cpydata(fileno(stdin), fd, "standard input", tmpfilenam);
180 if (fstat(fd, &st) == NOTOK) {
182 adios(tmpfilenam, "unable to fstat");
184 if (close(fd) == NOTOK)
185 adios(tmpfilenam, "error closing");
187 /* don't add file if it is empty */
188 if (st.st_size == 0) {
190 advise(NULL, "empty file");
195 ** read folder and create message structure
197 if (!(mp = folder_read(folder)))
198 adios(NULL, "unable to read folder %s", folder);
201 ** Link message into folder, and possibly add
202 ** to the Unseen-Sequence's.
204 if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL))
209 ** Add the message to any extra sequences
210 ** that have been specified.
212 for (seqp = 0; seqs[seqp]; seqp++) {
213 if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw))
217 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
218 seq_save(mp); /* synchronize and save message sequences */
219 folder_free(mp); /* free folder/message structure */
221 context_save(); /* save the global context file */
222 unlink(tmpfilenam); /* remove temporary file */
234 if (tmpfilenam && *tmpfilenam) {