]> diplodocus.org Git - nmh/blob - uip/mhstore.c
Removed temporary probes added in commit
[nmh] / uip / mhstore.c
1
2 /*
3 * mhstore.c -- store the contents of MIME messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <h/mts.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 #define MHSTORE_SWITCHES \
22 X("auto", 0, AUTOSW) \
23 X("noauto", 0, NAUTOSW) \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("verbose", 0, VERBSW) \
27 X("noverbose", 0, NVERBSW) \
28 X("file file", 0, FILESW) /* interface from show */ \
29 X("outfile outfile", 0, OUTFILESW) \
30 X("part number", 0, PARTSW) \
31 X("type content", 0, TYPESW) \
32 X("rcache policy", 0, RCACHESW) \
33 X("wcache policy", 0, WCACHESW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
36 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
37 X("debug", -5, DEBUGSW) \
38
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(MHSTORE);
41 #undef X
42
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(MHSTORE, switches);
45 #undef X
46
47
48 int save_clobber_policy (const char *);
49 extern int files_not_clobbered;
50
51 /* mhparse.c */
52 extern char *tmp; /* directory to place temp files */
53
54 /* mhcachesbr.c */
55 extern int rcachesw;
56 extern int wcachesw;
57 extern char *cache_public;
58 extern char *cache_private;
59
60 /* mhstoresbr.c */
61 extern int autosw;
62 extern char *cwd; /* cache current working directory */
63
64 /* mhmisc.c */
65 extern int npart;
66 extern int ntype;
67 extern char *parts[NPARTS + 1];
68 extern char *types[NTYPES + 1];
69 extern int userrs;
70
71 int debugsw = 0;
72 int verbosw = 0;
73
74 #define quitser pipeser
75
76 /* mhparse.c */
77 CT parse_mime (char *);
78
79 /* mhmisc.c */
80 int part_ok (CT, int);
81 int type_ok (CT, int);
82 void flush_errors (void);
83
84 /* mhstoresbr.c */
85 void store_all_messages (CT *);
86
87 /* mhfree.c */
88 extern CT *cts;
89 void freects_done (int) NORETURN;
90
91 /*
92 * static prototypes
93 */
94 static void pipeser (int);
95
96
97 int
98 main (int argc, char **argv)
99 {
100 int msgnum, *icachesw;
101 char *cp, *file = NULL, *outfile = NULL, *folder = NULL;
102 char *maildir, buf[100], **argp;
103 char **arguments;
104 struct msgs_array msgs = { 0, 0, NULL };
105 struct msgs *mp = NULL;
106 CT ct, *ctp;
107 FILE *fp;
108
109 done=freects_done;
110
111 #ifdef LOCALE
112 setlocale(LC_ALL, "");
113 #endif
114 invo_name = r1bindex (argv[0], '/');
115
116 /* read user profile/context */
117 context_read();
118
119 arguments = getarguments (invo_name, argc, argv, 1);
120 argp = arguments;
121
122 /*
123 * Parse arguments
124 */
125 while ((cp = *argp++)) {
126 if (*cp == '-') {
127 switch (smatch (++cp, switches)) {
128 case AMBIGSW:
129 ambigsw (cp, switches);
130 done (1);
131 case UNKWNSW:
132 adios (NULL, "-%s unknown", cp);
133
134 case HELPSW:
135 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
136 invo_name);
137 print_help (buf, switches, 1);
138 done (0);
139 case VERSIONSW:
140 print_version(invo_name);
141 done (0);
142
143 case AUTOSW:
144 autosw++;
145 continue;
146 case NAUTOSW:
147 autosw = 0;
148 continue;
149
150 case RCACHESW:
151 icachesw = &rcachesw;
152 goto do_cache;
153 case WCACHESW:
154 icachesw = &wcachesw;
155 do_cache:
156 if (!(cp = *argp++) || *cp == '-')
157 adios (NULL, "missing argument to %s", argp[-2]);
158 switch (*icachesw = smatch (cp, caches)) {
159 case AMBIGSW:
160 ambigsw (cp, caches);
161 done (1);
162 case UNKWNSW:
163 adios (NULL, "%s unknown", cp);
164 default:
165 break;
166 }
167 continue;
168
169 case CHECKSW:
170 checksw++;
171 continue;
172 case NCHECKSW:
173 checksw = 0;
174 continue;
175
176 case PARTSW:
177 if (!(cp = *argp++) || *cp == '-')
178 adios (NULL, "missing argument to %s", argp[-2]);
179 if (npart >= NPARTS)
180 adios (NULL, "too many parts (starting with %s), %d max",
181 cp, NPARTS);
182 parts[npart++] = cp;
183 continue;
184
185 case TYPESW:
186 if (!(cp = *argp++) || *cp == '-')
187 adios (NULL, "missing argument to %s", argp[-2]);
188 if (ntype >= NTYPES)
189 adios (NULL, "too many types (starting with %s), %d max",
190 cp, NTYPES);
191 types[ntype++] = cp;
192 continue;
193
194 case FILESW:
195 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
196 adios (NULL, "missing argument to %s", argp[-2]);
197 file = *cp == '-' ? cp : path (cp, TFILE);
198 continue;
199
200 case OUTFILESW:
201 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
202 adios (NULL, "missing argument to %s", argp[-2]);
203 outfile = *cp == '-' ? cp : path (cp, TFILE);
204 continue;
205
206 case VERBSW:
207 verbosw = 1;
208 continue;
209 case NVERBSW:
210 verbosw = 0;
211 continue;
212 case CLOBBERSW:
213 if (!(cp = *argp++) || *cp == '-')
214 adios (NULL, "missing argument to %s", argp[-2]);
215 if (save_clobber_policy (cp)) {
216 adios (NULL, "invalid argument, %s, to %s", argp[-1],
217 argp[-2]);
218 }
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 /*
239 * Check if we've specified an additional profile
240 */
241 if ((cp = getenv ("MHSTORE"))) {
242 if ((fp = fopen (cp, "r"))) {
243 readconfig ((struct node **) 0, fp, cp, 0);
244 fclose (fp);
245 } else {
246 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
247 }
248 }
249
250 /*
251 * Read the standard profile setup
252 */
253 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
254 readconfig ((struct node **) 0, fp, cp, 0);
255 fclose (fp);
256 }
257
258 /* Check for public cache location */
259 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
260 cache_public = NULL;
261
262 /* Check for private cache location */
263 if (!(cache_private = context_find (nmhprivcache)))
264 cache_private = ".cache";
265 cache_private = getcpy (m_maildir (cache_private));
266
267 /*
268 * Cache the current directory before we do any chdirs()'s.
269 */
270 cwd = getcpy (pwd());
271
272 /*
273 * Check for storage directory. If specified,
274 * then store temporary files there. Else we
275 * store them in standard nmh directory.
276 */
277 if ((cp = context_find (nmhstorage)) && *cp)
278 tmp = concat (cp, "/", invo_name, NULL);
279 else
280 tmp = add (m_maildir (invo_name), NULL);
281
282 if (!context_find ("path"))
283 free (path ("./", TFOLDER));
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 if (outfile) {
299 ct->c_storage = outfile;
300 }
301 }
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, 1)))
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), sizeof(*cts))))
330 adios (NULL, "out of memory");
331 ctp = cts;
332
333 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
334 if (is_selected(mp, msgnum)) {
335 char *msgnam;
336
337 msgnam = m_name (msgnum);
338 if ((ct = parse_mime (msgnam))) {
339 *ctp++ = ct;
340 if (outfile) {
341 ct->c_storage = add (outfile, NULL);
342 }
343 }
344 }
345 }
346 }
347
348 if (!*cts)
349 done (1);
350
351 userrs = 1;
352 SIGNAL (SIGQUIT, quitser);
353 SIGNAL (SIGPIPE, pipeser);
354
355 /*
356 * Get the associated umask for the relevant contents.
357 */
358 for (ctp = cts; *ctp; ctp++) {
359 struct stat st;
360
361 ct = *ctp;
362 if (type_ok (ct, 1) && !ct->c_umask) {
363 if (stat (ct->c_file, &st) != NOTOK)
364 ct->c_umask = ~(st.st_mode & 0777);
365 else
366 ct->c_umask = ~m_gmprot();
367 }
368 }
369
370 /*
371 * Store the message content
372 */
373 store_all_messages (cts);
374
375 /* Now free all the structures for the content */
376 for (ctp = cts; *ctp; ctp++)
377 free_content (*ctp);
378
379 free ((char *) cts);
380 cts = NULL;
381
382 /* If reading from a folder, do some updating */
383 if (mp) {
384 context_replace (pfolder, folder);/* update current folder */
385 seq_setcur (mp, mp->hghsel); /* update current message */
386 seq_save (mp); /* synchronize sequences */
387 context_save (); /* save the context file */
388 }
389
390 done (files_not_clobbered);
391 return 1;
392 }
393
394
395 static void
396 pipeser (int i)
397 {
398 if (i == SIGQUIT) {
399 fflush (stdout);
400 fprintf (stderr, "\n");
401 fflush (stderr);
402 }
403
404 done (1);
405 /* NOTREACHED */
406 }