1 /* sendmail-like interface to /bin/mail for system V,
2 Copyright (C) 1985, 1994, 1999, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 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 3, 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. */
22 #define _XOPEN_SOURCE 500 /* for cuserid */
28 #if defined (BSD_SYSTEM) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
29 /* This program isnot used in BSD, so just avoid loader complaints. */
35 #else /* not BSD 4.2 (or newer) */
43 /* This conditional contains all the rest of the file. */
45 /* These are defined in config in some versions. */
61 /* This is to declare cuserid. */
66 /* Type definitions */
72 #define TM_YEAR_BASE 1900
74 /* Nonzero if TM_YEAR is a struct tm's tm_year value that causes
75 asctime to have well-defined behavior. */
76 #ifndef TM_YEAR_IN_ASCTIME_RANGE
77 # define TM_YEAR_IN_ASCTIME_RANGE(tm_year) \
78 (1000 - TM_YEAR_BASE <= (tm_year) && (tm_year) <= 9999 - TM_YEAR_BASE)
86 struct line_record
*continuation
;
88 typedef struct line_record
*line_list
;
93 struct header_record
*next
;
94 struct header_record
*previous
;
96 typedef struct header_record
*header
;
102 struct stream_record
*rest_streams
;
104 typedef struct stream_record
*stream_list
;
106 /* A `struct linebuffer' is a structure which holds a line of text.
107 * `readline' reads a line from a stream into a linebuffer
108 * and works regardless of the length of the line.
117 struct linebuffer lb
;
120 ((line_list) xmalloc (sizeof (struct line_record)))
121 #define new_header() \
122 ((header) xmalloc (sizeof (struct header_record)))
123 #define new_stream() \
124 ((stream_list) xmalloc (sizeof (struct stream_record)))
125 #define alloc_string(nchars) \
126 ((char *) xmalloc ((nchars) + 1))
128 /* Global declarations */
131 #define KEYWORD_SIZE 256
132 #define FROM_PREFIX "From"
133 #define MY_NAME "fakemail"
134 #define NIL ((line_list) NULL)
135 #define INITIAL_LINE_SIZE 200
137 #ifndef MAIL_PROGRAM_NAME
138 #define MAIL_PROGRAM_NAME "/bin/mail"
141 static char *my_name
;
142 static char *the_date
;
143 static char *the_user
;
144 static line_list file_preface
;
145 static stream_list the_streams
;
146 static boolean no_problems
= true;
148 extern FILE *popen ();
149 extern int fclose (), pclose ();
152 extern struct passwd
*getpwuid ();
153 extern unsigned short geteuid ();
154 static struct passwd
*my_entry
;
156 (my_entry = getpwuid (((int) geteuid ())), \
162 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
168 printf ("%s: ", my_name
);
174 /* Print error message and exit. */
184 /* Like malloc but get fatal error if memory is exhausted. */
190 long *result
= (long *) malloc (((unsigned) size
));
191 if (result
== ((long *) NULL
))
192 fatal ("virtual memory exhausted");
201 long *result
= (long *) realloc (ptr
, ((unsigned) size
));
202 if (result
== ((long *) NULL
))
203 fatal ("virtual memory exhausted");
207 /* Initialize a linebuffer for use */
210 init_linebuffer (linebuffer
)
211 struct linebuffer
*linebuffer
;
213 linebuffer
->size
= INITIAL_LINE_SIZE
;
214 linebuffer
->buffer
= ((char *) xmalloc (INITIAL_LINE_SIZE
));
217 /* Read a line of text from `stream' into `linebuffer'.
218 Return the length of the line. */
221 readline (linebuffer
, stream
)
222 struct linebuffer
*linebuffer
;
225 char *buffer
= linebuffer
->buffer
;
226 char *p
= linebuffer
->buffer
;
227 char *end
= p
+ linebuffer
->size
;
231 int c
= getc (stream
);
234 linebuffer
->size
*= 2;
235 buffer
= ((char *) xrealloc ((long *)buffer
, linebuffer
->size
));
236 p
= buffer
+ (p
- linebuffer
->buffer
);
237 end
= buffer
+ linebuffer
->size
;
238 linebuffer
->buffer
= buffer
;
240 if (c
< 0 || c
== '\n')
251 /* Extract a colon-terminated keyword from the string FIELD.
252 Return that keyword as a string stored in a static buffer.
253 Store the address of the rest of the string into *REST.
255 If there is no keyword, return NULL and don't alter *REST. */
258 get_keyword (field
, rest
)
259 register char *field
;
262 static char keyword
[KEYWORD_SIZE
];
267 c
= (unsigned char) *field
++;
268 if (isspace (c
) || c
== ':')
269 return ((char *) NULL
);
270 *ptr
++ = (islower (c
) ? toupper (c
) : c
);
271 while (((c
= (unsigned char) *field
++) != ':') && ! isspace (c
))
272 *ptr
++ = (islower (c
) ? toupper (c
) : c
);
275 c
= (unsigned char) *field
++;
277 return ((char *) NULL
);
282 /* Nonzero if the string FIELD starts with a colon-terminated keyword. */
289 return (get_keyword (field
, &ignored
) != ((char *) NULL
));
292 /* Store the string FIELD, followed by any lines in THE_LIST,
293 into the buffer WHERE.
294 Concatenate lines, putting just a space between them.
295 Delete everything contained in parentheses.
296 When a recipient name contains <...>, we discard
297 everything except what is inside the <...>.
299 We don't pay attention to overflowing WHERE;
300 the caller has to make it big enough. */
303 add_field (the_list
, field
, where
)
305 register char *field
, *where
;
310 char *this_recipient_where
;
314 this_recipient_where
= where
;
316 while ((c
= *field
++) != '\0')
322 in_quotes
= ! in_quotes
;
329 while (*field
&& *field
!= ')') ++field
;
330 if (! (*field
++)) break; /* no close */
336 /* When we get to the end of one recipient,
337 don't discard it if the next one has <...>. */
338 this_recipient_where
= where
;
341 /* Discard everything we got before the `<'. */
342 where
= this_recipient_where
;
344 /* Discard the rest of this name that follows the `>'. */
346 while (*field
&& *field
!= ',') ++field
;
347 if (! (*field
++)) break; /* no comma */
353 if (the_list
== NIL
) break;
354 field
= the_list
->string
;
355 the_list
= the_list
->continuation
;
363 char *the_string
, *temp
;
364 long idiotic_interface
;
371 prefix_length
= strlen (FROM_PREFIX
);
372 time (&idiotic_interface
);
373 /* Convert to a string, checking for out-of-range time stamps.
374 Don't use 'ctime', as that might dump core if the hardware clock
375 is set to a bizarre value. */
376 tm
= localtime (&idiotic_interface
);
377 if (! (tm
&& TM_YEAR_IN_ASCTIME_RANGE (tm
->tm_year
)
378 && (the_date
= asctime (tm
))))
379 fatal ("current time is out of range");
380 /* the_date has an unwanted newline at the end */
381 date_length
= strlen (the_date
) - 1;
382 the_date
[date_length
] = '\0';
383 temp
= cuserid ((char *) NULL
);
384 user_length
= strlen (temp
);
385 the_user
= alloc_string (user_length
+ 1);
386 strcpy (the_user
, temp
);
387 the_string
= alloc_string (3 + prefix_length
391 strcpy (temp
, FROM_PREFIX
);
392 temp
= &temp
[prefix_length
];
394 strcpy (temp
, the_user
);
395 temp
= &temp
[user_length
];
397 strcpy (temp
, the_date
);
398 result
= new_list ();
399 result
->string
= the_string
;
400 result
->continuation
= ((line_list
) NULL
);
405 write_line_list (the_list
, the_stream
)
406 register line_list the_list
;
410 the_list
!= ((line_list
) NULL
) ;
411 the_list
= the_list
->continuation
)
413 fputs (the_list
->string
, the_stream
);
414 putc ('\n', the_stream
);
422 register stream_list rem
;
423 for (rem
= the_streams
;
424 rem
!= ((stream_list
) NULL
);
425 rem
= rem
->rest_streams
)
426 no_problems
= (no_problems
&&
427 ((*rem
->action
) (rem
->handle
) == 0));
428 the_streams
= ((stream_list
) NULL
);
429 return (no_problems
? EXIT_SUCCESS
: EXIT_FAILURE
);
433 add_a_stream (the_stream
, closing_action
)
435 int (*closing_action
)();
437 stream_list old
= the_streams
;
438 the_streams
= new_stream ();
439 the_streams
->handle
= the_stream
;
440 the_streams
->action
= closing_action
;
441 the_streams
->rest_streams
= old
;
449 putc ('\n', the_file
);
451 return fclose (the_file
);
458 FILE *the_stream
= fopen (name
, "a");
459 if (the_stream
!= ((FILE *) NULL
))
461 add_a_stream (the_stream
, my_fclose
);
462 if (the_user
== ((char *) NULL
))
463 file_preface
= make_file_preface ();
464 write_line_list (file_preface
, the_stream
);
474 register stream_list rem
;
475 for (rem
= the_streams
;
476 rem
!= ((stream_list
) NULL
);
477 rem
= rem
->rest_streams
)
478 fputs (s
, rem
->handle
);
486 register stream_list rem
;
487 for (rem
= the_streams
;
488 rem
!= ((stream_list
) NULL
);
489 rem
= rem
->rest_streams
)
494 /* Divide STRING into lines. */
499 /* Find the last char that fits. */
500 for (breakpos
= s
; *breakpos
&& column
< 78; ++breakpos
)
502 if (*breakpos
== '\t')
507 /* If we didn't reach end of line, break the line. */
510 /* Back up to just after the last comma that fits. */
511 while (breakpos
!= s
&& breakpos
[-1] != ',') --breakpos
;
515 /* If no comma fits, move past the first address anyway. */
516 while (*breakpos
!= 0 && *breakpos
!= ',') ++breakpos
;
518 /* Include the comma after it. */
522 /* Output that much, then break the line. */
523 fwrite (s
, 1, breakpos
- s
, rem
->handle
);
526 /* Skip whitespace and prepare to print more addresses. */
528 while (*s
== ' ' || *s
== '\t') ++s
;
530 fputs ("\n\t", rem
->handle
);
532 putc ('\n', rem
->handle
);
537 #define mail_error error
539 /* Handle an FCC field. FIELD is the text of the first line (after
540 the header name), and THE_LIST holds the continuation lines if any.
541 Call open_a_file for each file. */
544 setup_files (the_list
, field
)
545 register line_list the_list
;
546 register char *field
;
548 register char *start
;
552 while (((c
= *field
) != '\0')
560 while (((c
= *field
) != '\0')
566 if (!open_a_file (start
))
567 mail_error ("Could not open file %s", start
);
569 if (c
!= '\0') continue;
571 if (the_list
== ((line_list
) NULL
))
573 field
= the_list
->string
;
574 the_list
= the_list
->continuation
;
578 /* Compute the total size of all recipient names stored in THE_HEADER.
579 The result says how big to make the buffer to pass to parse_header. */
582 args_size (the_header
)
585 register header old
= the_header
;
586 register line_list rem
;
587 register int size
= 0;
591 register char *keyword
= get_keyword (the_header
->text
->string
, &field
);
592 if ((strcmp (keyword
, "TO") == 0)
593 || (strcmp (keyword
, "CC") == 0)
594 || (strcmp (keyword
, "BCC") == 0))
596 size
+= 1 + strlen (field
);
597 for (rem
= the_header
->text
->continuation
;
599 rem
= rem
->continuation
)
600 size
+= 1 + strlen (rem
->string
);
602 the_header
= the_header
->next
;
603 } while (the_header
!= old
);
607 /* Scan the header described by the lists THE_HEADER,
608 and put all recipient names into the buffer WHERE.
609 Precede each recipient name with a space.
611 Also, if the header has any FCC fields, call setup_files for each one. */
614 parse_header (the_header
, where
)
616 register char *where
;
618 register header old
= the_header
;
622 register char *keyword
= get_keyword (the_header
->text
->string
, &field
);
623 if (strcmp (keyword
, "TO") == 0)
624 where
= add_field (the_header
->text
->continuation
, field
, where
);
625 else if (strcmp (keyword
, "CC") == 0)
626 where
= add_field (the_header
->text
->continuation
, field
, where
);
627 else if (strcmp (keyword
, "BCC") == 0)
629 where
= add_field (the_header
->text
->continuation
, field
, where
);
630 the_header
->previous
->next
= the_header
->next
;
631 the_header
->next
->previous
= the_header
->previous
;
633 else if (strcmp (keyword
, "FCC") == 0)
634 setup_files (the_header
->text
->continuation
, field
);
635 the_header
= the_header
->next
;
636 } while (the_header
!= old
);
641 /* Read lines from the input until we get a blank line.
642 Create a list of `header' objects, one for each header field,
643 each of which points to a list of `line_list' objects,
644 one for each line in that field.
645 Continuation lines are grouped in the headers they continue. */
650 register header the_header
= ((header
) NULL
);
651 register line_list
*next_line
= ((line_list
*) NULL
);
653 init_linebuffer (&lb
);
660 readline (&lb
, stdin
);
662 length
= strlen (line
);
663 if (length
== 0) break;
665 if (has_keyword (line
))
667 register header old
= the_header
;
668 the_header
= new_header ();
669 if (old
== ((header
) NULL
))
671 the_header
->next
= the_header
;
672 the_header
->previous
= the_header
;
676 the_header
->previous
= old
;
677 the_header
->next
= old
->next
;
678 old
->next
= the_header
;
680 next_line
= &(the_header
->text
);
683 if (next_line
== ((line_list
*) NULL
))
685 /* Not a valid header */
688 *next_line
= new_list ();
689 (*next_line
)->string
= alloc_string (length
);
690 strcpy (((*next_line
)->string
), line
);
691 next_line
= &((*next_line
)->continuation
);
697 fatal ("input message has no header");
698 return the_header
->next
;
702 write_header (the_header
)
705 register header old
= the_header
;
708 register line_list the_list
;
709 for (the_list
= the_header
->text
;
711 the_list
= the_list
->continuation
)
712 put_line (the_list
->string
);
713 the_header
= the_header
->next
;
714 } while (the_header
!= old
);
727 char *mail_program_name
;
728 char buf
[BUFLEN
+ 1];
732 extern char *getenv ();
734 mail_program_name
= getenv ("FAKEMAILER");
735 if (!(mail_program_name
&& *mail_program_name
))
736 mail_program_name
= MAIL_PROGRAM_NAME
;
737 name_length
= strlen (mail_program_name
);
740 the_streams
= ((stream_list
) NULL
);
741 the_date
= ((char *) NULL
);
742 the_user
= ((char *) NULL
);
744 the_header
= read_header ();
745 command_line
= alloc_string (name_length
+ args_size (the_header
));
746 strcpy (command_line
, mail_program_name
);
747 parse_header (the_header
, &command_line
[name_length
]);
749 the_pipe
= popen (command_line
, "w");
750 if (the_pipe
== ((FILE *) NULL
))
751 fatal ("cannot open pipe to real mailer");
753 add_a_stream (the_pipe
, pclose
);
755 write_header (the_header
);
757 /* Dump the message itself */
759 while (!feof (stdin
))
761 size
= fread (buf
, 1, BUFLEN
, stdin
);
766 exit (close_the_streams ());
769 #endif /* not MSDOS */
770 #endif /* not BSD 4.2 (or newer) */
772 /* arch-tag: acb0afa6-315a-4c5b-b9e3-def5725c8783
773 (do not change this comment) */
775 /* fakemail.c ends here */