-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugin-lua.c
153 lines (138 loc) · 2.84 KB
/
plugin-lua.c
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
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "mailfront.h"
#include <msg/msg.h>
static lua_State* L = 0;
static const response* get_response(void)
{
static response resp;
static str msgcopy;
const char* msg;
resp.number = lua_tonumber(L, -2);
msg = lua_tostring(L, -1);
if (msg == 0 || *msg == 0) {
resp.message = (resp.number < 400) ? "OK"
: (resp.number < 500) ? "Deferred"
: "Rejected";
}
else {
if (!str_copys(&msgcopy, msg))
return &resp_oom;
resp.message = msgcopy.s;
}
lua_pop(L, 2);
return (resp.number > 0) ? &resp : 0;
}
static int setup(const char* name)
{
if (L != 0) {
lua_getfield(L, LUA_GLOBALSINDEX, name);
if (lua_isfunction(L, -1))
return 1;
lua_pop(L, 1);
}
return 0;
}
static const response* callit(int nparams)
{
int code;
if ((code = lua_pcall(L, nparams, 2, 0)) != 0) {
msgf("{Lua error: }d{ (}s{)}", code, lua_tostring(L, -1));
lua_pop(L, 1);
return &resp_internal;
}
return get_response();
}
static const response* init(void)
{
const char* env;
env = session_getenv("LUA_SCRIPT");
if (env != 0) {
L = luaL_newstate();
if (L == 0)
return &resp_oom;
switch (luaL_loadfile(L, env)) {
case LUA_ERRMEM:
return &resp_oom;
case 0:
break;
case LUA_ERRFILE:
msg3("Cannot read \"", env, "\"");
return &resp_internal;
case LUA_ERRSYNTAX:
msg3("Syntax error in \"", env, "\"");
return &resp_internal;
default:
return &resp_internal;
}
return callit(0);
}
return 0;
}
static const response* reset(void)
{
if (setup("reset"))
return callit(0);
return 0;
}
static const response* helo(str* hostname)
{
if (setup("helo")) {
lua_pushlstring(L, hostname->s, hostname->len);
return callit(1);
}
return 0;
}
static const response* sender(str* address)
{
if (setup("sender")) {
lua_pushlstring(L, address->s, address->len);
return callit(1);
}
return 0;
}
static const response* recipient(str* address)
{
if (setup("recipient")) {
lua_pushlstring(L, address->s, address->len);
return callit(1);
}
return 0;
}
static const response* data_start(int fd)
{
if (setup("data_start")) {
lua_pushinteger(L, fd);
return callit(1);
}
return 0;
}
static const response* data_block(const char* bytes, unsigned long len)
{
if (setup("data_block")) {
lua_pushlstring(L, bytes, len);
return callit(1);
}
return 0;
}
static const response* message_end(int fd)
{
if (setup("message_end")) {
lua_pushinteger(L, fd);
return callit(1);
}
return 0;
}
struct plugin plugin = {
.version = PLUGIN_VERSION,
.flags = 0,
.init = init,
.reset = reset,
.helo = helo,
.sender = sender,
.recipient = recipient,
.data_start = data_start,
.data_block = data_block,
.message_end = message_end,
};