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 SXEmacs.
6 SXEmacs 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 3 of the License, or
9 (at your option) any later version.
11 SXEmacs 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 this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Synched up with: FSF 19.28. */
24 #if defined (BSD) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
25 /* This program is not used in BSD, so just avoid loader complaints. */
26 int main(int argc, char *argv[])
33 int main(int argc, char *argv[])
35 /* Linux /bin/mail, if it exists, is NOT the Unix v7 mail that
36 fakemail depends on! This causes garbled mail. Better to
37 output an error message. */
38 fprintf(stderr, "Sorry, fakemail does not work on Linux.\n");
39 fprintf(stderr, "Make sure you have the sendmail program, and\n");
40 fprintf(stderr, "set the Lisp variable `sendmail-program' to point\n");
41 fprintf(stderr, "to the path of the sendmail binary.\n");
44 #else /* not BSD 4.2 (or newer) */
46 /* These are defined in config in some versions. */
60 #if __STDC__ || defined(STDC_HEADERS)
69 /* Type definitions */
79 struct line_record *continuation;
81 typedef struct line_record *line_list;
83 struct header_record {
85 struct header_record *next;
86 struct header_record *previous;
88 typedef struct header_record *header;
90 struct stream_record {
92 int (*action) (FILE *);
93 struct stream_record *rest_streams;
95 typedef struct stream_record *stream_list;
97 /* A `struct linebuffer' is a structure which holds a line of text.
98 * `readline' reads a line from a stream into a linebuffer
99 * and works regardless of the length of the line.
107 struct linebuffer lb;
110 ((line_list) xmalloc (sizeof (struct line_record)))
111 #define new_header() \
112 ((header) xmalloc (sizeof (struct header_record)))
113 #define new_stream() \
114 ((stream_list) xmalloc (sizeof (struct stream_record)))
115 #define alloc_string(nchars) \
116 ((char *) xmalloc ((nchars) + 1))
118 /* Global declarations */
121 #define KEYWORD_SIZE 256
122 #define FROM_PREFIX "From"
123 #define MY_NAME "fakemail"
124 #define NIL ((line_list) NULL)
125 #define INITIAL_LINE_SIZE 200
127 #ifndef MAIL_PROGRAM_NAME
128 #define MAIL_PROGRAM_NAME "/bin/mail"
131 static const char *my_name;
132 static char *the_date;
133 static char *the_user;
134 static line_list file_preface;
135 static stream_list the_streams;
136 static boolean no_problems = true;
138 #if !__STDC__ && !defined(STDC_HEADERS)
139 extern FILE *popen();
140 extern int fclose(), pclose();
141 extern char *malloc(), *realloc();
144 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400000
149 extern struct passwd *getpwuid();
150 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400000
151 extern uid_t geteuid ();
153 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. */
165 static void error(const char *s1, const char *s2)
167 printf("%s: ", my_name);
173 /* Print error message and exit. */
175 static void fatal(const char *s1, const char *s2)
181 /* Like malloc but get fatal error if memory is exhausted. */
183 static void *xmalloc(size_t size)
185 void *result = malloc(size);
187 fatal("virtual memory exhausted", (char *)0);
191 static void *xrealloc(void *ptr, size_t size)
193 void *result = realloc(ptr, size);
195 fatal("virtual memory exhausted", (char *)0);
199 /* Initialize a linebuffer for use */
201 static void init_linebuffer(struct linebuffer *linebuffer)
203 linebuffer->size = INITIAL_LINE_SIZE;
204 linebuffer->buffer = (char *)xmalloc(INITIAL_LINE_SIZE);
207 /* Read a line of text from `stream' into `linebuffer'.
208 * Return the length of the line.
211 static long readline(struct linebuffer *linebuffer, FILE * stream)
213 char *buffer = linebuffer->buffer;
214 char *p = linebuffer->buffer;
215 char *end = p + linebuffer->size;
218 int c = getc(stream);
220 linebuffer->size *= 2;
221 buffer = (char *)xrealloc(buffer, linebuffer->size);
222 p = buffer + (p - linebuffer->buffer);
223 end = buffer + linebuffer->size;
224 linebuffer->buffer = buffer;
226 if (c < 0 || c == '\n') {
236 static char *get_keyword(register char *field, char **rest)
238 static char keyword[KEYWORD_SIZE];
244 if ((isspace((int)(unsigned char)c)) || (c == ':'))
246 *ptr++ = ((islower((int)(unsigned char)c)) ?
247 (toupper((int)(unsigned char)c)) : c);
248 while (((c = *field++) != ':') && (!(isspace((int)(unsigned char)c))))
249 *ptr++ = ((islower((int)(unsigned char)c)) ?
250 (toupper((int)(unsigned char)c)) : c);
252 while (isspace((int)(unsigned char)c))
260 static boolean has_keyword(char *field)
263 return (get_keyword(field, &ignored) != (char *)NULL);
266 static char *add_field(line_list the_list, register char *field,
267 register char *where)
272 while ((c = *field++) != '\0') {
274 while (*field && *field != ')')
277 break; /* no closer */
279 break; /* closerNULL */
283 ((c == ',' || c == '>' || c == '<') ? ' ' : c);
287 field = the_list->string;
288 the_list = the_list->continuation;
293 static line_list make_file_preface(void)
295 char *the_string, *temp;
296 time_t idiotic_interface;
301 size_t the_string_len, the_user_len, temp_len;
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 if (the_date[date_length] == '\n')
309 the_date[date_length] = '\0';
310 temp = cuserid((char *)NULL);
312 the_user_len = strlen(temp);
313 the_user = alloc_string(the_user_len + 1);
314 strncpy(the_user, the_user_len, temp);
315 /* alloc the_string */
316 the_string_len = 3 + prefix_length + the_user_len + date_length;
317 the_string = alloc_string(the_string_len);
318 temp_len = the_string_len;
320 strncpy(temp, temp_len, FROM_PREFIX);
322 temp = &temp[prefix_length];
324 temp_len -= prefix_length + 1;
325 strncpy(temp, temp_len, the_user);
327 temp = &temp[the_user_len];
329 temp_len -= the_user_len + 1;
330 strncpy(temp, temp_len, the_date);
333 result->string = the_string;
334 result->continuation = ((line_list) NULL);
338 static void write_line_list(register line_list the_list, FILE * the_stream)
341 the_list != ((line_list) NULL);
342 the_list = the_list->continuation) {
343 fputs(the_list->string, the_stream);
344 putc('\n', the_stream);
349 static int close_the_streams(void)
351 register stream_list rem;
352 for (rem = the_streams;
353 rem != ((stream_list) NULL); rem = rem->rest_streams)
354 no_problems = (no_problems &&
355 ((*rem->action) (rem->handle) == 0));
356 the_streams = ((stream_list) NULL);
357 return (no_problems ? 0 : 1);
360 static void add_a_stream(FILE * the_stream, int (*closing_action) (FILE *))
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;
370 static int my_fclose(FILE * the_file)
372 putc('\n', the_file);
374 return fclose(the_file);
377 static boolean open_a_file(char *name)
379 FILE *the_stream = fopen(name, "a");
380 if (the_stream != ((FILE *) NULL)) {
381 add_a_stream(the_stream, my_fclose);
382 if (the_user == (char *)NULL)
383 file_preface = make_file_preface();
384 write_line_list(file_preface, the_stream);
390 static void put_string(char *s)
392 register stream_list rem;
393 for (rem = the_streams;
394 rem != ((stream_list) NULL); rem = rem->rest_streams)
395 fputs(s, rem->handle);
399 static void put_line(const char *string)
401 register stream_list rem;
402 for (rem = the_streams;
403 rem != ((stream_list) NULL); rem = rem->rest_streams) {
404 const char *s = string;
407 /* Divide STRING into lines. */
409 const char *breakpos;
411 /* Find the last char that fits. */
412 for (breakpos = s; *breakpos && column < 78; ++breakpos) {
413 if (*breakpos == '\t')
418 /* If we didn't reach end of line, break the line. */
420 /* Back up to just after the last comma that fits. */
421 while (breakpos != s && breakpos[-1] != ',')
425 /* If no comma fits, move past the first address anyway. */
426 while (*breakpos != 0
430 /* Include the comma after it. */
434 /* Output that much, then break the line. */
435 fwrite(s, 1, breakpos - s, rem->handle);
438 /* Skip whitespace and prepare to print more addresses. */
440 while (*s == ' ' || *s == '\t')
443 fputs("\n\t", rem->handle);
445 putc('\n', rem->handle);
450 #define mail_error error
452 static void setup_files(register line_list the_list, register char *field)
454 register char *start;
457 while (((c = *field) != '\0') &&
458 ((c == ' ') || (c == '\t') || (c == ',')))
462 while (((c = *field) != '\0') &&
463 (c != ' ') && (c != '\t') && (c != ','))
466 if (!open_a_file(start))
467 mail_error("Could not open file %s", start);
472 if (the_list == ((line_list) NULL))
474 field = the_list->string;
475 the_list = the_list->continuation;
479 static int args_size(header the_header)
481 register header old = the_header;
482 register line_list rem;
483 register int size = 0;
486 register char *keyword =
487 get_keyword(the_header->text->string, &field);
488 if ((strcmp(keyword, "TO") == 0) || (strcmp(keyword, "CC") == 0)
489 || (strcmp(keyword, "BCC") == 0)) {
490 size += 1 + strlen(field);
491 for (rem = the_header->text->continuation;
492 rem != NIL; rem = rem->continuation)
493 size += 1 + strlen(rem->string);
495 the_header = the_header->next;
496 } while (the_header != old);
500 static void parse_header(header the_header, register char *where)
502 register header old = the_header;
505 register char *keyword =
506 get_keyword(the_header->text->string, &field);
507 if (strcmp(keyword, "TO") == 0)
509 add_field(the_header->text->continuation, field,
511 else if (strcmp(keyword, "CC") == 0)
513 add_field(the_header->text->continuation, field,
515 else if (strcmp(keyword, "BCC") == 0) {
517 add_field(the_header->text->continuation, field,
519 the_header->previous->next = the_header->next;
520 the_header->next->previous = the_header->previous;
521 } else if (strcmp(keyword, "FCC") == 0)
522 setup_files(the_header->text->continuation, field);
523 the_header = the_header->next;
524 } while (the_header != old);
529 static header read_header(void)
531 register header the_header = ((header) NULL);
532 register line_list *next_line = ((line_list *) NULL);
534 init_linebuffer(&lb);
540 readline(&lb, stdin);
542 length = strlen(line);
546 if (has_keyword(line)) {
547 register header old = the_header;
548 the_header = new_header();
549 if (old == ((header) NULL)) {
550 the_header->next = the_header;
551 the_header->previous = the_header;
553 the_header->previous = old;
554 the_header->next = old->next;
555 old->next = the_header;
557 next_line = &(the_header->text);
560 if (next_line == ((line_list *) NULL)) {
561 /* Not a valid header */
564 *next_line = new_list();
565 (*next_line)->string = alloc_string((size_t) length);
566 strncpy(((*next_line)->string), length, line);
567 next_line = &((*next_line)->continuation);
572 return the_header->next;
575 static void write_header(header the_header)
577 register header old = the_header;
579 register line_list the_list;
580 for (the_list = the_header->text;
581 the_list != NIL; the_list = the_list->continuation)
582 put_line(the_list->string);
583 the_header = the_header->next;
584 } while (the_header != old);
589 int main(int argc, char *argv[])
592 size_t command_line_len;
595 char *mail_program_name;
596 char buf[BUFLEN + 1];
600 mail_program_name = getenv("FAKEMAILER");
601 if (!(mail_program_name && *mail_program_name))
602 mail_program_name = (char *)MAIL_PROGRAM_NAME;
603 name_length = strlen(mail_program_name);
606 the_streams = ((stream_list) NULL);
607 the_date = (char *)NULL;
608 the_user = (char *)NULL;
610 the_header = read_header();
611 command_line_len = name_length + args_size(the_header);
612 command_line = alloc_string(command_line_len);
613 strncpy(command_line, command_line_len, mail_program_name);
614 parse_header(the_header, &command_line[name_length]);
616 the_pipe = popen(command_line, "w");
617 if (the_pipe == ((FILE *) NULL))
618 fatal("cannot open pipe to real mailer", (char *)NULL);
620 add_a_stream(the_pipe, pclose);
622 write_header(the_header);
624 /* Dump the message itself */
626 while (!feof(stdin)) {
627 size = fread(buf, 1, BUFLEN, stdin);
632 return close_the_streams();
635 #endif /* not BSD 4.2 (or newer) */