# HG changeset patch # User Matthew Wild # Date 1425054348 0 # Node ID 43c54a27bab2e9aadc9fef0d21eec57846192b5f # Parent c56baec031e8e571993dc378a1fb1c1f0e04df27 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST diff -r c56baec031e8 -r 43c54a27bab2 mod_pubsub_post/mod_pubsub_post.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_pubsub_post/mod_pubsub_post.lua Fri Feb 27 16:25:48 2015 +0000 @@ -0,0 +1,37 @@ +module:depends("http"); + +local st = require "util.stanza"; +local json = require "util.json"; +local formdecode = require "net.http".formdecode; +local uuid_generate = require "util.uuid".generate; +local timestamp_generate = require "util.datetime".datetime; + +local pubsub_service = module:depends("pubsub").service; + +function handle_POST(event, path) + local data = event.request.body; + local item_id = "default"; + + local post_item = st.stanza("item", { id = item_id, xmlns = "http://jabber.org/protocol/pubsub" }) + :tag("entry", { xmlns = "http://www.w3.org/2005/Atom" }) + :tag("id"):text(uuid_generate()):up() + :tag("title"):text(data):up() + :tag("author") + :tag("name"):text(event.request.conn:ip()):up() + :up() + :tag("published"):text(timestamp_generate()):up(); + + local ok, err = pubsub_service:publish(path, true, item_id, post_item); + module:log("debug", "Handled POST: \n%s\n", tostring(event.request.body)); + return ok and "Posted" or ("Error: "..err); +end + +module:provides("http", { + route = { + ["POST /*"] = handle_POST; + }; +}); + +function module.load() + module:log("debug", "Loaded at %s", module:http_url()); +end