Annotate

plugins/mod_pep.lua @ 1429:51cfb152cb38

mod_pep: Ignore presence subscriptions when extractng hash information
author Waqas Hussain <waqas20@gmail.com>
date Fri, 26 Jun 2009 23:42:59 +0500
parent 1428:02e97e716675
child 1430:39169cf8d36f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1136
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local jid_bare = require "util.jid".bare;
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local jid_split = require "util.jid".split;
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local st = require "util.stanza";
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local hosts = hosts;
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local user_exists = require "core.usermanager".user_exists;
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 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
8 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
9 local next = next;
1325
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
10 local load_roster = require "core.rostermanager".load_roster;
1136
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
1323
e1bfe7816761 mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents: 1136
diff changeset
12 local data = {};
1325
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
13 local recipients = {};
1323
e1bfe7816761 mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents: 1136
diff changeset
14
1335
7c71783b40cf mod_pep: Add host identity pubsub/pep
Waqas Hussain <waqas20@gmail.com>
parents: 1332
diff changeset
15 module:add_identity("pubsub", "pep");
1338
0bfc8d866479 mod_pep: Add pubsub#publsih disco feature to host
Waqas Hussain <waqas20@gmail.com>
parents: 1335
diff changeset
16 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
17
1136
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 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
19 local disable = #item.tags ~= 1 or #item.tags[1].tags == 0;
1402
34723bbd5acb mod_pep: Broadcast from the user's bare JID, not full JID
Waqas Hussain <waqas20@gmail.com>
parents: 1401
diff changeset
20 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
21 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
22 :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
23 :tag('items', {node=node})
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 :add_child(item)
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 :up()
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 :up();
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27
1323
e1bfe7816761 mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents: 1136
diff changeset
28 -- store for the future
e1bfe7816761 mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents: 1136
diff changeset
29 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
30 if disable then
a3ce55c1e43a mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents: 1400
diff changeset
31 if user_data then user_data[node] = nil; end
a3ce55c1e43a mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents: 1400
diff changeset
32 if not next(user_data) then data[bare] = nil; end
a3ce55c1e43a mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents: 1400
diff changeset
33 else
a3ce55c1e43a mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents: 1400
diff changeset
34 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
35 user_data[node] = stanza;
a3ce55c1e43a mod_pep: Remove data when a user disables a node
Waqas Hussain <waqas20@gmail.com>
parents: 1400
diff changeset
36 end
1323
e1bfe7816761 mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents: 1136
diff changeset
37
1136
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 -- broadcast to resources
1323
e1bfe7816761 mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents: 1136
diff changeset
39 stanza.attr.to = bare;
1136
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 core_route_stanza(session, stanza);
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 -- broadcast to contacts
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 for jid, item in pairs(session.roster) do
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 if jid and jid ~= "pending" and (item.subscription == 'from' or item.subscription == 'both') then
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 stanza.attr.to = jid;
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 core_route_stanza(session, stanza);
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 end
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 end
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 end
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50
1428
02e97e716675 mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents: 1425
diff changeset
51 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
52 local t = stanza.attr.type;
51cfb152cb38 mod_pep: Ignore presence subscriptions when extractng hash information
Waqas Hussain <waqas20@gmail.com>
parents: 1428
diff changeset
53 if not t then
1428
02e97e716675 mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents: 1425
diff changeset
54 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
55 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
56 local attr = child.attr;
02e97e716675 mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents: 1425
diff changeset
57 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
58 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
59 else -- legacy caps
02e97e716675 mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents: 1425
diff changeset
60 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
61 end
02e97e716675 mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents: 1425
diff changeset
62 return; -- bad caps format
02e97e716675 mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents: 1425
diff changeset
63 end
02e97e716675 mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents: 1425
diff changeset
64 end
1429
51cfb152cb38 mod_pep: Ignore presence subscriptions when extractng hash information
Waqas Hussain <waqas20@gmail.com>
parents: 1428
diff changeset
65 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
66 return;
1428
02e97e716675 mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents: 1425
diff changeset
67 end
1429
51cfb152cb38 mod_pep: Ignore presence subscriptions when extractng hash information
Waqas Hussain <waqas20@gmail.com>
parents: 1428
diff changeset
68 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
69 end
02e97e716675 mod_pep: Added function get_caps_hash_from_presence
Waqas Hussain <waqas20@gmail.com>
parents: 1425
diff changeset
70
1399
8879f8278762 mod_pep: Fixed detection of contact presence changes
Waqas Hussain <waqas20@gmail.com>
parents: 1338
diff changeset
71 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
72 -- 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
73 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
74
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
75 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
76 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
77 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
78 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
79 local t = stanza.attr.type;
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
80 local recipient = stanza.attr.from;
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
81 if t == "unavailable" or t == "error" then
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
82 if recipients[user] then recipients[user][recipient] = nil; end
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
83 elseif not t then
1332
f7fed9f77455 mod_pep: Fix a nil index bug
Waqas Hussain <waqas20@gmail.com>
parents: 1327
diff changeset
84 recipients[user] = recipients[user] or {};
1325
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
85 if not recipients[user][recipient] then
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
86 recipients[user][recipient] = true;
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
87 for node, message in pairs(data[user] or {}) do
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
88 message.attr.to = stanza.attr.from;
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
89 origin.send(message);
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
90 end
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
91 end
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
92 end
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
93 end
1327
20285e9d71ee mod_pep: Give the presence handler a higher than default priority
Waqas Hussain <waqas20@gmail.com>
parents: 1325
diff changeset
94 end, 10);
1325
b58645973d7d mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents: 1324
diff changeset
95
1404
12abd2da8885 mod_pep: Use new style events
Waqas Hussain <waqas20@gmail.com>
parents: 1402
diff changeset
96 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
97 local session, stanza = event.origin, event.stanza;
1136
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 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
99 local payload = stanza.tags[1];
1400
cbbb4f4fed68 mod_pep: Added some comments
Waqas Hussain <waqas20@gmail.com>
parents: 1399
diff changeset
100 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
101 payload = payload.tags[1];
1400
cbbb4f4fed68 mod_pep: Added some comments
Waqas Hussain <waqas20@gmail.com>
parents: 1399
diff changeset
102 if payload and payload.name == 'publish' 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
103 local node = payload.attr.node;
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 payload = payload.tags[1];
1400
cbbb4f4fed68 mod_pep: Added some comments
Waqas Hussain <waqas20@gmail.com>
parents: 1399
diff changeset
105 if payload then -- <item>
1136
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 publish(session, node, payload);
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 return true;
1425
8187f72b7425 mod_pep: Removed some YODO comments
Waqas Hussain <waqas20@gmail.com>
parents: 1424
diff changeset
108 end
8187f72b7425 mod_pep: Removed some YODO comments
Waqas Hussain <waqas20@gmail.com>
parents: 1424
diff changeset
109 end
1136
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 end
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 end
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 end);
506012db54e8 mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113
1424
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
114 module:hook("iq/bare/disco", function(event)
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
115 local session, stanza = event.origin, event.stanza;
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
116 if stanza.attr.type == "result" then
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
117 local disco = stanza.tags[1];
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
118 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
119 -- Process disco response
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
120 -- TODO check if waiting for recipient's response
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
121 local hash; -- TODO calculate hash
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
122 -- TODO update hash map
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
123 -- TODO set recipient's data to calculated data
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
124 end
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
125 end
6969ad97ca58 mod_pep: Added handler for disco responses
Waqas Hussain <waqas20@gmail.com>
parents: 1404
diff changeset
126 end);