plugins/urltitle.lua

Fri, 07 Aug 2015 21:44:21 +0200

author
Kim Alvefur <zash@zash.se>
date
Fri, 07 Aug 2015 21:44:21 +0200
changeset 127
6c977fbf6d0b
parent 115
6498ca5ed831
permissions
-rw-r--r--

plugins.github: Don't check for #issuenumber in direct messages

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