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.
5 This file is part of GNU Emacs.
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)
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.
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. */
23 #define _XOPEN_SOURCE 500 /* for cuserid */
29 #if defined (BSD_SYSTEM) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
30 /* This program isnot used in BSD, so just avoid loader complaints. */
36 #else /* not BSD 4.2 (or newer) */
44 /* This conditional contains all the rest of the file. */
46 /* These are defined in config in some versions. */
63 /* This is to declare cuserid. */
68 /* Type definitions */
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. */
79 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) ((tm_year) <= INT_MAX - 1900)
81 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
82 (-999 - 1900 <= (tm_year) && (tm_year) <= 9999 - 1900)
90 struct line_record
*continuation
;
92 typedef struct line_record
*line_list
;
97 struct header_record
*next
;
98 struct header_record
*previous
;
100 typedef struct header_record
*header
;
106 struct stream_record
*rest_streams
;
108 typedef struct stream_record
*stream_list
;
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.
121 struct linebuffer lb
;
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))
132 /* Global declarations */
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
141 #ifndef MAIL_PROGRAM_NAME
142 #define MAIL_PROGRAM_NAME "/bin/mail"
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;
152 extern FILE *popen ();
153 extern int fclose (), pclose ();
156 extern struct passwd
*getpwuid ();
157 extern unsigned short geteuid ();
158 static struct passwd
*my_entry
;
160 (my_entry = getpwuid (((int) geteuid ())), \
166 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
172 printf ("%s: ", my_name
);
178 /* Print error message and exit. */
188 /* Like malloc but get fatal error if memory is exhausted. */
194 long *result
= (long *) malloc (((unsigned) size
));
195 if (result
== ((long *) NULL
))
196 fatal ("virtual memory exhausted", 0);
205 long *result
= (long *) realloc (ptr
, ((unsigned) size
));
206 if (result
== ((long *) NULL
))
207 fatal ("virtual memory exhausted");
211 /* Initialize a linebuffer for use */
214 init_linebuffer (linebuffer
)
215 struct linebuffer
*linebuffer
;
217 linebuffer
->size
= INITIAL_LINE_SIZE
;
218 linebuffer
->buffer
= ((char *) xmalloc (INITIAL_LINE_SIZE
));
221 /* Read a line of text from `stream' into `linebuffer'.
222 Return the length of the line. */
225 readline (linebuffer
, stream
)
226 struct linebuffer
*linebuffer
;
229 char *buffer
= linebuffer
->buffer
;
230 char *p
= linebuffer
->buffer
;
231 char *end
= p
+ linebuffer
->size
;
235 int c
= getc (stream
);
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
;
244 if (c
< 0 || c
== '\n')
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.
259 If there is no keyword, return NULL and don't alter *REST. */
262 get_keyword (field
, rest
)
263 register char *field
;
266 static char keyword
[KEYWORD_SIZE
];
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
);
279 c
= (unsigned char) *field
++;
281 return ((char *) NULL
);
286 /* Nonzero if the string FIELD starts with a colon-terminated keyword. */
293 return (get_keyword (field
, &ignored
) != ((char *) NULL
));
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 <...>.
303 We don't pay attention to overflowing WHERE;
304 the caller has to make it big enough. */
307 add_field (the_list
, field
, where
)
309 register char *field
, *where
;
314 char *this_recipient_where
;
318 this_recipient_where
= where
;
320 while ((c
= *field
++) != '\0')
326 in_quotes
= ! in_quotes
;
333 while (*field
&& *field
!= ')') ++field
;
334 if (! (*field
++)) break; /* no close */
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
;
345 /* Discard everything we got before the `<'. */
346 where
= this_recipient_where
;
348 /* Discard the rest of this name that follows the `>'. */
350 while (*field
&& *field
!= ',') ++field
;
351 if (! (*field
++)) break; /* no comma */
357 if (the_list
== NIL
) break;
358 field
= the_list
->string
;
359 the_list
= the_list
->continuation
;
367 char *the_string
, *temp
;
368 long idiotic_interface
;
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
395 strcpy (temp
, FROM_PREFIX
);
396 temp
= &temp
[prefix_length
];
398 strcpy (temp
, the_user
);
399 temp
= &temp
[user_length
];
401 strcpy (temp
, the_date
);
402 result
= new_list ();
403 result
->string
= the_string
;
404 result
->continuation
= ((line_list
) NULL
);
409 write_line_list (the_list
, the_stream
)
410 register line_list the_list
;
414 the_list
!= ((line_list
) NULL
) ;
415 the_list
= the_list
->continuation
)
417 fputs (the_list
->string
, the_stream
);
418 putc ('\n', the_stream
);
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
);
437 add_a_stream (the_stream
, closing_action
)
439 int (*closing_action
)();
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
;
453 putc ('\n', the_file
);
455 return fclose (the_file
);
462 FILE *the_stream
= fopen (name
, "a");
463 if (the_stream
!= ((FILE *) NULL
))
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
);
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
);
490 register stream_list rem
;
491 for (rem
= the_streams
;
492 rem
!= ((stream_list
) NULL
);
493 rem
= rem
->rest_streams
)
498 /* Divide STRING into lines. */
503 /* Find the last char that fits. */
504 for (breakpos
= s
; *breakpos
&& column
< 78; ++breakpos
)
506 if (*breakpos
== '\t')
511 /* If we didn't reach end of line, break the line. */
514 /* Back up to just after the last comma that fits. */
515 while (breakpos
!= s
&& breakpos
[-1] != ',') --breakpos
;
519 /* If no comma fits, move past the first address anyway. */
520 while (*breakpos
!= 0 && *breakpos
!= ',') ++breakpos
;
522 /* Include the comma after it. */
526 /* Output that much, then break the line. */
527 fwrite (s
, 1, breakpos
- s
, rem
->handle
);
530 /* Skip whitespace and prepare to print more addresses. */
532 while (*s
== ' ' || *s
== '\t') ++s
;
534 fputs ("\n\t", rem
->handle
);
536 putc ('\n', rem
->handle
);
541 #define mail_error error
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. */
548 setup_files (the_list
, field
)
549 register line_list the_list
;
550 register char *field
;
552 register char *start
;
556 while (((c
= *field
) != '\0')
564 while (((c
= *field
) != '\0')
570 if (!open_a_file (start
))
571 mail_error ("Could not open file %s", start
);
573 if (c
!= '\0') continue;
575 if (the_list
== ((line_list
) NULL
))
577 field
= the_list
->string
;
578 the_list
= the_list
->continuation
;
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. */
586 args_size (the_header
)
589 register header old
= the_header
;
590 register line_list rem
;
591 register int size
= 0;
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))
600 size
+= 1 + strlen (field
);
601 for (rem
= the_header
->text
->continuation
;
603 rem
= rem
->continuation
)
604 size
+= 1 + strlen (rem
->string
);
606 the_header
= the_header
->next
;
607 } while (the_header
!= old
);
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.
615 Also, if the header has any FCC fields, call setup_files for each one. */
618 parse_header (the_header
, where
)
620 register char *where
;
622 register header old
= the_header
;
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)
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
;
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
);
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. */
654 register header the_header
= ((header
) NULL
);
655 register line_list
*next_line
= ((line_list
*) NULL
);
657 init_linebuffer (&lb
);
664 readline (&lb
, stdin
);
666 length
= strlen (line
);
667 if (length
== 0) break;
669 if (has_keyword (line
))
671 register header old
= the_header
;
672 the_header
= new_header ();
673 if (old
== ((header
) NULL
))
675 the_header
->next
= the_header
;
676 the_header
->previous
= the_header
;
680 the_header
->previous
= old
;
681 the_header
->next
= old
->next
;
682 old
->next
= the_header
;
684 next_line
= &(the_header
->text
);
687 if (next_line
== ((line_list
*) NULL
))
689 /* Not a valid header */
692 *next_line
= new_list ();
693 (*next_line
)->string
= alloc_string (length
);
694 strcpy (((*next_line
)->string
), line
);
695 next_line
= &((*next_line
)->continuation
);
700 return the_header
->next
;
704 write_header (the_header
)
707 register header old
= the_header
;
710 register line_list the_list
;
711 for (the_list
= the_header
->text
;
713 the_list
= the_list
->continuation
)
714 put_line (the_list
->string
);
715 the_header
= the_header
->next
;
716 } while (the_header
!= old
);
729 char *mail_program_name
;
730 char buf
[BUFLEN
+ 1];
734 extern char *getenv ();
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
);
742 the_streams
= ((stream_list
) NULL
);
743 the_date
= ((char *) NULL
);
744 the_user
= ((char *) NULL
);
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
]);
751 the_pipe
= popen (command_line
, "w");
752 if (the_pipe
== ((FILE *) NULL
))
753 fatal ("cannot open pipe to real mailer");
755 add_a_stream (the_pipe
, pclose
);
757 write_header (the_header
);
759 /* Dump the message itself */
761 while (!feof (stdin
))
763 size
= fread (buf
, 1, BUFLEN
, stdin
);
768 exit (close_the_streams ());
771 #endif /* not MSDOS */
772 #endif /* not BSD 4.2 (or newer) */
774 /* arch-tag: acb0afa6-315a-4c5b-b9e3-def5725c8783
775 (do not change this comment) */
777 /* fakemail.c ends here */