Merged h/mts.h into h/prototypes.h.
[mmh] / uip / mhstore.c
1 /*
2 ** mhstore.c -- store the contents of MIME messages
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 <h/md5.h>
13 #include <errno.h>
14 #include <signal.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
19 #include <h/utils.h>
20
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
23 #endif
24
25 static struct swit switches[] = {
26 #define AUTOSW  0
27         { "auto", 0 },
28 #define NAUTOSW  1
29         { "noauto", 0 },
30 #define CHECKSW  2
31         { "check", 0 },
32 #define NCHECKSW  3
33         { "nocheck", 0 },
34 #define VERBSW  4
35         { "verbose", 0 },
36 #define NVERBSW  5
37         { "noverbose", 0 },
38 #define FILESW  6  /* interface from show */
39         { "file file", 0 },
40 #define PARTSW  7
41         { "part number", 0 },
42 #define TYPESW  8
43         { "type content", 0 },
44 #define RCACHESW  9
45         { "rcache policy", 0 },
46 #define WCACHESW  10
47         { "wcache policy", 0 },
48 #define VERSIONSW  11
49         { "version", 0 },
50 #define HELPSW  12
51         { "help", 0 },
52
53 /*
54 ** switches for debugging
55 */
56 #define DEBUGSW  13
57         { "debug", -5 },
58         { NULL, 0 }
59 };
60
61
62 /* mhparse.c */
63 extern char *tmp;  /* directory to place temp files */
64
65 /* mhcachesbr.c */
66 extern int rcachesw;
67 extern int wcachesw;
68 extern char *cache_public;
69 extern char *cache_private;
70
71 /* mhstoresbr.c */
72 extern int autosw;
73 extern char *cwd;  /* cache current working directory */
74
75 /* mhmisc.c */
76 extern int npart;
77 extern int ntype;
78 extern char *parts[NPARTS + 1];
79 extern char *types[NTYPES + 1];
80 extern int userrs;
81
82 int debugsw = 0;
83 int verbosw = 0;
84
85 #define quitser pipeser
86
87 /* mhparse.c */
88 CT parse_mime(char *);
89
90 /* mhmisc.c */
91 int part_ok(CT, int);
92 int type_ok(CT, int);
93 void set_endian(void);
94 void flush_errors(void);
95
96 /* mhstoresbr.c */
97 void store_all_messages(CT *);
98
99 /* mhfree.c */
100 void free_content(CT);
101 extern CT *cts;
102 void freects_done(int) NORETURN;
103
104 /*
105 ** static prototypes
106 */
107 static RETSIGTYPE pipeser(int);
108
109
110 int
111 main(int argc, char **argv)
112 {
113         int msgnum, *icachesw;
114         char *cp, *file = NULL, *folder = NULL;
115         char *maildir, buf[100], **argp;
116         char **arguments;
117         struct msgs_array msgs = { 0, 0, NULL };
118         struct msgs *mp = NULL;
119         CT ct, *ctp;
120         FILE *fp;
121
122         done=freects_done;
123
124 #ifdef LOCALE
125         setlocale(LC_ALL, "");
126 #endif
127         invo_name = mhbasename(argv[0]);
128
129         /* read user profile/context */
130         context_read();
131
132         arguments = getarguments(invo_name, argc, argv, 1);
133         argp = arguments;
134
135         /*
136         ** Parse arguments
137         */
138         while ((cp = *argp++)) {
139                 if (*cp == '-') {
140                         switch (smatch(++cp, switches)) {
141                         case AMBIGSW:
142                                 ambigsw(cp, switches);
143                                 done(1);
144                         case UNKWNSW:
145                                 adios(NULL, "-%s unknown", cp);
146
147                         case HELPSW:
148                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
149                                 print_help(buf, switches, 1);
150                                 done(1);
151                         case VERSIONSW:
152                                 print_version(invo_name);
153                                 done(1);
154
155                         case AUTOSW:
156                                 autosw++;
157                                 continue;
158                         case NAUTOSW:
159                                 autosw = 0;
160                                 continue;
161
162                         case RCACHESW:
163                                 icachesw = &rcachesw;
164                                 goto do_cache;
165                         case WCACHESW:
166                                 icachesw = &wcachesw;
167 do_cache:
168                                 if (!(cp = *argp++) || *cp == '-')
169                                         adios(NULL, "missing argument to %s",
170                                                         argp[-2]);
171                                 switch (*icachesw = smatch(cp, caches)) {
172                                 case AMBIGSW:
173                                         ambigsw(cp, caches);
174                                         done(1);
175                                 case UNKWNSW:
176                                         adios(NULL, "%s unknown", cp);
177                                 default:
178                                         break;
179                                 }
180                                 continue;
181
182                         case CHECKSW:
183                                 checksw++;
184                                 continue;
185                         case NCHECKSW:
186                                 checksw = 0;
187                                 continue;
188
189                         case PARTSW:
190                                 if (!(cp = *argp++) || *cp == '-')
191                                         adios(NULL, "missing argument to %s",
192                                                         argp[-2]);
193                                 if (npart >= NPARTS)
194                                         adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
195                                 parts[npart++] = cp;
196                                 continue;
197
198                         case TYPESW:
199                                 if (!(cp = *argp++) || *cp == '-')
200                                         adios(NULL, "missing argument to %s",
201                                                         argp[-2]);
202                                 if (ntype >= NTYPES)
203                                         adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
204                                 types[ntype++] = cp;
205                                 continue;
206
207                         case FILESW:
208                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
209                                         adios(NULL, "missing argument to %s",
210                                                         argp[-2]);
211                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
212                                 continue;
213
214                         case VERBSW:
215                                 verbosw = 1;
216                                 continue;
217                         case NVERBSW:
218                                 verbosw = 0;
219                                 continue;
220                         case DEBUGSW:
221                                 debugsw = 1;
222                                 continue;
223                         }
224                 }
225                 if (*cp == '+' || *cp == '@') {
226                         if (folder)
227                                 adios(NULL, "only one folder at a time!");
228                         else
229                                 folder = getcpy(expandfol(cp));
230                 } else
231                         app_msgarg(&msgs, cp);
232         }
233
234         /* null terminate the list of acceptable parts/types */
235         parts[npart] = NULL;
236         types[ntype] = NULL;
237
238         set_endian();
239
240         /*
241         ** Check if we've specified an additional profile
242         */
243         if ((cp = getenv("MHSTORE"))) {
244                 if ((fp = fopen(cp, "r"))) {
245                         readconfig((struct node **) 0, fp, cp, 0);
246                         fclose(fp);
247                 } else {
248                         admonish("", "unable to read $MHSTORE profile (%s)",
249                                         cp);
250                 }
251         }
252
253         /*
254         ** Read the standard profile setup
255         */
256         if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
257                 readconfig((struct node **) 0, fp, cp, 0);
258                 fclose(fp);
259         }
260
261         /* Check for public cache location */
262         if ((cache_public = context_find(nmhcache)) && *cache_public != '/')
263                 cache_public = NULL;
264
265         /* Check for private cache location */
266         if (!(cache_private = context_find(nmhprivcache)))
267                 cache_private = ".cache";
268         cache_private = getcpy(toabsdir(cache_private));
269
270         /*
271         ** Cache the current directory before we do any chdirs()'s.
272         */
273         cwd = getcpy(pwd());
274
275         /*
276         ** Check for storage directory.  If specified,
277         ** then store temporary files there.  Else we
278         ** store them in standard nmh directory.
279         */
280         if ((cp = context_find(nmhstorage)) && *cp)
281                 tmp = concat(cp, "/", invo_name, NULL);
282         else
283                 tmp = getcpy(toabsdir(invo_name));
284
285         if (file && msgs.size)
286                 adios(NULL, "cannot specify msg and file at same time!");
287
288         /*
289         ** check if message is coming from file
290         */
291         if (file) {
292                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
293                         adios(NULL, "out of memory");
294                 ctp = cts;
295
296                 if ((ct = parse_mime(file)));
297                         *ctp++ = ct;
298         } else {
299                 /*
300                 ** message(s) are coming from a folder
301                 */
302                 if (!msgs.size)
303                         app_msgarg(&msgs, seq_cur);
304                 if (!folder)
305                         folder = getcurfol();
306                 maildir = toabsdir(folder);
307
308                 if (chdir(maildir) == NOTOK)
309                         adios(maildir, "unable to change directory to");
310
311                 /* read folder and create message structure */
312                 if (!(mp = folder_read(folder)))
313                         adios(NULL, "unable to read folder %s", folder);
314
315                 /* check for empty folder */
316                 if (mp->nummsg == 0)
317                         adios(NULL, "no messages in %s", folder);
318
319                 /* parse all the message ranges/sequences and set SELECTED */
320                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
321                         if (!m_convert(mp, msgs.msgs[msgnum]))
322                                 done(1);
323                 seq_setprev(mp);  /* set the previous-sequence */
324
325                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
326                                 sizeof(*cts))))
327                         adios(NULL, "out of memory");
328                 ctp = cts;
329
330                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
331                         if (is_selected(mp, msgnum)) {
332                                 char *msgnam;
333
334                                 msgnam = m_name(msgnum);
335                                 if ((ct = parse_mime(msgnam)))
336                                         *ctp++ = ct;
337                         }
338                 }
339         }
340
341         if (!*cts)
342                 done(1);
343
344         userrs = 1;
345         SIGNAL(SIGQUIT, quitser);
346         SIGNAL(SIGPIPE, pipeser);
347
348         /*
349         ** Get the associated umask for the relevant contents.
350         */
351         for (ctp = cts; *ctp; ctp++) {
352                 struct stat st;
353
354                 ct = *ctp;
355                 if (type_ok(ct, 1) && !ct->c_umask) {
356                         if (stat(ct->c_file, &st) != NOTOK)
357                                 ct->c_umask = ~(st.st_mode & 0777);
358                         else
359                                 ct->c_umask = ~m_gmprot();
360                 }
361         }
362
363         /*
364         ** Store the message content
365         */
366         store_all_messages(cts);
367
368         /* Now free all the structures for the content */
369         for (ctp = cts; *ctp; ctp++)
370                 free_content(*ctp);
371
372         free((char *) cts);
373         cts = NULL;
374
375         /* If reading from a folder, do some updating */
376         if (mp) {
377                 context_replace(curfolder, folder); /* update current folder */
378                 seq_setcur(mp, mp->hghsel);  /* update current message */
379                 seq_save(mp);  /* synchronize sequences  */
380                 context_save();  /* save the context file  */
381         }
382
383         done(0);
384         return 1;
385 }
386
387
388 static RETSIGTYPE
389 pipeser(int i)
390 {
391         if (i == SIGQUIT) {
392                 unlink("core");
393                 fflush(stdout);
394                 fprintf(stderr, "\n");
395                 fflush(stderr);
396         }
397
398         done(1);
399         /* NOTREACHED */
400 }