aboutsummaryrefslogtreecommitdiff
path: root/src/mod_mix_pam_mnesia.erl
diff options
context:
space:
mode:
authorEvgeny Khramtsov <ekhramtsov@process-one.net>2018-12-05 13:14:29 +0300
committerEvgeny Khramtsov <ekhramtsov@process-one.net>2018-12-05 13:14:29 +0300
commitd5e4da54cfde1cf5b7876a192d9fe3b952eb345f (patch)
tree606b32538a4f2a1b79904249fb262785ab2dd46a /src/mod_mix_pam_mnesia.erl
parentKeep info about carbons inside session table (diff)
Update MIX code to reflect newest specification
Note that support for older specification is completely dropped, i.e. no backward compatibility is provided since the XEPs are still very experimental and being changed drastically
Diffstat (limited to 'src/mod_mix_pam_mnesia.erl')
-rw-r--r--src/mod_mix_pam_mnesia.erl91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/mod_mix_pam_mnesia.erl b/src/mod_mix_pam_mnesia.erl
new file mode 100644
index 000000000..568c4b9fa
--- /dev/null
+++ b/src/mod_mix_pam_mnesia.erl
@@ -0,0 +1,91 @@
+%%%-------------------------------------------------------------------
+%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
+%%% Created : 4 Dec 2018 by Evgeny Khramtsov <ekhramtsov@process-one.net>
+%%%
+%%%
+%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
+%%%
+%%% 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.
+%%%
+%%%----------------------------------------------------------------------
+-module(mod_mix_pam_mnesia).
+-behaviour(mod_mix_pam).
+
+%% API
+-export([init/2, add_channel/3, get_channel/2,
+ get_channels/1, del_channel/2, del_channels/1,
+ use_cache/1]).
+
+-record(mix_pam, {user_channel :: {binary(), binary(), binary(), binary()},
+ user :: {binary(), binary()},
+ id :: binary()}).
+
+%%%===================================================================
+%%% API
+%%%===================================================================
+init(_Host, _Opts) ->
+ case ejabberd_mnesia:create(?MODULE, mix_pam,
+ [{disc_only_copies, [node()]},
+ {attributes, record_info(fields, mix_pam)},
+ {index, [user]}]) of
+ {atomic, _} -> ok;
+ _ -> {error, db_failure}
+ end.
+
+use_cache(Host) ->
+ case mnesia:table_info(mix_pam, storage_type) of
+ disc_only_copies ->
+ gen_mod:get_module_opt(Host, mod_mix_pam, use_cache);
+ _ ->
+ false
+ end.
+
+add_channel(User, Channel, ID) ->
+ {LUser, LServer, _} = jid:tolower(User),
+ {Chan, Service, _} = jid:tolower(Channel),
+ mnesia:dirty_write(#mix_pam{user_channel = {LUser, LServer, Chan, Service},
+ user = {LUser, LServer},
+ id = ID}).
+
+get_channel(User, Channel) ->
+ {LUser, LServer, _} = jid:tolower(User),
+ {Chan, Service, _} = jid:tolower(Channel),
+ case mnesia:dirty_read(mix_pam, {LUser, LServer, Chan, Service}) of
+ [#mix_pam{id = ID}] -> {ok, ID};
+ [] -> {error, notfound}
+ end.
+
+get_channels(User) ->
+ {LUser, LServer, _} = jid:tolower(User),
+ Ret = mnesia:dirty_index_read(mix_pam, #mix_pam.user, {LUser, LServer}),
+ {ok, lists:map(
+ fun(#mix_pam{user_channel = {_, _, Chan, Service},
+ id = ID}) ->
+ {jid:make(Chan, Service), ID}
+ end, Ret)}.
+
+del_channel(User, Channel) ->
+ {LUser, LServer, _} = jid:tolower(User),
+ {Chan, Service, _} = jid:tolower(Channel),
+ mnesia:dirty_delete(mix_pam, {LUser, LServer, Chan, Service}).
+
+del_channels(User) ->
+ {LUser, LServer, _} = jid:tolower(User),
+ Ret = mnesia:dirty_index_read(mix_pam, #mix_pam.user, {LUser, LServer}),
+ lists:foreach(fun mnesia:dirty_delete_object/1, Ret).
+
+%%%===================================================================
+%%% Internal functions
+%%%===================================================================