1 |
|
%%%---------------------------------------------------------------------- |
2 |
|
%%% File : mod_muc_light_codec_backend.erl |
3 |
|
%%% Author : Piotr Nosek <piotr.nosek@erlang-solutions.com> |
4 |
|
%%% Purpose : MUC Light codec behaviour |
5 |
|
%%% Created : 29 Sep 2015 by Piotr Nosek <piotr.nosek@erlang-solutions.com> |
6 |
|
%%%---------------------------------------------------------------------- |
7 |
|
|
8 |
|
-module(mod_muc_light_codec_backend). |
9 |
|
-author('piotr.nosek@erlang-solutions.com'). |
10 |
|
|
11 |
|
-define(MAIN_MODULE, mod_muc_light_codec). |
12 |
|
|
13 |
|
-include("mod_muc_light.hrl"). |
14 |
|
-include("jlib.hrl"). |
15 |
|
|
16 |
|
%% API |
17 |
|
-export([start/2]). |
18 |
|
-export([stop/1]). |
19 |
|
-export([decode/4]). |
20 |
|
-export([encode/5]). |
21 |
|
-export([encode_error/5]). |
22 |
|
|
23 |
|
-export([encode_error/6]). |
24 |
|
|
25 |
|
-type encoded_packet_handler() :: |
26 |
|
fun((From :: jid:jid(), To :: jid:jid(), Packet :: exml:element()) -> mongoose_acc:t()). |
27 |
|
|
28 |
|
-type decode_result() :: {ok, muc_light_packet() | muc_light_disco() | jlib:iq()} |
29 |
|
| {error, bad_request} |
30 |
|
| ignore. |
31 |
|
|
32 |
|
-export_type([encoded_packet_handler/0, decode_result/0]). |
33 |
|
|
34 |
|
%%==================================================================== |
35 |
|
%% Behaviour callbacks |
36 |
|
%%==================================================================== |
37 |
|
|
38 |
|
-callback decode(From :: jid:jid(), |
39 |
|
To :: jid:jid(), |
40 |
|
Stanza :: jlib:iq() | exml:element(), |
41 |
|
Acc :: mongoose_acc:t()) -> |
42 |
|
decode_result(). |
43 |
|
|
44 |
|
-callback encode(Request :: muc_light_encode_request(), |
45 |
|
OriginalSender :: jid:jid(), |
46 |
|
RoomJid :: jid:jid(), % may be just service domain |
47 |
|
HandleFun :: encoded_packet_handler(), |
48 |
|
Acc :: mongoose_acc:t()) -> |
49 |
|
mongoose_acc:t(). |
50 |
|
|
51 |
|
-callback encode_error(ErrMsg :: tuple(), |
52 |
|
OrigFrom :: jid:jid(), |
53 |
|
OrigTo :: jid:jid(), |
54 |
|
OrigPacket :: exml:element(), |
55 |
|
Acc :: mongoose_acc:t()) -> |
56 |
|
mongoose_acc:t(). |
57 |
|
|
58 |
|
%%==================================================================== |
59 |
|
%% API |
60 |
|
%%==================================================================== |
61 |
|
|
62 |
|
-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
63 |
|
start(HostType, Opts) -> |
64 |
87 |
mongoose_backend:init(HostType, ?MAIN_MODULE, [], Opts), |
65 |
87 |
ok. |
66 |
|
|
67 |
|
-spec stop(mongooseim:host_type()) -> ok. |
68 |
|
stop(_HostType) -> |
69 |
87 |
ok. |
70 |
|
|
71 |
|
-spec decode(From :: jid:jid(), |
72 |
|
To :: jid:jid(), |
73 |
|
Stanza :: jlib:iq() | exml:element(), |
74 |
|
Acc :: mongoose_acc:t()) -> |
75 |
|
decode_result(). |
76 |
|
decode(From, To, Stanza, Acc) -> |
77 |
868 |
HostType = mongoose_acc:host_type(Acc), |
78 |
868 |
Args = [From, To, Stanza, Acc], |
79 |
868 |
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
80 |
|
|
81 |
|
-spec encode(Request :: muc_light_encode_request(), |
82 |
|
OriginalSender :: jid:jid(), |
83 |
|
RoomJID :: jid:jid(), |
84 |
|
HandleFun :: encoded_packet_handler(), |
85 |
|
Acc :: mongoose_acc:t()) -> |
86 |
|
mongoose_acc:t(). |
87 |
|
encode(Request, Sender, RoomBareJid, HandleFun, Acc) -> |
88 |
910 |
HostType = mongoose_acc:host_type(Acc), |
89 |
910 |
Args = [Request, Sender, RoomBareJid, HandleFun, Acc], |
90 |
910 |
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
91 |
|
|
92 |
|
-spec encode_error( |
93 |
|
ErrMsg :: tuple(), OrigFrom :: jid:jid(), OrigTo :: jid:jid(), |
94 |
|
OrigPacket :: exml:element(), Acc :: mongoose_acc:t()) -> |
95 |
|
mongoose_acc:t(). |
96 |
|
encode_error(ErrMsg, OrigFrom, OrigTo, OrigPacket, Acc) -> |
97 |
28 |
HostType = mongoose_acc:host_type(Acc), |
98 |
28 |
Args = [ErrMsg, OrigFrom, OrigTo, OrigPacket, Acc], |
99 |
28 |
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
100 |
|
|
101 |
|
-spec encode_error(ErrMsg :: tuple(), |
102 |
|
ExtraChildren :: [jlib:xmlch()], |
103 |
|
OrigFrom :: jid:jid(), |
104 |
|
OrigTo :: jid:jid(), |
105 |
|
OrigPacket :: exml:element(), |
106 |
|
Acc :: mongoose_acc:t()) -> |
107 |
|
mongoose_acc:t(). |
108 |
|
encode_error(ErrMsg, ExtraChildren, OrigFrom, OrigTo, OrigPacket, Acc) -> |
109 |
28 |
ErrorElem = make_error_elem(ErrMsg), |
110 |
28 |
ErrorPacket = jlib:make_error_reply(OrigPacket#xmlel{ children = ExtraChildren }, ErrorElem), |
111 |
28 |
ejabberd_router:route(OrigTo, OrigFrom, Acc, ErrorPacket). |
112 |
|
|
113 |
|
-spec make_error_elem(tuple()) -> exml:element(). |
114 |
|
make_error_elem({error, not_allowed}) -> |
115 |
8 |
mongoose_xmpp_errors:not_allowed(); |
116 |
|
make_error_elem({error, bad_request}) -> |
117 |
3 |
mongoose_xmpp_errors:bad_request(<<"en">>, <<"Uncategorized bad request">>); |
118 |
|
make_error_elem({error, item_not_found}) -> |
119 |
5 |
mongoose_xmpp_errors:item_not_found(); |
120 |
|
make_error_elem({error, {conflict, Text}}) -> |
121 |
1 |
mongoose_xmpp_errors:conflict(<<"en">>, Text); |
122 |
|
make_error_elem({error, {bad_request, Text}}) -> |
123 |
2 |
make_error_elem({error, bad_request, Text}); |
124 |
|
make_error_elem({error, bad_request, Text}) -> |
125 |
3 |
mongoose_xmpp_errors:bad_request(<<"en">>, iolist_to_binary(Text)); |
126 |
|
make_error_elem({error, feature_not_implemented}) -> |
127 |
:-( |
mongoose_xmpp_errors:feature_not_implemented(); |
128 |
|
make_error_elem({error, {feature_not_implemented, Text}}) -> |
129 |
:-( |
mongoose_xmpp_errors:feature_not_implemented(<<"en">>, iolist_to_binary(Text)); |
130 |
|
make_error_elem({error, internal_server_error}) -> |
131 |
:-( |
mongoose_xmpp_errors:internal_server_error(); |
132 |
|
make_error_elem({error, registration_required}) -> |
133 |
1 |
mongoose_xmpp_errors:registration_required(); |
134 |
|
make_error_elem({error, _}) -> |
135 |
7 |
mongoose_xmpp_errors:bad_request(). |