Annotate

plugins/mod_pubsub/pubsub.lib.lua @ 5851:cdcfd93e2f43

mod_pubsub, util.pubsub: Keep track of the order of items
author Kim Alvefur <zash@zash.se>
date Fri, 04 Oct 2013 16:40:27 +0200
parent 5626:8416d4619d80
child 6438:b1c40054b59d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5626
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
1 local st = require "util.stanza";
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
2 local uuid_generate = require "util.uuid".generate;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
3
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
4 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
5 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
6
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
7 local _M = {};
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
8
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
9 local handlers = {};
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
10 _M.handlers = handlers;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
11
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
12 local pubsub_errors = {
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
13 ["conflict"] = { "cancel", "conflict" };
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
14 ["invalid-jid"] = { "modify", "bad-request", nil, "invalid-jid" };
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
15 ["jid-required"] = { "modify", "bad-request", nil, "jid-required" };
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
16 ["nodeid-required"] = { "modify", "bad-request", nil, "nodeid-required" };
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
17 ["item-not-found"] = { "cancel", "item-not-found" };
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
18 ["not-subscribed"] = { "modify", "unexpected-request", nil, "not-subscribed" };
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
19 ["forbidden"] = { "cancel", "forbidden" };
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
20 };
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
21 local function pubsub_error_reply(stanza, error)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
22 local e = pubsub_errors[error];
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
23 local reply = st.error_reply(stanza, unpack(e, 1, 3));
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
24 if e[4] then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
25 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up();
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
26 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
27 return reply;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
28 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
29 _M.pubsub_error_reply = pubsub_error_reply;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
30
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
31 function handlers.get_items(origin, stanza, items, service)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
32 local node = items.attr.node;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
33 local item = items:get_child("item");
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
34 local id = item and item.attr.id;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
35
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
36 if not node then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
37 return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
38 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
39 local ok, results = service:get_items(node, stanza.attr.from, id);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
40 if not ok then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
41 return origin.send(pubsub_error_reply(stanza, results));
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
42 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
43
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
44 local data = st.stanza("items", { node = node });
5851
cdcfd93e2f43 mod_pubsub, util.pubsub: Keep track of the order of items
Kim Alvefur <zash@zash.se>
parents: 5626
diff changeset
45 for _, id in ipairs(results) do
cdcfd93e2f43 mod_pubsub, util.pubsub: Keep track of the order of items
Kim Alvefur <zash@zash.se>
parents: 5626
diff changeset
46 data:add_child(results[id]);
5626
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
47 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
48 local reply;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
49 if data then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
50 reply = st.reply(stanza)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
51 :tag("pubsub", { xmlns = xmlns_pubsub })
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
52 :add_child(data);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
53 else
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
54 reply = pubsub_error_reply(stanza, "item-not-found");
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
55 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
56 return origin.send(reply);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
57 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
58
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
59 function handlers.get_subscriptions(origin, stanza, subscriptions, service)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
60 local node = subscriptions.attr.node;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
61 local ok, ret = service:get_subscriptions(node, stanza.attr.from, stanza.attr.from);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
62 if not ok then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
63 return origin.send(pubsub_error_reply(stanza, ret));
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
64 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
65 local reply = st.reply(stanza)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
66 :tag("pubsub", { xmlns = xmlns_pubsub })
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
67 :tag("subscriptions");
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
68 for _, sub in ipairs(ret) do
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
69 reply:tag("subscription", { node = sub.node, jid = sub.jid, subscription = 'subscribed' }):up();
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
70 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
71 return origin.send(reply);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
72 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
73
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
74 function handlers.set_create(origin, stanza, create, service)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
75 local node = create.attr.node;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
76 local ok, ret, reply;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
77 if node then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
78 ok, ret = service:create(node, stanza.attr.from);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
79 if ok then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
80 reply = st.reply(stanza);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
81 else
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
82 reply = pubsub_error_reply(stanza, ret);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
83 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
84 else
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
85 repeat
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
86 node = uuid_generate();
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
87 ok, ret = service:create(node, stanza.attr.from);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
88 until ok or ret ~= "conflict";
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
89 if ok then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
90 reply = st.reply(stanza)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
91 :tag("pubsub", { xmlns = xmlns_pubsub })
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
92 :tag("create", { node = node });
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
93 else
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
94 reply = pubsub_error_reply(stanza, ret);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
95 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
96 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
97 return origin.send(reply);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
98 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
99
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
100 function handlers.set_delete(origin, stanza, delete, service)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
101 local node = delete.attr.node;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
102
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
103 local reply, notifier;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
104 if not node then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
105 return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
106 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
107 local ok, ret = service:delete(node, stanza.attr.from);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
108 if ok then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
109 reply = st.reply(stanza);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
110 else
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
111 reply = pubsub_error_reply(stanza, ret);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
112 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
113 return origin.send(reply);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
114 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
115
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
116 function handlers.set_subscribe(origin, stanza, subscribe, service)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
117 local node, jid = subscribe.attr.node, subscribe.attr.jid;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
118 if not (node and jid) then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
119 return origin.send(pubsub_error_reply(stanza, jid and "nodeid-required" or "invalid-jid"));
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
120 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
121 --[[
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
122 local options_tag, options = stanza.tags[1]:get_child("options"), nil;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
123 if options_tag then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
124 options = options_form:data(options_tag.tags[1]);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
125 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
126 --]]
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
127 local options_tag, options; -- FIXME
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
128 local ok, ret = service:add_subscription(node, stanza.attr.from, jid, options);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
129 local reply;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
130 if ok then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
131 reply = st.reply(stanza)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
132 :tag("pubsub", { xmlns = xmlns_pubsub })
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
133 :tag("subscription", {
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
134 node = node,
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
135 jid = jid,
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
136 subscription = "subscribed"
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
137 }):up();
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
138 if options_tag then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
139 reply:add_child(options_tag);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
140 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
141 else
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
142 reply = pubsub_error_reply(stanza, ret);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
143 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
144 origin.send(reply);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
145 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
146
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
147 function handlers.set_unsubscribe(origin, stanza, unsubscribe, service)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
148 local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
149 if not (node and jid) then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
150 return origin.send(pubsub_error_reply(stanza, jid and "nodeid-required" or "invalid-jid"));
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
151 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
152 local ok, ret = service:remove_subscription(node, stanza.attr.from, jid);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
153 local reply;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
154 if ok then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
155 reply = st.reply(stanza);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
156 else
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
157 reply = pubsub_error_reply(stanza, ret);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
158 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
159 return origin.send(reply);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
160 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
161
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
162 function handlers.set_publish(origin, stanza, publish, service)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
163 local node = publish.attr.node;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
164 if not node then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
165 return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
166 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
167 local item = publish:get_child("item");
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
168 local id = (item and item.attr.id);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
169 if not id then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
170 id = uuid_generate();
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
171 if item then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
172 item.attr.id = id;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
173 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
174 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
175 local ok, ret = service:publish(node, stanza.attr.from, id, item);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
176 local reply;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
177 if ok then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
178 reply = st.reply(stanza)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
179 :tag("pubsub", { xmlns = xmlns_pubsub })
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
180 :tag("publish", { node = node })
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
181 :tag("item", { id = id });
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
182 else
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
183 reply = pubsub_error_reply(stanza, ret);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
184 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
185 return origin.send(reply);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
186 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
187
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
188 function handlers.set_retract(origin, stanza, retract, service)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
189 local node, notify = retract.attr.node, retract.attr.notify;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
190 notify = (notify == "1") or (notify == "true");
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
191 local item = retract:get_child("item");
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
192 local id = item and item.attr.id
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
193 if not (node and id) then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
194 return origin.send(pubsub_error_reply(stanza, node and "item-not-found" or "nodeid-required"));
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
195 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
196 local reply, notifier;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
197 if notify then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
198 notifier = st.stanza("retract", { id = id });
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
199 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
200 local ok, ret = service:retract(node, stanza.attr.from, id, notifier);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
201 if ok then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
202 reply = st.reply(stanza);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
203 else
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
204 reply = pubsub_error_reply(stanza, ret);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
205 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
206 return origin.send(reply);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
207 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
208
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
209 function handlers.set_purge(origin, stanza, purge, service)
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
210 local node, notify = purge.attr.node, purge.attr.notify;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
211 notify = (notify == "1") or (notify == "true");
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
212 local reply;
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
213 if not node then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
214 return origin.send(pubsub_error_reply(stanza, "nodeid-required"));
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
215 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
216 local ok, ret = service:purge(node, stanza.attr.from, notify);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
217 if ok then
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
218 reply = st.reply(stanza);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
219 else
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
220 reply = pubsub_error_reply(stanza, ret);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
221 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
222 return origin.send(reply);
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
223 end
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
224
8416d4619d80 mod_pubsub: Split out handlers into a module library
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
225 return _M;