fb7382c20762665052aca0e58ef91fe684f281ee
[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 add_iq_handler({"c2s", "s2sin"}, "vcard-temp", 
12                 function (session, stanza)
13                         if stanza.tags[1].name == "vCard" then
14                                 local to = stanza.attr.to;
15                                 if stanza.attr.type == "get" then
16                                         local vCard;
17                                         if to then
18                                                 local node, host = jid_split(to);
19                                                 if hosts[host] and hosts[host].type == "local" then
20                                                         vCard = st.deserialize(datamanager.load(node, host, "vCard")); -- load vCard for user or server
21                                                 end
22                                         else
23                                                 vCard = st.deserialize(datamanager.load(session.username, session.host, "vCard"));-- load user's own vCard
24                                         end
25                                         if vCard then
26                                                 session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
27                                         else
28                                                 session.send(st.error_reply(stanza, "cancel", "item-not-found"));
29                                         end
30                                 elseif stanza.attr.type == "set" then
31                                         if not to or to == session.username.."@"..session.host then
32                                                 if datamanager.store(session.username, session.host, "vCard", st.preserialize(stanza.tags[1])) then
33                                                         session.send(st.reply(stanza));
34                                                 else
35                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
36                                                         session.send(st.error_reply(stanza, "wait", "internal-server-error"));
37                                                 end
38                                         else
39                                                 session.send(st.error_reply(stanza, "auth", "forbidden"));
40                                         end
41                                 end
42                                 return true;
43                         end
44                 end);
45
46 add_event_hook("stream-features", 
47                                         function (session, features)                                                                                            
48                                                 if session.type == "c2s" then
49                                                         t_insert(features, "<feature var='vcard-temp'/>");
50                                                 end
51                                         end);