1 |
|
%%%------------------------------------------------------------------- |
2 |
|
%%% @doc |
3 |
|
%%% A behaviour which should be used by all modules being used in a |
4 |
|
%%% routing pipeline. The pipeline, manage by ejabberd_router:route |
5 |
|
%%% func, calls filter and route for each successful module. |
6 |
|
%%% |
7 |
|
%%% Module has to implement both functions, can be a no-op just returning |
8 |
|
%%% a tuple of its args. |
9 |
|
%%% @end |
10 |
|
%%%------------------------------------------------------------------- |
11 |
|
-module(xmpp_router). |
12 |
|
|
13 |
|
-define(ARGS, From :: jid:jid(), |
14 |
|
To :: jid:jid(), |
15 |
|
Acc :: mongoose_acc:t(), |
16 |
|
Packet :: exml:element()). |
17 |
|
-record(router_handler, { |
18 |
|
filter :: filter_fun(), |
19 |
|
route :: route_fun() |
20 |
|
}). |
21 |
|
-type t() :: #router_handler{}. |
22 |
|
-type filter_fun() :: fun((?ARGS) -> drop | filter()). |
23 |
|
-type route_fun() :: fun((?ARGS) -> {done, mongoose_acc:t()} | filter()). |
24 |
|
-type filter() :: {?ARGS}. |
25 |
|
-export_type([filter/0, t/0]). |
26 |
|
|
27 |
|
-callback route(?ARGS) -> {done, mongoose_acc:t()} | filter(). |
28 |
|
-callback filter(?ARGS) -> drop | filter(). |
29 |
|
|
30 |
|
-export([expand_routing_modules/1]). |
31 |
|
-export([call_route/5, call_filter/5]). |
32 |
|
|
33 |
|
-spec call_route(Handler :: t(), From :: jid:jid(), |
34 |
|
To :: jid:jid(), Acc :: mongoose_acc:t(), Packet :: exml:element()) -> |
35 |
|
{done, mongoose_acc:t()} | filter(). |
36 |
|
call_route(#router_handler{route = Route}, From, To, Acc, Packet) -> |
37 |
101014 |
Route(From, To, Acc, Packet). |
38 |
|
|
39 |
|
-spec call_filter(Handler :: t(), From :: jid:jid(), |
40 |
|
To :: jid:jid(), Acc :: mongoose_acc:t(), Packet :: exml:element()) -> drop | filter(). |
41 |
|
call_filter(#router_handler{filter = Filter}, From, To, Acc, Packet) -> |
42 |
101400 |
Filter(From, To, Acc, Packet). |
43 |
|
|
44 |
|
-spec expand_routing_modules([module()]) -> [t()]. |
45 |
|
expand_routing_modules(ModuleList) -> |
46 |
104 |
[ #router_handler{filter = fun Module:filter/4, |
47 |
|
route = fun Module:route/4} |
48 |
104 |
|| Module <- ModuleList ]. |