support.c revision 1.19 1 1.19 christos /* $NetBSD: support.c,v 1.19 2007/10/23 14:58:45 christos Exp $ */
2 1.1 tv
3 1.1 tv /*
4 1.1 tv * Copyright (c) 1980, 1993
5 1.1 tv * The Regents of the University of California. All rights reserved.
6 1.1 tv *
7 1.1 tv * Redistribution and use in source and binary forms, with or without
8 1.1 tv * modification, are permitted provided that the following conditions
9 1.1 tv * are met:
10 1.1 tv * 1. Redistributions of source code must retain the above copyright
11 1.1 tv * notice, this list of conditions and the following disclaimer.
12 1.1 tv * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tv * notice, this list of conditions and the following disclaimer in the
14 1.1 tv * documentation and/or other materials provided with the distribution.
15 1.10 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 tv * may be used to endorse or promote products derived from this software
17 1.1 tv * without specific prior written permission.
18 1.1 tv *
19 1.1 tv * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 tv * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 tv * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 tv * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 tv * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 tv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 tv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 tv * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 tv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 tv * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 tv * SUCH DAMAGE.
30 1.1 tv */
31 1.1 tv
32 1.1 tv #include <sys/cdefs.h>
33 1.1 tv #ifndef lint
34 1.1 tv #if 0
35 1.1 tv static char sccsid[] = "@(#)aux.c 8.1 (Berkeley) 6/6/93";
36 1.1 tv #else
37 1.19 christos __RCSID("$NetBSD: support.c,v 1.19 2007/10/23 14:58:45 christos Exp $");
38 1.1 tv #endif
39 1.1 tv #endif /* not lint */
40 1.1 tv
41 1.17 christos #include <stdarg.h>
42 1.17 christos #include <util.h>
43 1.17 christos
44 1.1 tv #include "rcv.h"
45 1.1 tv #include "extern.h"
46 1.15 christos #include "mime.h"
47 1.17 christos #include "thread.h"
48 1.1 tv
49 1.1 tv /*
50 1.1 tv * Mail -- a mail program
51 1.1 tv *
52 1.1 tv * Auxiliary functions.
53 1.1 tv */
54 1.17 christos
55 1.17 christos
56 1.1 tv /*
57 1.1 tv * Return a pointer to a dynamic copy of the argument.
58 1.1 tv */
59 1.17 christos PUBLIC char *
60 1.2 wiz savestr(const char *str)
61 1.1 tv {
62 1.1 tv char *new;
63 1.14 christos size_t size = strlen(str) + 1;
64 1.1 tv
65 1.4 wiz if ((new = salloc(size)) != NULL)
66 1.14 christos (void)memmove(new, str, size);
67 1.1 tv return new;
68 1.1 tv }
69 1.1 tv
70 1.1 tv /*
71 1.1 tv * Make a copy of new argument incorporating old one.
72 1.1 tv */
73 1.1 tv static char *
74 1.2 wiz save2str(char *str, char *old)
75 1.1 tv {
76 1.1 tv char *new;
77 1.14 christos size_t newsize = strlen(str) + 1;
78 1.14 christos size_t oldsize = old ? strlen(old) + 1 : 0;
79 1.1 tv
80 1.4 wiz if ((new = salloc(newsize + oldsize)) != NULL) {
81 1.1 tv if (oldsize) {
82 1.14 christos (void)memmove(new, old, oldsize);
83 1.1 tv new[oldsize - 1] = ' ';
84 1.1 tv }
85 1.14 christos (void)memmove(new + oldsize, str, newsize);
86 1.1 tv }
87 1.1 tv return new;
88 1.1 tv }
89 1.1 tv
90 1.1 tv /*
91 1.17 christos * Like asprintf(), but with result salloc-ed rather than malloc-ed.
92 1.17 christos */
93 1.17 christos PUBLIC int
94 1.17 christos sasprintf(char **ret, const char *format, ...)
95 1.17 christos {
96 1.17 christos int rval;
97 1.17 christos char *p;
98 1.17 christos va_list args;
99 1.17 christos
100 1.17 christos va_start(args, format);
101 1.17 christos rval = evasprintf(&p, format, args);
102 1.17 christos *ret = savestr(p);
103 1.17 christos free(p);
104 1.17 christos va_end(args);
105 1.17 christos return rval;
106 1.17 christos }
107 1.17 christos
108 1.17 christos struct set_m_flag_args_s {
109 1.17 christos int and_bits;
110 1.17 christos int xor_bits;
111 1.17 christos };
112 1.17 christos static int
113 1.17 christos set_m_flag_core(struct message *mp, void *v)
114 1.17 christos {
115 1.17 christos struct set_m_flag_args_s *args;
116 1.17 christos args = v;
117 1.17 christos mp->m_flag &= args->and_bits;
118 1.17 christos mp->m_flag ^= args->xor_bits;
119 1.17 christos return 0;
120 1.17 christos }
121 1.17 christos
122 1.17 christos PUBLIC struct message *
123 1.17 christos set_m_flag(int msgnum, int and_bits, int xor_bits)
124 1.17 christos {
125 1.17 christos struct message *mp;
126 1.17 christos struct set_m_flag_args_s args;
127 1.17 christos mp = get_message(msgnum);
128 1.17 christos args.and_bits = and_bits;
129 1.17 christos args.xor_bits = xor_bits;
130 1.17 christos (void)thread_recursion(mp, set_m_flag_core, &args);
131 1.17 christos return mp;
132 1.17 christos }
133 1.17 christos
134 1.17 christos /*
135 1.1 tv * Touch the named message by setting its MTOUCH flag.
136 1.1 tv * Touched messages have the effect of not being sent
137 1.1 tv * back to the system mailbox on exit.
138 1.1 tv */
139 1.17 christos PUBLIC void
140 1.2 wiz touch(struct message *mp)
141 1.1 tv {
142 1.1 tv
143 1.1 tv mp->m_flag |= MTOUCH;
144 1.1 tv if ((mp->m_flag & MREAD) == 0)
145 1.1 tv mp->m_flag |= MREAD|MSTATUS;
146 1.1 tv }
147 1.1 tv
148 1.1 tv /*
149 1.1 tv * Test to see if the passed file name is a directory.
150 1.1 tv * Return true if it is.
151 1.1 tv */
152 1.17 christos PUBLIC int
153 1.13 christos isdir(const char name[])
154 1.1 tv {
155 1.1 tv struct stat sbuf;
156 1.1 tv
157 1.1 tv if (stat(name, &sbuf) < 0)
158 1.17 christos return 0;
159 1.17 christos return S_ISDIR(sbuf.st_mode);
160 1.1 tv }
161 1.1 tv
162 1.1 tv /*
163 1.1 tv * Count the number of arguments in the given string raw list.
164 1.1 tv */
165 1.17 christos PUBLIC int
166 1.2 wiz argcount(char **argv)
167 1.1 tv {
168 1.1 tv char **ap;
169 1.1 tv
170 1.19 christos for (ap = argv; *ap++ != NULL; /*EMPTY*/)
171 1.17 christos continue;
172 1.1 tv return ap - argv - 1;
173 1.1 tv }
174 1.1 tv
175 1.1 tv /*
176 1.17 christos * Check whether the passed line is a header line of
177 1.17 christos * the desired breed. Return the field body, or 0.
178 1.1 tv */
179 1.17 christos static char*
180 1.17 christos ishfield(const char linebuf[], char *colon, const char field[])
181 1.1 tv {
182 1.17 christos char *cp = colon;
183 1.15 christos
184 1.17 christos *cp = 0;
185 1.17 christos if (strcasecmp(linebuf, field) != 0) {
186 1.17 christos *cp = ':';
187 1.17 christos return 0;
188 1.1 tv }
189 1.17 christos *cp = ':';
190 1.19 christos for (cp++; is_WSP(*cp); cp++)
191 1.17 christos continue;
192 1.17 christos return cp;
193 1.1 tv }
194 1.1 tv
195 1.1 tv /*
196 1.1 tv * Return the next header field found in the given message.
197 1.1 tv * Return >= 0 if something found, < 0 elsewise.
198 1.1 tv * "colon" is set to point to the colon in the header.
199 1.1 tv * Must deal with \ continuations & other such fraud.
200 1.17 christos *
201 1.17 christos * WARNING - do not call this outside hfield() or decoding will not
202 1.17 christos * get done.
203 1.1 tv */
204 1.15 christos static int
205 1.2 wiz gethfield(FILE *f, char linebuf[], int rem, char **colon)
206 1.1 tv {
207 1.1 tv char line2[LINESIZE];
208 1.1 tv char *cp, *cp2;
209 1.1 tv int c;
210 1.1 tv
211 1.1 tv for (;;) {
212 1.1 tv if (--rem < 0)
213 1.1 tv return -1;
214 1.15 christos if ((c = mail_readline(f, linebuf, LINESIZE)) <= 0)
215 1.1 tv return -1;
216 1.19 christos for (cp = linebuf;
217 1.19 christos isprint((unsigned char)*cp) && *cp != ' ' && *cp != ':';
218 1.1 tv cp++)
219 1.17 christos continue;
220 1.1 tv if (*cp != ':' || cp == linebuf)
221 1.1 tv continue;
222 1.1 tv /*
223 1.1 tv * I guess we got a headline.
224 1.1 tv * Handle wraparounding
225 1.1 tv */
226 1.1 tv *colon = cp;
227 1.1 tv cp = linebuf + c;
228 1.1 tv for (;;) {
229 1.19 christos while (--cp >= linebuf && is_WSP(*cp))
230 1.17 christos continue;
231 1.1 tv cp++;
232 1.1 tv if (rem <= 0)
233 1.1 tv break;
234 1.14 christos (void)ungetc(c = getc(f), f);
235 1.19 christos if (!is_WSP(c))
236 1.1 tv break;
237 1.15 christos if ((c = mail_readline(f, line2, LINESIZE)) < 0)
238 1.1 tv break;
239 1.1 tv rem--;
240 1.19 christos cp2 = skip_WSP(line2);
241 1.1 tv c -= cp2 - line2;
242 1.1 tv if (cp + c >= linebuf + LINESIZE - 2)
243 1.1 tv break;
244 1.1 tv *cp++ = ' ';
245 1.14 christos (void)memmove(cp, cp2, (size_t)c);
246 1.1 tv cp += c;
247 1.1 tv }
248 1.1 tv *cp = 0;
249 1.1 tv return rem;
250 1.1 tv }
251 1.1 tv /* NOTREACHED */
252 1.1 tv }
253 1.1 tv
254 1.1 tv /*
255 1.17 christos * Return the desired header line from the passed message
256 1.17 christos * pointer (or NULL if the desired header field is not available).
257 1.1 tv */
258 1.17 christos PUBLIC char *
259 1.17 christos hfield(const char field[], const struct message *mp)
260 1.17 christos {
261 1.17 christos FILE *ibuf;
262 1.17 christos char linebuf[LINESIZE];
263 1.17 christos int lc;
264 1.17 christos char *headerfield;
265 1.17 christos char *colon, *oldhfield = NULL;
266 1.17 christos #ifdef MIME_SUPPORT
267 1.17 christos int decode;
268 1.1 tv
269 1.17 christos decode = value(ENAME_MIME_DECODE_MSG) &&
270 1.17 christos value(ENAME_MIME_DECODE_HDR);
271 1.17 christos #endif
272 1.1 tv
273 1.17 christos ibuf = setinput(mp);
274 1.17 christos if ((lc = mp->m_lines - 1) < 0)
275 1.17 christos return NULL;
276 1.17 christos if (mail_readline(ibuf, linebuf, LINESIZE) < 0)
277 1.17 christos return NULL;
278 1.17 christos while (lc > 0) {
279 1.17 christos if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
280 1.17 christos return oldhfield;
281 1.17 christos #ifdef MIME_SUPPORT
282 1.17 christos if ((headerfield = ishfield(linebuf, colon, field)) != NULL) {
283 1.17 christos char linebuf2[LINESIZE];
284 1.17 christos if (decode && colon)
285 1.17 christos headerfield = mime_decode_hfield(linebuf2, sizeof(linebuf2), headerfield);
286 1.17 christos oldhfield = save2str(headerfield, oldhfield);
287 1.17 christos }
288 1.17 christos #else
289 1.17 christos if ((headerfield = ishfield(linebuf, colon, field)) != NULL)
290 1.17 christos oldhfield = save2str(headerfield, oldhfield);
291 1.17 christos #endif
292 1.1 tv }
293 1.17 christos return oldhfield;
294 1.1 tv }
295 1.1 tv
296 1.1 tv /*
297 1.1 tv * Copy a string, lowercasing it as we go.
298 1.1 tv */
299 1.17 christos PUBLIC void
300 1.13 christos istrcpy(char *dest, const char *src)
301 1.1 tv {
302 1.1 tv
303 1.1 tv do {
304 1.12 dsl *dest++ = tolower((unsigned char)*src);
305 1.1 tv } while (*src++ != 0);
306 1.1 tv }
307 1.1 tv
308 1.1 tv /*
309 1.1 tv * The following code deals with input stacking to do source
310 1.1 tv * commands. All but the current file pointer are saved on
311 1.1 tv * the stack.
312 1.1 tv */
313 1.1 tv
314 1.1 tv static int ssp; /* Top of file stack */
315 1.17 christos static struct sstack {
316 1.1 tv FILE *s_file; /* File we were in. */
317 1.1 tv int s_cond; /* Saved state of conditionals */
318 1.17 christos #ifdef NEW_CONDITIONAL
319 1.17 christos struct cond_stack_s *s_cond_stack; /* Saved conditional stack */
320 1.17 christos #endif
321 1.1 tv int s_loading; /* Loading .mailrc, etc. */
322 1.1 tv } sstack[NOFILE];
323 1.1 tv
324 1.1 tv /*
325 1.1 tv * Pushdown current input file and switch to a new one.
326 1.1 tv * Set the global flag "sourcing" so that others will realize
327 1.1 tv * that they are no longer reading from a tty (in all probability).
328 1.1 tv */
329 1.17 christos PUBLIC int
330 1.2 wiz source(void *v)
331 1.1 tv {
332 1.1 tv char **arglist = v;
333 1.1 tv FILE *fi;
334 1.13 christos const char *cp;
335 1.1 tv
336 1.4 wiz if ((cp = expand(*arglist)) == NULL)
337 1.17 christos return 1;
338 1.1 tv if ((fi = Fopen(cp, "r")) == NULL) {
339 1.9 wiz warn("%s", cp);
340 1.17 christos return 1;
341 1.1 tv }
342 1.1 tv if (ssp >= NOFILE - 1) {
343 1.14 christos (void)printf("Too much \"sourcing\" going on.\n");
344 1.14 christos (void)Fclose(fi);
345 1.17 christos return 1;
346 1.1 tv }
347 1.1 tv sstack[ssp].s_file = input;
348 1.1 tv sstack[ssp].s_cond = cond;
349 1.17 christos #ifdef NEW_CONDITIONAL
350 1.17 christos sstack[ssp].s_cond_stack = cond_stack;
351 1.17 christos #endif
352 1.1 tv sstack[ssp].s_loading = loading;
353 1.1 tv ssp++;
354 1.1 tv loading = 0;
355 1.17 christos cond = CNONE;
356 1.1 tv input = fi;
357 1.1 tv sourcing++;
358 1.17 christos return 0;
359 1.1 tv }
360 1.1 tv
361 1.1 tv /*
362 1.1 tv * Pop the current input back to the previous level.
363 1.1 tv * Update the "sourcing" flag as appropriate.
364 1.1 tv */
365 1.17 christos PUBLIC int
366 1.2 wiz unstack(void)
367 1.1 tv {
368 1.1 tv if (ssp <= 0) {
369 1.14 christos (void)printf("\"Source\" stack over-pop.\n");
370 1.1 tv sourcing = 0;
371 1.17 christos return 1;
372 1.1 tv }
373 1.14 christos (void)Fclose(input);
374 1.17 christos if (cond != CNONE || cond_stack != NULL)
375 1.14 christos (void)printf("Unmatched \"if\"\n");
376 1.1 tv ssp--;
377 1.17 christos input = sstack[ssp].s_file;
378 1.1 tv cond = sstack[ssp].s_cond;
379 1.17 christos #ifdef NEW_CONDITIONAL
380 1.17 christos cond_stack = sstack[ssp].s_cond_stack;
381 1.17 christos #endif
382 1.1 tv loading = sstack[ssp].s_loading;
383 1.1 tv if (ssp == 0)
384 1.1 tv sourcing = loading;
385 1.17 christos return 0;
386 1.1 tv }
387 1.1 tv
388 1.1 tv /*
389 1.1 tv * Touch the indicated file.
390 1.1 tv * This is nifty for the shell.
391 1.1 tv */
392 1.17 christos PUBLIC void
393 1.2 wiz alter(char *name)
394 1.1 tv {
395 1.1 tv struct stat sb;
396 1.1 tv struct timeval tv[2];
397 1.1 tv
398 1.1 tv if (stat(name, &sb))
399 1.1 tv return;
400 1.8 wiz (void)gettimeofday(&tv[0], NULL);
401 1.1 tv tv[0].tv_sec++;
402 1.1 tv TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
403 1.8 wiz (void)utimes(name, tv);
404 1.1 tv }
405 1.1 tv
406 1.1 tv /*
407 1.1 tv * Examine the passed line buffer and
408 1.1 tv * return true if it is all blanks and tabs.
409 1.1 tv */
410 1.17 christos PUBLIC int
411 1.2 wiz blankline(char linebuf[])
412 1.1 tv {
413 1.1 tv char *cp;
414 1.1 tv
415 1.1 tv for (cp = linebuf; *cp; cp++)
416 1.19 christos if (!is_WSP(*cp))
417 1.17 christos return 0;
418 1.17 christos return 1;
419 1.1 tv }
420 1.1 tv
421 1.1 tv /*
422 1.1 tv * Start of a "comment".
423 1.1 tv * Ignore it.
424 1.1 tv */
425 1.17 christos static char *
426 1.2 wiz skip_comment(char *cp)
427 1.1 tv {
428 1.1 tv int nesting = 1;
429 1.1 tv
430 1.17 christos for (/*EMPTY*/; nesting > 0 && *cp; cp++) {
431 1.1 tv switch (*cp) {
432 1.1 tv case '\\':
433 1.1 tv if (cp[1])
434 1.1 tv cp++;
435 1.1 tv break;
436 1.1 tv case '(':
437 1.1 tv nesting++;
438 1.1 tv break;
439 1.1 tv case ')':
440 1.1 tv nesting--;
441 1.1 tv break;
442 1.1 tv }
443 1.1 tv }
444 1.1 tv return cp;
445 1.1 tv }
446 1.1 tv
447 1.1 tv /*
448 1.1 tv * Skin an arpa net address according to the RFC 822 interpretation
449 1.1 tv * of "host-phrase."
450 1.1 tv */
451 1.17 christos PUBLIC char *
452 1.2 wiz skin(char *name)
453 1.1 tv {
454 1.1 tv int c;
455 1.1 tv char *cp, *cp2;
456 1.1 tv char *bufend;
457 1.1 tv int gotlt, lastsp;
458 1.17 christos char nbuf[LINESIZE];
459 1.1 tv
460 1.4 wiz if (name == NULL)
461 1.17 christos return NULL;
462 1.4 wiz if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
463 1.4 wiz && strchr(name, ' ') == NULL)
464 1.17 christos return name;
465 1.1 tv gotlt = 0;
466 1.1 tv lastsp = 0;
467 1.1 tv bufend = nbuf;
468 1.19 christos for (cp = name, cp2 = bufend; (c = *cp++) != '\0'; /*EMPTY*/) {
469 1.1 tv switch (c) {
470 1.1 tv case '(':
471 1.1 tv cp = skip_comment(cp);
472 1.1 tv lastsp = 0;
473 1.1 tv break;
474 1.1 tv
475 1.1 tv case '"':
476 1.1 tv /*
477 1.1 tv * Start of a "quoted-string".
478 1.1 tv * Copy it in its entirety.
479 1.1 tv */
480 1.1 tv while ((c = *cp) != '\0') {
481 1.1 tv cp++;
482 1.1 tv if (c == '"')
483 1.1 tv break;
484 1.1 tv if (c != '\\')
485 1.1 tv *cp2++ = c;
486 1.1 tv else if ((c = *cp) != '\0') {
487 1.1 tv *cp2++ = c;
488 1.1 tv cp++;
489 1.1 tv }
490 1.1 tv }
491 1.1 tv lastsp = 0;
492 1.1 tv break;
493 1.1 tv
494 1.1 tv case ' ':
495 1.1 tv if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
496 1.1 tv cp += 3, *cp2++ = '@';
497 1.1 tv else
498 1.1 tv if (cp[0] == '@' && cp[1] == ' ')
499 1.1 tv cp += 2, *cp2++ = '@';
500 1.1 tv else
501 1.1 tv lastsp = 1;
502 1.1 tv break;
503 1.1 tv
504 1.1 tv case '<':
505 1.1 tv cp2 = bufend;
506 1.1 tv gotlt++;
507 1.1 tv lastsp = 0;
508 1.1 tv break;
509 1.1 tv
510 1.1 tv case '>':
511 1.1 tv if (gotlt) {
512 1.1 tv gotlt = 0;
513 1.1 tv while ((c = *cp) && c != ',') {
514 1.1 tv cp++;
515 1.1 tv if (c == '(')
516 1.1 tv cp = skip_comment(cp);
517 1.1 tv else if (c == '"')
518 1.1 tv while ((c = *cp) != '\0') {
519 1.1 tv cp++;
520 1.1 tv if (c == '"')
521 1.1 tv break;
522 1.1 tv if (c == '\\' && *cp)
523 1.1 tv cp++;
524 1.1 tv }
525 1.1 tv }
526 1.1 tv lastsp = 0;
527 1.1 tv break;
528 1.1 tv }
529 1.14 christos /* FALLTHROUGH */
530 1.1 tv
531 1.1 tv default:
532 1.1 tv if (lastsp) {
533 1.1 tv lastsp = 0;
534 1.1 tv *cp2++ = ' ';
535 1.1 tv }
536 1.1 tv *cp2++ = c;
537 1.1 tv if (c == ',' && !gotlt) {
538 1.1 tv *cp2++ = ' ';
539 1.17 christos for (/*EMPTY*/; *cp == ' '; cp++)
540 1.17 christos continue;
541 1.1 tv lastsp = 0;
542 1.1 tv bufend = cp2;
543 1.1 tv }
544 1.1 tv }
545 1.1 tv }
546 1.1 tv *cp2 = 0;
547 1.1 tv
548 1.17 christos return savestr(nbuf);
549 1.1 tv }
550 1.1 tv
551 1.1 tv /*
552 1.1 tv * Fetch the sender's name from the passed message.
553 1.1 tv * Reptype can be
554 1.1 tv * 0 -- get sender's name for display purposes
555 1.1 tv * 1 -- get sender's name for reply
556 1.1 tv * 2 -- get sender's name for Reply
557 1.1 tv */
558 1.17 christos static char *
559 1.2 wiz name1(struct message *mp, int reptype)
560 1.1 tv {
561 1.1 tv char namebuf[LINESIZE];
562 1.1 tv char linebuf[LINESIZE];
563 1.1 tv char *cp, *cp2;
564 1.1 tv FILE *ibuf;
565 1.3 wiz int firstrun = 1;
566 1.1 tv
567 1.4 wiz if ((cp = hfield("from", mp)) != NULL)
568 1.1 tv return cp;
569 1.4 wiz if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
570 1.1 tv return cp;
571 1.1 tv ibuf = setinput(mp);
572 1.1 tv namebuf[0] = '\0';
573 1.15 christos if (mail_readline(ibuf, linebuf, LINESIZE) < 0)
574 1.17 christos return savestr(namebuf);
575 1.17 christos newname:
576 1.1 tv for (cp = linebuf; *cp && *cp != ' '; cp++)
577 1.17 christos continue;
578 1.19 christos cp = skip_WSP(cp);
579 1.1 tv for (cp2 = &namebuf[strlen(namebuf)];
580 1.19 christos *cp && !is_WSP(*cp) && cp2 < namebuf + LINESIZE - 1;
581 1.19 christos /*EMPTY*/)
582 1.1 tv *cp2++ = *cp++;
583 1.1 tv *cp2 = '\0';
584 1.15 christos if (mail_readline(ibuf, linebuf, LINESIZE) < 0)
585 1.17 christos return savestr(namebuf);
586 1.1 tv if ((cp = strchr(linebuf, 'F')) == NULL)
587 1.17 christos return savestr(namebuf);
588 1.1 tv if (strncmp(cp, "From", 4) != 0)
589 1.17 christos return savestr(namebuf);
590 1.1 tv while ((cp = strchr(cp, 'r')) != NULL) {
591 1.1 tv if (strncmp(cp, "remote", 6) == 0) {
592 1.1 tv if ((cp = strchr(cp, 'f')) == NULL)
593 1.1 tv break;
594 1.1 tv if (strncmp(cp, "from", 4) != 0)
595 1.1 tv break;
596 1.1 tv if ((cp = strchr(cp, ' ')) == NULL)
597 1.1 tv break;
598 1.1 tv cp++;
599 1.3 wiz if (firstrun) {
600 1.1 tv cp2 = namebuf;
601 1.3 wiz firstrun = 0;
602 1.1 tv } else
603 1.1 tv cp2 = strrchr(namebuf, '!') + 1;
604 1.1 tv while (*cp && cp2 < namebuf + LINESIZE - 1)
605 1.1 tv *cp2++ = *cp++;
606 1.1 tv if (cp2 < namebuf + LINESIZE - 1)
607 1.1 tv *cp2++ = '!';
608 1.1 tv *cp2 = '\0';
609 1.1 tv if (cp2 < namebuf + LINESIZE - 1)
610 1.1 tv goto newname;
611 1.1 tv else
612 1.1 tv break;
613 1.1 tv }
614 1.1 tv cp++;
615 1.1 tv }
616 1.17 christos return savestr(namebuf);
617 1.1 tv }
618 1.1 tv
619 1.1 tv /*
620 1.15 christos * Count the occurrences of c in str
621 1.1 tv */
622 1.17 christos static int
623 1.2 wiz charcount(char *str, int c)
624 1.1 tv {
625 1.1 tv char *cp;
626 1.1 tv int i;
627 1.1 tv
628 1.1 tv for (i = 0, cp = str; *cp; cp++)
629 1.1 tv if (*cp == c)
630 1.1 tv i++;
631 1.17 christos return i;
632 1.1 tv }
633 1.1 tv
634 1.1 tv /*
635 1.17 christos * Get sender's name from this message. If the message has
636 1.17 christos * a bunch of arpanet stuff in it, we may have to skin the name
637 1.17 christos * before returning it.
638 1.1 tv */
639 1.17 christos PUBLIC char *
640 1.17 christos nameof(struct message *mp, int reptype)
641 1.1 tv {
642 1.17 christos char *cp, *cp2;
643 1.1 tv
644 1.17 christos cp = skin(name1(mp, reptype));
645 1.17 christos if (reptype != 0 || charcount(cp, '!') < 2)
646 1.17 christos return cp;
647 1.17 christos cp2 = strrchr(cp, '!');
648 1.17 christos cp2--;
649 1.17 christos while (cp2 > cp && *cp2 != '!')
650 1.17 christos cp2--;
651 1.17 christos if (*cp2 == '!')
652 1.17 christos return cp2 + 1;
653 1.17 christos return cp;
654 1.1 tv }
655 1.1 tv
656 1.1 tv /*
657 1.1 tv * Copy s1 to s2, return pointer to null in s2.
658 1.1 tv */
659 1.17 christos PUBLIC char *
660 1.2 wiz copy(char *s1, char *s2)
661 1.1 tv {
662 1.1 tv
663 1.1 tv while ((*s2++ = *s1++) != '\0')
664 1.17 christos continue;
665 1.1 tv return s2 - 1;
666 1.1 tv }
667 1.1 tv
668 1.1 tv /*
669 1.17 christos * The core routine to check the ignore table for a field.
670 1.17 christos * Note: realfield should be lower-cased!
671 1.17 christos */
672 1.17 christos PUBLIC int
673 1.17 christos member(char *realfield, struct ignoretab *table)
674 1.17 christos {
675 1.17 christos struct ignore *igp;
676 1.17 christos
677 1.17 christos for (igp = table->i_head[hash(realfield)]; igp != 0; igp = igp->i_link)
678 1.17 christos if (*igp->i_field == *realfield &&
679 1.17 christos equal(igp->i_field, realfield))
680 1.17 christos return 1;
681 1.17 christos return 0;
682 1.17 christos }
683 1.17 christos
684 1.17 christos /*
685 1.1 tv * See if the given header field is supposed to be ignored.
686 1.1 tv */
687 1.17 christos PUBLIC int
688 1.13 christos isign(const char *field, struct ignoretab ignoretabs[2])
689 1.1 tv {
690 1.1 tv char realfld[LINESIZE];
691 1.1 tv
692 1.3 wiz if (ignoretabs == ignoreall)
693 1.1 tv return 1;
694 1.1 tv /*
695 1.1 tv * Lower-case the string, so that "Status" and "status"
696 1.1 tv * will hash to the same place.
697 1.1 tv */
698 1.1 tv istrcpy(realfld, field);
699 1.3 wiz if (ignoretabs[1].i_count > 0)
700 1.17 christos return !member(realfld, ignoretabs + 1);
701 1.1 tv else
702 1.17 christos return member(realfld, ignoretabs);
703 1.1 tv }
704 1.18 christos
705 1.18 christos /*
706 1.18 christos * Write a file to stdout, skipping comment lines.
707 1.18 christos */
708 1.18 christos PUBLIC void
709 1.18 christos cathelp(const char *fname)
710 1.18 christos {
711 1.18 christos char *line;
712 1.18 christos FILE *f;
713 1.18 christos size_t len;
714 1.18 christos
715 1.18 christos if ((f = Fopen(fname, "r")) == NULL) {
716 1.18 christos warn(fname);
717 1.18 christos return;
718 1.18 christos }
719 1.18 christos while ((line = fgetln(f, &len)) != NULL) {
720 1.18 christos if (*line == '#')
721 1.18 christos continue;
722 1.18 christos if (fwrite(line, 1, len, stdout) != len)
723 1.18 christos break;
724 1.18 christos }
725 1.18 christos (void)Fclose(f);
726 1.18 christos return;
727 1.18 christos }
728