./ct_report/coverage/mod_privacy_mnesia.COVER.html

1 %%%----------------------------------------------------------------------
2 %%% Copyright notice from original mod_privacy
3 %%%
4 %%% File : mod_privacy.erl
5 %%% Author : Alexey Shchepin <alexey@process-one.net>
6 %%% Purpose : jabber:iq:privacy support
7 %%% Created : 21 Jul 2003 by Alexey Shchepin <alexey@process-one.net>
8 %%%
9 %%%
10 %%% ejabberd, Copyright (C) 2002-2011 ProcessOne
11 %%%
12 %%% This program is free software; you can redistribute it and/or
13 %%% modify it under the terms of the GNU General Public License as
14 %%% published by the Free Software Foundation; either version 2 of the
15 %%% License, or (at your option) any later version.
16 %%%
17 %%% This program is distributed in the hope that it will be useful,
18 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
19 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 %%% General Public License for more details.
21 %%%
22 %%% You should have received a copy of the GNU General Public License
23 %%% along with this program; if not, write to the Free Software
24 %%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 %%%
26 %%%----------------------------------------------------------------------
27
28 -module(mod_privacy_mnesia).
29 -author('alexey@process-one.net').
30 -author('arcusfelis@gmail.com').
31 -behaviour(mod_privacy_backend).
32
33 -export([init/2,
34 get_default_list/3,
35 get_list_names/3,
36 get_privacy_list/4,
37 set_default_list/4,
38 forget_default_list/3,
39 remove_privacy_list/4,
40 replace_privacy_list/5,
41 remove_user/3,
42 remove_domain/2]).
43
44 -include("mongoose.hrl").
45 -include("jlib.hrl").
46 -include("mod_privacy.hrl").
47
48 init(_HostType, _Opts) ->
49
:-(
mongoose_mnesia:create_table(privacy,
50 [{disc_copies, [node()]},
51 {attributes, record_info(fields, privacy)}]),
52
:-(
ok.
53
54 get_default_list(_HostType, LUser, LServer) ->
55
:-(
case catch mnesia:dirty_read(privacy, {LUser, LServer}) of
56 [] ->
57
:-(
{error, not_found};
58 [#privacy{default = Default, lists = Lists}] ->
59
:-(
case lists:keysearch(Default, 1, Lists) of
60 {value, {_, List}} ->
61
:-(
{ok, {Default, List}};
62 _ ->
63
:-(
{error, not_found}
64 end;
65 {'EXIT', Reason} ->
66
:-(
{error, Reason}
67 end.
68
69 get_list_names(_HostType, LUser, LServer) ->
70
:-(
case catch mnesia:dirty_read(privacy, {LUser, LServer}) of
71 {'EXIT', Reason} ->
72
:-(
{error, Reason};
73 [] ->
74
:-(
{error, not_found};
75 [#privacy{default = Default, lists = Lists}] ->
76
:-(
Names = [Name || {Name, _} <- Lists],
77
:-(
{ok, {Default, Names}}
78 end.
79
80 get_privacy_list(_HostType, LUser, LServer, Name) ->
81
:-(
case catch mnesia:dirty_read(privacy, {LUser, LServer}) of
82 {'EXIT', Reason} ->
83
:-(
{error, Reason};
84 [] ->
85
:-(
{error, not_found};
86 [#privacy{lists = Lists}] ->
87
:-(
case lists:keysearch(Name, 1, Lists) of
88 {value, {_, List}} ->
89
:-(
{ok, List};
90 _ ->
91
:-(
{error, not_found}
92 end
93 end.
94
95 %% @doc Set no default list for user.
96 forget_default_list(_HostType, LUser, LServer) ->
97
:-(
F = fun() ->
98
:-(
case mnesia:read({privacy, {LUser, LServer}}) of
99 [] ->
100
:-(
ok;
101 [R] ->
102
:-(
mnesia:write(R#privacy{default = none}),
103
:-(
ok
104 end
105 end,
106
:-(
case mnesia:transaction(F) of
107 {atomic, ok} ->
108
:-(
ok;
109 {aborted, Reason} ->
110
:-(
{error, {aborted, Reason}}
111 end.
112
113 set_default_list(_HostType, LUser, LServer, Name) ->
114
:-(
case mnesia:transaction(fun() -> set_default_list_t(LUser, LServer, Name) end) of
115 {atomic, ok} ->
116
:-(
ok;
117 {atomic, {error, Reason}} ->
118
:-(
{error, Reason};
119 {aborted, Reason} ->
120
:-(
{error, {aborted, Reason}}
121 end.
122
123 -spec set_default_list_t(jid:luser(), jid:lserver(), Name :: binary()) ->
124 ok | {error, not_found}.
125 set_default_list_t(LUser, LServer, Name) ->
126
:-(
case mnesia:read({privacy, {LUser, LServer}}) of
127 [] ->
128
:-(
{error, not_found};
129 [#privacy{lists = Lists} = P] ->
130
:-(
case lists:keymember(Name, 1, Lists) of
131 true ->
132
:-(
mnesia:write(P#privacy{default = Name}),
133
:-(
ok;
134 false ->
135
:-(
{error, not_found}
136 end
137 end.
138
139 remove_privacy_list(_HostType, LUser, LServer, Name) ->
140
:-(
F = fun() ->
141
:-(
case mnesia:read({privacy, {LUser, LServer}}) of
142 [] ->
143
:-(
ok;
144 [#privacy{default = Default}] when Name == Default ->
145
:-(
{error, conflict};
146 [#privacy{lists = Lists} = P] ->
147
:-(
NewLists = lists:keydelete(Name, 1, Lists),
148
:-(
mnesia:write(P#privacy{lists = NewLists}),
149
:-(
ok
150 end
151 end,
152
:-(
case mnesia:transaction(F) of
153 {atomic, ok} ->
154
:-(
ok;
155 {atomic, {error, _} = Error} ->
156
:-(
Error;
157 {aborted, Reason} ->
158
:-(
{error, {aborted, Reason}}
159 end.
160
161 replace_privacy_list(_HostType, LUser, LServer, Name, List) ->
162
:-(
US = {LUser, LServer},
163
:-(
F = fun() ->
164
:-(
case mnesia:wread({privacy, US}) of
165 [] ->
166
:-(
NewLists = [{Name, List}],
167
:-(
mnesia:write(#privacy{us = US, lists = NewLists}),
168
:-(
ok;
169 [#privacy{lists = Lists} = P] ->
170
:-(
NewLists1 = lists:keydelete(Name, 1, Lists),
171
:-(
NewLists = [{Name, List} | NewLists1],
172
:-(
mnesia:write(P#privacy{lists = NewLists}),
173
:-(
ok
174 end
175 end,
176
:-(
case mnesia:transaction(F) of
177 {atomic, ok} ->
178
:-(
ok;
179 {aborted, Reason} ->
180
:-(
{error, {aborted, Reason}}
181 end.
182
183 remove_user(_HostType, LUser, LServer) ->
184
:-(
F = fun() -> mnesia:delete({privacy, {LUser, LServer}}) end,
185
:-(
mnesia:transaction(F).
186
187 remove_domain(_HostType, LServer) ->
188
:-(
F = fun(#privacy{us = {_, LS}} = Rec, _) when LS =:= LServer ->
189
:-(
mnesia:delete_object(Rec),
190
:-(
ok;
191 (_, _) ->
192
:-(
ok
193 end,
194
:-(
mnesia:transaction(fun mnesia:foldl/3, [F, ok, privacy]).
Line Hits Source