1 /* This file is part of Mailfromd.
2    Copyright (C) 2010-2021 Sergey Poznyakoff
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 #include <stdlib.h>
21 #include <string.h>
22 #include <mailutils/error.h>
23 #include <mailutils/stream.h>
24 #include <gettext.h>
25 
26 #define DEFINE_BUILTIN_MODULE
27 #include "mailfromd.h"
28 #include "prog.h"
29 #include "builtin.h"
30 
31 void
builtin_setup()32 builtin_setup ()
33 {
34 	struct builtin_module *mod;
35 	for (mod = builtin_module; mod->name; mod++)
36 		if (mod->init)
37 			mod->init();
38 }
39 
40 void
builtin_set_module_trace(const char * name,size_t len,int val)41 builtin_set_module_trace(const char *name, size_t len, int val)
42 {
43 	struct builtin_module *mod;
44 
45 	for (mod = builtin_module; mod->name; mod++)
46 		if (strlen (mod->name) == len
47 		    && memcmp(mod->name, name, len) == 0) {
48 			mod->trace = val;
49 			return;
50 		}
51 
52 	mu_error(gettext("no such module: %*.*s"), (int)len, (int)len, name);
53 }
54 
55 void
builtin_set_all_module_trace(int val)56 builtin_set_all_module_trace(int val)
57 {
58 	struct builtin_module *mod;
59 
60 	for (mod = builtin_module; mod->name; mod++)
61 		mod->trace = val;
62 }
63 
64 int
builtin_module_trace(unsigned idx)65 builtin_module_trace(unsigned idx)
66 {
67 	return idx < BUILTIN_IDX_MAX && builtin_module[idx].trace;
68 }
69 
70 void
_builtin_stream_cleanup(void * ptr)71 _builtin_stream_cleanup(void *ptr)
72 {
73 	mu_stream_t str = ptr;
74 	mu_stream_unref(str);
75 }
76 
77 
78 mu_message_t
_builtin_mu_stream_to_message(mu_stream_t mstr,eval_environ_t env,const char * func_name)79 _builtin_mu_stream_to_message(mu_stream_t mstr, eval_environ_t env,
80 			      const char *func_name)
81 {
82 	int rc;
83 	mu_header_t hdr;
84 	mu_message_t msg;
85 
86 	rc = mu_stream_to_message(mstr, &msg);
87 	if (rc)
88 		env_throw_bi(env, mfe_failure,
89 			     func_name,
90 			     "cannot obtain stream reference: %s",
91 			     mu_strerror(rc));
92 	mu_stream_unref(mstr);
93 	/* FIXME: This works over a bug in mailutils 2.99.92
94 	   <= release-2.2-378-g6060ab1 */
95 	mu_message_get_header(msg, &hdr);
96 	return msg;
97 }
98 
99 int
_builtin_const_to_c(struct builtin_const_trans * tab,size_t count,int num,int * ret)100 _builtin_const_to_c(struct builtin_const_trans *tab, size_t count,
101 		    int num, int *ret)
102 {
103 	for (; count; tab++, count--)
104 		if (tab->const_mfl == num) {
105 			*ret = tab->const_c;
106 			return 0;
107 		}
108 	return -1;
109 }
110 
111 int
_builtin_c_to_const(struct builtin_const_trans * tab,size_t count,int num,int * ret)112 _builtin_c_to_const(struct builtin_const_trans *tab, size_t count,
113 		    int num, int *ret)
114 {
115 	for (; count; tab++, count--)
116 		if (tab->const_c == num) {
117 			*ret = tab->const_mfl;
118 			return 0;
119 		}
120 	return -1;
121 }
122 
123 int
_builtin_const_to_bitmap(struct builtin_const_trans * tab,size_t count,int num)124 _builtin_const_to_bitmap(struct builtin_const_trans *tab, size_t count,
125 			 int num)
126 {
127 	int f = 0;
128 	for (; count; tab++, count--)
129 		if (tab->const_c & num)
130 			f |= tab->const_mfl;
131 	return f;
132 }
133 
134