Replace getcpy() and strdup() with mh_xstrdup()
[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
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 = mh_xstrdup(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         /* copy the message from stdin into temp file */
183         cpydata(fileno(stdin), fd, "standard input", tmpfilenam);
184
185         if (fstat(fd, &st) == NOTOK) {
186                 unlink(tmpfilenam);
187                 adios(EX_IOERR, tmpfilenam, "unable to fstat");
188         }
189         if (close(fd) == NOTOK)
190                 adios(EX_IOERR, tmpfilenam, "error closing");
191
192         /* don't add file if it is empty */
193         if (st.st_size == 0) {
194                 unlink(tmpfilenam);
195                 advise(NULL, "empty file");
196                 exit(EX_OK);
197         }
198
199         /*
200         ** read folder and create message structure
201         */
202         if (!(mp = folder_read(folder)))
203                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
204
205         /*
206         ** Link message into folder, and possibly add
207         ** to the Unseen-Sequence's.
208         */
209         if ((msgnum = folder_addmsg(&mp, tmpfilenam, 0, unseensw, 0, 0, NULL))
210                         == -1)
211                 exit(EX_SOFTWARE);
212
213         /*
214         ** Add the message to any extra sequences
215         ** that have been specified.
216         */
217         for (seqp = 0; seqs[seqp]; seqp++) {
218                 if (!seq_addmsg(mp, seqs[seqp], msgnum, publicsw, zerosw))
219                         exit(EX_SOFTWARE);
220         }
221
222         seq_setunseen(mp, 1);  /* add new msgs to unseen sequences */
223         seq_save(mp);  /* synchronize and save message sequences */
224         folder_free(mp);  /* free folder/message structure */
225
226         context_save();  /* save the global context file */
227         unlink(tmpfilenam);  /* remove temporary file */
228         tmpfilenam = NULL;
229
230         return EX_OK;
231 }
232
233 /*
234 ** Clean up and exit
235 */
236 void
237 unlink_done()
238 {
239         if (tmpfilenam && *tmpfilenam) {
240                 unlink(tmpfilenam);
241         }
242 }