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