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 |
|
|
33 |
|
%% New API. |
34 |
|
-export([add_iq_handler_for_domain/6, |
35 |
|
add_iq_handler_for_subdomain/7, |
36 |
|
remove_iq_handler_for_domain/3, |
37 |
|
remove_iq_handler_for_subdomain/4]). |
38 |
|
|
39 |
|
-type execution_type() :: mongoose_iq_handler:execution_type(). |
40 |
|
-type subdomain_pattern() :: mongoose_subdomain_utils:subdomain_pattern(). |
41 |
|
|
42 |
|
%%==================================================================== |
43 |
|
%% Old API. Get rid of it once all the modules adopted. |
44 |
|
%%==================================================================== |
45 |
|
-spec add_iq_handler(Component :: module(), |
46 |
|
Domain :: jid:lserver(), |
47 |
|
Namespace :: binary(), |
48 |
|
Module :: atom(), |
49 |
|
Function :: atom(), |
50 |
|
ExecutionType :: mongoose_iq_handler:execution_type()) -> any(). |
51 |
|
add_iq_handler(Component, Domain, Namespace, Module, Function, ExecutionType) -> |
52 |
56 |
Extra = #{delete_on_unregister => true, module => Module, function => Function}, |
53 |
56 |
IQHandlerFn = make_iq_handler_fn(Module, Function), |
54 |
56 |
IQHandler = mongoose_iq_handler:new(IQHandlerFn, Extra, ExecutionType), |
55 |
56 |
gen_iq_component:register_iq_handler(Component, Domain, Namespace, IQHandler). |
56 |
|
|
57 |
|
-spec remove_iq_handler(Component :: module(), |
58 |
|
Domain :: jid:lserver(), |
59 |
|
Namespace :: binary()) -> any(). |
60 |
|
remove_iq_handler(Component, Domain, Namespace) -> |
61 |
56 |
gen_iq_component:unregister_iq_handler(Component, Domain, Namespace). |
62 |
|
|
63 |
|
%%==================================================================== |
64 |
|
%% New API. |
65 |
|
%%==================================================================== |
66 |
|
-spec add_iq_handler_for_domain(HostType :: mongooseim:host_type(), |
67 |
|
Namespace :: binary(), |
68 |
|
Component :: module(), |
69 |
|
IQHandlerFn :: mongoose_iq_handler:handler_fn(), |
70 |
|
Extra :: map(), |
71 |
|
ExecutionType :: execution_type()) -> |
72 |
|
ok | {error, atom()}. |
73 |
|
add_iq_handler_for_domain(HostType, Namespace, Component, IQHandlerFn, |
74 |
|
Extra, ExecutionType) -> |
75 |
|
%% TODO: `delete_on_unregister` extra field is not needed once old API is removed |
76 |
6746 |
NewExtra = Extra#{delete_on_unregister => false}, |
77 |
6746 |
IQHandler = mongoose_iq_handler:new(IQHandlerFn, NewExtra, ExecutionType), |
78 |
6746 |
mongoose_lazy_routing:register_iq_handler_for_domain(HostType, Namespace, |
79 |
|
Component, IQHandler). |
80 |
|
|
81 |
|
-spec add_iq_handler_for_subdomain(HostType :: mongooseim:host_type(), |
82 |
|
SubdomainPattern :: subdomain_pattern(), |
83 |
|
Namespace :: binary(), |
84 |
|
Component :: module(), |
85 |
|
IQHandlerFn :: mongoose_iq_handler:handler_fn(), |
86 |
|
Extra :: map(), |
87 |
|
ExecutionType :: execution_type()) -> |
88 |
|
ok | {error, atom()}. |
89 |
|
add_iq_handler_for_subdomain(HostType, SubdomainPattern, Namespace, Component, |
90 |
|
IQHandlerFn, Extra, ExecutionType) -> |
91 |
|
%% TODO: `delete_on_unregister` extra field is not needed once old API is removed |
92 |
154 |
NewExtra = Extra#{delete_on_unregister => false}, |
93 |
154 |
IQHandler = mongoose_iq_handler:new(IQHandlerFn, NewExtra, ExecutionType), |
94 |
154 |
mongoose_lazy_routing:register_iq_handler_for_subdomain(HostType, SubdomainPattern, |
95 |
|
Namespace, Component, |
96 |
|
IQHandler). |
97 |
|
|
98 |
|
-spec remove_iq_handler_for_domain(HostType :: mongooseim:host_type(), |
99 |
|
Namespace :: binary(), |
100 |
|
Component :: module()) -> |
101 |
|
ok | {error, not_registered}. |
102 |
|
remove_iq_handler_for_domain(HostType, Namespace, Component) -> |
103 |
6736 |
case mongoose_lazy_routing:unregister_iq_handler_for_domain( |
104 |
|
HostType, Namespace, Component) of |
105 |
|
{ok, IQHandler} -> |
106 |
5914 |
mongoose_iq_handler:delete(IQHandler); |
107 |
822 |
{error, not_found} -> {error, not_registered} |
108 |
|
end. |
109 |
|
|
110 |
|
-spec remove_iq_handler_for_subdomain(HostType :: mongooseim:host_type(), |
111 |
|
SubdomainPattern :: subdomain_pattern(), |
112 |
|
Namespace :: binary(), |
113 |
|
Component :: module()) -> |
114 |
|
ok | {error, not_registered}. |
115 |
|
remove_iq_handler_for_subdomain(HostType, SubdomainPattern, Namespace, Component) -> |
116 |
154 |
case mongoose_lazy_routing:unregister_iq_handler_for_subdomain( |
117 |
|
HostType, SubdomainPattern, Namespace, Component) of |
118 |
|
{ok, IQHandler} -> |
119 |
154 |
mongoose_iq_handler:delete(IQHandler); |
120 |
:-( |
{error, not_found} -> {error, not_registered} |
121 |
|
end. |
122 |
|
|
123 |
|
%%-------------------------------------------------------------------- |
124 |
|
%%% Internal functions |
125 |
|
%%-------------------------------------------------------------------- |
126 |
|
-spec make_iq_handler_fn(module(), atom()) -> mongoose_iq_handler:handler_fn(). |
127 |
|
make_iq_handler_fn(Module, Function) -> |
128 |
|
%TODO: remove this function with removal of the old API |
129 |
56 |
fun(Acc, From, To, IQ, _Extra) -> |
130 |
167 |
Module:Function(From, To, Acc, IQ) |
131 |
|
end. |