Reformated comments and long lines
[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 = r1bindex (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]",
150                                                 invo_name);
151                                 print_help (buf, switches, 1);
152                                 done (1);
153                         case VERSIONSW:
154                                 print_version(invo_name);
155                                 done (1);
156
157                         case AUTOSW:
158                                 autosw++;
159                                 continue;
160                         case NAUTOSW:
161                                 autosw = 0;
162                                 continue;
163
164                         case RCACHESW:
165                                 icachesw = &rcachesw;
166                                 goto do_cache;
167                         case WCACHESW:
168                                 icachesw = &wcachesw;
169 do_cache:
170                                 if (!(cp = *argp++) || *cp == '-')
171                                         adios (NULL, "missing argument to %s", 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", argp[-2]);
193                                 if (npart >= NPARTS)
194                                         adios (NULL, "too many parts (starting with %s), %d max",
195                                                    cp, NPARTS);
196                                 parts[npart++] = cp;
197                                 continue;
198
199                         case TYPESW:
200                                 if (!(cp = *argp++) || *cp == '-')
201                                         adios (NULL, "missing argument to %s", argp[-2]);
202                                 if (ntype >= NTYPES)
203                                         adios (NULL, "too many types (starting with %s), %d max",
204                                                    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", argp[-2]);
211                                 file = *cp == '-' ? cp : path (cp, TFILE);
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 = pluspath (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)", cp);
249                 }
250         }
251
252         /*
253         ** Read the standard profile setup
254         */
255         if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
256                 readconfig ((struct node **) 0, fp, cp, 0);
257                 fclose (fp);
258         }
259
260         /* Check for public cache location */
261         if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
262                 cache_public = NULL;
263
264         /* Check for private cache location */
265         if (!(cache_private = context_find (nmhprivcache)))
266                 cache_private = ".cache";
267         cache_private = getcpy (m_maildir (cache_private));
268
269         /*
270         ** Cache the current directory before we do any chdirs()'s.
271         */
272         cwd = getcpy (pwd());
273
274         /*
275         ** Check for storage directory.  If specified,
276         ** then store temporary files there.  Else we
277         ** store them in standard nmh directory.
278         */
279         if ((cp = context_find (nmhstorage)) && *cp)
280                 tmp = concat (cp, "/", invo_name, NULL);
281         else
282                 tmp = add (m_maildir (invo_name), NULL);
283
284         if (!context_find ("path"))
285                 free (path ("./", TFOLDER));
286
287         if (file && msgs.size)
288                 adios (NULL, "cannot specify msg and file at same time!");
289
290         /*
291         ** check if message is coming from file
292         */
293         if (file) {
294                 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
295                         adios (NULL, "out of memory");
296                 ctp = cts;
297
298                 if ((ct = parse_mime (file)));
299                         *ctp++ = ct;
300         } else {
301                 /*
302                 ** message(s) are coming from a folder
303                 */
304                 if (!msgs.size)
305                         app_msgarg(&msgs, "cur");
306                 if (!folder)
307                         folder = getfolder (1);
308                 maildir = m_maildir (folder);
309
310                 if (chdir (maildir) == NOTOK)
311                         adios (maildir, "unable to change directory to");
312
313                 /* read folder and create message structure */
314                 if (!(mp = folder_read (folder)))
315                         adios (NULL, "unable to read folder %s", folder);
316
317                 /* check for empty folder */
318                 if (mp->nummsg == 0)
319                         adios (NULL, "no messages in %s", folder);
320
321                 /* parse all the message ranges/sequences and set SELECTED */
322                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
323                         if (!m_convert (mp, msgs.msgs[msgnum]))
324                                 done (1);
325                 seq_setprev (mp);  /* set the previous-sequence */
326
327                 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1),
328                                 sizeof(*cts))))
329                         adios (NULL, "out of memory");
330                 ctp = cts;
331
332                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
333                         if (is_selected(mp, msgnum)) {
334                                 char *msgnam;
335
336                                 msgnam = m_name (msgnum);
337                                 if ((ct = parse_mime (msgnam)))
338                                         *ctp++ = ct;
339                         }
340                 }
341         }
342
343         if (!*cts)
344                 done (1);
345
346         userrs = 1;
347         SIGNAL (SIGQUIT, quitser);
348         SIGNAL (SIGPIPE, pipeser);
349
350         /*
351         ** Get the associated umask for the relevant contents.
352         */
353         for (ctp = cts; *ctp; ctp++) {
354                 struct stat st;
355
356                 ct = *ctp;
357                 if (type_ok (ct, 1) && !ct->c_umask) {
358                         if (stat (ct->c_file, &st) != NOTOK)
359                                 ct->c_umask = ~(st.st_mode & 0777);
360                         else
361                                 ct->c_umask = ~m_gmprot();
362                 }
363         }
364
365         /*
366         ** Store the message content
367         */
368         store_all_messages (cts);
369
370         /* Now free all the structures for the content */
371         for (ctp = cts; *ctp; ctp++)
372                 free_content (*ctp);
373
374         free ((char *) cts);
375         cts = NULL;
376
377         /* If reading from a folder, do some updating */
378         if (mp) {
379                 context_replace (pfolder, folder);  /* update current folder */
380                 seq_setcur (mp, mp->hghsel);  /* update current message */
381                 seq_save (mp);  /* synchronize sequences  */
382                 context_save ();  /* save the context file  */
383         }
384
385         done (0);
386         return 1;
387 }
388
389
390 static RETSIGTYPE
391 pipeser (int i)
392 {
393         if (i == SIGQUIT) {
394                 unlink ("core");
395                 fflush (stdout);
396                 fprintf (stderr, "\n");
397                 fflush (stderr);
398         }
399
400         done (1);
401         /* NOTREACHED */
402 }