Software / code / prosody
Comparison
plugins/mod_pep.lua @ 1430:39169cf8d36f
mod_pep: Broadcast only to available recipients with caps
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Fri, 26 Jun 2009 23:58:52 +0500 |
| parent | 1429:51cfb152cb38 |
| child | 1437:cf2eba9b1716 |
comparison
equal
deleted
inserted
replaced
| 1429:51cfb152cb38 | 1430:39169cf8d36f |
|---|---|
| 7 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed; | 7 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed; |
| 8 local pairs, ipairs = pairs, ipairs; | 8 local pairs, ipairs = pairs, ipairs; |
| 9 local next = next; | 9 local next = next; |
| 10 local load_roster = require "core.rostermanager".load_roster; | 10 local load_roster = require "core.rostermanager".load_roster; |
| 11 | 11 |
| 12 local NULL = {}; | |
| 12 local data = {}; | 13 local data = {}; |
| 13 local recipients = {}; | 14 local recipients = {}; |
| 14 | 15 |
| 15 module:add_identity("pubsub", "pep"); | 16 module:add_identity("pubsub", "pep"); |
| 16 module:add_feature("http://jabber.org/protocol/pubsub#publish"); | 17 module:add_feature("http://jabber.org/protocol/pubsub#publish"); |
| 33 else | 34 else |
| 34 if not user_data then user_data = {}; data[bare] = user_data; end | 35 if not user_data then user_data = {}; data[bare] = user_data; end |
| 35 user_data[node] = stanza; | 36 user_data[node] = stanza; |
| 36 end | 37 end |
| 37 | 38 |
| 38 -- broadcast to resources | 39 -- broadcast |
| 39 stanza.attr.to = bare; | 40 for recipient in pairs(recipients[user] or NULL) do |
| 40 core_route_stanza(session, stanza); | 41 stanza.attr.to = recipient; |
| 41 | 42 core_post_stanza(session, stanza); |
| 42 -- broadcast to contacts | |
| 43 for jid, item in pairs(session.roster) do | |
| 44 if jid and jid ~= "pending" and (item.subscription == 'from' or item.subscription == 'both') then | |
| 45 stanza.attr.to = jid; | |
| 46 core_route_stanza(session, stanza); | |
| 47 end | |
| 48 end | 43 end |
| 49 end | 44 end |
| 50 | 45 |
| 51 local function get_caps_hash_from_presence(stanza, current) | 46 local function get_caps_hash_from_presence(stanza, current) |
| 52 local t = stanza.attr.type; | 47 local t = stanza.attr.type; |
| 74 | 69 |
| 75 local user = stanza.attr.to or (origin.username..'@'..origin.host); | 70 local user = stanza.attr.to or (origin.username..'@'..origin.host); |
| 76 local bare = jid_bare(stanza.attr.from); | 71 local bare = jid_bare(stanza.attr.from); |
| 77 local item = load_roster(jid_split(user))[bare]; | 72 local item = load_roster(jid_split(user))[bare]; |
| 78 if not stanza.attr.to or (item and (item.subscription == 'from' or item.subscription == 'both')) then | 73 if not stanza.attr.to or (item and (item.subscription == 'from' or item.subscription == 'both')) then |
| 79 local t = stanza.attr.type; | |
| 80 local recipient = stanza.attr.from; | 74 local recipient = stanza.attr.from; |
| 81 if t == "unavailable" or t == "error" then | 75 local current = recipients[user] and recipients[user][recipient]; |
| 76 local hash = get_caps_hash_from_presence(stanza, current); | |
| 77 if current == hash then return; end | |
| 78 if not hash then | |
| 82 if recipients[user] then recipients[user][recipient] = nil; end | 79 if recipients[user] then recipients[user][recipient] = nil; end |
| 83 elseif not t then | 80 else |
| 84 recipients[user] = recipients[user] or {}; | 81 recipients[user] = recipients[user] or {}; |
| 85 if not recipients[user][recipient] then | 82 recipients[user][recipient] = hash; |
| 86 recipients[user][recipient] = true; | 83 for node, message in pairs(data[user] or NULL) do |
| 87 for node, message in pairs(data[user] or {}) do | 84 message.attr.to = stanza.attr.from; |
| 88 message.attr.to = stanza.attr.from; | 85 origin.send(message); |
| 89 origin.send(message); | |
| 90 end | |
| 91 end | 86 end |
| 92 end | 87 end |
| 93 end | 88 end |
| 94 end, 10); | 89 end, 10); |
| 95 | 90 |