3 * rcvstore.c -- asynchronously add mail to a folder
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
17 static struct swit switches[] = {
35 { "sequence name", 0 },
45 * name of temporary file to store incoming message
47 static char *tmpfilenam = NULL;
49 static void unlink_done(int) NORETURN;
52 main (int argc, char **argv)
54 int publicsw = -1, zerosw = 0;
55 int create = 1, unseensw = 1;
56 int fd, msgnum, seqp = 0;
57 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
58 char **argp, **arguments, *seqs[NUMATTRS+1];
65 setlocale(LC_ALL, "");
67 invo_name = r1bindex (argv[0], '/');
69 /* read user profile/context */
73 arguments = getarguments (invo_name, argc, argv, 1);
77 while ((cp = *argp++)) {
79 switch (smatch (++cp, switches)) {
81 ambigsw (cp, switches);
84 adios (NULL, "-%s unknown", cp);
87 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
89 print_help (buf, switches, 1);
92 print_version(invo_name);
96 if (!(cp = *argp++) || *cp == '-')
97 adios (NULL, "missing argument name to %s", argp[-2]);
99 /* check if too many sequences specified */
100 if (seqp >= NUMATTRS)
101 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
134 if (*cp == '+' || *cp == '@') {
136 adios (NULL, "only one folder at a time!");
138 folder = pluspath (cp);
140 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
144 seqs[seqp] = NULL; /* NULL terminate list of sequences */
146 if (!context_find ("path"))
147 free (path ("./", TFOLDER));
149 /* if no folder is given, use default folder */
151 folder = getfolder (0);
152 maildir = m_maildir (folder);
154 /* check if folder exists */
155 if (stat (maildir, &st) == NOTOK) {
157 adios (maildir, "error on folder");
159 adios (NULL, "folder %s doesn't exist", maildir);
160 if (!makedir (maildir))
161 adios (NULL, "unable to create folder %s", maildir);
164 if (chdir (maildir) == NOTOK)
165 adios (maildir, "unable to change directory to");
167 /* ignore a few signals */
168 SIGNAL (SIGHUP, SIG_IGN);
169 SIGNAL (SIGINT, SIG_IGN);
170 SIGNAL (SIGQUIT, SIG_IGN);
171 SIGNAL (SIGTERM, SIG_IGN);
173 /* create a temporary file */
174 tmpfilenam = m_mktemp (invo_name, &fd, NULL);
175 if (tmpfilenam == NULL) {
176 adios ("rcvstore", "unable to create temporary file");
178 chmod (tmpfilenam, m_gmprot());
180 /* copy the message from stdin into temp file */
181 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
183 if (fstat (fd, &st) == NOTOK) {
185 adios (tmpfilenam, "unable to fstat");
187 if (close (fd) == NOTOK)
188 adios (tmpfilenam, "error closing");
190 /* don't add file if it is empty */
191 if (st.st_size == 0) {
193 advise (NULL, "empty file");
198 * read folder and create message structure
200 if (!(mp = folder_read (folder)))
201 adios (NULL, "unable to read folder %s", folder);
204 * Link message into folder, and possibly add
205 * to the Unseen-Sequence's.
207 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, (char *)0)) == -1)
211 * Add the message to any extra sequences
212 * that have been specified.
214 for (seqp = 0; seqs[seqp]; seqp++) {
215 if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw))
219 seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */
220 seq_save (mp); /* synchronize and save message sequences */
221 folder_free (mp); /* free folder/message structure */
223 context_save (); /* save the global context file */
224 unlink (tmpfilenam); /* remove temporary file */
235 unlink_done(int status)
237 if (tmpfilenam && *tmpfilenam)