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