mod_ping: Convert from Windows line endings
[prosody.git] / plugins / mod_vcard.lua
1 -- Prosody IM v0.4
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 module:add_feature("vcard-temp");
21
22 module:add_iq_handler({"c2s", "s2sin"}, "vcard-temp", 
23                 function (session, stanza)
24                         if stanza.tags[1].name == "vCard" then
25                                 local to = stanza.attr.to;
26                                 if stanza.attr.type == "get" then
27                                         local vCard;
28                                         if to then
29                                                 local node, host = jid_split(to);
30                                                 if hosts[host] and hosts[host].type == "local" then
31                                                         vCard = st.deserialize(datamanager.load(node, host, "vcard")); -- load vCard for user or server
32                                                 end
33                                         else
34                                                 vCard = st.deserialize(datamanager.load(session.username, session.host, "vcard"));-- load user's own vCard
35                                         end
36                                         if vCard then
37                                                 session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
38                                         else
39                                                 session.send(st.error_reply(stanza, "cancel", "item-not-found"));
40                                         end
41                                 elseif stanza.attr.type == "set" then
42                                         if not to or to == session.username.."@"..session.host then
43                                                 if datamanager.store(session.username, session.host, "vcard", st.preserialize(stanza.tags[1])) then
44                                                         session.send(st.reply(stanza));
45                                                 else
46                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
47                                                         session.send(st.error_reply(stanza, "wait", "internal-server-error"));
48                                                 end
49                                         else
50                                                 session.send(st.error_reply(stanza, "auth", "forbidden"));
51                                         end
52                                 end
53                                 return true;
54                         end
55                 end);
56
57 local feature_vcard_attr = { var='vcard-temp' };
58 module:add_event_hook("stream-features",
59                                         function (session, features)
60                                                 if session.type == "c2s" then
61                                                         features:tag("feature", feature_vcard_attr):up();
62                                                 end
63                                         end);