Mercurial > prosody-modules
comparison mod_pubsub_post/mod_pubsub_post.lua @ 1619:43c54a27bab2
mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 27 Feb 2015 16:25:48 +0000 |
| parents | |
| children | 8bfba31a1f8b |
comparison
equal
deleted
inserted
replaced
| 1618:c56baec031e8 | 1619:43c54a27bab2 |
|---|---|
| 1 module:depends("http"); | |
| 2 | |
| 3 local st = require "util.stanza"; | |
| 4 local json = require "util.json"; | |
| 5 local formdecode = require "net.http".formdecode; | |
| 6 local uuid_generate = require "util.uuid".generate; | |
| 7 local timestamp_generate = require "util.datetime".datetime; | |
| 8 | |
| 9 local pubsub_service = module:depends("pubsub").service; | |
| 10 | |
| 11 function handle_POST(event, path) | |
| 12 local data = event.request.body; | |
| 13 local item_id = "default"; | |
| 14 | |
| 15 local post_item = st.stanza("item", { id = item_id, xmlns = "http://jabber.org/protocol/pubsub" }) | |
| 16 :tag("entry", { xmlns = "http://www.w3.org/2005/Atom" }) | |
| 17 :tag("id"):text(uuid_generate()):up() | |
| 18 :tag("title"):text(data):up() | |
| 19 :tag("author") | |
| 20 :tag("name"):text(event.request.conn:ip()):up() | |
| 21 :up() | |
| 22 :tag("published"):text(timestamp_generate()):up(); | |
| 23 | |
| 24 local ok, err = pubsub_service:publish(path, true, item_id, post_item); | |
| 25 module:log("debug", "Handled POST: \n%s\n", tostring(event.request.body)); | |
| 26 return ok and "Posted" or ("Error: "..err); | |
| 27 end | |
| 28 | |
| 29 module:provides("http", { | |
| 30 route = { | |
| 31 ["POST /*"] = handle_POST; | |
| 32 }; | |
| 33 }); | |
| 34 | |
| 35 function module.load() | |
| 36 module:log("debug", "Loaded at %s", module:http_url()); | |
| 37 end |
