Simplify Lua modules.

This commit is contained in:
Nodir Temirkhodjaev 2015-01-26 09:55:17 +05:00
parent 5b400100fc
commit 8c849420f4
4 changed files with 153 additions and 164 deletions

View File

@ -8,7 +8,9 @@ local util_fs = require"wipf.util.fs"
local util_ip = require"wipf.util.ip" local util_ip = require"wipf.util.ip"
local APP_GROUP_MAX = 10 local util_conf = {
APP_GROUP_MAX = 10
}
local function parse_app(line) local function parse_app(line)
@ -53,8 +55,8 @@ local function app_groups_to_plain(app_groups)
local groups, groups_count = {}, app_groups.n local groups, groups_count = {}, app_groups.n
local apps_map = {} local apps_map = {}
if groups_count > APP_GROUP_MAX then if groups_count > util_conf.APP_GROUP_MAX then
return nil, i18n.tr_fmt('err_conf_app_group_max', APP_GROUP_MAX) return nil, i18n.tr_fmt('err_conf_app_group_max', util_conf.APP_GROUP_MAX)
end end
for i = 1, groups_count do for i = 1, groups_count do
@ -97,98 +99,97 @@ end
-- Configuration objects meta-table -- Configuration objects meta-table
local conf_meta = { local conf_meta = {}
set_ip_include_all = function (self, bool)
self.ip_include_all = bool
end,
get_ip_include_all = function (self)
return self.ip_include_all
end,
set_ip_exclude_all = function (self, bool)
self.ip_exclude_all = bool
end,
get_ip_exclude_all = function (self)
return self.ip_exclude_all
end,
set_app_log_blocked = function (self, bool)
self.app_log_blocked = bool
end,
get_app_log_blocked = function (self)
return self.app_log_blocked
end,
set_app_block_all = function (self, bool)
self.app_block_all = bool
end,
get_app_block_all = function (self)
return self.app_block_all
end,
set_app_allow_all = function (self, bool)
self.app_allow_all = bool
end,
get_app_allow_all = function (self)
return self.app_allow_all
end,
set_ip_include = function (self, str)
self.ip_include = str
end,
get_ip_include = function (self)
return self.ip_include
end,
set_ip_exclude = function (self, str)
self.ip_exclude = str
end,
get_ip_exclude = function (self)
return self.ip_exclude
end,
add_app_group = function (self, app_group)
local app_groups = self.app_groups
table.insert(app_groups, app_group)
app_groups.n = app_groups.n + 1
end,
remove_app_group = function (self, index)
local app_groups = self.app_groups
table.remove(app_groups, index)
app_groups.n = app_groups.n - 1
end,
-- Write conf. object to buffer
write = function (self, buf)
local iprange_from_inc, iprange_to_inc =
util_ip.ip4range_to_numbers(self.ip_include)
if not iprange_from_inc then
return nil, i18n.tr_fmt('err_conf_iprange_inc', iprange_to_inc)
end
local iprange_from_exc, iprange_to_exc =
util_ip.ip4range_to_numbers(self.ip_exclude)
if not iprange_from_exc then
return nil, i18n.tr_fmt('err_conf_iprange_exc', iprange_to_exc)
end
local group_bits, groups, app_3bits, apps =
app_groups_to_plain(self.app_groups)
if not group_bits then
return nil, groups
end
return true
end,
}
conf_meta.__index = conf_meta conf_meta.__index = conf_meta
function conf_meta:set_ip_include_all(bool)
self.ip_include_all = bool
end
function conf_meta:get_ip_include_all()
return self.ip_include_all
end
function conf_meta:set_ip_exclude_all(bool)
self.ip_exclude_all = bool
end
function conf_meta:get_ip_exclude_all()
return self.ip_exclude_all
end
function conf_meta:set_app_log_blocked(bool)
self.app_log_blocked = bool
end
function conf_meta:get_app_log_blocked()
return self.app_log_blocked
end
function conf_meta:set_app_block_all(bool)
self.app_block_all = bool
end
function conf_meta:get_app_block_all()
return self.app_block_all
end
function conf_meta:set_app_allow_all(bool)
self.app_allow_all = bool
end
function conf_meta:get_app_allow_all()
return self.app_allow_all
end
function conf_meta:set_ip_include(str)
self.ip_include = str
end
function conf_meta:get_ip_include()
return self.ip_include
end
function conf_meta:set_ip_exclude(str)
self.ip_exclude = str
end
function conf_meta:get_ip_exclude()
return self.ip_exclude
end
function conf_meta:add_app_group(app_group)
local app_groups = self.app_groups
table.insert(app_groups, app_group)
app_groups.n = app_groups.n + 1
end
function conf_meta:remove_app_group(index)
local app_groups = self.app_groups
table.remove(app_groups, index)
app_groups.n = app_groups.n - 1
end
-- Write conf. object to buffer
function conf_meta:write(buf)
local iprange_from_inc, iprange_to_inc =
util_ip.ip4range_to_numbers(self.ip_include)
if not iprange_from_inc then
return nil, i18n.tr_fmt('err_conf_iprange_inc', iprange_to_inc)
end
local iprange_from_exc, iprange_to_exc =
util_ip.ip4range_to_numbers(self.ip_exclude)
if not iprange_from_exc then
return nil, i18n.tr_fmt('err_conf_iprange_exc', iprange_to_exc)
end
local group_bits, groups, app_3bits, apps =
app_groups_to_plain(self.app_groups)
if not group_bits then
return nil, groups
end
return true
end
-- New conf. object -- New conf. object
local function new_conf() function util_conf.new_conf()
return setmetatable({ return setmetatable({
ip_include_all = false, ip_include_all = false,
ip_exclude_all = false, ip_exclude_all = false,
@ -206,41 +207,40 @@ end
-- Application group's meta-table -- Application group's meta-table
local app_group_meta = { local app_group_meta = {}
set_name = function (self, str)
self.name = str
end,
get_name = function (self)
return self.name
end,
set_enabled = function (self, bool)
self.enabled = bool
end,
get_enabled = function (self)
return self.enabled
end,
set_block = function (self, str)
self.block = str
end,
get_block = function (self)
return self.block
end,
set_allow = function (self, str)
self.allow = str
end,
get_allow = function (self)
return self.allow
end,
}
app_group_meta.__index = app_group_meta app_group_meta.__index = app_group_meta
function app_group_meta:set_name(str)
self.name = str
end
function app_group_meta:get_name()
return self.name
end
function app_group_meta:set_enabled(bool)
self.enabled = bool
end
function app_group_meta:get_enabled()
return self.enabled
end
function app_group_meta:set_block(str)
self.block = str
end
function app_group_meta:get_block()
return self.block
end
function app_group_meta:set_allow(str)
self.allow = str
end
function app_group_meta:get_allow()
return self.allow
end
-- New app. group object -- New app. group object
local function new_app_group() function util_conf.new_app_group()
return setmetatable({ return setmetatable({
name = "", name = "",
enabled = true, enabled = true,
@ -250,8 +250,4 @@ local function new_app_group()
end end
return { return util_conf
APPGROUP_MAX = APPGROUP_MAX,
new_conf = new_conf,
new_app_group = new_app_group,
}

View File

@ -5,20 +5,18 @@ local sys = require"sys"
local win32 = sys.win32 local win32 = sys.win32
local util_fs = {}
-- Get Process's Native path by identifier -- Get Process's Native path by identifier
local function pid_dospath(id) function util_fs.pid_dospath(id)
local pid = sys.pid(id, true) local pid = sys.pid(id, true)
local path = (pid and pid:path()) or "" local path = (pid and pid:path()) or ""
pid:close() pid:close()
return path return path
end end
-- Convert DOS device name to drive letter (A: .. Z:) -- DOS device name <-> drive letter (A: .. Z:)
local dosname_to_drive
-- Convert drive letter (A: .. Z:) to DOS device name
local drive_to_dosname
do do
local drives local drives
@ -39,35 +37,37 @@ do
return drives[key] or "" return drives[key] or ""
end end
dosname_to_drive = function (dos_name) -- Convert DOS device name to drive letter (A: .. Z:)
function util_fs.dosname_to_drive(dos_name)
return get_value(dos_name:lower()) return get_value(dos_name:lower())
end end
drive_to_dosname = function (drive) -- Convert drive letter (A: .. Z:) to DOS device name
function util_fs.drive_to_dosname(drive)
return get_value(drive:upper()) return get_value(drive:upper())
end end
end end
-- Convert Native path to Win32 path -- Convert Native path to Win32 path
local function dospath_to_path(dos_path) function util_fs.dospath_to_path(dos_path)
local dos_name, sub_path = string.match(dos_path, [[(\[^\]+\[^\]+)(\.+)]]) local dos_name, sub_path = string.match(dos_path, [[(\[^\]+\[^\]+)(\.+)]])
if not dos_name then if not dos_name then
return dos_path return dos_path
end end
return dosname_to_drive(dos_name) .. sub_path return util_fs.dosname_to_drive(dos_name) .. sub_path
end end
-- Convert Win32 path to Native path -- Convert Win32 path to Native path
local function path_to_dospath(path) function util_fs.path_to_dospath(path)
local drive, sub_path = string.match(path, [[(%a:)(\.+)]]) local drive, sub_path = string.match(path, [[(%a:)(\.+)]])
if not drive then if not drive then
return path return path
end end
return drive_to_dosname(drive) .. sub_path return util_fs.drive_to_dosname(drive) .. sub_path
end end
-- Load file, run it in sandbox and return it's globals in a table -- Load file, run it in sandbox and return it's globals in a table
function sandbox(path) function util_fs.sandbox(path)
local chunk, err_msg = loadfile(path) local chunk, err_msg = loadfile(path)
if not chunk then if not chunk then
return nil, err_msg return nil, err_msg
@ -79,9 +79,4 @@ function sandbox(path)
end end
return { return util_fs
pid_dospath = pid_dospath,
dospath_to_path = dospath_to_path,
path_to_dospath = path_to_dospath,
sandbox = sandbox,
}

View File

@ -3,25 +3,27 @@
local util_fs = require"wipf.util.fs" local util_fs = require"wipf.util.fs"
local i18n = {}
local current_lang, lang_strings local current_lang, lang_strings
local function set_current_lang(lang) function i18n.set_current_lang(lang)
current_lang = lang current_lang = lang
lang_strings = assert(util_fs.sandbox("lua/wipf/lang/" .. lang .. ".lua")) lang_strings = assert(util_fs.sandbox("lua/wipf/lang/" .. lang .. ".lua"))
end end
local function get_current_lang() function i18n.get_current_lang()
return current_lang return current_lang
end end
-- Get translation text for l10n identifier -- Get translation text for l10n identifier
local function tr(id) function i18n.tr(id)
return lang_strings[id] or id return lang_strings[id] or id
end end
-- Get formatted translation text for l10n identifier -- Get formatted translation text for l10n identifier
local function tr_fmt(id, ...) function i18n.tr_fmt(id, ...)
local s = lang_strings[id] local s = lang_strings[id]
if not s then if not s then
return id return id
@ -32,12 +34,7 @@ end
-- Set default language English -- Set default language English
set_current_lang("en") i18n.set_current_lang("en")
return { return i18n
set_current_lang = set_current_lang,
get_current_lang = get_current_lang,
tr = tr,
tr_fmt = tr_fmt,
}

View File

@ -6,8 +6,10 @@ local sys = require"sys"
local sock = require"sys.sock" local sock = require"sys.sock"
-- Convert IPv4 ranges in text to 'from_ip4' & 'to_ip4' arrays with numbers local util_ip = {}
local ip4range_to_numbers
-- Convert IPv4 ranges
do do
-- sort and try to merge ranges -- sort and try to merge ranges
local function iprange_map_merge(map) local function iprange_map_merge(map)
@ -82,7 +84,8 @@ do
return from_ip, to_ip return from_ip, to_ip
end end
ip4range_to_numbers = function (text) -- Convert IPv4 ranges in text to 'from_ip4' & 'to_ip4' arrays with numbers
function util_ip.ip4range_to_numbers(text)
local iprange_map = {} local iprange_map = {}
local line_no = 0 local line_no = 0
@ -103,6 +106,4 @@ do
end end
return { return util_ip
ip4range_to_numbers = ip4range_to_numbers,
}