Software /
code /
prosody
Annotate
core/rostermanager.lua @ 171:28f420d057a0
Inbound subscription approval
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 25 Oct 2008 02:16:29 +0500 |
parent | 132:9a4aa57af367 |
child | 172:71247788c7c7 |
rev | line source |
---|---|
0 | 1 |
2 local mainlog = log; | |
3 local function log(type, message) | |
4 mainlog(type, "rostermanager", message); | |
5 end | |
6 | |
7 local setmetatable = setmetatable; | |
8 local format = string.format; | |
9 local loadfile, setfenv, pcall = loadfile, setfenv, pcall; | |
110 | 10 local pairs, ipairs = pairs, ipairs; |
0 | 11 |
103
ebdb7875443e
Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents:
101
diff
changeset
|
12 local hosts = hosts; |
ebdb7875443e
Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents:
101
diff
changeset
|
13 |
0 | 14 require "util.datamanager" |
15 | |
16 local datamanager = datamanager; | |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
17 local st = require "util.stanza"; |
0 | 18 |
19 module "rostermanager" | |
20 | |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
21 function add_to_roster(session, jid, item) |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
22 if session.roster then |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
23 local old_item = session.roster[jid]; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
24 session.roster[jid] = item; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
25 if save_roster(session.username, session.host) then |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
26 return true; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
27 else |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
28 session.roster[jid] = old_item; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
29 return nil, "wait", "internal-server-error", "Unable to save roster"; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
30 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
31 else |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
32 return nil, "auth", "not-authorized", "Session's roster not loaded"; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
33 end |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
34 end |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
35 |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
36 function remove_from_roster(session, jid) |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
37 if session.roster then |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
38 local old_item = session.roster[jid]; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
39 session.roster[jid] = nil; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
40 if save_roster(session.username, session.host) then |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
41 return true; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
42 else |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
43 session.roster[jid] = old_item; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
44 return nil, "wait", "internal-server-error", "Unable to save roster"; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
45 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
46 else |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
47 return nil, "auth", "not-authorized", "Session's roster not loaded"; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
48 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
49 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
50 |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
51 function roster_push(username, host, jid) |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
52 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
53 local item = hosts[host].sessions[username].roster[jid]; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
54 local stanza = st.iq({type="set"}); |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
55 stanza:tag("query", {xmlns = "jabber:iq:roster"}); |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
56 if item then |
132 | 57 stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name, ask = item.ask}); |
110 | 58 for group in pairs(item.groups) do |
59 stanza:tag("group"):text(group):up(); | |
60 end | |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
61 else |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
62 stanza:tag("item", {jid = jid, subscription = "remove"}); |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
63 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
64 stanza:up(); |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
65 stanza:up(); |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
66 -- stanza ready |
110 | 67 for _, session in pairs(hosts[host].sessions[username].sessions) do |
114
bed2a8508cf5
Added session property for interested resources
Waqas Hussain <waqas20@gmail.com>
parents:
110
diff
changeset
|
68 if session.interested then |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
69 -- FIXME do we need to set stanza.attr.to? |
110 | 70 session.send(stanza); |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
71 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
72 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
73 end |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
74 end |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
75 |
103
ebdb7875443e
Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents:
101
diff
changeset
|
76 function load_roster(username, host) |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
77 if hosts[host] and hosts[host].sessions[username] then |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
78 local roster = hosts[host].sessions[username].roster; |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
79 if not roster then |
103
ebdb7875443e
Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents:
101
diff
changeset
|
80 roster = datamanager.load(username, host, "roster") or {}; |
ebdb7875443e
Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents:
101
diff
changeset
|
81 hosts[host].sessions[username].roster = roster; |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
82 end |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
83 return roster; |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
84 end |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
85 -- Attempt to load roster for non-loaded user |
171
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
86 return datamanager.load(username, host, "roster") or {}; |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
87 end |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
88 |
103
ebdb7875443e
Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents:
101
diff
changeset
|
89 function save_roster(username, host) |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
90 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then |
110 | 91 return datamanager.store(username, host, "roster", hosts[host].sessions[username].roster); |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
92 end |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
93 return nil; |
0 | 94 end |
80
523ac742cc19
Fixed: rostermanager.lua now returns the module
Waqas Hussain <waqas20@gmail.com>
parents:
6
diff
changeset
|
95 |
171
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
96 function process_inbound_subscription_approval(username, host, jid) |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
97 local roster = load_roster(username, host); |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
98 local item = roster[jid]; |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
99 if item and item.ask and (item.subscription == "none" or item.subscription == "from") then |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
100 if item.subscription == "none" then |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
101 item.subscription = "to"; |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
102 else |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
103 item.subscription = "both"; |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
104 end |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
105 item.ask = nil; |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
106 return datamanager.store(username, host, "roster", roster); |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
107 end |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
108 end |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
109 |
80
523ac742cc19
Fixed: rostermanager.lua now returns the module
Waqas Hussain <waqas20@gmail.com>
parents:
6
diff
changeset
|
110 return _M; |