-
Notifications
You must be signed in to change notification settings - Fork 426
/
mongoose_lib.erl
224 lines (191 loc) · 7.66 KB
/
mongoose_lib.erl
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
-module(mongoose_lib).
-export([log_if_backend_error/4]).
%% Maps
-export([maps_append/3]).
-export([maps_foreach/2]).
-export([pairs_foreach/2]).
-export([maps_or_pairs_foreach/2]).
%% Busy Wait
-export([wait_until/2, wait_until/3]).
-export([parse_ip_netmask/1]).
-export([get_message_type/1, does_local_user_exist/3]).
%% Private, just for warning
-export([deprecated_logging/1]).
-deprecated({deprecated_logging, 1, eventually}).
-ignore_xref([pairs_foreach/2, wait_until/3]).
-export_type([microseconds/0]).
-export([pmap/2, pmap/3]).
-ignore_xref([pmap/3]).
-include("mongoose.hrl").
-include("jlib.hrl").
-type microseconds() :: integer().
-type message_type() :: one2one | groupchat.
%% ------------------------------------------------------------------
%% Logging
%% ------------------------------------------------------------------
%% @doc Database backends for various modules return ok, {atomic, ok}
%% or {atomic, []} on success, and usually {error, ...} on failure.
%% All we need is to log an error if such occurred, and proceed normally.
-spec log_if_backend_error(V :: any(), % value return by called backend fun
Module :: atom(), % caller
Line :: integer(),
Args :: any() ) -> ok.
log_if_backend_error(V, Module, Line, Args) ->
case V of
ok -> ok;
{atomic, _} -> ok;
{updated, _} -> ok; % rdbms
L when is_list(L) -> ok; % riak
{error, E} ->
?LOG_ERROR(#{what => backend_error,
text => <<"Error calling backend module">>,
caller_module => Module, caller_line => Line,
reason => E, args => Args});
E ->
?LOG_ERROR(#{what => backend_error,
text => <<"Unexpected return from backend">>,
caller_module => Module, caller_line => Line,
reason => E, args => Args})
end,
ok.
%% ------------------------------------------------------------------
%% Maps
%% ------------------------------------------------------------------
%% Appends a new Value to the current list of values associated with Key.
maps_append(Key, Value, Map) ->
Values = maps:get(Key, Map, []),
maps:put(Key, Values ++ [Value], Map).
-spec maps_foreach(fun(), map()) -> ok.
maps_foreach(Fun, Map) when is_function(Fun, 1) ->
maps:fold(fun(Key, Value, Acc) ->
Fun({Key, Value}), Acc
end, ok, Map);
maps_foreach(Fun, Map) when is_function(Fun, 2) ->
maps:fold(fun(Key, Value, Acc) ->
Fun(Key, Value), Acc
end, ok, Map).
-spec pairs_foreach(Fun, [{Key, Value}]) -> ok
when
Fun :: fun((Key, Value) -> term())
| fun(({Key, Value}) -> term()),
Key :: term(),
Value :: term().
pairs_foreach(Fun, List) when is_function(Fun, 1) ->
lists:foreach(Fun, List);
pairs_foreach(Fun, List) when is_function(Fun, 2) ->
lists:foreach(fun({K,V}) -> Fun(K,V) end, List).
maps_or_pairs_foreach(Fun, Map) when is_map(Map) ->
maps_foreach(Fun, Map);
maps_or_pairs_foreach(Fun, List) when is_list(List) ->
pairs_foreach(Fun, List).
%% Busy wait
wait_until(Fun, ExpectedValue) ->
wait_until(Fun, ExpectedValue, #{}).
wait_until(Fun, ExpectedValue, Opts) ->
Defaults = #{time_left => timer:seconds(5), sleep_time => 100},
do_wait_until(Fun, ExpectedValue, maps:merge(Defaults, Opts)).
do_wait_until(_, _, #{time_left := TimeLeft}) when TimeLeft =< 0 ->
ok;
do_wait_until(Fun, ExpectedValue, Opts) ->
case Fun() of
ExpectedValue -> {ok, ExpectedValue};
_OtherValue -> wait_and_continue(Fun, ExpectedValue, Opts)
end.
wait_and_continue(Fun, ExpectedValue, #{time_left := TimeLeft, sleep_time := SleepTime} = Opts) ->
timer:sleep(SleepTime),
do_wait_until(Fun, ExpectedValue, Opts#{time_left => TimeLeft - SleepTime}).
deprecated_logging(Location) ->
Map = #{what => deprecated_logging_macro,
text => <<"Deprecated logging macro is used in your code">>},
mongoose_deprecations:log(Location, Map, [{log_level, warning}]).
%% ------------------------------------------------------------------
%% Parse IP
%% ------------------------------------------------------------------
parse_ip_netmask(S) ->
case string:tokens(S, "/") of
[IPStr] -> parse_ip_netmask(IPStr, undefined);
[IPStr, MaskStr] -> parse_ip_netmask(IPStr, MaskStr);
_ -> error
end.
parse_ip_netmask(IPStr, undefined) ->
case inet_parse:address(IPStr) of
{ok, {_, _, _, _} = IP} ->
{ok, {IP, 32}};
{ok, {_, _, _, _, _, _, _, _} = IP} ->
{ok, {IP, 128}};
_ ->
error
end;
parse_ip_netmask(IPStr, MaskStr) ->
case catch list_to_integer(MaskStr) of
Mask when is_integer(Mask),
Mask >= 0 ->
case inet_parse:address(IPStr) of
{ok, {_, _, _, _} = IP} when Mask =< 32 ->
{ok, {IP, Mask}};
{ok, {_, _, _, _, _, _, _, _} = IP} when Mask =< 128 ->
{ok, {IP, Mask}};
_ ->
error
end;
_ ->
error
end.
%% ------------------------------------------------------------------
%% does_local_user_exist
%% ------------------------------------------------------------------
-spec get_message_type(mongoose_acc:t()) -> message_type().
get_message_type(Acc) ->
case mongoose_acc:stanza_type(Acc) of
<<"groupchat">> -> groupchat;
_ -> one2one
end.
-spec does_local_user_exist(mongooseim:host_type(), jid:jid(), message_type()) -> boolean().
does_local_user_exist(HostType, To, groupchat) ->
(not is_to_room(To)) andalso ejabberd_auth:does_user_exist(HostType, To, stored);
does_local_user_exist(HostType, To, _) ->
ejabberd_auth:does_user_exist(HostType, To, stored).
%% WHY: filter_local_packet is executed twice in the pipeline of muc messages. in two routing steps:
%% - From the sender to the room: runs filter_local_packet with From=Sender, To=Room
%% - For each member of the room:
%% From the room to each member: runs with From=Room/Sender, To=Member
%% So, as inbox is a per-user concept, it is on the second routing step only when we want to do act.
%% NOTE: ideally for groupchats, we could instead act on `filter_room_packet`, like MAM.
-spec is_to_room(jid:jid()) -> boolean().
is_to_room(Jid) ->
{error, not_found} =:= mongoose_domain_api:get_domain_host_type(Jid#jid.lserver).
%% ------------------------------------------------------------------
%% parallel map
%% ------------------------------------------------------------------
%% Runs a function for each element on the same node
pmap(F, Es) ->
pmap(F, Es, 5000).
pmap(F, Es, Timeout) ->
TimerRef = erlang:start_timer(Timeout, self(), pmap_timeout),
Running =
[spawn_monitor(fun() -> exit({pmap_result, F(E)}) end)
|| E <- Es],
Result = collect(Running, TimerRef),
cancel_and_flush_timer(TimerRef),
Result.
collect([], _TimerRef) -> [];
collect([{Pid, MRef} | Next] = In, TimerRef) ->
receive
{'DOWN', MRef, process, Pid, Reason} ->
[reason_to_result(Reason) | collect(Next, TimerRef)];
{timeout, TimerRef, pmap_timeout} ->
stop_processes(In),
collect(In, TimerRef)
end.
stop_processes(In) ->
[erlang:exit(Pid, timeout) || {Pid, _} <- In].
reason_to_result({pmap_result, Result}) ->
{ok, Result};
reason_to_result(Reason) ->
{error, Reason}.
cancel_and_flush_timer(TimerRef) ->
erlang:cancel_timer(TimerRef),
receive
{timeout, TimerRef, _} -> ok
after 0 -> ok
end.