Merge

Tue, 18 Aug 2015 15:22:52 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 18 Aug 2015 15:22:52 +0100
changeset 132
feafc98e8c78
parent 116
0735c0b39d4a (current diff)
parent 131
51fd36d01b27 (diff)
child 134
2c49724e0cca

Merge

--- a/plugins/factoids.lua	Wed May 13 13:23:33 2015 +0100
+++ b/plugins/factoids.lua	Tue Aug 18 15:22:52 2015 +0100
@@ -89,15 +89,10 @@
 		load = function()
 			local chunk, err = loadfile(factoids_file);
 			if chunk then
-				local old_mt = getmetatable(factoids);
-				setmetatable(factoids, {
-					__newindex = function (...) rawset(...); print(...) end
-				});
 				setfenv(chunk, {_G = factoids} );
 				chunk();
-				setmetatable(factoids, old_mt);
 			else
-				print(err);
+				bot.stream:err(tostring(err));
 			end
 		end,
 		save = function()
@@ -130,7 +125,6 @@
 	actions.load();
 	setmetatable(factoids, {
 		__newindex = function(t, k, v)
-			print("__newindex", t, k, v);
 			rawset(t, k, v);
 			actions.add(k, v);
 		end,
@@ -168,7 +162,6 @@
 	actions.load();
 	setmetatable(factoids, {
 		__newindex = function(t, k, v)
-			print("__newindex", t, k, v);
 			rawset(t, k, v);
 			actions.add(k, v);
 		end,
@@ -185,14 +178,9 @@
 		load = function()
 			bot.stream:private_get(factoids_node, factoids_xmlns, function (storage)
 				if storage then
-					local old_mt = getmetatable(factoids);
-					setmetatable(factoids, {
-						__newindex = function (...) rawset(...); print(...) end
-					});
 					for factoid in storage:children() do
 						factoids[factoid.attr.name or "something-invalid"] = factoid:get_text();
 					end
-					setmetatable(factoids, old_mt);
 				end
 			end);
 		end,
@@ -210,7 +198,6 @@
 		actions.load();
 		setmetatable(factoids, {
 			__newindex = function(t, k, v)
-				print("__newindex", t, k, v);
 				rawset(t, k, v);
 				actions.add(k, v);
 			end,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/github.lua	Tue Aug 18 15:22:52 2015 +0100
@@ -0,0 +1,53 @@
+local url = require"socket.url";
+local json = require"util.json";
+local http = require"net.http";
+
+function riddim.plugins.github(bot)
+	local conf = bot.config.github;
+	local base_url = url.parse("https://api.github.com/repos/x/y/issues/123");
+	local base_path = url.parse_path(base_url.path);
+
+	local ex = {
+		headers = {
+			Accept = "application/vnd.github.v3+json";
+		};
+	};
+
+	local function get_issue_url(conf, number)
+		base_path[2] = conf.user;
+		base_path[3] = conf.repo or conf.project;
+		base_path[5] = number;
+		base_url.path = url.build_path(base_path);
+		local url = url.build(base_url);
+		return url;
+	end
+
+	bot:hook("commands/issue", function (command)
+		local issue_id = tonumber(command.param);
+		if not issue_id then return; end
+		local current_conf = conf[command.room and command.room.jid] or conf;
+		if not current_conf.user then return end
+		assert(http.request(get_issue_url(current_conf, issue_id), ex, function (issue, code)
+			if code > 400 then
+				return command:reply("HTTP Error "..code.." :(");
+			end
+			issue = issue and json.decode(issue);
+			if not issue then
+				return command:reply("Got invalid JSON back :(");
+			end
+			command:reply(("%s #%d\n%s"):format(issue.title, issue.number, issue.html_url));
+		end));
+		return true;
+	end);
+
+	local function check_for_issue_id(message)
+		local issue_id = message.body and message.body:match"#(%d+)";
+		if issue_id then
+			return bot:event("commands/issue", { param = issue_id, reply = message.reply, });
+		end
+	end
+
+	bot:hook("groupchat/joining", function (room)
+		room:hook("message", check_for_issue_id);
+	end);
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/issues.lua	Tue Aug 18 15:22:52 2015 +0100
@@ -0,0 +1,65 @@
+-- Plugin for querying issue-tracker
+--
+-- Example config:
+-- issues = {
+--   ["project@conference.example.org"] = "http://example.org/issues/";
+-- }
+
+local url = require"socket.url";
+local json = require"util.json";
+local http = require"net.http";
+
+function riddim.plugins.issues(bot)
+	local conf = bot.config.issues;
+
+	local ex = {
+		headers = {
+			Accept = "application/json";
+		};
+	};
+
+	local function get_issue_url(base, id)
+		local base_url = url.parse(base or "http://localhost:8006/");
+		local base_path = url.parse_path(base_url.path);
+		base_path[#base_path+1] = "issue";
+		base_path[#base_path+1] = id;
+		base_path.is_directory = nil;
+		base_url.path = url.build_path(base_path);
+		return url.build(base_url);
+	end
+
+	bot:hook("commands/issue", function (command)
+		local issue_id = tonumber(command.param);
+		if not issue_id then return; end
+		local current_conf = conf[command.room and command.room.jid or "default"];
+		if not current_conf then return end
+		local issue_url = get_issue_url(current_conf, issue_id);
+		http.request(issue_url, ex, function (data, code)
+			if code > 400 then
+				return command:reply("HTTP Error "..code.." :(");
+			end
+			data = data and json.decode(data);
+			if not data then
+				return command:reply("Got invalid JSON back :(");
+			end
+			local issue = data.issue;
+			local tags = {};
+			for tag in pairs(issue.tags) do
+				table.insert(tags, tag);
+			end
+			command:reply(("Issue #%d %q {%s}\n%s"):format(issue.id, issue.title, table.concat(tags, ", "), issue_url));
+		end);
+		return true;
+	end);
+
+	local function check_for_issue_id(message)
+		local issue_id = message.body and message.body:match"#(%d+)";
+		if issue_id then
+			return bot:event("commands/issue", { param = issue_id, reply = message.reply, room = message.room });
+		end
+	end
+
+	bot:hook("groupchat/joining", function (room)
+		room:hook("message", check_for_issue_id);
+	end);
+end
--- a/plugins/version.lua	Wed May 13 13:23:33 2015 +0100
+++ b/plugins/version.lua	Tue Aug 18 15:22:52 2015 +0100
@@ -2,6 +2,12 @@
 
 local xmlns_version = "jabber:iq:version";
 
+local friendly_errors = {
+	["service-unavailable"] = " doesn't reply to version requests";
+	["feature-not-implemented"] = " doesn't support version requests";
+	["remote-server-not-found"] = " can't be reached via XMPP";
+}
+
 function riddim.plugins.version(bot)
 	bot.stream:add_plugin("version");
 	bot.stream.version:set{
@@ -20,20 +26,22 @@
 		bot.stream:query_version(who, function (reply)
 			if not reply.error then
 				local saywho = (who == command.sender.jid and "You are") or (param and param.." is" or "I am");
-				command:reply(saywho.." running "..(reply.name or "something")
-					.." version "..(reply.version or "unknown")
-					.." on "..(reply.platform or "an unknown platform"));
+				local isrunning = saywho.." running "..(reply.name or "something");
+				if reply.version then
+					isrunning = isrunning .." version "..reply.version;
+				end
+				if reply.platform then
+					isrunning = isrunning .." on "..reply.platform;
+				end
+				command:reply(isrunning);
 			else
 				local type, condition, text = reply.type, reply.condition, reply.text;
 				local r = "There was an error requesting "..param.."'s version";
-				if condition == "service-unavailable" then
-					r = param.." doesn't reply to version requests";
-				elseif condition == "feature-not-implemented" then
-					r = param.." doesn't support version requests";
-				elseif condition == "remote-server-not-found" then
-					r = param.." can't be reached via XMPP";
+				local friendly_error = friendly_errors[condition];
+				if friendly_error then
+					r = r .. friendly_error;
 				elseif condition and not text then
-					r = r..": "..condition;
+					r = r..": "..(condition):gsub("%-", "  ");
 				end
 				if text then
 					r = r .. " ("..text..")";
--- a/plugins/xkcd2.lua	Wed May 13 13:23:33 2015 +0100
+++ b/plugins/xkcd2.lua	Tue Aug 18 15:22:52 2015 +0100
@@ -17,11 +17,11 @@
 		local strip;
 
 		if q then
-			local t, num = q:match("^([#\"]?)(%d+)\"?$");
+			local t, num = q:match("^([#\"]?)(%-?%d+)\"?$");
 			if t ~= '"' then
 				num = tonumber(num);
 				if num and num < 0 then
-					num = #strip + num;
+					num = table.maxn(strips) + num;
 				end
 			end
 			strip = strips[num or q:lower()];
@@ -172,4 +172,5 @@
 		return "List of strips emptied";
 	end);
 
+	do_load();
 end
--- a/squishy	Wed May 13 13:23:33 2015 +0100
+++ b/squishy	Tue Aug 18 15:22:52 2015 +0100
@@ -1,11 +1,37 @@
-Module "riddim.plugins.autosubscribe"  "plugins/autosubscribe.lua"
-Module "riddim.plugins.groupchat"      "plugins/groupchat.lua"
-Module "riddim.plugins.commands"       "plugins/commands.lua"
-Module "riddim.plugins.version"        "plugins/version.lua"
-Module "riddim.plugins.ping"           "plugins/ping.lua"
-Module "riddim.plugins.tell"           "plugins/tell.lua"
-Module "riddim.plugins.uptime"         "plugins/uptime.lua"
-Module "riddim.plugins.resolvejid"     "plugins/resolvejid.lua"
+-- Riddim plugins
+plugins = {
+	"autosubscribe",
+	"groupchat",
+	"commands",
+	"version",
+	"ping",
+	"tell",
+	"uptime",
+	"resolvejid",
+	"bookmarks",
+	"bugzilla",
+	"disco",
+	"factoids",
+	"ietf",
+	"invited",
+	"msgforward",
+	"opdown",
+	"pubsub2room",
+	"simple_commands",
+	"slap",
+	"topic",
+	"trac",
+	"github",
+	"urltitle",
+	"xeps",
+	"xkcd",
+	"xkcd2",
+	"youtube",
+}
+
+for _, plugin in ipairs(plugins) do
+	Module("riddim.plugins."..plugin)("plugins/"..plugin..".lua")
+end
 
 Main "init.lua"
 

mercurial