General fixes for s2s, to make it more robust (I hope), sending data to remote hosts...
[prosody.git] / plugins / mod_vcard.lua
1
2 require "util.datamanager"
3 local datamanager = datamanager;
4
5 local st = require "util.stanza"
6 local send = require "core.sessionmanager".send_to_session
7 local t_concat, t_insert = table.concat, table.insert;
8
9 require "util.jid"
10 local jid_split = jid.split;
11
12 add_iq_handler("c2s", "vcard-temp", 
13                 function (session, stanza)
14                         if stanza.tags[1].name == "vCard" then
15                                 local to = stanza.attr.to;
16                                 if stanza.attr.type == "get" then
17                                         local vCard;
18                                         if to then
19                                                 local node, host = jid_split(to);
20                                                 if hosts[host] and hosts[host].type == "local" then
21                                                         vCard = st.deserialize(datamanager.load(node, host, "vCard")); -- load vCard for user or server
22                                                 end
23                                         else
24                                                 vCard = st.deserialize(datamanager.load(session.username, session.host, "vCard"));-- load user's own vCard
25                                         end
26                                         if vCard then
27                                                 send(session, st.reply(stanza):add_child(vCard)); -- send vCard!
28                                         else
29                                                 send(session, st.error_reply(stanza, "cancel", "item-not-found"));
30                                         end
31                                 elseif stanza.attr.type == "set" then
32                                         if not to or to == session.username.."@"..session.host then
33                                                 if datamanager.store(session.username, session.host, "vCard", st.preserialize(stanza.tags[1])) then
34                                                         send(session, st.reply(stanza));
35                                                 else
36                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
37                                                         send(session, st.error_reply(stanza, "wait", "internal-server-error"));
38                                                 end
39                                         else
40                                                 send(session, st.error_reply(stanza, "auth", "forbidden"));
41                                         end
42                                 end
43                                 return true;
44                         end
45                 end);
46
47 add_event_hook("stream-features", 
48                                         function (session, features)                                                                                            
49                                                 if session.type == "c2s" then
50                                                         t_insert(features, "<feature var='vcard-temp'/>");
51                                                 end
52                                         end);