Merge 0.10->trunk
[prosody.git] / plugins / mod_vcard.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local st = require "util.stanza"
10 local jid_split = require "util.jid".split;
11
12 local vcards = module:open_store();
13
14 module:add_feature("vcard-temp");
15
16 local function handle_vcard(event)
17         local session, stanza = event.origin, event.stanza;
18         local to = stanza.attr.to;
19         if stanza.attr.type == "get" then
20                 local vCard;
21                 if to then
22                         local node, host = jid_split(to);
23                         vCard = st.deserialize(vcards:get(node)); -- load vCard for user or server
24                 else
25                         vCard = st.deserialize(vcards:get(session.username));-- load user's own vCard
26                 end
27                 if vCard then
28                         session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
29                 else
30                         session.send(st.error_reply(stanza, "cancel", "item-not-found"));
31                 end
32         else
33                 if not to then
34                         if vcards:set(session.username, st.preserialize(stanza.tags[1])) then
35                                 session.send(st.reply(stanza));
36                         else
37                                 -- TODO unable to write file, file may be locked, etc, what's the correct error?
38                                 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
39                         end
40                 else
41                         session.send(st.error_reply(stanza, "auth", "forbidden"));
42                 end
43         end
44         return true;
45 end
46
47 module:hook("iq/bare/vcard-temp:vCard", handle_vcard);
48 module:hook("iq/host/vcard-temp:vCard", handle_vcard);
49
50 -- COMPAT w/0.8
51 if module:get_option("vcard_compatibility") ~= nil then
52         module:log("error", "The vcard_compatibility option has been removed, see"..
53                 "mod_compat_vcard in prosody-modules if you still need this.");
54 end