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;
47 static void unlink_done(int) NORETURN;
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];
63 setlocale(LC_ALL, "");
64 invo_name = mhbasename(argv[0]);
66 /* read user profile/context */
69 arguments = getarguments(invo_name, argc, argv, 1);
73 while ((cp = *argp++)) {
75 switch (smatch(++cp, switches)) {
77 ambigsw(cp, switches);
80 adios(NULL, "-%s unknown", cp);
83 snprintf(buf, sizeof(buf),
84 "%s [+folder] [switches]",
86 print_help(buf, switches, 1);
89 print_version(invo_name);
93 if (!(cp = *argp++) || *cp == '-')
94 adios(NULL, "missing argument name to %s", argp[-2]);
96 /* check if too many sequences specified */
98 adios(NULL, "too many sequences (more than %d) specified", NUMATTRS);
131 if (*cp == '+' || *cp == '@') {
133 adios(NULL, "only one folder at a time!");
135 folder = getcpy(expandfol(cp));
137 adios(NULL, "usage: %s [+folder] [switches]",
142 seqs[seqp] = NULL; /* NULL terminate list of sequences */
144 /* if no folder is given, use default folder */
146 folder = getdeffol();
147 maildir = toabsdir(folder);
149 /* check if folder exists */
150 if (stat(maildir, &st) == NOTOK) {
152 adios(maildir, "error on folder");
154 adios(NULL, "folder %s doesn't exist", maildir);
155 if (!makedir(maildir))
156 adios(NULL, "unable to create folder %s", maildir);
159 if (chdir(maildir) == NOTOK)
160 adios(maildir, "unable to change directory to");
162 /* ignore a few signals */
163 SIGNAL(SIGHUP, SIG_IGN);
164 SIGNAL(SIGINT, SIG_IGN);
165 SIGNAL(SIGQUIT, SIG_IGN);
166 SIGNAL(SIGTERM, SIG_IGN);
168 /* create a temporary file */
169 tmpfilenam = m_mktemp(invo_name, &fd, NULL);
170 if (tmpfilenam == NULL) {
171 adios("rcvstore", "unable to create temporary file");
173 chmod(tmpfilenam, m_gmprot());
175 /* copy the message from stdin into temp file */
176 cpydata(fileno(stdin), fd, "standard input", tmpfilenam);
178 if (fstat(fd, &st) == NOTOK) {
180 adios(tmpfilenam, "unable to fstat");
182 if (close(fd) == NOTOK)
183 adios(tmpfilenam, "error closing");
185 /* don't add file if it is empty */
186 if (st.st_size == 0) {
188 advise(NULL, "empty file");
193 ** read folder and create message structure
195 if (!(mp = folder_read(folder)))
196 adios(NULL, "unable to read folder %s", folder);
199 ** Link message into folder, and possibly add
200 ** to the Unseen-Sequence's.
202 if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL))
207 ** Add the message to any extra sequences
208 ** that have been specified.
210 for (seqp = 0; seqs[seqp]; seqp++) {
211 if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw))
215 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
216 seq_save(mp); /* synchronize and save message sequences */
217 folder_free(mp); /* free folder/message structure */
219 context_save(); /* save the global context file */
220 unlink(tmpfilenam); /* remove temporary file */
231 unlink_done(int status)
233 if (tmpfilenam && *tmpfilenam)