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