plugins/urltitle.lua

Tue, 21 Oct 2014 19:27:57 +0200

author
Kim Alvefur <zash@zash.se>
date
Tue, 21 Oct 2014 19:27:57 +0200
changeset 115
6498ca5ed831
parent 94
5cc3b481d3e7
permissions
-rw-r--r--

plugins.{trac,urltitle,xeps,xkcd,xkcd2,youtube}: Remove net.httpclient_listener, no longer required since verse:8e9f44ecb95b

function riddim.plugins.urltitle(bot)
	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_message(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