support.c revision 1.27 1 1.27 shm /* $NetBSD: support.c,v 1.27 2023/09/08 20:46:45 shm 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.27 shm __RCSID("$NetBSD: support.c,v 1.27 2023/09/08 20:46:45 shm 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.22 christos return (int)(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.21 christos * the desired breed. Return the field body, or NULL.
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.21 christos return NULL;
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.22 christos if ((c = readline(f, linebuf, LINESIZE, 0)) <= 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.22 christos if ((c = readline(f, line2, LINESIZE, 0)) < 0)
238 1.1 tv break;
239 1.1 tv rem--;
240 1.19 christos cp2 = skip_WSP(line2);
241 1.22 christos c -= (int)(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.22 christos if ((lc = (int)(mp->m_lines - 1)) < 0)
275 1.17 christos return NULL;
276 1.22 christos if (readline(ibuf, linebuf, LINESIZE, 0) < 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.21 christos if (decode)
285 1.21 christos headerfield = mime_decode_hfield(linebuf2,
286 1.21 christos sizeof(linebuf2), linebuf, headerfield);
287 1.17 christos oldhfield = save2str(headerfield, oldhfield);
288 1.17 christos }
289 1.17 christos #else
290 1.17 christos if ((headerfield = ishfield(linebuf, colon, field)) != NULL)
291 1.17 christos oldhfield = save2str(headerfield, oldhfield);
292 1.17 christos #endif
293 1.1 tv }
294 1.17 christos return oldhfield;
295 1.1 tv }
296 1.1 tv
297 1.1 tv /*
298 1.1 tv * Copy a string, lowercasing it as we go.
299 1.1 tv */
300 1.17 christos PUBLIC void
301 1.13 christos istrcpy(char *dest, const char *src)
302 1.1 tv {
303 1.1 tv
304 1.1 tv do {
305 1.12 dsl *dest++ = tolower((unsigned char)*src);
306 1.1 tv } while (*src++ != 0);
307 1.1 tv }
308 1.1 tv
309 1.1 tv /*
310 1.1 tv * The following code deals with input stacking to do source
311 1.1 tv * commands. All but the current file pointer are saved on
312 1.1 tv * the stack.
313 1.1 tv */
314 1.1 tv
315 1.1 tv static int ssp; /* Top of file stack */
316 1.17 christos static struct sstack {
317 1.1 tv FILE *s_file; /* File we were in. */
318 1.1 tv int s_cond; /* Saved state of conditionals */
319 1.17 christos #ifdef NEW_CONDITIONAL
320 1.17 christos struct cond_stack_s *s_cond_stack; /* Saved conditional stack */
321 1.17 christos #endif
322 1.1 tv int s_loading; /* Loading .mailrc, etc. */
323 1.1 tv } sstack[NOFILE];
324 1.1 tv
325 1.1 tv /*
326 1.1 tv * Pushdown current input file and switch to a new one.
327 1.1 tv * Set the global flag "sourcing" so that others will realize
328 1.1 tv * that they are no longer reading from a tty (in all probability).
329 1.1 tv */
330 1.17 christos PUBLIC int
331 1.2 wiz source(void *v)
332 1.1 tv {
333 1.1 tv char **arglist = v;
334 1.1 tv FILE *fi;
335 1.13 christos const char *cp;
336 1.1 tv
337 1.4 wiz if ((cp = expand(*arglist)) == NULL)
338 1.17 christos return 1;
339 1.25 christos if ((fi = Fopen(cp, "ref")) == NULL) {
340 1.9 wiz warn("%s", cp);
341 1.17 christos return 1;
342 1.1 tv }
343 1.1 tv if (ssp >= NOFILE - 1) {
344 1.14 christos (void)printf("Too much \"sourcing\" going on.\n");
345 1.14 christos (void)Fclose(fi);
346 1.17 christos return 1;
347 1.1 tv }
348 1.1 tv sstack[ssp].s_file = input;
349 1.1 tv sstack[ssp].s_cond = cond;
350 1.17 christos #ifdef NEW_CONDITIONAL
351 1.17 christos sstack[ssp].s_cond_stack = cond_stack;
352 1.17 christos #endif
353 1.1 tv sstack[ssp].s_loading = loading;
354 1.1 tv ssp++;
355 1.1 tv loading = 0;
356 1.17 christos cond = CNONE;
357 1.1 tv input = fi;
358 1.1 tv sourcing++;
359 1.17 christos return 0;
360 1.1 tv }
361 1.1 tv
362 1.1 tv /*
363 1.1 tv * Pop the current input back to the previous level.
364 1.1 tv * Update the "sourcing" flag as appropriate.
365 1.1 tv */
366 1.17 christos PUBLIC int
367 1.2 wiz unstack(void)
368 1.1 tv {
369 1.1 tv if (ssp <= 0) {
370 1.14 christos (void)printf("\"Source\" stack over-pop.\n");
371 1.1 tv sourcing = 0;
372 1.17 christos return 1;
373 1.1 tv }
374 1.14 christos (void)Fclose(input);
375 1.17 christos if (cond != CNONE || cond_stack != NULL)
376 1.14 christos (void)printf("Unmatched \"if\"\n");
377 1.1 tv ssp--;
378 1.17 christos input = sstack[ssp].s_file;
379 1.1 tv cond = sstack[ssp].s_cond;
380 1.17 christos #ifdef NEW_CONDITIONAL
381 1.17 christos cond_stack = sstack[ssp].s_cond_stack;
382 1.17 christos #endif
383 1.1 tv loading = sstack[ssp].s_loading;
384 1.1 tv if (ssp == 0)
385 1.1 tv sourcing = loading;
386 1.17 christos return 0;
387 1.1 tv }
388 1.1 tv
389 1.1 tv /*
390 1.1 tv * Touch the indicated file.
391 1.1 tv * This is nifty for the shell.
392 1.1 tv */
393 1.17 christos PUBLIC void
394 1.2 wiz alter(char *name)
395 1.1 tv {
396 1.1 tv struct stat sb;
397 1.1 tv struct timeval tv[2];
398 1.1 tv
399 1.1 tv if (stat(name, &sb))
400 1.1 tv return;
401 1.8 wiz (void)gettimeofday(&tv[0], NULL);
402 1.1 tv tv[0].tv_sec++;
403 1.1 tv TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
404 1.8 wiz (void)utimes(name, tv);
405 1.1 tv }
406 1.1 tv
407 1.1 tv /*
408 1.1 tv * Examine the passed line buffer and
409 1.1 tv * return true if it is all blanks and tabs.
410 1.1 tv */
411 1.17 christos PUBLIC int
412 1.2 wiz blankline(char linebuf[])
413 1.1 tv {
414 1.1 tv char *cp;
415 1.1 tv
416 1.1 tv for (cp = linebuf; *cp; cp++)
417 1.19 christos if (!is_WSP(*cp))
418 1.17 christos return 0;
419 1.17 christos return 1;
420 1.1 tv }
421 1.1 tv
422 1.1 tv /*
423 1.1 tv * Start of a "comment".
424 1.1 tv * Ignore it.
425 1.1 tv */
426 1.17 christos static char *
427 1.2 wiz skip_comment(char *cp)
428 1.1 tv {
429 1.1 tv int nesting = 1;
430 1.1 tv
431 1.17 christos for (/*EMPTY*/; nesting > 0 && *cp; cp++) {
432 1.1 tv switch (*cp) {
433 1.1 tv case '\\':
434 1.1 tv if (cp[1])
435 1.1 tv cp++;
436 1.1 tv break;
437 1.1 tv case '(':
438 1.1 tv nesting++;
439 1.1 tv break;
440 1.1 tv case ')':
441 1.1 tv nesting--;
442 1.1 tv break;
443 1.1 tv }
444 1.1 tv }
445 1.1 tv return cp;
446 1.1 tv }
447 1.1 tv
448 1.1 tv /*
449 1.1 tv * Skin an arpa net address according to the RFC 822 interpretation
450 1.1 tv * of "host-phrase."
451 1.1 tv */
452 1.17 christos PUBLIC char *
453 1.2 wiz skin(char *name)
454 1.1 tv {
455 1.1 tv int c;
456 1.1 tv char *cp, *cp2;
457 1.1 tv char *bufend;
458 1.1 tv int gotlt, lastsp;
459 1.26 shm char *nbuf, *ret;
460 1.1 tv
461 1.4 wiz if (name == NULL)
462 1.17 christos return NULL;
463 1.4 wiz if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
464 1.4 wiz && strchr(name, ' ') == NULL)
465 1.17 christos return name;
466 1.26 shm
467 1.26 shm nbuf = emalloc(strlen(name) + 1);
468 1.1 tv gotlt = 0;
469 1.1 tv lastsp = 0;
470 1.1 tv bufend = nbuf;
471 1.19 christos for (cp = name, cp2 = bufend; (c = *cp++) != '\0'; /*EMPTY*/) {
472 1.1 tv switch (c) {
473 1.1 tv case '(':
474 1.1 tv cp = skip_comment(cp);
475 1.1 tv lastsp = 0;
476 1.1 tv break;
477 1.1 tv
478 1.1 tv case '"':
479 1.1 tv /*
480 1.1 tv * Start of a "quoted-string".
481 1.1 tv * Copy it in its entirety.
482 1.1 tv */
483 1.1 tv while ((c = *cp) != '\0') {
484 1.1 tv cp++;
485 1.1 tv if (c == '"')
486 1.1 tv break;
487 1.1 tv if (c != '\\')
488 1.1 tv *cp2++ = c;
489 1.1 tv else if ((c = *cp) != '\0') {
490 1.1 tv *cp2++ = c;
491 1.1 tv cp++;
492 1.1 tv }
493 1.1 tv }
494 1.1 tv lastsp = 0;
495 1.1 tv break;
496 1.1 tv
497 1.1 tv case ' ':
498 1.1 tv if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
499 1.1 tv cp += 3, *cp2++ = '@';
500 1.1 tv else
501 1.1 tv if (cp[0] == '@' && cp[1] == ' ')
502 1.1 tv cp += 2, *cp2++ = '@';
503 1.1 tv else
504 1.1 tv lastsp = 1;
505 1.1 tv break;
506 1.1 tv
507 1.1 tv case '<':
508 1.1 tv cp2 = bufend;
509 1.1 tv gotlt++;
510 1.1 tv lastsp = 0;
511 1.1 tv break;
512 1.1 tv
513 1.1 tv case '>':
514 1.1 tv if (gotlt) {
515 1.1 tv gotlt = 0;
516 1.1 tv while ((c = *cp) && c != ',') {
517 1.1 tv cp++;
518 1.1 tv if (c == '(')
519 1.1 tv cp = skip_comment(cp);
520 1.1 tv else if (c == '"')
521 1.1 tv while ((c = *cp) != '\0') {
522 1.1 tv cp++;
523 1.1 tv if (c == '"')
524 1.1 tv break;
525 1.1 tv if (c == '\\' && *cp)
526 1.1 tv cp++;
527 1.1 tv }
528 1.1 tv }
529 1.1 tv lastsp = 0;
530 1.1 tv break;
531 1.1 tv }
532 1.14 christos /* FALLTHROUGH */
533 1.1 tv
534 1.1 tv default:
535 1.1 tv if (lastsp) {
536 1.1 tv lastsp = 0;
537 1.1 tv *cp2++ = ' ';
538 1.1 tv }
539 1.1 tv *cp2++ = c;
540 1.27 shm if (c == ',' && *cp == ' ' && !gotlt) {
541 1.1 tv *cp2++ = ' ';
542 1.17 christos for (/*EMPTY*/; *cp == ' '; cp++)
543 1.17 christos continue;
544 1.1 tv lastsp = 0;
545 1.1 tv bufend = cp2;
546 1.1 tv }
547 1.1 tv }
548 1.1 tv }
549 1.1 tv *cp2 = 0;
550 1.26 shm
551 1.26 shm ret = savestr(nbuf);
552 1.26 shm free(nbuf);
553 1.1 tv
554 1.26 shm return ret;
555 1.1 tv }
556 1.1 tv
557 1.1 tv /*
558 1.1 tv * Fetch the sender's name from the passed message.
559 1.1 tv * Reptype can be
560 1.1 tv * 0 -- get sender's name for display purposes
561 1.1 tv * 1 -- get sender's name for reply
562 1.1 tv * 2 -- get sender's name for Reply
563 1.1 tv */
564 1.17 christos static char *
565 1.2 wiz name1(struct message *mp, int reptype)
566 1.1 tv {
567 1.1 tv char namebuf[LINESIZE];
568 1.1 tv char linebuf[LINESIZE];
569 1.1 tv char *cp, *cp2;
570 1.1 tv FILE *ibuf;
571 1.3 wiz int firstrun = 1;
572 1.1 tv
573 1.4 wiz if ((cp = hfield("from", mp)) != NULL)
574 1.1 tv return cp;
575 1.4 wiz if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
576 1.1 tv return cp;
577 1.1 tv ibuf = setinput(mp);
578 1.1 tv namebuf[0] = '\0';
579 1.22 christos if (readline(ibuf, linebuf, LINESIZE, 0) < 0)
580 1.17 christos return savestr(namebuf);
581 1.17 christos newname:
582 1.1 tv for (cp = linebuf; *cp && *cp != ' '; cp++)
583 1.17 christos continue;
584 1.19 christos cp = skip_WSP(cp);
585 1.1 tv for (cp2 = &namebuf[strlen(namebuf)];
586 1.19 christos *cp && !is_WSP(*cp) && cp2 < namebuf + LINESIZE - 1;
587 1.19 christos /*EMPTY*/)
588 1.1 tv *cp2++ = *cp++;
589 1.1 tv *cp2 = '\0';
590 1.22 christos if (readline(ibuf, linebuf, LINESIZE, 0) < 0)
591 1.17 christos return savestr(namebuf);
592 1.1 tv if ((cp = strchr(linebuf, 'F')) == NULL)
593 1.17 christos return savestr(namebuf);
594 1.1 tv if (strncmp(cp, "From", 4) != 0)
595 1.17 christos return savestr(namebuf);
596 1.1 tv while ((cp = strchr(cp, 'r')) != NULL) {
597 1.1 tv if (strncmp(cp, "remote", 6) == 0) {
598 1.1 tv if ((cp = strchr(cp, 'f')) == NULL)
599 1.1 tv break;
600 1.1 tv if (strncmp(cp, "from", 4) != 0)
601 1.1 tv break;
602 1.1 tv if ((cp = strchr(cp, ' ')) == NULL)
603 1.1 tv break;
604 1.1 tv cp++;
605 1.3 wiz if (firstrun) {
606 1.1 tv cp2 = namebuf;
607 1.3 wiz firstrun = 0;
608 1.1 tv } else
609 1.1 tv cp2 = strrchr(namebuf, '!') + 1;
610 1.1 tv while (*cp && cp2 < namebuf + LINESIZE - 1)
611 1.1 tv *cp2++ = *cp++;
612 1.1 tv if (cp2 < namebuf + LINESIZE - 1)
613 1.1 tv *cp2++ = '!';
614 1.1 tv *cp2 = '\0';
615 1.1 tv if (cp2 < namebuf + LINESIZE - 1)
616 1.1 tv goto newname;
617 1.1 tv else
618 1.1 tv break;
619 1.1 tv }
620 1.1 tv cp++;
621 1.1 tv }
622 1.17 christos return savestr(namebuf);
623 1.1 tv }
624 1.1 tv
625 1.1 tv /*
626 1.15 christos * Count the occurrences of c in str
627 1.1 tv */
628 1.17 christos static int
629 1.2 wiz charcount(char *str, int c)
630 1.1 tv {
631 1.1 tv char *cp;
632 1.1 tv int i;
633 1.1 tv
634 1.1 tv for (i = 0, cp = str; *cp; cp++)
635 1.1 tv if (*cp == c)
636 1.1 tv i++;
637 1.17 christos return i;
638 1.1 tv }
639 1.1 tv
640 1.1 tv /*
641 1.17 christos * Get sender's name from this message. If the message has
642 1.17 christos * a bunch of arpanet stuff in it, we may have to skin the name
643 1.17 christos * before returning it.
644 1.1 tv */
645 1.17 christos PUBLIC char *
646 1.17 christos nameof(struct message *mp, int reptype)
647 1.1 tv {
648 1.17 christos char *cp, *cp2;
649 1.1 tv
650 1.17 christos cp = skin(name1(mp, reptype));
651 1.17 christos if (reptype != 0 || charcount(cp, '!') < 2)
652 1.17 christos return cp;
653 1.17 christos cp2 = strrchr(cp, '!');
654 1.17 christos cp2--;
655 1.17 christos while (cp2 > cp && *cp2 != '!')
656 1.17 christos cp2--;
657 1.17 christos if (*cp2 == '!')
658 1.17 christos return cp2 + 1;
659 1.17 christos return cp;
660 1.1 tv }
661 1.1 tv
662 1.1 tv /*
663 1.1 tv * Copy s1 to s2, return pointer to null in s2.
664 1.1 tv */
665 1.17 christos PUBLIC char *
666 1.2 wiz copy(char *s1, char *s2)
667 1.1 tv {
668 1.1 tv
669 1.1 tv while ((*s2++ = *s1++) != '\0')
670 1.17 christos continue;
671 1.1 tv return s2 - 1;
672 1.1 tv }
673 1.1 tv
674 1.1 tv /*
675 1.17 christos * The core routine to check the ignore table for a field.
676 1.17 christos * Note: realfield should be lower-cased!
677 1.17 christos */
678 1.17 christos PUBLIC int
679 1.17 christos member(char *realfield, struct ignoretab *table)
680 1.17 christos {
681 1.17 christos struct ignore *igp;
682 1.17 christos
683 1.17 christos for (igp = table->i_head[hash(realfield)]; igp != 0; igp = igp->i_link)
684 1.17 christos if (*igp->i_field == *realfield &&
685 1.17 christos equal(igp->i_field, realfield))
686 1.17 christos return 1;
687 1.17 christos return 0;
688 1.17 christos }
689 1.17 christos
690 1.17 christos /*
691 1.1 tv * See if the given header field is supposed to be ignored.
692 1.1 tv */
693 1.17 christos PUBLIC int
694 1.13 christos isign(const char *field, struct ignoretab ignoretabs[2])
695 1.1 tv {
696 1.1 tv char realfld[LINESIZE];
697 1.1 tv
698 1.3 wiz if (ignoretabs == ignoreall)
699 1.1 tv return 1;
700 1.1 tv /*
701 1.1 tv * Lower-case the string, so that "Status" and "status"
702 1.1 tv * will hash to the same place.
703 1.1 tv */
704 1.1 tv istrcpy(realfld, field);
705 1.3 wiz if (ignoretabs[1].i_count > 0)
706 1.17 christos return !member(realfld, ignoretabs + 1);
707 1.1 tv else
708 1.17 christos return member(realfld, ignoretabs);
709 1.1 tv }
710 1.18 christos
711 1.20 christos PUBLIC void
712 1.20 christos add_ignore(const char *name, struct ignoretab *tab)
713 1.20 christos {
714 1.20 christos char field[LINESIZE];
715 1.20 christos int h;
716 1.20 christos struct ignore *igp;
717 1.20 christos
718 1.20 christos istrcpy(field, name);
719 1.20 christos if (member(field, tab))
720 1.20 christos return;
721 1.20 christos h = hash(field);
722 1.20 christos igp = ecalloc(1, sizeof(struct ignore));
723 1.20 christos igp->i_field = ecalloc(strlen(field) + 1, sizeof(char));
724 1.20 christos (void)strcpy(igp->i_field, field);
725 1.20 christos igp->i_link = tab->i_head[h];
726 1.20 christos tab->i_head[h] = igp;
727 1.20 christos tab->i_count++;
728 1.20 christos }
729 1.20 christos
730 1.18 christos /*
731 1.18 christos * Write a file to stdout, skipping comment lines.
732 1.18 christos */
733 1.18 christos PUBLIC void
734 1.18 christos cathelp(const char *fname)
735 1.18 christos {
736 1.18 christos char *line;
737 1.18 christos FILE *f;
738 1.18 christos size_t len;
739 1.18 christos
740 1.25 christos if ((f = Fopen(fname, "ref")) == NULL) {
741 1.23 joerg warn("%s", fname);
742 1.18 christos return;
743 1.18 christos }
744 1.18 christos while ((line = fgetln(f, &len)) != NULL) {
745 1.18 christos if (*line == '#')
746 1.18 christos continue;
747 1.18 christos if (fwrite(line, 1, len, stdout) != len)
748 1.18 christos break;
749 1.18 christos }
750 1.18 christos (void)Fclose(f);
751 1.18 christos return;
752 1.18 christos }
753