summaryrefslogtreecommitdiffstats
path: root/src/plugins/mail-lua/mail-lua-plugin.c
blob: 146d95bb728ae7becafc2c87ef6eef746f2b8c37 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Copyright (c) 2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "module-dir.h"
#include "mail-lua-plugin.h"
#include "mail-storage-lua.h"
#include "mail-storage-private.h"
#include "mail-storage-hooks.h"
#include "dlua-script-private.h"

#define MAIL_LUA_SCRIPT "mail_lua_script"
#define MAIL_LUA_USER_CREATED_FN "mail_user_created"
#define MAIL_LUA_USER_DEINIT_FN "mail_user_deinit"
#define MAIL_LUA_USER_DEINIT_PRE_FN "mail_user_deinit_pre"
#define MAIL_LUA_USER_CONTEXT(obj) \
	MODULE_CONTEXT(obj, mail_lua_user_module)

static MODULE_CONTEXT_DEFINE_INIT(mail_lua_user_module,
				  &mail_user_module_register);

struct mail_lua_user_context {
	union mail_user_module_context module_ctx;
	struct dlua_script *script;
};

static int mail_lua_call_hook(struct dlua_script *script,
			      struct mail_user *user,
			      const char *hook,
			      const char **error_r)
{
	const char *error;

	if (!dlua_script_has_function(script, hook))
		return 0;

	if (user->mail_debug)
		e_debug(user->event, "mail-lua: Calling %s(user)", hook);

	dlua_push_mail_user(script->L, user);

	if (dlua_pcall(script->L, hook, 1, 2, &error) < 0) {
		*error_r = t_strdup_printf("%s(user) failed: %s", hook, error);
		return -1;
	}

	int ret = lua_tonumber(script->L, -2);
	const char *errmsg = lua_tostring(script->L, -1);

	if (ret < 0) {
		*error_r = t_strdup_printf("%s(user) failed: %s",
					   hook, errmsg);
	}

	lua_pop(script->L, 2);
	(void)lua_gc(script->L, LUA_GCCOLLECT, 0);

	return ret < 0 ? -1 : 1;
}

static void mail_lua_user_deinit_pre(struct mail_user *user)
{
	struct mail_lua_user_context *luser = MAIL_LUA_USER_CONTEXT(user);
	const char *error;

	if (luser == NULL)
		return;

	if (mail_lua_call_hook(luser->script, user, MAIL_LUA_USER_DEINIT_PRE_FN,
			       &error) < 0) {
		e_error(user->event, "mail-lua: %s", error);
	}

	luser->module_ctx.super.deinit_pre(user);
}

static void mail_lua_user_deinit(struct mail_user *user)
{
	struct mail_lua_user_context *luser = MAIL_LUA_USER_CONTEXT(user);
	const char *error;

	if (luser == NULL)
		return;

	luser->module_ctx.super.deinit(user);

	if (mail_lua_call_hook(luser->script, user, MAIL_LUA_USER_DEINIT_FN,
			       &error) < 0) {
		e_error(user->event, "mail-lua: %s", error);
	}

	dlua_script_unref(&luser->script);
}

static void mail_lua_user_created(struct mail_user *user)
{
	struct mail_lua_user_context *luser;
	struct mail_user_vfuncs *v = user->vlast;
	struct dlua_script *script;
	const char *error;
	const char *script_fn = mail_user_plugin_getenv(user, MAIL_LUA_SCRIPT);
	int ret;

	if (script_fn == NULL)
		return;

	if (dlua_script_create_file(script_fn, &script, user->event, &error) < 0) {
		user->error = p_strdup_printf(user->pool, "dlua_script_create_file(%s) failed: %s",
					      script_fn, error);
		return;
	}

	dlua_dovecot_register(script);
	dlua_register_mail_storage(script);

	/* init */
	if (dlua_script_init(script, &error) < 0) {
		user->error = p_strdup_printf(user->pool, "dlua_script_init(%s) failed: %s",
					      script_fn, error);
		dlua_script_unref(&script);
		return;
	}

	/* call postlogin hook */
	if ((ret = mail_lua_call_hook(script, user, MAIL_LUA_USER_CREATED_FN,
				      &error)) <= 0) {
		if (ret < 0)
			user->error = p_strdup(user->pool, error);
		dlua_script_unref(&script);
		return;
	}

	luser = p_new(user->pool, struct mail_lua_user_context, 1);
	luser->module_ctx.super = *v;
	v->deinit_pre = mail_lua_user_deinit_pre;
	v->deinit = mail_lua_user_deinit;
	luser->script = script;
	user->vlast = &luser->module_ctx.super;

	MODULE_CONTEXT_SET(user, mail_lua_user_module, luser);
}

bool mail_lua_plugin_get_script(struct mail_user *user,
				struct dlua_script **script_r)
{
	struct mail_lua_user_context *luser = MAIL_LUA_USER_CONTEXT(user);
	if (luser != NULL) {
		*script_r = luser->script;
		return TRUE;
	}
	return FALSE;
}

static const struct mail_storage_hooks mail_lua_hooks = {
	.mail_user_created = mail_lua_user_created,
};

void mail_lua_plugin_init(struct module *module)
{
	mail_storage_hooks_add(module, &mail_lua_hooks);
}

void mail_lua_plugin_deinit(void)
{
	mail_storage_hooks_remove(&mail_lua_hooks);
}

const char *mail_lua_plugin_dependencies[] = { NULL };