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. */
26 #if defined (BSD) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
27 /* This program is not used in BSD, so just avoid loader complaints. */
29 main (int argc, char *argv[])
37 main (int argc, char *argv[])
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) */
50 /* These are defined in config in some versions. */
64 #if __STDC__ || defined(STDC_HEADERS)
73 /* Type definitions */
84 struct line_record *continuation;
86 typedef struct line_record *line_list;
91 struct header_record *next;
92 struct header_record *previous;
94 typedef struct header_record *header;
99 int (*action)(FILE *);
100 struct stream_record *rest_streams;
102 typedef struct stream_record *stream_list;
104 /* A `struct linebuffer' is a structure which holds a line of text.
105 * `readline' reads a line from a stream into a linebuffer
106 * and works regardless of the length of the line.
115 struct linebuffer lb;
118 ((line_list) xmalloc (sizeof (struct line_record)))
119 #define new_header() \
120 ((header) xmalloc (sizeof (struct header_record)))
121 #define new_stream() \
122 ((stream_list) xmalloc (sizeof (struct stream_record)))
123 #define alloc_string(nchars) \
124 ((char *) xmalloc ((nchars) + 1))
126 /* Global declarations */
129 #define KEYWORD_SIZE 256
130 #define FROM_PREFIX "From"
131 #define MY_NAME "fakemail"
132 #define NIL ((line_list) NULL)
133 #define INITIAL_LINE_SIZE 200
135 #ifndef MAIL_PROGRAM_NAME
136 #define MAIL_PROGRAM_NAME "/bin/mail"
139 static const char *my_name;
140 static char *the_date;
141 static char *the_user;
142 static line_list file_preface;
143 static stream_list the_streams;
144 static boolean no_problems = true;
146 #if !__STDC__ && !defined(STDC_HEADERS)
147 extern FILE *popen ();
148 extern int fclose (), pclose ();
149 extern char *malloc (), *realloc ();
152 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400000
157 extern struct passwd *getpwuid ();
158 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400000
159 extern uid_t geteuid ();
161 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. */
194 xmalloc (size_t size)
196 void *result = malloc (size);
198 fatal ("virtual memory exhausted", (char *) 0);
203 xrealloc (void *ptr, size_t size)
205 void *result = realloc (ptr, size);
207 fatal ("virtual memory exhausted", (char *) 0);
211 /* Initialize a linebuffer for use */
214 init_linebuffer (struct linebuffer *linebuffer)
216 linebuffer->size = INITIAL_LINE_SIZE;
217 linebuffer->buffer = (char *) xmalloc (INITIAL_LINE_SIZE);
220 /* Read a line of text from `stream' into `linebuffer'.
221 * Return the length of the line.
225 readline (struct linebuffer *linebuffer, FILE *stream)
227 char *buffer = linebuffer->buffer;
228 char *p = linebuffer->buffer;
229 char *end = p + linebuffer->size;
233 int c = getc (stream);
236 linebuffer->size *= 2;
237 buffer = (char *) xrealloc (buffer, linebuffer->size);
238 p = buffer + (p - linebuffer->buffer);
239 end = buffer + linebuffer->size;
240 linebuffer->buffer = buffer;
242 if (c < 0 || c == '\n')
254 get_keyword (register char *field, char **rest)
256 static char keyword[KEYWORD_SIZE];
262 if ((isspace ((int) (unsigned char) c)) || (c == ':'))
263 return (char *) NULL;
264 *ptr++ = ((islower ((int) (unsigned char) c)) ?
265 (toupper ((int) (unsigned char) c)) : c);
266 while (((c = *field++) != ':') &&
267 (!(isspace ((int) (unsigned char) c))))
268 *ptr++ = ((islower ((int) (unsigned char) c)) ?
269 (toupper ((int) (unsigned char) c)) : c);
271 while (isspace ((int) (unsigned char) c)) c = *field++;
272 if (c != ':') return (char *) NULL;
278 has_keyword (char *field)
281 return (get_keyword (field, &ignored) != (char *) NULL);
285 add_field (line_list the_list, register char *field, register char *where)
291 while ((c = *field++) != '\0')
295 while (*field && *field != ')') ++field;
296 if (! (*field++)) break; /* no closer */
297 if (! (*field)) break; /* closerNULL */
300 *where++ = ((c == ','||c=='>'||c=='<') ? ' ' : c);
302 if (the_list == NIL) break;
303 field = the_list->string;
304 the_list = the_list->continuation;
310 make_file_preface (void)
312 char *the_string, *temp;
313 time_t idiotic_interface;
319 prefix_length = strlen (FROM_PREFIX);
320 time (&idiotic_interface);
321 the_date = ctime (&idiotic_interface);
322 /* the_date has an unwanted newline at the end */
323 date_length = strlen (the_date) - 1;
324 if (the_date[date_length] == '\n')
325 the_date[date_length] = '\0';
329 temp = cuserid ((char *) NULL);
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 +
338 strcpy (temp, FROM_PREFIX);
339 temp = &temp[prefix_length];
341 strcpy (temp, the_user);
342 temp = &temp[user_length];
344 strcpy (temp, the_date);
345 result = new_list ();
346 result->string = the_string;
347 result->continuation = ((line_list) NULL);
352 write_line_list (register line_list the_list, FILE *the_stream)
355 the_list != ((line_list) NULL) ;
356 the_list = the_list->continuation)
358 fputs (the_list->string, the_stream);
359 putc ('\n', the_stream);
365 close_the_streams (void)
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);
378 add_a_stream (FILE *the_stream, int (*closing_action)(FILE *))
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;
389 my_fclose (FILE *the_file)
391 putc ('\n', the_file);
393 return fclose (the_file);
397 open_a_file (char *name)
399 FILE *the_stream = fopen (name, "a");
400 if (the_stream != ((FILE *) NULL))
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);
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);
423 put_line (const char *string)
425 register stream_list rem;
426 for (rem = the_streams;
427 rem != ((stream_list) NULL);
428 rem = rem->rest_streams)
430 const char *s = string;
433 /* Divide STRING into lines. */
436 const char *breakpos;
438 /* Find the last char that fits. */
439 for (breakpos = s; *breakpos && column < 78; ++breakpos)
441 if (*breakpos == '\t')
446 /* If we didn't reach end of line, break the line. */
449 /* Back up to just after the last comma that fits. */
450 while (breakpos != s && breakpos[-1] != ',') --breakpos;
454 /* If no comma fits, move past the first address anyway. */
455 while (*breakpos != 0 && *breakpos != ',') ++breakpos;
457 /* Include the comma after it. */
461 /* Output that much, then break the line. */
462 fwrite (s, 1, breakpos - s, rem->handle);
465 /* Skip whitespace and prepare to print more addresses. */
467 while (*s == ' ' || *s == '\t') ++s;
469 fputs ("\n\t", rem->handle);
471 putc ('\n', rem->handle);
476 #define mail_error error
479 setup_files (register line_list the_list, register char *field)
481 register char *start;
485 while (((c = *field) != '\0') &&
493 while (((c = *field) != '\0') &&
499 if (!open_a_file (start))
500 mail_error ("Could not open file %s", start);
502 if (c != '\0') continue;
504 if (the_list == ((line_list) NULL)) return;
505 field = the_list->string;
506 the_list = the_list->continuation;
511 args_size (header the_header)
513 register header old = the_header;
514 register line_list rem;
515 register int size = 0;
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))
524 size += 1 + strlen (field);
525 for (rem = the_header->text->continuation;
527 rem = rem->continuation)
528 size += 1 + strlen (rem->string);
530 the_header = the_header->next;
531 } while (the_header != old);
536 parse_header (header the_header, register char *where)
538 register header old = the_header;
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)
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;
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);
564 register header the_header = ((header) NULL);
565 register line_list *next_line = ((line_list *) NULL);
567 init_linebuffer (&lb);
574 readline (&lb, stdin);
576 length = strlen (line);
577 if (length == 0) break;
579 if (has_keyword (line))
581 register header old = the_header;
582 the_header = new_header ();
583 if (old == ((header) NULL))
585 the_header->next = the_header;
586 the_header->previous = the_header;
590 the_header->previous = old;
591 the_header->next = old->next;
592 old->next = the_header;
594 next_line = &(the_header->text);
597 if (next_line == ((line_list *) NULL))
599 /* Not a valid header */
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);
610 return the_header->next;
614 write_header (header the_header)
616 register header old = the_header;
619 register line_list the_list;
620 for (the_list = the_header->text;
622 the_list = the_list->continuation)
623 put_line (the_list->string);
624 the_header = the_header->next;
625 } while (the_header != old);
631 main (int argc, char *argv[])
636 char *mail_program_name;
637 char buf[BUFLEN + 1];
641 mail_program_name = getenv ("FAKEMAILER");
642 if (!(mail_program_name && *mail_program_name))
643 mail_program_name = (char *) MAIL_PROGRAM_NAME;
644 name_length = strlen (mail_program_name);
647 the_streams = ((stream_list) NULL);
648 the_date = (char *) NULL;
649 the_user = (char *) NULL;
651 the_header = read_header ();
652 command_line = alloc_string ((size_t) (name_length +
653 args_size (the_header)));
654 strcpy (command_line, mail_program_name);
655 parse_header (the_header, &command_line[name_length]);
657 the_pipe = popen (command_line, "w");
658 if (the_pipe == ((FILE *) NULL))
659 fatal ("cannot open pipe to real mailer", (char *) NULL);
661 add_a_stream (the_pipe, pclose);
663 write_header (the_header);
665 /* Dump the message itself */
667 while (!feof (stdin))
669 size = fread (buf, 1, BUFLEN, stdin);
674 return close_the_streams ();
677 #endif /* not BSD 4.2 (or newer) */