Annotate

core/rostermanager.lua @ 583:5821eaa80baa

Remove print()s from sessionmanager and s2smanager
author Matthew Wild <mwild1@gmail.com>
date Sat, 06 Dec 2008 23:14:39 +0000
parent 519:cccd610a0ef9
child 615:4ae3e81513f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
1 -- Prosody IM v0.1
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
2 -- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
3 -- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
4 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
5 -- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
6 -- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
7 -- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
8 -- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
9 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
10 -- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
13 -- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
14 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
15 -- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
16 -- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
18 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
19
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
20
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
21
263
75275b6b4e03 Update rostermanager to use new logger
Matthew Wild <mwild1@gmail.com>
parents: 193
diff changeset
22
75275b6b4e03 Update rostermanager to use new logger
Matthew Wild <mwild1@gmail.com>
parents: 193
diff changeset
23 local log = require "util.logger".init("rostermanager");
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
24
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
25 local setmetatable = setmetatable;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
26 local format = string.format;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
27 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
110
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
28 local pairs, ipairs = pairs, ipairs;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
29
103
ebdb7875443e Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents: 101
diff changeset
30 local hosts = hosts;
ebdb7875443e Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents: 101
diff changeset
31
183
403db3b5c3c2 Combined some lines
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
32 local datamanager = require "util.datamanager"
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
33 local st = require "util.stanza";
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
34
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
35 module "rostermanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
36
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
37 function add_to_roster(session, jid, item)
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
38 if session.roster then
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
39 local old_item = session.roster[jid];
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
40 session.roster[jid] = item;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
41 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
42 return true;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
43 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
44 session.roster[jid] = old_item;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
45 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
46 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
47 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
48 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
49 end
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
50 end
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
51
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
52 function remove_from_roster(session, jid)
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
53 if session.roster then
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
54 local old_item = session.roster[jid];
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
55 session.roster[jid] = nil;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
56 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
57 return true;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
58 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
59 session.roster[jid] = old_item;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
60 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
61 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
62 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
63 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
64 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
65 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
66
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
67 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
68 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
69 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
70 local stanza = st.iq({type="set"});
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
71 stanza:tag("query", {xmlns = "jabber:iq:roster"});
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
72 if item then
132
9a4aa57af367 Roster updates
Waqas Hussain <waqas20@gmail.com>
parents: 114
diff changeset
73 stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name, ask = item.ask});
110
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
74 for group in pairs(item.groups) do
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
75 stanza:tag("group"):text(group):up();
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
76 end
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
77 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
78 stanza:tag("item", {jid = jid, subscription = "remove"});
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
79 end
193
13ac34255c37 mod_roster fix
Waqas Hussain <waqas20@gmail.com>
parents: 183
diff changeset
80 stanza:up(); -- move out from item
13ac34255c37 mod_roster fix
Waqas Hussain <waqas20@gmail.com>
parents: 183
diff changeset
81 stanza:up(); -- move out from stanza
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
82 -- stanza ready
110
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
83 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
84 if session.interested then
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
85 -- FIXME do we need to set stanza.attr.to?
110
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
86 session.send(stanza);
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
87 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
88 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
89 end
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
90 end
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
91
103
ebdb7875443e Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents: 101
diff changeset
92 function load_roster(username, host)
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 176
diff changeset
93 log("debug", "load_roster: asked for: "..username.."@"..host);
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
94 if hosts[host] and hosts[host].sessions[username] then
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
95 local roster = hosts[host].sessions[username].roster;
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
96 if not roster then
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 176
diff changeset
97 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
98 roster = datamanager.load(username, host, "roster") or {};
ebdb7875443e Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents: 101
diff changeset
99 hosts[host].sessions[username].roster = roster;
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
100 end
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
101 return roster;
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
102 end
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
103 -- 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
104 log("debug", "load_roster: loading for offline user: "..username.."@"..host);
171
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
105 return datamanager.load(username, host, "roster") or {};
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
106 end
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
107
103
ebdb7875443e Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents: 101
diff changeset
108 function save_roster(username, host)
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 176
diff changeset
109 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
110 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
110
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
111 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
112 end
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
113 return nil;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
114 end
80
523ac742cc19 Fixed: rostermanager.lua now returns the module
Waqas Hussain <waqas20@gmail.com>
parents: 6
diff changeset
115
171
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
116 function process_inbound_subscription_approval(username, host, jid)
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
117 local roster = load_roster(username, host);
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
118 local item = roster[jid];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
119 if item and item.ask then
171
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
120 if item.subscription == "none" then
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
121 item.subscription = "to";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
122 else -- subscription == from
171
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
123 item.subscription = "both";
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
124 end
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
125 item.ask = nil;
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
126 return datamanager.store(username, host, "roster", roster);
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
127 end
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
128 end
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
129
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
130 function process_inbound_subscription_cancellation(username, host, jid)
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
131 local roster = load_roster(username, host);
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
132 local item = roster[jid];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
133 local changed = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
134 if is_contact_pending_out(username, host, jid) then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
135 item.ask = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
136 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
137 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
138 if item then
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
139 if item.subscription == "to" then
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
140 item.subscription = "none";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
141 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
142 elseif item.subscription == "both" then
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
143 item.subscription = "from";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
144 changed = true;
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
145 end
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
146 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
147 if changed then
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
148 return datamanager.store(username, host, "roster", roster);
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
149 end
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
150 end
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
151
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
152 function process_inbound_unsubscribe(username, host, jid)
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
153 local roster = load_roster(username, host);
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
154 local item = roster[jid];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
155 local changed = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
156 if is_contact_pending_in(username, host, jid) then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
157 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
158 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
159 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
160 if item then
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
161 if item.subscription == "from" then
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
162 item.subscription = "none";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
163 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
164 elseif item.subscription == "both" then
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
165 item.subscription = "to";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
166 changed = true;
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
167 end
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
168 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
169 if changed then
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
170 return datamanager.store(username, host, "roster", roster);
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
171 end
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
172 end
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
173
174
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
174 function is_contact_subscribed(username, host, jid)
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
175 local roster = load_roster(username, host);
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
176 local item = roster[jid];
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
177 return item and (item.subscription == "from" or item.subscription == "both");
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
178 end
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
179
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
180 function is_contact_pending_in(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
181 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
182 return roster.pending and roster.pending[jid];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
183 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
184 function set_contact_pending_in(username, host, jid, pending)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
185 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
186 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
187 if item and (item.subscription == "from" or item.subscription == "both") then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
188 return; -- false
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
189 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
190 if not roster.pending then roster.pending = {}; end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
191 roster.pending[jid] = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
192 return datamanager.store(username, host, "roster", roster);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
193 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
194 function is_contact_pending_out(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
195 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
196 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
197 return item and item.ask;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
198 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
199 function set_contact_pending_out(username, host, jid) -- subscribe
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
200 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
201 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
202 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
203 return true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
204 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
205 if not item then
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 176
diff changeset
206 item = {subscription = "none", groups = {}};
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
207 roster[jid] = item;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
208 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
209 item.ask = "subscribe";
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 176
diff changeset
210 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
211 return datamanager.store(username, host, "roster", roster);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
212 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
213 function unsubscribe(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
214 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
215 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
216 if not item then return false; end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
217 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
218 return true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
219 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
220 item.ask = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
221 if item.subscription == "both" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
222 item.subscription = "from";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
223 elseif item.subscription == "to" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
224 item.subscription = "none";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
225 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
226 return datamanager.store(username, host, "roster", roster);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
227 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
228 function subscribed(username, host, jid)
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 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
231 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
232 if item.subscription == "none" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
233 item.subscription = "from";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
234 else -- subscription == to
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 176
diff changeset
235 item.subscription = "both";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
236 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
237 roster.pending[jid] = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
238 -- TODO maybe remove roster.pending if empty
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
239 return datamanager.store(username, host, "roster", roster);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
240 end -- TODO else implement optional feature pre-approval (ask = subscribed)
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 function unsubscribed(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
243 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
244 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
245 local pending = is_contact_pending_in(username, host, jid);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
246 local changed = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
247 if is_contact_pending_in(username, host, jid) then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
248 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
249 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
250 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
251 if item then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
252 if item.subscription == "from" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
253 item.subscription = "none";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
254 changed = true;
267
6303b7454ad0 Some bugs fixed
Waqas Hussain <waqas20@gmail.com>
parents: 193
diff changeset
255 elseif item.subscription == "both" then
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
256 item.subscription = "to";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
257 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
258 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
259 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
260 if changed then
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 function process_outbound_subscription_request(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
266 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
267 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
268 if item and (item.subscription == "none" or item.subscription == "from") then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
269 item.ask = "subscribe";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
270 return datamanager.store(username, host, "roster", roster);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
271 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
272 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
273
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
274 --[[function process_outbound_subscription_approval(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
275 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
276 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
277 if item and (item.subscription == "none" or item.subscription == "from" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
278 item.ask = "subscribe";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
279 return datamanager.store(username, host, "roster", roster);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
280 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
281 end]]
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
282
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
283
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
284
80
523ac742cc19 Fixed: rostermanager.lua now returns the module
Waqas Hussain <waqas20@gmail.com>
parents: 6
diff changeset
285 return _M;