Bumper commit for the new modulemanager API \o/ Updates all the modules, though some...
[prosody.git] / plugins / mod_vcard.lua
1
2 require "util.datamanager"
3 local datamanager = datamanager;
4
5 local st = require "util.stanza"
6 local t_concat, t_insert = table.concat, table.insert;
7
8 require "util.jid"
9 local jid_split = jid.split;
10
11 require "core.discomanager".set("vcard", "vcard-temp");
12
13 module:add_iq_handler({"c2s", "s2sin"}, "vcard-temp", 
14                 function (session, stanza)
15                         if stanza.tags[1].name == "vCard" then
16                                 local to = stanza.attr.to;
17                                 if stanza.attr.type == "get" then
18                                         local vCard;
19                                         if to then
20                                                 local node, host = jid_split(to);
21                                                 if hosts[host] and hosts[host].type == "local" then
22                                                         vCard = st.deserialize(datamanager.load(node, host, "vCard")); -- load vCard for user or server
23                                                 end
24                                         else
25                                                 vCard = st.deserialize(datamanager.load(session.username, session.host, "vCard"));-- 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                                 elseif stanza.attr.type == "set" then
33                                         if not to or to == session.username.."@"..session.host then
34                                                 if datamanager.store(session.username, session.host, "vCard", 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                 end);
47
48 local feature_vcard_attr = { var='vcard-temp' };
49 module:add_event_hook("stream-features", 
50                                         function (session, features)                                                                                            
51                                                 if session.type == "c2s" then
52                                                         features:tag("feature", feature_vcard_attr):up();
53                                                 end
54                                         end);