Examples updates (shortenurl)
* some updates to marking (freezes now)
* added defunct readonly
* added shortenulr
-- SHORTENURL
-- uses:
-- * goo.gl (online service)
-- * curl (program)
-- * lua-json (https://github.com/harningt/luajson/)
-- * shell_escape (mcabberrc.lua)
-- * option lua_shorten_post_url
-- TODO:
-- * other backends
-- * detect curl/wget
-- * ensure curl will not mess screen up
-- * -s switch to send url automatically
local json = require 'json'
local encodeoptions = {
strings = {
encodeSet = '\\"%z\1-\031', -- do not escape /, google does not understand that
}
}
local curlcommand = "curl -s https://www.googleapis.com/urlshortener/v1/url -H 'Content-type: application/json' -d %s"
main.command ( 'shorten',
function ( args )
if ( not args ) or not args:match ( '%w' ) then
print ( 'shorten: You must specify url' )
return
end
local jid = main.current_buddy ()
local replystring = ''
main.bgread ( ( curlcommand ):format ( shell_escape ( json.encode ( { longUrl = args }, encodeoptions ) ) ),
function ( data )
if not data then
-- eof
print ( 'lua reply: ' .. replystring )
local reply = json.decode ( replystring )
if reply and reply.id then
if jid and main.yesno ( main.option ( 'lua_shorten_post_url' ) ) then
main.run ( ('say_to -q %s %s'):format ( jid, reply.id ) )
else
print ( ('Shortened url: %s'):format ( reply.id ) )
end
else
-- XXX extract message from json?
print ( ('Failed to shorten url: %s'):format ( replystring ) )
end
return false
else
replystring = replystring .. data
return true
end
end )
end )
-- vim: se ts=4 sw=4: --