Annotate

plugins/mod_pubsub.lua @ 3697:67c01f75af97

mod_pubsub: Use pubsub_error_reply everywhere
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 03 Dec 2010 16:36:13 +0100
parent 3672:b24db47995ac
child 3698:77171fd1dc3c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local pubsub = require "util.pubsub";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local st = require "util.stanza";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local jid_bare = require "util.jid".bare;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local uuid_generate = require "util.uuid".generate;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 require "core.modulemanager".load(module.host, "iq");
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local service;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local handlers = {};
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 function handle_pubsub_iq(event)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local origin, stanza = event.origin, event.stanza;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local pubsub = stanza.tags[1];
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local action = pubsub.tags[1];
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local handler = handlers[stanza.attr.type.."_"..action.name];
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if handler then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 handler(origin, stanza, action);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 return true;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local pubsub_errors = {
3697
67c01f75af97 mod_pubsub: Use pubsub_error_reply everywhere
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
28 ["conflict"] = { "cancel", "conflict" };
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 ["invalid-jid"] = { "modify", "bad-request", nil, "invalid-jid" };
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 ["item-not-found"] = { "cancel", "item-not-found" };
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 };
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 function pubsub_error_reply(stanza, error)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local e = pubsub_errors[error];
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local reply = st.error_reply(stanza, unpack(e, 1, 3));
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 if e[4] then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up();
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 return reply;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
3641
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
41 function handlers.get_items(origin, stanza, items)
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
42 local node = items.attr.node;
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
43 local item = items:get_child("item");
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
44 local id = item and item.attr.id;
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
45 local data = st.stanza("items", { node = node });
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
46 for _, entry in pairs(service:get(node, stanza.attr.from, id)) do
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
47 data:add_child(entry);
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
48 end
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
49 if data then
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
50 reply = st.reply(stanza)
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
51 :tag("pubsub", { xmlns = xmlns_pubsub })
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
52 :add_child(data);
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
53 else
3697
67c01f75af97 mod_pubsub: Use pubsub_error_reply everywhere
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
54 reply = pubsub_error_reply(stanza, "item-not-found");
3641
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
55 end
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
56 return origin.send(reply);
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
57 end
3603aeb325de mod_pubsub, util.pubsub: Support for fetching items
Florian Zeitz <florob@babelmonkeys.de>
parents: 3622
diff changeset
58
3672
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
59 function handlers.set_create(origin, stanza, create)
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
60 local node = create.attr.node;
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
61 local ok, ret, reply;
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
62 if node then
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
63 ok, ret = service:create(node, stanza.attr.from);
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
64 if ok then
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
65 reply = st.reply(stanza);
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
66 else
3697
67c01f75af97 mod_pubsub: Use pubsub_error_reply everywhere
Florian Zeitz <florob@babelmonkeys.de>
parents: 3672
diff changeset
67 reply = pubsub_error_reply(stanza, ret);
3672
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
68 end
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
69 else
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
70 repeat
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
71 node = uuid_generate();
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
72 ok, ret = service:create(node, stanza.attr.from);
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
73 until ok;
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
74 reply = st.reply(stanza)
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
75 :tag("pubsub", { xmlns = xmlns_pubsub })
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
76 :tag("create", { node = node });
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
77 end
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
78 origin.send(reply);
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
79 end
b24db47995ac mod_pubsub, util.pubsub: Support node creation
Florian Zeitz <florob@babelmonkeys.de>
parents: 3641
diff changeset
80
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 function handlers.set_subscribe(origin, stanza, subscribe)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local node, jid = subscribe.attr.node, subscribe.attr.jid;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if jid_bare(jid) ~= jid_bare(stanza.attr.from) then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 return origin.send(pubsub_error_reply(stanza, "invalid-jid"));
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local ok, ret = service:add_subscription(node, stanza.attr.from, jid);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local reply;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if ok then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 reply = st.reply(stanza)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 :tag("pubsub", { xmlns = xmlns_pubsub })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 :tag("subscription", {
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 node = node,
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 jid = jid,
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 subscription = "subscribed"
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 });
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 else
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 reply = pubsub_error_reply(stanza, ret);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 return origin.send(reply);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 function handlers.set_publish(origin, stanza, publish)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 local node = publish.attr.node;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 local item = publish:get_child("item");
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 local id = (item and item.attr.id) or uuid_generate();
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 local ok, ret = service:publish(node, stanza.attr.from, id, item);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 local reply;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 if ok then
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 reply = st.reply(stanza)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 :tag("pubsub", { xmlns = xmlns_pubsub })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 :tag("publish", { node = node })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 :tag("item", { id = id });
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 else
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 reply = pubsub_error_reply(stanza, ret);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 return origin.send(reply);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 function simple_broadcast(node, jids, item)
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 local message = st.message({ from = module.host, type = "headline" })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 :tag("event", { xmlns = xmlns_pubsub_event })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 :tag("items", { node = node })
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 :add_child(item);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 for jid in pairs(jids) do
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 module:log("debug", "Sending notification to %s", jid);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 message.attr.to = jid;
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 core_post_stanza(hosts[module.host], message);
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
3621
7e1a1dd42088 mod_pubsub: Expose 'service'
Matthew Wild <mwild1@gmail.com>
parents: 3619
diff changeset
131 module:hook("iq/host/http://jabber.org/protocol/pubsub:pubsub", handle_pubsub_iq);
7e1a1dd42088 mod_pubsub: Expose 'service'
Matthew Wild <mwild1@gmail.com>
parents: 3619
diff changeset
132
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 service = pubsub.new({
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 broadcaster = simple_broadcast
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 });
3622
418354197a02 mod_pubsub: Use module.environment to reference the module's environment
Matthew Wild <mwild1@gmail.com>
parents: 3621
diff changeset
136 module.environment.service = service;
3619
291ae816d800 mod_pubsub: It's aliiiive!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137