Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mod_vcard_xupdate.erl « src - github.com/processone/ejabberd.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3d163b1304f2530db3c68c5ad6850eafed5ee99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
%%%----------------------------------------------------------------------
%%% 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
-export([start/2,
         stop/1]).

%% hooks
-export([update_presence/3,
	 vcard_set/3]).

-include("ejabberd.hrl").
-include_lib("exmpp/include/exmpp.hrl").

-record(vcard_xupdate, {us, hash}).

%%====================================================================
%% gen_mod callbacks
%%====================================================================

start(Host, _Opts) ->
    HostB = list_to_binary(Host),
    mnesia:create_table(vcard_xupdate,
                        [{disc_copies, [node()]},
                         {attributes, record_info(fields, vcard_xupdate)}]),
    ejabberd_hooks:add(c2s_update_presence, HostB,
		       ?MODULE, update_presence, 100),
    ejabberd_hooks:add(vcard_set, HostB,
		       ?MODULE, vcard_set, 100),
    ok.

stop(Host) ->
    HostB = list_to_binary(Host),
    ejabberd_hooks:delete(c2s_update_presence, HostB,
			  ?MODULE, update_presence, 100),
    ejabberd_hooks:delete(vcard_set, HostB,
			  ?MODULE, vcard_set, 100),
    ok.

%%====================================================================
%% Hooks
%%====================================================================

update_presence(Packet, User, Host) ->
    case exmpp_presence:is_presence(Packet) andalso
	exmpp_xml:get_attribute_as_binary(Packet, type, undefined)
	== undefined of
        true ->
	    presence_with_xupdate(Packet, User, Host);
        false ->
            Packet
    end.

vcard_set(User, Server, VCARD) ->
    US = {User, Server},
    case exmpp_xml:get_path(VCARD, [{element, "PHOTO"}, {element, "BINVAL"}, cdata_as_list]) of
	[] ->
	    remove_xupdate(User, Server);
	BinVal ->
	    add_xupdate(User, Server, sha:sha(jlib:decode_base64(BinVal)))
    end,
    ejabberd_sm:force_update_presence(US).

%%====================================================================
%% Mnesia storage
%%====================================================================

add_xupdate(LUser, LServer, Hash) ->
    F = fun() ->
                mnesia:write(#vcard_xupdate{us = {LUser, LServer}, hash = Hash})
        end,
    mnesia:transaction(F).

get_xupdate(LUser, LServer) ->
    case mnesia:dirty_read(vcard_xupdate, {LUser, LServer}) of
        [#vcard_xupdate{hash = Hash}] ->
            Hash;
        _ ->
            undefined
    end.

remove_xupdate(LUser, LServer) ->
    F = fun() ->
                mnesia:delete({vcard_xupdate, {LUser, LServer}})
        end,
    mnesia:transaction(F).

%%%----------------------------------------------------------------------
%%% Presence stanza rebuilding
%%%----------------------------------------------------------------------

presence_with_xupdate(Stanza, User, Host) ->
    XPhotoEl = build_xphotoel(User, Host),
    StanzaReduced = exmpp_xml:remove_element(Stanza, ?NS_VCARD_UPDATE, x),
    exmpp_xml:append_child(StanzaReduced, XPhotoEl).

build_xphotoel(User, Host) ->
    Hash = get_xupdate(User, Host),
    PhotoSubEls = case Hash of
		      Hash when is_list(Hash) ->
			  [exmpp_xml:cdata(Hash)];
		      _ ->
			  []
		  end,
    PhotoEl = [exmpp_xml:element(?NS_VCARD_UPDATE, photo, [], PhotoSubEls)],
    exmpp_xml:element(?NS_VCARD_UPDATE, x, [], PhotoEl).