Mercurial > hg > xemacs-beta
comparison lib-src/fakemail.c @ 428:3ecd8885ac67 r21-2-22
Import from CVS: tag r21-2-22
author | cvs |
---|---|
date | Mon, 13 Aug 2007 11:28:15 +0200 |
parents | |
children | 9d177e8d4150 |
comparison
equal
deleted
inserted
replaced
427:0a0253eac470 | 428:3ecd8885ac67 |
---|---|
1 /* sendmail-like interface to /bin/mail for system V, | |
2 Copyright (C) 1985, 1994 Free Software Foundation, Inc. | |
3 | |
4 This file is part of GNU Emacs. | |
5 | |
6 GNU Emacs is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
11 GNU Emacs is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GNU Emacs; see the file COPYING. If not, write to | |
18 the Free the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 Boston, MA 02111-1307, USA. */ | |
20 | |
21 /* Synched up with: FSF 19.28. */ | |
22 | |
23 #define NO_SHORTNAMES | |
24 #include <../src/config.h> | |
25 | |
26 #if defined (BSD) && !defined (BSD4_1) && !defined (USE_FAKEMAIL) | |
27 /* This program is not used in BSD, so just avoid loader complaints. */ | |
28 int | |
29 main (int argc, char *argv[]) | |
30 { | |
31 return 0; | |
32 } | |
33 #elif defined (LINUX) | |
34 #include <stdio.h> | |
35 #include <stdlib.h> | |
36 int | |
37 main (int argc, char *argv[]) | |
38 { | |
39 /* Linux /bin/mail, if it exists, is NOT the Unix v7 mail that | |
40 fakemail depends on! This causes garbled mail. Better to | |
41 output an error message. */ | |
42 fprintf (stderr, "Sorry, fakemail does not work on Linux.\n"); | |
43 fprintf (stderr, "Make sure you have the sendmail program, and\n"); | |
44 fprintf (stderr, "set the Lisp variable `sendmail-program' to point\n"); | |
45 fprintf (stderr, "to the path of the sendmail binary.\n"); | |
46 return 1; | |
47 } | |
48 #else /* not BSD 4.2 (or newer) */ | |
49 #ifdef MSDOS | |
50 int | |
51 main (int argc, char *argv[]) | |
52 { | |
53 return 0; | |
54 } | |
55 #else /* not MSDOS */ | |
56 /* This conditional contains all the rest of the file. */ | |
57 | |
58 /* These are defined in config in some versions. */ | |
59 | |
60 #ifdef static | |
61 #undef static | |
62 #endif | |
63 | |
64 #ifdef read | |
65 #undef read | |
66 #undef write | |
67 #undef open | |
68 #undef close | |
69 #endif | |
70 | |
71 #include <stdio.h> | |
72 #if __STDC__ || defined(STDC_HEADERS) | |
73 #include <stdlib.h> | |
74 #include <unistd.h> | |
75 #endif | |
76 #include <string.h> | |
77 #include <ctype.h> | |
78 #include <time.h> | |
79 #include <pwd.h> | |
80 | |
81 /* Type definitions */ | |
82 | |
83 #define boolean int | |
84 #define true 1 | |
85 #define false 0 | |
86 | |
87 /* Various lists */ | |
88 | |
89 struct line_record | |
90 { | |
91 char *string; | |
92 struct line_record *continuation; | |
93 }; | |
94 typedef struct line_record *line_list; | |
95 | |
96 struct header_record | |
97 { | |
98 line_list text; | |
99 struct header_record *next; | |
100 struct header_record *previous; | |
101 }; | |
102 typedef struct header_record *header; | |
103 | |
104 struct stream_record | |
105 { | |
106 FILE *handle; | |
107 int (*action)(FILE *); | |
108 struct stream_record *rest_streams; | |
109 }; | |
110 typedef struct stream_record *stream_list; | |
111 | |
112 /* A `struct linebuffer' is a structure which holds a line of text. | |
113 * `readline' reads a line from a stream into a linebuffer | |
114 * and works regardless of the length of the line. | |
115 */ | |
116 | |
117 struct linebuffer | |
118 { | |
119 long size; | |
120 char *buffer; | |
121 }; | |
122 | |
123 struct linebuffer lb; | |
124 | |
125 #define new_list() \ | |
126 ((line_list) xmalloc (sizeof (struct line_record))) | |
127 #define new_header() \ | |
128 ((header) xmalloc (sizeof (struct header_record))) | |
129 #define new_stream() \ | |
130 ((stream_list) xmalloc (sizeof (struct stream_record))) | |
131 #define alloc_string(nchars) \ | |
132 ((char *) xmalloc ((nchars) + 1)) | |
133 | |
134 /* Global declarations */ | |
135 | |
136 #define BUFLEN 1024 | |
137 #define KEYWORD_SIZE 256 | |
138 #define FROM_PREFIX "From" | |
139 #define MY_NAME "fakemail" | |
140 #define NIL ((line_list) NULL) | |
141 #define INITIAL_LINE_SIZE 200 | |
142 | |
143 #ifndef MAIL_PROGRAM_NAME | |
144 #define MAIL_PROGRAM_NAME "/bin/mail" | |
145 #endif | |
146 | |
147 static CONST char *my_name; | |
148 static char *the_date; | |
149 static char *the_user; | |
150 static line_list file_preface; | |
151 static stream_list the_streams; | |
152 static boolean no_problems = true; | |
153 | |
154 #if !__STDC__ && !defined(STDC_HEADERS) | |
155 extern FILE *popen (); | |
156 extern int fclose (), pclose (); | |
157 extern char *malloc (), *realloc (); | |
158 #endif | |
159 | |
160 #ifdef CURRENT_USER | |
161 extern struct passwd *getpwuid (); | |
162 extern unsigned short geteuid (); | |
163 static struct passwd *my_entry; | |
164 #define cuserid(s) \ | |
165 (my_entry = getpwuid (((int) geteuid ())), \ | |
166 my_entry->pw_name) | |
167 #endif | |
168 | |
169 /* Utilities */ | |
170 | |
171 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ | |
172 | |
173 static void | |
174 error (CONST char *s1, CONST char *s2) | |
175 { | |
176 printf ("%s: ", my_name); | |
177 printf (s1, s2); | |
178 printf ("\n"); | |
179 no_problems = false; | |
180 } | |
181 | |
182 /* Print error message and exit. */ | |
183 | |
184 static void | |
185 fatal (CONST char *s1, CONST char *s2) | |
186 { | |
187 error (s1, s2); | |
188 exit (1); | |
189 } | |
190 | |
191 /* Like malloc but get fatal error if memory is exhausted. */ | |
192 | |
193 static char * | |
194 xmalloc (size_t size) | |
195 { | |
196 char *result = malloc (((unsigned) size)); | |
197 if (result == ((char *) NULL)) | |
198 fatal ("virtual memory exhausted", (char *) 0); | |
199 return result; | |
200 } | |
201 | |
202 static char * | |
203 xrealloc (char *ptr, size_t size) | |
204 { | |
205 char *result = realloc (ptr, ((unsigned) size)); | |
206 if (result == ((char *) NULL)) | |
207 fatal ("virtual memory exhausted", (char *) 0); | |
208 return result; | |
209 } | |
210 | |
211 /* Initialize a linebuffer for use */ | |
212 | |
213 static void | |
214 init_linebuffer (struct linebuffer *linebuffer) | |
215 { | |
216 linebuffer->size = INITIAL_LINE_SIZE; | |
217 linebuffer->buffer = ((char *) xmalloc (INITIAL_LINE_SIZE)); | |
218 } | |
219 | |
220 /* Read a line of text from `stream' into `linebuffer'. | |
221 * Return the length of the line. | |
222 */ | |
223 | |
224 static long | |
225 readline (struct linebuffer *linebuffer, FILE *stream) | |
226 { | |
227 char *buffer = linebuffer->buffer; | |
228 char *p = linebuffer->buffer; | |
229 char *end = p + linebuffer->size; | |
230 | |
231 while (true) | |
232 { | |
233 int c = getc (stream); | |
234 if (p == end) | |
235 { | |
236 linebuffer->size *= 2; | |
237 buffer = ((char *) xrealloc ((char *) buffer, | |
238 (size_t) (linebuffer->size))); | |
239 p = buffer + (p - linebuffer->buffer); | |
240 end = buffer + linebuffer->size; | |
241 linebuffer->buffer = buffer; | |
242 } | |
243 if (c < 0 || c == '\n') | |
244 { | |
245 *p = 0; | |
246 break; | |
247 } | |
248 *p++ = c; | |
249 } | |
250 | |
251 return p - buffer; | |
252 } | |
253 | |
254 static char * | |
255 get_keyword (register char *field, char **rest) | |
256 { | |
257 static char keyword[KEYWORD_SIZE]; | |
258 register char *ptr; | |
259 register char c; | |
260 | |
261 ptr = &keyword[0]; | |
262 c = *field++; | |
263 if ((isspace ((int) (unsigned char) c)) || (c == ':')) | |
264 return (char *) NULL; | |
265 *ptr++ = ((islower ((int) (unsigned char) c)) ? | |
266 (toupper ((int) (unsigned char) c)) : c); | |
267 while (((c = *field++) != ':') && | |
268 (!(isspace ((int) (unsigned char) c)))) | |
269 *ptr++ = ((islower ((int) (unsigned char) c)) ? | |
270 (toupper ((int) (unsigned char) c)) : c); | |
271 *ptr++ = '\0'; | |
272 while (isspace ((int) (unsigned char) c)) c = *field++; | |
273 if (c != ':') return (char *) NULL; | |
274 *rest = field; | |
275 return &keyword[0]; | |
276 } | |
277 | |
278 static boolean | |
279 has_keyword (char *field) | |
280 { | |
281 char *ignored; | |
282 return (get_keyword (field, &ignored) != ((char *) NULL)); | |
283 } | |
284 | |
285 static char * | |
286 add_field (line_list the_list, register char *field, register char *where) | |
287 { | |
288 register char c; | |
289 while (true) | |
290 { | |
291 *where++ = ' '; | |
292 while ((c = *field++) != '\0') | |
293 { | |
294 if (c == '(') | |
295 { | |
296 while (*field && *field != ')') ++field; | |
297 if (! (*field++)) break; /* no closer */ | |
298 if (! (*field)) break; /* closerNULL */ | |
299 c = *field; | |
300 } | |
301 *where++ = ((c == ','||c=='>'||c=='<') ? ' ' : c); | |
302 } | |
303 if (the_list == NIL) break; | |
304 field = the_list->string; | |
305 the_list = the_list->continuation; | |
306 } | |
307 return where; | |
308 } | |
309 | |
310 static line_list | |
311 make_file_preface (void) | |
312 { | |
313 char *the_string, *temp; | |
314 long idiotic_interface; | |
315 long prefix_length; | |
316 long user_length; | |
317 long date_length; | |
318 line_list result; | |
319 | |
320 prefix_length = strlen (FROM_PREFIX); | |
321 time (&idiotic_interface); | |
322 the_date = ctime (&idiotic_interface); | |
323 /* the_date has an unwanted newline at the end */ | |
324 date_length = strlen (the_date) - 1; | |
325 the_date[date_length] = '\0'; | |
326 #ifdef WINDOWSNT | |
327 temp = "(null)"; | |
328 #else | |
329 temp = cuserid ((char *) NULL); | |
330 #endif | |
331 user_length = strlen (temp); | |
332 the_user = alloc_string ((size_t) (user_length + 1)); | |
333 strcpy (the_user, temp); | |
334 the_string = alloc_string ((size_t) (3 + prefix_length + | |
335 user_length + | |
336 date_length)); | |
337 temp = the_string; | |
338 strcpy (temp, FROM_PREFIX); | |
339 temp = &temp[prefix_length]; | |
340 *temp++ = ' '; | |
341 strcpy (temp, the_user); | |
342 temp = &temp[user_length]; | |
343 *temp++ = ' '; | |
344 strcpy (temp, the_date); | |
345 result = new_list (); | |
346 result->string = the_string; | |
347 result->continuation = ((line_list) NULL); | |
348 return result; | |
349 } | |
350 | |
351 static void | |
352 write_line_list (register line_list the_list, FILE *the_stream) | |
353 { | |
354 for ( ; | |
355 the_list != ((line_list) NULL) ; | |
356 the_list = the_list->continuation) | |
357 { | |
358 fputs (the_list->string, the_stream); | |
359 putc ('\n', the_stream); | |
360 } | |
361 return; | |
362 } | |
363 | |
364 static int | |
365 close_the_streams (void) | |
366 { | |
367 register stream_list rem; | |
368 for (rem = the_streams; | |
369 rem != ((stream_list) NULL); | |
370 rem = rem->rest_streams) | |
371 no_problems = (no_problems && | |
372 ((*rem->action) (rem->handle) == 0)); | |
373 the_streams = ((stream_list) NULL); | |
374 return (no_problems ? 0 : 1); | |
375 } | |
376 | |
377 static void | |
378 add_a_stream (FILE *the_stream, int (*closing_action)(FILE *)) | |
379 { | |
380 stream_list old = the_streams; | |
381 the_streams = new_stream (); | |
382 the_streams->handle = the_stream; | |
383 the_streams->action = closing_action; | |
384 the_streams->rest_streams = old; | |
385 return; | |
386 } | |
387 | |
388 static int | |
389 my_fclose (FILE *the_file) | |
390 { | |
391 putc ('\n', the_file); | |
392 fflush (the_file); | |
393 return fclose (the_file); | |
394 } | |
395 | |
396 static boolean | |
397 open_a_file (char *name) | |
398 { | |
399 FILE *the_stream = fopen (name, "a"); | |
400 if (the_stream != ((FILE *) NULL)) | |
401 { | |
402 add_a_stream (the_stream, my_fclose); | |
403 if (the_user == ((char *) NULL)) | |
404 file_preface = make_file_preface (); | |
405 write_line_list (file_preface, the_stream); | |
406 return true; | |
407 } | |
408 return false; | |
409 } | |
410 | |
411 static void | |
412 put_string (char *s) | |
413 { | |
414 register stream_list rem; | |
415 for (rem = the_streams; | |
416 rem != ((stream_list) NULL); | |
417 rem = rem->rest_streams) | |
418 fputs (s, rem->handle); | |
419 return; | |
420 } | |
421 | |
422 static void | |
423 put_line (CONST char *string) | |
424 { | |
425 register stream_list rem; | |
426 for (rem = the_streams; | |
427 rem != ((stream_list) NULL); | |
428 rem = rem->rest_streams) | |
429 { | |
430 CONST char *s = string; | |
431 int column = 0; | |
432 | |
433 /* Divide STRING into lines. */ | |
434 while (*s != 0) | |
435 { | |
436 CONST char *breakpos; | |
437 | |
438 /* Find the last char that fits. */ | |
439 for (breakpos = s; *breakpos && column < 78; ++breakpos) | |
440 { | |
441 if (*breakpos == '\t') | |
442 column += 8; | |
443 else | |
444 column++; | |
445 } | |
446 /* If we didn't reach end of line, break the line. */ | |
447 if (*breakpos) | |
448 { | |
449 /* Back up to just after the last comma that fits. */ | |
450 while (breakpos != s && breakpos[-1] != ',') --breakpos; | |
451 | |
452 if (breakpos == s) | |
453 { | |
454 /* If no comma fits, move past the first address anyway. */ | |
455 while (*breakpos != 0 && *breakpos != ',') ++breakpos; | |
456 if (*breakpos != 0) | |
457 /* Include the comma after it. */ | |
458 ++breakpos; | |
459 } | |
460 } | |
461 /* Output that much, then break the line. */ | |
462 fwrite (s, 1, breakpos - s, rem->handle); | |
463 column = 8; | |
464 | |
465 /* Skip whitespace and prepare to print more addresses. */ | |
466 s = breakpos; | |
467 while (*s == ' ' || *s == '\t') ++s; | |
468 if (*s != 0) | |
469 fputs ("\n\t", rem->handle); | |
470 } | |
471 putc ('\n', rem->handle); | |
472 } | |
473 return; | |
474 } | |
475 | |
476 #define mail_error error | |
477 | |
478 static void | |
479 setup_files (register line_list the_list, register char *field) | |
480 { | |
481 register char *start; | |
482 register char c; | |
483 while (true) | |
484 { | |
485 while (((c = *field) != '\0') && | |
486 ((c == ' ') || | |
487 (c == '\t') || | |
488 (c == ','))) | |
489 field += 1; | |
490 if (c != '\0') | |
491 { | |
492 start = field; | |
493 while (((c = *field) != '\0') && | |
494 (c != ' ') && | |
495 (c != '\t') && | |
496 (c != ',')) | |
497 field += 1; | |
498 *field = '\0'; | |
499 if (!open_a_file (start)) | |
500 mail_error ("Could not open file %s", start); | |
501 *field = c; | |
502 if (c != '\0') continue; | |
503 } | |
504 if (the_list == ((line_list) NULL)) return; | |
505 field = the_list->string; | |
506 the_list = the_list->continuation; | |
507 } | |
508 } | |
509 | |
510 static int | |
511 args_size (header the_header) | |
512 { | |
513 register header old = the_header; | |
514 register line_list rem; | |
515 register int size = 0; | |
516 do | |
517 { | |
518 char *field; | |
519 register char *keyword = get_keyword (the_header->text->string, &field); | |
520 if ((strcmp (keyword, "TO") == 0) || | |
521 (strcmp (keyword, "CC") == 0) || | |
522 (strcmp (keyword, "BCC") == 0)) | |
523 { | |
524 size += 1 + strlen (field); | |
525 for (rem = the_header->text->continuation; | |
526 rem != NIL; | |
527 rem = rem->continuation) | |
528 size += 1 + strlen (rem->string); | |
529 } | |
530 the_header = the_header->next; | |
531 } while (the_header != old); | |
532 return size; | |
533 } | |
534 | |
535 static void | |
536 parse_header (header the_header, register char *where) | |
537 { | |
538 register header old = the_header; | |
539 do | |
540 { | |
541 char *field; | |
542 register char *keyword = get_keyword (the_header->text->string, &field); | |
543 if (strcmp (keyword, "TO") == 0) | |
544 where = add_field (the_header->text->continuation, field, where); | |
545 else if (strcmp (keyword, "CC") == 0) | |
546 where = add_field (the_header->text->continuation, field, where); | |
547 else if (strcmp (keyword, "BCC") == 0) | |
548 { | |
549 where = add_field (the_header->text->continuation, field, where); | |
550 the_header->previous->next = the_header->next; | |
551 the_header->next->previous = the_header->previous; | |
552 } | |
553 else if (strcmp (keyword, "FCC") == 0) | |
554 setup_files (the_header->text->continuation, field); | |
555 the_header = the_header->next; | |
556 } while (the_header != old); | |
557 *where = '\0'; | |
558 return; | |
559 } | |
560 | |
561 static header | |
562 read_header (void) | |
563 { | |
564 register header the_header = ((header) NULL); | |
565 register line_list *next_line = ((line_list *) NULL); | |
566 | |
567 init_linebuffer (&lb); | |
568 | |
569 do | |
570 { | |
571 long length; | |
572 register char *line; | |
573 | |
574 readline (&lb, stdin); | |
575 line = lb.buffer; | |
576 length = strlen (line); | |
577 if (length == 0) break; | |
578 | |
579 if (has_keyword (line)) | |
580 { | |
581 register header old = the_header; | |
582 the_header = new_header (); | |
583 if (old == ((header) NULL)) | |
584 { | |
585 the_header->next = the_header; | |
586 the_header->previous = the_header; | |
587 } | |
588 else | |
589 { | |
590 the_header->previous = old; | |
591 the_header->next = old->next; | |
592 old->next = the_header; | |
593 } | |
594 next_line = &(the_header->text); | |
595 } | |
596 | |
597 if (next_line == ((line_list *) NULL)) | |
598 { | |
599 /* Not a valid header */ | |
600 exit (1); | |
601 } | |
602 *next_line = new_list (); | |
603 (*next_line)->string = alloc_string ((size_t) length); | |
604 strcpy (((*next_line)->string), line); | |
605 next_line = &((*next_line)->continuation); | |
606 *next_line = NIL; | |
607 | |
608 } while (true); | |
609 | |
610 return the_header->next; | |
611 } | |
612 | |
613 static void | |
614 write_header (header the_header) | |
615 { | |
616 register header old = the_header; | |
617 do | |
618 { | |
619 register line_list the_list; | |
620 for (the_list = the_header->text; | |
621 the_list != NIL; | |
622 the_list = the_list->continuation) | |
623 put_line (the_list->string); | |
624 the_header = the_header->next; | |
625 } while (the_header != old); | |
626 put_line (""); | |
627 return; | |
628 } | |
629 | |
630 int | |
631 main (int argc, char *argv[]) | |
632 { | |
633 char *command_line; | |
634 header the_header; | |
635 long name_length; | |
636 char *mail_program_name; | |
637 char buf[BUFLEN + 1]; | |
638 register int size; | |
639 FILE *the_pipe; | |
640 | |
641 #if !(__STDC__ || defined(STDC_HEADERS)) | |
642 extern char *getenv (); | |
643 #endif | |
644 | |
645 mail_program_name = getenv ("FAKEMAILER"); | |
646 if (!(mail_program_name && *mail_program_name)) | |
647 mail_program_name = (char *) MAIL_PROGRAM_NAME; | |
648 name_length = strlen (mail_program_name); | |
649 | |
650 my_name = MY_NAME; | |
651 the_streams = ((stream_list) NULL); | |
652 the_date = ((char *) NULL); | |
653 the_user = ((char *) NULL); | |
654 | |
655 the_header = read_header (); | |
656 command_line = alloc_string ((size_t) (name_length + | |
657 args_size (the_header))); | |
658 strcpy (command_line, mail_program_name); | |
659 parse_header (the_header, &command_line[name_length]); | |
660 | |
661 the_pipe = popen (command_line, "w"); | |
662 if (the_pipe == ((FILE *) NULL)) | |
663 fatal ("cannot open pipe to real mailer", (char *) 0); | |
664 | |
665 add_a_stream (the_pipe, pclose); | |
666 | |
667 write_header (the_header); | |
668 | |
669 /* Dump the message itself */ | |
670 | |
671 while (!feof (stdin)) | |
672 { | |
673 size = fread (buf, 1, BUFLEN, stdin); | |
674 buf[size] = '\0'; | |
675 put_string (buf); | |
676 } | |
677 | |
678 return close_the_streams (); | |
679 } | |
680 | |
681 #endif /* not MSDOS */ | |
682 #endif /* not BSD 4.2 (or newer) */ |