Skip to content

Instantly share code, notes, and snippets.

@Papouchcom
Last active December 15, 2020 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Papouchcom/69b0242f96c9a8bafe5225a78b523a56 to your computer and use it in GitHub Desktop.
Save Papouchcom/69b0242f96c9a8bafe5225a78b523a56 to your computer and use it in GitHub Desktop.
Clone of Prosody mod_post_msg allowing sending multicast messages/chat via JSON + PHP script example.
module:depends"http"
local jid_split = require "util.jid".split;
local jid_prep = require "util.jid".prep;
local msg = require "util.stanza".message;
local test_password = require "core.usermanager".test_password;
local b64_decode = require "util.encodings".base64.decode;
local json = require "util.json";
local function require_valid_user(f)
return function(event, path)
local request = event.request;
local response = event.response;
local headers = request.headers;
if not headers.authorization then
response.headers.www_authenticate = ("Basic realm=%q"):format(module.host.."/"..module.name);
return 401
end
local from_jid, password = b64_decode(headers.authorization:match"[^ ]*$"):match"([^:]*):(.*)";
from_jid = jid_prep(from_jid);
if from_jid and password then
local user, host = jid_split(from_jid);
local ok, err = test_password(user, host, password);
if ok and user and host then
module:log("debug", "Authed as %s", from_jid);
return f(event, path, from_jid);
elseif err then
module:log("debug", "User failed authentication: %s", err);
end
end
return 401
end
end
local function handle_post(event, path, authed_user)
local request = event.request;
local response = event.response;
local headers = request.headers;
local body_type = headers.content_type;
local to = jid_prep(path);
local message;
if body_type ~= "application/json" then return 415; end
local post_body = json.decode(request.body);
if not post_body then return 400; end
for jid in string.gmatch(post_body.to, "%S+") do
message = msg({ to = jid, from = post_body.from, type = post_body.type or "chat"}, post_body.body);
module:log("debug", "Sending %s", tostring(message));
module:send(message);
end
return 201;
end
module:provides("http", {
default_path = "/msg";
route = {
["POST /*"] = require_valid_user(handle_post);
OPTIONS = function(e)
local headers = e.response.headers;
headers.allow = "POST";
headers.accept = "application/json";
return 200;
end;
}
});
<?php
function SendJabber($from, $to, $msg) {
$message = $msg;
if(is_array($to)) {
$adr = implode(" ", $to);
} else {
$adr = $to;
};
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com:5281/msg/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "user@example.com" . ":" . "password");
$data = array("body" => "$message", "type" => "message", "to" => "$adr", "from" => "$from");
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
};
curl_close ($ch);
};
?>
@joshp23
Copy link

joshp23 commented May 10, 2019

what is the correct type to send to muc? I've been trying groupchat as indicated here, but no dice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment