./ct_report/coverage/mod_muc_commands.COVER.html

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 Mult-user Chat (MUC)
18 %%==============================================================================
19
20 -module(mod_muc_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_instant_room/4]).
28 -export([invite_to_room/5]).
29 -export([send_message_to_room/4]).
30 -export([kick_user_from_room/3]).
31
32 -include_lib("jid/include/jid.hrl").
33
34 -ignore_xref([create_instant_room/4, invite_to_room/5, kick_user_from_room/3,
35 send_message_to_room/4]).
36
37 start(_, _) ->
38 320 mongoose_commands:register(commands()).
39
40 stop(_) ->
41 320 mongoose_commands:unregister(commands()).
42
43 -spec supported_features() -> [atom()].
44 supported_features() ->
45 148 [dynamic_domains].
46
47 commands() ->
48 640 [
49 [{name, create_muc_room},
50 {category, <<"mucs">>},
51 {desc, <<"Create a MUC room.">>},
52 {module, ?MODULE},
53 {function, create_instant_room},
54 {action, create},
55 {identifiers, [domain]},
56 {args,
57 %% The argument `domain' is what we normally term the XMPP
58 %% domain, `name' is the room name, `owner' is the XMPP entity
59 %% that would normally request an instant MUC room.
60 [{domain, binary},
61 {name, binary},
62 {owner, binary},
63 {nick, binary}]},
64 {result, {name, binary}}],
65
66 [{name, invite_to_muc_room},
67 {category, <<"mucs">>},
68 {subcategory, <<"participants">>},
69 {desc, <<"Send a MUC room invite from one user to another.">>},
70 {module, ?MODULE},
71 {function, invite_to_room},
72 {action, create},
73 {identifiers, [domain, name]},
74 {args,
75 [{domain, binary},
76 {name, binary},
77 {sender, binary},
78 {recipient, binary},
79 {reason, binary}
80 ]},
81 {result, ok}],
82
83 [{name, send_message_to_room},
84 {category, <<"mucs">>},
85 {subcategory, <<"messages">>},
86 {desc, <<"Send a message to a MUC room from a given user.">>},
87 {module, ?MODULE},
88 {function, send_message_to_room},
89 {action, create},
90 {identifiers, [domain, name]},
91 {args,
92 [{domain, binary},
93 {name, binary},
94 {from, binary},
95 {body, binary}
96 ]},
97 {result, ok}],
98
99 [{name, kick_user_from_room},
100 {category, <<"mucs">>},
101 {desc,
102 <<"Kick a user from a MUC room (on behalf of a moderator).">>},
103 {module, ?MODULE},
104 {function, kick_user_from_room},
105 {action, delete},
106 {identifiers, [domain, name, nick]},
107 {args,
108 [{domain, binary},
109 {name, binary},
110 {nick, binary}
111 ]},
112 {result, ok}]
113
114 ].
115
116 create_instant_room(Domain, Name, Owner, Nick) ->
117 5 case jid:binary_to_bare(Owner) of
118 error ->
119
:-(
error;
120 OwnerJID ->
121 5 #jid{luser = RName, lserver = MUCServer} = room_jid(Domain, Name),
122 5 case mod_muc_api:create_instant_room(MUCServer, RName, OwnerJID, Nick) of
123 5 {ok, #{title := RName}} -> RName;
124
:-(
Error -> make_rest_error(Error)
125 end
126 end.
127
128 invite_to_room(Domain, Name, Sender, Recipient, Reason) ->
129 10 case mongoose_stanza_helper:parse_from_to(Sender, Recipient) of
130 {ok, SenderJID, RecipientJID} ->
131 6 RoomJID = room_jid(Domain, Name),
132 6 case mod_muc_api:invite_to_room(RoomJID, SenderJID, RecipientJID, Reason) of
133 {ok, _} ->
134 3 ok;
135 Error ->
136 3 make_rest_error(Error)
137 end;
138 Error ->
139 4 Error
140 end.
141
142 send_message_to_room(Domain, Name, Sender, Message) ->
143 2 RoomJID = room_jid(Domain, Name),
144 2 case jid:from_binary(Sender) of
145 error ->
146
:-(
error;
147 SenderJID ->
148 2 mod_muc_api:send_message_to_room(RoomJID, SenderJID, Message)
149 end.
150
151 kick_user_from_room(Domain, Name, Nick) ->
152 1 Reason = <<"User kicked from the admin REST API">>,
153 1 RoomJID = room_jid(Domain, Name),
154 1 case mod_muc_api:kick_user_from_room(RoomJID, Nick, Reason) of
155 {ok, _} ->
156 1 ok;
157 Error ->
158
:-(
make_rest_error(Error)
159 end.
160
161 %%--------------------------------------------------------------------
162 %% Ancillary
163 %%--------------------------------------------------------------------
164
165 -spec room_jid(jid:lserver(), binary()) -> jid:jid() | error.
166 room_jid(Domain, Name) ->
167 14 {ok, HostType} = mongoose_domain_api:get_domain_host_type(Domain),
168 14 MUCDomain = mod_muc:server_host_to_muc_host(HostType, Domain),
169 14 jid:make(Name, MUCDomain, <<>>).
170
171 3 make_rest_error({room_not_found, ErrMsg}) -> {error, not_found, ErrMsg};
172
:-(
make_rest_error({user_not_found, ErrMsg}) -> {error, not_found, ErrMsg};
173
:-(
make_rest_error({moderator_not_found, ErrMsg}) -> {error, not_found, ErrMsg};
174
:-(
make_rest_error({internal, ErrMsg}) -> {error, internal, ErrMsg}.
Line Hits Source