Renamed r1bindex() to mhbasename(), to make its function becomes clear.
[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 : path(cp, TFILE);
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 = pluspath(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(m_maildir(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 = add(m_maildir(invo_name), NULL);
285
286         if (!context_find("path"))
287                 free(path("./", TFOLDER));
288
289         if (file && msgs.size)
290                 adios(NULL, "cannot specify msg and file at same time!");
291
292         /*
293         ** check if message is coming from file
294         */
295         if (file) {
296                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
297                         adios(NULL, "out of memory");
298                 ctp = cts;
299
300                 if ((ct = parse_mime(file)));
301                         *ctp++ = ct;
302         } else {
303                 /*
304                 ** message(s) are coming from a folder
305                 */
306                 if (!msgs.size)
307                         app_msgarg(&msgs, "cur");
308                 if (!folder)
309                         folder = getfolder(1);
310                 maildir = m_maildir(folder);
311
312                 if (chdir(maildir) == NOTOK)
313                         adios(maildir, "unable to change directory to");
314
315                 /* read folder and create message structure */
316                 if (!(mp = folder_read(folder)))
317                         adios(NULL, "unable to read folder %s", folder);
318
319                 /* check for empty folder */
320                 if (mp->nummsg == 0)
321                         adios(NULL, "no messages in %s", folder);
322
323                 /* parse all the message ranges/sequences and set SELECTED */
324                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
325                         if (!m_convert(mp, msgs.msgs[msgnum]))
326                                 done(1);
327                 seq_setprev(mp);  /* set the previous-sequence */
328
329                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
330                                 sizeof(*cts))))
331                         adios(NULL, "out of memory");
332                 ctp = cts;
333
334                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
335                         if (is_selected(mp, msgnum)) {
336                                 char *msgnam;
337
338                                 msgnam = m_name(msgnum);
339                                 if ((ct = parse_mime(msgnam)))
340                                         *ctp++ = ct;
341                         }
342                 }
343         }
344
345         if (!*cts)
346                 done(1);
347
348         userrs = 1;
349         SIGNAL(SIGQUIT, quitser);
350         SIGNAL(SIGPIPE, pipeser);
351
352         /*
353         ** Get the associated umask for the relevant contents.
354         */
355         for (ctp = cts; *ctp; ctp++) {
356                 struct stat st;
357
358                 ct = *ctp;
359                 if (type_ok(ct, 1) && !ct->c_umask) {
360                         if (stat(ct->c_file, &st) != NOTOK)
361                                 ct->c_umask = ~(st.st_mode & 0777);
362                         else
363                                 ct->c_umask = ~m_gmprot();
364                 }
365         }
366
367         /*
368         ** Store the message content
369         */
370         store_all_messages(cts);
371
372         /* Now free all the structures for the content */
373         for (ctp = cts; *ctp; ctp++)
374                 free_content(*ctp);
375
376         free((char *) cts);
377         cts = NULL;
378
379         /* If reading from a folder, do some updating */
380         if (mp) {
381                 context_replace(pfolder, folder);  /* update current folder */
382                 seq_setcur(mp, mp->hghsel);  /* update current message */
383                 seq_save(mp);  /* synchronize sequences  */
384                 context_save();  /* save the context file  */
385         }
386
387         done(0);
388         return 1;
389 }
390
391
392 static RETSIGTYPE
393 pipeser(int i)
394 {
395         if (i == SIGQUIT) {
396                 unlink("core");
397                 fflush(stdout);
398                 fprintf(stderr, "\n");
399                 fflush(stderr);
400         }
401
402         done(1);
403         /* NOTREACHED */
404 }