3 * rcvstore.c -- asynchronously add mail to a folder
10 #include <h/signals.h>
13 #include <zotnet/mts/mts.h>
15 static struct swit switches[] = {
33 { "sequence name", 0 },
44 * name of temporary file to store incoming message
46 static char *tmpfilenam = NULL;
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];
61 setlocale(LC_ALL, "");
63 invo_name = r1bindex (argv[0], '/');
65 /* read user profile/context */
69 arguments = getarguments (invo_name, argc, argv, 1);
73 while ((cp = *argp++)) {
75 switch (smatch (++cp, switches)) {
77 ambigsw (cp, switches);
80 adios (NULL, "-%s unknown", cp);
83 snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
85 print_help (buf, switches, 1);
88 print_version(invo_name);
92 if (!(cp = *argp++) || *cp == '-')
93 adios (NULL, "missing argument name to %s", argp[-2]);
95 /* check if too many sequences specified */
97 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
130 if (*cp == '+' || *cp == '@') {
132 adios (NULL, "only one folder at a time!");
134 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
136 adios (NULL, "usage: %s [+folder] [switches]", invo_name);
140 seqs[seqp] = NULL; /* NULL terminate list of sequences */
142 if (!context_find ("path"))
143 free (path ("./", TFOLDER));
145 /* if no folder is given, use default folder */
147 folder = getfolder (0);
148 maildir = m_maildir (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_scratch ("", invo_name);
171 if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
172 adios (tmpfilenam, "unable to create");
173 chmod (tmpfilenam, m_gmprot());
175 /* copy the message from stdin into temp file */
176 cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
178 if (fstat (fd, &st) == NOTOK) {
180 adios (tmpfilenam, "unable to fstat");
182 if (close (fd) == NOTOK)
183 adios (tmpfilenam, "error closing");
185 /* don't add file if it is empty */
186 if (st.st_size == 0) {
188 advise (NULL, "empty file");
193 * read folder and create message structure
195 if (!(mp = folder_read (folder)))
196 adios (NULL, "unable to read folder %s", folder);
199 * Link message into folder, and possibly add
200 * to the Unseen-Sequence's.
202 if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0)) == -1)
206 * Add the message to any extra sequences
207 * that have been specified.
209 for (seqp = 0; seqs[seqp]; seqp++) {
210 if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw))
214 seq_setunseen (mp, 0); /* synchronize any Unseen-Sequence's */
215 seq_save (mp); /* synchronize and save message sequences */
216 folder_free (mp); /* free folder/message structure */
218 context_save (); /* save the global context file */
219 unlink (tmpfilenam); /* remove temporary file */
231 if (tmpfilenam && *tmpfilenam)
234 return 1; /* dead code to satisfy the compiler */