]> diplodocus.org Git - nmh/blob - uip/mhstore.c
sbr/dtime.c: Remove struct-assigning twscopy().
[nmh] / uip / mhstore.c
1 /* mhstore.c -- store the contents of MIME messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/signals.h>
11 #include <h/md5.h>
12 #include <h/mts.h>
13 #include <h/tws.h>
14 #include <h/mime.h>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
17 #include <h/utils.h>
18
19 #define MHSTORE_SWITCHES \
20 X("auto", 0, AUTOSW) \
21 X("noauto", 0, NAUTOSW) \
22 X("check", 0, CHECKSW) \
23 X("nocheck", 0, NCHECKSW) \
24 X("verbose", 0, VERBSW) \
25 X("noverbose", 0, NVERBSW) \
26 X("file file", 0, FILESW) /* interface from show */ \
27 X("outfile outfile", 0, OUTFILESW) \
28 X("part number", 0, PARTSW) \
29 X("type content", 0, TYPESW) \
30 X("prefer content", 0, PREFERSW) \
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 /* mhcachesbr.c */
48 extern int rcachesw;
49 extern int wcachesw;
50 extern char *cache_public;
51 extern char *cache_private;
52
53 /* mhmisc.c */
54 extern int npart;
55 extern int ntype;
56 extern char *parts[NPARTS + 1];
57 extern char *types[NTYPES + 1];
58 extern int userrs;
59
60 /* mhparse.c */
61 extern char *preferred_types[];
62 extern char *preferred_subtypes[];
63 extern int npreferred;
64
65 #define quitser pipeser
66
67 /* mhparse.c */
68 int debugsw = 0;
69 CT parse_mime (char *);
70
71 /* mhmisc.c */
72 int part_ok (CT);
73 int type_ok (CT, int);
74 void flush_errors (void);
75
76 /* mhfree.c */
77 extern CT *cts;
78 void freects_done (int) NORETURN;
79
80 /*
81 * static prototypes
82 */
83 static void pipeser (int);
84
85
86 int
87 main (int argc, char **argv)
88 {
89 /* verbosw defaults to 1 for backward compatibility. */
90 int msgnum, *icachesw, autosw = 0, verbosw = 1;
91 const char *clobbersw = "always";
92 char *cp, *file = NULL, *outfile = NULL, *folder = NULL;
93 char *maildir, buf[100], **argp;
94 char **arguments;
95 char *cwd;
96 struct msgs_array msgs = { 0, 0, NULL };
97 struct msgs *mp = NULL;
98 CT ct, *ctp;
99 FILE *fp;
100 int files_not_clobbered;
101 mhstoreinfo_t info;
102
103 if (nmh_init(argv[0], 1)) { return 1; }
104
105 done=freects_done;
106
107 arguments = getarguments (invo_name, argc, argv, 1);
108 argp = arguments;
109
110 /*
111 * Parse arguments
112 */
113 while ((cp = *argp++)) {
114 if (*cp == '-') {
115 switch (smatch (++cp, switches)) {
116 case AMBIGSW:
117 ambigsw (cp, switches);
118 done (1);
119 case UNKWNSW:
120 adios (NULL, "-%s unknown", cp);
121
122 case HELPSW:
123 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
124 invo_name);
125 print_help (buf, switches, 1);
126 done (0);
127 case VERSIONSW:
128 print_version(invo_name);
129 done (0);
130
131 case AUTOSW:
132 autosw++;
133 continue;
134 case NAUTOSW:
135 autosw = 0;
136 continue;
137
138 case RCACHESW:
139 icachesw = &rcachesw;
140 goto do_cache;
141 case WCACHESW:
142 icachesw = &wcachesw;
143 do_cache:
144 if (!(cp = *argp++) || *cp == '-')
145 adios (NULL, "missing argument to %s", argp[-2]);
146 switch (*icachesw = smatch (cp, caches)) {
147 case AMBIGSW:
148 ambigsw (cp, caches);
149 done (1);
150 case UNKWNSW:
151 adios (NULL, "%s unknown", cp);
152 default:
153 break;
154 }
155 continue;
156
157 case CHECKSW:
158 checksw++;
159 continue;
160 case NCHECKSW:
161 checksw = 0;
162 continue;
163
164 case PARTSW:
165 if (!(cp = *argp++) || *cp == '-')
166 adios (NULL, "missing argument to %s", argp[-2]);
167 if (npart >= NPARTS)
168 adios (NULL, "too many parts (starting with %s), %d max",
169 cp, NPARTS);
170 parts[npart++] = cp;
171 continue;
172
173 case TYPESW:
174 if (!(cp = *argp++) || *cp == '-')
175 adios (NULL, "missing argument to %s", argp[-2]);
176 if (ntype >= NTYPES)
177 adios (NULL, "too many types (starting with %s), %d max",
178 cp, NTYPES);
179 types[ntype++] = cp;
180 continue;
181
182 case PREFERSW:
183 if (!(cp = *argp++) || *cp == '-')
184 adios (NULL, "missing argument to %s", argp[-2]);
185 if (npreferred >= NPREFS)
186 adios (NULL, "too many preferred types (starting with %s), %d max",
187 cp, NPREFS);
188 preferred_types[npreferred] = cp;
189 cp = strchr(cp, '/');
190 if (cp) *cp++ = '\0';
191 preferred_subtypes[npreferred++] = 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 clobbersw = cp;
216 continue;
217 case DEBUGSW:
218 debugsw = 1;
219 continue;
220 }
221 }
222 if (*cp == '+' || *cp == '@') {
223 if (folder)
224 adios (NULL, "only one folder at a time!");
225 else
226 folder = pluspath (cp);
227 } else
228 app_msgarg(&msgs, cp);
229 }
230
231 /* null terminate the list of acceptable parts/types */
232 parts[npart] = NULL;
233 types[ntype] = NULL;
234
235 /*
236 * Check if we've specified an additional profile
237 */
238 if ((cp = getenv ("MHSTORE"))) {
239 if ((fp = fopen (cp, "r"))) {
240 readconfig ((struct node **) 0, fp, cp, 0);
241 fclose (fp);
242 } else {
243 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
244 }
245 }
246
247 /*
248 * Read the standard profile setup
249 */
250 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
251 readconfig ((struct node **) 0, fp, cp, 0);
252 fclose (fp);
253 }
254
255 /* Check for public cache location */
256 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
257 cache_public = NULL;
258
259 /* Check for private cache location */
260 if (!(cache_private = context_find (nmhprivcache)))
261 cache_private = ".cache";
262 cache_private = getcpy (m_maildir (cache_private));
263
264 /*
265 * Cache the current directory before we do any chdirs()'s.
266 */
267 cwd = mh_xstrdup(pwd());
268
269 if (!context_find ("path"))
270 free (path ("./", TFOLDER));
271
272 if (file && msgs.size)
273 adios (NULL, "cannot specify msg and file at same time!");
274
275 /*
276 * check if message is coming from file
277 */
278 if (file) {
279 cts = mh_xcalloc(2, sizeof *cts);
280 ctp = cts;
281
282 if ((ct = parse_mime (file))) {
283 *ctp++ = ct;
284 if (outfile) {
285 ct->c_storage = mh_xstrdup(outfile);
286 }
287 }
288 } else {
289 /*
290 * message(s) are coming from a folder
291 */
292 if (!msgs.size)
293 app_msgarg(&msgs, "cur");
294 if (!folder)
295 folder = getfolder (1);
296 maildir = m_maildir (folder);
297
298 if (chdir (maildir) == NOTOK)
299 adios (maildir, "unable to change directory to");
300
301 /* read folder and create message structure */
302 if (!(mp = folder_read (folder, 1)))
303 adios (NULL, "unable to read folder %s", folder);
304
305 /* check for empty folder */
306 if (mp->nummsg == 0)
307 adios (NULL, "no messages in %s", folder);
308
309 /* parse all the message ranges/sequences and set SELECTED */
310 for (msgnum = 0; msgnum < msgs.size; msgnum++)
311 if (!m_convert (mp, msgs.msgs[msgnum]))
312 done (1);
313 seq_setprev (mp); /* set the previous-sequence */
314
315 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
316 ctp = cts;
317
318 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
319 if (is_selected(mp, msgnum)) {
320 char *msgnam;
321
322 msgnam = m_name (msgnum);
323 if ((ct = parse_mime (msgnam))) {
324 *ctp++ = ct;
325 if (outfile) {
326 ct->c_storage = mh_xstrdup(outfile);
327 }
328 }
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 info = mhstoreinfo_create (cts, cwd, clobbersw, autosw, verbosw);
359 store_all_messages (info);
360 files_not_clobbered = mhstoreinfo_files_not_clobbered(info);
361 mhstoreinfo_free(info);
362
363 /* Now free all the structures for the content */
364 for (ctp = cts; *ctp; ctp++)
365 free_content (*ctp);
366
367 free (cts);
368 cts = NULL;
369
370 /* If reading from a folder, do some updating */
371 if (mp) {
372 context_replace (pfolder, folder);/* update current folder */
373 seq_setcur (mp, mp->hghsel); /* update current message */
374 seq_save (mp); /* synchronize sequences */
375 context_save (); /* save the context file */
376 }
377
378 done (files_not_clobbered);
379 return 1;
380 }
381
382
383 static void
384 pipeser (int i)
385 {
386 if (i == SIGQUIT) {
387 fflush (stdout);
388 fprintf (stderr, "\n");
389 fflush (stderr);
390 }
391
392 done (1);
393 /* NOTREACHED */
394 }