1 |
|
%%%---------------------------------------------------------------------- |
2 |
|
%%% Old copyright notice from mod_private.erl |
3 |
|
%%% |
4 |
|
%%% Author : Alexey Shchepin <alexey@process-one.net> |
5 |
|
%%% Created : 16 Jan 2003 by Alexey Shchepin <alexey@process-one.net> |
6 |
|
%%% |
7 |
|
%%% |
8 |
|
%%% ejabberd, Copyright (C) 2002-2011 ProcessOne |
9 |
|
%%% |
10 |
|
%%% This program is free software; you can redistribute it and/or |
11 |
|
%%% modify it under the terms of the GNU General Public License as |
12 |
|
%%% published by the Free Software Foundation; either version 2 of the |
13 |
|
%%% License, or (at your option) any later version. |
14 |
|
%%% |
15 |
|
%%% This program is distributed in the hope that it will be useful, |
16 |
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 |
|
%%% General Public License for more details. |
19 |
|
%%% |
20 |
|
%%% You should have received a copy of the GNU General Public License |
21 |
|
%%% along with this program; if not, write to the Free Software |
22 |
|
%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 |
|
%%% |
24 |
|
%%%---------------------------------------------------------------------- |
25 |
|
|
26 |
|
%%% NS is namespace or key. |
27 |
|
%%% XML is #xmlel{} or value. |
28 |
|
-module(mod_private_mnesia). |
29 |
|
-author('alexey@process-one.net'). |
30 |
|
-author('arcusfelis@gmail.com'). |
31 |
|
-behaviour(mod_private_backend). |
32 |
|
|
33 |
|
-export([init/2, |
34 |
|
multi_set_data/4, |
35 |
|
multi_get_data/4, |
36 |
|
get_all_nss/3, |
37 |
|
remove_user/3, |
38 |
|
remove_domain/2]). |
39 |
|
|
40 |
|
-include("mongoose.hrl"). |
41 |
|
-include("jlib.hrl"). |
42 |
|
|
43 |
|
-record(private_storage, {usns, xml}). |
44 |
|
|
45 |
|
init(_HostType, _Opts) -> |
46 |
:-( |
mongoose_mnesia:create_table(private_storage, |
47 |
|
[{disc_only_copies, [node()]}, |
48 |
|
{attributes, record_info(fields, private_storage)}]), |
49 |
:-( |
ok. |
50 |
|
|
51 |
|
multi_set_data(_HostType, LUser, LServer, NS2XML) -> |
52 |
:-( |
F = fun() -> multi_set_data_t(LUser, LServer, NS2XML) end, |
53 |
:-( |
case mnesia:transaction(F) of |
54 |
:-( |
{atomic, ok} -> ok; |
55 |
:-( |
{aborted, Reason} -> {aborted, Reason} |
56 |
|
end. |
57 |
|
|
58 |
|
multi_set_data_t(LUser, LServer, NS2XML) -> |
59 |
:-( |
[set_data_t(LUser, LServer, NS, XML) || {NS, XML} <- NS2XML], |
60 |
:-( |
ok. |
61 |
|
|
62 |
|
set_data_t(LUser, LServer, NS, XML) -> |
63 |
:-( |
mnesia:write(#private_storage{usns = {LUser, LServer, NS}, xml = XML}). |
64 |
|
|
65 |
|
multi_get_data(_HostType, LUser, LServer, NS2Def) -> |
66 |
:-( |
[get_data(LUser, LServer, NS, Default) || {NS, Default} <- NS2Def]. |
67 |
|
|
68 |
|
get_all_nss(_HostType, LUser, LServer) -> |
69 |
:-( |
F = fun() -> |
70 |
:-( |
select_namespaces_t(LUser, LServer) |
71 |
|
end, |
72 |
:-( |
{atomic, NSs} = mnesia:transaction(F), |
73 |
:-( |
NSs. |
74 |
|
|
75 |
|
%% @doc Return stored value or default. |
76 |
|
get_data(LUser, LServer, NS, Default) -> |
77 |
:-( |
case mnesia:dirty_read(private_storage, {LUser, LServer, NS}) of |
78 |
:-( |
[#private_storage{xml=XML}] -> XML; |
79 |
:-( |
[] -> Default |
80 |
|
end. |
81 |
|
|
82 |
|
remove_user(_HostType, LUser, LServer) -> |
83 |
:-( |
F = fun() -> |
84 |
:-( |
NSs = select_namespaces_t(LUser, LServer), |
85 |
:-( |
[delete_record_t(LUser, LServer, NS) || NS <- NSs] |
86 |
|
end, |
87 |
:-( |
mnesia:transaction(F). |
88 |
|
|
89 |
|
%% There is no optimized way to remove a domain. |
90 |
|
%% We expect, that domain removal process would call remove_user instead |
91 |
|
%% for each user. |
92 |
|
remove_domain(_HostType, _LServer) -> |
93 |
:-( |
ok. |
94 |
|
|
95 |
|
select_namespaces_t(LUser, LServer) -> |
96 |
:-( |
Result = mnesia:select( |
97 |
|
private_storage, |
98 |
|
[{#private_storage{usns={LUser, LServer, '$1'}, _ = '_'}, |
99 |
|
[], |
100 |
|
['$$']}]), |
101 |
:-( |
[NS || [NS] <- Result]. |
102 |
|
|
103 |
|
delete_record_t(LUser, LServer, NS) -> |
104 |
:-( |
mnesia:delete({private_storage, {LUser, LServer, NS}}). |