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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include <../src/config.h>
24 #if defined (BSD) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
25 /* This program isnot used in BSD, so just avoid loader complaints. */
30 #else /* not BSD 4.2 (or newer) */
37 /* This conditional contains all the rest of the file. */
39 /* These are defined in config in some versions. */
58 /* Type definitions */
69 struct line_record
*continuation
;
71 typedef struct line_record
*line_list
;
76 struct header_record
*next
;
77 struct header_record
*previous
;
79 typedef struct header_record
*header
;
85 struct stream_record
*rest_streams
;
87 typedef struct stream_record
*stream_list
;
89 /* A `struct linebuffer' is a structure which holds a line of text.
90 * `readline' reads a line from a stream into a linebuffer
91 * and works regardless of the length of the line.
100 struct linebuffer lb
;
103 ((line_list) xmalloc (sizeof (struct line_record)))
104 #define new_header() \
105 ((header) xmalloc (sizeof (struct header_record)))
106 #define new_stream() \
107 ((stream_list) xmalloc (sizeof (struct stream_record)))
108 #define alloc_string(nchars) \
109 ((char *) xmalloc ((nchars) + 1))
111 /* Global declarations */
114 #define KEYWORD_SIZE 256
115 #define FROM_PREFIX "From"
116 #define MY_NAME "fakemail"
117 #define NIL ((line_list) NULL)
118 #define INITIAL_LINE_SIZE 200
120 #ifndef MAIL_PROGRAM_NAME
121 #define MAIL_PROGRAM_NAME "/bin/mail"
124 static char *my_name
;
125 static char *the_date
;
126 static char *the_user
;
127 static line_list file_preface
;
128 static stream_list the_streams
;
129 static boolean no_problems
= true;
131 extern FILE *popen ();
132 extern int fclose (), pclose ();
135 extern struct passwd
*getpwuid ();
136 extern unsigned short geteuid ();
137 static struct passwd
*my_entry
;
139 (my_entry = getpwuid (((int) geteuid ())), \
145 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
151 printf ("%s: ", my_name
);
157 /* Print error message and exit. */
167 /* Like malloc but get fatal error if memory is exhausted. */
173 char *result
= malloc (((unsigned) size
));
174 if (result
== ((char *) NULL
))
175 fatal ("virtual memory exhausted", 0);
184 char *result
= realloc (ptr
, ((unsigned) size
));
185 if (result
== ((char *) NULL
))
186 fatal ("virtual memory exhausted");
190 /* Initialize a linebuffer for use */
193 init_linebuffer (linebuffer
)
194 struct linebuffer
*linebuffer
;
196 linebuffer
->size
= INITIAL_LINE_SIZE
;
197 linebuffer
->buffer
= ((char *) xmalloc (INITIAL_LINE_SIZE
));
200 /* Read a line of text from `stream' into `linebuffer'.
201 * Return the length of the line.
205 readline (linebuffer
, stream
)
206 struct linebuffer
*linebuffer
;
209 char *buffer
= linebuffer
->buffer
;
210 char *p
= linebuffer
->buffer
;
211 char *end
= p
+ linebuffer
->size
;
215 int c
= getc (stream
);
218 linebuffer
->size
*= 2;
219 buffer
= ((char *) xrealloc (buffer
, linebuffer
->size
));
220 p
= buffer
+ (p
- linebuffer
->buffer
);
221 end
= buffer
+ linebuffer
->size
;
222 linebuffer
->buffer
= buffer
;
224 if (c
< 0 || c
== '\n')
236 get_keyword (field
, rest
)
237 register char *field
;
240 static char keyword
[KEYWORD_SIZE
];
246 if ((isspace (c
)) || (c
== ':'))
247 return ((char *) NULL
);
248 *ptr
++ = ((islower (c
)) ? (toupper (c
)) : c
);
249 while (((c
= *field
++) != ':') && (!(isspace (c
))))
250 *ptr
++ = ((islower (c
)) ? (toupper (c
)) : c
);
252 while (isspace (c
)) c
= *field
++;
253 if (c
!= ':') return ((char *) NULL
);
263 return (get_keyword (field
, &ignored
) != ((char *) NULL
));
267 add_field (the_list
, field
, where
)
269 register char *field
, *where
;
275 while ((c
= *field
++) != '\0')
279 while (*field
&& *field
!= ')') ++field
;
280 if (! (*field
++)) break; /* no closer */
281 if (! (*field
)) break; /* closerNULL */
284 *where
++ = ((c
== ','||c
=='>'||c
=='<') ? ' ' : c
);
286 if (the_list
== NIL
) break;
287 field
= the_list
->string
;
288 the_list
= the_list
->continuation
;
296 char *the_string
, *temp
;
297 long idiotic_interface
;
303 prefix_length
= strlen (FROM_PREFIX
);
304 time (&idiotic_interface
);
305 the_date
= ctime (&idiotic_interface
);
306 /* the_date has an unwanted newline at the end */
307 date_length
= strlen (the_date
) - 1;
308 the_date
[date_length
] = '\0';
309 temp
= cuserid ((char *) NULL
);
310 user_length
= strlen (temp
);
311 the_user
= alloc_string (user_length
+ 1);
312 strcpy (the_user
, temp
);
313 the_string
= alloc_string (3 + prefix_length
+
317 strcpy (temp
, FROM_PREFIX
);
318 temp
= &temp
[prefix_length
];
320 strcpy (temp
, the_user
);
321 temp
= &temp
[user_length
];
323 strcpy (temp
, the_date
);
324 result
= new_list ();
325 result
->string
= the_string
;
326 result
->continuation
= ((line_list
) NULL
);
331 write_line_list (the_list
, the_stream
)
332 register line_list the_list
;
336 the_list
!= ((line_list
) NULL
) ;
337 the_list
= the_list
->continuation
)
339 fputs (the_list
->string
, the_stream
);
340 putc ('\n', the_stream
);
348 register stream_list rem
;
349 for (rem
= the_streams
;
350 rem
!= ((stream_list
) NULL
);
351 rem
= rem
->rest_streams
)
352 no_problems
= (no_problems
&&
353 ((*rem
->action
) (rem
->handle
) == 0));
354 the_streams
= ((stream_list
) NULL
);
355 return (no_problems
? 0 : 1);
359 add_a_stream (the_stream
, closing_action
)
361 int (*closing_action
)();
363 stream_list old
= the_streams
;
364 the_streams
= new_stream ();
365 the_streams
->handle
= the_stream
;
366 the_streams
->action
= closing_action
;
367 the_streams
->rest_streams
= old
;
375 putc ('\n', the_file
);
377 return fclose (the_file
);
384 FILE *the_stream
= fopen (name
, "a");
385 if (the_stream
!= ((FILE *) NULL
))
387 add_a_stream (the_stream
, my_fclose
);
388 if (the_user
== ((char *) NULL
))
389 file_preface
= make_file_preface ();
390 write_line_list (file_preface
, the_stream
);
400 register stream_list rem
;
401 for (rem
= the_streams
;
402 rem
!= ((stream_list
) NULL
);
403 rem
= rem
->rest_streams
)
404 fputs (s
, rem
->handle
);
412 register stream_list rem
;
413 for (rem
= the_streams
;
414 rem
!= ((stream_list
) NULL
);
415 rem
= rem
->rest_streams
)
420 /* Divide STRING into lines. */
425 /* Find the last char that fits. */
426 for (breakpos
= s
; *breakpos
&& column
< 78; ++breakpos
)
428 if (*breakpos
== '\t')
433 /* If we didn't reach end of line, break the line. */
436 /* Back up to just after the last comma that fits. */
437 while (breakpos
!= s
&& breakpos
[-1] != ',') --breakpos
;
441 /* If no comma fits, move past the first address anyway. */
442 while (*breakpos
!= 0 && *breakpos
!= ',') ++breakpos
;
444 /* Include the comma after it. */
448 /* Output that much, then break the line. */
449 fwrite (s
, 1, breakpos
- s
, rem
->handle
);
452 /* Skip whitespace and prepare to print more addresses. */
454 while (*s
== ' ' || *s
== '\t') ++s
;
456 fputs ("\n\t", rem
->handle
);
458 putc ('\n', rem
->handle
);
463 #define mail_error error
466 setup_files (the_list
, field
)
467 register line_list the_list
;
468 register char *field
;
470 register char *start
;
474 while (((c
= *field
) != '\0') &&
482 while (((c
= *field
) != '\0') &&
488 if (!open_a_file (start
))
489 mail_error ("Could not open file %s", start
);
491 if (c
!= '\0') continue;
493 if (the_list
== ((line_list
) NULL
)) return;
494 field
= the_list
->string
;
495 the_list
= the_list
->continuation
;
500 args_size (the_header
)
503 register header old
= the_header
;
504 register line_list rem
;
505 register int size
= 0;
509 register char *keyword
= get_keyword (the_header
->text
->string
, &field
);
510 if ((strcmp (keyword
, "TO") == 0) ||
511 (strcmp (keyword
, "CC") == 0) ||
512 (strcmp (keyword
, "BCC") == 0))
514 size
+= 1 + strlen (field
);
515 for (rem
= the_header
->text
->continuation
;
517 rem
= rem
->continuation
)
518 size
+= 1 + strlen (rem
->string
);
520 the_header
= the_header
->next
;
521 } while (the_header
!= old
);
525 parse_header (the_header
, where
)
527 register char *where
;
529 register header old
= the_header
;
533 register char *keyword
= get_keyword (the_header
->text
->string
, &field
);
534 if (strcmp (keyword
, "TO") == 0)
535 where
= add_field (the_header
->text
->continuation
, field
, where
);
536 else if (strcmp (keyword
, "CC") == 0)
537 where
= add_field (the_header
->text
->continuation
, field
, where
);
538 else if (strcmp (keyword
, "BCC") == 0)
540 where
= add_field (the_header
->text
->continuation
, field
, where
);
541 the_header
->previous
->next
= the_header
->next
;
542 the_header
->next
->previous
= the_header
->previous
;
544 else if (strcmp (keyword
, "FCC") == 0)
545 setup_files (the_header
->text
->continuation
, field
);
546 the_header
= the_header
->next
;
547 } while (the_header
!= old
);
555 register header the_header
= ((header
) NULL
);
556 register line_list
*next_line
= ((line_list
*) NULL
);
558 init_linebuffer (&lb
);
565 readline (&lb
, stdin
);
567 length
= strlen (line
);
568 if (length
== 0) break;
570 if (has_keyword (line
))
572 register header old
= the_header
;
573 the_header
= new_header ();
574 if (old
== ((header
) NULL
))
576 the_header
->next
= the_header
;
577 the_header
->previous
= the_header
;
581 the_header
->previous
= old
;
582 the_header
->next
= old
->next
;
583 old
->next
= the_header
;
585 next_line
= &(the_header
->text
);
588 if (next_line
== ((line_list
*) NULL
))
590 /* Not a valid header */
593 *next_line
= new_list ();
594 (*next_line
)->string
= alloc_string (length
);
595 strcpy (((*next_line
)->string
), line
);
596 next_line
= &((*next_line
)->continuation
);
601 return the_header
->next
;
605 write_header (the_header
)
608 register header old
= the_header
;
611 register line_list the_list
;
612 for (the_list
= the_header
->text
;
614 the_list
= the_list
->continuation
)
615 put_line (the_list
->string
);
616 the_header
= the_header
->next
;
617 } while (the_header
!= old
);
630 char *mail_program_name
;
631 char buf
[BUFLEN
+ 1];
635 extern char *getenv ();
637 mail_program_name
= getenv ("FAKEMAILER");
638 if (!(mail_program_name
&& *mail_program_name
))
639 mail_program_name
= MAIL_PROGRAM_NAME
;
640 name_length
= strlen (mail_program_name
);
643 the_streams
= ((stream_list
) NULL
);
644 the_date
= ((char *) NULL
);
645 the_user
= ((char *) NULL
);
647 the_header
= read_header ();
648 command_line
= alloc_string (name_length
+ args_size (the_header
));
649 strcpy (command_line
, mail_program_name
);
650 parse_header (the_header
, &command_line
[name_length
]);
652 the_pipe
= popen (command_line
, "w");
653 if (the_pipe
== ((FILE *) NULL
))
654 fatal ("cannot open pipe to real mailer");
656 add_a_stream (the_pipe
, pclose
);
658 write_header (the_header
);
660 /* Dump the message itself */
662 while (!feof (stdin
))
664 size
= fread (buf
, 1, BUFLEN
, stdin
);
669 exit (close_the_streams ());
672 #endif /* not MSDOS */
673 #endif /* not BSD 4.2 (or newer) */