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>
18 static struct swit switches[] = {
36 { "sequence name", 0 },
46 ** name of temporary file to store incoming message
48 static char *tmpfilenam = NULL;
53 main(int argc, char **argv)
55 int publicsw = -1, zerosw = 0;
56 int create = 1, unseensw = 1;
59 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
60 char **argp, **arguments, *seqs[NUMATTRS+1];
64 if (atexit(unlink_done) != 0) {
65 adios(NULL, "atexit failed");
68 setlocale(LC_ALL, "");
69 invo_name = mhbasename(argv[0]);
71 /* read user profile/context */
74 arguments = getarguments(invo_name, argc, argv, 1);
78 while ((cp = *argp++)) {
80 switch (smatch(++cp, switches)) {
82 ambigsw(cp, switches);
85 adios(NULL, "-%s unknown", cp);
88 snprintf(buf, sizeof(buf),
89 "%s [+folder] [switches]",
91 print_help(buf, switches, 1);
94 print_version(invo_name);
98 if (!(cp = *argp++) || *cp == '-')
99 adios(NULL, "missing argument name to %s", argp[-2]);
101 /* check if too many sequences specified */
102 if (seqp >= NUMATTRS)
103 adios(NULL, "too many sequences (more than %d) specified", NUMATTRS);
136 if (*cp == '+' || *cp == '@') {
138 adios(NULL, "only one folder at a time!");
140 folder = getcpy(expandfol(cp));
142 adios(NULL, "usage: %s [+folder] [switches]",
147 seqs[seqp] = NULL; /* NULL terminate list of sequences */
149 /* if no folder is given, use default folder */
151 folder = getdeffol();
152 maildir = toabsdir(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_mktemp(invo_name, &fd, NULL);
175 if (tmpfilenam == NULL) {
176 adios("rcvstore", "unable to create temporary file");
178 chmod(tmpfilenam, m_gmprot());
180 /* copy the message from stdin into temp file */
181 cpydata(fileno(stdin), fd, "standard input", tmpfilenam);
183 if (fstat(fd, &st) == NOTOK) {
185 adios(tmpfilenam, "unable to fstat");
187 if (close(fd) == NOTOK)
188 adios(tmpfilenam, "error closing");
190 /* don't add file if it is empty */
191 if (st.st_size == 0) {
193 advise(NULL, "empty file");
198 ** read folder and create message structure
200 if (!(mp = folder_read(folder)))
201 adios(NULL, "unable to read folder %s", folder);
204 ** Link message into folder, and possibly add
205 ** to the Unseen-Sequence's.
207 if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL))
212 ** Add the message to any extra sequences
213 ** that have been specified.
215 for (seqp = 0; seqs[seqp]; seqp++) {
216 if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw))
220 seq_setunseen(mp, 1); /* add new msgs to unseen sequences */
221 seq_save(mp); /* synchronize and save message sequences */
222 folder_free(mp); /* free folder/message structure */
224 context_save(); /* save the global context file */
225 unlink(tmpfilenam); /* remove temporary file */
237 if (tmpfilenam && *tmpfilenam) {