]> diplodocus.org Git - nmh/blob - uip/mhstore.c
With -messageid random, make the part after the @ more resemble a
[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 (0);
149 case VERSIONSW:
150 print_version(invo_name);
151 done (0);
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 /*
235 * Check if we've specified an additional profile
236 */
237 if ((cp = getenv ("MHSTORE"))) {
238 if ((fp = fopen (cp, "r"))) {
239 readconfig ((struct node **) 0, fp, cp, 0);
240 fclose (fp);
241 } else {
242 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
243 }
244 }
245
246 /*
247 * Read the standard profile setup
248 */
249 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
250 readconfig ((struct node **) 0, fp, cp, 0);
251 fclose (fp);
252 }
253
254 /* Check for public cache location */
255 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
256 cache_public = NULL;
257
258 /* Check for private cache location */
259 if (!(cache_private = context_find (nmhprivcache)))
260 cache_private = ".cache";
261 cache_private = getcpy (m_maildir (cache_private));
262
263 /*
264 * Cache the current directory before we do any chdirs()'s.
265 */
266 cwd = getcpy (pwd());
267
268 /*
269 * Check for storage directory. If specified,
270 * then store temporary files there. Else we
271 * store them in standard nmh directory.
272 */
273 if ((cp = context_find (nmhstorage)) && *cp)
274 tmp = concat (cp, "/", invo_name, NULL);
275 else
276 tmp = add (m_maildir (invo_name), NULL);
277
278 if (!context_find ("path"))
279 free (path ("./", TFOLDER));
280
281 if (file && msgs.size)
282 adios (NULL, "cannot specify msg and file at same time!");
283
284 /*
285 * check if message is coming from file
286 */
287 if (file) {
288 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
289 adios (NULL, "out of memory");
290 ctp = cts;
291
292 if ((ct = parse_mime (file)))
293 *ctp++ = ct;
294 } else {
295 /*
296 * message(s) are coming from a folder
297 */
298 if (!msgs.size)
299 app_msgarg(&msgs, "cur");
300 if (!folder)
301 folder = getfolder (1);
302 maildir = m_maildir (folder);
303
304 if (chdir (maildir) == NOTOK)
305 adios (maildir, "unable to change directory to");
306
307 /* read folder and create message structure */
308 if (!(mp = folder_read (folder)))
309 adios (NULL, "unable to read folder %s", folder);
310
311 /* check for empty folder */
312 if (mp->nummsg == 0)
313 adios (NULL, "no messages in %s", folder);
314
315 /* parse all the message ranges/sequences and set SELECTED */
316 for (msgnum = 0; msgnum < msgs.size; msgnum++)
317 if (!m_convert (mp, msgs.msgs[msgnum]))
318 done (1);
319 seq_setprev (mp); /* set the previous-sequence */
320
321 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
322 adios (NULL, "out of memory");
323 ctp = cts;
324
325 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
326 if (is_selected(mp, msgnum)) {
327 char *msgnam;
328
329 msgnam = m_name (msgnum);
330 if ((ct = parse_mime (msgnam)))
331 *ctp++ = ct;
332 }
333 }
334 }
335
336 if (!*cts)
337 done (1);
338
339 userrs = 1;
340 SIGNAL (SIGQUIT, quitser);
341 SIGNAL (SIGPIPE, pipeser);
342
343 /*
344 * Get the associated umask for the relevant contents.
345 */
346 for (ctp = cts; *ctp; ctp++) {
347 struct stat st;
348
349 ct = *ctp;
350 if (type_ok (ct, 1) && !ct->c_umask) {
351 if (stat (ct->c_file, &st) != NOTOK)
352 ct->c_umask = ~(st.st_mode & 0777);
353 else
354 ct->c_umask = ~m_gmprot();
355 }
356 }
357
358 /*
359 * Store the message content
360 */
361 store_all_messages (cts);
362
363 /* Now free all the structures for the content */
364 for (ctp = cts; *ctp; ctp++)
365 free_content (*ctp);
366
367 free ((char *) 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 (0);
379 return 1;
380 }
381
382
383 static void
384 pipeser (int i)
385 {
386 if (i == SIGQUIT) {
387 unlink ("core");
388 fflush (stdout);
389 fprintf (stderr, "\n");
390 fflush (stderr);
391 }
392
393 done (1);
394 /* NOTREACHED */
395 }