2010-04-27 12:33:56 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_vcard_xupdate.erl
|
|
|
|
%%% Author : Igor Goryachev <igor@goryachev.org>
|
|
|
|
%%% Purpose : Add avatar hash in presence on behalf of client (XEP-0153)
|
|
|
|
%%% Created : 9 Mar 2007 by Igor Goryachev <igor@goryachev.org>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
2010-04-27 12:33:56 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_vcard_xupdate).
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2018-03-16 10:10:57 +01:00
|
|
|
-protocol({xep, 398, '0.2.0'}).
|
|
|
|
|
2010-04-27 12:33:56 +02:00
|
|
|
%% gen_mod callbacks
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3]).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
2017-09-17 09:26:48 +02:00
|
|
|
-export([update_presence/1, vcard_set/1, remove_user/2,
|
2018-01-23 08:54:52 +01:00
|
|
|
user_send_packet/1, mod_opt_type/1, mod_options/1, depends/2]).
|
2018-02-12 15:37:36 +01:00
|
|
|
%% API
|
|
|
|
-export([compute_hash/1]).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-18 14:01:32 +02:00
|
|
|
-include("xmpp.hrl").
|
2010-04-27 12:33:56 +02:00
|
|
|
|
2017-05-17 16:13:34 +02:00
|
|
|
-define(VCARD_XUPDATE_CACHE, vcard_xupdate_cache).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_mod callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
start(Host, Opts) ->
|
2017-05-17 16:13:34 +02:00
|
|
|
init_cache(Host, Opts),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(c2s_self_presence, Host, ?MODULE,
|
2013-03-14 10:33:02 +01:00
|
|
|
update_presence, 100),
|
2017-09-18 13:17:34 +02:00
|
|
|
ejabberd_hooks:add(user_send_packet, Host, ?MODULE,
|
|
|
|
user_send_packet, 50),
|
2017-09-17 09:26:48 +02:00
|
|
|
ejabberd_hooks:add(vcard_iq_set, Host, ?MODULE, vcard_set,
|
|
|
|
90),
|
2017-05-17 16:13:34 +02:00
|
|
|
ejabberd_hooks:add(remove_user, Host, ?MODULE, remove_user, 50).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
|
|
|
stop(Host) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(c2s_self_presence, Host,
|
2010-04-27 12:33:56 +02:00
|
|
|
?MODULE, update_presence, 100),
|
2017-09-18 13:17:34 +02:00
|
|
|
ejabberd_hooks:delete(user_send_packet, Host, ?MODULE,
|
|
|
|
user_send_packet, 50),
|
2017-09-17 09:26:48 +02:00
|
|
|
ejabberd_hooks:delete(vcard_iq_set, Host, ?MODULE,
|
|
|
|
vcard_set, 90),
|
2017-05-17 16:13:34 +02:00
|
|
|
ejabberd_hooks:delete(remove_user, Host, ?MODULE, remove_user, 50).
|
|
|
|
|
|
|
|
reload(Host, NewOpts, _OldOpts) ->
|
|
|
|
init_cache(Host, NewOpts).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
2017-05-17 16:13:34 +02:00
|
|
|
[{mod_vcard, hard}].
|
2016-07-06 13:58:48 +02:00
|
|
|
|
2010-04-27 12:33:56 +02:00
|
|
|
%%====================================================================
|
|
|
|
%% Hooks
|
|
|
|
%%====================================================================
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec update_presence({presence(), ejabberd_c2s:state()})
|
|
|
|
-> {presence(), ejabberd_c2s:state()}.
|
|
|
|
update_presence({#presence{type = available} = Pres,
|
|
|
|
#{jid := #jid{luser = LUser, lserver = LServer}} = State}) ->
|
2018-03-16 10:10:57 +01:00
|
|
|
case xmpp:get_subtag(Pres, #vcard_xupdate{}) of
|
|
|
|
#vcard_xupdate{hash = <<>>} ->
|
|
|
|
%% XEP-0398 forbids overwriting vcard:x:update
|
|
|
|
%% tags with empty <photo/> element
|
|
|
|
{Pres, State};
|
|
|
|
_ ->
|
|
|
|
Pres1 = case get_xupdate(LUser, LServer) of
|
|
|
|
undefined -> xmpp:remove_subtag(Pres, #vcard_xupdate{});
|
|
|
|
XUpdate -> xmpp:set_subtag(Pres, XUpdate)
|
|
|
|
end,
|
|
|
|
{Pres1, State}
|
|
|
|
end;
|
2017-01-09 15:02:17 +01:00
|
|
|
update_presence(Acc) ->
|
|
|
|
Acc.
|
2010-04-27 12:33:56 +02:00
|
|
|
|
2017-09-18 13:17:34 +02:00
|
|
|
-spec user_send_packet({presence(), ejabberd_c2s:state()})
|
|
|
|
-> {presence(), ejabberd_c2s:state()}.
|
|
|
|
user_send_packet({#presence{type = available,
|
|
|
|
to = #jid{luser = U, lserver = S,
|
|
|
|
lresource = <<"">>}},
|
|
|
|
#{jid := #jid{luser = U, lserver = S}}} = Acc) ->
|
|
|
|
%% This is processed by update_presence/2 explicitly, we don't
|
|
|
|
%% want to call this multiple times for performance reasons
|
|
|
|
Acc;
|
|
|
|
user_send_packet(Acc) ->
|
|
|
|
update_presence(Acc).
|
|
|
|
|
2017-09-17 09:26:48 +02:00
|
|
|
-spec vcard_set(iq()) -> iq().
|
|
|
|
vcard_set(#iq{from = #jid{luser = LUser, lserver = LServer}} = IQ) ->
|
2017-05-17 16:13:34 +02:00
|
|
|
ets_cache:delete(?VCARD_XUPDATE_CACHE, {LUser, LServer}),
|
2017-09-17 09:26:48 +02:00
|
|
|
ejabberd_sm:force_update_presence({LUser, LServer}),
|
|
|
|
IQ;
|
|
|
|
vcard_set(Acc) ->
|
|
|
|
Acc.
|
2017-05-17 16:13:34 +02:00
|
|
|
|
|
|
|
-spec remove_user(binary(), binary()) -> ok.
|
|
|
|
remove_user(User, Server) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
ets_cache:delete(?VCARD_XUPDATE_CACHE, {LUser, LServer}).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
2012-04-27 11:52:05 +02:00
|
|
|
%% Storage
|
2010-04-27 12:33:56 +02:00
|
|
|
%%====================================================================
|
2017-09-17 09:26:48 +02:00
|
|
|
-spec get_xupdate(binary(), binary()) -> vcard_xupdate() | undefined.
|
2010-04-27 12:33:56 +02:00
|
|
|
get_xupdate(LUser, LServer) ->
|
2017-05-17 16:13:34 +02:00
|
|
|
Result = case use_cache(LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?VCARD_XUPDATE_CACHE, {LUser, LServer},
|
|
|
|
fun() -> db_get_xupdate(LUser, LServer) end);
|
|
|
|
false ->
|
|
|
|
db_get_xupdate(LUser, LServer)
|
|
|
|
end,
|
|
|
|
case Result of
|
2017-09-17 09:26:48 +02:00
|
|
|
{ok, external} -> undefined;
|
|
|
|
{ok, Hash} -> #vcard_xupdate{hash = Hash};
|
|
|
|
error -> #vcard_xupdate{}
|
2017-05-17 16:13:34 +02:00
|
|
|
end.
|
|
|
|
|
2017-09-17 09:26:48 +02:00
|
|
|
-spec db_get_xupdate(binary(), binary()) -> {ok, binary() | external} | error.
|
2017-05-17 16:13:34 +02:00
|
|
|
db_get_xupdate(LUser, LServer) ->
|
|
|
|
case mod_vcard:get_vcard(LUser, LServer) of
|
|
|
|
[VCard] ->
|
|
|
|
{ok, compute_hash(VCard)};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec init_cache(binary(), gen_mod:opts()) -> ok.
|
|
|
|
init_cache(Host, Opts) ->
|
|
|
|
case use_cache(Host) of
|
|
|
|
true ->
|
2018-01-23 08:54:52 +01:00
|
|
|
CacheOpts = cache_opts(Opts),
|
2017-05-17 16:13:34 +02:00
|
|
|
ets_cache:new(?VCARD_XUPDATE_CACHE, CacheOpts);
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(?VCARD_XUPDATE_CACHE)
|
|
|
|
end.
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec cache_opts(gen_mod:opts()) -> [proplists:property()].
|
|
|
|
cache_opts(Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxSize = mod_vcard_xupdate_opt:cache_size(Opts),
|
|
|
|
CacheMissed = mod_vcard_xupdate_opt:cache_missed(Opts),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = mod_vcard_xupdate_opt:cache_life_time(Opts),
|
2017-05-17 16:13:34 +02:00
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
|
|
|
-spec use_cache(binary()) -> boolean().
|
|
|
|
use_cache(Host) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_vcard_xupdate_opt:use_cache(Host).
|
2017-05-17 16:13:34 +02:00
|
|
|
|
2017-09-17 09:26:48 +02:00
|
|
|
-spec compute_hash(xmlel()) -> binary() | external.
|
2017-05-17 16:13:34 +02:00
|
|
|
compute_hash(VCard) ->
|
2017-09-17 09:26:48 +02:00
|
|
|
case fxml:get_subtag(VCard, <<"PHOTO">>) of
|
|
|
|
false ->
|
2017-05-17 16:13:34 +02:00
|
|
|
<<>>;
|
2017-09-17 09:26:48 +02:00
|
|
|
Photo ->
|
|
|
|
try xmpp:decode(Photo, ?NS_VCARD, []) of
|
|
|
|
#vcard_photo{binval = <<_, _/binary>> = BinVal} ->
|
|
|
|
str:sha(BinVal);
|
|
|
|
#vcard_photo{extval = <<_, _/binary>>} ->
|
|
|
|
external;
|
|
|
|
_ ->
|
|
|
|
<<>>
|
|
|
|
catch _:{xmpp_codec, _} ->
|
|
|
|
<<>>
|
2017-05-23 09:43:26 +02:00
|
|
|
end
|
2017-05-17 16:13:34 +02:00
|
|
|
end.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% Options
|
|
|
|
%%====================================================================
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(use_cache) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_size) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:pos_int(infinity);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_missed) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_life_time) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:timeout(second, infinity).
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(Host) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
[{use_cache, ejabberd_option:use_cache(Host)},
|
|
|
|
{cache_size, ejabberd_option:cache_size(Host)},
|
|
|
|
{cache_missed, ejabberd_option:cache_missed(Host)},
|
|
|
|
{cache_life_time, ejabberd_option:cache_life_time(Host)}].
|