2016-04-13 10:06:59 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% @copyright (C) 2016, Evgeny Khramtsov
|
|
|
|
%%% @doc
|
|
|
|
%%%
|
|
|
|
%%% @end
|
|
|
|
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(mod_vcard_xupdate_sql).
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
2016-05-01 10:03:20 +02:00
|
|
|
-behaviour(mod_vcard_xupdate).
|
|
|
|
|
2016-04-13 10:06:59 +02:00
|
|
|
%% API
|
|
|
|
-export([init/2, import/2, add_xupdate/3, get_xupdate/2, remove_xupdate/2,
|
|
|
|
import/1, export/1]).
|
|
|
|
|
|
|
|
-include("mod_vcard_xupdate.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2016-04-13 10:06:59 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
add_xupdate(LUser, LServer, Hash) ->
|
|
|
|
F = fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"vcard_xupdate",
|
|
|
|
["!username=%(LUser)s",
|
|
|
|
"hash=%(Hash)s"])
|
2016-04-13 10:06:59 +02:00
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(LServer, F).
|
2016-04-13 10:06:59 +02:00
|
|
|
|
|
|
|
get_xupdate(LUser, LServer) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(hash)s from vcard_xupdate where"
|
|
|
|
" username=%(LUser)s"))
|
2016-04-13 10:06:59 +02:00
|
|
|
of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, [{Hash}]} -> Hash;
|
|
|
|
_ -> undefined
|
2016-04-13 10:06:59 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
remove_xupdate(LUser, LServer) ->
|
|
|
|
F = fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from vcard_xupdate where username=%(LUser)s"))
|
2016-04-13 10:06:59 +02:00
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(LServer, F).
|
2016-04-13 10:06:59 +02:00
|
|
|
|
|
|
|
export(_Server) ->
|
|
|
|
[{vcard_xupdate,
|
|
|
|
fun(Host, #vcard_xupdate{us = {LUser, LServer}, hash = Hash})
|
2016-05-04 20:01:05 +02:00
|
|
|
when LServer == Host ->
|
|
|
|
[?SQL("delete from vcard_xupdate where username=%(LUser)s;"),
|
|
|
|
?SQL("insert into vcard_xupdate(username, hash) values ("
|
|
|
|
"%(LUser)s, %(Hash)s);")];
|
2016-04-13 10:06:59 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(LServer) ->
|
|
|
|
[{<<"select username, hash from vcard_xupdate;">>,
|
|
|
|
fun([LUser, Hash]) ->
|
|
|
|
#vcard_xupdate{us = {LUser, LServer}, hash = Hash}
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(_LServer, _) ->
|
|
|
|
pass.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|