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;
53 main (int argc, char **argv)
55 int publicsw = -1, zerosw = 0;
56 int create = 1, unseensw = 1;
57 int fd, msgnum, seqp = 0;
58 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
59 char **argp, **arguments, *seqs[NUMATTRS+1];
64 setlocale(LC_ALL, "");
66 invo_name = r1bindex (argv[0], '/');
68 /* read user profile/context */
72 arguments = getarguments (invo_name, argc, argv, 1);
76 while ((cp = *argp++)) {
78 switch (smatch (++cp, switches)) {
80 ambigsw (cp, switches);
83 adios (NULL, "-%s unknown", cp);
86 snprintf (buf, sizeof(buf), "%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 = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
139 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
143 seqs[seqp] = NULL; /* NULL terminate list of sequences */
145 if (!context_find ("path"))
146 free (path ("./", TFOLDER));
148 /* if no folder is given, use default folder */
150 folder = getfolder (0);
151 maildir = m_maildir (folder);
153 /* check if folder exists */
154 if (stat (maildir, &st) == NOTOK) {
156 adios (maildir, "error on folder");
158 adios (NULL, "folder %s doesn't exist", maildir);
159 if (!makedir (maildir))
160 adios (NULL, "unable to create folder %s", maildir);
163 if (chdir (maildir) == NOTOK)
164 adios (maildir, "unable to change directory to");
166 /* ignore a few signals */
167 SIGNAL (SIGHUP, SIG_IGN);
168 SIGNAL (SIGINT, SIG_IGN);
169 SIGNAL (SIGQUIT, SIG_IGN);
170 SIGNAL (SIGTERM, SIG_IGN);
172 /* create a temporary file */
173 tmpfilenam = m_scratch ("", invo_name);
174 if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
175 adios (tmpfilenam, "unable to create");
176 chmod (tmpfilenam, m_gmprot());
178 /* copy the message from stdin into temp file */
179 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
181 if (fstat (fd, &st) == NOTOK) {
183 adios (tmpfilenam, "unable to fstat");
185 if (close (fd) == NOTOK)
186 adios (tmpfilenam, "error closing");
188 /* don't add file if it is empty */
189 if (st.st_size == 0) {
191 advise (NULL, "empty file");
196 * read folder and create message structure
198 if (!(mp = folder_read (folder)))
199 adios (NULL, "unable to read folder %s", folder);
202 * Link message into folder, and possibly add
203 * to the Unseen-Sequence's.
205 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, (char *)0)) == -1)
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, 0); /* synchronize any Unseen-Sequence's */
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)
237 return 1; /* dead code to satisfy the compiler */