Comparison

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
comparison
equal deleted inserted replaced
106:f2a3d204a76a 107:8d8debda3df2
11 local hosts = hosts; 11 local hosts = hosts;
12 12
13 require "util.datamanager" 13 require "util.datamanager"
14 14
15 local datamanager = datamanager; 15 local datamanager = datamanager;
16 local st = require "util.stanza";
17 local send = require "core.sessionmanager".send_to_session;
16 18
17 module "rostermanager" 19 module "rostermanager"
18 20
19 --[[function getroster(username, host) 21 --[[function getroster(username, host)
20 return { 22 return {
25 ["idw@getjabber.ath.cx"] = true, 27 ["idw@getjabber.ath.cx"] = true,
26 } 28 }
27 --return datamanager.load(username, host, "roster") or {}; 29 --return datamanager.load(username, host, "roster") or {};
28 end]] 30 end]]
29 31
30 function add_to_roster(roster, jid, item) 32 function add_to_roster(session, jid, item)
31 roster[jid] = item; 33 if session.roster then
32 -- TODO implement 34 local old_item = session.roster[jid];
35 session.roster[jid] = item;
36 if save_roster(session.username, session.host) then
37 return true;
38 else
39 session.roster[jid] = old_item;
40 return nil, "wait", "internal-server-error", "Unable to save roster";
41 end
42 else
43 return nil, "auth", "not-authorized", "Session's roster not loaded";
44 end
33 end 45 end
34 46
35 function remove_from_roster(roster, jid) 47 function remove_from_roster(session, jid)
36 roster[jid] = nil; 48 if session.roster then
37 -- TODO implement 49 local old_item = session.roster[jid];
50 session.roster[jid] = nil;
51 if save_roster(session.username, session.host) then
52 return true;
53 else
54 session.roster[jid] = old_item;
55 return nil, "wait", "internal-server-error", "Unable to save roster";
56 end
57 else
58 return nil, "auth", "not-authorized", "Session's roster not loaded";
59 end
60 end
61
62 function roster_push(username, host, jid)
63 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
64 local item = hosts[host].sessions[username].roster[jid];
65 local stanza = st.iq({type="set"});
66 stanza:tag("query", {xmlns = "jabber:iq:roster"});
67 if item then
68 stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name});
69 else
70 stanza:tag("item", {jid = jid, subscription = "remove"});
71 end
72 for group in item.groups do
73 stanza:tag("group"):text(group):up();
74 end
75 stanza:up();
76 stanza:up();
77 -- stanza ready
78 for _, session in ipairs(hosts[host].sessions[username].sessions) do
79 if session.full_jid then
80 -- FIXME do we need to set stanza.attr.to?
81 send(session, stanza);
82 end
83 end
84 end
38 end 85 end
39 86
40 function load_roster(username, host) 87 function load_roster(username, host)
41 if hosts[host] and hosts[host].sessions[username] then 88 if hosts[host] and hosts[host].sessions[username] then
42 local roster = hosts[host].sessions[username].roster; 89 local roster = hosts[host].sessions[username].roster;
44 roster = datamanager.load(username, host, "roster") or {}; 91 roster = datamanager.load(username, host, "roster") or {};
45 hosts[host].sessions[username].roster = roster; 92 hosts[host].sessions[username].roster = roster;
46 end 93 end
47 return roster; 94 return roster;
48 end 95 end
49 error("Attempt to load roster for non-loaded user"); --return nil; 96 -- Attempt to load roster for non-loaded user
50 end 97 end
51 98
52 function save_roster(username, host) 99 function save_roster(username, host)
53 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then 100 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
54 return datamanager.save(username, host, "roster", hosts[host].sessions[username].roster); 101 return datamanager.save(username, host, "roster", hosts[host].sessions[username].roster);