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 Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include <../src/config.h>
25 #if defined (BSD_SYSTEM) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
26 /* This program isnot used in BSD, so just avoid loader complaints. */
32 #else /* not BSD 4.2 (or newer) */
40 /* This conditional contains all the rest of the file. */
42 /* These are defined in config in some versions. */
65 /* This is to declare cuserid. */
70 /* Type definitions */
81 struct line_record
*continuation
;
83 typedef struct line_record
*line_list
;
88 struct header_record
*next
;
89 struct header_record
*previous
;
91 typedef struct header_record
*header
;
97 struct stream_record
*rest_streams
;
99 typedef struct stream_record
*stream_list
;
101 /* A `struct linebuffer' is a structure which holds a line of text.
102 * `readline' reads a line from a stream into a linebuffer
103 * and works regardless of the length of the line.
112 struct linebuffer lb
;
115 ((line_list) xmalloc (sizeof (struct line_record)))
116 #define new_header() \
117 ((header) xmalloc (sizeof (struct header_record)))
118 #define new_stream() \
119 ((stream_list) xmalloc (sizeof (struct stream_record)))
120 #define alloc_string(nchars) \
121 ((char *) xmalloc ((nchars) + 1))
123 /* Global declarations */
126 #define KEYWORD_SIZE 256
127 #define FROM_PREFIX "From"
128 #define MY_NAME "fakemail"
129 #define NIL ((line_list) NULL)
130 #define INITIAL_LINE_SIZE 200
132 #ifndef MAIL_PROGRAM_NAME
133 #define MAIL_PROGRAM_NAME "/bin/mail"
136 static char *my_name
;
137 static char *the_date
;
138 static char *the_user
;
139 static line_list file_preface
;
140 static stream_list the_streams
;
141 static boolean no_problems
= true;
143 extern FILE *popen ();
144 extern int fclose (), pclose ();
147 extern struct passwd
*getpwuid ();
148 extern unsigned short geteuid ();
149 static struct passwd
*my_entry
;
151 (my_entry = getpwuid (((int) geteuid ())), \
157 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
163 printf ("%s: ", my_name
);
169 /* Print error message and exit. */
179 /* Like malloc but get fatal error if memory is exhausted. */
185 long *result
= (long *) malloc (((unsigned) size
));
186 if (result
== ((long *) NULL
))
187 fatal ("virtual memory exhausted", 0);
196 long *result
= (long *) realloc (ptr
, ((unsigned) size
));
197 if (result
== ((long *) NULL
))
198 fatal ("virtual memory exhausted");
202 /* Initialize a linebuffer for use */
205 init_linebuffer (linebuffer
)
206 struct linebuffer
*linebuffer
;
208 linebuffer
->size
= INITIAL_LINE_SIZE
;
209 linebuffer
->buffer
= ((char *) xmalloc (INITIAL_LINE_SIZE
));
212 /* Read a line of text from `stream' into `linebuffer'.
213 * Return the length of the line.
217 readline (linebuffer
, stream
)
218 struct linebuffer
*linebuffer
;
221 char *buffer
= linebuffer
->buffer
;
222 char *p
= linebuffer
->buffer
;
223 char *end
= p
+ linebuffer
->size
;
227 int c
= getc (stream
);
230 linebuffer
->size
*= 2;
231 buffer
= ((char *) xrealloc (buffer
, linebuffer
->size
));
232 p
= buffer
+ (p
- linebuffer
->buffer
);
233 end
= buffer
+ linebuffer
->size
;
234 linebuffer
->buffer
= buffer
;
236 if (c
< 0 || c
== '\n')
247 /* Extract a colon-terminated keyword from the string FIELD.
248 Return that keyword as a string stored in a static buffer.
249 Store the address of the rest of the string into *REST.
251 If there is no keyword, return NULL and don't alter *REST. */
254 get_keyword (field
, rest
)
255 register char *field
;
258 static char keyword
[KEYWORD_SIZE
];
264 if (isspace (c
) || c
== ':')
265 return ((char *) NULL
);
266 *ptr
++ = (islower (c
) ? toupper (c
) : c
);
267 while (((c
= *field
++) != ':') && ! isspace (c
))
268 *ptr
++ = (islower (c
) ? toupper (c
) : c
);
273 return ((char *) NULL
);
278 /* Nonzero if the string FIELD starts with a colon-terminated keyword. */
285 return (get_keyword (field
, &ignored
) != ((char *) NULL
));
288 /* Store the string FIELD, followed by any lines in THE_LIST,
289 into the buffer WHERE.
290 Concatenate lines, putting just a space between them.
291 Delete everything contained in parentheses.
292 When a recipient name contains <...>, we discard
293 everything except what is inside the <...>.
295 We don't pay attention to overflowing WHERE;
296 the caller has to make it big enough. */
299 add_field (the_list
, field
, where
)
301 register char *field
, *where
;
306 char *this_recipient_where
;
310 this_recipient_where
= where
;
312 while ((c
= *field
++) != '\0')
318 in_quotes
= ! in_quotes
;
325 while (*field
&& *field
!= ')') ++field
;
326 if (! (*field
++)) break; /* no close */
332 /* When we get to the end of one recipient,
333 don't discard it if the next one has <...>. */
334 this_recipient_where
= where
;
337 /* Discard everything we got before the `<'. */
338 where
= this_recipient_where
;
340 /* Discard the rest of this name that follows the `>'. */
342 while (*field
&& *field
!= ',') ++field
;
343 if (! (*field
++)) break; /* no comma */
349 if (the_list
== NIL
) break;
350 field
= the_list
->string
;
351 the_list
= the_list
->continuation
;
359 char *the_string
, *temp
;
360 long idiotic_interface
;
366 prefix_length
= strlen (FROM_PREFIX
);
367 time (&idiotic_interface
);
368 the_date
= ctime (&idiotic_interface
);
369 /* the_date has an unwanted newline at the end */
370 date_length
= strlen (the_date
) - 1;
371 the_date
[date_length
] = '\0';
372 temp
= cuserid ((char *) NULL
);
373 user_length
= strlen (temp
);
374 the_user
= alloc_string (user_length
+ 1);
375 strcpy (the_user
, temp
);
376 the_string
= alloc_string (3 + prefix_length
380 strcpy (temp
, FROM_PREFIX
);
381 temp
= &temp
[prefix_length
];
383 strcpy (temp
, the_user
);
384 temp
= &temp
[user_length
];
386 strcpy (temp
, the_date
);
387 result
= new_list ();
388 result
->string
= the_string
;
389 result
->continuation
= ((line_list
) NULL
);
394 write_line_list (the_list
, the_stream
)
395 register line_list the_list
;
399 the_list
!= ((line_list
) NULL
) ;
400 the_list
= the_list
->continuation
)
402 fputs (the_list
->string
, the_stream
);
403 putc ('\n', the_stream
);
411 register stream_list rem
;
412 for (rem
= the_streams
;
413 rem
!= ((stream_list
) NULL
);
414 rem
= rem
->rest_streams
)
415 no_problems
= (no_problems
&&
416 ((*rem
->action
) (rem
->handle
) == 0));
417 the_streams
= ((stream_list
) NULL
);
418 return (no_problems
? 0 : 1);
422 add_a_stream (the_stream
, closing_action
)
424 int (*closing_action
)();
426 stream_list old
= the_streams
;
427 the_streams
= new_stream ();
428 the_streams
->handle
= the_stream
;
429 the_streams
->action
= closing_action
;
430 the_streams
->rest_streams
= old
;
438 putc ('\n', the_file
);
440 return fclose (the_file
);
447 FILE *the_stream
= fopen (name
, "a");
448 if (the_stream
!= ((FILE *) NULL
))
450 add_a_stream (the_stream
, my_fclose
);
451 if (the_user
== ((char *) NULL
))
452 file_preface
= make_file_preface ();
453 write_line_list (file_preface
, the_stream
);
463 register stream_list rem
;
464 for (rem
= the_streams
;
465 rem
!= ((stream_list
) NULL
);
466 rem
= rem
->rest_streams
)
467 fputs (s
, rem
->handle
);
475 register stream_list rem
;
476 for (rem
= the_streams
;
477 rem
!= ((stream_list
) NULL
);
478 rem
= rem
->rest_streams
)
483 /* Divide STRING into lines. */
488 /* Find the last char that fits. */
489 for (breakpos
= s
; *breakpos
&& column
< 78; ++breakpos
)
491 if (*breakpos
== '\t')
496 /* If we didn't reach end of line, break the line. */
499 /* Back up to just after the last comma that fits. */
500 while (breakpos
!= s
&& breakpos
[-1] != ',') --breakpos
;
504 /* If no comma fits, move past the first address anyway. */
505 while (*breakpos
!= 0 && *breakpos
!= ',') ++breakpos
;
507 /* Include the comma after it. */
511 /* Output that much, then break the line. */
512 fwrite (s
, 1, breakpos
- s
, rem
->handle
);
515 /* Skip whitespace and prepare to print more addresses. */
517 while (*s
== ' ' || *s
== '\t') ++s
;
519 fputs ("\n\t", rem
->handle
);
521 putc ('\n', rem
->handle
);
526 #define mail_error error
528 /* Handle an FCC field. FIELD is the text of the first line (after
529 the header name), and THE_LIST holds the continuation lines if any.
530 Call open_a_file for each file. */
533 setup_files (the_list
, field
)
534 register line_list the_list
;
535 register char *field
;
537 register char *start
;
541 while (((c
= *field
) != '\0')
549 while (((c
= *field
) != '\0')
555 if (!open_a_file (start
))
556 mail_error ("Could not open file %s", start
);
558 if (c
!= '\0') continue;
560 if (the_list
== ((line_list
) NULL
))
562 field
= the_list
->string
;
563 the_list
= the_list
->continuation
;
567 /* Compute the total size of all recipient names stored in THE_HEADER.
568 The result says how big to make the buffer to pass to parse_header. */
571 args_size (the_header
)
574 register header old
= the_header
;
575 register line_list rem
;
576 register int size
= 0;
580 register char *keyword
= get_keyword (the_header
->text
->string
, &field
);
581 if ((strcmp (keyword
, "TO") == 0)
582 || (strcmp (keyword
, "CC") == 0)
583 || (strcmp (keyword
, "BCC") == 0))
585 size
+= 1 + strlen (field
);
586 for (rem
= the_header
->text
->continuation
;
588 rem
= rem
->continuation
)
589 size
+= 1 + strlen (rem
->string
);
591 the_header
= the_header
->next
;
592 } while (the_header
!= old
);
596 /* Scan the header described by the lists THE_HEADER,
597 and put all recipient names into the buffer WHERE.
598 Precede each recipient name with a space.
600 Also, if the header has any FCC fields, call setup_files for each one. */
602 parse_header (the_header
, where
)
604 register char *where
;
606 register header old
= the_header
;
610 register char *keyword
= get_keyword (the_header
->text
->string
, &field
);
611 if (strcmp (keyword
, "TO") == 0)
612 where
= add_field (the_header
->text
->continuation
, field
, where
);
613 else if (strcmp (keyword
, "CC") == 0)
614 where
= add_field (the_header
->text
->continuation
, field
, where
);
615 else if (strcmp (keyword
, "BCC") == 0)
617 where
= add_field (the_header
->text
->continuation
, field
, where
);
618 the_header
->previous
->next
= the_header
->next
;
619 the_header
->next
->previous
= the_header
->previous
;
621 else if (strcmp (keyword
, "FCC") == 0)
622 setup_files (the_header
->text
->continuation
, field
);
623 the_header
= the_header
->next
;
624 } while (the_header
!= old
);
629 /* Read lines from the input until we get a blank line.
630 Create a list of `header' objects, one for each header field,
631 each of which points to a list of `line_list' objects,
632 one for each line in that field.
633 Continuation lines are grouped in the headers they continue. */
638 register header the_header
= ((header
) NULL
);
639 register line_list
*next_line
= ((line_list
*) NULL
);
641 init_linebuffer (&lb
);
648 readline (&lb
, stdin
);
650 length
= strlen (line
);
651 if (length
== 0) break;
653 if (has_keyword (line
))
655 register header old
= the_header
;
656 the_header
= new_header ();
657 if (old
== ((header
) NULL
))
659 the_header
->next
= the_header
;
660 the_header
->previous
= the_header
;
664 the_header
->previous
= old
;
665 the_header
->next
= old
->next
;
666 old
->next
= the_header
;
668 next_line
= &(the_header
->text
);
671 if (next_line
== ((line_list
*) NULL
))
673 /* Not a valid header */
676 *next_line
= new_list ();
677 (*next_line
)->string
= alloc_string (length
);
678 strcpy (((*next_line
)->string
), line
);
679 next_line
= &((*next_line
)->continuation
);
684 return the_header
->next
;
688 write_header (the_header
)
691 register header old
= the_header
;
694 register line_list the_list
;
695 for (the_list
= the_header
->text
;
697 the_list
= the_list
->continuation
)
698 put_line (the_list
->string
);
699 the_header
= the_header
->next
;
700 } while (the_header
!= old
);
713 char *mail_program_name
;
714 char buf
[BUFLEN
+ 1];
718 extern char *getenv ();
720 mail_program_name
= getenv ("FAKEMAILER");
721 if (!(mail_program_name
&& *mail_program_name
))
722 mail_program_name
= MAIL_PROGRAM_NAME
;
723 name_length
= strlen (mail_program_name
);
726 the_streams
= ((stream_list
) NULL
);
727 the_date
= ((char *) NULL
);
728 the_user
= ((char *) NULL
);
730 the_header
= read_header ();
731 command_line
= alloc_string (name_length
+ args_size (the_header
));
732 strcpy (command_line
, mail_program_name
);
733 parse_header (the_header
, &command_line
[name_length
]);
735 the_pipe
= popen (command_line
, "w");
736 if (the_pipe
== ((FILE *) NULL
))
737 fatal ("cannot open pipe to real mailer");
739 add_a_stream (the_pipe
, pclose
);
741 write_header (the_header
);
743 /* Dump the message itself */
745 while (!feof (stdin
))
747 size
= fread (buf
, 1, BUFLEN
, stdin
);
752 exit (close_the_streams ());
755 #endif /* not MSDOS */
756 #endif /* not BSD 4.2 (or newer) */