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;
58 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
59 char **argp, **arguments, *seqs[NUMATTRS+1];
66 setlocale(LC_ALL, "");
68 invo_name = r1bindex (argv[0], '/');
70 /* read user profile/context */
74 arguments = getarguments (invo_name, argc, argv, 1);
78 while ((cp = *argp++)) {
80 switch (smatch (++cp, switches)) {
82 ambigsw (cp, switches);
85 adios (NULL, "-%s unknown", cp);
88 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
90 print_help (buf, switches, 1);
93 print_version(invo_name);
97 if (!(cp = *argp++) || *cp == '-')
98 adios (NULL, "missing argument name to %s", argp[-2]);
100 /* check if too many sequences specified */
101 if (seqp >= NUMATTRS)
102 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
135 if (*cp == '+' || *cp == '@') {
137 adios (NULL, "only one folder at a time!");
139 folder = pluspath (cp);
141 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
145 seqs[seqp] = NULL; /* NULL terminate list of sequences */
147 if (!context_find ("path"))
148 free (path ("./", TFOLDER));
150 /* if no folder is given, use default folder */
152 folder = getfolder (0);
153 maildir = m_maildir (folder);
155 /* check if folder exists */
156 if (stat (maildir, &st) == NOTOK) {
158 adios (maildir, "error on folder");
160 adios (NULL, "folder %s doesn't exist", maildir);
161 if (!makedir (maildir))
162 adios (NULL, "unable to create folder %s", maildir);
165 if (chdir (maildir) == NOTOK)
166 adios (maildir, "unable to change directory to");
168 /* ignore a few signals */
169 SIGNAL (SIGHUP, SIG_IGN);
170 SIGNAL (SIGINT, SIG_IGN);
171 SIGNAL (SIGQUIT, SIG_IGN);
172 SIGNAL (SIGTERM, SIG_IGN);
174 /* create a temporary file */
175 tmpfilenam = m_mktemp (invo_name, &fd, NULL);
176 if (tmpfilenam == NULL) {
177 adios ("rcvstore", "unable to create temporary file");
179 chmod (tmpfilenam, m_gmprot());
181 /* copy the message from stdin into temp file */
182 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
184 if (fstat (fd, &st) == NOTOK) {
186 adios (tmpfilenam, "unable to fstat");
188 if (close (fd) == NOTOK)
189 adios (tmpfilenam, "error closing");
191 /* don't add file if it is empty */
192 if (st.st_size == 0) {
194 advise (NULL, "empty file");
199 * read folder and create message structure
201 if (!(mp = folder_read (folder)))
202 adios (NULL, "unable to read folder %s", folder);
205 * Link message into folder, and possibly add
206 * to the Unseen-Sequence's.
208 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, (char *)0)) == -1)
212 * Add the message to any extra sequences
213 * that have been specified.
215 for (seqp = 0; seqs[seqp]; seqp++) {
216 if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw))
220 seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */
221 seq_save (mp); /* synchronize and save message sequences */
222 folder_free (mp); /* free folder/message structure */
224 context_save (); /* save the global context file */
225 unlink (tmpfilenam); /* remove temporary file */
236 unlink_done(int status)
238 if (tmpfilenam && *tmpfilenam)