summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Smith <git@kismith.co.uk>2011-02-27 22:45:32 (GMT)
committerKevin Smith <git@kismith.co.uk>2011-03-06 20:25:01 (GMT)
commit27d21b371f24272466a2d6a5bf2e2b717ee2d9fc (patch)
tree5f53281711d4f467933e4b3315241e4eee58a64c /Swiftob/scripts
parentd9c9df3b4ae5432552417fc4db74d62ab34f066d (diff)
downloadswift-contrib-27d21b371f24272466a2d6a5bf2e2b717ee2d9fc.zip
swift-contrib-27d21b371f24272466a2d6a5bf2e2b717ee2d9fc.tar.bz2
A start on Swiftob, a Swiften-based chatbot.
Diffstat (limited to 'Swiftob/scripts')
-rw-r--r--Swiftob/scripts/agenda.lua94
-rw-r--r--Swiftob/scripts/echo.lua5
-rw-r--r--Swiftob/scripts/eval.lua14
-rw-r--r--Swiftob/scripts/version.lua32
4 files changed, 145 insertions, 0 deletions
diff --git a/Swiftob/scripts/agenda.lua b/Swiftob/scripts/agenda.lua
new file mode 100644
index 0000000..897b89c
--- /dev/null
+++ b/Swiftob/scripts/agenda.lua
@@ -0,0 +1,94 @@
+agendas = {}
+currents = {}
+
+function full_agenda(from)
+ fullagenda = {}
+ fullagenda[1] = "Roll call"
+ fullagenda[2] = "Agenda bashing"
+ for i, v in ipairs(agendas[from]) do
+ table.insert(fullagenda, v)
+ end
+ table.insert(fullagenda, "Date of next meeting")
+ table.insert(fullagenda, "Any other business")
+ return fullagenda
+end
+
+function agenda_full_command(command, params, message)
+ from = message['frombare']
+ ensure_loaded(from)
+ agenda = agendas[from]
+ fullagenda = full_agenda(from)
+ reply = ""
+ for i, v in ipairs(fullagenda) do
+ reply = reply..i..") "..v.."\n"
+ end
+ reply = reply.."Fini"
+ swiftob_reply_to(message, reply)
+end
+
+function agenda_append_command(command, params, message)
+ from = message['frombare']
+ agenda_append(from, params)
+ agenda_save(from)
+ swiftob_reply_to(message, "Done.")
+end
+
+function agenda_up_command(command, params, message)
+ from = message['frombare']
+ ensure_loaded(from)
+ up = tonumber(params)
+ if up == nil then up = 1 end
+ currents[from] = currents[from] + up
+ if currents[from] <= 0 then currents[from] = 1 end
+ item = full_agenda(from)[currents[from]]
+ if item == nil then item = "Fini." end
+ reply = currents[from]..") "..item
+ swiftob_reply_to(message, reply)
+end
+
+
+function agenda_clear_command(command, params, message)
+ from = message['frombare']
+ agendas[from] = {}
+ agenda_save(from)
+ swiftob_reply_to(message, "Done.")
+end
+
+function agenda_save(from)
+ agenda = agendas[from]
+ swiftob_store_setting("count@@@"..from, #agenda)
+ for i, v in ipairs(agenda) do
+ swiftob_store_setting(i.."@@@"..from, v)
+ end
+end
+
+function ensure_loaded(from)
+ if agendas[from] == nil then
+ agenda_load(from)
+ end
+end
+
+function agenda_load(from)
+ agendas[from] = {}
+ currents[from] = 0
+ num_items = tonumber(swiftob_get_setting("count@@@"..from))
+ if num_items == nil then num_items = 0 end
+ for i = 1, num_items do
+ agenda_append(from, swiftob_get_setting(i.."@@@"..from))
+ end
+end
+
+function agenda_append(from, item)
+ ensure_loaded(from)
+ agenda = agendas[from]
+ table.insert(agenda, item)
+ agendas[from] = agenda
+end
+
+swiftob_register_command("agenda", "Anyone", "print the full agenda", agenda_full_command)
+swiftob_register_command("agendaappend", "Owner", "append an item to the agenda", agenda_append_command)
+swiftob_register_command("agendaclear", "Owner", "clear the agenda", agenda_clear_command)
+swiftob_register_command("agendaup", "Owner", "Moves the current counter by n, and returns the current agenda item", agenda_up_command)
+
+
+
diff --git a/Swiftob/scripts/echo.lua b/Swiftob/scripts/echo.lua
new file mode 100644
index 0000000..7adc2b3
--- /dev/null
+++ b/Swiftob/scripts/echo.lua
@@ -0,0 +1,5 @@
+function echo_message(command, params, message)
+ swiftob_reply_to(message, params)
+end
+
+swiftob_register_command("echo", "Anyone", "What did you say?", echo_message) \ No newline at end of file
diff --git a/Swiftob/scripts/eval.lua b/Swiftob/scripts/eval.lua
new file mode 100644
index 0000000..c9840dc
--- /dev/null
+++ b/Swiftob/scripts/eval.lua
@@ -0,0 +1,14 @@
+
+function eval_command(command, params, message)
+ assert(loadstring(params))()
+ swiftob_reply_to(message, "done")
+end
+
+function evalr_command(command, params, message)
+ result = assert(loadstring(params))()
+ swiftob_reply_to(message, "" .. result)
+end
+
+swiftob_register_command("eval", "Owner", "Evaluate an expression", eval_command)
+swiftob_register_command("evalr", "Owner", "Evaluate an expression and return the result", evalr_command)
+
diff --git a/Swiftob/scripts/version.lua b/Swiftob/scripts/version.lua
new file mode 100644
index 0000000..40b4e4d
--- /dev/null
+++ b/Swiftob/scripts/version.lua
@@ -0,0 +1,32 @@
+function friendly_version(version)
+ result = version['name']
+ if version['version'] ~= nil and version['version'] ~= "" then
+ result = result.." version "..version['version']
+ end
+ if version['os'] ~= nil and version['os'] ~= "" then
+ result = result .." on "..version['os']
+ end
+ return result
+end
+
+function version_command(command, params, message)
+ jid = swiftob_muc_input_to_jid(params, message['from'])
+ if jid == nil then
+
+ else
+ swiftob_get_software_version({
+ to=jid,
+ timeout=10,
+ success_callback=function(result)
+ swiftob_reply_to(message, params.." is running "..friendly_version(result))
+ end,
+ failure_callback=function(error)
+ swiftob_reply_to(message, "Error getting version from "..params..": "..error)
+ end,
+ timeout_callback=function()
+ swiftob_reply_to(message, "Timeout waiting for version from "..params)
+ end})
+ end
+end
+
+swiftob_register_command("version", "Anyone", "Ask for someone's version", version_command) \ No newline at end of file