2 ** rcvstore.c -- asynchronously add mail to a folder
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
12 #include <h/signals.h>
20 static struct swit switches[] = {
38 { "sequence name", 0 },
48 ** name of temporary file to store incoming message
50 static char *tmpfilenam = NULL;
55 main(int argc, char **argv)
57 int publicsw = -1, zerosw = 0;
58 int create = 1, unseensw = 1;
61 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
62 char **argp, **arguments, *seqs[NUMATTRS+1];
66 if (atexit(unlink_done) != 0) {
67 adios(EX_OSERR, NULL, "atexit failed");
70 setlocale(LC_ALL, "");
71 invo_name = mhbasename(argv[0]);
73 /* read user profile/context */
76 arguments = getarguments(invo_name, argc, argv, 1);
80 while ((cp = *argp++)) {
82 switch (smatch(++cp, switches)) {
84 ambigsw(cp, switches);
87 adios(EX_USAGE, NULL, "-%s unknown", cp);
90 snprintf(buf, sizeof(buf),
91 "%s [+folder] [switches]",
93 print_help(buf, switches, 1);
94 exit(argc == 2 ? EX_OK : EX_USAGE);
96 print_version(invo_name);
97 exit(argc == 2 ? EX_OK : EX_USAGE);
100 if (!(cp = *argp++) || *cp == '-')
101 adios(EX_USAGE, NULL, "missing argument name to %s", argp[-2]);
103 /* check if too many sequences specified */
104 if (seqp >= NUMATTRS)
105 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
138 if (*cp == '+' || *cp == '@') {
140 adios(EX_USAGE, NULL, "only one folder at a time!");
142 folder = mh_xstrdup(expandfol(cp));
144 adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
149 seqs[seqp] = NULL; /* NULL terminate list of sequences */
151 /* if no folder is given, use default folder */
153 folder = getdeffol();
154 maildir = toabsdir(folder);
156 /* check if folder exists */
157 if (stat(maildir, &st) == NOTOK) {
159 adios(EX_IOERR, maildir, "error on folder");
161 adios(EX_USAGE, NULL, "folder %s doesn't exist", maildir);
162 if (!makedir(maildir))
163 adios(EX_CANTCREAT, NULL, "unable to create folder %s", maildir);
166 if (chdir(maildir) == NOTOK)
167 adios(EX_OSERR, 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(EX_CANTCREAT, "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(EX_IOERR, tmpfilenam, "unable to fstat");
189 if (close(fd) == NOTOK)
190 adios(EX_IOERR, 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(EX_IOERR, 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, NULL))
214 ** Add the message to any extra sequences
215 ** that have been specified.
217 for (seqp = 0; seqs[seqp]; seqp++) {
218 if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw))
222 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
223 seq_save(mp); /* synchronize and save message sequences */
224 folder_free(mp); /* free folder/message structure */
226 context_save(); /* save the global context file */
227 unlink(tmpfilenam); /* remove temporary file */
239 if (tmpfilenam && *tmpfilenam) {