1 /* sendmail-like interface to /bin/mail for system V,
2 Copyright (C) 1985 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. */
29 #else /* not BSD 4.2 (or newer) */
35 /* This conditional contains all the rest of the file. */
37 /* These are defined in config in some versions. */
56 /* Type definitions */
67 struct line_record
*continuation
;
69 typedef struct line_record
*line_list
;
74 struct header_record
*next
;
75 struct header_record
*previous
;
77 typedef struct header_record
*header
;
83 struct stream_record
*rest_streams
;
85 typedef struct stream_record
*stream_list
;
87 /* A `struct linebuffer' is a structure which holds a line of text.
88 * `readline' reads a line from a stream into a linebuffer
89 * and works regardless of the length of the line.
101 ((line_list) xmalloc (sizeof (struct line_record)))
102 #define new_header() \
103 ((header) xmalloc (sizeof (struct header_record)))
104 #define new_stream() \
105 ((stream_list) xmalloc (sizeof (struct stream_record)))
106 #define alloc_string(nchars) \
107 ((char *) xmalloc ((nchars) + 1))
109 /* Global declarations */
112 #define KEYWORD_SIZE 256
113 #define FROM_PREFIX "From"
114 #define MY_NAME "fakemail"
115 #define NIL ((line_list) NULL)
116 #define INITIAL_LINE_SIZE 200
118 #ifndef MAIL_PROGRAM_NAME
119 #define MAIL_PROGRAM_NAME "/bin/mail"
122 static char *my_name
;
123 static char *the_date
;
124 static char *the_user
;
125 static line_list file_preface
;
126 static stream_list the_streams
;
127 static boolean no_problems
= true;
129 extern FILE *popen ();
130 extern int fclose (), pclose ();
131 extern char *malloc (), *realloc ();
134 extern struct passwd
*getpwuid ();
135 extern unsigned short geteuid ();
136 static struct passwd
*my_entry
;
138 (my_entry = getpwuid (((int) geteuid ())), \
144 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
150 printf ("%s: ", my_name
);
156 /* Print error message and exit. */
166 /* Like malloc but get fatal error if memory is exhausted. */
172 char *result
= malloc (((unsigned) size
));
173 if (result
== ((char *) NULL
))
174 fatal ("virtual memory exhausted", 0);
183 char *result
= realloc (ptr
, ((unsigned) size
));
184 if (result
== ((char *) NULL
))
185 fatal ("virtual memory exhausted");
189 /* Initialize a linebuffer for use */
192 init_linebuffer (linebuffer
)
193 struct linebuffer
*linebuffer
;
195 linebuffer
->size
= INITIAL_LINE_SIZE
;
196 linebuffer
->buffer
= ((char *) xmalloc (INITIAL_LINE_SIZE
));
199 /* Read a line of text from `stream' into `linebuffer'.
200 * Return the length of the line.
204 readline (linebuffer
, stream
)
205 struct linebuffer
*linebuffer
;
208 char *buffer
= linebuffer
->buffer
;
209 char *p
= linebuffer
->buffer
;
210 char *end
= p
+ linebuffer
->size
;
214 int c
= getc (stream
);
217 linebuffer
->size
*= 2;
218 buffer
= ((char *) xrealloc (buffer
, linebuffer
->size
));
219 p
+= buffer
- linebuffer
->buffer
;
220 end
+= buffer
- linebuffer
->buffer
;
221 linebuffer
->buffer
= buffer
;
223 if (c
< 0 || c
== '\n')
235 get_keyword (field
, rest
)
236 register char *field
;
239 static char keyword
[KEYWORD_SIZE
];
245 if ((isspace (c
)) || (c
== ':'))
246 return ((char *) NULL
);
247 *ptr
++ = ((islower (c
)) ? (toupper (c
)) : c
);
248 while (((c
= *field
++) != ':') && (!(isspace (c
))))
249 *ptr
++ = ((islower (c
)) ? (toupper (c
)) : c
);
251 while (isspace (c
)) c
= *field
++;
252 if (c
!= ':') return ((char *) NULL
);
262 return (get_keyword (field
, &ignored
) != ((char *) NULL
));
266 add_field (the_list
, field
, where
)
268 register char *field
, *where
;
274 while ((c
= *field
++) != '\0')
278 while (*field
&& *field
!= ')') ++field
;
279 if (! (*field
++)) break; /* no closer */
280 if (! (*field
)) break; /* closerNULL */
283 *where
++ = ((c
== ','||c
=='>'||c
=='<') ? ' ' : c
);
285 if (the_list
== NIL
) break;
286 field
= the_list
->string
;
287 the_list
= the_list
->continuation
;
295 char *the_string
, *temp
;
296 long idiotic_interface
;
302 prefix_length
= strlen (FROM_PREFIX
);
303 time (&idiotic_interface
);
304 the_date
= ctime (&idiotic_interface
);
305 /* the_date has an unwanted newline at the end */
306 date_length
= strlen (the_date
) - 1;
307 the_date
[date_length
] = '\0';
308 temp
= cuserid ((char *) NULL
);
309 user_length
= strlen (temp
);
310 the_user
= alloc_string (user_length
+ 1);
311 strcpy (the_user
, temp
);
312 the_string
= alloc_string (3 + prefix_length
+
316 strcpy (temp
, FROM_PREFIX
);
317 temp
= &temp
[prefix_length
];
319 strcpy (temp
, the_user
);
320 temp
= &temp
[user_length
];
322 strcpy (temp
, the_date
);
323 result
= new_list ();
324 result
->string
= the_string
;
325 result
->continuation
= ((line_list
) NULL
);
330 write_line_list (the_list
, the_stream
)
331 register line_list the_list
;
335 the_list
!= ((line_list
) NULL
) ;
336 the_list
= the_list
->continuation
)
338 fputs (the_list
->string
, the_stream
);
339 putc ('\n', the_stream
);
347 register stream_list rem
;
348 for (rem
= the_streams
;
349 rem
!= ((stream_list
) NULL
);
350 rem
= rem
->rest_streams
)
351 no_problems
= (no_problems
&&
352 ((*rem
->action
) (rem
->handle
) == 0));
353 the_streams
= ((stream_list
) NULL
);
354 return (no_problems
? 0 : 1);
358 add_a_stream (the_stream
, closing_action
)
360 int (*closing_action
)();
362 stream_list old
= the_streams
;
363 the_streams
= new_stream ();
364 the_streams
->handle
= the_stream
;
365 the_streams
->action
= closing_action
;
366 the_streams
->rest_streams
= old
;
374 putc ('\n', the_file
);
376 return fclose (the_file
);
383 FILE *the_stream
= fopen (name
, "a");
384 if (the_stream
!= ((FILE *) NULL
))
386 add_a_stream (the_stream
, my_fclose
);
387 if (the_user
== ((char *) NULL
))
388 file_preface
= make_file_preface ();
389 write_line_list (file_preface
, the_stream
);
399 register stream_list rem
;
400 for (rem
= the_streams
;
401 rem
!= ((stream_list
) NULL
);
402 rem
= rem
->rest_streams
)
403 fputs (s
, rem
->handle
);
411 register stream_list rem
;
412 for (rem
= the_streams
;
413 rem
!= ((stream_list
) NULL
);
414 rem
= rem
->rest_streams
)
419 /* Divide STRING into lines. */
424 /* Find the last char that fits. */
425 for (breakpos
= s
; *breakpos
&& column
< 78; ++breakpos
)
427 if (*breakpos
== '\t')
432 /* If we didn't reach end of line, break the line. */
435 /* Back up to just after the last comma that fits. */
436 while (breakpos
!= s
&& breakpos
[-1] != ',') --breakpos
;
440 /* If no comma fits, move past the first address anyway. */
441 while (*breakpos
!= 0 && *breakpos
!= ',') ++breakpos
;
443 /* Include the comma after it. */
447 /* Output that much, then break the line. */
448 fwrite (s
, 1, breakpos
- s
, rem
->handle
);
451 /* Skip whitespace and prepare to print more addresses. */
453 while (*s
== ' ' || *s
== '\t') ++s
;
455 fputs ("\n\t", rem
->handle
);
457 putc ('\n', rem
->handle
);
462 #define mail_error error
465 setup_files (the_list
, field
)
466 register line_list the_list
;
467 register char *field
;
469 register char *start
;
473 while (((c
= *field
) != '\0') &&
481 while (((c
= *field
) != '\0') &&
487 if (!open_a_file (start
))
488 mail_error ("Could not open file %s", start
);
490 if (c
!= '\0') continue;
492 if (the_list
== ((line_list
) NULL
)) return;
493 field
= the_list
->string
;
494 the_list
= the_list
->continuation
;
499 args_size (the_header
)
502 register header old
= the_header
;
503 register line_list rem
;
504 register int size
= 0;
508 register char *keyword
= get_keyword (the_header
->text
->string
, &field
);
509 if ((strcmp (keyword
, "TO") == 0) ||
510 (strcmp (keyword
, "CC") == 0) ||
511 (strcmp (keyword
, "BCC") == 0))
513 size
+= 1 + strlen (field
);
514 for (rem
= the_header
->text
->continuation
;
516 rem
= rem
->continuation
)
517 size
+= 1 + strlen (rem
->string
);
519 the_header
= the_header
->next
;
520 } while (the_header
!= old
);
524 parse_header (the_header
, where
)
526 register char *where
;
528 register header old
= the_header
;
532 register char *keyword
= get_keyword (the_header
->text
->string
, &field
);
533 if (strcmp (keyword
, "TO") == 0)
534 where
= add_field (the_header
->text
->continuation
, field
, where
);
535 else if (strcmp (keyword
, "CC") == 0)
536 where
= add_field (the_header
->text
->continuation
, field
, where
);
537 else if (strcmp (keyword
, "BCC") == 0)
539 where
= add_field (the_header
->text
->continuation
, field
, where
);
540 the_header
->previous
->next
= the_header
->next
;
541 the_header
->next
->previous
= the_header
->previous
;
543 else if (strcmp (keyword
, "FCC") == 0)
544 setup_files (the_header
->text
->continuation
, field
);
545 the_header
= the_header
->next
;
546 } while (the_header
!= old
);
554 register header the_header
= ((header
) NULL
);
555 register line_list
*next_line
= ((line_list
*) NULL
);
557 init_linebuffer (&lb
);
564 readline (&lb
, stdin
);
566 length
= strlen (line
);
567 if (length
== 0) break;
569 if (has_keyword (line
))
571 register header old
= the_header
;
572 the_header
= new_header ();
573 if (old
== ((header
) NULL
))
575 the_header
->next
= the_header
;
576 the_header
->previous
= the_header
;
580 the_header
->previous
= old
;
581 the_header
->next
= old
->next
;
582 old
->next
= the_header
;
584 next_line
= &(the_header
->text
);
587 if (next_line
== ((line_list
*) NULL
))
589 /* Not a valid header */
592 *next_line
= new_list ();
593 (*next_line
)->string
= alloc_string (length
);
594 strcpy (((*next_line
)->string
), line
);
595 next_line
= &((*next_line
)->continuation
);
600 return the_header
->next
;
604 write_header (the_header
)
607 register header old
= the_header
;
610 register line_list the_list
;
611 for (the_list
= the_header
->text
;
613 the_list
= the_list
->continuation
)
614 put_line (the_list
->string
);
615 the_header
= the_header
->next
;
616 } while (the_header
!= old
);
629 char *mail_program_name
;
630 char buf
[BUFLEN
+ 1];
634 extern char *getenv ();
636 mail_program_name
= getenv ("FAKEMAILER");
637 if (!(mail_program_name
&& *mail_program_name
))
638 mail_program_name
= MAIL_PROGRAM_NAME
;
639 name_length
= strlen (mail_program_name
);
642 the_streams
= ((stream_list
) NULL
);
643 the_date
= ((char *) NULL
);
644 the_user
= ((char *) NULL
);
646 the_header
= read_header ();
647 command_line
= alloc_string (name_length
+ args_size (the_header
));
648 strcpy (command_line
, mail_program_name
);
649 parse_header (the_header
, &command_line
[name_length
]);
651 the_pipe
= popen (command_line
, "w");
652 if (the_pipe
== ((FILE *) NULL
))
653 fatal ("cannot open pipe to real mailer");
655 add_a_stream (the_pipe
, pclose
);
657 write_header (the_header
);
659 /* Dump the message itself */
661 while (!feof (stdin
))
663 size
= fread (buf
, 1, BUFLEN
, stdin
);
668 exit (close_the_streams ());
671 #endif /* not MSDOS */
672 #endif /* not BSD 4.2 (or newer) */