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.
11 #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;
54 main(int argc, char **argv)
56 int publicsw = -1, zerosw = 0;
57 int create = 1, unseensw = 1;
60 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
61 char **argp, **arguments, *seqs[NUMATTRS+1];
65 if (atexit(unlink_done) != 0) {
66 adios(EX_OSERR, NULL, "atexit failed");
69 setlocale(LC_ALL, "");
70 invo_name = mhbasename(argv[0]);
72 /* read user profile/context */
75 arguments = getarguments(invo_name, argc, argv, 1);
79 while ((cp = *argp++)) {
81 switch (smatch(++cp, switches)) {
83 ambigsw(cp, switches);
86 adios(EX_USAGE, NULL, "-%s unknown", cp);
89 snprintf(buf, sizeof(buf),
90 "%s [+folder] [switches]",
92 print_help(buf, switches, 1);
93 exit(argc == 2 ? EX_OK : EX_USAGE);
95 print_version(invo_name);
96 exit(argc == 2 ? EX_OK : EX_USAGE);
99 if (!(cp = *argp++) || *cp == '-')
100 adios(EX_USAGE, NULL, "missing argument name to %s", argp[-2]);
102 /* check if too many sequences specified */
103 if (seqp >= NUMATTRS)
104 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
137 if (*cp == '+' || *cp == '@') {
139 adios(EX_USAGE, NULL, "only one folder at a time!");
141 folder = getcpy(expandfol(cp));
143 adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
148 seqs[seqp] = NULL; /* NULL terminate list of sequences */
150 /* if no folder is given, use default folder */
152 folder = getdeffol();
153 maildir = toabsdir(folder);
155 /* check if folder exists */
156 if (stat(maildir, &st) == NOTOK) {
158 adios(EX_IOERR, maildir, "error on folder");
160 adios(EX_USAGE, NULL, "folder %s doesn't exist", maildir);
161 if (!makedir(maildir))
162 adios(EX_CANTCREAT, NULL, "unable to create folder %s", maildir);
165 if (chdir(maildir) == NOTOK)
166 adios(EX_OSERR, 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(EX_CANTCREAT, "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(EX_IOERR, tmpfilenam, "unable to fstat");
188 if (close(fd) == NOTOK)
189 adios(EX_IOERR, 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(EX_IOERR, 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, NULL))
213 ** Add the message to any extra sequences
214 ** that have been specified.
216 for (seqp = 0; seqs[seqp]; seqp++) {
217 if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw))
221 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
222 seq_save(mp); /* synchronize and save message sequences */
223 folder_free(mp); /* free folder/message structure */
225 context_save(); /* save the global context file */
226 unlink(tmpfilenam); /* remove temporary file */
238 if (tmpfilenam && *tmpfilenam) {