Clone of Prosody mod_post_msg allowing sending multicast messages/chat via JSON + PHP script example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
}; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is the correct
type
to send to muc? I've been tryinggroupchat
as indicated here, but no dice.