0.1 -> 0.2
[prosody.git] / plugins / mod_vcard.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
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.
9 -- 
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.
14 -- 
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.
18 --
19
20
21
22 require "util.datamanager"
23 local datamanager = datamanager;
24
25 local st = require "util.stanza"
26 local t_concat, t_insert = table.concat, table.insert;
27
28 require "util.jid"
29 local jid_split = jid.split;
30
31 module:add_feature("vcard-temp");
32
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
38                                         local vCard;
39                                         if to 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
43                                                 end
44                                         else
45                                                 vCard = st.deserialize(datamanager.load(session.username, session.host, "vcard"));-- load user's own vCard
46                                         end
47                                         if vCard then
48                                                 session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
49                                         else
50                                                 session.send(st.error_reply(stanza, "cancel", "item-not-found"));
51                                         end
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));
56                                                 else
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"));
59                                                 end
60                                         else
61                                                 session.send(st.error_reply(stanza, "auth", "forbidden"));
62                                         end
63                                 end
64                                 return true;
65                         end
66                 end);
67
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();
73                                                 end
74                                         end);