summaryrefslogtreecommitdiffstats
path: root/src/lib-storage/mail-lua.c
blob: a559f98ab82a3c41969c860cb34a4a94b72005a5 (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
/* Copyright (c) 2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "istream.h"
#include "array.h"
#include "var-expand.h"
#include "dlua-script.h"
#include "dlua-script-private.h"
#include "mail-storage.h"
#include "mailbox-attribute.h"
#include "mail-storage-lua.h"
#include "mail-storage-lua-private.h"
#include "mail-user.h"

#define LUA_STORAGE_MAIL "struct mail"

void dlua_push_mail(lua_State *L, struct mail *mail)
{
	luaL_checkstack(L, 20, "out of memory");
	/* create a table for holding few things */
	lua_createtable(L, 0, 20);
	luaL_setmetatable(L, LUA_STORAGE_MAIL);

	lua_pushlightuserdata(L, mail);
	lua_setfield(L, -2, "item");

#undef LUA_TABLE_SET_NUMBER
#define LUA_TABLE_SET_NUMBER(field) \
	lua_pushnumber(L, mail->field); \
	lua_setfield(L, -2, #field);
#undef LUA_TABLE_SET_BOOL
#define LUA_TABLE_SET_BOOL(field) \
	lua_pushboolean(L, mail->field); \
	lua_setfield(L, -2, #field);

	LUA_TABLE_SET_NUMBER(seq);
	LUA_TABLE_SET_NUMBER(uid);
	LUA_TABLE_SET_BOOL(expunged);

	dlua_push_mailbox(L, mail->box);
	lua_setfield(L, -2, "mailbox");

}

static struct mail *
lua_check_storage_mail(lua_State *L, int arg)
{
	if (!lua_istable(L, arg)) {
		(void)luaL_error(L, "Bad argument #%d, expected %s got %s",
				 arg, LUA_STORAGE_MAIL,
				 lua_typename(L, lua_type(L, arg)));
	}
	lua_pushliteral(L, "item");
	lua_rawget(L, arg);
	void *bp = (void*)lua_touserdata(L, -1);
	lua_pop(L, 1);
	return (struct mail*)bp;
}

static int lua_storage_mail_tostring(lua_State *L)
{
	DLUA_REQUIRE_ARGS(L, 1);
	struct mail *mail = lua_check_storage_mail(L, 1);

	const char *str =
		t_strdup_printf("<%s:UID %u>", mailbox_get_vname(mail->box),
				mail->uid);
	lua_pushstring(L, str);
	return 1;
}

static int lua_storage_mail_eq(lua_State *L)
{
	DLUA_REQUIRE_ARGS(L, 2);
	struct mail *mail = lua_check_storage_mail(L, 1);
	struct mail *mail2 = lua_check_storage_mail(L, 2);

	if (!DLUA_MAILBOX_EQUALS(mail->box, mail2->box))
		lua_pushboolean(L, FALSE);
	else
		lua_pushboolean(L, mail->uid != mail2->uid);
	return 1;
}

static int lua_storage_mail_lt(lua_State *L)
{
	DLUA_REQUIRE_ARGS(L, 2);
	struct mail *mail = lua_check_storage_mail(L, 1);
	struct mail *mail2 = lua_check_storage_mail(L, 2);

	if (!DLUA_MAILBOX_EQUALS(mail->box, mail2->box))
		return luaL_error(L,
				  "For lt, Mail can only be compared within same mailbox");
	else
		lua_pushboolean(L, mail->uid < mail2->uid);
	return 1;
}

static int lua_storage_mail_le(lua_State *L)
{
	DLUA_REQUIRE_ARGS(L, 2);
	struct mail *mail = lua_check_storage_mail(L, 1);
	struct mail *mail2 = lua_check_storage_mail(L, 2);

	if (!DLUA_MAILBOX_EQUALS(mail->box, mail2->box))
		return luaL_error(L,
				 "For le, mails can only be within same mailbox");
	else
		lua_pushboolean(L, mail->uid <= mail2->uid);

	return 1;
}

static int lua_storage_mail_gc(lua_State *L)
{
	(void)lua_check_storage_mail(L, 1);

	/* reset value to NULL */
	lua_pushliteral(L, "item");
	lua_pushnil(L);
	lua_rawset(L, 1);

	return 0;
}

static luaL_Reg lua_storage_mail_methods[] = {
	{ "__tostring", lua_storage_mail_tostring },
	{ "__eq", lua_storage_mail_eq },
	{ "__lt", lua_storage_mail_lt },
	{ "__le", lua_storage_mail_le },
	{ "__gc", lua_storage_mail_gc },
	{ NULL, NULL }
};

void lua_storage_mail_register(struct dlua_script *script)
{
	luaL_newmetatable(script->L, LUA_STORAGE_MAIL);
	lua_pushvalue(script->L, -1);
	lua_setfield(script->L, -2, "__index");
	luaL_setfuncs(script->L, lua_storage_mail_methods, 0);
	lua_pop(script->L, 1);
}