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 },
48 * name of temporary file to store incoming message
50 static char *tmpfilenam = NULL;
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];
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 = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
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_scratch ("", invo_name);
175 if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
176 adios (tmpfilenam, "unable to create");
177 chmod (tmpfilenam, m_gmprot());
179 /* copy the message from stdin into temp file */
180 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
182 if (fstat (fd, &st) == NOTOK) {
184 adios (tmpfilenam, "unable to fstat");
186 if (close (fd) == NOTOK)
187 adios (tmpfilenam, "error closing");
189 /* don't add file if it is empty */
190 if (st.st_size == 0) {
192 advise (NULL, "empty file");
197 * read folder and create message structure
199 if (!(mp = folder_read (folder)))
200 adios (NULL, "unable to read folder %s", folder);
203 * Link message into folder, and possibly add
204 * to the Unseen-Sequence's.
206 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0)) == -1)
210 * Add the message to any extra sequences
211 * that have been specified.
213 for (seqp = 0; seqs[seqp]; seqp++) {
214 if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw))
218 seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */
219 seq_save (mp); /* synchronize and save message sequences */
220 folder_free (mp); /* free folder/message structure */
222 context_save (); /* save the global context file */
223 unlink (tmpfilenam); /* remove temporary file */
235 if (tmpfilenam && *tmpfilenam)
238 return 1; /* dead code to satisfy the compiler */