3 * rcvstore.c -- asynchronously add mail to a folder
10 #include <h/signals.h>
14 static struct swit switches[] = {
32 { "sequence name", 0 },
43 * name of temporary file to store incoming message
45 static char *tmpfilenam = NULL;
49 main (int argc, char **argv)
51 int publicsw = -1, zerosw = 0;
52 int create = 1, unseensw = 1;
53 int fd, msgnum, seqp = 0;
54 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
55 char **argp, **arguments, *seqs[NUMATTRS+1];
60 setlocale(LC_ALL, "");
62 invo_name = r1bindex (argv[0], '/');
64 /* read user profile/context */
68 arguments = getarguments (invo_name, argc, argv, 1);
72 while ((cp = *argp++)) {
74 switch (smatch (++cp, switches)) {
76 ambigsw (cp, switches);
79 adios (NULL, "-%s unknown", cp);
82 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
84 print_help (buf, switches, 1);
87 print_version(invo_name);
91 if (!(cp = *argp++) || *cp == '-')
92 adios (NULL, "missing argument name to %s", argp[-2]);
94 /* check if too many sequences specified */
96 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
129 if (*cp == '+' || *cp == '@') {
131 adios (NULL, "only one folder at a time!");
133 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
135 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
139 seqs[seqp] = NULL; /* NULL terminate list of sequences */
141 if (!context_find ("path"))
142 free (path ("./", TFOLDER));
144 /* if no folder is given, use default folder */
146 folder = getfolder (0);
147 maildir = m_maildir (folder);
149 /* check if folder exists */
150 if (stat (maildir, &st) == NOTOK) {
152 adios (maildir, "error on folder");
154 adios (NULL, "folder %s doesn't exist", maildir);
155 if (!makedir (maildir))
156 adios (NULL, "unable to create folder %s", maildir);
159 if (chdir (maildir) == NOTOK)
160 adios (maildir, "unable to change directory to");
162 /* ignore a few signals */
163 SIGNAL (SIGHUP, SIG_IGN);
164 SIGNAL (SIGINT, SIG_IGN);
165 SIGNAL (SIGQUIT, SIG_IGN);
166 SIGNAL (SIGTERM, SIG_IGN);
168 /* create a temporary file */
169 tmpfilenam = m_scratch ("", invo_name);
170 if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
171 adios (tmpfilenam, "unable to create");
172 chmod (tmpfilenam, m_gmprot());
174 /* copy the message from stdin into temp file */
175 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
177 if (fstat (fd, &st) == NOTOK) {
179 adios (tmpfilenam, "unable to fstat");
181 if (close (fd) == NOTOK)
182 adios (tmpfilenam, "error closing");
184 /* don't add file if it is empty */
185 if (st.st_size == 0) {
187 advise (NULL, "empty file");
192 * read folder and create message structure
194 if (!(mp = folder_read (folder)))
195 adios (NULL, "unable to read folder %s", folder);
198 * Link message into folder, and possibly add
199 * to the Unseen-Sequence's.
201 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0)) == -1)
205 * Add the message to any extra sequences
206 * that have been specified.
208 for (seqp = 0; seqs[seqp]; seqp++) {
209 if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw))
213 seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */
214 seq_save (mp); /* synchronize and save message sequences */
215 folder_free (mp); /* free folder/message structure */
217 context_save (); /* save the global context file */
218 unlink (tmpfilenam); /* remove temporary file */
230 if (tmpfilenam && *tmpfilenam)