1 /*
2 ** mhlist.c -- list the contents of MIME messages
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/signals.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <h/tws.h>
15 #include <h/mime.h>
16 #include <h/mhparse.h>
17 #include <h/utils.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <locale.h>
21 #include <sysexits.h>
22
23 static struct swit switches[] = {
24 #define VERBSW 0
25 { "verbose", 0 },
26 #define NVERBSW 1
27 { "noverbose", 2 },
28 #define FILESW 2 /* interface from show */
29 { "file file", 0 },
30 #define PARTSW 3
31 { "part number", 0 },
32 #define TYPESW 4
33 { "type content", 0 },
34 #define VERSIONSW 5
35 { "Version", 0 },
36 #define HELPSW 6
37 { "help", 0 },
38 #define DEBUGSW 7
39 { "debug", -5 },
40 { NULL, 0 }
41 };
42
43 char *version=VERSION;
44
45 /* mhparse.c */
46 extern char *tmp; /* directory to place temp files */
47
48 /* mhmisc.c */
49 extern int npart;
50 extern int ntype;
51 extern char *parts[NPARTS + 1];
52 extern char *types[NTYPES + 1];
53 extern int userrs;
54
55 /*
56 ** This is currently needed to keep mhparse happy.
57 ** This needs to be changed.
58 */
59 pid_t xpid = 0;
60
61 int debugsw = 0;
62 int verbosw = 0;
63
64 #define quitser pipeser
65
66 /* mhparse.c */
67 CT parse_mime(char *);
68
69 /* mhmisc.c */
70 int part_ok(CT, int);
71 int type_ok(CT, int);
72 void set_endian(void);
73 void flush_errors(void);
74
75 /* mhlistsbr.c */
76 void list_all_messages(CT *, int, int);
77
78 /* mhfree.c */
79 void free_content(CT);
80 extern CT *cts;
81 void freects_done();
82
83 /*
84 ** static prototypes
85 */
86 static void pipeser(int);
87
88
89 int
main(int argc,char ** argv)90 main(int argc, char **argv)
91 {
92 int msgnum;
93 char *cp, *file = NULL, *folder = NULL;
94 char *maildir, buf[100], **argp;
95 char **arguments;
96 struct msgs_array msgs = { 0, 0, NULL };
97 struct msgs *mp = NULL;
98 CT ct, *ctp;
99
100 if (atexit(freects_done) != 0) {
101 adios(EX_OSERR, NULL, "atexit failed");
102 }
103
104 setlocale(LC_ALL, "");
105 invo_name = mhbasename(argv[0]);
106
107 /* read user profile/context */
108 context_read();
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 exit(EX_USAGE);
122 case UNKWNSW:
123 adios(EX_USAGE, NULL, "-%s unknown", cp);
124
125 case HELPSW:
126 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
127 print_help(buf, switches, 1);
128 exit(argc == 2 ? EX_OK : EX_USAGE);
129 case VERSIONSW:
130 print_version(invo_name);
131 exit(argc == 2 ? EX_OK : EX_USAGE);
132
133 case PARTSW:
134 if (!(cp = *argp++) || *cp == '-')
135 adios(EX_USAGE, NULL, "missing argument to %s",
136 argp[-2]);
137 if (npart >= NPARTS)
138 adios(EX_USAGE, NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
139 parts[npart++] = cp;
140 continue;
141
142 case TYPESW:
143 if (!(cp = *argp++) || *cp == '-')
144 adios(EX_USAGE, NULL, "missing argument to %s",
145 argp[-2]);
146 if (ntype >= NTYPES)
147 adios(EX_USAGE, NULL, "too many types (starting with %s), %d max", cp, NTYPES);
148 types[ntype++] = cp;
149 continue;
150
151 case FILESW:
152 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
153 adios(EX_USAGE, NULL, "missing argument to %s",
154 argp[-2]);
155 file = *cp == '-' ? cp : mh_xstrdup(expanddir(cp));
156 continue;
157
158 case VERBSW:
159 verbosw = 1;
160 continue;
161 case NVERBSW:
162 verbosw = 0;
163 continue;
164 case DEBUGSW:
165 debugsw = 1;
166 continue;
167 }
168 }
169 if (*cp == '+' || *cp == '@') {
170 if (folder)
171 adios(EX_USAGE, NULL, "only one folder at a time!");
172 else
173 folder = mh_xstrdup(expandfol(cp));
174 } else
175 app_msgarg(&msgs, cp);
176 }
177
178 /* null terminate the list of acceptable parts/types */
179 parts[npart] = NULL;
180 types[ntype] = NULL;
181
182 set_endian();
183
184 /*
185 ** Check for storage directory. If specified,
186 ** then store temporary files there. Else we
187 ** store them in standard nmh directory.
188 */
189 if ((cp = context_find(nmhstorage)) && *cp)
190 tmp = concat(cp, "/", invo_name, NULL);
191 else
192 tmp = mh_xstrdup(toabsdir(invo_name));
193
194 if (file && msgs.size)
195 adios(EX_USAGE, NULL, "cannot specify msg and file at same time!");
196
197 /*
198 ** check if message is coming from file
199 */
200 if (file) {
201 cts = mh_xcalloc(2, sizeof(*cts));
202 ctp = cts;
203
204 if ((ct = parse_mime(file)))
205 *ctp++ = ct;
206 } else {
207 /*
208 ** message(s) are coming from a folder
209 */
210 if (!msgs.size)
211 app_msgarg(&msgs, seq_cur);
212 if (!folder)
213 folder = getcurfol();
214 maildir = toabsdir(folder);
215
216 if (chdir(maildir) == NOTOK)
217 adios(EX_OSERR, maildir, "unable to change directory to");
218
219 /* read folder and create message structure */
220 if (!(mp = folder_read(folder)))
221 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
222
223 /* check for empty folder */
224 if (mp->nummsg == 0)
225 adios(EX_DATAERR, NULL, "no messages in %s", folder);
226
227 /* parse all the message ranges/sequences and set SELECTED */
228 for (msgnum = 0; msgnum < msgs.size; msgnum++) {
229 if (!m_convert(mp, msgs.msgs[msgnum])) {
230 exit(EX_SOFTWARE);
231 }
232 }
233 seq_setprev(mp); /* set the previous-sequence */
234
235 cts = mh_xcalloc(mp->numsel + 1, sizeof(*cts));
236 ctp = cts;
237
238 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
239 if (is_selected(mp, msgnum)) {
240 char *msgnam;
241
242 msgnam = m_name(msgnum);
243 if ((ct = parse_mime(msgnam)))
244 *ctp++ = ct;
245 }
246 }
247 }
248
249 if (!*cts)
250 exit(EX_SOFTWARE);
251
252 userrs = 1;
253 SIGNAL(SIGQUIT, quitser);
254 SIGNAL(SIGPIPE, pipeser);
255
256 /*
257 ** Get the associated umask for the relevant contents.
258 */
259 for (ctp = cts; *ctp; ctp++) {
260 struct stat st;
261
262 ct = *ctp;
263 if (type_ok(ct, 1) && !ct->c_umask) {
264 if (stat(ct->c_file, &st) != NOTOK)
265 ct->c_umask = ~(st.st_mode & 0777);
266 else
267 ct->c_umask = ~m_gmprot();
268 }
269 }
270
271 /*
272 ** List the message content
273 */
274 list_all_messages(cts, verbosw, debugsw);
275
276 /* Now free all the structures for the content */
277 for (ctp = cts; *ctp; ctp++)
278 free_content(*ctp);
279
280 mh_free0(&cts);
281
282 /* If reading from a folder, do some updating */
283 if (mp) {
284 context_replace(curfolder, folder); /* update current folder */
285 seq_setcur(mp, mp->hghsel); /* update current message */
286 seq_save(mp); /* synchronize sequences */
287 context_save(); /* save the context file */
288 }
289
290 return EX_OK;
291 }
292
293
294 static void
pipeser(int i)295 pipeser(int i)
296 {
297 if (i == SIGQUIT) {
298 unlink("core");
299 fflush(stdout);
300 fprintf(stderr, "\n");
301 fflush(stderr);
302 }
303
304 _exit(EX_IOERR);
305 /* NOTREACHED */
306 }
307