Customized.
[bpt/emacs.git] / lib-src / fakemail.c
1 /* sendmail-like interface to /bin/mail for system V,
2 Copyright (C) 1985, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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)
9 any later version.
10
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.
15
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. */
20
21
22 #define NO_SHORTNAMES
23 #include <../src/config.h>
24
25 #if defined (BSD_SYSTEM) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
26 /* This program isnot used in BSD, so just avoid loader complaints. */
27 int
28 main ()
29 {
30 return 0;
31 }
32 #else /* not BSD 4.2 (or newer) */
33 #ifdef MSDOS
34 int
35 main ()
36 {
37 return 0;
38 }
39 #else /* not MSDOS */
40 /* This conditional contains all the rest of the file. */
41
42 /* These are defined in config in some versions. */
43
44 #ifdef static
45 #undef static
46 #endif
47
48 #ifdef read
49 #undef read
50 #undef write
51 #undef open
52 #undef close
53 #endif
54
55 #ifdef WINDOWSNT
56 #include "ntlib.h"
57 #endif
58
59 #include <stdio.h>
60 #include <string.h>
61 #include <ctype.h>
62 #include <time.h>
63 #include <pwd.h>
64
65 /* This is to declare cuserid. */
66 #ifdef HAVE_UNISTD_H
67 #include <unistd.h>
68 #endif
69 \f
70 /* Type definitions */
71
72 #define boolean int
73 #define true 1
74 #define false 0
75
76 /* Various lists */
77
78 struct line_record
79 {
80 char *string;
81 struct line_record *continuation;
82 };
83 typedef struct line_record *line_list;
84
85 struct header_record
86 {
87 line_list text;
88 struct header_record *next;
89 struct header_record *previous;
90 };
91 typedef struct header_record *header;
92
93 struct stream_record
94 {
95 FILE *handle;
96 int (*action)();
97 struct stream_record *rest_streams;
98 };
99 typedef struct stream_record *stream_list;
100
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.
104 */
105
106 struct linebuffer
107 {
108 long size;
109 char *buffer;
110 };
111
112 struct linebuffer lb;
113
114 #define new_list() \
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))
122 \f
123 /* Global declarations */
124
125 #define BUFLEN 1024
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
131
132 #ifndef MAIL_PROGRAM_NAME
133 #define MAIL_PROGRAM_NAME "/bin/mail"
134 #endif
135
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;
142
143 extern FILE *popen ();
144 extern int fclose (), pclose ();
145
146 #ifdef CURRENT_USER
147 extern struct passwd *getpwuid ();
148 extern unsigned short geteuid ();
149 static struct passwd *my_entry;
150 #define cuserid(s) \
151 (my_entry = getpwuid (((int) geteuid ())), \
152 my_entry->pw_name)
153 #endif
154 \f
155 /* Utilities */
156
157 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
158
159 static void
160 error (s1, s2)
161 char *s1, *s2;
162 {
163 printf ("%s: ", my_name);
164 printf (s1, s2);
165 printf ("\n");
166 no_problems = false;
167 }
168
169 /* Print error message and exit. */
170
171 static void
172 fatal (s1, s2)
173 char *s1, *s2;
174 {
175 error (s1, s2);
176 exit (1);
177 }
178
179 /* Like malloc but get fatal error if memory is exhausted. */
180
181 static long *
182 xmalloc (size)
183 int size;
184 {
185 long *result = (long *) malloc (((unsigned) size));
186 if (result == ((long *) NULL))
187 fatal ("virtual memory exhausted", 0);
188 return result;
189 }
190
191 static long *
192 xrealloc (ptr, size)
193 long *ptr;
194 int size;
195 {
196 long *result = (long *) realloc (ptr, ((unsigned) size));
197 if (result == ((long *) NULL))
198 fatal ("virtual memory exhausted");
199 return result;
200 }
201 \f
202 /* Initialize a linebuffer for use */
203
204 void
205 init_linebuffer (linebuffer)
206 struct linebuffer *linebuffer;
207 {
208 linebuffer->size = INITIAL_LINE_SIZE;
209 linebuffer->buffer = ((char *) xmalloc (INITIAL_LINE_SIZE));
210 }
211
212 /* Read a line of text from `stream' into `linebuffer'.
213 * Return the length of the line.
214 */
215
216 long
217 readline (linebuffer, stream)
218 struct linebuffer *linebuffer;
219 FILE *stream;
220 {
221 char *buffer = linebuffer->buffer;
222 char *p = linebuffer->buffer;
223 char *end = p + linebuffer->size;
224
225 while (true)
226 {
227 int c = getc (stream);
228 if (p == end)
229 {
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;
235 }
236 if (c < 0 || c == '\n')
237 {
238 *p = 0;
239 break;
240 }
241 *p++ = c;
242 }
243
244 return p - buffer;
245 }
246 \f
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.
250
251 If there is no keyword, return NULL and don't alter *REST. */
252
253 char *
254 get_keyword (field, rest)
255 register char *field;
256 char **rest;
257 {
258 static char keyword[KEYWORD_SIZE];
259 register char *ptr;
260 register char c;
261
262 ptr = &keyword[0];
263 c = *field++;
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);
269 *ptr++ = '\0';
270 while (isspace (c))
271 c = *field++;
272 if (c != ':')
273 return ((char *) NULL);
274 *rest = field;
275 return &keyword[0];
276 }
277
278 /* Nonzero if the string FIELD starts with a colon-terminated keyword. */
279
280 boolean
281 has_keyword (field)
282 char *field;
283 {
284 char *ignored;
285 return (get_keyword (field, &ignored) != ((char *) NULL));
286 }
287
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 <...>.
294
295 We don't pay attention to overflowing WHERE;
296 the caller has to make it big enough. */
297
298 char *
299 add_field (the_list, field, where)
300 line_list the_list;
301 register char *field, *where;
302 {
303 register char c;
304 while (true)
305 {
306 char *this_recipient_where;
307 int in_quotes = 0;
308
309 *where++ = ' ';
310 this_recipient_where = where;
311
312 while ((c = *field++) != '\0')
313 {
314 if (c == '\\')
315 *where++ = c;
316 else if (c == '"')
317 {
318 in_quotes = ! in_quotes;
319 *where++ = c;
320 }
321 else if (in_quotes)
322 *where++ = c;
323 else if (c == '(')
324 {
325 while (*field && *field != ')') ++field;
326 if (! (*field++)) break; /* no close */
327 continue;
328 }
329 else if (c == ',')
330 {
331 *where++ = ' ';
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;
335 }
336 else if (c == '<')
337 /* Discard everything we got before the `<'. */
338 where = this_recipient_where;
339 else if (c == '>')
340 /* Discard the rest of this name that follows the `>'. */
341 {
342 while (*field && *field != ',') ++field;
343 if (! (*field++)) break; /* no comma */
344 continue;
345 }
346 else
347 *where++ = c;
348 }
349 if (the_list == NIL) break;
350 field = the_list->string;
351 the_list = the_list->continuation;
352 }
353 return where;
354 }
355 \f
356 line_list
357 make_file_preface ()
358 {
359 char *the_string, *temp;
360 long idiotic_interface;
361 long prefix_length;
362 long user_length;
363 long date_length;
364 line_list result;
365
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
377 + user_length
378 + date_length);
379 temp = the_string;
380 strcpy (temp, FROM_PREFIX);
381 temp = &temp[prefix_length];
382 *temp++ = ' ';
383 strcpy (temp, the_user);
384 temp = &temp[user_length];
385 *temp++ = ' ';
386 strcpy (temp, the_date);
387 result = new_list ();
388 result->string = the_string;
389 result->continuation = ((line_list) NULL);
390 return result;
391 }
392
393 void
394 write_line_list (the_list, the_stream)
395 register line_list the_list;
396 FILE *the_stream;
397 {
398 for ( ;
399 the_list != ((line_list) NULL) ;
400 the_list = the_list->continuation)
401 {
402 fputs (the_list->string, the_stream);
403 putc ('\n', the_stream);
404 }
405 return;
406 }
407 \f
408 int
409 close_the_streams ()
410 {
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);
419 }
420
421 void
422 add_a_stream (the_stream, closing_action)
423 FILE *the_stream;
424 int (*closing_action)();
425 {
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;
431 return;
432 }
433
434 int
435 my_fclose (the_file)
436 FILE *the_file;
437 {
438 putc ('\n', the_file);
439 fflush (the_file);
440 return fclose (the_file);
441 }
442
443 boolean
444 open_a_file (name)
445 char *name;
446 {
447 FILE *the_stream = fopen (name, "a");
448 if (the_stream != ((FILE *) NULL))
449 {
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);
454 return true;
455 }
456 return false;
457 }
458
459 void
460 put_string (s)
461 char *s;
462 {
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);
468 return;
469 }
470
471 void
472 put_line (string)
473 char *string;
474 {
475 register stream_list rem;
476 for (rem = the_streams;
477 rem != ((stream_list) NULL);
478 rem = rem->rest_streams)
479 {
480 char *s = string;
481 int column = 0;
482
483 /* Divide STRING into lines. */
484 while (*s != 0)
485 {
486 char *breakpos;
487
488 /* Find the last char that fits. */
489 for (breakpos = s; *breakpos && column < 78; ++breakpos)
490 {
491 if (*breakpos == '\t')
492 column += 8;
493 else
494 column++;
495 }
496 /* If we didn't reach end of line, break the line. */
497 if (*breakpos)
498 {
499 /* Back up to just after the last comma that fits. */
500 while (breakpos != s && breakpos[-1] != ',') --breakpos;
501
502 if (breakpos == s)
503 {
504 /* If no comma fits, move past the first address anyway. */
505 while (*breakpos != 0 && *breakpos != ',') ++breakpos;
506 if (*breakpos != 0)
507 /* Include the comma after it. */
508 ++breakpos;
509 }
510 }
511 /* Output that much, then break the line. */
512 fwrite (s, 1, breakpos - s, rem->handle);
513 column = 8;
514
515 /* Skip whitespace and prepare to print more addresses. */
516 s = breakpos;
517 while (*s == ' ' || *s == '\t') ++s;
518 if (*s != 0)
519 fputs ("\n\t", rem->handle);
520 }
521 putc ('\n', rem->handle);
522 }
523 return;
524 }
525 \f
526 #define mail_error error
527
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. */
531
532 void
533 setup_files (the_list, field)
534 register line_list the_list;
535 register char *field;
536 {
537 register char *start;
538 register char c;
539 while (true)
540 {
541 while (((c = *field) != '\0')
542 && (c == ' '
543 || c == '\t'
544 || c == ','))
545 field += 1;
546 if (c != '\0')
547 {
548 start = field;
549 while (((c = *field) != '\0')
550 && c != ' '
551 && c != '\t'
552 && c != ',')
553 field += 1;
554 *field = '\0';
555 if (!open_a_file (start))
556 mail_error ("Could not open file %s", start);
557 *field = c;
558 if (c != '\0') continue;
559 }
560 if (the_list == ((line_list) NULL))
561 return;
562 field = the_list->string;
563 the_list = the_list->continuation;
564 }
565 }
566 \f
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. */
569
570 int
571 args_size (the_header)
572 header the_header;
573 {
574 register header old = the_header;
575 register line_list rem;
576 register int size = 0;
577 do
578 {
579 char *field;
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))
584 {
585 size += 1 + strlen (field);
586 for (rem = the_header->text->continuation;
587 rem != NIL;
588 rem = rem->continuation)
589 size += 1 + strlen (rem->string);
590 }
591 the_header = the_header->next;
592 } while (the_header != old);
593 return size;
594 }
595
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.
599
600 Also, if the header has any FCC fields, call setup_files for each one. */
601
602 parse_header (the_header, where)
603 header the_header;
604 register char *where;
605 {
606 register header old = the_header;
607 do
608 {
609 char *field;
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)
616 {
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;
620 }
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);
625 *where = '\0';
626 return;
627 }
628 \f
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. */
634
635 header
636 read_header ()
637 {
638 register header the_header = ((header) NULL);
639 register line_list *next_line = ((line_list *) NULL);
640
641 init_linebuffer (&lb);
642
643 do
644 {
645 long length;
646 register char *line;
647
648 readline (&lb, stdin);
649 line = lb.buffer;
650 length = strlen (line);
651 if (length == 0) break;
652
653 if (has_keyword (line))
654 {
655 register header old = the_header;
656 the_header = new_header ();
657 if (old == ((header) NULL))
658 {
659 the_header->next = the_header;
660 the_header->previous = the_header;
661 }
662 else
663 {
664 the_header->previous = old;
665 the_header->next = old->next;
666 old->next = the_header;
667 }
668 next_line = &(the_header->text);
669 }
670
671 if (next_line == ((line_list *) NULL))
672 {
673 /* Not a valid header */
674 exit (1);
675 }
676 *next_line = new_list ();
677 (*next_line)->string = alloc_string (length);
678 strcpy (((*next_line)->string), line);
679 next_line = &((*next_line)->continuation);
680 *next_line = NIL;
681
682 } while (true);
683
684 return the_header->next;
685 }
686 \f
687 void
688 write_header (the_header)
689 header the_header;
690 {
691 register header old = the_header;
692 do
693 {
694 register line_list the_list;
695 for (the_list = the_header->text;
696 the_list != NIL;
697 the_list = the_list->continuation)
698 put_line (the_list->string);
699 the_header = the_header->next;
700 } while (the_header != old);
701 put_line ("");
702 return;
703 }
704 \f
705 int
706 main (argc, argv)
707 int argc;
708 char **argv;
709 {
710 char *command_line;
711 header the_header;
712 long name_length;
713 char *mail_program_name;
714 char buf[BUFLEN + 1];
715 register int size;
716 FILE *the_pipe;
717
718 extern char *getenv ();
719
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);
724
725 my_name = MY_NAME;
726 the_streams = ((stream_list) NULL);
727 the_date = ((char *) NULL);
728 the_user = ((char *) NULL);
729
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]);
734
735 the_pipe = popen (command_line, "w");
736 if (the_pipe == ((FILE *) NULL))
737 fatal ("cannot open pipe to real mailer");
738
739 add_a_stream (the_pipe, pclose);
740
741 write_header (the_header);
742
743 /* Dump the message itself */
744
745 while (!feof (stdin))
746 {
747 size = fread (buf, 1, BUFLEN, stdin);
748 buf[size] = '\0';
749 put_string (buf);
750 }
751
752 exit (close_the_streams ());
753 }
754
755 #endif /* not MSDOS */
756 #endif /* not BSD 4.2 (or newer) */