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 #define xstrncpy(d_,s_,l_) \
135 strncat((dst_),(s_),(l_)-1); \
138 static const char *my_name;
139 static char *the_date;
140 static char *the_user;
141 static line_list file_preface;
142 static stream_list the_streams;
143 static boolean no_problems = true;
145 #if !__STDC__ && !defined(STDC_HEADERS)
146 extern FILE *popen();
147 extern int fclose(), pclose();
148 extern char *malloc(), *realloc();
151 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400000
156 extern struct passwd *getpwuid();
157 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400000
158 extern uid_t geteuid ();
160 extern unsigned short geteuid();
162 static struct passwd *my_entry;
164 (my_entry = getpwuid ((int) geteuid ()), \
170 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
172 static void error(const char *s1, const char *s2)
174 printf("%s: ", my_name);
180 /* Print error message and exit. */
182 static void fatal(const char *s1, const char *s2)
188 /* Like malloc but get fatal error if memory is exhausted. */
190 static void *xmalloc(size_t size)
192 void *result = malloc(size);
194 fatal("virtual memory exhausted", (char *)0);
198 static void *xrealloc(void *ptr, size_t size)
200 void *result = realloc(ptr, size);
202 fatal("virtual memory exhausted", (char *)0);
206 /* Initialize a linebuffer for use */
208 static void init_linebuffer(struct linebuffer *linebuffer)
210 linebuffer->size = INITIAL_LINE_SIZE;
211 linebuffer->buffer = (char *)xmalloc(INITIAL_LINE_SIZE);
214 /* Read a line of text from `stream' into `linebuffer'.
215 * Return the length of the line.
218 static long readline(struct linebuffer *linebuffer, FILE * stream)
220 char *buffer = linebuffer->buffer;
221 char *p = linebuffer->buffer;
222 char *end = p + linebuffer->size;
225 int c = getc(stream);
227 linebuffer->size *= 2;
228 buffer = (char *)xrealloc(buffer, linebuffer->size);
229 p = buffer + (p - linebuffer->buffer);
230 end = buffer + linebuffer->size;
231 linebuffer->buffer = buffer;
233 if (c < 0 || c == '\n') {
243 static char *get_keyword(register char *field, char **rest)
245 static char keyword[KEYWORD_SIZE];
251 if ((isspace((int)(unsigned char)c)) || (c == ':'))
253 *ptr++ = ((islower((int)(unsigned char)c)) ?
254 (toupper((int)(unsigned char)c)) : c);
255 while (((c = *field++) != ':') && (!(isspace((int)(unsigned char)c))))
256 *ptr++ = ((islower((int)(unsigned char)c)) ?
257 (toupper((int)(unsigned char)c)) : c);
259 while (isspace((int)(unsigned char)c))
267 static boolean has_keyword(char *field)
270 return (get_keyword(field, &ignored) != (char *)NULL);
273 static char *add_field(line_list the_list, register char *field,
274 register char *where)
279 while ((c = *field++) != '\0') {
281 while (*field && *field != ')')
284 break; /* no closer */
286 break; /* closerNULL */
290 ((c == ',' || c == '>' || c == '<') ? ' ' : c);
294 field = the_list->string;
295 the_list = the_list->continuation;
300 static line_list make_file_preface(void)
302 char *the_string, *temp;
303 time_t idiotic_interface;
308 size_t the_string_len, the_user_len, temp_len;
310 prefix_length = strlen(FROM_PREFIX);
311 time(&idiotic_interface);
312 the_date = ctime(&idiotic_interface);
313 /* the_date has an unwanted newline at the end */
314 date_length = strlen(the_date) - 1;
315 if (the_date[date_length] == '\n')
316 the_date[date_length] = '\0';
317 temp = cuserid((char *)NULL);
319 the_user_len = strlen(temp);
320 the_user = alloc_string(the_user_len);
321 xstrncpy(the_user, temp, the_user_len+1);
322 /* alloc the_string */
323 the_string_len = 3 + prefix_length + the_user_len + date_length;
324 the_string = alloc_string(the_string_len);
325 temp_len = the_string_len+1;
327 xstrncpy(temp, FROM_PREFIX, temp_len);
329 temp = &temp[prefix_length];
331 temp_len -= prefix_length + 1;
332 xstrncpy(temp, the_user, temp_len);
334 temp = &temp[the_user_len];
336 temp_len -= the_user_len + 1;
337 xstrncpy(temp, the_date, temp_len);
340 result->string = the_string;
341 result->continuation = ((line_list) NULL);
345 static void write_line_list(register line_list the_list, FILE * the_stream)
348 the_list != ((line_list) NULL);
349 the_list = the_list->continuation) {
350 fputs(the_list->string, the_stream);
351 putc('\n', the_stream);
356 static int close_the_streams(void)
358 register stream_list rem;
359 for (rem = the_streams;
360 rem != ((stream_list) NULL); rem = rem->rest_streams)
361 no_problems = (no_problems &&
362 ((*rem->action) (rem->handle) == 0));
363 the_streams = ((stream_list) NULL);
364 return (no_problems ? 0 : 1);
367 static void add_a_stream(FILE * the_stream, int (*closing_action) (FILE *))
369 stream_list old = the_streams;
370 the_streams = new_stream();
371 the_streams->handle = the_stream;
372 the_streams->action = closing_action;
373 the_streams->rest_streams = old;
377 static int my_fclose(FILE * the_file)
379 putc('\n', the_file);
381 return fclose(the_file);
384 static boolean open_a_file(char *name)
386 FILE *the_stream = fopen(name, "a");
387 if (the_stream != ((FILE *) NULL)) {
388 add_a_stream(the_stream, my_fclose);
389 if (the_user == (char *)NULL)
390 file_preface = make_file_preface();
391 write_line_list(file_preface, the_stream);
397 static void put_string(char *s)
399 register stream_list rem;
400 for (rem = the_streams;
401 rem != ((stream_list) NULL); rem = rem->rest_streams)
402 fputs(s, rem->handle);
406 static void put_line(const char *string)
408 register stream_list rem;
409 for (rem = the_streams;
410 rem != ((stream_list) NULL); rem = rem->rest_streams) {
411 const char *s = string;
414 /* Divide STRING into lines. */
416 const char *breakpos;
418 /* Find the last char that fits. */
419 for (breakpos = s; *breakpos && column < 78; ++breakpos) {
420 if (*breakpos == '\t')
425 /* If we didn't reach end of line, break the line. */
427 /* Back up to just after the last comma that fits. */
428 while (breakpos != s && breakpos[-1] != ',')
432 /* If no comma fits, move past the first address anyway. */
433 while (*breakpos != 0
437 /* Include the comma after it. */
441 /* Output that much, then break the line. */
442 fwrite(s, 1, breakpos - s, rem->handle);
445 /* Skip whitespace and prepare to print more addresses. */
447 while (*s == ' ' || *s == '\t')
450 fputs("\n\t", rem->handle);
452 putc('\n', rem->handle);
457 #define mail_error error
459 static void setup_files(register line_list the_list, register char *field)
461 register char *start;
464 while (((c = *field) != '\0') &&
465 ((c == ' ') || (c == '\t') || (c == ',')))
469 while (((c = *field) != '\0') &&
470 (c != ' ') && (c != '\t') && (c != ','))
473 if (!open_a_file(start))
474 mail_error("Could not open file %s", start);
479 if (the_list == ((line_list) NULL))
481 field = the_list->string;
482 the_list = the_list->continuation;
486 static int args_size(header the_header)
488 register header old = the_header;
489 register line_list rem;
490 register int size = 0;
493 register char *keyword =
494 get_keyword(the_header->text->string, &field);
495 if ((strcmp(keyword, "TO") == 0) || (strcmp(keyword, "CC") == 0)
496 || (strcmp(keyword, "BCC") == 0)) {
497 size += 1 + strlen(field);
498 for (rem = the_header->text->continuation;
499 rem != NIL; rem = rem->continuation)
500 size += 1 + strlen(rem->string);
502 the_header = the_header->next;
503 } while (the_header != old);
507 static void parse_header(header the_header, register char *where)
509 register header old = the_header;
512 register char *keyword =
513 get_keyword(the_header->text->string, &field);
514 if (strcmp(keyword, "TO") == 0)
516 add_field(the_header->text->continuation, field,
518 else if (strcmp(keyword, "CC") == 0)
520 add_field(the_header->text->continuation, field,
522 else if (strcmp(keyword, "BCC") == 0) {
524 add_field(the_header->text->continuation, field,
526 the_header->previous->next = the_header->next;
527 the_header->next->previous = the_header->previous;
528 } else if (strcmp(keyword, "FCC") == 0)
529 setup_files(the_header->text->continuation, field);
530 the_header = the_header->next;
531 } while (the_header != old);
536 static header read_header(void)
538 register header the_header = ((header) NULL);
539 register line_list *next_line = ((line_list *) NULL);
541 init_linebuffer(&lb);
547 readline(&lb, stdin);
549 length = strlen(line);
553 if (has_keyword(line)) {
554 register header old = the_header;
555 the_header = new_header();
556 if (old == ((header) NULL)) {
557 the_header->next = the_header;
558 the_header->previous = the_header;
560 the_header->previous = old;
561 the_header->next = old->next;
562 old->next = the_header;
564 next_line = &(the_header->text);
567 if (next_line == ((line_list *) NULL)) {
568 /* Not a valid header */
571 *next_line = new_list();
572 (*next_line)->string = alloc_string((size_t) length);
573 xstrncpy(((*next_line)->string), length, line);
574 next_line = &((*next_line)->continuation);
579 return the_header->next;
582 static void write_header(header the_header)
584 register header old = the_header;
586 register line_list the_list;
587 for (the_list = the_header->text;
588 the_list != NIL; the_list = the_list->continuation)
589 put_line(the_list->string);
590 the_header = the_header->next;
591 } while (the_header != old);
596 int main(int argc, char *argv[])
599 size_t command_line_len;
602 char *mail_program_name;
603 char buf[BUFLEN + 1];
607 mail_program_name = getenv("FAKEMAILER");
608 if (!(mail_program_name && *mail_program_name))
609 mail_program_name = (char *)MAIL_PROGRAM_NAME;
610 name_length = strlen(mail_program_name);
613 the_streams = ((stream_list) NULL);
614 the_date = (char *)NULL;
615 the_user = (char *)NULL;
617 the_header = read_header();
618 command_line_len = name_length + args_size(the_header);
619 command_line = alloc_string(command_line_len);
620 xstrncpy(command_line, command_line_len, mail_program_name);
621 parse_header(the_header, &command_line[name_length]);
623 the_pipe = popen(command_line, "w");
624 if (the_pipe == ((FILE *) NULL))
625 fatal("cannot open pipe to real mailer", (char *)NULL);
627 add_a_stream(the_pipe, pclose);
629 write_header(the_header);
631 /* Dump the message itself */
633 while (!feof(stdin)) {
634 size = fread(buf, 1, BUFLEN, stdin);
639 return close_the_streams();
642 #endif /* not BSD 4.2 (or newer) */