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