Import urltitle plugin (thanks ruskie)

Tue, 31 Jan 2012 19:29:02 +0100

author
Kim Alvefur <zash@zash.se>
date
Tue, 31 Jan 2012 19:29:02 +0100
changeset 92
37e804dfaf37
parent 91
076caaedab2c
child 93
ab58062505e2

Import urltitle plugin (thanks ruskie)

plugins/urltitle.lua file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/urltitle.lua	Tue Jan 31 19:29:02 2012 +0100
@@ -0,0 +1,27 @@
+function riddim.plugins.urltitle(bot)
+	require "net.httpclient_listener";
+	local http = require "net.http";
+
+	local function handler(message)
+		local url = message.body and message.body:match("https?://%S+");
+		if url then
+			http.request(url, nil, function (data, code)
+				if code ~= 200 then return end
+				local title = data:match("<title[^>]*>([^<]+)");
+
+				if title then
+					title = title:gsub("\n", "");
+					if message.room then
+						message.room:send(title);
+					else
+						message:reply(title);
+					end
+				end
+			end);
+		end
+	end
+	bot:hook("message", handler);
+	bot:hook("groupchat/joined", function(room)
+		room:hook("message", handler)
+	end);
+end

mercurial