Annotate

mod_service_directories/mod_service_directories.lua @ 5377:ca477408f90b

mod_http_oauth2: Fix misplaced 'default' on wrong side of } in client registration schema
author Kim Alvefur <zash@zash.se>
date Thu, 27 Apr 2023 19:14:23 +0200
parent 1343:7dbde05b48a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 -- Prosody IM
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2011 Waqas Hussain
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1280
diff changeset
3 --
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 --
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 -- An implementation of [XEP-0309: Service Directories]
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 -- Imports and defines
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local st = require "util.stanza";
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local jid_split = require "util.jid".split;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local adhoc_new = module:require "adhoc".new;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local to_ascii = require "util.encodings".idna.to_ascii;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local nameprep = require "util.encodings".stringprep.nameprep;
904
7648ee2d59d1 mod_service_directories: Use dataforms correctly
Kim Alvefur <zash@zash.se>
parents: 759
diff changeset
17 local dataforms_new = require "util.dataforms".new;
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local pairs, ipairs = pairs, ipairs;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 local module = module;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local hosts = hosts;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local subscription_from = {};
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local subscription_to = {};
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local contact_features = {};
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local contact_vcards = {};
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 -- Advertise in disco
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28
1280
42625801d15a mod_service_directories: Respect the 'name' option (Thanks Thomas)
Kim Alvefur <zash@zash.se>
parents: 904
diff changeset
29 module:add_identity("server", "directory", module:get_option_string("name", "Prosody"));
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 module:add_feature("urn:xmpp:server-presence");
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 -- Handle subscriptions
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 module:hook("presence/host", function(event) -- inbound presence to the host
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 local origin, stanza = event.origin, event.stanza;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 local node, host, resource = jid_split(stanza.attr.from);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 if stanza.attr.from ~= host then return; end -- not from a host
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 local t = stanza.attr.type;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 if t == "probe" then
759
6531a029fce5 mod_service_directories: Replace use of core_post_stanza() with module:send()
Kim Alvefur <zash@zash.se>
parents: 552
diff changeset
42 module:send(st.presence({ from = module.host, to = host, id = stanza.attr.id }));
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 elseif t == "subscribe" then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 subscription_from[host] = true;
759
6531a029fce5 mod_service_directories: Replace use of core_post_stanza() with module:send()
Kim Alvefur <zash@zash.se>
parents: 552
diff changeset
45 module:send(st.presence({ from = module.host, to = host, id = stanza.attr.id, type = "subscribed" }));
6531a029fce5 mod_service_directories: Replace use of core_post_stanza() with module:send()
Kim Alvefur <zash@zash.se>
parents: 552
diff changeset
46 module:send(st.presence({ from = module.host, to = host, id = stanza.attr.id }));
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 add_contact(host);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 elseif t == "subscribed" then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 subscription_to[host] = true;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 query_host(host);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 elseif t == "unsubscribe" then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 subscription_from[host] = nil;
759
6531a029fce5 mod_service_directories: Replace use of core_post_stanza() with module:send()
Kim Alvefur <zash@zash.se>
parents: 552
diff changeset
53 module:send(st.presence({ from = module.host, to = host, id = stanza.attr.id, type = "unsubscribed" }));
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 remove_contact(host);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 elseif t == "unsubscribed" then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 subscription_to[host] = nil;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 remove_contact(host);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 return true;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 end, 10); -- priority over mod_presence
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 function remove_contact(host, id)
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 contact_features[host] = nil;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 contact_vcards[host] = nil;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 if subscription_to[host] then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 subscription_to[host] = nil;
759
6531a029fce5 mod_service_directories: Replace use of core_post_stanza() with module:send()
Kim Alvefur <zash@zash.se>
parents: 552
diff changeset
67 module:send(st.presence({ from = module.host, to = host, id = id, type = "unsubscribe" }));
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 if subscription_from[host] then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 subscription_from[host] = nil;
759
6531a029fce5 mod_service_directories: Replace use of core_post_stanza() with module:send()
Kim Alvefur <zash@zash.se>
parents: 552
diff changeset
71 module:send(st.presence({ from = module.host, to = host, id = id, type = "unsubscribed" }));
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 function add_contact(host, id)
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 if not subscription_to[host] then
759
6531a029fce5 mod_service_directories: Replace use of core_post_stanza() with module:send()
Kim Alvefur <zash@zash.se>
parents: 552
diff changeset
76 module:send(st.presence({ from = module.host, to = host, id = id, type = "subscribe" }));
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 -- Admin ad-hoc command to subscribe
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 local function add_contact_handler(self, data, state)
904
7648ee2d59d1 mod_service_directories: Use dataforms correctly
Kim Alvefur <zash@zash.se>
parents: 759
diff changeset
83 local layout = dataforms_new{
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 title = "Adding a Server Buddy";
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 instructions = "Fill out this form to add a \"server buddy\".";
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 { name = "peerjid", type = "jid-single", required = true, label = "The server to add" };
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 };
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 if not state then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 return { status = "executing", form = layout }, "executing";
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 elseif data.action == "canceled" then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 return { status = "canceled" };
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 else
904
7648ee2d59d1 mod_service_directories: Use dataforms correctly
Kim Alvefur <zash@zash.se>
parents: 759
diff changeset
96 local fields = layout:data(data.form);
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 local peerjid = nameprep(fields.peerjid);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 if not peerjid or peerjid == "" or #peerjid > 1023 or not to_ascii(peerjid) then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 return { status = "completed", error = { message = "Invalid JID" } };
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 add_contact(peerjid);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 return { status = "completed" };
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 local add_contact_command = adhoc_new("Adding a Server Buddy", "http://jabber.org/protocol/admin#server-buddy", add_contact_handler, "admin");
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 module:add_item("adhoc", add_contact_command);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 -- Disco query remote host
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 function query_host(host)
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 local stanza = st.iq({ from = module.host, to = host, type = "get", id = "mod_service_directories:disco" })
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 :query("http://jabber.org/protocol/disco#info");
759
6531a029fce5 mod_service_directories: Replace use of core_post_stanza() with module:send()
Kim Alvefur <zash@zash.se>
parents: 552
diff changeset
113 module:send(stanza);
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 -- Handle disco query result
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 module:hook("iq-result/bare/mod_service_directories:disco", function(event)
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 local origin, stanza = event.origin, event.stanza;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 if not subscription_to[stanza.attr.from] then return; end -- not from a contact
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 local host = stanza.attr.from;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 local query = stanza:get_child("query", "http://jabber.org/protocol/disco#info")
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 if not query then return; end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 -- extract disco features
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 local features = {};
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 for _,tag in ipairs(query.tags) do
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 if tag.name == "feature" and tag.attr.var then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 features[tag.attr.var] = true;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 contact_features[host] = features;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 if features["urn:ietf:params:xml:ns:vcard-4.0"] then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 local stanza = st.iq({ from = module.host, to = host, type = "get", id = "mod_service_directories:vcard" })
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 :tag("vcard", { xmlns = "urn:ietf:params:xml:ns:vcard-4.0" });
759
6531a029fce5 mod_service_directories: Replace use of core_post_stanza() with module:send()
Kim Alvefur <zash@zash.se>
parents: 552
diff changeset
138 module:send(stanza);
552
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 return true;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 end);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 -- Handle vcard result
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 module:hook("iq-result/bare/mod_service_directories:vcard", function(event)
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 local origin, stanza = event.origin, event.stanza;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 if not subscription_to[stanza.attr.from] then return; end -- not from a contact
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 local host = stanza.attr.from;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 local vcard = stanza:get_child("vcard", "urn:ietf:params:xml:ns:vcard-4.0");
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 if not vcard then return; end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
152
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 contact_vcards[host] = st.clone(vcard);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 return true;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 end);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 -- PubSub
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 -- TODO the following should be replaced by mod_pubsub
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 module:hook("iq-get/host/http://jabber.org/protocol/pubsub:pubsub", function(event)
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 local origin, stanza = event.origin, event.stanza;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 local payload = stanza.tags[1];
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 local items = payload:get_child("items", "http://jabber.org/protocol/pubsub");
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 if items and items.attr.node == "urn:xmpp:contacts" then
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 local reply = st.reply(stanza)
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" })
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 :tag("items", { node = "urn:xmpp:contacts" });
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 for host, vcard in pairs(contact_vcards) do
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 reply:tag("item", { id = host })
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 :add_child(vcard)
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 :up();
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 origin.send(reply);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 return true;
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 end
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 end);
d1e83cb12885 mod_service_directories: Initial commit. Untested. Support for both directory and buddy use cases. Incomplete pubsub support (only getting all items supported).
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179