]> diplodocus.org Git - nmh/blob - uip/mhmisc.c
Use pref_encoding() to select the default encoding for all headers.
[nmh] / uip / mhmisc.c
1
2 /*
3 * mhparse.c -- misc routines to process 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 <h/mime.h>
12 #include <h/mhparse.h>
13 #include <h/utils.h>
14
15 extern int debugsw;
16
17 /*
18 * limit actions to specified parts or content types
19 */
20 int npart = 0;
21 int ntype = 0;
22 char *parts[NPARTS + 1];
23 char *types[NTYPES + 1];
24
25 int userrs = 0;
26
27 static char *errs = NULL;
28
29
30 /*
31 * prototypes
32 */
33 int part_ok (CT, int);
34 int type_ok (CT, int);
35 void content_error (char *, CT, char *, ...);
36 void flush_errors (void);
37
38
39 int
40 part_ok (CT ct, int sP)
41 {
42 char **ap;
43 int len;
44
45 if (npart == 0 || (ct->c_type == CT_MULTIPART && (sP || ct->c_subtype)))
46 return 1;
47
48 for (ap = parts; *ap; ap++) {
49 len = strlen(*ap);
50 if (!strncmp (*ap, ct->c_partno, len) &&
51 (!ct->c_partno[len] || ct->c_partno[len] == '.' ))
52 return 1;
53 }
54
55 return 0;
56 }
57
58
59 int
60 type_ok (CT ct, int sP)
61 {
62 char **ap;
63 char buffer[BUFSIZ];
64 CI ci = &ct->c_ctinfo;
65
66 if (ntype == 0 || (ct->c_type == CT_MULTIPART && (sP || ct->c_subtype)))
67 return 1;
68
69 snprintf (buffer, sizeof(buffer), "%s/%s", ci->ci_type, ci->ci_subtype);
70 for (ap = types; *ap; ap++)
71 if (!strcasecmp (*ap, ci->ci_type) || !strcasecmp (*ap, buffer))
72 return 1;
73
74 return 0;
75 }
76
77
78 int
79 make_intermediates (char *file)
80 {
81 char *cp;
82
83 for (cp = file + 1; (cp = strchr(cp, '/')); cp++) {
84 struct stat st;
85
86 *cp = '\0';
87 if (stat (file, &st) == NOTOK) {
88 int answer;
89 char *ep;
90 if (errno != ENOENT) {
91 advise (file, "error on directory");
92 losing_directory:
93 *cp = '/';
94 return NOTOK;
95 }
96
97 ep = concat ("Create directory \"", file, "\"? ", NULL);
98 answer = getanswer (ep);
99 free (ep);
100
101 if (!answer)
102 goto losing_directory;
103 if (!makedir (file)) {
104 advise (NULL, "unable to create directory %s", file);
105 goto losing_directory;
106 }
107 }
108
109 *cp = '/';
110 }
111
112 return OK;
113 }
114
115
116 /*
117 * Construct error message for content
118 */
119
120 void
121 content_error (char *what, CT ct, char *fmt, ...)
122 {
123 va_list arglist;
124 int i, len, buflen;
125 char *bp, buffer[BUFSIZ];
126 CI ci;
127
128 bp = buffer;
129 buflen = sizeof(buffer);
130
131 if (userrs && invo_name && *invo_name) {
132 snprintf (bp, buflen, "%s: ", invo_name);
133 len = strlen (bp);
134 bp += len;
135 buflen -= len;
136 }
137
138 va_start (arglist, fmt);
139
140 vsnprintf (bp, buflen, fmt, arglist);
141 len = strlen (bp);
142 bp += len;
143 buflen -= len;
144
145 ci = &ct->c_ctinfo;
146
147 if (what) {
148 char *s;
149
150 if (*what) {
151 snprintf (bp, buflen, " %s: ", what);
152 len = strlen (bp);
153 bp += len;
154 buflen -= len;
155 }
156
157 if ((s = strerror (errno)))
158 snprintf (bp, buflen, "%s", s);
159 else
160 snprintf (bp, buflen, "Error %d", errno);
161
162 len = strlen (bp);
163 bp += len;
164 buflen -= len;
165 }
166
167 i = strlen (invo_name) + 2;
168
169 /* Now add content type and subtype */
170 snprintf (bp, buflen, "\n%*.*s(content %s/%s", i, i, "",
171 ci->ci_type, ci->ci_subtype);
172 len = strlen (bp);
173 bp += len;
174 buflen -= len;
175
176 /* Now add the message/part number */
177 if (ct->c_file) {
178 snprintf (bp, buflen, " in message %s", ct->c_file);
179 len = strlen (bp);
180 bp += len;
181 buflen -= len;
182
183 if (ct->c_partno) {
184 snprintf (bp, buflen, ", part %s", ct->c_partno);
185 len = strlen (bp);
186 bp += len;
187 buflen -= len;
188 }
189 }
190
191 snprintf (bp, buflen, ")");
192 len = strlen (bp);
193 bp += len;
194 buflen -= len;
195
196 if (userrs) {
197 *bp++ = '\n';
198 *bp = '\0';
199 buflen--;
200
201 errs = add (buffer, errs);
202 } else {
203 advise (NULL, "%s", buffer);
204 }
205 }
206
207
208 void
209 flush_errors (void)
210 {
211 if (errs) {
212 fflush (stdout);
213 fprintf (stderr, "%s", errs);
214 free (errs);
215 errs = NULL;
216 }
217 }