From d18e0ae6c767758b17d2bd26fca3cee953b35fdf Mon Sep 17 00:00:00 2001 From: yago Date: Wed, 27 Aug 2014 15:50:35 +0200 Subject: [PATCH] Check for users image url (jpg and png) --- bot/bot.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/bot/bot.lua b/bot/bot.lua index 4ff106a..7012367 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -5,7 +5,6 @@ VERSION = 'v0.1' function on_msg_receive (msg) -- vardump(msg) - -- mark_read(get_receiver(msg)) if msg.out then return end @@ -23,9 +22,38 @@ function on_msg_receive (msg) msg.text = msg.text:sub(2,-1) do_action(msg) end + +-- This is very unestable ... + if is_image_url(msg.text) then + last = string.get_last_word(msg.text) + last = last:match("[%w_:/.%%&-]+") -- Lets sanitize! + extension = string.get_extension_from_filename(last) + + file_name = string.random(5) + file = "/tmp/"..file_name.."."..extension + sh = "curl --insecure -o '"..file.."' "..last + run_bash(sh) + send_photo(get_receiver(msg), file) + end + + mark_read(get_receiver(msg)) + -- write_log_file(msg) end +function is_image_url(text) + print ('IS image ' .. text ..'?') + last = string.get_last_word(text) + print ('Last is: ' .. last) + extension = string.get_extension_from_filename(last) + if extension == 'jpg' or extension == 'png' then + print('Is image :D') + return true + end + print 'Not image D:' + return false +end + -- Where magic happens function do_action(msg) receiver = get_receiver(msg) @@ -241,7 +269,6 @@ function get_9GAG() if link_image:sub(0,2) == '//' then link_image = msg.text:sub(3,-1) end - print("9gag image"..link_image) return link_image, title end @@ -287,6 +314,25 @@ function string.random(length) return str; end +function string.get_extension_from_filename( filename ) + return filename:match( "%.([^%.]+)$" ) +end + +function string.get_last_word( words ) + print ('Last word of: ' .. words) + local splitted = split_by_space ( words ) + return splitted[#splitted] +end + +function split_by_space ( text ) + print ('Split: ' .. text) + words = {} + for word in string.gmatch(text, "[^%s]+") do + table.insert(words, word) + end + return words +end + function vardump(value, depth, key) local linePrefix = "" local spaces = ""