2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 require "util.datamanager"
23 local datamanager = datamanager;
25 local st = require "util.stanza"
26 local t_concat, t_insert = table.concat, table.insert;
29 local jid_split = jid.split;
31 module:add_feature("vcard-temp");
33 module:add_iq_handler({"c2s", "s2sin"}, "vcard-temp",
34 function (session, stanza)
35 if stanza.tags[1].name == "vCard" then
36 local to = stanza.attr.to;
37 if stanza.attr.type == "get" then
40 local node, host = jid_split(to);
41 if hosts[host] and hosts[host].type == "local" then
42 vCard = st.deserialize(datamanager.load(node, host, "vCard")); -- load vCard for user or server
45 vCard = st.deserialize(datamanager.load(session.username, session.host, "vCard"));-- load user's own vCard
48 session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
50 session.send(st.error_reply(stanza, "cancel", "item-not-found"));
52 elseif stanza.attr.type == "set" then
53 if not to or to == session.username.."@"..session.host then
54 if datamanager.store(session.username, session.host, "vCard", st.preserialize(stanza.tags[1])) then
55 session.send(st.reply(stanza));
57 -- TODO unable to write file, file may be locked, etc, what's the correct error?
58 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
61 session.send(st.error_reply(stanza, "auth", "forbidden"));
68 local feature_vcard_attr = { var='vcard-temp' };
69 module:add_event_hook("stream-features",
70 function (session, features)
71 if session.type == "c2s" then
72 features:tag("feature", feature_vcard_attr):up();