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