plugins/mod_vcard.lua
author Waqas Hussain <waqas20@gmail.com>
Sun, 18 Oct 2009 07:05:56 +0500
changeset 2001 2f73fe2b3edd
parent 2000 c31aa53348b8
child 2002 fa71261d8a15
permissions -rw-r--r--
mod_vcard: Moved completely to new event based hooks.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
     1
-- Prosody IM
760
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
     2
-- Copyright (C) 2008-2009 Matthew Wild
90ce865eebd8 Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents: 759
diff changeset
     3
-- Copyright (C) 2008-2009 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     4
-- 
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     7
--
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 438
diff changeset
     9
86
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    10
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    11
local hosts = _G.hosts;
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    12
local datamanager = require "util.datamanager"
86
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    13
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    14
local st = require "util.stanza"
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    15
local t_concat, t_insert = table.concat, table.insert;
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    16
1042
a3d77353c18a mod_*: Fix a load of global accesses
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
    17
local jid = require "util.jid"
86
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    18
local jid_split = jid.split;
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    19
1956
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    20
local xmlns_vcard = "vcard-temp";
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    21
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    22
module:add_feature(xmlns_vcard);
421
63be85693710 Modules now sending disco replies
Waqas Hussain <waqas20@gmail.com>
parents: 357
diff changeset
    23
2001
2f73fe2b3edd mod_vcard: Moved completely to new event based hooks.
Waqas Hussain <waqas20@gmail.com>
parents: 2000
diff changeset
    24
function handle_vcard(event)
2f73fe2b3edd mod_vcard: Moved completely to new event based hooks.
Waqas Hussain <waqas20@gmail.com>
parents: 2000
diff changeset
    25
	local session, stanza = event.origin, event.stanza;
1956
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    26
	if stanza.tags[1].name == "vCard" then
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    27
		local to = stanza.attr.to;
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    28
		if stanza.attr.type == "get" then
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    29
			local vCard;
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    30
			if to then
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    31
				local node, host = jid_split(to);
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    32
				if hosts[host] and hosts[host].type == "local" then
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    33
					vCard = st.deserialize(datamanager.load(node, host, "vcard")); -- load vCard for user or server
86
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    34
				end
1956
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    35
			else
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    36
				vCard = st.deserialize(datamanager.load(session.username, session.host, "vcard"));-- load user's own vCard
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    37
			end
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    38
			if vCard then
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    39
				session.send(st.reply(stanza):add_child(vCard)); -- send vCard!
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    40
			else
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    41
				session.send(st.error_reply(stanza, "cancel", "item-not-found"));
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    42
			end
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    43
		elseif stanza.attr.type == "set" then
2000
c31aa53348b8 mod_vcard: Fixed traceback on attempt to set vcards by non-c2s sessions.
Waqas Hussain <waqas20@gmail.com>
parents: 1957
diff changeset
    44
			if not to then
1956
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    45
				if datamanager.store(session.username, session.host, "vcard", st.preserialize(stanza.tags[1])) then
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    46
					session.send(st.reply(stanza));
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    47
				else
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    48
					-- TODO unable to write file, file may be locked, etc, what's the correct error?
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    49
					session.send(st.error_reply(stanza, "wait", "internal-server-error"));
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    50
				end
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    51
			else
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    52
				session.send(st.error_reply(stanza, "auth", "forbidden"));
86
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    53
			end
1956
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    54
		end
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    55
		return true;
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    56
	end
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    57
end
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    58
2001
2f73fe2b3edd mod_vcard: Moved completely to new event based hooks.
Waqas Hussain <waqas20@gmail.com>
parents: 2000
diff changeset
    59
--module:add_iq_handler({"c2s", "s2sin", "component"}, xmlns_vcard, handle_vcard);
2f73fe2b3edd mod_vcard: Moved completely to new event based hooks.
Waqas Hussain <waqas20@gmail.com>
parents: 2000
diff changeset
    60
module:hook("iq/bare/vcard-temp:vCard", handle_vcard);
2f73fe2b3edd mod_vcard: Moved completely to new event based hooks.
Waqas Hussain <waqas20@gmail.com>
parents: 2000
diff changeset
    61
module:hook("iq/host/vcard-temp:vCard", handle_vcard);
86
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    62
1956
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    63
-- COMPAT: https://support.process-one.net/browse/EJAB-1045
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    64
if module:get_option("vcard_compatibility") then
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    65
	module:hook("iq/full", function (data)
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    66
		local stanza = data.stanza;
1957
5856b2dcf81e mod_vcard: Hide me from the trailing-whitespace police
Matthew Wild <mwild1@gmail.com>
parents: 1956
diff changeset
    67
		if stanza.attr.type == "get" and stanza.tags[1]
1956
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    68
			and stanza.tags[1].attr.xmlns == xmlns_vcard then
2001
2f73fe2b3edd mod_vcard: Moved completely to new event based hooks.
Waqas Hussain <waqas20@gmail.com>
parents: 2000
diff changeset
    69
				return handle_vcard(data);
1956
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    70
		end
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    71
	end, 1);
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    72
end
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    73
ec04b571fa86 mod_vcard: Add vcard_compatibility option to handle vcard stanzas routed to the full JID by ejabberd MUC rooms
Matthew Wild <mwild1@gmail.com>
parents: 1779
diff changeset
    74
local feature_vcard_attr = { var=xmlns_vcard };
549
c0947c0af398 Changed the datastore for vCards from vCard to vcard in mod_vcard and mod_register
Waqas Hussain <waqas20@gmail.com>
parents: 541
diff changeset
    75
module:add_event_hook("stream-features",
c0947c0af398 Changed the datastore for vCards from vCard to vcard in mod_vcard and mod_register
Waqas Hussain <waqas20@gmail.com>
parents: 541
diff changeset
    76
					function (session, features)
89
081e920dc74e Fixed: incorrect auth check
Waqas Hussain <waqas20@gmail.com>
parents: 86
diff changeset
    77
						if session.type == "c2s" then
357
17bcecb06420 Use a stanza for c2s stream features instead of an array of strings. Removes a FIXME.
Matthew Wild <mwild1@gmail.com>
parents: 307
diff changeset
    78
							features:tag("feature", feature_vcard_attr):up();
86
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    79
						end
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    80
					end);