import -ko -b 1.1.3 XEmacs XEmacs-21_2 r21-2-35
[chise/xemacs-chise.git.1] / 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 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 /* Synched up with: FSF 19.28. */
22
23 #define NO_SHORTNAMES
24 #include <config.h>
25
26 #if defined (BSD) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
27 /* This program is not used in BSD, so just avoid loader complaints.  */
28 int
29 main (int argc, char *argv[])
30 {
31   return 0;
32 }
33 #elif defined (LINUX)
34 #include <stdio.h>
35 #include <stdlib.h>
36 int
37 main (int argc, char *argv[])
38 {
39   /* Linux /bin/mail, if it exists, is NOT the Unix v7 mail that
40      fakemail depends on!  This causes garbled mail.  Better to
41      output an error message. */
42   fprintf (stderr, "Sorry, fakemail does not work on Linux.\n");
43   fprintf (stderr, "Make sure you have the sendmail program, and\n");
44   fprintf (stderr, "set the Lisp variable `sendmail-program' to point\n");
45   fprintf (stderr, "to the path of the sendmail binary.\n");
46   return 1;
47 }
48 #else /* not BSD 4.2 (or newer) */
49
50 /* These are defined in config in some versions. */
51
52 #ifdef static
53 #undef static
54 #endif
55
56 #ifdef read
57 #undef read
58 #undef write
59 #undef open
60 #undef close
61 #endif
62
63 #include <stdio.h>
64 #if __STDC__ || defined(STDC_HEADERS)
65 #include <stdlib.h>
66 #include <unistd.h>
67 #endif
68 #include <string.h>
69 #include <ctype.h>
70 #include <time.h>
71 #include <pwd.h>
72 \f
73 /* Type definitions */
74
75 #define boolean int
76 #define true 1
77 #define false 0
78
79 /* Various lists */
80
81 struct line_record
82 {
83   char *string;
84   struct line_record *continuation;
85 };
86 typedef struct line_record *line_list;
87
88 struct header_record
89 {
90   line_list text;
91   struct header_record *next;
92   struct header_record *previous;
93 };
94 typedef struct header_record *header;
95
96 struct stream_record
97 {
98   FILE *handle;
99   int (*action)(FILE *);
100   struct stream_record *rest_streams;
101 };
102 typedef struct stream_record *stream_list;
103
104 /* A `struct linebuffer' is a structure which holds a line of text.
105  * `readline' reads a line from a stream into a linebuffer
106  * and works regardless of the length of the line.
107  */
108
109 struct linebuffer
110 {
111   size_t size;
112   char *buffer;
113 };
114
115 struct linebuffer lb;
116
117 #define new_list()                                      \
118   ((line_list) xmalloc (sizeof (struct line_record)))
119 #define new_header()                            \
120   ((header) xmalloc (sizeof (struct header_record)))
121 #define new_stream()                            \
122   ((stream_list) xmalloc (sizeof (struct stream_record)))
123 #define alloc_string(nchars)                            \
124   ((char *) xmalloc ((nchars) + 1))
125 \f
126 /* Global declarations */
127
128 #define BUFLEN 1024
129 #define KEYWORD_SIZE 256
130 #define FROM_PREFIX "From"
131 #define MY_NAME "fakemail"
132 #define NIL ((line_list) NULL)
133 #define INITIAL_LINE_SIZE 200
134
135 #ifndef MAIL_PROGRAM_NAME
136 #define MAIL_PROGRAM_NAME "/bin/mail"
137 #endif
138
139 static const char *my_name;
140 static char *the_date;
141 static char *the_user;
142 static line_list file_preface;
143 static stream_list the_streams;
144 static boolean no_problems = true;
145
146 #if !__STDC__ && !defined(STDC_HEADERS)
147 extern FILE *popen ();
148 extern int fclose (), pclose ();
149 extern char *malloc (), *realloc ();
150 #endif
151
152 #ifdef CURRENT_USER
153 extern struct passwd *getpwuid ();
154 extern unsigned short geteuid ();
155 static struct passwd *my_entry;
156 #define cuserid(s)                              \
157 (my_entry = getpwuid ((int) geteuid ()),        \
158  my_entry->pw_name)
159 #endif
160 \f
161 /* Utilities */
162
163 /* Print error message.  `s1' is printf control string, `s2' is arg for it. */
164
165 static void
166 error (const char *s1, const char *s2)
167 {
168   printf ("%s: ", my_name);
169   printf (s1, s2);
170   printf ("\n");
171   no_problems = false;
172 }
173
174 /* Print error message and exit.  */
175
176 static void
177 fatal (const char *s1, const char *s2)
178 {
179   error (s1, s2);
180   exit (1);
181 }
182
183 /* Like malloc but get fatal error if memory is exhausted.  */
184
185 static void *
186 xmalloc (size_t size)
187 {
188   void *result = malloc (size);
189   if (result == NULL)
190     fatal ("virtual memory exhausted", (char *) 0);
191   return result;
192 }
193
194 static void *
195 xrealloc (void *ptr, size_t size)
196 {
197   void *result = realloc (ptr, size);
198   if (result == NULL)
199     fatal ("virtual memory exhausted", (char *) 0);
200   return result;
201 }
202 \f
203 /* Initialize a linebuffer for use */
204
205 static void
206 init_linebuffer (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 static long
217 readline (struct linebuffer *linebuffer, FILE *stream)
218 {
219   char *buffer = linebuffer->buffer;
220   char *p = linebuffer->buffer;
221   char *end = p + linebuffer->size;
222
223   while (true)
224     {
225       int c = getc (stream);
226       if (p == end)
227         {
228           linebuffer->size *= 2;
229           buffer = (char *) xrealloc (buffer, linebuffer->size);
230           p = buffer + (p - linebuffer->buffer);
231           end = buffer + linebuffer->size;
232           linebuffer->buffer = buffer;
233         }
234       if (c < 0 || c == '\n')
235         {
236           *p = 0;
237           break;
238         }
239       *p++ = c;
240     }
241
242   return p - buffer;
243 }
244 \f
245 static char *
246 get_keyword (register char *field, char **rest)
247 {
248   static char keyword[KEYWORD_SIZE];
249   register char *ptr;
250   register char c;
251
252   ptr = &keyword[0];
253   c = *field++;
254   if ((isspace ((int) (unsigned char) c)) || (c == ':'))
255     return (char *) NULL;
256   *ptr++ = ((islower ((int) (unsigned char) c)) ?
257             (toupper ((int) (unsigned char) c)) : c);
258   while (((c = *field++) != ':') &&
259          (!(isspace ((int) (unsigned char) c))))
260     *ptr++ = ((islower ((int) (unsigned char) c)) ?
261               (toupper ((int) (unsigned char) c)) : c);
262   *ptr++ = '\0';
263   while (isspace ((int) (unsigned char) c)) c = *field++;
264   if (c != ':') return (char *) NULL;
265   *rest = field;
266   return &keyword[0];
267 }
268
269 static boolean
270 has_keyword (char *field)
271 {
272   char *ignored;
273   return (get_keyword (field, &ignored) != (char *) NULL);
274 }
275
276 static char *
277 add_field (line_list the_list, register char *field, register char *where)
278 {
279   register char c;
280   while (true)
281     {
282       *where++ = ' ';
283       while ((c = *field++) != '\0')
284         {
285           if (c == '(')
286             {
287               while (*field && *field != ')') ++field;
288               if (! (*field++)) break; /* no closer */
289               if (! (*field))   break; /* closerNULL */
290               c = *field;
291             }
292           *where++ = ((c == ','||c=='>'||c=='<') ? ' ' : c);
293         }
294       if (the_list == NIL) break;
295       field = the_list->string;
296       the_list = the_list->continuation;
297     }
298   return where;
299 }
300 \f
301 static line_list
302 make_file_preface (void)
303 {
304   char *the_string, *temp;
305   long idiotic_interface;
306   long prefix_length;
307   long user_length;
308   long date_length;
309   line_list result;
310
311   prefix_length = strlen (FROM_PREFIX);
312   time (&idiotic_interface);
313   the_date = ctime (&idiotic_interface);
314   /* the_date has an unwanted newline at the end */
315   date_length = strlen (the_date) - 1;
316   the_date[date_length] = '\0';
317 #ifdef WIN32_NATIVE
318   temp = "(null)";
319 #else
320   temp = cuserid ((char *) NULL);
321 #endif
322   user_length = strlen (temp);
323   the_user = alloc_string ((size_t) (user_length + 1));
324   strcpy (the_user, temp);
325   the_string = alloc_string ((size_t) (3 + prefix_length +
326                                        user_length +
327                                        date_length));
328   temp = the_string;
329   strcpy (temp, FROM_PREFIX);
330   temp = &temp[prefix_length];
331   *temp++ = ' ';
332   strcpy (temp, the_user);
333   temp = &temp[user_length];
334   *temp++ = ' ';
335   strcpy (temp, the_date);
336   result = new_list ();
337   result->string = the_string;
338   result->continuation = ((line_list) NULL);
339   return result;
340 }
341
342 static void
343 write_line_list (register line_list the_list, FILE *the_stream)
344 {
345   for ( ;
346       the_list != ((line_list) NULL) ;
347       the_list = the_list->continuation)
348     {
349       fputs (the_list->string, the_stream);
350       putc ('\n', the_stream);
351     }
352   return;
353 }
354 \f
355 static int
356 close_the_streams (void)
357 {
358   register stream_list rem;
359   for (rem = the_streams;
360        rem != ((stream_list) NULL);
361        rem = rem->rest_streams)
362     no_problems = (no_problems &&
363                    ((*rem->action) (rem->handle) == 0));
364   the_streams = ((stream_list) NULL);
365   return (no_problems ? 0 : 1);
366 }
367
368 static void
369 add_a_stream (FILE *the_stream, int (*closing_action)(FILE *))
370 {
371   stream_list old = the_streams;
372   the_streams = new_stream ();
373   the_streams->handle = the_stream;
374   the_streams->action = closing_action;
375   the_streams->rest_streams = old;
376   return;
377 }
378
379 static int
380 my_fclose (FILE *the_file)
381 {
382   putc ('\n', the_file);
383   fflush (the_file);
384   return fclose (the_file);
385 }
386
387 static boolean
388 open_a_file (char *name)
389 {
390   FILE *the_stream = fopen (name, "a");
391   if (the_stream != ((FILE *) NULL))
392     {
393       add_a_stream (the_stream, my_fclose);
394       if (the_user == (char *) NULL)
395         file_preface = make_file_preface ();
396       write_line_list (file_preface, the_stream);
397       return true;
398     }
399   return false;
400 }
401
402 static void
403 put_string (char *s)
404 {
405   register stream_list rem;
406   for (rem = the_streams;
407        rem != ((stream_list) NULL);
408        rem = rem->rest_streams)
409     fputs (s, rem->handle);
410   return;
411 }
412
413 static void
414 put_line (const char *string)
415 {
416   register stream_list rem;
417   for (rem = the_streams;
418        rem != ((stream_list) NULL);
419        rem = rem->rest_streams)
420     {
421       const char *s = string;
422       int column = 0;
423
424       /* Divide STRING into lines.  */
425       while (*s != 0)
426         {
427           const char *breakpos;
428
429           /* Find the last char that fits.  */
430           for (breakpos = s; *breakpos && column < 78; ++breakpos)
431             {
432               if (*breakpos == '\t')
433                 column += 8;
434               else
435                 column++;
436             }
437           /* If we didn't reach end of line, break the line.  */
438           if (*breakpos)
439             {
440               /* Back up to just after the last comma that fits.  */
441               while (breakpos != s && breakpos[-1] != ',') --breakpos;
442
443               if (breakpos == s)
444                 {
445                   /* If no comma fits, move past the first address anyway.  */
446                   while (*breakpos != 0 && *breakpos != ',') ++breakpos;
447                   if (*breakpos != 0)
448                     /* Include the comma after it.  */
449                     ++breakpos;
450                 }
451             }
452           /* Output that much, then break the line.  */
453           fwrite (s, 1, breakpos - s, rem->handle);
454           column = 8;
455
456           /* Skip whitespace and prepare to print more addresses.  */
457           s = breakpos;
458           while (*s == ' ' || *s == '\t') ++s;
459           if (*s != 0)
460             fputs ("\n\t", rem->handle);
461         }
462       putc ('\n', rem->handle);
463     }
464   return;
465 }
466 \f
467 #define mail_error error
468
469 static void
470 setup_files (register line_list the_list, register char *field)
471 {
472   register char *start;
473   register char c;
474   while (true)
475     {
476       while (((c = *field) != '\0') &&
477              ((c == ' ') ||
478               (c == '\t') ||
479               (c == ',')))
480         field += 1;
481       if (c != '\0')
482         {
483           start = field;
484           while (((c = *field) != '\0') &&
485                  (c != ' ') &&
486                  (c != '\t') &&
487                  (c != ','))
488             field += 1;
489           *field = '\0';
490           if (!open_a_file (start))
491             mail_error ("Could not open file %s", start);
492           *field = c;
493           if (c != '\0') continue;
494         }
495       if (the_list == ((line_list) NULL)) return;
496       field = the_list->string;
497       the_list = the_list->continuation;
498     }
499 }
500 \f
501 static int
502 args_size (header the_header)
503 {
504   register header old = the_header;
505   register line_list rem;
506   register int size = 0;
507   do
508     {
509       char *field;
510       register char *keyword = get_keyword (the_header->text->string, &field);
511       if ((strcmp (keyword, "TO") == 0) ||
512           (strcmp (keyword, "CC") == 0) ||
513           (strcmp (keyword, "BCC") == 0))
514         {
515           size += 1 + strlen (field);
516           for (rem = the_header->text->continuation;
517                rem != NIL;
518                rem = rem->continuation)
519             size += 1 + strlen (rem->string);
520         }
521       the_header = the_header->next;
522     } while (the_header != old);
523   return size;
524 }
525
526 static void
527 parse_header (header the_header, register char *where)
528 {
529   register header old = the_header;
530   do
531     {
532       char *field;
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)
539         {
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;
543         }
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);
548   *where = '\0';
549   return;
550 }
551 \f
552 static header
553 read_header (void)
554 {
555   register header the_header = ((header) NULL);
556   register line_list *next_line = ((line_list *) NULL);
557
558   init_linebuffer (&lb);
559
560   do
561     {
562       long length;
563       register char *line;
564
565       readline (&lb, stdin);
566       line = lb.buffer;
567       length = strlen (line);
568       if (length == 0) break;
569
570       if (has_keyword (line))
571         {
572           register header old = the_header;
573           the_header = new_header ();
574           if (old == ((header) NULL))
575             {
576               the_header->next = the_header;
577               the_header->previous = the_header;
578             }
579           else
580             {
581               the_header->previous = old;
582               the_header->next = old->next;
583               old->next = the_header;
584             }
585           next_line = &(the_header->text);
586         }
587
588       if (next_line == ((line_list *) NULL))
589         {
590           /* Not a valid header */
591           exit (1);
592         }
593       *next_line = new_list ();
594       (*next_line)->string = alloc_string ((size_t) length);
595       strcpy (((*next_line)->string), line);
596       next_line = &((*next_line)->continuation);
597       *next_line = NIL;
598
599     } while (true);
600
601   return the_header->next;
602 }
603 \f
604 static void
605 write_header (header the_header)
606 {
607   register header old = the_header;
608   do
609     {
610       register line_list the_list;
611       for (the_list = the_header->text;
612            the_list != NIL;
613            the_list = the_list->continuation)
614         put_line (the_list->string);
615       the_header = the_header->next;
616     } while (the_header != old);
617   put_line ("");
618   return;
619 }
620 \f
621 int
622 main (int argc, char *argv[])
623 {
624   char *command_line;
625   header the_header;
626   long name_length;
627   char *mail_program_name;
628   char buf[BUFLEN + 1];
629   register int size;
630   FILE *the_pipe;
631
632   mail_program_name = getenv ("FAKEMAILER");
633   if (!(mail_program_name && *mail_program_name))
634     mail_program_name = (char *) MAIL_PROGRAM_NAME;
635   name_length = strlen (mail_program_name);
636
637   my_name = MY_NAME;
638   the_streams = ((stream_list) NULL);
639   the_date = (char *) NULL;
640   the_user = (char *) NULL;
641
642   the_header = read_header ();
643   command_line = alloc_string ((size_t) (name_length +
644                                          args_size (the_header)));
645   strcpy (command_line, mail_program_name);
646   parse_header (the_header, &command_line[name_length]);
647
648   the_pipe = popen (command_line, "w");
649   if (the_pipe == ((FILE *) NULL))
650     fatal ("cannot open pipe to real mailer", (char *) NULL);
651
652   add_a_stream (the_pipe, pclose);
653
654   write_header (the_header);
655
656   /* Dump the message itself */
657
658   while (!feof (stdin))
659     {
660       size = fread (buf, 1, BUFLEN, stdin);
661       buf[size] = '\0';
662       put_string (buf);
663     }
664
665   return close_the_streams ();
666 }
667
668 #endif /* not BSD 4.2 (or newer) */