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