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