Skip to content

Instantly share code, notes, and snippets.

@andsve
Created December 20, 2010 01:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andsve/747913 to your computer and use it in GitHub Desktop.
Save andsve/747913 to your computer and use it in GitHub Desktop.
module("modules/imgcache/imgcache")
function get_image(url)
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
-- connect somewhere
local c = socket.try(socket.connect(host, 80))
c:settimeout(3)
local try = socket.newtry(function() print("connection failed"); c:close() end)
try(c:send("GET " .. tostring(path) .. " HTTP/1.1\r\nHost: " .. tostring(host) .. "\r\nReferer: " .. tostring(url) .. "\r\n\r\n"))
local content = ""
local answer = ""
local get_mode = "*l"
local recv_type = "header"
local content_length = nil
local content_type = "png"
repeat
answer = try(c:receive(get_mode))
if (not answer) then
break
end
--print(recv_type .. ": " .. tostring(answer))
-- recieving content
if (recv_type == "content") then
content = answer
if (get_mode ~= "*l") then
break
end
else
-- recieving header
-- Redirected?
local i,j,l = string.find(answer, "Location: (.*)")
if (i) then
--print("Redirected to: " .. tostring(l))
content = get_content(l)
break
end
-- Is this an image?
local i,j,t = string.find(answer, "Content%-Type: (.*)")
if (i) then
local i,j,t = string.find(t, "image/(.*)")
if (not i) then
print("Not an image!")
break
else
print("Image type: " .. tostring(t))
if (t == "jpeg") then
content_type = "jpg"
else
content_type = t
end
end
end
end
-- see if the header has ended
if (answer == "") then
if (content_length) then
get_mode = content_length
end
recv_type = "content"
end
-- find content length
local i,j,l = string.find(answer, "Content%-Length: (.*)")
if (i) then
content_length = tonumber(l)
if (content_length) <= 0 then
print("get_content Error: Invalid content length.")
break
end
end
until (not answer)
c:close()
return content, content_type
end
function parse_message(bot, msg)
local i,j,chan,s = string.find(msg, ":.-!.- PRIVMSG (.-) :(.+)")
if not (i == nil) then
local output_dir = "/home/sweetfish/www.md5/cache"
-- see if we have a url in this message
local i,j,url = string.find(s, "([%S]+://[%S]+)")
if not (i == nil) then
-- see if this is an image
local content, content_type = get_image(url)
if (content) then
local new_filename = tostring(os.time()) .. tostring(content_type)
local tmpfile = io.open(tostring(output_dir) .. "/" .. tostring(new_filename), "w+")
tmpfile:write(content)
tmpfile:close()
end
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 http = require("socket.http")
local b, c, h = http.request(last_url)
if not (b == nil) then
b = string.gsub(b, "<[^%s>]+", string.lower)
local i,j,title = string.find(b, "<title>(.+)</title>")
if not (i == nil) then
bot:say(chan, tostring(title))
else
bot:say(chan, "No title found for url: " .. tostring(last_url))
end
else
bot:say(chan, "Could not request 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