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