Software /
code /
prosody
Annotate
plugins/mod_pep.lua @ 1338:0bfc8d866479
mod_pep: Add pubsub#publsih disco feature to host
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 12 Jun 2009 15:33:41 +0500 |
parent | 1335:7c71783b40cf |
child | 1399:8879f8278762 |
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; |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
9 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
|
10 |
1323
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
11 local data = {}; |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
12 local recipients = {}; |
1323
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
13 |
1335
7c71783b40cf
mod_pep: Add host identity pubsub/pep
Waqas Hussain <waqas20@gmail.com>
parents:
1332
diff
changeset
|
14 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
|
15 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
|
16 |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 local function publish(session, node, item) |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 local stanza = st.message({from=session.full_jid, type='headline'}) |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 :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
|
20 :tag('items', {node=node}) |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 :add_child(item) |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 :up() |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 :up(); |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 |
1323
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
25 local bare = session.username..'@'..session.host; |
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
26 -- store for the future |
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
27 local user_data = data[bare]; |
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
28 if not user_data then user_data = {}; data[bare] = user_data; end |
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
29 user_data[node] = stanza; |
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
30 |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 -- broadcast to resources |
1323
e1bfe7816761
mod_pep: Maintain user nodes for delayed delivery
Waqas Hussain <waqas20@gmail.com>
parents:
1136
diff
changeset
|
32 stanza.attr.to = bare; |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 core_route_stanza(session, stanza); |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 -- broadcast to contacts |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 for jid, item in pairs(session.roster) do |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 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
|
38 stanza.attr.to = jid; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 core_route_stanza(session, stanza); |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 end |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 end |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 end |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
44 module:hook("presence/bare", function(data) |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
45 -- inbound presence to bare JID recieved |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
46 local origin, stanza = data.origin, data.stanza; |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
47 |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
48 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
|
49 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
|
50 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
|
51 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
|
52 local t = stanza.attr.type; |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
53 local recipient = stanza.attr.from; |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
54 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
|
55 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
|
56 elseif not t then |
1332
f7fed9f77455
mod_pep: Fix a nil index bug
Waqas Hussain <waqas20@gmail.com>
parents:
1327
diff
changeset
|
57 recipients[user] = recipients[user] or {}; |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
58 if not recipients[user][recipient] then |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
59 recipients[user][recipient] = true; |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
60 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
|
61 message.attr.to = stanza.attr.from; |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
62 origin.send(message); |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
63 end |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
64 end |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
65 end |
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
66 end |
1327
20285e9d71ee
mod_pep: Give the presence handler a higher than default priority
Waqas Hussain <waqas20@gmail.com>
parents:
1325
diff
changeset
|
67 end, 10); |
1325
b58645973d7d
mod_pep: Send items to contacts coming online
Waqas Hussain <waqas20@gmail.com>
parents:
1324
diff
changeset
|
68 |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 module:add_iq_handler("c2s", "http://jabber.org/protocol/pubsub", function (session, stanza) |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 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
|
71 local payload = stanza.tags[1]; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 if payload.name == 'pubsub' then |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 payload = payload.tags[1]; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 if payload and payload.name == 'publish' and payload.attr.node then |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 local node = payload.attr.node; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 payload = payload.tags[1]; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 if payload then |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 publish(session, node, payload); |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 return true; |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 end -- TODO else error |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 end -- TODO else error |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 end |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 end |
1324
dc64a841f0c2
mod_pep: Fix undefined global access
Waqas Hussain <waqas20@gmail.com>
parents:
1323
diff
changeset
|
84 session.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
1136
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 end); |
506012db54e8
mod_pep: Initial commit (extremely basic implementation)
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 |