Annotate

core/rostermanager.lua @ 176:e5cd2a03891d

Outbound presence subscription
author Waqas Hussain <waqas20@gmail.com>
date Sat, 25 Oct 2008 21:16:08 +0500
parent 174:f9aff1fc7e99
child 177:606c433955e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
1
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
2 local mainlog = log;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
3 local function log(type, message)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
4 mainlog(type, "rostermanager", message);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
5 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
6
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
7 local setmetatable = setmetatable;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
8 local format = string.format;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
9 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
110
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
10 local pairs, ipairs = pairs, ipairs;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
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
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
14 require "util.datamanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
15
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
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
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
18
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
19 module "rostermanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
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
9a4aa57af367 Roster updates
Waqas Hussain <waqas20@gmail.com>
parents: 114
diff changeset
57 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
58 for group in pairs(item.groups) do
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
59 stanza:tag("group"):text(group):up();
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
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
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
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
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
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
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
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
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
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];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
99 if item and item.ask then
171
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";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
102 else -- subscription == from
171
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
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
110 function process_inbound_subscription_cancellation(username, host, jid)
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
111 local roster = load_roster(username, host);
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
112 local item = roster[jid];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
113 local changed = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
114 if is_contact_pending_out(username, host, jid) then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
115 item.ask = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
116 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
117 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
118 if item then
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
119 if item.subscription == "to" then
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
120 item.subscription = "none";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
121 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
122 elseif item.subscription == "both" then
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
123 item.subscription = "from";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
124 changed = true;
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
125 end
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
126 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
127 if changed then
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
128 return datamanager.store(username, host, "roster", roster);
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
129 end
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
130 end
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
131
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
132 function process_inbound_unsubscribe(username, host, jid)
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
133 local roster = load_roster(username, host);
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
134 local item = roster[jid];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
135 local changed = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
136 if is_contact_pending_in(username, host, jid) then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
137 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
138 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
139 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
140 if item then
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
141 if item.subscription == "from" then
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
142 item.subscription = "none";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
143 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
144 elseif item.subscription == "both" then
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
145 item.subscription = "to";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
146 changed = true;
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
147 end
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
148 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
149 if changed then
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
150 return datamanager.store(username, host, "roster", roster);
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
151 end
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
152 end
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
153
174
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
154 function is_contact_subscribed(username, host, jid)
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
155 local roster = load_roster(username, host);
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
156 local item = roster[jid];
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
157 return item and (item.subscription == "from" or item.subscription == "both");
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
158 end
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
159
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
160 function is_contact_pending_in(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
161 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
162 return roster.pending or roster.pending[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
163 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
164 function set_contact_pending_in(username, host, jid, pending)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
165 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
166 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
167 if item and (item.subscription == "from" or item.subscription == "both") then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
168 return; -- false
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
169 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
170 if not roster.pending then roster.pending = {}; end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
171 roster.pending[jid] = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
172 return datamanager.store(username, host, "roster", roster);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
173 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
174 function is_contact_pending_out(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
175 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
176 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
177 return item and item.ask;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
178 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
179 function set_contact_pending_out(username, host, jid) -- subscribe
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
180 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
181 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
182 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
183 return true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
184 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
185 if not item then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
186 item = {subscription = "none"};
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
187 roster[jid] = item;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
188 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
189 item.ask = "subscribe";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
190 return datamanager.store(username, host, "roster", roster);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
191 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
192 function unsubscribe(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
193 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
194 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
195 if not item then return false; end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
196 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
197 return true;
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 item.ask = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
200 if item.subscription == "both" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
201 item.subscription = "from";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
202 elseif item.subscription == "to" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
203 item.subscription = "none";
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 return datamanager.store(username, host, "roster", roster);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
206 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
207 function subscribed(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
208 if is_contact_pending_in(username, host, jid) then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
209 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
210 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
211 if item.subscription == "none" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
212 item.subscription = "from";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
213 else -- subscription == to
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
214 item.subsctiption = "both";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
215 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
216 roster.pending[jid] = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
217 -- TODO maybe remove roster.pending if empty
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
218 return datamanager.store(username, host, "roster", roster);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
219 end -- TODO else implement optional feature pre-approval (ask = subscribed)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
220 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
221 function unsubscribed(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
222 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
223 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
224 local pending = is_contact_pending_in(username, host, jid);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
225 local changed = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
226 if is_contact_pending_in(username, host, jid) then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
227 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
228 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
229 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
230 if item then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
231 if item.subscription == "from" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
232 item.subscription = "none";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
233 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
234 elseif item.subscription == both then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
235 item.subscription = "to";
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 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
238 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
239 if changed then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
240 return datamanager.store(username, host, "roster", roster);
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 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
243
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
244 function process_outbound_subscription_request(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
245 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
246 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
247 if item and (item.subscription == "none" or item.subscription == "from") then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
248 item.ask = "subscribe";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
249 return datamanager.store(username, host, "roster", roster);
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 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
252
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
253 --[[function process_outbound_subscription_approval(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
254 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
255 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
256 if item and (item.subscription == "none" or item.subscription == "from" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
257 item.ask = "subscribe";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
258 return datamanager.store(username, host, "roster", roster);
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 end]]
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
261
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
262
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
263
80
523ac742cc19 Fixed: rostermanager.lua now returns the module
Waqas Hussain <waqas20@gmail.com>
parents: 6
diff changeset
264 return _M;