-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION

do

local BASE_URL = 'https://graph.facebook.com'

function get_fb_data (fb_code)
  local access_token = cred_data.fb_access_token
  local url = BASE_URL..'/'..fb_code..'?access_token='..access_token..'&locale=de_DE'
  local res,code  = https.request(url)
  if code ~= 200 then return "HTTP-FEHLER" end
  local data = json:decode(res)
  return data
end

function send_fb_data(data, receiver)
  local from = data.from.name
  local message = data.message
  local name = data.name
  local link = data.link
  local text = from..' hat gepostet:\n'..message..'\n'..name..' '..link
  send_msg(receiver, text, ok_cb, false)
end

function run(msg, matches)
  local fb_code = matches[2]
  local data = get_fb_data(fb_code)
  local receiver = get_receiver(msg)
  send_fb_data(data, receiver)
end

return {
  description = "Sendet Facebook-Post.", 
  usage = {"Link zu einem Facebook-Post"},
  patterns = {"facebook.com/([A-Za-z0-9-_-]+)/posts/([0-9-]+)","facebook.com/permalink.([a-z-]+)%?story_fbid=([0-9-]+)"},
  run = run 
}

end
