Posted by
| Nick Gammon
Australia (23,100 posts) Bio
Forum Administrator |
Message
| There have been problems in the past with doing the internal AddTrigger script function because it does not support all of the trigger options. The method below lets you add a trigger, timer or alias using ImportXML, and supports all available options.
html_replacements = {
["<"] = "<",
[">"] = ">",
["&"] = "&",
['"'] = """,
}
-- fix text so that < > & and double-quote are escaped
function fixhtml (s)
return (string.gsub (tostring (s), '[<>&"]',
function (str)
return html_replacements [str] or str
end ))
end -- fixhtml
function GeneralAdd (t, which, plural)
assert (type (t) == "table", "Table must be supplied to add a " .. which)
local k, v
local xml = {}
local send = fixhtml (t.send or "") -- send is done differently
t.send = nil
-- turn into XML options
for k, v in pairs (t) do
-- fix true/false to y/n
if v == true then
v = "y"
elseif v == false then
v = "n"
end -- if true or false
table.insert (xml, k .. '="' .. fixhtml (v) .. '"')
end -- for loop
assert (ImportXML (string.format (
"<%s><%s %s ><send>%s</send></%s></%s>",
plural, -- eg. triggers
which, -- eg. trigger
table.concat (xml, "\n"), -- eg. match="nick"
send, -- eg. "go north"
which, -- eg. trigger
plural) -- eg. triggers
) == 1, "Import of " .. which .. " failed")
end -- GeneralAdd
function LuaAddTrigger (t)
GeneralAdd (t, "trigger", "triggers")
end -- LuaAddTrigger
function LuaAddAlias (t)
GeneralAdd (t, "alias", "aliases")
end -- LuaAddAlias
function LuaAddTimer (t)
GeneralAdd (t, "timer", "timers")
end -- LuaAddTimer
function LuaAddMacro (t)
GeneralAdd (t, "macro", "macros")
end -- LuaAddMacro
What this does is let you do an LuaAddTrigger (alias or timer) by supplying a table of all the keywords you want to set. The others will take their default values (of zero, false, or an empty string) depending on their type.
These examples below show the general idea:
LuaAddTrigger { match = "swordfish",
regexp = true,
['repeat'] = true, -- repeat is lua keyword
send = "hi there",
sequence = 50,
enabled = true,
}
LuaAddAlias { match = "test",
send = "sing",
group = "test group"
}
LuaAddTimer { name = "mytimer",
minute = 5,
second = 3,
send "drink water"
}
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|