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>
15 static struct swit switches[] = {
33 { "sequence name", 0 },
43 ** name of temporary file to store incoming message
45 static char *tmpfilenam = NULL;
47 static void unlink_done(int) NORETURN;
50 main(int argc, char **argv)
52 int publicsw = -1, zerosw = 0;
53 int create = 1, unseensw = 1;
54 int fd, msgnum, seqp = 0;
55 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
56 char **argp, **arguments, *seqs[NUMATTRS+1];
63 setlocale(LC_ALL, "");
65 invo_name = mhbasename(argv[0]);
67 /* read user profile/context */
70 arguments = getarguments(invo_name, argc, argv, 1);
74 while ((cp = *argp++)) {
76 switch (smatch(++cp, switches)) {
78 ambigsw(cp, switches);
81 adios(NULL, "-%s unknown", cp);
84 snprintf(buf, sizeof(buf),
85 "%s [+folder] [switches]",
87 print_help(buf, switches, 1);
90 print_version(invo_name);
94 if (!(cp = *argp++) || *cp == '-')
95 adios(NULL, "missing argument name to %s", argp[-2]);
97 /* check if too many sequences specified */
99 adios(NULL, "too many sequences (more than %d) specified", NUMATTRS);
132 if (*cp == '+' || *cp == '@') {
134 adios(NULL, "only one folder at a time!");
136 folder = getcpy(expandfol(cp));
138 adios(NULL, "usage: %s [+folder] [switches]",
143 seqs[seqp] = NULL; /* NULL terminate list of sequences */
145 /* if no folder is given, use default folder */
147 folder = getdeffol();
148 maildir = toabsdir(folder);
150 /* check if folder exists */
151 if (stat(maildir, &st) == NOTOK) {
153 adios(maildir, "error on folder");
155 adios(NULL, "folder %s doesn't exist", maildir);
156 if (!makedir(maildir))
157 adios(NULL, "unable to create folder %s", maildir);
160 if (chdir(maildir) == NOTOK)
161 adios(maildir, "unable to change directory to");
163 /* ignore a few signals */
164 SIGNAL(SIGHUP, SIG_IGN);
165 SIGNAL(SIGINT, SIG_IGN);
166 SIGNAL(SIGQUIT, SIG_IGN);
167 SIGNAL(SIGTERM, SIG_IGN);
169 /* create a temporary file */
170 tmpfilenam = m_mktemp(invo_name, &fd, NULL);
171 if (tmpfilenam == NULL) {
172 adios("rcvstore", "unable to create temporary file");
174 chmod(tmpfilenam, m_gmprot());
176 /* copy the message from stdin into temp file */
177 cpydata(fileno(stdin), fd, "standard input", tmpfilenam);
179 if (fstat(fd, &st) == NOTOK) {
181 adios(tmpfilenam, "unable to fstat");
183 if (close(fd) == NOTOK)
184 adios(tmpfilenam, "error closing");
186 /* don't add file if it is empty */
187 if (st.st_size == 0) {
189 advise(NULL, "empty file");
194 ** read folder and create message structure
196 if (!(mp = folder_read(folder)))
197 adios(NULL, "unable to read folder %s", folder);
200 ** Link message into folder, and possibly add
201 ** to the Unseen-Sequence's.
203 if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL))
208 ** Add the message to any extra sequences
209 ** that have been specified.
211 for (seqp = 0; seqs[seqp]; seqp++) {
212 if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw))
216 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
217 seq_save(mp); /* synchronize and save message sequences */
218 folder_free(mp); /* free folder/message structure */
220 context_save(); /* save the global context file */
221 unlink(tmpfilenam); /* remove temporary file */
232 unlink_done(int status)
234 if (tmpfilenam && *tmpfilenam)