1 /* sendmail-like interface to /bin/mail for system V,
2 Copyright (C) 1985, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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. */
21 /* Synched up with: FSF 19.28. */
24 #include <../src/config.h>
26 #if defined (BSD) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
27 /* This program isnot used in BSD, so just avoid loader complaints. */
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");
48 #else /* not BSD 4.2 (or newer) */
56 /* This conditional contains all the rest of the file. */
58 /* These are defined in config in some versions. */
72 #if __STDC__ || defined(STDC_HEADERS)
81 /* Type definitions */
92 struct line_record *continuation;
94 typedef struct line_record *line_list;
99 struct header_record *next;
100 struct header_record *previous;
102 typedef struct header_record *header;
108 struct stream_record *rest_streams;
110 typedef struct stream_record *stream_list;
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.
123 struct linebuffer lb;
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))
134 /* Global declarations */
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
143 #ifndef MAIL_PROGRAM_NAME
144 #define MAIL_PROGRAM_NAME "/bin/mail"
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;
154 #if !__STDC__ && !defined(STDC_HEADERS)
155 extern FILE *popen ();
156 extern int fclose (), pclose ();
157 extern char *malloc (), *realloc ();
161 extern struct passwd *getpwuid ();
162 extern unsigned short geteuid ();
163 static struct passwd *my_entry;
165 (my_entry = getpwuid (((int) geteuid ())), \
171 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
174 error (CONST char *s1, CONST char *s2)
176 printf ("%s: ", my_name);
182 /* Print error message and exit. */
185 fatal (CONST char *s1, CONST char *s2)
191 /* Like malloc but get fatal error if memory is exhausted. */
197 char *result = malloc (((unsigned) size));
198 if (result == ((char *) NULL))
199 fatal ("virtual memory exhausted", (char *) 0);
208 char *result = realloc (ptr, ((unsigned) size));
209 if (result == ((char *) NULL))
210 fatal ("virtual memory exhausted", (char *) 0);
214 /* Initialize a linebuffer for use */
217 init_linebuffer (struct linebuffer *linebuffer)
219 linebuffer->size = INITIAL_LINE_SIZE;
220 linebuffer->buffer = ((char *) xmalloc (INITIAL_LINE_SIZE));
223 /* Read a line of text from `stream' into `linebuffer'.
224 * Return the length of the line.
228 readline (struct linebuffer *linebuffer, FILE *stream)
230 char *buffer = linebuffer->buffer;
231 char *p = linebuffer->buffer;
232 char *end = p + linebuffer->size;
236 int c = getc (stream);
239 linebuffer->size *= 2;
240 buffer = ((char *) xrealloc ((char *) buffer,
241 (size_t) (linebuffer->size)));
242 p = buffer + (p - linebuffer->buffer);
243 end = buffer + linebuffer->size;
244 linebuffer->buffer = buffer;
246 if (c < 0 || c == '\n')
258 get_keyword (register char *field, char **rest)
260 static char keyword[KEYWORD_SIZE];
266 if ((isspace (c)) || (c == ':'))
267 return ((char *) NULL);
268 *ptr++ = ((islower (c)) ? (toupper (c)) : c);
269 while (((c = *field++) != ':') && (!(isspace (c))))
270 *ptr++ = ((islower (c)) ? (toupper (c)) : c);
272 while (isspace (c)) c = *field++;
273 if (c != ':') return ((char *) NULL);
279 has_keyword (char *field)
282 return (get_keyword (field, &ignored) != ((char *) NULL));
286 add_field (line_list the_list, register char *field, register char *where)
292 while ((c = *field++) != '\0')
296 while (*field && *field != ')') ++field;
297 if (! (*field++)) break; /* no closer */
298 if (! (*field)) break; /* closerNULL */
301 *where++ = ((c == ','||c=='>'||c=='<') ? ' ' : c);
303 if (the_list == NIL) break;
304 field = the_list->string;
305 the_list = the_list->continuation;
311 make_file_preface (void)
313 char *the_string, *temp;
314 long idiotic_interface;
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 temp = cuserid ((char *) NULL);
327 user_length = strlen (temp);
328 the_user = alloc_string ((size_t) (user_length + 1));
329 strcpy (the_user, temp);
330 the_string = alloc_string ((size_t) (3 + prefix_length +
334 strcpy (temp, FROM_PREFIX);
335 temp = &temp[prefix_length];
337 strcpy (temp, the_user);
338 temp = &temp[user_length];
340 strcpy (temp, the_date);
341 result = new_list ();
342 result->string = the_string;
343 result->continuation = ((line_list) NULL);
348 write_line_list (register line_list the_list, FILE *the_stream)
351 the_list != ((line_list) NULL) ;
352 the_list = the_list->continuation)
354 fputs (the_list->string, the_stream);
355 putc ('\n', the_stream);
361 close_the_streams (void)
363 register stream_list rem;
364 for (rem = the_streams;
365 rem != ((stream_list) NULL);
366 rem = rem->rest_streams)
367 no_problems = (no_problems &&
368 ((*rem->action) (rem->handle) == 0));
369 the_streams = ((stream_list) NULL);
370 return (no_problems ? 0 : 1);
374 add_a_stream (FILE *the_stream, int (*closing_action)())
376 stream_list old = the_streams;
377 the_streams = new_stream ();
378 the_streams->handle = the_stream;
379 the_streams->action = closing_action;
380 the_streams->rest_streams = old;
385 my_fclose (FILE *the_file)
387 putc ('\n', the_file);
389 return fclose (the_file);
393 open_a_file (char *name)
395 FILE *the_stream = fopen (name, "a");
396 if (the_stream != ((FILE *) NULL))
398 add_a_stream (the_stream, my_fclose);
399 if (the_user == ((char *) NULL))
400 file_preface = make_file_preface ();
401 write_line_list (file_preface, the_stream);
410 register stream_list rem;
411 for (rem = the_streams;
412 rem != ((stream_list) NULL);
413 rem = rem->rest_streams)
414 fputs (s, rem->handle);
419 put_line (CONST char *string)
421 register stream_list rem;
422 for (rem = the_streams;
423 rem != ((stream_list) NULL);
424 rem = rem->rest_streams)
426 CONST char *s = string;
429 /* Divide STRING into lines. */
432 CONST char *breakpos;
434 /* Find the last char that fits. */
435 for (breakpos = s; *breakpos && column < 78; ++breakpos)
437 if (*breakpos == '\t')
442 /* If we didn't reach end of line, break the line. */
445 /* Back up to just after the last comma that fits. */
446 while (breakpos != s && breakpos[-1] != ',') --breakpos;
450 /* If no comma fits, move past the first address anyway. */
451 while (*breakpos != 0 && *breakpos != ',') ++breakpos;
453 /* Include the comma after it. */
457 /* Output that much, then break the line. */
458 fwrite (s, 1, breakpos - s, rem->handle);
461 /* Skip whitespace and prepare to print more addresses. */
463 while (*s == ' ' || *s == '\t') ++s;
465 fputs ("\n\t", rem->handle);
467 putc ('\n', rem->handle);
472 #define mail_error error
475 setup_files (register line_list the_list, register char *field)
477 register char *start;
481 while (((c = *field) != '\0') &&
489 while (((c = *field) != '\0') &&
495 if (!open_a_file (start))
496 mail_error ("Could not open file %s", start);
498 if (c != '\0') continue;
500 if (the_list == ((line_list) NULL)) return;
501 field = the_list->string;
502 the_list = the_list->continuation;
507 args_size (header the_header)
509 register header old = the_header;
510 register line_list rem;
511 register int size = 0;
515 register char *keyword = get_keyword (the_header->text->string, &field);
516 if ((strcmp (keyword, "TO") == 0) ||
517 (strcmp (keyword, "CC") == 0) ||
518 (strcmp (keyword, "BCC") == 0))
520 size += 1 + strlen (field);
521 for (rem = the_header->text->continuation;
523 rem = rem->continuation)
524 size += 1 + strlen (rem->string);
526 the_header = the_header->next;
527 } while (the_header != old);
532 parse_header (header the_header, register char *where)
534 register header old = the_header;
538 register char *keyword = get_keyword (the_header->text->string, &field);
539 if (strcmp (keyword, "TO") == 0)
540 where = add_field (the_header->text->continuation, field, where);
541 else if (strcmp (keyword, "CC") == 0)
542 where = add_field (the_header->text->continuation, field, where);
543 else if (strcmp (keyword, "BCC") == 0)
545 where = add_field (the_header->text->continuation, field, where);
546 the_header->previous->next = the_header->next;
547 the_header->next->previous = the_header->previous;
549 else if (strcmp (keyword, "FCC") == 0)
550 setup_files (the_header->text->continuation, field);
551 the_header = the_header->next;
552 } while (the_header != old);
560 register header the_header = ((header) NULL);
561 register line_list *next_line = ((line_list *) NULL);
563 init_linebuffer (&lb);
570 readline (&lb, stdin);
572 length = strlen (line);
573 if (length == 0) break;
575 if (has_keyword (line))
577 register header old = the_header;
578 the_header = new_header ();
579 if (old == ((header) NULL))
581 the_header->next = the_header;
582 the_header->previous = the_header;
586 the_header->previous = old;
587 the_header->next = old->next;
588 old->next = the_header;
590 next_line = &(the_header->text);
593 if (next_line == ((line_list *) NULL))
595 /* Not a valid header */
598 *next_line = new_list ();
599 (*next_line)->string = alloc_string ((size_t) length);
600 strcpy (((*next_line)->string), line);
601 next_line = &((*next_line)->continuation);
606 return the_header->next;
610 write_header (header the_header)
612 register header old = the_header;
615 register line_list the_list;
616 for (the_list = the_header->text;
618 the_list = the_list->continuation)
619 put_line (the_list->string);
620 the_header = the_header->next;
621 } while (the_header != old);
634 char *mail_program_name;
635 char buf[BUFLEN + 1];
639 #if !(__STDC__ || defined(STDC_HEADERS))
640 extern char *getenv ();
643 mail_program_name = getenv ("FAKEMAILER");
644 if (!(mail_program_name && *mail_program_name))
645 mail_program_name = (char *) MAIL_PROGRAM_NAME;
646 name_length = strlen (mail_program_name);
649 the_streams = ((stream_list) NULL);
650 the_date = ((char *) NULL);
651 the_user = ((char *) NULL);
653 the_header = read_header ();
654 command_line = alloc_string ((size_t) (name_length +
655 args_size (the_header)));
656 strcpy (command_line, mail_program_name);
657 parse_header (the_header, &command_line[name_length]);
659 the_pipe = popen (command_line, "w");
660 if (the_pipe == ((FILE *) NULL))
661 fatal ("cannot open pipe to real mailer", (char *) 0);
663 add_a_stream (the_pipe, pclose);
665 write_header (the_header);
667 /* Dump the message itself */
669 while (!feof (stdin))
671 size = fread (buf, 1, BUFLEN, stdin);
676 return close_the_streams ();
679 #endif /* not MSDOS */
680 #endif /* not BSD 4.2 (or newer) */