simple mbox support for rcvstore
[mmh] / uip / rcvstore.c
1 /*
2 ** rcvstore.c -- asynchronously add mail to a folder
3 **
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.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/signals.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <unistd.h>
15 #include <sys/stat.h>
16 #include <locale.h>
17 #include <sysexits.h>
18
19 static struct swit switches[] = {
20 #define CRETSW  0
21         { "create",   0 },
22 #define NCRETSW  1
23         { "nocreate", 2 },
24 #define UNSEENSW  2
25         { "unseen",   0 },
26 #define NUNSEENSW  3
27         { "nounseen", 2 },
28 #define PUBSW  4
29         { "public",   0 },
30 #define NPUBSW  5
31         { "nopublic", 2 },
32 #define ZEROSW  6
33         { "zero",     0 },
34 #define NZEROSW  7
35         { "nozero",   2 },
36 #define SEQSW  8
37         { "sequence name", 0 },
38 #define VERSIONSW  9
39         { "Version", 0 },
40 #define HELPSW  10
41         { "help", 0 },
42         { NULL, 0 }
43 };
44
45
46 /*
47 ** name of temporary file to store incoming message
48 */
49 static char *tmpfilenam = NULL;
50
51 void unlink_done();
52 static void fix_mbox(int out, char *ofile);
53
54 int
55 main(int argc, char **argv)
56 {
57         int publicsw = -1, zerosw = 0;
58         int create = 1, unseensw = 1;
59         int fd, msgnum;
60         size_t seqp = 0;
61         char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
62         char **argp, **arguments, *seqs[NUMATTRS+1];
63         struct msgs *mp;
64         struct stat st;
65
66         if (atexit(unlink_done) != 0) {
67                 adios(EX_OSERR, NULL, "atexit failed");
68         }
69
70         setlocale(LC_ALL, "");
71         invo_name = mhbasename(argv[0]);
72
73         /* read user profile/context */
74         context_read();
75
76         arguments = getarguments(invo_name, argc, argv, 1);
77         argp = arguments;
78
79         /* parse arguments */
80         while ((cp = *argp++)) {
81                 if (*cp == '-') {
82                         switch (smatch(++cp, switches)) {
83                         case AMBIGSW:
84                                 ambigsw(cp, switches);
85                                 exit(EX_USAGE);
86                         case UNKWNSW:
87                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
88
89                         case HELPSW:
90                                 snprintf(buf, sizeof(buf),
91                                                 "%s [+folder] [switches]",
92                                                 invo_name);
93                                 print_help(buf, switches, 1);
94                                 exit(argc == 2 ? EX_OK : EX_USAGE);
95                         case VERSIONSW:
96                                 print_version(invo_name);
97                                 exit(argc == 2 ? EX_OK : EX_USAGE);
98
99                         case SEQSW:
100                                 if (!(cp = *argp++) || *cp == '-')
101                                         adios(EX_USAGE, NULL, "missing argument name to %s", argp[-2]);
102
103                                 /* check if too many sequences specified */
104                                 if (seqp >= NUMATTRS)
105                                         adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
106                                 seqs[seqp++] = cp;
107                                 continue;
108
109                         case UNSEENSW:
110                                 unseensw = 1;
111                                 continue;
112                         case NUNSEENSW:
113                                 unseensw = 0;
114                                 continue;
115
116                         case PUBSW:
117                                 publicsw = 1;
118                                 continue;
119                         case NPUBSW:
120                                 publicsw = 0;
121                                 continue;
122
123                         case ZEROSW:
124                                 zerosw++;
125                                 continue;
126                         case NZEROSW:
127                                 zerosw = 0;
128                                 continue;
129
130                         case CRETSW:
131                                 create++;
132                                 continue;
133                         case NCRETSW:
134                                 create = 0;
135                                 continue;
136                         }
137                 }
138                 if (*cp == '+' || *cp == '@') {
139                         if (folder)
140                                 adios(EX_USAGE, NULL, "only one folder at a time!");
141                         else
142                                 folder = getcpy(expandfol(cp));
143                 } else {
144                         adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
145                                         invo_name);
146                 }
147         }
148
149         seqs[seqp] = NULL;  /* NULL terminate list of sequences */
150
151         /* if no folder is given, use default folder */
152         if (!folder)
153                 folder = getdeffol();
154         maildir = toabsdir(folder);
155
156         /* check if folder exists */
157         if (stat(maildir, &st) == NOTOK) {
158                 if (errno != ENOENT)
159                         adios(EX_IOERR, maildir, "error on folder");
160                 if (!create)
161                         adios(EX_USAGE, NULL, "folder %s doesn't exist", maildir);
162                 if (!makedir(maildir))
163                         adios(EX_CANTCREAT, NULL, "unable to create folder %s", maildir);
164         }
165
166         if (chdir(maildir) == NOTOK)
167                 adios(EX_OSERR, maildir, "unable to change directory to");
168
169         /* ignore a few signals */
170         SIGNAL(SIGHUP, SIG_IGN);
171         SIGNAL(SIGINT, SIG_IGN);
172         SIGNAL(SIGQUIT, SIG_IGN);
173         SIGNAL(SIGTERM, SIG_IGN);
174
175         /* create a temporary file */
176         tmpfilenam = m_mktemp(invo_name, &fd, NULL);
177         if (tmpfilenam == NULL) {
178                 adios(EX_CANTCREAT, "rcvstore", "unable to create temporary file");
179         }
180         chmod(tmpfilenam, m_gmprot());
181
182         /* check if incoming mail is in mbox-format */
183         fix_mbox(fd, tmpfilenam);
184
185         /* copy the message from stdin into temp file */
186         cpydata(fileno(stdin), fd, "standard input", tmpfilenam);
187
188         if (fstat(fd, &st) == NOTOK) {
189                 unlink(tmpfilenam);
190                 adios(EX_IOERR, tmpfilenam, "unable to fstat");
191         }
192         if (close(fd) == NOTOK)
193                 adios(EX_IOERR, tmpfilenam, "error closing");
194
195         /* don't add file if it is empty */
196         if (st.st_size == 0) {
197                 unlink(tmpfilenam);
198                 advise(NULL, "empty file");
199                 exit(EX_OK);
200         }
201
202         /*
203         ** read folder and create message structure
204         */
205         if (!(mp = folder_read(folder)))
206                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
207
208         /*
209         ** Link message into folder, and possibly add
210         ** to the Unseen-Sequence's.
211         */
212         if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL))
213                         == -1)
214                 exit(EX_SOFTWARE);
215
216         /*
217         ** Add the message to any extra sequences
218         ** that have been specified.
219         */
220         for (seqp = 0; seqs[seqp]; seqp++) {
221                 if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw))
222                         exit(EX_SOFTWARE);
223         }
224
225         seq_setunseen(mp, 1);  /* add new msgs to unseen sequences */
226         seq_save(mp);  /* synchronize and save message sequences */
227         folder_free(mp);  /* free folder/message structure */
228
229         context_save();  /* save the global context file */
230         unlink(tmpfilenam);  /* remove temporary file */
231         tmpfilenam = NULL;
232
233         return EX_OK;
234 }
235
236 static void
237 fix_mbox(int out, char *outfile)
238 {
239         char mbox[5];
240         int ret;
241
242         if ((ret = read(fileno(stdin), mbox, sizeof(mbox))) != sizeof(mbox)) {
243                 if (ret == -1) {
244                         adios(EX_IOERR, "standard input", "error reading");
245                 }
246                 return;
247         }
248
249         if (strncmp(mbox, "From ", sizeof(mbox))==0) {
250                 do {
251                         if ((ret = read(fileno(stdin), mbox, 1)) != 1) {
252                                 if (ret == -1) {
253                                         adios(EX_IOERR, "standard input", "error reading");
254                                 }
255                                 return;
256                         }
257                 } while (*mbox != '\n');
258         } else {
259                 if (write(out, mbox, sizeof(mbox)) != sizeof(mbox)) {
260                         adios(EX_IOERR, outfile, "error writing");
261                 }
262         }
263 }
264
265 /*
266 ** Clean up and exit
267 */
268 void
269 unlink_done()
270 {
271         if (tmpfilenam && *tmpfilenam) {
272                 unlink(tmpfilenam);
273         }
274 }