3 * rcvstore.c -- asynchronously add mail to a folder
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
19 static struct swit switches[] = {
37 { "sequence name", 0 },
47 * name of temporary file to store incoming message
49 static char *tmpfilenam = NULL;
51 static void unlink_done(int) NORETURN;
54 main (int argc, char **argv)
56 int publicsw = -1, zerosw = 0;
57 int create = 1, unseensw = 1;
58 int fd, msgnum, seqp = 0;
59 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
60 char **argp, **arguments, *seqs[NUMATTRS+1];
67 setlocale(LC_ALL, "");
69 invo_name = r1bindex (argv[0], '/');
71 /* read user profile/context */
75 arguments = getarguments (invo_name, argc, argv, 1);
79 while ((cp = *argp++)) {
81 switch (smatch (++cp, switches)) {
83 ambigsw (cp, switches);
86 adios (NULL, "-%s unknown", cp);
89 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
91 print_help (buf, switches, 1);
94 print_version(invo_name);
98 if (!(cp = *argp++) || *cp == '-')
99 adios (NULL, "missing argument name to %s", argp[-2]);
101 /* check if too many sequences specified */
102 if (seqp >= NUMATTRS)
103 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
136 if (*cp == '+' || *cp == '@') {
138 adios (NULL, "only one folder at a time!");
140 folder = pluspath (cp);
142 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
146 seqs[seqp] = NULL; /* NULL terminate list of sequences */
148 if (!context_find ("path"))
149 free (path ("./", TFOLDER));
151 /* if no folder is given, use default folder */
153 folder = getfolder (0);
154 maildir = m_maildir (folder);
156 /* check if folder exists */
157 if (stat (maildir, &st) == NOTOK) {
159 adios (maildir, "error on folder");
161 adios (NULL, "folder %s doesn't exist", maildir);
162 if (!makedir (maildir))
163 adios (NULL, "unable to create folder %s", maildir);
166 if (chdir (maildir) == NOTOK)
167 adios (maildir, "unable to change directory to");
169 /* ignore a few signals */
170 SIGNAL (SIGHUP, SIG_IGN);
171 SIGNAL (SIGINT, SIG_IGN);
172 SIGNAL (SIGQUIT, SIG_IGN);
173 SIGNAL (SIGTERM, SIG_IGN);
175 /* create a temporary file */
176 tmpfilenam = m_mktemp (invo_name, &fd, NULL);
177 if (tmpfilenam == NULL) {
178 adios ("rcvstore", "unable to create temporary file");
180 chmod (tmpfilenam, m_gmprot());
182 /* copy the message from stdin into temp file */
183 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
185 if (fstat (fd, &st) == NOTOK) {
187 adios (tmpfilenam, "unable to fstat");
189 if (close (fd) == NOTOK)
190 adios (tmpfilenam, "error closing");
192 /* don't add file if it is empty */
193 if (st.st_size == 0) {
195 advise (NULL, "empty file");
200 * read folder and create message structure
202 if (!(mp = folder_read (folder)))
203 adios (NULL, "unable to read folder %s", folder);
206 * Link message into folder, and possibly add
207 * to the Unseen-Sequence's.
209 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, (char *)0)) == -1)
213 * Add the message to any extra sequences
214 * that have been specified.
216 for (seqp = 0; seqs[seqp]; seqp++) {
217 if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw))
221 seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */
222 seq_save (mp); /* synchronize and save message sequences */
223 folder_free (mp); /* free folder/message structure */
225 context_save (); /* save the global context file */
226 unlink (tmpfilenam); /* remove temporary file */
237 unlink_done(int status)
239 if (tmpfilenam && *tmpfilenam)