Port to Lua 5.2.

This commit is contained in:
Lefteris Chatzimparmpas 2012-02-05 13:29:43 +01:00
parent a6148a4466
commit dc5b33b60a
11 changed files with 69 additions and 58 deletions

2
README
View File

@ -25,7 +25,7 @@ Changes
Installation
Compile time requirements are Lua (version 5.1), the PCRE library, and
Compile time requirements are Lua (version 5.2 or 5.1), the PCRE library, and
optionally the OpenSSL library (for SSL/TLS and CRAM-MD5 support).
Compile and install the program:

View File

@ -1039,7 +1039,7 @@ searching:
.Bd -literal -offset 4n
results = account.INBOX:is_unseen()
for _, message in ipairs(results) do
mailbox, uid = unpack(message)
mailbox, uid = table.unpack(message)
header = mailbox[uid]:fetch_header()
end
.Ed

View File

@ -34,7 +34,7 @@ all = myaccount.mymailbox:select_all()
results = Set {}
for _, mesg in ipairs(all) do
mbox, uid = unpack(mesg)
mbox, uid = table.unpack(mesg)
text = mbox[uid]:fetch_message()
if (pipe_to('bayesian-spam-filter', text) == 1) then
table.insert(results, mesg)
@ -52,7 +52,7 @@ all = myaccount.mymailbox:select_all()
results = Set {}
for _, mesg in ipairs(all) do
mbox, uid = unpack(mesg)
mbox, uid = table.unpack(mesg)
structure = mbox[uid]:fetch_structure()
for partid, partinf in pairs(structure) do
if partinf.type:lower() == 'text/plain' and partinf.size < 1024 then
@ -77,7 +77,7 @@ results:delete_messages()
all = myaccount.mymailbox:select_all()
for _, mesg in ipairs(all) do
mbox, uid = unpack(all)
mbox, uid = table.unpack(all)
header = mbox[uid]:fetch_header()
body = mbox[uid]:fetch_body()
message = header:gsub('[\r\n]+$', '\r\n') ..

View File

@ -32,7 +32,7 @@ end
function _extract_mailboxes(messages)
local t = {}
for _, v in ipairs(messages) do
b, _ = unpack(v)
b, _ = table.unpack(v)
t[b] = true
end
return t
@ -41,7 +41,7 @@ end
function _extract_messages(mailbox, messages)
local t = {}
for _, v in ipairs(messages) do
b, m = unpack(v)
b, m = table.unpack(v)
if mailbox == b then
table.insert(t, m)
end

View File

@ -38,7 +38,7 @@ static int ifcore_idle(lua_State *lua);
/* Lua imapfilter core library functions. */
static const luaL_reg ifcorelib[] = {
static const luaL_Reg ifcorelib[] = {
{ "noop", ifcore_noop },
{ "logout", ifcore_logout },
{ "login", ifcore_login },
@ -944,7 +944,12 @@ ifcore_append(lua_State *lua)
lua_pop(lua, 1);
r = request_append(s, p, u, lua_tostring(lua, 2), lua_tostring(lua, 3),
lua_strlen(lua, 3), lua_type(lua, 4) == LUA_TSTRING ?
#if LUA_VERSION_NUM < 502
lua_objlen(lua, 3),
#else
lua_rawlen(lua, 3),
#endif
lua_type(lua, 4) == LUA_TSTRING ?
lua_tostring(lua, 4) : NULL, lua_type(lua, 5) == LUA_TSTRING ?
lua_tostring(lua, 5) : NULL);
@ -1159,7 +1164,12 @@ LUALIB_API int
luaopen_ifcore(lua_State *lua)
{
#if LUA_VERSION_NUM < 502
luaL_register(lua, "ifcore", ifcorelib);
#else
luaL_newlib(lua, ifcorelib);
lua_setglobal(lua, "ifcore");
#endif
return 1;
}

View File

@ -1,4 +1,11 @@
-- The old and deprecated interface, provided for compatibility.
-- Compatibility workarounds and the old obsolete interface
if (_VERSION == 'Lua 5.1') then
table.unpack = unpack
else
unpack = table.unpack
end
function check(account, mbox)
_check_required(account, 'table')

View File

@ -159,17 +159,9 @@ get_option_boolean(const char *opt)
{
int b;
lua_pushstring(lua, "options");
lua_gettable(lua, LUA_GLOBALSINDEX);
if (!lua_istable(lua, -1)) {
lua_pop(lua, 1);
return 0;
}
lua_pushstring(lua, opt);
lua_gettable(lua, -2);
lua_getglobal(lua, "options");
lua_getfield(lua, -1, opt);
b = lua_toboolean(lua, -1);
lua_pop(lua, 2);
return b;
@ -184,17 +176,9 @@ get_option_number(const char *opt)
{
lua_Number n;
lua_pushstring(lua, "options");
lua_gettable(lua, LUA_GLOBALSINDEX);
if (!lua_istable(lua, -1)) {
lua_pop(lua, 1);
return 0;
}
lua_pushstring(lua, opt);
lua_gettable(lua, -2);
lua_getglobal(lua, "options");
lua_getfield(lua, -1, opt);
n = lua_tonumber(lua, -1);
lua_pop(lua, 2);
return n;
@ -209,17 +193,9 @@ get_option_string(const char *opt)
{
const char *s;
lua_pushstring(lua, "options");
lua_gettable(lua, LUA_GLOBALSINDEX);
if (!lua_istable(lua, -1)) {
lua_pop(lua, 1);
return NULL;
}
lua_pushstring(lua, opt);
lua_gettable(lua, -2);
lua_getglobal(lua, "options");
lua_getfield(lua, -1, opt);
s = lua_tostring(lua, -1);
lua_pop(lua, 2);
return s;
@ -236,9 +212,7 @@ get_table_type(const char *key)
lua_pushstring(lua, key);
lua_gettable(lua, -2);
t = lua_type(lua, -1);
lua_pop(lua, 1);
return t;
@ -255,9 +229,7 @@ get_table_number(const char *key)
lua_pushstring(lua, key);
lua_gettable(lua, -2);
n = lua_tonumber(lua, -1);
lua_pop(lua, 1);
return n;
@ -274,9 +246,7 @@ get_table_string(const char *key)
lua_pushstring(lua, key);
lua_gettable(lua, -2);
s = lua_tostring(lua, -1);
lua_pop(lua, 1);
return s;

View File

@ -15,7 +15,7 @@ static int ifre_exec(lua_State *lua);
static int ifre_free(lua_State *lua);
/* Lua imapfilter library of PCRE related functions. */
static const luaL_reg ifrelib[] = {
static const luaL_Reg ifrelib[] = {
{ "flags", ifre_flags },
{ "compile", ifre_compile },
{ "exec", ifre_exec },
@ -207,8 +207,13 @@ ifre_exec(lua_State *lua)
for (i = 0; i <= ovecsize; i++)
ovector[2 * i] = ovector[2 * i + 1] = -1;
n = pcre_exec(re, NULL, lua_tostring(lua, 2), lua_strlen(lua, 2), 0,
lua_tonumber(lua, 3), ovector, (ovecsize + 1) * 3);
n = pcre_exec(re, NULL, lua_tostring(lua, 2),
#if LUA_VERSION_NUM < 502
lua_objlen(lua, 2),
#else
lua_rawlen(lua, 2),
#endif
0, lua_tonumber(lua, 3), ovector, (ovecsize + 1) * 3);
if (n > 0)
for (i = 1; i < n; i++)
@ -262,7 +267,12 @@ LUALIB_API int
luaopen_ifre(lua_State *lua)
{
#if LUA_VERSION_NUM < 502
luaL_register(lua, "ifre", ifrelib);
#else
luaL_newlib(lua, ifrelib);
lua_setglobal(lua, "ifre");
#endif
return 1;
}

View File

@ -7,7 +7,9 @@ setmetatable(_regex_cache, _regex_cache.mt)
_regex_cache.mt.__index = function (self, key)
local _, _, pattern, cflags = string.find(key, '^(.*)%z(.*)$')
local zero
if (_VERSION == 'Lua 5.1') then zero = '%z' else zero = '\0' end
local _, _, pattern, cflags = string.find(key, '^(.*)' .. zero .. '(.*)$')
local _, compiled = ifre.compile(pattern, tonumber(cflags))

View File

@ -33,13 +33,13 @@ function Set._union(seta, setb)
local t = {}
for _, v in ipairs(seta) do
b, m = unpack(v)
b, m = table.unpack(v)
if not t[b] then t[b] = {} end
t[b][m] = true
end
for _, v in ipairs(setb) do
b, m = unpack(v)
b, m = table.unpack(v)
if not t[b] then t[b] = {} end
t[b][m] = true
end
@ -59,13 +59,13 @@ function Set._intersection(seta, setb)
local tb = {}
for _, v in ipairs(seta) do
b, m = unpack(v)
b, m = table.unpack(v)
if not ta[b] then ta[b] = {} end
ta[b][m] = true
end
for _, v in ipairs(setb) do
b, m = unpack(v)
b, m = table.unpack(v)
if not tb[b] then tb[b] = {} end
tb[b][m] = true
end
@ -88,13 +88,13 @@ function Set._difference(seta, setb)
local t = {}
for _, v in ipairs(seta) do
b, m = unpack(v)
b, m = table.unpack(v)
if not t[b] then t[b] = {} end
t[b][m] = true
end
for _, v in ipairs(setb) do
b, m = unpack(v)
b, m = table.unpack(v)
if t[b] then
t[b][m] = nil
end

View File

@ -24,7 +24,7 @@ static int ifsys_sleep(lua_State *lua);
static int ifsys_daemon(lua_State *lua);
/* Lua imapfilter library of system's functions. */
static const luaL_reg ifsyslib[] = {
static const luaL_Reg ifsyslib[] = {
{ "echo", ifsys_echo },
{ "noecho", ifsys_noecho },
{ "popen", ifsys_popen },
@ -182,7 +182,13 @@ ifsys_read(lua_State *lua)
if (fgets(c, LUAL_BUFFERSIZE, fp) == NULL && feof(fp)) {
luaL_pushresult(&b);
return (lua_strlen(lua, -1) > 0);
return (
#if LUA_VERSION_NUM < 502
lua_objlen(lua, -1)
#else
lua_rawlen(lua, -1)
#endif
> 0);
}
n = strlen(c);
@ -302,7 +308,13 @@ LUALIB_API int
luaopen_ifsys(lua_State *lua)
{
#if LUA_VERSION_NUM < 502
luaL_register(lua, "ifsys", ifsyslib);
#else
luaL_newlib(lua, ifsyslib);
lua_setglobal(lua, "ifsys");
#endif
return 1;
}