]> diplodocus.org Git - nmh/blob - uip/mhstore.c
00e0715848fb76eac4014352963d9a0aeee0fdaf
[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 <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 #define MHSTORE_SWITCHES \
23 X("auto", 0, AUTOSW) \
24 X("noauto", 0, NAUTOSW) \
25 X("check", 0, CHECKSW) \
26 X("nocheck", 0, NCHECKSW) \
27 X("verbose", 0, VERBSW) \
28 X("noverbose", 0, NVERBSW) \
29 X("file file", 0, FILESW) /* interface from show */ \
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, *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 VERBSW:
201 verbosw = 1;
202 continue;
203 case NVERBSW:
204 verbosw = 0;
205 continue;
206 case CLOBBERSW:
207 if (!(cp = *argp++) || *cp == '-')
208 adios (NULL, "missing argument to %s", argp[-2]);
209 if (save_clobber_policy (cp)) {
210 adios (NULL, "invalid argument, %s, to %s", argp[-1],
211 argp[-2]);
212 }
213 continue;
214 case DEBUGSW:
215 debugsw = 1;
216 continue;
217 }
218 }
219 if (*cp == '+' || *cp == '@') {
220 if (folder)
221 adios (NULL, "only one folder at a time!");
222 else
223 folder = pluspath (cp);
224 } else
225 app_msgarg(&msgs, cp);
226 }
227
228 /* null terminate the list of acceptable parts/types */
229 parts[npart] = NULL;
230 types[ntype] = NULL;
231
232 /*
233 * Check if we've specified an additional profile
234 */
235 if ((cp = getenv ("MHSTORE"))) {
236 if ((fp = fopen (cp, "r"))) {
237 readconfig ((struct node **) 0, fp, cp, 0);
238 fclose (fp);
239 } else {
240 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
241 }
242 }
243
244 /*
245 * Read the standard profile setup
246 */
247 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
248 readconfig ((struct node **) 0, fp, cp, 0);
249 fclose (fp);
250 }
251
252 /* Check for public cache location */
253 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
254 cache_public = NULL;
255
256 /* Check for private cache location */
257 if (!(cache_private = context_find (nmhprivcache)))
258 cache_private = ".cache";
259 cache_private = getcpy (m_maildir (cache_private));
260
261 /*
262 * Cache the current directory before we do any chdirs()'s.
263 */
264 cwd = getcpy (pwd());
265
266 /*
267 * Check for storage directory. If specified,
268 * then store temporary files there. Else we
269 * store them in standard nmh directory.
270 */
271 if ((cp = context_find (nmhstorage)) && *cp)
272 tmp = concat (cp, "/", invo_name, NULL);
273 else
274 tmp = add (m_maildir (invo_name), NULL);
275
276 if (!context_find ("path"))
277 free (path ("./", TFOLDER));
278
279 if (file && msgs.size)
280 adios (NULL, "cannot specify msg and file at same time!");
281
282 /*
283 * check if message is coming from file
284 */
285 if (file) {
286 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
287 adios (NULL, "out of memory");
288 ctp = cts;
289
290 if ((ct = parse_mime (file)))
291 *ctp++ = ct;
292 } else {
293 /*
294 * message(s) are coming from a folder
295 */
296 if (!msgs.size)
297 app_msgarg(&msgs, "cur");
298 if (!folder)
299 folder = getfolder (1);
300 maildir = m_maildir (folder);
301
302 if (chdir (maildir) == NOTOK)
303 adios (maildir, "unable to change directory to");
304
305 /* read folder and create message structure */
306 if (!(mp = folder_read (folder, 1)))
307 adios (NULL, "unable to read folder %s", folder);
308
309 /* check for empty folder */
310 if (mp->nummsg == 0)
311 adios (NULL, "no messages in %s", folder);
312
313 /* parse all the message ranges/sequences and set SELECTED */
314 for (msgnum = 0; msgnum < msgs.size; msgnum++)
315 if (!m_convert (mp, msgs.msgs[msgnum]))
316 done (1);
317 seq_setprev (mp); /* set the previous-sequence */
318
319 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
320 adios (NULL, "out of memory");
321 ctp = cts;
322
323 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
324 if (is_selected(mp, msgnum)) {
325 char *msgnam;
326
327 msgnam = m_name (msgnum);
328 if ((ct = parse_mime (msgnam)))
329 *ctp++ = ct;
330 }
331 }
332 }
333
334 if (!*cts)
335 done (1);
336
337 userrs = 1;
338 SIGNAL (SIGQUIT, quitser);
339 SIGNAL (SIGPIPE, pipeser);
340
341 /*
342 * Get the associated umask for the relevant contents.
343 */
344 for (ctp = cts; *ctp; ctp++) {
345 struct stat st;
346
347 ct = *ctp;
348 if (type_ok (ct, 1) && !ct->c_umask) {
349 if (stat (ct->c_file, &st) != NOTOK)
350 ct->c_umask = ~(st.st_mode & 0777);
351 else
352 ct->c_umask = ~m_gmprot();
353 }
354 }
355
356 /*
357 * Store the message content
358 */
359 store_all_messages (cts);
360
361 /* Now free all the structures for the content */
362 for (ctp = cts; *ctp; ctp++)
363 free_content (*ctp);
364
365 free ((char *) cts);
366 cts = NULL;
367
368 /* If reading from a folder, do some updating */
369 if (mp) {
370 context_replace (pfolder, folder);/* update current folder */
371 seq_setcur (mp, mp->hghsel); /* update current message */
372 seq_save (mp); /* synchronize sequences */
373 context_save (); /* save the context file */
374 }
375
376 done (files_not_clobbered);
377 return 1;
378 }
379
380
381 static void
382 pipeser (int i)
383 {
384 if (i == SIGQUIT) {
385 fflush (stdout);
386 fprintf (stderr, "\n");
387 fflush (stderr);
388 }
389
390 done (1);
391 /* NOTREACHED */
392 }