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>
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_vcard_xupdate).
|
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
%% gen_mod callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/2, stop/1]).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-export([update_presence/3, vcard_set/3, export/1,
|
2016-07-06 13:58:48 +02:00
|
|
|
import/1, import/3, mod_opt_type/1, depends/2]).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-04-13 10:06:59 +02:00
|
|
|
-include("mod_vcard_xupdate.hrl").
|
2010-04-27 12:33:56 +02:00
|
|
|
-include("jlib.hrl").
|
|
|
|
|
2016-04-13 10:06:59 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
|
|
|
-callback import(binary(), #vcard_xupdate{}) -> ok | pass.
|
|
|
|
-callback add_xupdate(binary(), binary(), binary()) -> {atomic, any()}.
|
|
|
|
-callback get_xupdate(binary(), binary()) -> binary() | undefined.
|
|
|
|
-callback remove_xupdate(binary(), binary()) -> {atomic, any()}.
|
2010-04-27 12:33:56 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_mod callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
start(Host, Opts) ->
|
2016-04-13 10:06:59 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
|
|
|
Mod:init(Host, Opts),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:add(c2s_update_presence, Host, ?MODULE,
|
|
|
|
update_presence, 100),
|
|
|
|
ejabberd_hooks:add(vcard_set, Host, ?MODULE, vcard_set,
|
|
|
|
100),
|
2010-04-27 12:33:56 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
ejabberd_hooks:delete(c2s_update_presence, Host,
|
|
|
|
?MODULE, update_presence, 100),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(vcard_set, Host, ?MODULE,
|
|
|
|
vcard_set, 100),
|
2010-04-27 12:33:56 +02:00
|
|
|
ok.
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2010-04-27 12:33:56 +02:00
|
|
|
%%====================================================================
|
|
|
|
%% Hooks
|
|
|
|
%%====================================================================
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
update_presence(#xmlel{name = <<"presence">>, attrs = Attrs} = Packet,
|
|
|
|
User, Host) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"type">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<>> -> presence_with_xupdate(Packet, User, Host);
|
|
|
|
_ -> Packet
|
2010-04-27 12:33:56 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
update_presence(Packet, _User, _Host) -> Packet.
|
2010-04-27 12:33:56 +02:00
|
|
|
|
|
|
|
vcard_set(LUser, LServer, VCARD) ->
|
|
|
|
US = {LUser, LServer},
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"PHOTO">>}, {elem, <<"BINVAL">>}, cdata])
|
|
|
|
of
|
|
|
|
<<>> -> remove_xupdate(LUser, LServer);
|
|
|
|
BinVal ->
|
|
|
|
add_xupdate(LUser, LServer,
|
2013-06-20 10:40:44 +02:00
|
|
|
p1_sha:sha(jlib:decode_base64(BinVal)))
|
2010-04-27 12:33:56 +02:00
|
|
|
end,
|
|
|
|
ejabberd_sm:force_update_presence(US).
|
|
|
|
|
|
|
|
%%====================================================================
|
2012-04-27 11:52:05 +02:00
|
|
|
%% Storage
|
2010-04-27 12:33:56 +02:00
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
add_xupdate(LUser, LServer, Hash) ->
|
2016-04-13 10:06:59 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:add_xupdate(LUser, LServer, Hash).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
|
|
|
get_xupdate(LUser, LServer) ->
|
2016-04-13 10:06:59 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:get_xupdate(LUser, LServer).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
|
|
|
remove_xupdate(LUser, LServer) ->
|
2016-04-13 10:06:59 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:remove_xupdate(LUser, LServer).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Presence stanza rebuilding
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
presence_with_xupdate(#xmlel{name = <<"presence">>,
|
|
|
|
attrs = Attrs, children = Els},
|
|
|
|
User, Host) ->
|
2010-04-27 12:33:56 +02:00
|
|
|
XPhotoEl = build_xphotoel(User, Host),
|
|
|
|
Els2 = presence_with_xupdate2(Els, [], XPhotoEl),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"presence">>, attrs = Attrs,
|
|
|
|
children = Els2}.
|
2010-04-27 12:33:56 +02:00
|
|
|
|
|
|
|
presence_with_xupdate2([], Els2, XPhotoEl) ->
|
|
|
|
lists:reverse([XPhotoEl | Els2]);
|
|
|
|
%% This clause assumes that the x element contains only the XMLNS attribute:
|
2013-03-14 10:33:02 +01:00
|
|
|
presence_with_xupdate2([#xmlel{name = <<"x">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_VCARD_UPDATE}]}
|
|
|
|
| Els],
|
|
|
|
Els2, XPhotoEl) ->
|
2010-04-27 12:33:56 +02:00
|
|
|
presence_with_xupdate2(Els, Els2, XPhotoEl);
|
|
|
|
presence_with_xupdate2([El | Els], Els2, XPhotoEl) ->
|
|
|
|
presence_with_xupdate2(Els, [El | Els2], XPhotoEl).
|
|
|
|
|
|
|
|
build_xphotoel(User, Host) ->
|
|
|
|
Hash = get_xupdate(User, Host),
|
|
|
|
PhotoSubEls = case Hash of
|
2013-03-14 10:33:02 +01:00
|
|
|
Hash when is_binary(Hash) -> [{xmlcdata, Hash}];
|
|
|
|
_ -> []
|
2010-04-27 12:33:56 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
PhotoEl = [#xmlel{name = <<"photo">>, attrs = [],
|
|
|
|
children = PhotoSubEls}],
|
|
|
|
#xmlel{name = <<"x">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_VCARD_UPDATE}],
|
|
|
|
children = PhotoEl}.
|
|
|
|
|
2016-04-13 10:06:59 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
|
|
|
import(LServer) ->
|
2016-04-13 10:06:59 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:import(LServer).
|
|
|
|
|
|
|
|
import(LServer, DBType, LA) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:import(LServer, LA).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-04-27 16:10:50 +02:00
|
|
|
mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(_) -> [db_type].
|