diff mod_post_msg/mod_post_msg.lua @ 2989:926aaaeb0d21

mod_post_msg: Add support for a JSON based format similar to what mod_component_http uses
author Kim Alvefur <zash@zash.se>
date Wed, 04 Apr 2018 15:55:09 +0200
parents a0375f84d65a
children
line wrap: on
line diff
--- a/mod_post_msg/mod_post_msg.lua	Wed Apr 04 15:54:30 2018 +0200
+++ b/mod_post_msg/mod_post_msg.lua	Wed Apr 04 15:55:09 2018 +0200
@@ -7,6 +7,7 @@
 local b64_decode = require "util.encodings".base64.decode;
 local formdecode = require "net.http".formdecode;
 local xml = require"util.xml";
+local json = require "util.json";
 
 local function require_valid_user(f)
 	return function(event, path)
@@ -57,6 +58,11 @@
 			end
 			message:tag("html", {xmlns="http://jabber.org/protocol/xhtml-im"}):add_child(html):up();
 		end
+	elseif body_type == "application/json" then
+		local post_body = json.decode(request.body);
+		if not post_body then return 400; end
+		message = msg({ to = post_body.to or to, from = authed_user,
+		                type = post_body.type or "chat"}, post_body.body);
 	else
 		return 415;
 	end