From dc5b33b60a9b12d907642ca0da07dd172cfc8cc5 Mon Sep 17 00:00:00 2001 From: Lefteris Chatzimparmpas Date: Sun, 5 Feb 2012 13:29:43 +0100 Subject: [PATCH] Port to Lua 5.2. --- README | 2 +- doc/imapfilter_config.5 | 2 +- samples/extend.lua | 6 +++--- src/common.lua | 4 ++-- src/core.c | 14 ++++++++++++-- src/deprecated.lua | 9 ++++++++- src/lua.c | 42 ++++++----------------------------------- src/pcre.c | 16 +++++++++++++--- src/regex.lua | 4 +++- src/set.lua | 12 ++++++------ src/system.c | 16 ++++++++++++++-- 11 files changed, 69 insertions(+), 58 deletions(-) diff --git a/README b/README index cca1df3..e3660af 100644 --- a/README +++ b/README @@ -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: diff --git a/doc/imapfilter_config.5 b/doc/imapfilter_config.5 index b239c1a..9ee8a37 100644 --- a/doc/imapfilter_config.5 +++ b/doc/imapfilter_config.5 @@ -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 diff --git a/samples/extend.lua b/samples/extend.lua index 1959cbf..28461b6 100644 --- a/samples/extend.lua +++ b/samples/extend.lua @@ -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') .. diff --git a/src/common.lua b/src/common.lua index 356e4b0..4db084a 100644 --- a/src/common.lua +++ b/src/common.lua @@ -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 diff --git a/src/core.c b/src/core.c index cd6379c..937f9e2 100644 --- a/src/core.c +++ b/src/core.c @@ -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; } diff --git a/src/deprecated.lua b/src/deprecated.lua index 3ba7bb8..4f2442e 100644 --- a/src/deprecated.lua +++ b/src/deprecated.lua @@ -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') diff --git a/src/lua.c b/src/lua.c index 659ac17..9faffc5 100644 --- a/src/lua.c +++ b/src/lua.c @@ -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; diff --git a/src/pcre.c b/src/pcre.c index cd263ee..a813b9f 100644 --- a/src/pcre.c +++ b/src/pcre.c @@ -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; } diff --git a/src/regex.lua b/src/regex.lua index 859a7b8..d6e6c1e 100644 --- a/src/regex.lua +++ b/src/regex.lua @@ -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)) diff --git a/src/set.lua b/src/set.lua index 6f192dd..f6ea486 100644 --- a/src/set.lua +++ b/src/set.lua @@ -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 diff --git a/src/system.c b/src/system.c index 65662de..303d866 100644 --- a/src/system.c +++ b/src/system.c @@ -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; }