comparison lib-src/fakemail.c @ 0:376386a54a3c r19-14

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