1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2016 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%% |
16 |
|
%% Author: Joseph Yiasemides <joseph.yiasemides@erlang-solutions.com> |
17 |
|
%% Description: Administration commands for MUC Light |
18 |
|
%%============================================================================== |
19 |
|
|
20 |
|
-module(mod_muc_light_commands). |
21 |
|
|
22 |
|
-behaviour(gen_mod). |
23 |
|
-behaviour(mongoose_module_metrics). |
24 |
|
|
25 |
|
-export([start/2, stop/1, supported_features/0]). |
26 |
|
|
27 |
|
-export([create_unique_room/4]). |
28 |
|
-export([create_identifiable_room/5]). |
29 |
|
-export([send_message/4]). |
30 |
|
-export([invite_to_room/4]). |
31 |
|
-export([delete_room/2]). |
32 |
|
-export([change_room_config/5]). |
33 |
|
|
34 |
|
-ignore_xref([create_identifiable_room/5, create_unique_room/4, delete_room/2, |
35 |
|
invite_to_room/4, send_message/4, change_room_config/5]). |
36 |
|
|
37 |
|
%%-------------------------------------------------------------------- |
38 |
|
%% `gen_mod' callbacks |
39 |
|
%%-------------------------------------------------------------------- |
40 |
|
|
41 |
|
start(_, _) -> |
42 |
316 |
mongoose_commands:register(commands()). |
43 |
|
|
44 |
|
stop(_) -> |
45 |
316 |
mongoose_commands:unregister(commands()). |
46 |
|
|
47 |
|
-spec supported_features() -> [atom()]. |
48 |
|
supported_features() -> |
49 |
146 |
[dynamic_domains]. |
50 |
|
|
51 |
|
%%-------------------------------------------------------------------- |
52 |
|
%% Interface descriptions |
53 |
|
%%-------------------------------------------------------------------- |
54 |
|
|
55 |
|
commands() -> |
56 |
|
|
57 |
632 |
[ |
58 |
|
[{name, create_muc_light_room}, |
59 |
|
{category, <<"muc-lights">>}, |
60 |
|
{desc, <<"Create a MUC Light room with unique username part in JID.">>}, |
61 |
|
{module, ?MODULE}, |
62 |
|
{function, create_unique_room}, |
63 |
|
{action, create}, |
64 |
|
{identifiers, [domain]}, |
65 |
|
{args, |
66 |
|
[ |
67 |
|
%% The parent `domain' under which MUC Light is |
68 |
|
%% configured. |
69 |
|
{domain, binary}, |
70 |
|
{name, binary}, |
71 |
|
{owner, binary}, |
72 |
|
{subject, binary} |
73 |
|
]}, |
74 |
|
{result, {name, binary}}], |
75 |
|
|
76 |
|
[{name, create_identifiable_muc_light_room}, |
77 |
|
{category, <<"muc-lights">>}, |
78 |
|
{desc, <<"Create a MUC Light room with user-provided username part in JID">>}, |
79 |
|
{module, ?MODULE}, |
80 |
|
{function, create_identifiable_room}, |
81 |
|
{action, update}, |
82 |
|
{identifiers, [domain]}, |
83 |
|
{args, |
84 |
|
[{domain, binary}, |
85 |
|
{id, binary}, |
86 |
|
{name, binary}, |
87 |
|
{owner, binary}, |
88 |
|
{subject, binary} |
89 |
|
]}, |
90 |
|
{result, {id, binary}}], |
91 |
|
|
92 |
|
[{name, change_muc_light_room_configuration}, |
93 |
|
{category, <<"muc-lights">>}, |
94 |
|
{subcategory, <<"config">>}, |
95 |
|
{desc, <<"Change configuration of MUC Light room.">>}, |
96 |
|
{module, ?MODULE}, |
97 |
|
{function, change_room_config}, |
98 |
|
{action, update}, |
99 |
|
{identifiers, [domain]}, |
100 |
|
{args, |
101 |
|
[ |
102 |
|
{domain, binary}, |
103 |
|
{id, binary}, |
104 |
|
{name, binary}, |
105 |
|
{user, binary}, |
106 |
|
{subject, binary} |
107 |
|
]}, |
108 |
|
{result, ok}], |
109 |
|
|
110 |
|
[{name, invite_to_room}, |
111 |
|
{category, <<"muc-lights">>}, |
112 |
|
{subcategory, <<"participants">>}, |
113 |
|
{desc, <<"Invite to a MUC Light room.">>}, |
114 |
|
{module, ?MODULE}, |
115 |
|
{function, invite_to_room}, |
116 |
|
{action, create}, |
117 |
|
{identifiers, [domain, id]}, |
118 |
|
{args, |
119 |
|
[{domain, binary}, |
120 |
|
{id, binary}, |
121 |
|
{sender, binary}, |
122 |
|
{recipient, binary} |
123 |
|
]}, |
124 |
|
{result, ok}], |
125 |
|
|
126 |
|
[{name, send_message_to_muc_light_room}, |
127 |
|
{category, <<"muc-lights">>}, |
128 |
|
{subcategory, <<"messages">>}, |
129 |
|
{desc, <<"Send a message to a MUC Light room.">>}, |
130 |
|
{module, ?MODULE}, |
131 |
|
{function, send_message}, |
132 |
|
{action, create}, |
133 |
|
{identifiers, [domain, id]}, |
134 |
|
{args, |
135 |
|
[{domain, binary}, |
136 |
|
{id, binary}, |
137 |
|
{from, binary}, |
138 |
|
{body, binary} |
139 |
|
]}, |
140 |
|
{result, ok}], |
141 |
|
|
142 |
|
[{name, delete_room}, |
143 |
|
{category, <<"muc-lights">>}, |
144 |
|
{subcategory, <<"management">>}, |
145 |
|
{desc, <<"Delete a MUC Light room.">>}, |
146 |
|
{module, ?MODULE}, |
147 |
|
{function, delete_room}, |
148 |
|
{action, delete}, |
149 |
|
{identifiers, [domain, id]}, |
150 |
|
{args, |
151 |
|
[{domain, binary}, |
152 |
|
{id, binary} |
153 |
|
]}, |
154 |
|
{result, ok}] |
155 |
|
]. |
156 |
|
|
157 |
|
|
158 |
|
%%-------------------------------------------------------------------- |
159 |
|
%% Internal procedures |
160 |
|
%%-------------------------------------------------------------------- |
161 |
|
|
162 |
|
-spec create_unique_room(jid:server(), jid:user(), jid:literal_jid(), binary()) -> |
163 |
|
jid:literal_jid() | {error, not_found | denied, iolist()}. |
164 |
|
create_unique_room(MUCServer, RoomName, Creator, Subject) -> |
165 |
2 |
CreatorJID = jid:from_binary(Creator), |
166 |
2 |
case mod_muc_light_api:create_room(MUCServer, CreatorJID, RoomName, Subject) of |
167 |
1 |
{ok, #{jid := JID}} -> jid:to_binary(JID); |
168 |
1 |
Error -> format_err_result(Error) |
169 |
|
end. |
170 |
|
|
171 |
|
-spec create_identifiable_room(jid:server(), jid:user(), binary(), |
172 |
|
jid:literal_jid(), binary()) -> |
173 |
|
jid:literal_jid() | {error, not_found | denied, iolist()}. |
174 |
|
create_identifiable_room(MUCServer, Identifier, RoomName, Creator, Subject) -> |
175 |
3 |
CreatorJID = jid:from_binary(Creator), |
176 |
3 |
case mod_muc_light_api:create_room(MUCServer, Identifier, CreatorJID, RoomName, Subject) of |
177 |
2 |
{ok, #{jid := JID}} -> jid:to_binary(JID); |
178 |
1 |
Error -> format_err_result(Error) |
179 |
|
end. |
180 |
|
|
181 |
|
-spec invite_to_room(jid:server(), jid:user(), jid:literal_jid(), jid:literal_jid()) -> |
182 |
|
ok | {error, not_found | denied, iolist()}. |
183 |
|
invite_to_room(MUCServer, RoomID, Sender, Recipient) -> |
184 |
1 |
SenderJID = jid:from_binary(Sender), |
185 |
1 |
RecipientJID = jid:from_binary(Recipient), |
186 |
1 |
RoomJID = jid:make_bare(RoomID, MUCServer), |
187 |
1 |
Result = mod_muc_light_api:invite_to_room(RoomJID, SenderJID, RecipientJID), |
188 |
1 |
format_result_no_msg(Result). |
189 |
|
|
190 |
|
-spec change_room_config(jid:server(), jid:user(), binary(), jid:literal_jid(), binary()) -> |
191 |
|
ok | {error, not_found | denied, iolist()}. |
192 |
|
change_room_config(MUCServer, RoomID, RoomName, User, Subject) -> |
193 |
:-( |
UserJID = jid:from_binary(User), |
194 |
:-( |
RoomJID = jid:make_bare(RoomID, MUCServer), |
195 |
:-( |
Result = mod_muc_light_api:change_room_config(RoomJID, UserJID, RoomName, Subject), |
196 |
:-( |
format_result_no_msg(Result). |
197 |
|
|
198 |
|
-spec send_message(jid:server(), jid:user(), jid:literal_jid(), jid:literal_jid()) -> |
199 |
|
ok | {error, not_found | denied, iolist()}. |
200 |
|
send_message(MUCServer, RoomID, Sender, Message) -> |
201 |
1 |
SenderJID = jid:from_binary(Sender), |
202 |
1 |
RoomJID = jid:make_bare(RoomID, MUCServer), |
203 |
1 |
Result = mod_muc_light_api:send_message(RoomJID, SenderJID, Message), |
204 |
1 |
format_result_no_msg(Result). |
205 |
|
|
206 |
|
-spec delete_room(jid:server(), jid:user()) -> |
207 |
|
ok | {error, not_found, iolist()}. |
208 |
|
delete_room(MUCServer, RoomID) -> |
209 |
2 |
RoomJID = jid:make_bare(RoomID, MUCServer), |
210 |
2 |
Result = mod_muc_light_api:delete_room(RoomJID), |
211 |
2 |
format_result_no_msg(Result). |
212 |
|
|
213 |
3 |
format_result_no_msg({ok, _}) -> ok; |
214 |
1 |
format_result_no_msg(Res) -> format_err_result(Res). |
215 |
|
|
216 |
|
format_err_result({ResStatus, Msg}) when room_not_found =:= ResStatus; |
217 |
|
muc_server_not_found =:= ResStatus -> |
218 |
2 |
{error, not_found, Msg}; |
219 |
|
format_err_result({ResStatus, Msg}) when already_exist =:= ResStatus; |
220 |
|
not_allowed =:= ResStatus; |
221 |
|
not_room_member =:= ResStatus -> |
222 |
1 |
{error, denied, Msg}; |
223 |
:-( |
format_err_result({_, Reason}) -> {error, internal, Reason}. |