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