MUC: Fixed an incorrect error message.
[prosody.git] / plugins / mod_vcard.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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
10
11 local hosts = _G.hosts;
12 local datamanager = require "util.datamanager"
13
14 local st = require "util.stanza"
15 local t_concat, t_insert = table.concat, table.insert;
16
17 local jid = require "util.jid"
18 local jid_split = jid.split;
19
20 local xmlns_vcard = "vcard-temp";
21
22 module:add_feature(xmlns_vcard);
23
24 function handle_vcard(session, stanza)
25         if stanza.tags[1].name == "vCard" then
26                 local to = stanza.attr.to;
27                 if stanza.attr.type == "get" then
28                         local vCard;
29                         if to then
30                                 local node, host = jid_split(to);
31                                 if hosts[host] and hosts[host].type == "local" then
32                                         vCard = st.deserialize(datamanager.load(node, host, "vcard")); -- load vCard for user or server
33                                 end
34                         else
35                                 vCard = st.deserialize(datamanager.load(session.username, session.host, "vcard"));-- load user's own vCard
36                         end
37                         if vCard then
38                                 session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
39                         else
40                                 session.send(st.error_reply(stanza, "cancel", "item-not-found"));
41                         end
42                 elseif stanza.attr.type == "set" then
43                         if not to or to == session.username.."@"..session.host then
44                                 if datamanager.store(session.username, session.host, "vcard", st.preserialize(stanza.tags[1])) then
45                                         session.send(st.reply(stanza));
46                                 else
47                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
48                                         session.send(st.error_reply(stanza, "wait", "internal-server-error"));
49                                 end
50                         else
51                                 session.send(st.error_reply(stanza, "auth", "forbidden"));
52                         end
53                 end
54                 return true;
55         end
56 end
57
58 module:add_iq_handler({"c2s", "s2sin", "component"}, xmlns_vcard, handle_vcard);
59
60 -- COMPAT: https://support.process-one.net/browse/EJAB-1045
61 if module:get_option("vcard_compatibility") then
62         module:hook("iq/full", function (data)
63                 local stanza = data.stanza;
64                 if stanza.attr.type == "get" and stanza.tags[1]
65                         and stanza.tags[1].attr.xmlns == xmlns_vcard then
66                                 return handle_vcard(data.origin, stanza);
67                 end
68         end, 1);
69 end
70
71 local feature_vcard_attr = { var=xmlns_vcard };
72 module:add_event_hook("stream-features",
73                                         function (session, features)
74                                                 if session.type == "c2s" then
75                                                         features:tag("feature", feature_vcard_attr):up();
76                                                 end
77                                         end);