Comparison

plugins/mod_vcard.lua @ 2002:fa71261d8a15

mod_vcard: Refactoring and cleanup.
author Waqas Hussain <waqas20@gmail.com>
date Sun, 18 Oct 2009 07:30:58 +0500
parent 2001:2f73fe2b3edd
child 2003:b7429073f73f
comparison
equal deleted inserted replaced
2001:2f73fe2b3edd 2002:fa71261d8a15
3 -- Copyright (C) 2008-2009 Waqas Hussain 3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8
9
10 8
11 local hosts = _G.hosts; 9 local hosts = _G.hosts;
12 local datamanager = require "util.datamanager" 10 local datamanager = require "util.datamanager"
13 11
14 local st = require "util.stanza" 12 local st = require "util.stanza"
16 14
17 local jid = require "util.jid" 15 local jid = require "util.jid"
18 local jid_split = jid.split; 16 local jid_split = jid.split;
19 17
20 local xmlns_vcard = "vcard-temp"; 18 local xmlns_vcard = "vcard-temp";
21
22 module:add_feature(xmlns_vcard); 19 module:add_feature(xmlns_vcard);
23 20
24 function handle_vcard(event) 21 function handle_vcard(event)
25 local session, stanza = event.origin, event.stanza; 22 local session, stanza = event.origin, event.stanza;
26 if stanza.tags[1].name == "vCard" then 23 local to = stanza.attr.to;
27 local to = stanza.attr.to; 24 if stanza.attr.type == "get" then
28 if stanza.attr.type == "get" then 25 local vCard;
29 local vCard; 26 if to then
30 if to then 27 local node, host = jid_split(to);
31 local node, host = jid_split(to); 28 vCard = st.deserialize(datamanager.load(node, host, "vcard")); -- load vCard for user or server
32 if hosts[host] and hosts[host].type == "local" then 29 else
33 vCard = st.deserialize(datamanager.load(node, host, "vcard")); -- load vCard for user or server 30 vCard = st.deserialize(datamanager.load(session.username, session.host, "vcard"));-- load user's own vCard
34 end 31 end
32 if vCard then
33 session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
34 else
35 session.send(st.error_reply(stanza, "cancel", "item-not-found"));
36 end
37 else
38 if not to then
39 if datamanager.store(session.username, session.host, "vcard", st.preserialize(stanza.tags[1])) then
40 session.send(st.reply(stanza));
35 else 41 else
36 vCard = st.deserialize(datamanager.load(session.username, session.host, "vcard"));-- load user's own vCard 42 -- TODO unable to write file, file may be locked, etc, what's the correct error?
43 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
37 end 44 end
38 if vCard then 45 else
39 session.send(st.reply(stanza):add_child(vCard)); -- send vCard! 46 session.send(st.error_reply(stanza, "auth", "forbidden"));
40 else
41 session.send(st.error_reply(stanza, "cancel", "item-not-found"));
42 end
43 elseif stanza.attr.type == "set" then
44 if not to then
45 if datamanager.store(session.username, session.host, "vcard", st.preserialize(stanza.tags[1])) then
46 session.send(st.reply(stanza));
47 else
48 -- TODO unable to write file, file may be locked, etc, what's the correct error?
49 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
50 end
51 else
52 session.send(st.error_reply(stanza, "auth", "forbidden"));
53 end
54 end 47 end
55 return true;
56 end 48 end
49 return true;
57 end 50 end
58 51
59 --module:add_iq_handler({"c2s", "s2sin", "component"}, xmlns_vcard, handle_vcard);
60 module:hook("iq/bare/vcard-temp:vCard", handle_vcard); 52 module:hook("iq/bare/vcard-temp:vCard", handle_vcard);
61 module:hook("iq/host/vcard-temp:vCard", handle_vcard); 53 module:hook("iq/host/vcard-temp:vCard", handle_vcard);
62 54
63 -- COMPAT: https://support.process-one.net/browse/EJAB-1045 55 -- COMPAT: https://support.process-one.net/browse/EJAB-1045
64 if module:get_option("vcard_compatibility") then 56 if module:get_option("vcard_compatibility") then
65 module:hook("iq/full", function (data) 57 module:hook("iq/full", function(data)
66 local stanza = data.stanza; 58 local stanza = data.stanza;
67 if stanza.attr.type == "get" and stanza.tags[1] 59 local payload = stanza.tags[1];
68 and stanza.tags[1].attr.xmlns == xmlns_vcard then 60 if stanza.attr.type == "get" or stanza.attr.type == "set" and payload.name == "vCard" and payload.attr.xmlns == xmlns_vcard then
69 return handle_vcard(data); 61 return handle_vcard(data);
70 end 62 end
71 end, 1); 63 end, 1);
72 end 64 end
73 65
74 local feature_vcard_attr = { var=xmlns_vcard }; 66 local feature_vcard = st.stanza("feature", { var = xmlns_vcard });
75 module:add_event_hook("stream-features", 67 module:add_event_hook("stream-features", function(session, features)
76 function (session, features) 68 if session.type == "c2s" then
77 if session.type == "c2s" then 69 features:add_child(feature_vcard);
78 features:tag("feature", feature_vcard_attr):up(); 70 end
79 end 71 end);
80 end);