./ct_report/coverage/service_admin_extra_srg.COVER.html

1 %%%-------------------------------------------------------------------
2 %%% File : service_admin_extra_srg.erl
3 %%% Author : Badlop <badlop@process-one.net>, Piotr Nosek <piotr.nosek@erlang-solutions.com>
4 %%% Purpose : Contributed administrative functions and commands
5 %%% Created : 10 Aug 2008 by Badlop <badlop@process-one.net>
6 %%%
7 %%%
8 %%% ejabberd, Copyright (C) 2002-2008 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(service_admin_extra_srg).
27 -author('badlop@process-one.net').
28
29 -export([
30 commands/0,
31
32 srg_create/5,
33 srg_delete/2,
34 srg_list/1,
35 srg_get_info/2,
36 srg_get_members/2,
37 srg_user_add/4,
38 srg_user_del/4
39 ]).
40
41 -define(MOD_SHARED_ROSTER, mod_shared_roster).
42 -ignore_xref([
43 {?MOD_SHARED_ROSTER, create_group, 3},
44 {?MOD_SHARED_ROSTER, delete_group, 2},
45 {?MOD_SHARED_ROSTER, get_group_opts, 2},
46 {?MOD_SHARED_ROSTER, get_group_explicit_users, 2},
47 {?MOD_SHARED_ROSTER, list_groups, 1},
48 {?MOD_SHARED_ROSTER, add_user_to_group, 3},
49 {?MOD_SHARED_ROSTER, remove_user_from_group, 3},
50 commands/0, srg_create/5, srg_delete/2, srg_list/1, srg_get_info/2,
51 srg_get_members/2, srg_user_add/4, srg_user_del/4
52 ]).
53
54 -include("mongoose.hrl").
55 -include("ejabberd_commands.hrl").
56 -include("mod_roster.hrl").
57 -include("jlib.hrl").
58 -include_lib("exml/include/exml.hrl").
59
60 %%%
61 %%% Register commands
62 %%%
63
64 -spec commands() -> [ejabberd_commands:cmd(), ...].
65 commands() ->
66
:-(
[
67 #ejabberd_commands{name = srg_create, tags = [shared_roster_group],
68 desc = "Create a Shared Roster Group",
69 longdesc = "If you want to specify several group "
70 "identifiers in the Display argument, \n"
71 "put \\ \" around the argument and\nseparate the "
72 "identifiers with \\ \\ n\n"
73 "For example:\n"
74 " mongooseimctl srg_create group3 localhost "
75 "name desc \\\"group1\\\\ngroup2\\\"",
76 module = ?MODULE, function = srg_create,
77 args = [{group, binary}, {host, binary},
78 {name, binary}, {description, binary}, {display, binary}],
79 result = {res, rescode}},
80 #ejabberd_commands{name = srg_delete, tags = [shared_roster_group],
81 desc = "Delete a Shared Roster Group",
82 module = ?MODULE, function = srg_delete,
83 args = [{group, binary}, {host, binary}],
84 result = {res, rescode}},
85 #ejabberd_commands{name = srg_list, tags = [shared_roster_group],
86 desc = "List the Shared Roster Groups in Host",
87 module = ?MODULE, function = srg_list,
88 args = [{host, binary}],
89 result = {groups, {list, {id, string}}}},
90 #ejabberd_commands{name = srg_get_info, tags = [shared_roster_group],
91 desc = "Get info of a Shared Roster Group",
92 module = ?MODULE, function = srg_get_info,
93 args = [{group, binary}, {host, binary}],
94 result = {informations,
95 {list,
96 {information, {tuple, [{key, string}, {value, string}]}}}}},
97 #ejabberd_commands{name = srg_get_members, tags = [shared_roster_group],
98 desc = "Get members of a Shared Roster Group",
99 module = ?MODULE, function = srg_get_members,
100 args = [{group, binary}, {host, binary}],
101 result = {members, {list, {member, string}}}},
102 #ejabberd_commands{name = srg_user_add, tags = [shared_roster_group],
103 desc = "Add the JID user@host to the Shared Roster Group",
104 module = ?MODULE, function = srg_user_add,
105 args = [{user, binary}, {host, binary},
106 {group, binary}, {grouphost, binary}],
107 result = {res, rescode}},
108 #ejabberd_commands{name = srg_user_del, tags = [shared_roster_group],
109 desc = "Delete this JID user@host from the Shared Roster Group",
110 module = ?MODULE, function = srg_user_del,
111 args = [{user, binary}, {host, binary},
112 {group, binary}, {grouphost, binary}],
113 result = {res, rescode}}
114 ].
115
116 %%%
117 %%% Shared Roster Groups
118 %%%
119
120 -type group() :: binary().
121 -spec srg_create(group(), jid:server(), jid:user(),
122 Description :: binary(), Display :: binary() | []) -> 'ok'.
123 srg_create(Group, Host, Name, Description, Display) ->
124
:-(
DisplayList = case Display of
125
:-(
[] -> [];
126
:-(
_ -> binary:split(Display, <<"\\\\n">>)
127 end,
128
:-(
Opts = [{name, Name},
129 {displayed_groups, DisplayList},
130 {description, Description}],
131
:-(
{atomic, ok} = mod_shared_roster:create_group(Host, Group, Opts),
132
:-(
ok.
133
134
135 -spec srg_delete(group(), jid:server()) -> 'ok'.
136 srg_delete(Group, Host) ->
137
:-(
{atomic, ok} = mod_shared_roster:delete_group(Host, Group),
138
:-(
ok.
139
140
141 -spec srg_list(jid:server()) -> [group()].
142 srg_list(Host) ->
143
:-(
lists:sort(mod_shared_roster:list_groups(Host)).
144
145
146 -spec srg_get_info(group(), jid:server()) -> [{string(), string()}].
147 srg_get_info(Group, Host) ->
148
:-(
Opts = mod_shared_roster:get_group_opts(Host, Group),
149
:-(
[{io_lib:format("~p", [Title]),
150
:-(
io_lib:format("~p", [Value])} || {Title, Value} <- Opts].
151
152
153 -spec srg_get_members(group(), jid:server()) -> [binary()].
154 srg_get_members(Group, Host) ->
155
:-(
Members = mod_shared_roster:get_group_explicit_users(Host, Group),
156
:-(
[jid:to_binary(jid:make(MUser, MServer, <<"">>))
157
:-(
|| {MUser, MServer} <- Members].
158
159
160 -spec srg_user_add(jid:user(), jid:server(),
161 group(), GroupHost :: jid:server()) -> 'ok'.
162 srg_user_add(User, Host, Group, GroupHost) ->
163
:-(
{atomic, ok} = mod_shared_roster:add_user_to_group(GroupHost, {User, Host}, Group),
164
:-(
ok.
165
166
167 -spec srg_user_del(jid:user(), jid:server(),
168 group(), GroupHost :: jid:server()) -> 'ok'.
169 srg_user_del(User, Host, Group, GroupHost) ->
170
:-(
{atomic, ok} = mod_shared_roster:remove_user_from_group(GroupHost, {User, Host}, Group),
171
:-(
ok.
172
Line Hits Source