tools/ejabberd2prosody: Fixed private storage export
[prosody.git] / core / rostermanager.lua
index 9a2033375e89b8478f7e0c4c895313d0ae9b97a2..867add2c1889b4b268f899f9f7c4fd93d473a590 100644 (file)
@@ -1,13 +1,21 @@
+-- Prosody IM v0.4
+-- Copyright (C) 2008-2009 Matthew Wild
+-- Copyright (C) 2008-2009 Waqas Hussain
+-- 
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
 
-local mainlog = log;
-local function log(type, message)
-       mainlog(type, "rostermanager", message);
-end
+
+
+
+local log = require "util.logger".init("rostermanager");
 
 local setmetatable = setmetatable;
 local format = string.format;
 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
 local pairs, ipairs = pairs, ipairs;
+local tostring = tostring;
 
 local hosts = hosts;
 
@@ -47,10 +55,11 @@ function remove_from_roster(session, jid)
 end
 
 function roster_push(username, host, jid)
-       if jid ~= "pending" and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
+       local roster = jid and jid ~= "pending" and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
+       if roster then
                local item = hosts[host].sessions[username].roster[jid];
                local stanza = st.iq({type="set"});
-               stanza:tag("query", {xmlns = "jabber:iq:roster"});
+               stanza:tag("query", {xmlns = "jabber:iq:roster", ver = tostring(roster[false].version or "1")  });
                if item then
                        stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name, ask = item.ask});
                        for group in pairs(item.groups) do
@@ -78,6 +87,7 @@ function load_roster(username, host)
                if not roster then
                        log("debug", "load_roster: loading for new user: "..username.."@"..host);
                        roster = datamanager.load(username, host, "roster") or {};
+                       if not roster[false] then roster[false] = { }; end
                        hosts[host].sessions[username].roster = roster;
                end
                return roster;
@@ -90,8 +100,11 @@ end
 function save_roster(username, host)
        log("debug", "save_roster: saving roster for "..username.."@"..host);
        if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
+               local roster = hosts[host].sessions[username].roster;
+               roster[false].version = (roster[false].version or 1) + 1;
                return datamanager.store(username, host, "roster", hosts[host].sessions[username].roster);
        end
+       log("warn", "save_roster: user had no roster to save");
        return nil;
 end
 
@@ -211,6 +224,10 @@ function subscribed(username, host, jid)
        if is_contact_pending_in(username, host, jid) then
                local roster = load_roster(username, host);
                local item = roster[jid];
+               if not item then -- FIXME should roster item be auto-created?
+                       item = {subscription = "none", groups = {}};
+                       roster[jid] = item;
+               end
                if item.subscription == "none" then
                        item.subscription = "from";
                else -- subscription == to
@@ -234,7 +251,7 @@ function unsubscribed(username, host, jid)
                if item.subscription == "from" then
                        item.subscription = "none";
                        changed = true;
-               elseif item.subscription == both then
+               elseif item.subscription == "both" then
                        item.subscription = "to";
                        changed = true;
                end
@@ -264,4 +281,4 @@ end]]
 
 
 
-return _M;
\ No newline at end of file
+return _M;