* b2m.c: Include <limits.h>.
[bpt/emacs.git] / lib-src / fakemail.c
1 /* sendmail-like interface to /bin/mail for system V,
2 Copyright (C) 1985, 1994, 1999, 2002, 2003, 2004,
3 2005, 2006 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #define NO_SHORTNAMES
23 #define _XOPEN_SOURCE 500 /* for cuserid */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #if defined (BSD_SYSTEM) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
30 /* This program isnot used in BSD, so just avoid loader complaints. */
31 int
32 main ()
33 {
34 return 0;
35 }
36 #else /* not BSD 4.2 (or newer) */
37 #ifdef MSDOS
38 int
39 main ()
40 {
41 return 0;
42 }
43 #else /* not MSDOS */
44 /* This conditional contains all the rest of the file. */
45
46 /* These are defined in config in some versions. */
47
48 #ifdef static
49 #undef static
50 #endif
51
52 #ifdef WINDOWSNT
53 #include "ntlib.h"
54 #endif
55
56 #include <limits.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <ctype.h>
60 #include <time.h>
61 #include <pwd.h>
62
63 /* This is to declare cuserid. */
64 #ifdef HAVE_UNISTD_H
65 #include <unistd.h>
66 #endif
67 \f
68 /* Type definitions */
69
70 #define boolean int
71 #define true 1
72 #define false 0
73
74 /* True if TM_YEAR is a struct tm's tm_year value that is acceptable
75 to asctime. Glibc asctime returns a useful string unless TM_YEAR
76 is nearly INT_MAX, but the C Standard lets C libraries overrun a
77 buffer if TM_YEAR needs more than 4 bytes. */
78 #ifdef __GLIBC__
79 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) ((tm_year) <= INT_MAX - 1900)
80 #else
81 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
82 (-999 - 1900 <= (tm_year) && (tm_year) <= 9999 - 1900)
83 #endif
84
85 /* Various lists */
86
87 struct line_record
88 {
89 char *string;
90 struct line_record *continuation;
91 };
92 typedef struct line_record *line_list;
93
94 struct header_record
95 {
96 line_list text;
97 struct header_record *next;
98 struct header_record *previous;
99 };
100 typedef struct header_record *header;
101
102 struct stream_record
103 {
104 FILE *handle;
105 int (*action)();
106 struct stream_record *rest_streams;
107 };
108 typedef struct stream_record *stream_list;
109
110 /* A `struct linebuffer' is a structure which holds a line of text.
111 * `readline' reads a line from a stream into a linebuffer
112 * and works regardless of the length of the line.
113 */
114
115 struct linebuffer
116 {
117 long size;
118 char *buffer;
119 };
120
121 struct linebuffer lb;
122
123 #define new_list() \
124 ((line_list) xmalloc (sizeof (struct line_record)))
125 #define new_header() \
126 ((header) xmalloc (sizeof (struct header_record)))
127 #define new_stream() \
128 ((stream_list) xmalloc (sizeof (struct stream_record)))
129 #define alloc_string(nchars) \
130 ((char *) xmalloc ((nchars) + 1))
131 \f
132 /* Global declarations */
133
134 #define BUFLEN 1024
135 #define KEYWORD_SIZE 256
136 #define FROM_PREFIX "From"
137 #define MY_NAME "fakemail"
138 #define NIL ((line_list) NULL)
139 #define INITIAL_LINE_SIZE 200
140
141 #ifndef MAIL_PROGRAM_NAME
142 #define MAIL_PROGRAM_NAME "/bin/mail"
143 #endif
144
145 static char *my_name;
146 static char *the_date;
147 static char *the_user;
148 static line_list file_preface;
149 static stream_list the_streams;
150 static boolean no_problems = true;
151
152 extern FILE *popen ();
153 extern int fclose (), pclose ();
154
155 #ifdef CURRENT_USER
156 extern struct passwd *getpwuid ();
157 extern unsigned short geteuid ();
158 static struct passwd *my_entry;
159 #define cuserid(s) \
160 (my_entry = getpwuid (((int) geteuid ())), \
161 my_entry->pw_name)
162 #endif
163 \f
164 /* Utilities */
165
166 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
167
168 static void
169 error (s1, s2)
170 char *s1, *s2;
171 {
172 printf ("%s: ", my_name);
173 printf (s1, s2);
174 printf ("\n");
175 no_problems = false;
176 }
177
178 /* Print error message and exit. */
179
180 static void
181 fatal (s1, s2)
182 char *s1, *s2;
183 {
184 error (s1, s2);
185 exit (EXIT_FAILURE);
186 }
187
188 /* Like malloc but get fatal error if memory is exhausted. */
189
190 static long *
191 xmalloc (size)
192 int size;
193 {
194 long *result = (long *) malloc (((unsigned) size));
195 if (result == ((long *) NULL))
196 fatal ("virtual memory exhausted", 0);
197 return result;
198 }
199
200 static long *
201 xrealloc (ptr, size)
202 long *ptr;
203 int size;
204 {
205 long *result = (long *) realloc (ptr, ((unsigned) size));
206 if (result == ((long *) NULL))
207 fatal ("virtual memory exhausted");
208 return result;
209 }
210 \f
211 /* Initialize a linebuffer for use */
212
213 void
214 init_linebuffer (linebuffer)
215 struct linebuffer *linebuffer;
216 {
217 linebuffer->size = INITIAL_LINE_SIZE;
218 linebuffer->buffer = ((char *) xmalloc (INITIAL_LINE_SIZE));
219 }
220
221 /* Read a line of text from `stream' into `linebuffer'.
222 Return the length of the line. */
223
224 long
225 readline (linebuffer, stream)
226 struct linebuffer *linebuffer;
227 FILE *stream;
228 {
229 char *buffer = linebuffer->buffer;
230 char *p = linebuffer->buffer;
231 char *end = p + linebuffer->size;
232
233 while (true)
234 {
235 int c = getc (stream);
236 if (p == end)
237 {
238 linebuffer->size *= 2;
239 buffer = ((char *) xrealloc ((long *)buffer, linebuffer->size));
240 p = buffer + (p - linebuffer->buffer);
241 end = buffer + linebuffer->size;
242 linebuffer->buffer = buffer;
243 }
244 if (c < 0 || c == '\n')
245 {
246 *p = 0;
247 break;
248 }
249 *p++ = c;
250 }
251
252 return p - buffer;
253 }
254 \f
255 /* Extract a colon-terminated keyword from the string FIELD.
256 Return that keyword as a string stored in a static buffer.
257 Store the address of the rest of the string into *REST.
258
259 If there is no keyword, return NULL and don't alter *REST. */
260
261 char *
262 get_keyword (field, rest)
263 register char *field;
264 char **rest;
265 {
266 static char keyword[KEYWORD_SIZE];
267 register char *ptr;
268 register int c;
269
270 ptr = &keyword[0];
271 c = (unsigned char) *field++;
272 if (isspace (c) || c == ':')
273 return ((char *) NULL);
274 *ptr++ = (islower (c) ? toupper (c) : c);
275 while (((c = (unsigned char) *field++) != ':') && ! isspace (c))
276 *ptr++ = (islower (c) ? toupper (c) : c);
277 *ptr++ = '\0';
278 while (isspace (c))
279 c = (unsigned char) *field++;
280 if (c != ':')
281 return ((char *) NULL);
282 *rest = field;
283 return &keyword[0];
284 }
285
286 /* Nonzero if the string FIELD starts with a colon-terminated keyword. */
287
288 boolean
289 has_keyword (field)
290 char *field;
291 {
292 char *ignored;
293 return (get_keyword (field, &ignored) != ((char *) NULL));
294 }
295
296 /* Store the string FIELD, followed by any lines in THE_LIST,
297 into the buffer WHERE.
298 Concatenate lines, putting just a space between them.
299 Delete everything contained in parentheses.
300 When a recipient name contains <...>, we discard
301 everything except what is inside the <...>.
302
303 We don't pay attention to overflowing WHERE;
304 the caller has to make it big enough. */
305
306 char *
307 add_field (the_list, field, where)
308 line_list the_list;
309 register char *field, *where;
310 {
311 register char c;
312 while (true)
313 {
314 char *this_recipient_where;
315 int in_quotes = 0;
316
317 *where++ = ' ';
318 this_recipient_where = where;
319
320 while ((c = *field++) != '\0')
321 {
322 if (c == '\\')
323 *where++ = c;
324 else if (c == '"')
325 {
326 in_quotes = ! in_quotes;
327 *where++ = c;
328 }
329 else if (in_quotes)
330 *where++ = c;
331 else if (c == '(')
332 {
333 while (*field && *field != ')') ++field;
334 if (! (*field++)) break; /* no close */
335 continue;
336 }
337 else if (c == ',')
338 {
339 *where++ = ' ';
340 /* When we get to the end of one recipient,
341 don't discard it if the next one has <...>. */
342 this_recipient_where = where;
343 }
344 else if (c == '<')
345 /* Discard everything we got before the `<'. */
346 where = this_recipient_where;
347 else if (c == '>')
348 /* Discard the rest of this name that follows the `>'. */
349 {
350 while (*field && *field != ',') ++field;
351 if (! (*field++)) break; /* no comma */
352 continue;
353 }
354 else
355 *where++ = c;
356 }
357 if (the_list == NIL) break;
358 field = the_list->string;
359 the_list = the_list->continuation;
360 }
361 return where;
362 }
363 \f
364 line_list
365 make_file_preface ()
366 {
367 char *the_string, *temp;
368 long idiotic_interface;
369 struct tm *tm;
370 long prefix_length;
371 long user_length;
372 long date_length;
373 line_list result;
374
375 prefix_length = strlen (FROM_PREFIX);
376 time (&idiotic_interface);
377 /* Convert to a string, checking for out-of-range time stamps.
378 Don't use 'ctime', as that might dump core if the hardware clock
379 is set to a bizarre value. */
380 tm = localtime (&idiotic_interface);
381 if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year)))
382 fatal ("current time is out of range", 0);
383 the_date = asctime (tm);
384 /* the_date has an unwanted newline at the end */
385 date_length = strlen (the_date) - 1;
386 the_date[date_length] = '\0';
387 temp = cuserid ((char *) NULL);
388 user_length = strlen (temp);
389 the_user = alloc_string (user_length + 1);
390 strcpy (the_user, temp);
391 the_string = alloc_string (3 + prefix_length
392 + user_length
393 + date_length);
394 temp = the_string;
395 strcpy (temp, FROM_PREFIX);
396 temp = &temp[prefix_length];
397 *temp++ = ' ';
398 strcpy (temp, the_user);
399 temp = &temp[user_length];
400 *temp++ = ' ';
401 strcpy (temp, the_date);
402 result = new_list ();
403 result->string = the_string;
404 result->continuation = ((line_list) NULL);
405 return result;
406 }
407
408 void
409 write_line_list (the_list, the_stream)
410 register line_list the_list;
411 FILE *the_stream;
412 {
413 for ( ;
414 the_list != ((line_list) NULL) ;
415 the_list = the_list->continuation)
416 {
417 fputs (the_list->string, the_stream);
418 putc ('\n', the_stream);
419 }
420 return;
421 }
422 \f
423 int
424 close_the_streams ()
425 {
426 register stream_list rem;
427 for (rem = the_streams;
428 rem != ((stream_list) NULL);
429 rem = rem->rest_streams)
430 no_problems = (no_problems &&
431 ((*rem->action) (rem->handle) == 0));
432 the_streams = ((stream_list) NULL);
433 return (no_problems ? EXIT_SUCCESS : EXIT_FAILURE);
434 }
435
436 void
437 add_a_stream (the_stream, closing_action)
438 FILE *the_stream;
439 int (*closing_action)();
440 {
441 stream_list old = the_streams;
442 the_streams = new_stream ();
443 the_streams->handle = the_stream;
444 the_streams->action = closing_action;
445 the_streams->rest_streams = old;
446 return;
447 }
448
449 int
450 my_fclose (the_file)
451 FILE *the_file;
452 {
453 putc ('\n', the_file);
454 fflush (the_file);
455 return fclose (the_file);
456 }
457
458 boolean
459 open_a_file (name)
460 char *name;
461 {
462 FILE *the_stream = fopen (name, "a");
463 if (the_stream != ((FILE *) NULL))
464 {
465 add_a_stream (the_stream, my_fclose);
466 if (the_user == ((char *) NULL))
467 file_preface = make_file_preface ();
468 write_line_list (file_preface, the_stream);
469 return true;
470 }
471 return false;
472 }
473
474 void
475 put_string (s)
476 char *s;
477 {
478 register stream_list rem;
479 for (rem = the_streams;
480 rem != ((stream_list) NULL);
481 rem = rem->rest_streams)
482 fputs (s, rem->handle);
483 return;
484 }
485
486 void
487 put_line (string)
488 char *string;
489 {
490 register stream_list rem;
491 for (rem = the_streams;
492 rem != ((stream_list) NULL);
493 rem = rem->rest_streams)
494 {
495 char *s = string;
496 int column = 0;
497
498 /* Divide STRING into lines. */
499 while (*s != 0)
500 {
501 char *breakpos;
502
503 /* Find the last char that fits. */
504 for (breakpos = s; *breakpos && column < 78; ++breakpos)
505 {
506 if (*breakpos == '\t')
507 column += 8;
508 else
509 column++;
510 }
511 /* If we didn't reach end of line, break the line. */
512 if (*breakpos)
513 {
514 /* Back up to just after the last comma that fits. */
515 while (breakpos != s && breakpos[-1] != ',') --breakpos;
516
517 if (breakpos == s)
518 {
519 /* If no comma fits, move past the first address anyway. */
520 while (*breakpos != 0 && *breakpos != ',') ++breakpos;
521 if (*breakpos != 0)
522 /* Include the comma after it. */
523 ++breakpos;
524 }
525 }
526 /* Output that much, then break the line. */
527 fwrite (s, 1, breakpos - s, rem->handle);
528 column = 8;
529
530 /* Skip whitespace and prepare to print more addresses. */
531 s = breakpos;
532 while (*s == ' ' || *s == '\t') ++s;
533 if (*s != 0)
534 fputs ("\n\t", rem->handle);
535 }
536 putc ('\n', rem->handle);
537 }
538 return;
539 }
540 \f
541 #define mail_error error
542
543 /* Handle an FCC field. FIELD is the text of the first line (after
544 the header name), and THE_LIST holds the continuation lines if any.
545 Call open_a_file for each file. */
546
547 void
548 setup_files (the_list, field)
549 register line_list the_list;
550 register char *field;
551 {
552 register char *start;
553 register char c;
554 while (true)
555 {
556 while (((c = *field) != '\0')
557 && (c == ' '
558 || c == '\t'
559 || c == ','))
560 field += 1;
561 if (c != '\0')
562 {
563 start = field;
564 while (((c = *field) != '\0')
565 && c != ' '
566 && c != '\t'
567 && c != ',')
568 field += 1;
569 *field = '\0';
570 if (!open_a_file (start))
571 mail_error ("Could not open file %s", start);
572 *field = c;
573 if (c != '\0') continue;
574 }
575 if (the_list == ((line_list) NULL))
576 return;
577 field = the_list->string;
578 the_list = the_list->continuation;
579 }
580 }
581 \f
582 /* Compute the total size of all recipient names stored in THE_HEADER.
583 The result says how big to make the buffer to pass to parse_header. */
584
585 int
586 args_size (the_header)
587 header the_header;
588 {
589 register header old = the_header;
590 register line_list rem;
591 register int size = 0;
592 do
593 {
594 char *field;
595 register char *keyword = get_keyword (the_header->text->string, &field);
596 if ((strcmp (keyword, "TO") == 0)
597 || (strcmp (keyword, "CC") == 0)
598 || (strcmp (keyword, "BCC") == 0))
599 {
600 size += 1 + strlen (field);
601 for (rem = the_header->text->continuation;
602 rem != NIL;
603 rem = rem->continuation)
604 size += 1 + strlen (rem->string);
605 }
606 the_header = the_header->next;
607 } while (the_header != old);
608 return size;
609 }
610
611 /* Scan the header described by the lists THE_HEADER,
612 and put all recipient names into the buffer WHERE.
613 Precede each recipient name with a space.
614
615 Also, if the header has any FCC fields, call setup_files for each one. */
616
617 void
618 parse_header (the_header, where)
619 header the_header;
620 register char *where;
621 {
622 register header old = the_header;
623 do
624 {
625 char *field;
626 register char *keyword = get_keyword (the_header->text->string, &field);
627 if (strcmp (keyword, "TO") == 0)
628 where = add_field (the_header->text->continuation, field, where);
629 else if (strcmp (keyword, "CC") == 0)
630 where = add_field (the_header->text->continuation, field, where);
631 else if (strcmp (keyword, "BCC") == 0)
632 {
633 where = add_field (the_header->text->continuation, field, where);
634 the_header->previous->next = the_header->next;
635 the_header->next->previous = the_header->previous;
636 }
637 else if (strcmp (keyword, "FCC") == 0)
638 setup_files (the_header->text->continuation, field);
639 the_header = the_header->next;
640 } while (the_header != old);
641 *where = '\0';
642 return;
643 }
644 \f
645 /* Read lines from the input until we get a blank line.
646 Create a list of `header' objects, one for each header field,
647 each of which points to a list of `line_list' objects,
648 one for each line in that field.
649 Continuation lines are grouped in the headers they continue. */
650
651 header
652 read_header ()
653 {
654 register header the_header = ((header) NULL);
655 register line_list *next_line = ((line_list *) NULL);
656
657 init_linebuffer (&lb);
658
659 do
660 {
661 long length;
662 register char *line;
663
664 readline (&lb, stdin);
665 line = lb.buffer;
666 length = strlen (line);
667 if (length == 0) break;
668
669 if (has_keyword (line))
670 {
671 register header old = the_header;
672 the_header = new_header ();
673 if (old == ((header) NULL))
674 {
675 the_header->next = the_header;
676 the_header->previous = the_header;
677 }
678 else
679 {
680 the_header->previous = old;
681 the_header->next = old->next;
682 old->next = the_header;
683 }
684 next_line = &(the_header->text);
685 }
686
687 if (next_line == ((line_list *) NULL))
688 {
689 /* Not a valid header */
690 exit (EXIT_FAILURE);
691 }
692 *next_line = new_list ();
693 (*next_line)->string = alloc_string (length);
694 strcpy (((*next_line)->string), line);
695 next_line = &((*next_line)->continuation);
696 *next_line = NIL;
697
698 } while (true);
699
700 return the_header->next;
701 }
702 \f
703 void
704 write_header (the_header)
705 header the_header;
706 {
707 register header old = the_header;
708 do
709 {
710 register line_list the_list;
711 for (the_list = the_header->text;
712 the_list != NIL;
713 the_list = the_list->continuation)
714 put_line (the_list->string);
715 the_header = the_header->next;
716 } while (the_header != old);
717 put_line ("");
718 return;
719 }
720 \f
721 int
722 main (argc, argv)
723 int argc;
724 char **argv;
725 {
726 char *command_line;
727 header the_header;
728 long name_length;
729 char *mail_program_name;
730 char buf[BUFLEN + 1];
731 register int size;
732 FILE *the_pipe;
733
734 extern char *getenv ();
735
736 mail_program_name = getenv ("FAKEMAILER");
737 if (!(mail_program_name && *mail_program_name))
738 mail_program_name = MAIL_PROGRAM_NAME;
739 name_length = strlen (mail_program_name);
740
741 my_name = MY_NAME;
742 the_streams = ((stream_list) NULL);
743 the_date = ((char *) NULL);
744 the_user = ((char *) NULL);
745
746 the_header = read_header ();
747 command_line = alloc_string (name_length + args_size (the_header));
748 strcpy (command_line, mail_program_name);
749 parse_header (the_header, &command_line[name_length]);
750
751 the_pipe = popen (command_line, "w");
752 if (the_pipe == ((FILE *) NULL))
753 fatal ("cannot open pipe to real mailer");
754
755 add_a_stream (the_pipe, pclose);
756
757 write_header (the_header);
758
759 /* Dump the message itself */
760
761 while (!feof (stdin))
762 {
763 size = fread (buf, 1, BUFLEN, stdin);
764 buf[size] = '\0';
765 put_string (buf);
766 }
767
768 exit (close_the_streams ());
769 }
770
771 #endif /* not MSDOS */
772 #endif /* not BSD 4.2 (or newer) */
773
774 /* arch-tag: acb0afa6-315a-4c5b-b9e3-def5725c8783
775 (do not change this comment) */
776
777 /* fakemail.c ends here */