Annotate

core/rostermanager.lua @ 107:8d8debda3df2

Added: Roster manipulation functions to core.rostermanager
author Waqas Hussain <waqas20@gmail.com>
date Fri, 17 Oct 2008 22:19:26 +0500
parent 103:ebdb7875443e
child 110:bb5ac5976a97
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;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
10
103
ebdb7875443e Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents: 101
diff changeset
11 local hosts = hosts;
ebdb7875443e Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents: 101
diff changeset
12
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
13 require "util.datamanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
14
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
15 local datamanager = datamanager;
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
16 local st = require "util.stanza";
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
17 local send = require "core.sessionmanager".send_to_session;
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
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
21 --[[function getroster(username, host)
3
f674eb704134 "Shared roster" - well, kind of :)
matthew
parents: 0
diff changeset
22 return {
f674eb704134 "Shared roster" - well, kind of :)
matthew
parents: 0
diff changeset
23 ["mattj@localhost"] = true,
f674eb704134 "Shared roster" - well, kind of :)
matthew
parents: 0
diff changeset
24 ["tobias@getjabber.ath.cx"] = true,
f674eb704134 "Shared roster" - well, kind of :)
matthew
parents: 0
diff changeset
25 ["waqas@getjabber.ath.cx"] = true,
f674eb704134 "Shared roster" - well, kind of :)
matthew
parents: 0
diff changeset
26 ["thorns@getjabber.ath.cx"] = true,
f674eb704134 "Shared roster" - well, kind of :)
matthew
parents: 0
diff changeset
27 ["idw@getjabber.ath.cx"] = true,
f674eb704134 "Shared roster" - well, kind of :)
matthew
parents: 0
diff changeset
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});
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
69 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
70 stanza:tag("item", {jid = jid, subscription = "remove"});
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 for group in item.groups do
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
73 stanza:tag("group"):text(group):up();
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
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
78 for _, session in ipairs(hosts[host].sessions[username].sessions) do
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
79 if session.full_jid then
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?
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
81 send(session, stanza);
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
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
101 return datamanager.save(username, host, "roster", hosts[host].sessions[username].roster);
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
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
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;