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