Software /
code /
prosody
Annotate
plugins/mod_pep.lua @ 1873:3b770710a1ee
sessionmanager: Use UUID for stream ids to clients, no math.random anymore :)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 03 Oct 2009 18:40:47 +0100 |
parent | 1696:2afc87ccc379 |
child | 2041:8cf9b978ab36 |
rev | line source |
---|---|
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1461
diff
changeset
|
1 -- Prosody IM |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1461
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1461
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1461
diff
changeset
|
4 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1461
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1461
diff
changeset
|
6 -- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1461
diff
changeset
|
7 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1461
diff
changeset
|
8 |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 local jid_bare = require "util.jid".bare; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local jid_split = require "util.jid".split; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 local st = require "util.stanza"; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 local hosts = hosts; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 local user_exists = require "core.usermanager".user_exists; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 local pairs, ipairs = pairs, ipairs; |
1401
a3ce55c1e43a
mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents:
1400
diff
changeset
|
17 local next = next; |
1437
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
18 local type = type; |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
19 local load_roster = require "core.rostermanager".load_roster; |
1437
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
20 local sha1 = require "util.hashes".sha1; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
21 local base64 = require "util.encodings".base64.encode; |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 |
1430
39169cf8d36f
mod_pep: Broadcast only to available recipients with caps
Waqas Hussain <waqas20@gmail.com>
parents:
1429
diff
changeset
|
23 local NULL = {}; |
1323
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
24 local data = {}; |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
25 local recipients = {}; |
1437
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
26 local hash_map = {}; |
1323
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
27 |
1696
2afc87ccc379
mod_pep: Added name 'Prosody' to the pubsub/pep identity
Waqas Hussain <waqas20@gmail.com>
parents:
1625
diff
changeset
|
28 module:add_identity("pubsub", "pep", "Prosody"); |
1338
0bfc8d866479
mod_pep: Add pubsub#publsih disco feature to host
Waqas Hussain <waqas20@gmail.com>
parents:
1335
diff
changeset
|
29 module:add_feature("http://jabber.org/protocol/pubsub#publish"); |
1335
7c71783b40cf
mod_pep: Add host identity pubsub/pep
Waqas Hussain <waqas20@gmail.com>
parents:
1332
diff
changeset
|
30 |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 local function publish(session, node, item) |
1401
a3ce55c1e43a
mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents:
1400
diff
changeset
|
32 local disable = #item.tags ~= 1 or #item.tags[1].tags == 0; |
1619
ae3250783db2
mod_pep: Added support for pubsub item retraction
Waqas Hussain <waqas20@gmail.com>
parents:
1522
diff
changeset
|
33 if #item.tags == 0 then item.name = "retract"; end |
1402
34723bbd5acb
mod_pep: Broadcast from the user's bare JID, not full JID
Waqas Hussain <waqas20@gmail.com>
parents:
1401
diff
changeset
|
34 local bare = session.username..'@'..session.host; |
34723bbd5acb
mod_pep: Broadcast from the user's bare JID, not full JID
Waqas Hussain <waqas20@gmail.com>
parents:
1401
diff
changeset
|
35 local stanza = st.message({from=bare, type='headline'}) |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 :tag('event', {xmlns='http://jabber.org/protocol/pubsub#event'}) |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 :tag('items', {node=node}) |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 :add_child(item) |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 :up() |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 :up(); |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 |
1323
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
42 -- store for the future |
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
43 local user_data = data[bare]; |
1401
a3ce55c1e43a
mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents:
1400
diff
changeset
|
44 if disable then |
1440
d3117a6ad1bf
mod_pep: Fixed a nil access error
Waqas Hussain <waqas20@gmail.com>
parents:
1439
diff
changeset
|
45 if user_data then |
d3117a6ad1bf
mod_pep: Fixed a nil access error
Waqas Hussain <waqas20@gmail.com>
parents:
1439
diff
changeset
|
46 user_data[node] = nil; |
d3117a6ad1bf
mod_pep: Fixed a nil access error
Waqas Hussain <waqas20@gmail.com>
parents:
1439
diff
changeset
|
47 if not next(user_data) then data[bare] = nil; end |
d3117a6ad1bf
mod_pep: Fixed a nil access error
Waqas Hussain <waqas20@gmail.com>
parents:
1439
diff
changeset
|
48 end |
1401
a3ce55c1e43a
mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents:
1400
diff
changeset
|
49 else |
a3ce55c1e43a
mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents:
1400
diff
changeset
|
50 if not user_data then user_data = {}; data[bare] = user_data; end |
a3ce55c1e43a
mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents:
1400
diff
changeset
|
51 user_data[node] = stanza; |
a3ce55c1e43a
mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents:
1400
diff
changeset
|
52 end |
1323
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
53 |
1430
39169cf8d36f
mod_pep: Broadcast only to available recipients with caps
Waqas Hussain <waqas20@gmail.com>
parents:
1429
diff
changeset
|
54 -- broadcast |
1437
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
55 for recipient, notify in pairs(recipients[bare] or NULL) do |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
56 if notify[node] then |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
57 stanza.attr.to = recipient; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
58 core_post_stanza(session, stanza); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
59 end |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 end |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 end |
1439
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
62 local function publish_all(user, recipient, session) |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
63 local d = data[user]; |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
64 local notify = recipients[user] and recipients[user][recipient]; |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
65 if d and notify then |
1451
cc0aa9470775
mod_pep: Fixed boolean indexing error while casting all messages for a user
Waqas Hussain <waqas20@gmail.com>
parents:
1440
diff
changeset
|
66 for node in pairs(notify) do |
cc0aa9470775
mod_pep: Fixed boolean indexing error while casting all messages for a user
Waqas Hussain <waqas20@gmail.com>
parents:
1440
diff
changeset
|
67 local message = d[node]; |
cc0aa9470775
mod_pep: Fixed boolean indexing error while casting all messages for a user
Waqas Hussain <waqas20@gmail.com>
parents:
1440
diff
changeset
|
68 if message then |
1439
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
69 message.attr.to = recipient; |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
70 session.send(message); |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
71 end |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
72 end |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
73 end |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
74 end |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 |
1428
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
76 local function get_caps_hash_from_presence(stanza, current) |
1429
51cfb152cb38
mod_pep: Ignore presence subscriptions when extractng hash information
Waqas Hussain <waqas20@gmail.com>
parents:
1428
diff
changeset
|
77 local t = stanza.attr.type; |
51cfb152cb38
mod_pep: Ignore presence subscriptions when extractng hash information
Waqas Hussain <waqas20@gmail.com>
parents:
1428
diff
changeset
|
78 if not t then |
1428
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
79 for _, child in pairs(stanza.tags) do |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
80 if child.name == "c" and child.attr.xmlns == "http://jabber.org/protocol/caps" then |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
81 local attr = child.attr; |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
82 if attr.hash then -- new caps |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
83 if attr.hash == 'sha-1' and attr.node and attr.ver then return attr.ver, attr.node.."#"..attr.ver; end |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
84 else -- legacy caps |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
85 if attr.node and attr.ver then return attr.node.."#"..attr.ver.."#"..(attr.ext or ""), attr.node.."#"..attr.ver; end |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
86 end |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
87 return; -- bad caps format |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
88 end |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
89 end |
1429
51cfb152cb38
mod_pep: Ignore presence subscriptions when extractng hash information
Waqas Hussain <waqas20@gmail.com>
parents:
1428
diff
changeset
|
90 elseif t == "unavailable" or t == "error" then |
51cfb152cb38
mod_pep: Ignore presence subscriptions when extractng hash information
Waqas Hussain <waqas20@gmail.com>
parents:
1428
diff
changeset
|
91 return; |
1428
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
92 end |
1429
51cfb152cb38
mod_pep: Ignore presence subscriptions when extractng hash information
Waqas Hussain <waqas20@gmail.com>
parents:
1428
diff
changeset
|
93 return current; -- no caps, could mean caps optimization, so return current |
1428
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
94 end |
02e97e716675
mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents:
1425
diff
changeset
|
95 |
1399
8879f8278762
mod_pep: Fixed detection of contact presence changes
Waqas Hussain <waqas20@gmail.com>
parents:
1338
diff
changeset
|
96 module:hook("presence/bare", function(event) |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
97 -- inbound presence to bare JID recieved |
1399
8879f8278762
mod_pep: Fixed detection of contact presence changes
Waqas Hussain <waqas20@gmail.com>
parents:
1338
diff
changeset
|
98 local origin, stanza = event.origin, event.stanza; |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
99 |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
100 local user = stanza.attr.to or (origin.username..'@'..origin.host); |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
101 local bare = jid_bare(stanza.attr.from); |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
102 local item = load_roster(jid_split(user))[bare]; |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
103 if not stanza.attr.to or (item and (item.subscription == 'from' or item.subscription == 'both')) then |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
104 local recipient = stanza.attr.from; |
1430
39169cf8d36f
mod_pep: Broadcast only to available recipients with caps
Waqas Hussain <waqas20@gmail.com>
parents:
1429
diff
changeset
|
105 local current = recipients[user] and recipients[user][recipient]; |
39169cf8d36f
mod_pep: Broadcast only to available recipients with caps
Waqas Hussain <waqas20@gmail.com>
parents:
1429
diff
changeset
|
106 local hash = get_caps_hash_from_presence(stanza, current); |
39169cf8d36f
mod_pep: Broadcast only to available recipients with caps
Waqas Hussain <waqas20@gmail.com>
parents:
1429
diff
changeset
|
107 if current == hash then return; end |
39169cf8d36f
mod_pep: Broadcast only to available recipients with caps
Waqas Hussain <waqas20@gmail.com>
parents:
1429
diff
changeset
|
108 if not hash then |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
109 if recipients[user] then recipients[user][recipient] = nil; end |
1430
39169cf8d36f
mod_pep: Broadcast only to available recipients with caps
Waqas Hussain <waqas20@gmail.com>
parents:
1429
diff
changeset
|
110 else |
1332
f7fed9f77455
mod_pep: Fix a nil index bug
Waqas Hussain <waqas20@gmail.com>
parents:
1327
diff
changeset
|
111 recipients[user] = recipients[user] or {}; |
1439
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
112 if hash_map[hash] then |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
113 recipients[user][recipient] = hash_map[hash]; |
1457
4723bd466a54
mod_pep: Pass origin/session to publish_all()
Matthew Wild <mwild1@gmail.com>
parents:
1451
diff
changeset
|
114 publish_all(user, recipient, origin); |
1439
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
115 else |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
116 recipients[user][recipient] = hash; |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
117 origin.send( |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
118 st.stanza("iq", {from=stanza.attr.to, to=stanza.attr.from, id="disco", type="get"}) |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
119 :query("http://jabber.org/protocol/disco#info") |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
120 ); |
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
121 end |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
122 end |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
123 end |
1327
20285e9d71ee
mod_pep: Give the presence handler a higher than default priority
Waqas Hussain <waqas20@gmail.com>
parents:
1325
diff
changeset
|
124 end, 10); |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
125 |
1404
12abd2da8885
mod_pep: Use new style events
Waqas Hussain <waqas20@gmail.com>
parents:
1402
diff
changeset
|
126 module:hook("iq/bare/http://jabber.org/protocol/pubsub:pubsub", function(event) |
12abd2da8885
mod_pep: Use new style events
Waqas Hussain <waqas20@gmail.com>
parents:
1402
diff
changeset
|
127 local session, stanza = event.origin, event.stanza; |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 if stanza.attr.type == 'set' and (not stanza.attr.to or jid_bare(stanza.attr.from) == stanza.attr.to) then |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 local payload = stanza.tags[1]; |
1400
cbbb4f4fed68
mod_pep: Added some comments
Waqas Hussain <waqas20@gmail.com>
parents:
1399
diff
changeset
|
130 if payload.name == 'pubsub' then -- <pubsub xmlns='http://jabber.org/protocol/pubsub'> |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 payload = payload.tags[1]; |
1619
ae3250783db2
mod_pep: Added support for pubsub item retraction
Waqas Hussain <waqas20@gmail.com>
parents:
1522
diff
changeset
|
132 if payload and (payload.name == 'publish' or payload.name == 'retract') and payload.attr.node then -- <publish node='http://jabber.org/protocol/tune'> |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 local node = payload.attr.node; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 payload = payload.tags[1]; |
1400
cbbb4f4fed68
mod_pep: Added some comments
Waqas Hussain <waqas20@gmail.com>
parents:
1399
diff
changeset
|
135 if payload then -- <item> |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 publish(session, node, payload); |
1625
b6645fe7d89f
Fixed: IQ replies are not sent back on publishing a PEP event
Waqas Hussain <waqas20@gmail.com>
parents:
1619
diff
changeset
|
137 session.send(st.reply(stanza)); |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 return true; |
1425
8187f72b7425
mod_pep: Removed some YODO comments
Waqas Hussain <waqas20@gmail.com>
parents:
1424
diff
changeset
|
139 end |
8187f72b7425
mod_pep: Removed some YODO comments
Waqas Hussain <waqas20@gmail.com>
parents:
1424
diff
changeset
|
140 end |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 end |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 end |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 end); |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 |
1437
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
145 local function calculate_hash(disco_info) |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
146 local identities, features, extensions = {}, {}, {}; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
147 for _, tag in pairs(disco_info) do |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
148 if tag.name == "identity" then |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
149 table.insert(identities, (tag.attr.category or "").."\0"..(tag.attr.type or "").."\0"..(tag.attr["xml:lang"] or "").."\0"..(tag.attr.name or "")); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
150 elseif tag.name == "feature" then |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
151 table.insert(features, tag.attr.var or ""); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
152 elseif tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
153 local form = {}; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
154 local FORM_TYPE; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
155 for _, field in pairs(tag.tags) do |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
156 if field.name == "field" and field.attr.var then |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
157 local values = {}; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
158 for _, val in pairs(field.tags) do |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
159 val = #val.tags == 0 and table.concat(val); -- FIXME use get_text? |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
160 if val then table.insert(values, val); end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
161 end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
162 table.sort(values); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
163 if field.attr.var == "FORM_TYPE" then |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
164 FORM_TYPE = values[1]; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
165 elseif #values > 0 then |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
166 table.insert(form, field.attr.var.."\0"..table.concat(values, "<")); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
167 else |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
168 table.insert(form, field.attr.var); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
169 end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
170 end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
171 end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
172 table.sort(form); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
173 form = table.concat(form, "<"); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
174 if FORM_TYPE then form = FORM_TYPE.."\0"..form; end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
175 table.insert(extensions, form); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
176 end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
177 end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
178 table.sort(identities); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
179 table.sort(features); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
180 table.sort(extensions); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
181 if #identities > 0 then identities = table.concat(identities, "<"):gsub("%z", "/").."<"; else identities = ""; end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
182 if #features > 0 then features = table.concat(features, "<").."<"; else features = ""; end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
183 if #extensions > 0 then extensions = table.concat(extensions, "<"):gsub("%z", "<").."<"; else extensions = ""; end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
184 local S = identities..features..extensions; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
185 local ver = base64(sha1(S)); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
186 return ver, S; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
187 end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
188 |
1424
6969ad97ca58
mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents:
1404
diff
changeset
|
189 module:hook("iq/bare/disco", function(event) |
6969ad97ca58
mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents:
1404
diff
changeset
|
190 local session, stanza = event.origin, event.stanza; |
6969ad97ca58
mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents:
1404
diff
changeset
|
191 if stanza.attr.type == "result" then |
6969ad97ca58
mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents:
1404
diff
changeset
|
192 local disco = stanza.tags[1]; |
6969ad97ca58
mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents:
1404
diff
changeset
|
193 if disco and disco.name == "query" and disco.attr.xmlns == "http://jabber.org/protocol/disco#info" then |
6969ad97ca58
mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents:
1404
diff
changeset
|
194 -- Process disco response |
1437
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
195 local user = stanza.attr.to or (session.username..'@'..session.host); |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
196 local contact = stanza.attr.from; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
197 local current = recipients[user] and recipients[user][contact]; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
198 if type(current) ~= "string" then return; end -- check if waiting for recipient's response |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
199 local ver = current; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
200 if not string.find(current, "#") then |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
201 ver = calculate_hash(disco.tags); -- calculate hash |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
202 end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
203 local notify = {}; |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
204 for _, feature in pairs(disco.tags) do |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
205 if feature.name == "feature" and feature.attr.var then |
1461
51f0202b0868
mod_pep: Escape + in pattern
Matthew Wild <mwild1@gmail.com>
parents:
1457
diff
changeset
|
206 local nfeature = feature.attr.var:match("^(.*)%+notify$"); |
1437
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
207 if nfeature then notify[nfeature] = true; end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
208 end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
209 end |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
210 hash_map[ver] = notify; -- update hash map |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
211 recipients[user][contact] = notify; -- set recipient's data to calculated data |
cf2eba9b1716
mod_pep: Entity capabilities
Waqas Hussain <waqas20@gmail.com>
parents:
1430
diff
changeset
|
212 -- send messages to recipient |
1439
f989608964ec
mod_pep: Use cached caps
Waqas Hussain <waqas20@gmail.com>
parents:
1437
diff
changeset
|
213 publish_all(user, contact, session); |
1424
6969ad97ca58
mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents:
1404
diff
changeset
|
214 end |
6969ad97ca58
mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents:
1404
diff
changeset
|
215 end |
6969ad97ca58
mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents:
1404
diff
changeset
|
216 end); |