From 218a74f45956be17abf14182824563a5b9f2f2ac Mon Sep 17 00:00:00 2001 From: Gerard Hickey Date: Sun, 7 Apr 2024 09:55:26 -0400 Subject: [PATCH] Replace nixio modules with pure Lua functions Signed-off-by: Gerard Hickey --- meshchat | 185 +++++++++++++++++++++---------------------- meshchatlib.lua | 91 +++++++++++++++++---- support/meshchatsync | 108 ++++++++++++------------- 3 files changed, 223 insertions(+), 161 deletions(-) diff --git a/meshchat b/meshchat index 2747936..8caa7f5 100755 --- a/meshchat +++ b/meshchat @@ -39,9 +39,8 @@ package.path = package.path .. ";/www/cgi-bin/?.lua" require('luci.http') local json = require("json") -require("nixio") -require("meshchatconfig") -require("meshchatlib") +local config = require("meshchatconfig") +local lib = require("meshchatlib") --- -- @module meshchat @@ -49,7 +48,7 @@ require("meshchatlib") local query = {} local uploadfilename if os.getenv("QUERY_STRING") ~= "" or os.getenv("REQUEST_METHOD") == "POST" then - local request = luci.http.Request(nixio.getenv(), + local request = luci.http.Request(lib.getenv(), function() local v = io.read(1024) if not v then @@ -65,7 +64,7 @@ if os.getenv("QUERY_STRING") ~= "" or os.getenv("REQUEST_METHOD") == "POST" then if meta and meta.file then uploadfilename = meta.file end - nixio.fs.mkdir(tmp_upload_dir) + mkdir(tmp_upload_dir) fp = io.open(tmp_upload_dir .. "/file", "w") end if chunk then @@ -111,12 +110,12 @@ function config() print("\r") local settings = { - version = app_version, - protocol_verison = protocol_version, - node = node_name(), - zone = zone_name(), - default_channel = default_channel, - debug = debug, + version = config.app_version, + protocol_verison = config.protocol_version, + node = lib.node_name(), + zone = lib.zone_name(), + default_channel = config.default_channel, + debug = config.debug, } print(json.encode(settings)) @@ -166,21 +165,21 @@ function send_message() epoch = query.epoch end - get_lock() + lib.get_lock(config.lock_file) local f = io.open(messages_db_file, "a") if not f then - release_lock() + lib.release_lock(config.lock_file) -- TODO return a proper error code on failure die("Cannot send message") end - f:write(id .. "\t" .. epoch .. "\t" .. message .. "\t" .. query.call_sign .. "\t" .. node_name() .. "\t" .. platform .. "\t" .. query.channel .. "\n") + f:write(id .. "\t" .. epoch .. "\t" .. message .. "\t" .. query.call_sign .. "\t" .. lib.node_name() .. "\t" .. config.platform .. "\t" .. query.channel .. "\n") f:close() - sort_and_trim_db() - save_messages_db_version() + lib.sort_and_trim_db() + lib.save_messages_db_version() - release_lock() + lib.release_lock(config.lock_file) print([[{"status":200, "response":"OK"}]]) end @@ -220,13 +219,13 @@ function messages() print("\r") io.flush() - get_lock() + lib.get_lock(config.lock_file) - local node = node_name() + local node = lib.node_name() -- read in message DB and parse the contents local messages = {} - for line in io.lines(messages_db_file) + for line in io.lines(config.messages_db_file) do local id, epoch, message, call_sign, node, platform, channel = line:match("^(%S+)\t(%S+)\t(.+)\t([^\t]+)\t(%S*)\t(%S+)\t(%S*)$") if epoch and #epoch > 0 then @@ -246,8 +245,8 @@ function messages() local users = {} -- read the users status file - if nixio.fs.stat(local_users_status_file) then - for line in io.lines(local_users_status_file) + if lib.file_exists(config.local_users_status_file) then + for line in io.lines(config.local_users_status_file) do local call_sign = line:match("^([^\t]+)\t") if call_sign then @@ -264,7 +263,7 @@ function messages() -- rewrite user status file updating the timestamp for requesting call sign -- query.id is the meshchat_id - local f = io.open(local_users_status_file, "w") + local f = io.open(config.local_users_status_file, "w") if f then local found_user = false for call_sign, line in pairs(users) @@ -283,7 +282,7 @@ function messages() end end - release_lock() + lib.release_lock(config.lock_file) -- order messages according to time table.sort(messages, function(a, b) return a.epoch > b.epoch end) @@ -312,11 +311,11 @@ function sync_status() print("Content-type: application/json\r") print("\r") - get_lock() + lib.get_lock(config.lock_file) local status = {} - if nixio.fs.stat(sync_status_file) then - for line in io.lines(sync_status_file) + if lib.file_exists(config.sync_status_file) then + for line in io.lines(config.sync_status_file) do local node, epoch = line:match("^(.*)\t(.*)$") status[#status + 1] = { @@ -326,7 +325,7 @@ function sync_status() end end - release_lock() + lib.release_lock(config.lock_file) table.sort(status, function(a, b) return a.epoch > b.epoch end) @@ -335,16 +334,16 @@ end --- Return a list of messages as text. function messages_raw() - get_lock() + lib.get_lock(config.lock_file) - local md5 = file_md5(messages_db_file) + local md5 = file_md5(config.messages_db_file) local lines = {} - for line in io.lines(messages_db_file) + for line in io.lines(config.messages_db_file) do lines[#lines + 1] = line end - release_lock() + lib.release_lock(config.lock_file) print("Content-MD5: " .. md5 .. "\r") print("Content-type: text/plain\r") @@ -358,11 +357,11 @@ end --- Return the current MD5 has of the messages database. function messages_md5() - get_lock() + lib.get_lock(config.lock_file) - local md5 = file_md5(messages_db_file) + local md5 = lib.file_md5(config.messages_db_file) - release_lock() + lib.release_lock(config.lock_file) print("Content-type: text/plain\r") print("\r") @@ -371,16 +370,16 @@ end --- Package the raw messages as the messages.txt file. function messages_download() - get_lock() + lib.get_lock() - local md5 = file_md5(messages_db_file) + local md5 = lib.file_md5(config.messages_db_file) local lines = {} - for line in io.lines(messages_db_file) + for line in io.lines(config.messages_db_file) do lines[#lines + 1] = line end - release_lock() + lib.release_lock(config.lock_file) print("Content-MD5: " .. md5 .. "\r") print("Content-Disposition: attachment; filename=messages.txt;\r") @@ -395,16 +394,16 @@ end --- Return the list of users as raw text. function users_raw() - get_lock() + lib.get_lock(config.lock_file) - local md5 = file_md5(local_users_status_file) + local md5 = lib.file_md5(config.local_users_status_file) local lines = {} - for line in io.lines(local_users_status_file) + for line in io.lines(config.local_users_status_file) do lines[#lines + 1] = line end - release_lock() + lib.release_lock(config.lock_file) print("Content-MD5: " .. md5 .. "\r") print("Content-type: text/plain\r") @@ -438,10 +437,10 @@ function users() print("Content-type: application/json\r") print("\r") - get_lock() + lib.get_lock(config.lock_file) local users = {} - for line in io.lines(local_users_status_file) + for line in io.lines(config.local_users_status_file) do local call_sign, id, node, epoch, platform = line:match("^(.*)\t(.*)\t(.*)\t(.*)\t(.*)$") if epoch and #epoch > 0 then @@ -454,7 +453,7 @@ function users() } end end - for line in io.lines(remote_users_status_file) + for line in io.lines(config.remote_users_status_file) do local call_sign, id, node, epoch, platform = line:match("^(.*)\t(.*)\t(.*)\t(.*)\t(.*)$") if epoch and #epoch > 0 then @@ -468,7 +467,7 @@ function users() end end - release_lock() + lib.release_lock(config.lock_file) table.sort(users, function(a, b) return a.epoch > b.epoch end) @@ -477,24 +476,24 @@ end --- Return a list of files as plain text. function local_files_raw() - get_lock() + lib.get_lock(config.lock_file) - local tmp_file = meshchat_path .. "/meshchat_files_local." .. nixio.getpid() + local tmp_file = config.meshchat_path .. "/meshchat_files_local." .. math.random(5000) local f = io.open(tmp_file, "w") if not f then die("Cannot list local files") end - local name = node_name() .. ":" .. os.getenv("SERVER_PORT") - for file in nixio.fs.dir(local_files_dir) + local name = lib.node_name() .. ":" .. os.getenv("SERVER_PORT") + for file in lib.filelist(config.local_files_dir) do - local stat = nixio.fs.stat(local_files_dir .. "/" .. file) - f:write(file .. "\t" .. name .. "\t" .. stat.size .. "\t" .. stat.mtime .. platform .. "\n") + local stat = lib.file_exists(config.local_files_dir .. "/" .. file) + f:write(file .. "\t" .. name .. "\t" .. stat.size .. "\t" .. stat.mtime .. config.platform .. "\n") end f:close() - local md5 = file_md5(tmp_file) + local md5 = lib.file_md5(tmp_file) - release_lock() + lib.release_lock(config.lock_file) print("Content-MD5: " .. md5 .. "\r") print("Content-type: text/plain\r") @@ -505,7 +504,7 @@ function local_files_raw() print(line) end - nixio.fs.remove(tmp_file) + lib.unlink(tmp_file) end --- Return a specified file as a download. @@ -523,19 +522,19 @@ end -- function file_download() local file = query.file - local file_path = local_files_dir .. "/" .. file + local file_path = config.local_files_dir .. "/" .. file - if file == "" or not nixio.fs.stat(file_path) then + if file == "" or not lib.file_exists(file_path) then error("no file") return end - get_lock() + lib.get_lock(config.lock_file) - local md5 = file_md5(file_path) + local md5 = lib.file_md5(file_path) local f = io.open(file_path, "rb") - release_lock() + lib.release_lock(config.lock_file) print("Content-MD5: " .. md5 .. "\r") print("Content-Disposition: attachment; filename=\"" .. file .. "\";\r") @@ -570,23 +569,23 @@ function files() print("Content-type: application/json\r") print("\r") - get_lock() + lib.get_lock(config.lock_file) local files = {} - local node = node_name() .. ":" .. os.getenv("SERVER_PORT") - for file in nixio.fs.dir(local_files_dir) + local node = lib.node_name() .. ":" .. os.getenv("SERVER_PORT") + for file in lib.filelist(config.local_files_dir) do - local stat = nixio.fs.stat(local_files_dir .. "/" .. file) + local stat = lib.file_exists(config.local_files_dir .. "/" .. file) files[#files + 1] = { file = file, epoch = stat.mtime, size = stat.size, node = node, - platform = platform + platform = config.platform } files[#files]["local"] = 1 end - for file in nixio.fs.dir(meshchat_path) + for file in lib.filelist(meshchat_path) do if file:match("^remote_files%.") then for line in io.lines(meshchat_path .. "/" .. file) @@ -598,7 +597,7 @@ function files() epoch = tonumber(epoch), size = size, node = node, - platform = platform + platform = config.platform } files[#files]["local"] = 0 end @@ -606,9 +605,9 @@ function files() end end - local stats = file_storage_stats() + local stats = lib.file_storage_stats() - release_lock() + lib.release_lock(config.lock_file) table.sort(files, function(a, b) return a.epoch > b.epoch end) @@ -620,7 +619,7 @@ end --- Delete the specified file. function delete_file() - nixio.fs.remove(local_files_dir .. "/" .. query.file) + lib.unlink(config.local_files_dir .. "/" .. query.file) print("Content-type: application/json\r") print("\r") print([[{"status":200, "response":"OK"}]]) @@ -630,7 +629,7 @@ end function messages_version() print("Content-type: text/plain\r") print("\r") - print(get_messages_db_version()) + print(lib.get_messages_db_version()) end --- Return a JSON document of the messages database. @@ -638,12 +637,12 @@ function messages_version_ui() print("Content-type: application/json\r") print("\r") - print(string.format([[{"messages_version":%s}]], get_messages_db_version())) + print(string.format([[{"messages_version":%s}]], lib.get_messages_db_version())) - get_lock() + lib.get_lock(config.lock_file) local users = {} - for line in io.lines(local_users_status_file) + for line in io.lines(config.local_users_status_file) do local call_sign = line:match("^([^\t]+)\t") if call_sign then @@ -651,32 +650,32 @@ function messages_version_ui() end end - local node = node_name() + local node = lib.node_name() local epoch = os.time() if tonumber(query.epoch) > epoch then epoch = query.epoch end -- TODO refactor here and messages function into a single code block - local f = io.open(local_users_status_file, "w") + local f = io.open(config.local_users_status_file, "w") if f then local found_user = false for call_sign, line in pairs(users) do if call_sign == query.call_sign then - f:write(call_sign .. "\t" .. query.id .. "\t" .. node .. "\t" .. epoch .. "\t" .. platform .. "\n") + f:write(call_sign .. "\t" .. query.id .. "\t" .. node .. "\t" .. epoch .. "\t" .. config.platform .. "\n") found_user = true else f:write(line .. "\n") end end if not found_user then - f:write(query.call_sign .. "\t" .. query.id .. "\t" .. node .. "\t" .. epoch .. "\t" .. platform .. "\n") + f:write(query.call_sign .. "\t" .. query.id .. "\t" .. node .. "\t" .. epoch .. "\t" .. config.platform .. "\n") end f:close() end - release_lock() + lib.release_lock(config.lock_file) end --- Return a JSON document describing all the hosts. @@ -699,7 +698,7 @@ function hosts() print("Content-type: application/json\r") print("\r") - local node = node_name() + local node = lib.node_name() local hosts = {} for line in io.lines("/var/dhcp.leases") do @@ -714,7 +713,7 @@ function hosts() for line in io.lines("/etc/config.mesh/_setup.dhcp.dmz") do local mac, num, hostname = line:match("^(%S+)%s(%S+)%s(%S+)$") - local ip = gethostbyname(hostname) + local ip = lib.gethostbyname(hostname) hosts[#hosts + 1] = { ip = ip, hostname = hostname, @@ -722,9 +721,9 @@ function hosts() } end - for _, remote_node in ipairs(node_list()) + for _, remote_node in ipairs(lib.node_list()) do - local f = io.popen("/usr/bin/curl --retry 0 --connect-timeout " .. connect_timeout .. " --speed-time " .. speed_time .. " --speed-limit " .. speed_limit .. " http://" .. remote_node .. ":8080/cgi-bin/meshchat?action=hosts_raw 2> /dev/null") + local f = io.popen("/usr/bin/curl --retry 0 --connect-timeout " .. config.connect_timeout .. " --speed-time " .. config.speed_time .. " --speed-limit " .. config.speed_limit .. " http://" .. remote_node .. ":8080/cgi-bin/meshchat?action=hosts_raw 2> /dev/null") if f then for line in f:lines() do @@ -764,7 +763,7 @@ function hosts_raw() for line in io.lines("/etc/config.mesh/_setup.dhcp.dmz") do local mac, num, hostname = line:match("^(%S+)%s(%S+)%s(%S+)$") - local ip = gethostbyname(hostname) + local ip = lib.gethostbyname(hostname) hosts[#hosts + 1] = { ip = ip, hostname = hostname @@ -779,27 +778,27 @@ end --- Store a file into the file directory. function upload_file() - local new_file_size = nixio.fs.stat(tmp_upload_dir .. "/file").size + local new_file_size = lib.file_exists(config.tmp_upload_dir .. "/file").size - get_lock() + lib.get_lock(config.lock_file) - local stats = file_storage_stats() + local stats = lib.file_storage_stats() - release_lock() + lib.release_lock(config.lock_file) print("Content-type: application/json\r") print("\r") if new_file_size > stats.files_free then - nixio.fs.remove(tmp_upload_dir .. "/file") + lib.unlink(config.tmp_upload_dir .. "/file") print([[{"status":500, "response":"Not enough storage, delete some files"}]]) else - local fi = io.open(tmp_upload_dir .. "/file", "r") - local fo = io.open(local_files_dir .. "/" .. uploadfilename, "w") + local fi = io.open(config.tmp_upload_dir .. "/file", "r") + local fo = io.open(config.local_files_dir .. "/" .. uploadfilename, "w") fo:write(fi:read("*a")) fi:close() fo:close() - nixio.fs.remove(tmp_upload_dir .. "/file") + lib.unlink(config.tmp_upload_dir .. "/file") print([[{"status":200, "response":"OK"}]]) end end diff --git a/meshchatlib.lua b/meshchatlib.lua index f409ea2..77f08b9 100755 --- a/meshchatlib.lua +++ b/meshchatlib.lua @@ -34,8 +34,8 @@ --]] -require("nixio") require("uci") +require("socket") --- @module meshchatlib @@ -86,7 +86,7 @@ function zone_name() if dmz_mode ~= "0" then servfile = "/etc/config.mesh/_setup.services.dmz" end - if nixio.fs.access(servfile) then + if file_exists(servfile) then for line in io.lines(servfile) do local zone = line:match("^(.*)|.*|.*|.*|.*|meshchat$") @@ -100,16 +100,19 @@ end messages_db_file = messages_db_file_orig .. "." .. zone_name() -local lock_fd -function get_lock() - if not lock_fd then - lock_fd = nixio.open(lock_file, "w", "666") +local lock_fd = false +function get_lock(lock_file) + while (not os.execute("mkdir " .. lock_file .. " >/dev/null 2>&1")) do + socket.select(nil, nil, 0.2) end - lock_fd:lock("lock") + lock_fd = true end -function release_lock() - lock_fd:lock("ulock") +function release_lock(lock_file) + if (lock_fd) then + os.execute("rmdir " .. lock_file) + lock_fd = false + end end --- Generate the MD5 sum of a file. @@ -125,7 +128,7 @@ end -- @treturn string Result of `md5sum` of the file -- function file_md5(file) - if not nixio.fs.stat(file) then + if not file_exists(file) then return "" end local output = capture("md5sum " .. file:gsub(" ", "\\ ")):match("^(%S+)%s") @@ -144,7 +147,7 @@ function save_messages_db_version() local f = io.open(messages_version_file, "w") f:write(get_messages_version_file() .. "\n") f:close() - nixio.fs.chmod(messages_version_file, "666") + chmod(messages_version_file, "666") end function get_messages_version_file() @@ -220,9 +223,9 @@ function file_storage_stats() local total = used + available local local_files_bytes = 0 - for file in nixio.fs.dir(local_files_dir) + for file in filelist(local_files_dir) do - local_files_bytes = local_files_bytes + nixio.fs.stat(local_files_dir .. "/" .. file).size + local_files_bytes = local_files_bytes + file_size(local_files_dir .. "/" .. file) end if max_file_storage - local_files_bytes < 0 then @@ -243,7 +246,7 @@ function gethostbyname(hostname) end function node_list() - if not nixio.fs.stat("/var/run/services_olsr") then + if not file_exists("/var/run/services_olsr") then return {} end local local_node = node_name():lower() @@ -283,3 +286,63 @@ end function str_escape(str) return str:gsub("%(", "%%("):gsub("%)", "%%)"):gsub("%%", "%%%%"):gsub("%.", "%%."):gsub("%+", "%%+"):gsub("-", "%%-"):gsub("%*", "%%*"):gsub("%[", "%%["):gsub("%?", "%%?"):gsub("%^", "%%^"):gsub("%$", "%%$") end + + +function getenv() + local osEnv = {} + + for line in io.popen("set"):lines() do + envName = line:match("^[^=]+") + osEnv[envName] = os.getenv(envName) + end + + return osEnv +end + +function filelist(dir) + local files = {} + for file in io.popen("ls -1 " .. dir .. "/*.txt"):lines() do + file = file:gsub(Config.aam_dir .. "/", "") + table.insert(files, file) + end + return files +end + +function file_exists(path) + local f = io.open(path, "r") + return f ~= nil and io.close(f) +end + +function file_size(path) + local f = io.open(path, "r") + local size = f:seek("end") + f:close() + return size +end + +function file_mtime(path) + local proc = io.popen("ls --full-time " .. path, "r") + local file_info = proc:read("*all") + proc:close() + -- -rw-r--r-- 1 root root 0 2023-09-29 00:32:22 -0400 foo + + local yr, mo, da, hr, mi, se, tz = file_info:match("[-rwx]+%s+%d+%s%w+%s+%w+%s+%d+%s(%d+)-(%d+)-(%d+)%s(%d+):(%d+):(%d+)%s([-+%d]+)%s") + local offset = (tz / 100) * 3600 + return os.time({year=yr, month=mo, day=da, hour=hr, min=mi, sec=se}) - offset +end + +function mkdir(path) + os.execute('mkdir ' .. path .. ' >/dev/null 2>&1') +end + +function unlink(path) + os.execute('rm ' .. path .. ' >/dev/null 2>&1') +end + +function chmod(path, mode) + os.execute('chmod ' .. mode .. ' ' .. path .. ' >/dev/null 2>&1') +end + +function rename(src, dest) + os.execute('mv ' .. src .. ' ' .. dest .. ' >/dev/null 2>&1') +end diff --git a/support/meshchatsync b/support/meshchatsync index b10631b..823f4b1 100755 --- a/support/meshchatsync +++ b/support/meshchatsync @@ -36,50 +36,49 @@ --]] package.path = package.path .. ";/www/cgi-bin/?.lua" -require("nixio") -require("meshchatconfig") -require("meshchatlib") +local config = require("meshchatconfig") +local lib = require("meshchatlib") local sync_status = {} local non_mesh_chat_nodes = {} local node = node_name() -if not nixio.fs.stat(meshchat_path) then - nixio.fs.mkdir(meshchat_path) - nixio.fs.mkdir(local_files_dir) +if not lib.file_exists(config.meshchat_path) then + mkdir(config.meshchat_path) + mkdir(config.local_files_dir) end -if not nixio.fs.stat(messages_db_file) then - io.open(messages_db_file, "w"):close() - nixio.fs.chmod(messages_db_file, "666") +if not lib.file_exists(config.messages_db_file) then + io.open(config.messages_db_file, "w"):close() + lib.chmod(config.messages_db_file, "666") end -io.open(local_users_status_file, "a"):close() -io.open(remote_users_status_file, "a"):close() +io.open(config.local_users_status_file, "a"):close() +io.open(config.config.remote_users_status_file, "a"):close() -save_messages_db_version() +lib.save_messages_db_version() -nixio.fs.chmod(meshchat_path, "666") +lib.chmod(config.meshchat_path, "666") -io.open(lock_file, "a"):close() +io.open(config.lock_file, "a"):close() function log_status() local cur_status = {} - if not nixio.fs.stat(sync_status_file) then - io.open(sync_status_file, "w"):close() + if not lib.file_exists(config.sync_status_file) then + io.open(config.sync_status_file, "w"):close() end - get_lock() + lib.get_lock() - for line in io.lines(sync_status_file) + for line in io.lines(config.sync_status_file) do local key, value = line:match("^(.*)\t(.*)$") cur_status[key] = value end - local f = io.open(sync_status_file, "w") + local f = io.open(config.sync_status_file, "w") if f then for key, value in pairs(sync_status) do @@ -94,28 +93,28 @@ function log_status() f:close() end - release_lock() + lib.release_lock() end function merge_messages() local rmsg = {} local lmsg = {} - for line in io.lines(meshchat_path .. "/remote_messages") + for line in io.lines(config.meshchat_path .. "/remote_messages") do local key = line:match("^(%S+)%s") rmsg[key] = line end - get_lock() + lib.get_lock() - for line in io.lines(messages_db_file) + for line in io.lines(config.messages_db_file) do local key = line:match("^(%S+)%s") lmsg[key] = line end - local f = io.open(messages_db_file, "a") + local f = io.open(config.messages_db_file, "a") if f then for rmsg_id, line in pairs(rmsg) do @@ -126,18 +125,18 @@ function merge_messages() f:close() end - sort_and_trim_db() + lib.sort_and_trim_db() - save_messages_db_version() + lib.save_messages_db_version() - release_lock() + lib.release_lock() end function merge_users() local rusers = {} local lusers = {} - for line in io.lines(meshchat_path .. "/remote_users") + for line in io.lines(config.meshchat_path .. "/remote_users") do local key, value = line:match("^(%S+\t%S+\t%S+)\t(.*)$") if not line:match("error") and key then @@ -145,9 +144,9 @@ function merge_users() end end - get_lock() + lib.get_lock() - for line in io.lines(remote_users_status_file) + for line in io.lines(config.remote_users_status_file) do local key, value = line:match("^(%S+\t%S+\t%S+)\t(.*)$") if not line:match("error") and key then @@ -155,7 +154,7 @@ function merge_users() end end - local f = io.open(remote_users_status_file, "w") + local f = io.open(config.remote_users_status_file, "w") if f then for key, _ in pairs(rusers) do @@ -174,7 +173,7 @@ function merge_users() f:close() end - release_lock() + lib.release_lock() end while true @@ -200,31 +199,31 @@ do port = ":8080" end - local version = get_messages_db_version() + local version = lib.get_messages_db_version() -- Poll non mesh chat nodes at a longer interval if non_mesh_chat_nodes[remote_node] and os.time() < non_mesh_chat_nodes[remote_node] then break end - nixio.fs.remove(meshchat_path .. "/remote_users") + lib.unlink(config.meshchat_path .. "/remote_users") -- Get remote users file - local f = io.popen("/usr/bin/curl --retry 0 --connect-timeout " .. connect_timeout .. " --speed-time " .. speed_time .. " --speed-limit " .. speed_limit .. " -sD - \"http://" .. remote_node .. port .. "/cgi-bin/meshchat?action=users_raw&platform=" .. platform .. "&node=" .. node .. "\" -o " .. meshchat_path .. "/remote_users 2>&1") + local f = io.popen("/usr/bin/curl --retry 0 --connect-timeout " .. config.connect_timeout .. " --speed-time " .. config.speed_time .. " --speed-limit " .. config.speed_limit .. " -sD - \"http://" .. remote_node .. port .. "/cgi-bin/meshchat?action=users_raw&platform=" .. platform .. "&node=" .. node .. "\" -o " .. config.meshchat_path .. "/remote_users 2>&1") local output = f:read("*a") f:close() -- Check if meshchat is installed if output:match("404 Not Found") then - non_mesh_chat_nodes[remote_node] = os.time() + non_meshchat_poll_interval + non_mesh_chat_nodes[remote_node] = os.time() + config.non_meshchat_poll_interval break end local md5 = output:match("Content%-MD5:%s([0-9a-f]+)\r\n") if md5 then - local f_md5 = file_md5(meshchat_path .. "/remote_users") + local f_md5 = file_md5(config.meshchat_path .. "/remote_users") if md5 == f_md5 then - local cur_size = nixio.fs.stat(meshchat_path .. "/remote_users").size + local cur_size = lib.file_exists(config.meshchat_path .. "/remote_users").size if cur_size > 0 then merge_users() end @@ -232,27 +231,27 @@ do end -- Get remote files file - nixio.fs.remove(meshchat_path .. "/remote_files") - f = io.popen("/usr/bin/curl --retry 0 --connect-timeout " .. connect_timeout .. " --speed-time " .. speed_time .. " --speed-limit " .. speed_limit .. " -sD - \"http://" .. remote_node .. port .. "/cgi-bin/meshchat?action=local_files_raw\" -o " .. meshchat_path .. "/remote_files 2>&1") + lib.unlink(config.meshchat_path .. "/remote_files") + f = io.popen("/usr/bin/curl --retry 0 --connect-timeout " .. config.connect_timeout .. " --speed-time " .. config.speed_time .. " --speed-limit " .. config.speed_limit .. " -sD - \"http://" .. remote_node .. port .. "/cgi-bin/meshchat?action=local_files_raw\" -o " .. config.meshchat_path .. "/remote_files 2>&1") output = f:read("*a") f:close() md5 = output:match("Content%-MD5:%s([0-9a-f]+)\r\n") if md5 then - local f_md5 = file_md5(meshchat_path .. "/remote_files") - nixio.fs.remove(meshchat_path .. "/remote_files." .. remote_node) + local f_md5 = file_md5(config.meshchat_path .. "/remote_files") + lib.unlink(config.meshchat_path .. "/remote_files." .. remote_node) if md5 == f_md5 then - local cur_size = nixio.fs.stat(meshchat_path .. "/remote_files").size + local cur_size = lib.file_exists(config.meshchat_path .. "/remote_files").size if cur_size > 0 then - nixio.fs.rename(meshchat_path .. "/remote_files", meshchat_path .. "/remote_files." .. remote_node) + lib.rename(config.meshchat_path .. "/remote_files", config.meshchat_path .. "/remote_files." .. remote_node) end end end -- Get remote messages - nixio.fs.remove(meshchat_path .. "/remote_messages") + lib.unlink(config.meshchat_path .. "/remote_messages") - f = io.popen("/usr/bin/curl --retry 0 --connect-timeout " .. connect_timeout .. " --speed-time " .. speed_time .. " --speed-limit " .. speed_limit .. " \"http://" .. remote_node .. port .. "/cgi-bin/meshchat?action=messages_version\" -o - 2> /dev/null") + f = io.popen("/usr/bin/curl --retry 0 --connect-timeout " .. config.connect_timeout .. " --speed-time " .. config.speed_time .. " --speed-limit " .. config.speed_limit .. " \"http://" .. remote_node .. port .. "/cgi-bin/meshchat?action=messages_version\" -o - 2> /dev/null") local remote_version = f:read("*a") f:close() @@ -263,16 +262,16 @@ do break end - f = io.popen("/usr/bin/curl --retry 0 --connect-timeout " .. connect_timeout .. " --speed-time " .. speed_time .. " --speed-limit " .. speed_limit .. " -sD - \"http://" .. remote_node .. port .. "/cgi-bin/meshchat?action=messages_raw\" -o " .. meshchat_path .. "/remote_messages 2>&1") + f = io.popen("/usr/bin/curl --retry 0 --connect-timeout " .. config.connect_timeout .. " --speed-time " .. config.speed_time .. " --speed-limit " .. config.speed_limit .. " -sD - \"http://" .. remote_node .. port .. "/cgi-bin/meshchat?action=messages_raw\" -o " .. config.meshchat_path .. "/remote_messages 2>&1") local output = f:read("*a") f:close() - if nixio.fs.stat(meshchat_path .. "/remote_messages") then + if lib.file_exists(config.meshchat_path .. "/remote_messages") then local md5 = output:match("Content%-MD5:%s([0-9a-f]+)\r\n") if md5 then - local f_md5 = file_md5(meshchat_path .. "/remote_messages") + local f_md5 = file_md5(config.meshchat_path .. "/remote_messages") if md5 == f_md5 then - local cur_size = nixio.fs.stat(meshchat_path .. "/remote_messages").size + local cur_size = lib.file_exists(config.meshchat_path .. "/remote_messages").size if cur_size > 0 then sync_status[remote_node] = os.time() merge_messages() @@ -285,9 +284,10 @@ do log_status() - nixio.fs.remove(meshchat_path .. "/remote_messages") - nixio.fs.remove(meshchat_path .. "/remote_users") - nixio.fs.remove(meshchat_path .. "/remote_files") + lib.unlink(config.meshchat_path .. "/remote_messages") + lib.unlink(config.meshchat_path .. "/remote_users") + lib.unlink(config.meshchat_path .. "/remote_files") - nixio.nanosleep(poll_interval, 0) + -- sleep for poll_interval seconds + os.execute('sleep ' .. config.poll_interval .. ' >/dev/null 2>&1') end