1 |
|
%%%---------------------------------------------------------------------- |
2 |
|
%%% File : gen_iq_handler.erl |
3 |
|
%%% Author : Alexey Shchepin <alexey@process-one.net> |
4 |
|
%%% Purpose : IQ handler support |
5 |
|
%%% Created : 22 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 |
|
-module(gen_iq_handler). |
27 |
|
-author('alexey@process-one.net'). |
28 |
|
|
29 |
|
%% Old API. Get rid of it once all the modules adopted. |
30 |
|
-export([add_iq_handler/6, |
31 |
|
remove_iq_handler/3, |
32 |
|
check_type/1]). |
33 |
|
|
34 |
|
%% New API. |
35 |
|
-export([add_iq_handler_for_domain/6, |
36 |
|
add_iq_handler_for_subdomain/7, |
37 |
|
remove_iq_handler_for_domain/3, |
38 |
|
remove_iq_handler_for_subdomain/4]). |
39 |
|
|
40 |
|
-type execution_type() :: mongoose_iq_handler:execution_type(). |
41 |
|
-type subdomain_pattern() :: mongoose_subdomain_utils:subdomain_pattern(). |
42 |
|
|
43 |
|
%%==================================================================== |
44 |
|
%% Old API. Get rid of it once all the modules adopted. |
45 |
|
%%==================================================================== |
46 |
|
-spec add_iq_handler(Component :: module(), |
47 |
|
Domain :: jid:lserver(), |
48 |
|
Namespace :: binary(), |
49 |
|
Module :: atom(), |
50 |
|
Function :: atom(), |
51 |
|
ExecutionType :: mongoose_iq_handler:execution_type()) -> any(). |
52 |
|
add_iq_handler(Component, Domain, Namespace, Module, Function, ExecutionType) -> |
53 |
36 |
Extra = #{delete_on_unregister => true, module => Module, function => Function}, |
54 |
36 |
IQHandlerFn = make_iq_handler_fn(Module, Function), |
55 |
36 |
IQHandler = mongoose_iq_handler:new(IQHandlerFn, Extra, ExecutionType), |
56 |
36 |
gen_iq_component:register_iq_handler(Component, Domain, Namespace, IQHandler). |
57 |
|
|
58 |
|
-spec remove_iq_handler(Component :: module(), |
59 |
|
Domain :: jid:lserver(), |
60 |
|
Namespace :: binary()) -> any(). |
61 |
|
remove_iq_handler(Component, Domain, Namespace) -> |
62 |
36 |
gen_iq_component:unregister_iq_handler(Component, Domain, Namespace). |
63 |
|
|
64 |
|
-spec check_type(execution_type()) -> execution_type(). |
65 |
:-( |
check_type(no_queue) -> no_queue; |
66 |
:-( |
check_type(parallel) -> parallel; |
67 |
:-( |
check_type(one_queue) -> one_queue; |
68 |
|
check_type({queues, Int}) when is_integer(Int), Int > 0 -> |
69 |
:-( |
{queues, Int}. |
70 |
|
|
71 |
|
%%==================================================================== |
72 |
|
%% New API. |
73 |
|
%%==================================================================== |
74 |
|
-spec add_iq_handler_for_domain(HostType :: mongooseim:host_type(), |
75 |
|
Namespace :: binary(), |
76 |
|
Component :: module(), |
77 |
|
IQHandlerFn :: mongoose_iq_handler:handler_fn(), |
78 |
|
Extra :: map(), |
79 |
|
ExecutionType :: execution_type()) -> |
80 |
|
ok | {error, atom()}. |
81 |
|
add_iq_handler_for_domain(HostType, Namespace, Component, IQHandlerFn, |
82 |
|
Extra, ExecutionType) -> |
83 |
|
%% TODO: `delete_on_unregister` extra field is not needed once old API is removed |
84 |
4892 |
NewExtra = Extra#{delete_on_unregister => false}, |
85 |
4892 |
IQHandler = mongoose_iq_handler:new(IQHandlerFn, NewExtra, ExecutionType), |
86 |
4892 |
mongoose_lazy_routing:register_iq_handler_for_domain(HostType, Namespace, |
87 |
|
Component, IQHandler). |
88 |
|
|
89 |
|
-spec add_iq_handler_for_subdomain(HostType :: mongooseim:host_type(), |
90 |
|
SubdomainPattern :: subdomain_pattern(), |
91 |
|
Namespace :: binary(), |
92 |
|
Component :: module(), |
93 |
|
IQHandlerFn :: mongoose_iq_handler:handler_fn(), |
94 |
|
Extra :: map(), |
95 |
|
ExecutionType :: execution_type()) -> |
96 |
|
ok | {error, atom()}. |
97 |
|
add_iq_handler_for_subdomain(HostType, SubdomainPattern, Namespace, Component, |
98 |
|
IQHandlerFn, Extra, ExecutionType) -> |
99 |
|
%% TODO: `delete_on_unregister` extra field is not needed once old API is removed |
100 |
10 |
NewExtra = Extra#{delete_on_unregister => false}, |
101 |
10 |
IQHandler = mongoose_iq_handler:new(IQHandlerFn, NewExtra, ExecutionType), |
102 |
10 |
mongoose_lazy_routing:register_iq_handler_for_subdomain(HostType, SubdomainPattern, |
103 |
|
Namespace, Component, |
104 |
|
IQHandler). |
105 |
|
|
106 |
|
-spec remove_iq_handler_for_domain(HostType :: mongooseim:host_type(), |
107 |
|
Namespace :: binary(), |
108 |
|
Component :: module()) -> |
109 |
|
ok | {error, not_registered}. |
110 |
|
remove_iq_handler_for_domain(HostType, Namespace, Component) -> |
111 |
4879 |
case mongoose_lazy_routing:unregister_iq_handler_for_domain( |
112 |
|
HostType, Namespace, Component) of |
113 |
|
{ok, IQHandler} -> |
114 |
4247 |
mongoose_iq_handler:delete(IQHandler); |
115 |
632 |
{error, not_found} -> {error, not_registered} |
116 |
|
end. |
117 |
|
|
118 |
|
-spec remove_iq_handler_for_subdomain(HostType :: mongooseim:host_type(), |
119 |
|
SubdomainPattern :: subdomain_pattern(), |
120 |
|
Namespace :: binary(), |
121 |
|
Component :: module()) -> |
122 |
|
ok | {error, not_registered}. |
123 |
|
remove_iq_handler_for_subdomain(HostType, SubdomainPattern, Namespace, Component) -> |
124 |
10 |
case mongoose_lazy_routing:unregister_iq_handler_for_subdomain( |
125 |
|
HostType, SubdomainPattern, Namespace, Component) of |
126 |
|
{ok, IQHandler} -> |
127 |
10 |
mongoose_iq_handler:delete(IQHandler); |
128 |
:-( |
{error, not_found} -> {error, not_registered} |
129 |
|
end. |
130 |
|
|
131 |
|
%%-------------------------------------------------------------------- |
132 |
|
%%% Internal functions |
133 |
|
%%-------------------------------------------------------------------- |
134 |
|
-spec make_iq_handler_fn(module(), atom()) -> mongoose_iq_handler:handler_fn(). |
135 |
|
make_iq_handler_fn(Module, Function) -> |
136 |
|
%TODO: remove this function with removal of the old API |
137 |
36 |
fun(Acc, From, To, IQ, _Extra) -> |
138 |
126 |
Module:Function(From, To, Acc, IQ) |
139 |
|
end. |