Software /
code /
prosody
Annotate
core/rostermanager.lua @ 227:211c2e04c82b
Fix some very misleading logging
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 08 Nov 2008 20:44:32 +0000 |
parent | 193:13ac34255c37 |
child | 263:75275b6b4e03 |
child | 267:6303b7454ad0 |
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 |
183 | 14 local datamanager = require "util.datamanager" |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
15 local st = require "util.stanza"; |
0 | 16 |
17 module "rostermanager" | |
18 | |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
19 function add_to_roster(session, jid, item) |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
20 if session.roster then |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
21 local old_item = session.roster[jid]; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
22 session.roster[jid] = item; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
23 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
|
24 return true; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
25 else |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
26 session.roster[jid] = old_item; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
27 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
|
28 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
29 else |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
30 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
|
31 end |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
32 end |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
33 |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
34 function remove_from_roster(session, jid) |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
35 if session.roster then |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
36 local old_item = session.roster[jid]; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
37 session.roster[jid] = nil; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
38 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
|
39 return true; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
40 else |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
41 session.roster[jid] = old_item; |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
42 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
|
43 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
44 else |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
45 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
|
46 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
47 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
48 |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
49 function roster_push(username, host, jid) |
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
176
diff
changeset
|
50 if jid ~= "pending" and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
51 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
|
52 local stanza = st.iq({type="set"}); |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
53 stanza:tag("query", {xmlns = "jabber:iq:roster"}); |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
54 if item then |
132 | 55 stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name, ask = item.ask}); |
110 | 56 for group in pairs(item.groups) do |
57 stanza:tag("group"):text(group):up(); | |
58 end | |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
59 else |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
60 stanza:tag("item", {jid = jid, subscription = "remove"}); |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
61 end |
193 | 62 stanza:up(); -- move out from item |
63 stanza:up(); -- move out from stanza | |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
64 -- stanza ready |
110 | 65 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
|
66 if session.interested then |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
67 -- FIXME do we need to set stanza.attr.to? |
110 | 68 session.send(stanza); |
107
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
69 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
70 end |
8d8debda3df2
Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents:
103
diff
changeset
|
71 end |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
72 end |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
73 |
103
ebdb7875443e
Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents:
101
diff
changeset
|
74 function load_roster(username, host) |
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
176
diff
changeset
|
75 log("debug", "load_roster: asked for: "..username.."@"..host); |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
76 if hosts[host] and hosts[host].sessions[username] then |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
77 local roster = hosts[host].sessions[username].roster; |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
78 if not roster then |
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
176
diff
changeset
|
79 log("debug", "load_roster: loading for new user: "..username.."@"..host); |
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 |
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
176
diff
changeset
|
86 log("debug", "load_roster: loading for offline user: "..username.."@"..host); |
171
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
87 return datamanager.load(username, host, "roster") or {}; |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
88 end |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
89 |
103
ebdb7875443e
Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents:
101
diff
changeset
|
90 function save_roster(username, host) |
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
176
diff
changeset
|
91 log("debug", "save_roster: saving roster for "..username.."@"..host); |
101
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
92 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then |
110 | 93 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
|
94 end |
c690fa382743
Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents:
80
diff
changeset
|
95 return nil; |
0 | 96 end |
80
523ac742cc19
Fixed: rostermanager.lua now returns the module
Waqas Hussain <waqas20@gmail.com>
parents:
6
diff
changeset
|
97 |
171
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
98 function process_inbound_subscription_approval(username, host, jid) |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
99 local roster = load_roster(username, host); |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
100 local item = roster[jid]; |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
101 if item and item.ask then |
171
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
102 if item.subscription == "none" then |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
103 item.subscription = "to"; |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
104 else -- subscription == from |
171
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
105 item.subscription = "both"; |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
106 end |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
107 item.ask = nil; |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
108 return datamanager.store(username, host, "roster", roster); |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
109 end |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
110 end |
28f420d057a0
Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents:
132
diff
changeset
|
111 |
172
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
112 function process_inbound_subscription_cancellation(username, host, jid) |
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
113 local roster = load_roster(username, host); |
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
114 local item = roster[jid]; |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
115 local changed = nil; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
116 if is_contact_pending_out(username, host, jid) then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
117 item.ask = nil; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
118 changed = true; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
119 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
120 if item then |
172
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
121 if item.subscription == "to" then |
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
122 item.subscription = "none"; |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
123 changed = true; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
124 elseif item.subscription == "both" then |
172
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
125 item.subscription = "from"; |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
126 changed = true; |
172
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
127 end |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
128 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
129 if changed then |
172
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
130 return datamanager.store(username, host, "roster", roster); |
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
131 end |
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
132 end |
71247788c7c7
Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents:
171
diff
changeset
|
133 |
173 | 134 function process_inbound_unsubscribe(username, host, jid) |
135 local roster = load_roster(username, host); | |
136 local item = roster[jid]; | |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
137 local changed = nil; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
138 if is_contact_pending_in(username, host, jid) then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
139 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty? |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
140 changed = true; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
141 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
142 if item then |
173 | 143 if item.subscription == "from" then |
144 item.subscription = "none"; | |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
145 changed = true; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
146 elseif item.subscription == "both" then |
173 | 147 item.subscription = "to"; |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
148 changed = true; |
173 | 149 end |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
150 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
151 if changed then |
173 | 152 return datamanager.store(username, host, "roster", roster); |
153 end | |
154 end | |
155 | |
174
f9aff1fc7e99
Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
173
diff
changeset
|
156 function is_contact_subscribed(username, host, jid) |
f9aff1fc7e99
Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
173
diff
changeset
|
157 local roster = load_roster(username, host); |
f9aff1fc7e99
Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
173
diff
changeset
|
158 local item = roster[jid]; |
f9aff1fc7e99
Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
173
diff
changeset
|
159 return item and (item.subscription == "from" or item.subscription == "both"); |
f9aff1fc7e99
Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
173
diff
changeset
|
160 end |
f9aff1fc7e99
Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents:
173
diff
changeset
|
161 |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
162 function is_contact_pending_in(username, host, jid) |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
163 local roster = load_roster(username, host); |
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
176
diff
changeset
|
164 return roster.pending and roster.pending[jid]; |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
165 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
166 function set_contact_pending_in(username, host, jid, pending) |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
167 local roster = load_roster(username, host); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
168 local item = roster[jid]; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
169 if item and (item.subscription == "from" or item.subscription == "both") then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
170 return; -- false |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
171 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
172 if not roster.pending then roster.pending = {}; end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
173 roster.pending[jid] = true; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
174 return datamanager.store(username, host, "roster", roster); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
175 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
176 function is_contact_pending_out(username, host, jid) |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
177 local roster = load_roster(username, host); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
178 local item = roster[jid]; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
179 return item and item.ask; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
180 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
181 function set_contact_pending_out(username, host, jid) -- subscribe |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
182 local roster = load_roster(username, host); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
183 local item = roster[jid]; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
184 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
185 return true; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
186 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
187 if not item then |
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
176
diff
changeset
|
188 item = {subscription = "none", groups = {}}; |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
189 roster[jid] = item; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
190 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
191 item.ask = "subscribe"; |
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
176
diff
changeset
|
192 log("debug", "set_contact_pending_out: saving roster; set "..username.."@"..host..".roster["..jid.."].ask=subscribe"); |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
193 return datamanager.store(username, host, "roster", roster); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
194 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
195 function unsubscribe(username, host, jid) |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
196 local roster = load_roster(username, host); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
197 local item = roster[jid]; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
198 if not item then return false; end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
199 if (item.subscription == "from" or item.subscription == "none") and not item.ask then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
200 return true; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
201 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
202 item.ask = nil; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
203 if item.subscription == "both" then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
204 item.subscription = "from"; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
205 elseif item.subscription == "to" then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
206 item.subscription = "none"; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
207 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
208 return datamanager.store(username, host, "roster", roster); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
209 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
210 function subscribed(username, host, jid) |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
211 if is_contact_pending_in(username, host, jid) then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
212 local roster = load_roster(username, host); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
213 local item = roster[jid]; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
214 if item.subscription == "none" then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
215 item.subscription = "from"; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
216 else -- subscription == to |
177
606c433955e7
Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents:
176
diff
changeset
|
217 item.subscription = "both"; |
176
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
218 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
219 roster.pending[jid] = nil; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
220 -- TODO maybe remove roster.pending if empty |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
221 return datamanager.store(username, host, "roster", roster); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
222 end -- TODO else implement optional feature pre-approval (ask = subscribed) |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
223 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
224 function unsubscribed(username, host, jid) |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
225 local roster = load_roster(username, host); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
226 local item = roster[jid]; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
227 local pending = is_contact_pending_in(username, host, jid); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
228 local changed = nil; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
229 if is_contact_pending_in(username, host, jid) then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
230 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty? |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
231 changed = true; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
232 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
233 if item then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
234 if item.subscription == "from" then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
235 item.subscription = "none"; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
236 changed = true; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
237 elseif item.subscription == both then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
238 item.subscription = "to"; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
239 changed = true; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
240 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
241 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
242 if changed then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
243 return datamanager.store(username, host, "roster", roster); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
244 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
245 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
246 |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
247 function process_outbound_subscription_request(username, host, jid) |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
248 local roster = load_roster(username, host); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
249 local item = roster[jid]; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
250 if item and (item.subscription == "none" or item.subscription == "from") then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
251 item.ask = "subscribe"; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
252 return datamanager.store(username, host, "roster", roster); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
253 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
254 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
255 |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
256 --[[function process_outbound_subscription_approval(username, host, jid) |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
257 local roster = load_roster(username, host); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
258 local item = roster[jid]; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
259 if item and (item.subscription == "none" or item.subscription == "from" then |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
260 item.ask = "subscribe"; |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
261 return datamanager.store(username, host, "roster", roster); |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
262 end |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
263 end]] |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
264 |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
265 |
e5cd2a03891d
Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents:
174
diff
changeset
|
266 |
80
523ac742cc19
Fixed: rostermanager.lua now returns the module
Waqas Hussain <waqas20@gmail.com>
parents:
6
diff
changeset
|
267 return _M; |