plugins/urltitle.lua

1
function riddim.plugins.urltitle(bot)
2
	local http = require "prosody.net.http";
3
 
4
	local function handler(message)
5
		local url = message.body and message.body:match("https?://%S+");
6
		if url then
7
			http.request(url, nil, function (data, code)
8
				if code ~= 200 then return end
9
				local title = data:match("<title[^>]*>([^<]+)");
10
 
11
				if title then
12
					title = title:gsub("\n", "");
13
					if message.room then
14
						message.room:send_message(title)
15
					else
16
						message:reply(title);
17
					end
18
				end
19
			end);
20
		end
21
	end
22
	bot:hook("message", handler);
23
	bot:hook("groupchat/joined", function(room)
24
		room:hook("message", handler)
25
	end);
26
end