User Tools

Site Tools


action_write
write.lua
--
-- Copyright (C) 2018 CurlyMo & Niek
--
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
--
 
local M = {}
 
local function split(s, delimiter)
    local result = {}
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match)
    end
    return result
end
 
function M.check(parameters)
if parameters['TEXT'] == nil then
		error("write action is missing a \"TEXT\" statement");
	end
 
	if parameters['TO'] == nil then
		error("write action is missing a \"TO\" statement");
	end
 
	if parameters['MODE'] == nil then
		error("write action is missing a \"MODE\" statement");
	end
 
	if #parameters['TEXT']['value'] ~= 1 or parameters['TEXT']['value'][2] ~= nil then
		error("write action \"TEXT\" only takes one argument");
	end
 
	if #parameters['TO']['value'] ~= 1 or parameters['TO']['value'][2] ~= nil then
		error("write action \"TO\" only takes one argument");
	end
 
	if #parameters['MODE']['value'] ~= 1 or parameters['MODE']['value'][2] ~= nil then
		error("write action \"MODE\" only takes one argument");
	end
 
	local mode = parameters['MODE']['value'][1]
	if mode ~= "new" and mode ~= "append" then
		error("write action \"MODE\" must be \"new\" or \"append\", \"".. mode .. "\" given");
	end
 
	local text = parameters['TEXT']['value'][1];
	local path = parameters['TO']['value'][1];
 
	local parts = split(path, "/")
 
	if parts[1] ~= "" then
		error("write action requires an absolute path, \"" .. path .."\" given");
	end
 
	for i=2, #parts - 1 do
		if parts[i] == "" then
			error("write action path \"" .. path .."\" is invalid");
		end
	end
 
	local filename = parts[#parts];
	if filename == "" or #parts < 3 then
		error("write action filename is missing in path \"" .. path .."\"");
	end
 
	local folder = string.sub(path, 1, string.len(path) - string.len(filename));
	--pseudo random filename to test write access
	local dummyfile = folder .. "_WRITE_ACTION_" .. tostring(pilight.common.random(1000000, 9999999)) .. ".tmp";
 
	local fh, errmsg = io.open(dummyfile, "w");
	if fh ~= nil then
		io.close(fh);
		os.remove(dummyfile)
	else
		local array = pilight.common.explode(errmsg, ": ");
		error("write action unable to write to folder \"" .. folder .. "\" (" .. array[2]  .. ")");		
	end
 
	return 1;
end
 
function M.thread(thread)
	local data = thread.getUserdata();
	local fh = io.open(data['path'], data['mode']);
	if fh ~= nil then
		io.output(fh);
		io.write(data['text'], "\n");
		io.close(fh);
	end
end
 
function M.run(parameters)
	local async = pilight.async.thread();
	local data = async.getUserdata();
	data['path'] = parameters['TO']['value'][1];
	data['text'] = parameters['TEXT']['value'][1];
	data['mode'] = "a";
	if parameters['MODE']['value'][1] == "new" then 
		data['mode'] = "w";
	end
	async.setCallback("thread");
	async.trigger();
 
return 1;
end
 
function M.parameters()
	return "TEXT", "TO", "MODE";
end
 
function M.info()
	return {
		name = "write",
		version = "1.0",
		reqversion = "8.1.1",
		reqcommit = "0"
	}
end
 
return M;
action_write.txt · Last modified: 2018/08/07 11:23 by Niek