Merge 0.10->trunk
[prosody.git] / plugins / mod_pubsub / mod_pubsub.lua
1 local pubsub = require "util.pubsub";
2 local st = require "util.stanza";
3 local jid_bare = require "util.jid".bare;
4 local usermanager = require "core.usermanager";
5
6 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
7 local xmlns_pubsub_event = "http://jabber.org/protocol/pubsub#event";
8 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
9
10 local autocreate_on_publish = module:get_option_boolean("autocreate_on_publish", false);
11 local autocreate_on_subscribe = module:get_option_boolean("autocreate_on_subscribe", false);
12 local pubsub_disco_name = module:get_option("name");
13 if type(pubsub_disco_name) ~= "string" then pubsub_disco_name = "Prosody PubSub Service"; end
14 local expose_publisher = module:get_option_boolean("expose_publisher", false)
15
16 local service;
17
18 local lib_pubsub = module:require "pubsub";
19 local handlers = lib_pubsub.handlers;
20 local pubsub_error_reply = lib_pubsub.pubsub_error_reply;
21
22 module:depends("disco");
23 module:add_identity("pubsub", "service", pubsub_disco_name);
24 module:add_feature("http://jabber.org/protocol/pubsub");
25
26 function handle_pubsub_iq(event)
27         local origin, stanza = event.origin, event.stanza;
28         local pubsub = stanza.tags[1];
29         local action = pubsub.tags[1];
30         if not action then
31                 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
32                 return true;
33         end
34         local handler = handlers[stanza.attr.type.."_"..action.name];
35         if handler then
36                 handler(origin, stanza, action, service);
37                 return true;
38         end
39 end
40
41 function simple_broadcast(kind, node, jids, item, actor)
42         if item then
43                 item = st.clone(item);
44                 item.attr.xmlns = nil; -- Clear the pubsub namespace
45                 if expose_publisher and actor then
46                         item.attr.publisher = actor
47                 end
48         end
49         local message = st.message({ from = module.host, type = "headline" })
50                 :tag("event", { xmlns = xmlns_pubsub_event })
51                         :tag(kind, { node = node })
52                                 :add_child(item);
53         for jid in pairs(jids) do
54                 module:log("debug", "Sending notification to %s", jid);
55                 message.attr.to = jid;
56                 module:send(message);
57         end
58 end
59
60 module:hook("iq/host/"..xmlns_pubsub..":pubsub", handle_pubsub_iq);
61 module:hook("iq/host/"..xmlns_pubsub_owner..":pubsub", handle_pubsub_iq);
62
63 local feature_map = {
64         create = { "create-nodes", "instant-nodes", "item-ids" };
65         retract = { "delete-items", "retract-items" };
66         purge = { "purge-nodes" };
67         publish = { "publish", autocreate_on_publish and "auto-create" };
68         delete = { "delete-nodes" };
69         get_items = { "retrieve-items" };
70         add_subscription = { "subscribe" };
71         get_subscriptions = { "retrieve-subscriptions" };
72         set_configure = { "config-node" };
73         get_default = { "retrieve-default" };
74 };
75
76 local function add_disco_features_from_service(service)
77         for method, features in pairs(feature_map) do
78                 if service[method] then
79                         for _, feature in ipairs(features) do
80                                 if feature then
81                                         module:add_feature(xmlns_pubsub.."#"..feature);
82                                 end
83                         end
84                 end
85         end
86         for affiliation in pairs(service.config.capabilities) do
87                 if affiliation ~= "none" and affiliation ~= "owner" then
88                         module:add_feature(xmlns_pubsub.."#"..affiliation.."-affiliation");
89                 end
90         end
91 end
92
93 module:hook("host-disco-info-node", function (event)
94         local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
95         local ok, ret = service:get_nodes(stanza.attr.from);
96         if not ok or not ret[node] then
97                 return;
98         end
99         event.exists = true;
100         reply:tag("identity", { category = "pubsub", type = "leaf" });
101 end);
102
103 module:hook("host-disco-items-node", function (event)
104         local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
105         local ok, ret = service:get_items(node, stanza.attr.from);
106         if not ok then
107                 return;
108         end
109
110         for _, id in ipairs(ret) do
111                 reply:tag("item", { jid = module.host, name = id }):up();
112         end
113         event.exists = true;
114 end);
115
116
117 module:hook("host-disco-items", function (event)
118         local stanza, origin, reply = event.stanza, event.origin, event.reply;
119         local ok, ret = service:get_nodes(event.stanza.attr.from);
120         if not ok then
121                 return;
122         end
123         for node, node_obj in pairs(ret) do
124                 reply:tag("item", { jid = module.host, node = node, name = node_obj.config.name }):up();
125         end
126 end);
127
128 local admin_aff = module:get_option_string("default_admin_affiliation", "owner");
129 local unowned_aff = module:get_option_string("default_unowned_affiliation");
130 local function get_affiliation(jid, node)
131         local bare_jid = jid_bare(jid);
132         if bare_jid == module.host or usermanager.is_admin(bare_jid, module.host) then
133                 return admin_aff;
134         end
135         if not node then
136                 return unowned_aff;
137         end
138 end
139
140 function set_service(new_service)
141         service = new_service;
142         module.environment.service = service;
143         add_disco_features_from_service(service);
144 end
145
146 function module.save()
147         return { service = service };
148 end
149
150 function module.restore(data)
151         set_service(data.service);
152 end
153
154 function module.load()
155         if module.reloading then return; end
156
157         set_service(pubsub.new({
158                 capabilities = {
159                         none = {
160                                 create = false;
161                                 publish = false;
162                                 retract = false;
163                                 get_nodes = true;
164
165                                 subscribe = true;
166                                 unsubscribe = true;
167                                 get_subscription = true;
168                                 get_subscriptions = true;
169                                 get_items = true;
170
171                                 subscribe_other = false;
172                                 unsubscribe_other = false;
173                                 get_subscription_other = false;
174                                 get_subscriptions_other = false;
175
176                                 be_subscribed = true;
177                                 be_unsubscribed = true;
178
179                                 set_affiliation = false;
180                         };
181                         publisher = {
182                                 create = false;
183                                 publish = true;
184                                 retract = true;
185                                 get_nodes = true;
186
187                                 subscribe = true;
188                                 unsubscribe = true;
189                                 get_subscription = true;
190                                 get_subscriptions = true;
191                                 get_items = true;
192
193                                 subscribe_other = false;
194                                 unsubscribe_other = false;
195                                 get_subscription_other = false;
196                                 get_subscriptions_other = false;
197
198                                 be_subscribed = true;
199                                 be_unsubscribed = true;
200
201                                 set_affiliation = false;
202                         };
203                         owner = {
204                                 create = true;
205                                 publish = true;
206                                 retract = true;
207                                 delete = true;
208                                 get_nodes = true;
209                                 configure = true;
210
211                                 subscribe = true;
212                                 unsubscribe = true;
213                                 get_subscription = true;
214                                 get_subscriptions = true;
215                                 get_items = true;
216
217
218                                 subscribe_other = true;
219                                 unsubscribe_other = true;
220                                 get_subscription_other = true;
221                                 get_subscriptions_other = true;
222
223                                 be_subscribed = true;
224                                 be_unsubscribed = true;
225
226                                 set_affiliation = true;
227                         };
228                 };
229
230                 autocreate_on_publish = autocreate_on_publish;
231                 autocreate_on_subscribe = autocreate_on_subscribe;
232
233                 broadcaster = simple_broadcast;
234                 get_affiliation = get_affiliation;
235
236                 normalize_jid = jid_bare;
237         }));
238 end