Created
August 26, 2010 15:00
-
-
Save andsve/551547 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module("modules/title/title") | |
function parse_message(bot, msg) | |
local i,j,chan,s = string.find(msg, ":.-!.- PRIVMSG (.-) :(.+)") | |
if not (i == nil) then | |
function get_title(url, block_hosts, loop_control) | |
if (loop_control <= 0) then | |
return nil | |
end | |
local socket = require("socket") | |
-- get host and path | |
local socketurl = require("socket.url") | |
local parsedurl = socketurl.parse(url) | |
local host = parsedurl.host | |
local path = parsedurl.path | |
local query = parsedurl.query | |
if query then | |
path = path .. "?" .. query | |
end | |
-- connect somewhere | |
local c = socket.try(socket.connect(host, 80)) | |
c:settimeout(10) | |
local try = socket.newtry(function() print("connection failed"); c:close() end) | |
local send_data = "GET " .. tostring(path) .. " HTTP/1.1\r\nHost: " .. tostring(host) .. "\r\nReferer: " .. tostring(url) .. "\r\n\r\n" | |
try(c:send(send_data)) | |
local answer = "" | |
local recv_type = "header" | |
local content_length = nil | |
local title_content = "" | |
local unfinished_title = false | |
repeat | |
answer = try(c:receive("*l")) | |
if (not answer) then | |
break | |
end | |
--print(recv_type .. ": " .. tostring(answer)) | |
-- recieving content | |
if (recv_type == "content") then | |
-- TODO: see if we got full title | |
if (unfinished_title) then | |
-- "unfinished" title, keep looking for end tag | |
local i,j = string.find(string.lower(answer), "</title>") | |
if (i) then | |
title_content = title_content .. string.sub(answer, 1, i-1) | |
break | |
else | |
title_content = title_content .. answer | |
end | |
else | |
local i,j,t = string.find(string.lower(answer), "<title>(.*)") | |
if (i) then | |
unfinished_title = true | |
local i2,j2 = string.find(string.lower(answer), "</title>") | |
if (i2) then | |
title_content = string.sub(answer, i+7, i2-1) | |
break | |
else | |
title_content = string.sub(answer, i, #t) | |
end | |
end | |
end | |
else | |
-- recieving header | |
-- Redirected? | |
local i,j,l = string.find(answer, "Location: (.*)") | |
if (i) then | |
--print("Redirected to: " .. tostring(l)) | |
title_content = get_title(l, block_hosts, loop_control - 1) | |
break | |
end | |
-- Is this NOT a html page? | |
local i,j,t = string.find(answer, "Content%-Type: (.*)") | |
if (i) then | |
local i,j,t = string.find(t, "text/html") | |
if (not i) then | |
print("Not a webpage!") | |
return nil | |
end | |
end | |
end | |
-- see if the header has ended | |
if (answer == "") then | |
recv_type = "content" | |
end | |
until (not answer) | |
c:close() | |
title_content = string.gsub(title_content, "%s+", " ") | |
title_content = string.gsub(title_content, "^%s+", "") -- trim start | |
title_content = string.gsub(title_content, "%s+$", "") -- trim end | |
return title_content | |
end | |
-- see if we have a url in this message | |
local i,j,url = string.find(s, "([%S]+://[%S]+)") | |
if not (i == nil) then | |
-- save url | |
last_url = url | |
end | |
-- see if last message was a title-trigger | |
local i,j,sender,chan = string.find(msg, ":(.-)!.- PRIVMSG (.-) :" .. tostring(bot.config.triggerprefix) .. "title") | |
if not (i == nil) then | |
if not (string.sub(chan, 1, 1) == "#") then | |
chan = sender | |
end | |
local url_title = get_title(last_url, nil, 5) | |
if (url_title) then | |
bot:say(chan, tostring(url_title)) | |
else | |
bot:say(chan, "No title found for url: " .. tostring(last_url)) | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment