mod_post_msg/mod_post_msg.lua
changeset 1302 e556219cb43d
parent 661 a6c8f252e5fa
child 1417 9b6fbababb8c
equal deleted inserted replaced
1301:6e4ebdc9b46b 1302:e556219cb43d
     4 local jid_prep = require "util.jid".prep;
     4 local jid_prep = require "util.jid".prep;
     5 local msg = require "util.stanza".message;
     5 local msg = require "util.stanza".message;
     6 local test_password = require "core.usermanager".test_password;
     6 local test_password = require "core.usermanager".test_password;
     7 local b64_decode = require "util.encodings".base64.decode;
     7 local b64_decode = require "util.encodings".base64.decode;
     8 local formdecode = require "net.http".formdecode;
     8 local formdecode = require "net.http".formdecode;
       
     9 local xml = require"util.xml";
     9 
    10 
    10 local function require_valid_user(f)
    11 local function require_valid_user(f)
    11 	return function(event, path)
    12 	return function(event, path)
    12 		local request = event.request;
    13 		local request = event.request;
    13 		local response = event.response;
    14 		local response = event.response;
    44 		if to and request.body then
    45 		if to and request.body then
    45 			message = msg({ to = to, from = authed_user, type = "chat"},request.body);
    46 			message = msg({ to = to, from = authed_user, type = "chat"},request.body);
    46 		end
    47 		end
    47 	elseif body_type == "application/x-www-form-urlencoded" then
    48 	elseif body_type == "application/x-www-form-urlencoded" then
    48 		local post_body = formdecode(request.body);
    49 		local post_body = formdecode(request.body);
    49 			message = msg({ to = post_body.to or to, from = authed_user,
    50 		message = msg({ to = post_body.to or to, from = authed_user,
    50 				type = post_body.type or "chat"}, post_body.body);
    51 				type = post_body.type or "chat"}, post_body.body);
       
    52 		if post_body.html then
       
    53 		   local html, err = xml.parse(post_body.html);
       
    54 		   if not html then
       
    55 		      module:log("warn", "mod_post_msg: invalid XML: %s", err);
       
    56 		      return 400;
       
    57 		   end
       
    58 		   message:tag("html", {xmlns="http://jabber.org/protocol/xhtml-im"}):add_child(html):up();
       
    59 		end
    51 	else
    60 	else
    52 		return 415;
    61 		return 415;
    53 	end
    62 	end
    54 	if message and message.attr.to then
    63 	if message and message.attr.to then
    55 		module:log("debug", "Sending %s", tostring(message));
    64 		module:log("debug", "Sending %s", tostring(message));