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. */
62 /* This is to declare cuserid. */
67 /* Type definitions */
73 #define TM_YEAR_BASE 1900
75 /* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
76 asctime to have well-defined behavior. */
77 #ifndef TM_YEAR_IN_ASCTIME_RANGE
78 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
79 (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
87 struct line_record
*continuation
;
89 typedef struct line_record
*line_list
;
94 struct header_record
*next
;
95 struct header_record
*previous
;
97 typedef struct header_record
*header
;
103 struct stream_record
*rest_streams
;
105 typedef struct stream_record
*stream_list
;
107 /* A `struct linebuffer' is a structure which holds a line of text.
108 * `readline' reads a line from a stream into a linebuffer
109 * and works regardless of the length of the line.
118 struct linebuffer lb
;
121 ((line_list) xmalloc (sizeof (struct line_record)))
122 #define new_header() \
123 ((header) xmalloc (sizeof (struct header_record)))
124 #define new_stream() \
125 ((stream_list) xmalloc (sizeof (struct stream_record)))
126 #define alloc_string(nchars) \
127 ((char *) xmalloc ((nchars) + 1))
129 /* Global declarations */
132 #define KEYWORD_SIZE 256
133 #define FROM_PREFIX "From"
134 #define MY_NAME "fakemail"
135 #define NIL ((line_list) NULL)
136 #define INITIAL_LINE_SIZE 200
138 #ifndef MAIL_PROGRAM_NAME
139 #define MAIL_PROGRAM_NAME "/bin/mail"
142 static char *my_name
;
143 static char *the_date
;
144 static char *the_user
;
145 static line_list file_preface
;
146 static stream_list the_streams
;
147 static boolean no_problems
= true;
149 extern FILE *popen ();
150 extern int fclose (), pclose ();
153 extern struct passwd
*getpwuid ();
154 extern unsigned short geteuid ();
155 static struct passwd
*my_entry
;
157 (my_entry = getpwuid (((int) geteuid ())), \
163 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
169 printf ("%s: ", my_name
);
175 /* Print error message and exit. */
185 /* Like malloc but get fatal error if memory is exhausted. */
191 long *result
= (long *) malloc (((unsigned) size
));
192 if (result
== ((long *) NULL
))
193 fatal ("virtual memory exhausted");
202 long *result
= (long *) realloc (ptr
, ((unsigned) size
));
203 if (result
== ((long *) NULL
))
204 fatal ("virtual memory exhausted");
208 /* Initialize a linebuffer for use */
211 init_linebuffer (linebuffer
)
212 struct linebuffer
*linebuffer
;
214 linebuffer
->size
= INITIAL_LINE_SIZE
;
215 linebuffer
->buffer
= ((char *) xmalloc (INITIAL_LINE_SIZE
));
218 /* Read a line of text from `stream' into `linebuffer'.
219 Return the length of the line. */
222 readline (linebuffer
, stream
)
223 struct linebuffer
*linebuffer
;
226 char *buffer
= linebuffer
->buffer
;
227 char *p
= linebuffer
->buffer
;
228 char *end
= p
+ linebuffer
->size
;
232 int c
= getc (stream
);
235 linebuffer
->size
*= 2;
236 buffer
= ((char *) xrealloc ((long *)buffer
, linebuffer
->size
));
237 p
= buffer
+ (p
- linebuffer
->buffer
);
238 end
= buffer
+ linebuffer
->size
;
239 linebuffer
->buffer
= buffer
;
241 if (c
< 0 || c
== '\n')
252 /* Extract a colon-terminated keyword from the string FIELD.
253 Return that keyword as a string stored in a static buffer.
254 Store the address of the rest of the string into *REST.
256 If there is no keyword, return NULL and don't alter *REST. */
259 get_keyword (field
, rest
)
260 register char *field
;
263 static char keyword
[KEYWORD_SIZE
];
268 c
= (unsigned char) *field
++;
269 if (isspace (c
) || c
== ':')
270 return ((char *) NULL
);
271 *ptr
++ = (islower (c
) ? toupper (c
) : c
);
272 while (((c
= (unsigned char) *field
++) != ':') && ! isspace (c
))
273 *ptr
++ = (islower (c
) ? toupper (c
) : c
);
276 c
= (unsigned char) *field
++;
278 return ((char *) NULL
);
283 /* Nonzero if the string FIELD starts with a colon-terminated keyword. */
290 return (get_keyword (field
, &ignored
) != ((char *) NULL
));
293 /* Store the string FIELD, followed by any lines in THE_LIST,
294 into the buffer WHERE.
295 Concatenate lines, putting just a space between them.
296 Delete everything contained in parentheses.
297 When a recipient name contains <...>, we discard
298 everything except what is inside the <...>.
300 We don't pay attention to overflowing WHERE;
301 the caller has to make it big enough. */
304 add_field (the_list
, field
, where
)
306 register char *field
, *where
;
311 char *this_recipient_where
;
315 this_recipient_where
= where
;
317 while ((c
= *field
++) != '\0')
323 in_quotes
= ! in_quotes
;
330 while (*field
&& *field
!= ')') ++field
;
331 if (! (*field
++)) break; /* no close */
337 /* When we get to the end of one recipient,
338 don't discard it if the next one has <...>. */
339 this_recipient_where
= where
;
342 /* Discard everything we got before the `<'. */
343 where
= this_recipient_where
;
345 /* Discard the rest of this name that follows the `>'. */
347 while (*field
&& *field
!= ',') ++field
;
348 if (! (*field
++)) break; /* no comma */
354 if (the_list
== NIL
) break;
355 field
= the_list
->string
;
356 the_list
= the_list
->continuation
;
364 char *the_string
, *temp
;
365 long idiotic_interface
;
372 prefix_length
= strlen (FROM_PREFIX
);
373 time (&idiotic_interface
);
374 /* Convert to a string, checking for out-of-range time stamps.
375 Don't use 'ctime', as that might dump core if the hardware clock
376 is set to a bizarre value. */
377 tm
= localtime (&idiotic_interface
);
378 if (! (tm
&& TM_YEAR_IN_ASCTIME_RANGE (tm
->tm_year
)
379 && (the_date
= asctime (tm
))))
380 fatal ("current time is out of range");
381 /* the_date has an unwanted newline at the end */
382 date_length
= strlen (the_date
) - 1;
383 the_date
[date_length
] = '\0';
384 temp
= cuserid ((char *) NULL
);
385 user_length
= strlen (temp
);
386 the_user
= alloc_string (user_length
+ 1);
387 strcpy (the_user
, temp
);
388 the_string
= alloc_string (3 + prefix_length
392 strcpy (temp
, FROM_PREFIX
);
393 temp
= &temp
[prefix_length
];
395 strcpy (temp
, the_user
);
396 temp
= &temp
[user_length
];
398 strcpy (temp
, the_date
);
399 result
= new_list ();
400 result
->string
= the_string
;
401 result
->continuation
= ((line_list
) NULL
);
406 write_line_list (the_list
, the_stream
)
407 register line_list the_list
;
411 the_list
!= ((line_list
) NULL
) ;
412 the_list
= the_list
->continuation
)
414 fputs (the_list
->string
, the_stream
);
415 putc ('\n', the_stream
);
423 register stream_list rem
;
424 for (rem
= the_streams
;
425 rem
!= ((stream_list
) NULL
);
426 rem
= rem
->rest_streams
)
427 no_problems
= (no_problems
&&
428 ((*rem
->action
) (rem
->handle
) == 0));
429 the_streams
= ((stream_list
) NULL
);
430 return (no_problems
? EXIT_SUCCESS
: EXIT_FAILURE
);
434 add_a_stream (the_stream
, closing_action
)
436 int (*closing_action
)();
438 stream_list old
= the_streams
;
439 the_streams
= new_stream ();
440 the_streams
->handle
= the_stream
;
441 the_streams
->action
= closing_action
;
442 the_streams
->rest_streams
= old
;
450 putc ('\n', the_file
);
452 return fclose (the_file
);
459 FILE *the_stream
= fopen (name
, "a");
460 if (the_stream
!= ((FILE *) NULL
))
462 add_a_stream (the_stream
, my_fclose
);
463 if (the_user
== ((char *) NULL
))
464 file_preface
= make_file_preface ();
465 write_line_list (file_preface
, the_stream
);
475 register stream_list rem
;
476 for (rem
= the_streams
;
477 rem
!= ((stream_list
) NULL
);
478 rem
= rem
->rest_streams
)
479 fputs (s
, rem
->handle
);
487 register stream_list rem
;
488 for (rem
= the_streams
;
489 rem
!= ((stream_list
) NULL
);
490 rem
= rem
->rest_streams
)
495 /* Divide STRING into lines. */
500 /* Find the last char that fits. */
501 for (breakpos
= s
; *breakpos
&& column
< 78; ++breakpos
)
503 if (*breakpos
== '\t')
508 /* If we didn't reach end of line, break the line. */
511 /* Back up to just after the last comma that fits. */
512 while (breakpos
!= s
&& breakpos
[-1] != ',') --breakpos
;
516 /* If no comma fits, move past the first address anyway. */
517 while (*breakpos
!= 0 && *breakpos
!= ',') ++breakpos
;
519 /* Include the comma after it. */
523 /* Output that much, then break the line. */
524 fwrite (s
, 1, breakpos
- s
, rem
->handle
);
527 /* Skip whitespace and prepare to print more addresses. */
529 while (*s
== ' ' || *s
== '\t') ++s
;
531 fputs ("\n\t", rem
->handle
);
533 putc ('\n', rem
->handle
);
538 #define mail_error error
540 /* Handle an FCC field. FIELD is the text of the first line (after
541 the header name), and THE_LIST holds the continuation lines if any.
542 Call open_a_file for each file. */
545 setup_files (the_list
, field
)
546 register line_list the_list
;
547 register char *field
;
549 register char *start
;
553 while (((c
= *field
) != '\0')
561 while (((c
= *field
) != '\0')
567 if (!open_a_file (start
))
568 mail_error ("Could not open file %s", start
);
570 if (c
!= '\0') continue;
572 if (the_list
== ((line_list
) NULL
))
574 field
= the_list
->string
;
575 the_list
= the_list
->continuation
;
579 /* Compute the total size of all recipient names stored in THE_HEADER.
580 The result says how big to make the buffer to pass to parse_header. */
583 args_size (the_header
)
586 register header old
= the_header
;
587 register line_list rem
;
588 register int size
= 0;
592 register char *keyword
= get_keyword (the_header
->text
->string
, &field
);
593 if ((strcmp (keyword
, "TO") == 0)
594 || (strcmp (keyword
, "CC") == 0)
595 || (strcmp (keyword
, "BCC") == 0))
597 size
+= 1 + strlen (field
);
598 for (rem
= the_header
->text
->continuation
;
600 rem
= rem
->continuation
)
601 size
+= 1 + strlen (rem
->string
);
603 the_header
= the_header
->next
;
604 } while (the_header
!= old
);
608 /* Scan the header described by the lists THE_HEADER,
609 and put all recipient names into the buffer WHERE.
610 Precede each recipient name with a space.
612 Also, if the header has any FCC fields, call setup_files for each one. */
615 parse_header (the_header
, where
)
617 register char *where
;
619 register header old
= the_header
;
623 register char *keyword
= get_keyword (the_header
->text
->string
, &field
);
624 if (strcmp (keyword
, "TO") == 0)
625 where
= add_field (the_header
->text
->continuation
, field
, where
);
626 else if (strcmp (keyword
, "CC") == 0)
627 where
= add_field (the_header
->text
->continuation
, field
, where
);
628 else if (strcmp (keyword
, "BCC") == 0)
630 where
= add_field (the_header
->text
->continuation
, field
, where
);
631 the_header
->previous
->next
= the_header
->next
;
632 the_header
->next
->previous
= the_header
->previous
;
634 else if (strcmp (keyword
, "FCC") == 0)
635 setup_files (the_header
->text
->continuation
, field
);
636 the_header
= the_header
->next
;
637 } while (the_header
!= old
);
642 /* Read lines from the input until we get a blank line.
643 Create a list of `header' objects, one for each header field,
644 each of which points to a list of `line_list' objects,
645 one for each line in that field.
646 Continuation lines are grouped in the headers they continue. */
651 register header the_header
= ((header
) NULL
);
652 register line_list
*next_line
= ((line_list
*) NULL
);
654 init_linebuffer (&lb
);
661 readline (&lb
, stdin
);
663 length
= strlen (line
);
664 if (length
== 0) break;
666 if (has_keyword (line
))
668 register header old
= the_header
;
669 the_header
= new_header ();
670 if (old
== ((header
) NULL
))
672 the_header
->next
= the_header
;
673 the_header
->previous
= the_header
;
677 the_header
->previous
= old
;
678 the_header
->next
= old
->next
;
679 old
->next
= the_header
;
681 next_line
= &(the_header
->text
);
684 if (next_line
== ((line_list
*) NULL
))
686 /* Not a valid header */
689 *next_line
= new_list ();
690 (*next_line
)->string
= alloc_string (length
);
691 strcpy (((*next_line
)->string
), line
);
692 next_line
= &((*next_line
)->continuation
);
698 fatal ("input message has no header");
699 return the_header
->next
;
703 write_header (the_header
)
706 register header old
= the_header
;
709 register line_list the_list
;
710 for (the_list
= the_header
->text
;
712 the_list
= the_list
->continuation
)
713 put_line (the_list
->string
);
714 the_header
= the_header
->next
;
715 } while (the_header
!= old
);
728 char *mail_program_name
;
729 char buf
[BUFLEN
+ 1];
733 extern char *getenv ();
735 mail_program_name
= getenv ("FAKEMAILER");
736 if (!(mail_program_name
&& *mail_program_name
))
737 mail_program_name
= MAIL_PROGRAM_NAME
;
738 name_length
= strlen (mail_program_name
);
741 the_streams
= ((stream_list
) NULL
);
742 the_date
= ((char *) NULL
);
743 the_user
= ((char *) NULL
);
745 the_header
= read_header ();
746 command_line
= alloc_string (name_length
+ args_size (the_header
));
747 strcpy (command_line
, mail_program_name
);
748 parse_header (the_header
, &command_line
[name_length
]);
750 the_pipe
= popen (command_line
, "w");
751 if (the_pipe
== ((FILE *) NULL
))
752 fatal ("cannot open pipe to real mailer");
754 add_a_stream (the_pipe
, pclose
);
756 write_header (the_header
);
758 /* Dump the message itself */
760 while (!feof (stdin
))
762 size
= fread (buf
, 1, BUFLEN
, stdin
);
767 exit (close_the_streams ());
770 #endif /* not MSDOS */
771 #endif /* not BSD 4.2 (or newer) */
773 /* arch-tag: acb0afa6-315a-4c5b-b9e3-def5725c8783
774 (do not change this comment) */
776 /* fakemail.c ends here */