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>
16 static struct swit switches[] = {
34 { "sequence name", 0 },
44 ** name of temporary file to store incoming message
46 static char *tmpfilenam = NULL;
48 static void unlink_done(int) NORETURN;
51 main(int argc, char **argv)
53 int publicsw = -1, zerosw = 0;
54 int create = 1, unseensw = 1;
55 int fd, msgnum, seqp = 0;
56 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
57 char **argp, **arguments, *seqs[NUMATTRS+1];
64 setlocale(LC_ALL, "");
66 invo_name = mhbasename(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),
87 "%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 = getcpy(expandfol(cp));
140 adios(NULL, "usage: %s [+folder] [switches]",
145 seqs[seqp] = NULL; /* NULL terminate list of sequences */
147 /* if no folder is given, use default folder */
149 folder = getdeffol();
150 maildir = toabsdir(folder);
152 /* check if folder exists */
153 if (stat(maildir, &st) == NOTOK) {
155 adios(maildir, "error on folder");
157 adios(NULL, "folder %s doesn't exist", maildir);
158 if (!makedir(maildir))
159 adios(NULL, "unable to create folder %s", maildir);
162 if (chdir(maildir) == NOTOK)
163 adios(maildir, "unable to change directory to");
165 /* ignore a few signals */
166 SIGNAL(SIGHUP, SIG_IGN);
167 SIGNAL(SIGINT, SIG_IGN);
168 SIGNAL(SIGQUIT, SIG_IGN);
169 SIGNAL(SIGTERM, SIG_IGN);
171 /* create a temporary file */
172 tmpfilenam = m_mktemp(invo_name, &fd, NULL);
173 if (tmpfilenam == NULL) {
174 adios("rcvstore", "unable to create temporary file");
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, NULL))
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, 1); /* add new msgs to unseen sequences */
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 */
234 unlink_done(int status)
236 if (tmpfilenam && *tmpfilenam)