1 #line 982 "../../src/builtin/snarf.m4"
2 /* -*- buffer-read-only: t -*- vi: set ro:
3 THIS FILE IS GENERATED AUTOMATICALLY. PLEASE DO NOT EDIT.
4 */
5 #line 982
6 #ifdef HAVE_CONFIG_H
7 #line 982
8 # include <config.h>
9 #line 982
10 #endif
11 #line 982
12 #include <sys/types.h>
13 #line 982
14
15 #line 982
16 #include "mailfromd.h"
17 #line 982
18 #include "prog.h"
19 #line 982
20 #include "builtin.h"
21 #line 982
22
23 #line 982
24
25 #line 1022 "../../src/builtin/snarf.m4"
26
27 /* End of snarf.m4 */
28 #line 1 "mail.bi"
29 /* This file is part of Mailfromd. -*- c -*-
30 Copyright (C) 2006-2021 Sergey Poznyakoff
31
32 This program is free software; you can redistribute it and/or modify
33 it under the terms of the GNU General Public License as published by
34 the Free Software Foundation; either version 3, or (at your option)
35 any later version.
36
37 This program is distributed in the hope that it will be useful,
38 but WITHOUT ANY WARRANTY; without even the implied warranty of
39 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 GNU General Public License for more details.
41
42 You should have received a copy of the GNU General Public License
43 along with this program. If not, see <http://www.gnu.org/licenses/>. */
44
45
46
47 #include "msg.h"
48
49 static int
_send(eval_environ_t env,const char * mailer_url,char * to,char * from,mu_message_t msg,void (* destroy)(void *),void * ptr)50 _send(eval_environ_t env,
51 const char *mailer_url, char *to, char *from, mu_message_t msg,
52 void (*destroy)(void*), void *ptr)
53 {
54 int status;
55 mu_mailer_t mailer;
56 mu_address_t to_addr = NULL;
57 mu_address_t from_addr = NULL;
58
59 if (!mailer_url)
60 mu_mailer_get_url_default(&mailer_url);
61 status = mu_mailer_create(&mailer, mailer_url);
62 if (status) {
63 destroy(ptr);
64 (
65 #line 36
66 env_throw_bi(env, mfe_failure, NULL, _("cannot create mailer `%s': %s"),mailer_url,mu_strerror(status))
67 #line 36
68 );
69 #line 39
70 }
71
72 if (to) {
73 status = mu_address_create(&to_addr, to);
74 if (status) {
75 destroy(ptr);
76 mu_mailer_destroy(&mailer);
77 (
78 #line 46
79 env_throw_bi(env, mfe_failure, NULL, _("bad recipient address `%s': %s"),to,mu_strerror(status))
80 #line 46
81 );
82 #line 49
83 }
84 }
85
86 if (!from || from[0] == 0)
87 from = "<>";
88 status = mu_address_create(&from_addr, from);
89 if (status) {
90 mu_address_destroy(&to_addr);
91 mu_mailer_destroy(&mailer);
92 destroy(ptr);
93 (
94 #line 59
95 env_throw_bi(env, mfe_failure, NULL, _("bad sender address `%s': %s"),from,mu_strerror(status))
96 #line 59
97 );
98 #line 62
99 }
100
101 /* FIXME: mailer flags? */
102 status = mu_mailer_open(mailer, 0);
103 if (status) {
104 mu_address_destroy(&to_addr);
105 mu_address_destroy(&from_addr);
106 mu_mailer_destroy(&mailer);
107 destroy(ptr);
108 (
109 #line 71
110 env_throw_bi(env, mfe_failure, NULL, _("opening mailer `%s' failed: %s"),mailer_url,mu_strerror (status))
111 #line 71
112 );
113 #line 74
114 }
115
116 status = mu_mailer_send_message(mailer, msg, from_addr, to_addr);
117 mu_address_destroy(&to_addr);
118 mu_address_destroy(&from_addr);
119 mu_mailer_destroy(&mailer);
120 destroy(ptr);
121 if (!(status == 0))
122 #line 81
123 (
124 #line 81
125 env_throw_bi(env, mfe_failure, NULL, _("cannot send message: %s"),mu_strerror(status))
126 #line 81
127 )
128 #line 83
129 ;
130 return 0;
131 }
132
133 static void
_destroy_msg(void * ptr)134 _destroy_msg(void *ptr)
135 {
136 mu_message_t msg = ptr;
137 mu_message_destroy(&msg, mu_message_get_owner(msg));
138 }
139
140 void
141 #line 94
bi_send_mail(eval_environ_t env)142 bi_send_mail(eval_environ_t env)
143 #line 94
144
145 #line 94
146
147 #line 94 "mail.bi"
148 {
149 #line 94
150
151 #line 94
152
153 #line 94
154 long __bi_argcnt;
155 #line 94
156 char * text;
157 #line 94
158 char * to;
159 #line 94
160 char * from;
161 #line 94
162 char * mailer_url;
163 #line 94
164
165 #line 94
166 get_string_arg(env, 1, &text);
167 #line 94
168 get_string_arg(env, 2, &to);
169 #line 94
170 get_string_arg(env, 3, &from);
171 #line 94
172 get_string_arg(env, 4, &mailer_url);
173 #line 94
174
175 #line 94
176 get_numeric_arg(env, 0, &__bi_argcnt);
177 #line 94
178 adjust_stack(env, __bi_argcnt + 1);
179 #line 94
180
181 #line 94
182
183 #line 94
184 if (builtin_module_trace(BUILTIN_IDX_mail))
185 #line 94
186 prog_trace(env, "send_mail %s %s %s %s",text, ((__bi_argcnt > 1) ? to : ""), ((__bi_argcnt > 2) ? from : ""), ((__bi_argcnt > 3) ? mailer_url : ""));;
187
188 {
189 mu_message_t msg = NULL;
190 mu_stream_t stream = NULL;
191
192 mu_message_create(&msg, NULL);
193 mu_static_memory_stream_create(&stream, text, strlen(text));
194 mu_message_set_stream(msg, stream, mu_message_get_owner(msg));
195
196 _send(env, ((__bi_argcnt > 3) ? mailer_url : NULL),
197 ((__bi_argcnt > 1) ? to : NULL),
198 ((__bi_argcnt > 2) ? from : NULL), msg, _destroy_msg, msg);
199 mu_stream_unref(stream);
200 }
201
202 #line 109
203 env_function_cleanup_flush(env, NULL);
204 #line 109
205 return;
206 #line 109
207 }
208
209 void
210 #line 111
bi_send_message(eval_environ_t env)211 bi_send_message(eval_environ_t env)
212 #line 111
213
214 #line 111
215
216 #line 111 "mail.bi"
217 {
218 #line 111
219
220 #line 111
221
222 #line 111
223 long __bi_argcnt;
224 #line 111
225 long nmsg;
226 #line 111
227 char * to;
228 #line 111
229 char * from;
230 #line 111
231 char * mailer_url;
232 #line 111
233
234 #line 111
235 get_numeric_arg(env, 1, &nmsg);
236 #line 111
237 get_string_arg(env, 2, &to);
238 #line 111
239 get_string_arg(env, 3, &from);
240 #line 111
241 get_string_arg(env, 4, &mailer_url);
242 #line 111
243
244 #line 111
245 get_numeric_arg(env, 0, &__bi_argcnt);
246 #line 111
247 adjust_stack(env, __bi_argcnt + 1);
248 #line 111
249
250 #line 111
251
252 #line 111
253 if (builtin_module_trace(BUILTIN_IDX_mail))
254 #line 111
255 prog_trace(env, "send_message %lu %s %s %s",nmsg, ((__bi_argcnt > 1) ? to : ""), ((__bi_argcnt > 2) ? from : ""), ((__bi_argcnt > 3) ? mailer_url : ""));;
256
257 {
258 mu_message_t msg = bi_message_from_descr(env, nmsg);
259 _send(env, ((__bi_argcnt > 3) ? mailer_url : NULL),
260 ((__bi_argcnt > 1) ? to : NULL),
261 ((__bi_argcnt > 2) ? from : NULL), msg, _destroy_msg, msg);
262 }
263
264 #line 119
265 env_function_cleanup_flush(env, NULL);
266 #line 119
267 return;
268 #line 119
269 }
270
271 static void
add_headers(mu_message_t msg,char * headers)272 add_headers(mu_message_t msg, char *headers)
273 {
274 mu_stream_t stream = NULL;
275 mu_header_t hdr = NULL;
276 size_t len;
277
278 if (!headers)
279 return;
280
281 len = strlen(headers);
282 mu_message_get_header(msg, &hdr);
283
284 mu_header_get_streamref(hdr, &stream);
285 mu_stream_seek(stream, 0, MU_SEEK_END, NULL);
286 mu_stream_write(stream, headers, len, NULL);
287 if (len < 2 || memcmp(headers + len - 2, "\n\n", 2)) {
288 if (len > 1 && headers[len-1] == '\n')
289 mu_stream_write(stream, "\n", 1, NULL);
290 else
291 mu_stream_write(stream, "\n\n", 2, NULL);
292 }
293 mu_stream_unref(stream);
294 }
295
296 void
297 #line 146
bi_send_text(eval_environ_t env)298 bi_send_text(eval_environ_t env)
299 #line 146
300
301 #line 146
302
303 #line 146 "mail.bi"
304 {
305 #line 146
306
307 #line 146
308
309 #line 146
310 long __bi_argcnt;
311 #line 146
312 char * text;
313 #line 146
314 char * headers;
315 #line 146
316 char * to;
317 #line 146
318 char * from;
319 #line 146
320 char * mailer_url;
321 #line 146
322
323 #line 146
324 get_string_arg(env, 1, &text);
325 #line 146
326 get_string_arg(env, 2, &headers);
327 #line 146
328 get_string_arg(env, 3, &to);
329 #line 146
330 get_string_arg(env, 4, &from);
331 #line 146
332 get_string_arg(env, 5, &mailer_url);
333 #line 146
334
335 #line 146
336 get_numeric_arg(env, 0, &__bi_argcnt);
337 #line 146
338 adjust_stack(env, __bi_argcnt + 1);
339 #line 146
340
341 #line 146
342
343 #line 146
344 if (builtin_module_trace(BUILTIN_IDX_mail))
345 #line 146
346 prog_trace(env, "send_text %s %s %s %s %s",text, headers, ((__bi_argcnt > 2) ? to : ""), ((__bi_argcnt > 3) ? from : ""), ((__bi_argcnt > 4) ? mailer_url : ""));;
347
348 {
349 mu_message_t msg = NULL;
350 mu_body_t body = NULL;
351 mu_stream_t stream = NULL;
352
353 mu_message_create(&msg, NULL);
354
355 mu_message_get_body(msg, &body);
356 mu_body_get_streamref(body, &stream);
357 mu_stream_write(stream, text, strlen(text), NULL);
358 mu_stream_unref(stream);
359
360 add_headers(msg, headers);
361 _send(env, ((__bi_argcnt > 4) ? mailer_url : NULL), ((__bi_argcnt > 2) ? to : NULL),
362 ((__bi_argcnt > 3) ? from : NULL), msg, _destroy_msg, msg);
363 }
364
365 #line 164
366 env_function_cleanup_flush(env, NULL);
367 #line 164
368 return;
369 #line 164
370 }
371
372
373 static void
mime_create_reason(mu_mime_t mime,const char * sender,const char * text)374 mime_create_reason(mu_mime_t mime, const char *sender, const char *text)
375 {
376 mu_message_t newmsg;
377 mu_stream_t stream;
378 time_t t;
379 struct tm *tm;
380 mu_body_t body;
381 mu_header_t hdr;
382 char datestr[80];
383 static char *content_header =
384 "Content-Type: text/plain;charset=US-ASCII\n" /* FIXME! */
385 "Content-Transfer-Encoding: 8bit\n";
386
387 mu_message_create(&newmsg, NULL);
388 mu_message_get_body(newmsg, &body);
389 mu_body_get_streamref(body, &stream);
390
391 time(&t);
392 tm = localtime(&t);
393 mu_strftime(datestr, sizeof datestr, "%a, %b %d %H:%M:%S %Y %Z", tm);
394
395 mu_stream_printf(stream,
396 "\n"
397 "The original message was received at %s from %s.\n",
398 datestr, sender);
399
400 mu_stream_write(stream, text, strlen(text), NULL);
401 mu_stream_flush(stream);
402 mu_stream_unref(stream);
403 mu_header_create(&hdr, content_header, strlen(content_header));
404 mu_message_set_header(newmsg, hdr, NULL);
405 mu_mime_add_part(mime, newmsg);
406 mu_message_unref(newmsg);
407 }
408
409 static void
mime_create_ds(mu_mime_t mime,char * recpt)410 mime_create_ds(mu_mime_t mime, char *recpt)
411 {
412 mu_message_t newmsg;
413 mu_stream_t stream;
414 mu_header_t hdr;
415 mu_body_t body;
416 char datestr[80];
417 time_t t;
418 struct tm *tm;
419
420 time(&t);
421 tm = localtime(&t);
422 mu_strftime(datestr, sizeof datestr, "%a, %b %d %H:%M:%S %Y %Z", tm);
423
424 mu_message_create(&newmsg, NULL);
425 mu_message_get_header(newmsg, &hdr);
426 mu_header_set_value(hdr, "Content-Type", "message/delivery-status", 1);
427 mu_message_get_body(newmsg, &body);
428 mu_body_get_streamref(body, &stream);
429 mu_stream_printf(stream, "Reporting-UA: %s\n", PACKAGE_STRING);
430 mu_stream_printf(stream, "Arrival-Date: %s\n", datestr);
431 mu_stream_printf(stream, "Final-Recipient: RFC822; %s\n",
432 recpt ? recpt : "unknown");
433 mu_stream_printf(stream, "%s", "Action: deleted\n");
434 mu_stream_printf(stream, "%s",
435 "Disposition: automatic-action/"
436 "MDN-sent-automatically;deleted\n");
437 mu_stream_printf(stream, "Last-Attempt-Date: %s\n", datestr);
438 mu_stream_unref(stream);
439 mu_mime_add_part(mime, newmsg);
440 mu_message_unref(newmsg);
441 }
442
443 /* Quote original message */
444 static int
mime_create_quote(mu_mime_t mime,mu_stream_t istream)445 mime_create_quote(mu_mime_t mime, mu_stream_t istream)
446 {
447 mu_message_t newmsg;
448 mu_stream_t ostream;
449 mu_header_t hdr;
450 mu_body_t body;
451 char *buf = NULL;
452 size_t bufsize = 0;
453
454 mu_message_create(&newmsg, NULL);
455 mu_message_get_header(newmsg, &hdr);
456 mu_header_set_value(hdr, "Content-Type", "message/rfc822", 1);
457 mu_message_get_body (newmsg, &body);
458 mu_body_get_streamref(body, &ostream);
459 /* Skip envelope line */
460 mu_stream_getline(istream, &buf, &bufsize, NULL);
461 free(buf);
462 mu_stream_copy(ostream, istream, 0, NULL);
463 mu_stream_unref(ostream);
464 mu_mime_add_part(mime, newmsg);
465 mu_message_unref(newmsg);
466 return 0;
467 }
468
469 static int
build_mime(mu_mime_t * pmime,mu_stream_t msgstr,char * sender,char * recpt,char * text)470 build_mime(mu_mime_t *pmime, mu_stream_t msgstr,
471 char *sender, char *recpt,
472 char *text)
473 {
474 mu_mime_t mime = NULL;
475 int status;
476
477 mu_mime_create(&mime, NULL, 0);
478 mime_create_reason(mime, sender, text);
479 mime_create_ds(mime, recpt);
480 status = mime_create_quote(mime, msgstr);
481 if (status) {
482 mu_mime_destroy(&mime);
483 return status;
484 }
485 *pmime = mime;
486 return 0;
487 }
488
489 static void
_destroy_mime(void * ptr)490 _destroy_mime(void *ptr)
491 {
492 mu_mime_t mime = ptr;
493 mu_mime_destroy(&mime);
494 }
495
496
497
498 void
499 #line 292
bi_create_dsn(eval_environ_t env)500 bi_create_dsn(eval_environ_t env)
501 #line 292
502
503 #line 292
504
505 #line 292 "mail.bi"
506 {
507 #line 292
508 mu_stream_t mstr;
509 #line 292
510
511 #line 292
512
513 #line 292
514
515 #line 292
516 long __bi_argcnt;
517 #line 292
518 char * sender;
519 #line 292
520 char * recpt;
521 #line 292
522 char * text;
523 #line 292
524 char * headers;
525 #line 292
526 char * from;
527 #line 292
528
529 #line 292
530 get_string_arg(env, 1, &sender);
531 #line 292
532 get_string_arg(env, 2, &recpt);
533 #line 292
534 get_string_arg(env, 3, &text);
535 #line 292
536 get_string_arg(env, 4, &headers);
537 #line 292
538 get_string_arg(env, 5, &from);
539 #line 292
540
541 #line 292
542 get_numeric_arg(env, 0, &__bi_argcnt);
543 #line 292
544 adjust_stack(env, __bi_argcnt + 1);
545 #line 292
546
547 #line 292
548 {
549 #line 292
550 int rc = env_get_stream(env,&mstr);
551 #line 292
552 if (!(rc == 0))
553 #line 292
554 (
555 #line 292
556
557 #line 292
558 mu_stream_unref(mstr),
559 #line 292
560 env_throw_bi(env, mfe_failure, "create_dsn", "cannot obtain capture stream reference: %s",mu_strerror(rc))
561 #line 292
562 )
563 #line 292
564 ;
565 #line 292
566 }
567 #line 292
568 if (builtin_module_trace(BUILTIN_IDX_mail))
569 #line 292
570 prog_trace(env, "create_dsn %s %s %s %s %s",sender, recpt, text, ((__bi_argcnt > 3) ? headers : ""), ((__bi_argcnt > 4) ? from : ""));;
571
572 {
573 int rc;
574 mu_mime_t mime;
575 mu_message_t msg;
576
577 rc = build_mime(&mime, mstr, sender, recpt, text);
578 if (!(rc == 0))
579 #line 300
580 (
581 #line 300
582
583 #line 300
584 mu_stream_unref(mstr),
585 #line 300
586 env_throw_bi(env, mfe_failure, "create_dsn", _("cannot create DSN: %s"),mu_strerror(rc))
587 #line 300
588 )
589 #line 303
590 ;
591
592 mu_mime_get_message(mime, &msg);
593 add_headers(msg, ((__bi_argcnt > 3) ? headers : NULL));
594 rc = bi_message_register(env, NULL, msg, MF_MSG_STANDALONE);
595 if (rc < 0) {
596 mu_message_destroy(&msg, mu_message_get_owner(msg));
597 (
598 #line 310
599
600 #line 310
601 mu_stream_unref(mstr),
602 #line 310
603 env_throw_bi(env, mfe_failure, "create_dsn", _("no more message slots available"))
604 #line 310
605 );
606 #line 312
607 }
608
609 #line 313
610 do {
611 #line 313
612 push(env, (STKVAL)(mft_number)(rc));
613 #line 313
614 goto endlab;
615 #line 313
616 } while (0);
617 }
618 endlab:
619 #line 315
620
621 #line 315
622 mu_stream_unref(mstr);
623 #line 315
624 env_function_cleanup_flush(env, NULL);
625 #line 315
626 return;
627 #line 315
628 }
629
630
631
632 void
633 #line 319
bi_send_dsn(eval_environ_t env)634 bi_send_dsn(eval_environ_t env)
635 #line 319
636
637 #line 319
638
639 #line 319 "mail.bi"
640 {
641 #line 319
642 mu_stream_t mstr;
643 #line 319
644
645 #line 319
646
647 #line 319
648
649 #line 319
650 long __bi_argcnt;
651 #line 319
652 char * to;
653 #line 319
654 char * sender;
655 #line 319
656 char * recpt;
657 #line 319
658 char * text;
659 #line 319
660 char * headers;
661 #line 319
662 char * from;
663 #line 319
664 char * mailer_url;
665 #line 319
666
667 #line 319
668 get_string_arg(env, 1, &to);
669 #line 319
670 get_string_arg(env, 2, &sender);
671 #line 319
672 get_string_arg(env, 3, &recpt);
673 #line 319
674 get_string_arg(env, 4, &text);
675 #line 319
676 get_string_arg(env, 5, &headers);
677 #line 319
678 get_string_arg(env, 6, &from);
679 #line 319
680 get_string_arg(env, 7, &mailer_url);
681 #line 319
682
683 #line 319
684 get_numeric_arg(env, 0, &__bi_argcnt);
685 #line 319
686 adjust_stack(env, __bi_argcnt + 1);
687 #line 319
688
689 #line 319
690 {
691 #line 319
692 int rc = env_get_stream(env,&mstr);
693 #line 319
694 if (!(rc == 0))
695 #line 319
696 (
697 #line 319
698
699 #line 319
700 mu_stream_unref(mstr),
701 #line 319
702 env_throw_bi(env, mfe_failure, "send_dsn", "cannot obtain capture stream reference: %s",mu_strerror(rc))
703 #line 319
704 )
705 #line 319
706 ;
707 #line 319
708 }
709 #line 319
710 if (builtin_module_trace(BUILTIN_IDX_mail))
711 #line 319
712 prog_trace(env, "send_dsn %s %s %s %s %s %s %s",to, sender, recpt, text, ((__bi_argcnt > 4) ? headers : ""), ((__bi_argcnt > 5) ? from : ""), ((__bi_argcnt > 6) ? mailer_url : ""));;
713 #line 321
714
715 {
716 int status;
717 mu_mime_t mime;
718 mu_message_t newmsg;
719
720 status = build_mime(&mime, mstr, sender, recpt, text);
721 if (!(status == 0))
722 #line 328
723 (
724 #line 328
725
726 #line 328
727 mu_stream_unref(mstr),
728 #line 328
729 env_throw_bi(env, mfe_failure, "send_dsn", _("cannot create DSN: %s"),mu_strerror(status))
730 #line 328
731 )
732 #line 331
733 ;
734
735 mu_mime_get_message(mime, &newmsg);
736 add_headers(newmsg, ((__bi_argcnt > 4) ? headers : NULL));
737 _send(env, ((__bi_argcnt > 6) ? mailer_url : NULL), to,
738 ((__bi_argcnt > 5) ? from : NULL), newmsg, _destroy_mime, mime);
739 }
740
741 #line 338
742
743 #line 338
744 mu_stream_unref(mstr);
745 #line 338
746 env_function_cleanup_flush(env, NULL);
747 #line 338
748 return;
749 #line 338
750 }
751
752 #line 982 "../../src/builtin/snarf.m4"
753
754 #line 982
755
756 #line 982
757
758 #line 982
759 void
760 #line 982
mail_init_builtin(void)761 mail_init_builtin(void)
762 #line 982
763 {
764 #line 982
765
766 #line 982
767 #line 94 "mail.bi"
768 va_builtin_install_ex("send_mail", bi_send_mail, 0, dtype_unspecified, 4, 3, 0|0, dtype_string, dtype_string, dtype_string, dtype_string);
769 #line 111 "mail.bi"
770 va_builtin_install_ex("send_message", bi_send_message, 0, dtype_unspecified, 4, 3, 0|0, dtype_number, dtype_string, dtype_string, dtype_string);
771 #line 146 "mail.bi"
772 va_builtin_install_ex("send_text", bi_send_text, 0, dtype_unspecified, 5, 3, 0|0, dtype_string, dtype_string, dtype_string, dtype_string, dtype_string);
773 #line 292 "mail.bi"
774 va_builtin_install_ex("create_dsn", bi_create_dsn, STATMASK(smtp_state_eom), dtype_number, 5, 2, MFD_BUILTIN_CAPTURE|0, dtype_string, dtype_string, dtype_string, dtype_string, dtype_string);
775 #line 319 "mail.bi"
776 va_builtin_install_ex("send_dsn", bi_send_dsn, STATMASK(smtp_state_eom), dtype_unspecified, 7, 3, MFD_BUILTIN_CAPTURE|0, dtype_string, dtype_string, dtype_string, dtype_string, dtype_string, dtype_string, dtype_string);
777
778 #line 982 "../../src/builtin/snarf.m4"
779
780 #line 982
781 }
782 #line 982 "../../src/builtin/snarf.m4"
783
784