aboutsummaryrefslogtreecommitdiff
path: root/src/builtin/builtin.c
blob: 4b6413dc62726dd6af1500a1eb0e940836494ccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* This file is part of Mailfromd.
   Copyright (C) 2010-2018 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/stream.h>
#include <gettext.h>

#define DEFINE_BUILTIN_MODULE
#include "mailfromd.h"
#include "prog.h"
#include "builtin.h"

void
builtin_setup ()
{
	struct builtin_module *mod;
	for (mod = builtin_module; mod->name; mod++)
		if (mod->init)
			mod->init();
}

void
builtin_set_module_trace(const char *name, size_t len, int val)
{
	struct builtin_module *mod;
	
	for (mod = builtin_module; mod->name; mod++)
		if (strlen (mod->name) == len
		    && memcmp(mod->name, name, len) == 0) {
			mod->trace = val;
			return;
		}

	mu_error(gettext("no such module: %*.*s"), (int)len, (int)len, name);
}

void
builtin_set_all_module_trace(int val)
{
	struct builtin_module *mod;

	for (mod = builtin_module; mod->name; mod++)
		mod->trace = val;
}

int
builtin_module_trace(unsigned idx)
{
	return idx < BUILTIN_IDX_MAX && builtin_module[idx].trace;
}

void
_builtin_stream_cleanup(void *ptr)
{
	mu_stream_t str = ptr;
	mu_stream_unref(str);
}


mu_message_t
_builtin_mu_stream_to_message(mu_stream_t mstr, eval_environ_t env,
			      const char *func_name)
{
	int rc;
	mu_header_t hdr;
	mu_message_t msg;
	
	rc = mu_stream_to_message(mstr, &msg);
	if (rc)
		env_throw_bi(env, mfe_failure,
			     func_name,
			     "cannot obtain stream reference: %s",
			     mu_strerror(rc));
	mu_stream_unref(mstr);
	/* FIXME: This works over a bug in mailutils 2.99.92
	   <= release-2.2-378-g6060ab1 */
	mu_message_get_header(msg, &hdr);
	return msg;
}

int
_builtin_const_to_c(struct builtin_const_trans *tab, size_t count,
		    int num, int *ret)
{
	for (; count; tab++, count--)
		if (tab->const_mfl == num) {
			*ret = tab->const_c;
			return 0;
		}
	return -1;
}

int
_builtin_c_to_const(struct builtin_const_trans *tab, size_t count,
		    int num, int *ret)
{
	for (; count; tab++, count--)
		if (tab->const_c == num) {
			*ret = tab->const_mfl;
			return 0;
		}
	return -1;
}

int
_builtin_const_to_bitmap(struct builtin_const_trans *tab, size_t count,
			 int num)
{
	int f = 0;
	for (; count; tab++, count--)
		if (tab->const_c & num)
			f |= tab->const_mfl;
	return f;
}

Return to:

Send suggestions and report system problems to the System administrator.