• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

processone / ejabberd / 603

17 Oct 2023 01:57PM UTC coverage: 32.654% (-0.4%) from 33.021%
603

push

github

badlop
Fixing minor typos in CHANGELOG

13497 of 41333 relevant lines covered (32.65%)

646.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/mod_shared_roster_sql.erl
1
%%%-------------------------------------------------------------------
2
%%% File    : mod_shared_roster_sql.erl
3
%%% Author  : Evgeny Khramtsov <ekhramtsov@process-one.net>
4
%%% Created : 14 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
5
%%%
6
%%%
7
%%% ejabberd, Copyright (C) 2002-2023   ProcessOne
8
%%%
9
%%% This program is free software; you can redistribute it and/or
10
%%% modify it under the terms of the GNU General Public License as
11
%%% published by the Free Software Foundation; either version 2 of the
12
%%% License, or (at your option) any later version.
13
%%%
14
%%% This program is distributed in the hope that it will be useful,
15
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
16
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
%%% General Public License for more details.
18
%%%
19
%%% You should have received a copy of the GNU General Public License along
20
%%% with this program; if not, write to the Free Software Foundation, Inc.,
21
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
%%%
23
%%%----------------------------------------------------------------------
24

25
-module(mod_shared_roster_sql).
26

27

28
-behaviour(mod_shared_roster).
29

30
%% API
31
-export([init/2, list_groups/1, groups_with_opts/1, create_group/3,
32
         delete_group/2, get_group_opts/2, set_group_opts/3,
33
         get_user_groups/2, get_group_explicit_users/2,
34
         get_user_displayed_groups/3, is_user_in_group/3,
35
         add_user_to_group/3, remove_user_from_group/3, import/3,
36
         export/1]).
37

38
-include_lib("xmpp/include/jid.hrl").
39
-include("mod_roster.hrl").
40
-include("mod_shared_roster.hrl").
41
-include("ejabberd_sql_pt.hrl").
42

43
%%%===================================================================
44
%%% API
45
%%%===================================================================
46
init(Host, _Opts) ->
47
    ejabberd_sql_schema:update_schema(Host, ?MODULE, schemas()),
×
48
    ok.
×
49

50
schemas() ->
51
    [#sql_schema{
×
52
        version = 1,
53
        tables =
54
            [#sql_table{
55
                name = <<"sr_group">>,
56
                columns =
57
                    [#sql_column{name = <<"name">>, type = text},
58
                     #sql_column{name = <<"server_host">>, type = text},
59
                     #sql_column{name = <<"opts">>, type = text},
60
                     #sql_column{name = <<"created_at">>, type = timestamp,
61
                                 default = true}],
62
                indices = [#sql_index{
63
                              columns = [<<"server_host">>, <<"name">>],
64
                              unique = true}]},
65
             #sql_table{
66
                name = <<"sr_user">>,
67
                columns =
68
                    [#sql_column{name = <<"jid">>, type = text},
69
                     #sql_column{name = <<"server_host">>, type = text},
70
                     #sql_column{name = <<"grp">>, type = text},
71
                     #sql_column{name = <<"created_at">>, type = timestamp,
72
                                 default = true}],
73
                indices = [#sql_index{
74
                              columns = [<<"server_host">>,
75
                                         <<"jid">>, <<"grp">>],
76
                              unique = true},
77
                           #sql_index{
78
                              columns = [<<"server_host">>, <<"grp">>]}]}]}].
79

80
list_groups(Host) ->
81
    case ejabberd_sql:sql_query(
×
82
           Host,
83
           ?SQL("select @(name)s from sr_group where %(Host)H")) of
×
84
        {selected, Rs} -> [G || {G} <- Rs];
×
85
        _ -> []
×
86
    end.
87

88
groups_with_opts(Host) ->
89
    case ejabberd_sql:sql_query(
×
90
           Host,
91
           ?SQL("select @(name)s, @(opts)s from sr_group where %(Host)H"))
×
92
        of
93
      {selected, Rs} ->
94
          [{G, mod_shared_roster:opts_to_binary(ejabberd_sql:decode_term(Opts))}
×
95
           || {G, Opts} <- Rs];
×
96
      _ -> []
×
97
    end.
98

99
create_group(Host, Group, Opts) ->
100
    SOpts = misc:term_to_expr(Opts),
×
101
    F = fun () ->
×
102
                ?SQL_UPSERT_T(
×
103
                   "sr_group",
104
                   ["!name=%(Group)s",
105
                    "!server_host=%(Host)s",
106
                    "opts=%(SOpts)s"])
107
        end,
108
    ejabberd_sql:sql_transaction(Host, F).
×
109

110
delete_group(Host, Group) ->
111
    F = fun () ->
×
112
                ejabberd_sql:sql_query_t(
×
113
                  ?SQL("delete from sr_group where name=%(Group)s and %(Host)H")),
×
114
                ejabberd_sql:sql_query_t(
×
115
                  ?SQL("delete from sr_user where grp=%(Group)s and %(Host)H"))
×
116
        end,
117
    case ejabberd_sql:sql_transaction(Host, F) of
×
118
        {atomic,{updated,_}} -> {atomic, ok};
×
119
        Res -> Res
×
120
    end.
121

122
get_group_opts(Host, Group) ->
123
    case catch ejabberd_sql:sql_query(
×
124
                 Host,
125
                 ?SQL("select @(opts)s from sr_group"
×
126
                      " where name=%(Group)s and %(Host)H")) of
127
        {selected, [{SOpts}]} ->
128
            {ok, mod_shared_roster:opts_to_binary(ejabberd_sql:decode_term(SOpts))};
×
129
        _ -> error
×
130
    end.
131

132
set_group_opts(Host, Group, Opts) ->
133
    SOpts = misc:term_to_expr(Opts),
×
134
    F = fun () ->
×
135
                ?SQL_UPSERT_T(
×
136
                   "sr_group",
137
                   ["!name=%(Group)s",
138
                    "!server_host=%(Host)s",
139
                    "opts=%(SOpts)s"])
140
        end,
141
    ejabberd_sql:sql_transaction(Host, F).
×
142

143
get_user_groups(US, Host) ->
144
    SJID = make_jid_s(US),
×
145
    case catch ejabberd_sql:sql_query(
×
146
                 Host,
147
                 ?SQL("select @(grp)s from sr_user"
×
148
                      " where jid=%(SJID)s and %(Host)H")) of
149
        {selected, Rs} -> [G || {G} <- Rs];
×
150
        _ -> []
×
151
    end.
152

153
get_group_explicit_users(Host, Group) ->
154
    case catch ejabberd_sql:sql_query(
×
155
                 Host,
156
                 ?SQL("select @(jid)s from sr_user"
×
157
                      " where grp=%(Group)s and %(Host)H")) of
158
        {selected, Rs} ->
159
            lists:map(
×
160
              fun({JID}) ->
161
                      {U, S, _} = jid:tolower(jid:decode(JID)),
×
162
                      {U, S}
×
163
              end, Rs);
164
        _ ->
165
            []
×
166
    end.
167

168
get_user_displayed_groups(LUser, LServer, GroupsOpts) ->
169
    SJID = make_jid_s(LUser, LServer),
×
170
    case catch ejabberd_sql:sql_query(
×
171
                 LServer,
172
                 ?SQL("select @(grp)s from sr_user"
×
173
                      " where jid=%(SJID)s and %(LServer)H")) of
174
        {selected, Rs} ->
175
            [{Group, proplists:get_value(Group, GroupsOpts, [])}
×
176
             || {Group} <- Rs];
×
177
        _ -> []
×
178
    end.
179

180
is_user_in_group(US, Group, Host) ->
181
    SJID = make_jid_s(US),
×
182
    case catch ejabberd_sql:sql_query(
×
183
                 Host,
184
                 ?SQL("select @(jid)s from sr_user where jid=%(SJID)s"
×
185
                      " and %(Host)H and grp=%(Group)s")) of
186
        {selected, []} -> false;
×
187
        _ -> true
×
188
    end.
189

190
add_user_to_group(Host, US, Group) ->
191
    SJID = make_jid_s(US),
×
192
    ejabberd_sql:sql_query(
×
193
      Host,
194
      ?SQL_INSERT(
×
195
         "sr_user",
196
         ["jid=%(SJID)s",
197
          "server_host=%(Host)s",
198
          "grp=%(Group)s"])).
199

200
remove_user_from_group(Host, US, Group) ->
201
    SJID = make_jid_s(US),
×
202
    F = fun () ->
×
203
                ejabberd_sql:sql_query_t(
×
204
                  ?SQL("delete from sr_user where jid=%(SJID)s and %(Host)H"
×
205
                       " and grp=%(Group)s")),
206
                ok
×
207
        end,
208
    ejabberd_sql:sql_transaction(Host, F).
×
209

210
export(_Server) ->
211
    [{sr_group,
×
212
      fun(Host, #sr_group{group_host = {Group, LServer}, opts = Opts})
213
            when LServer == Host ->
214
              SOpts = misc:term_to_expr(Opts),
×
215
              [?SQL("delete from sr_group where name=%(Group)s and %(Host)H;"),
×
216
               ?SQL_INSERT(
×
217
                  "sr_group",
218
                  ["name=%(Group)s",
219
                   "server_host=%(Host)s",
220
                   "opts=%(SOpts)s"])];
221
         (_Host, _R) ->
222
              []
×
223
      end},
224
     {sr_user,
225
      fun(Host, #sr_user{us = {U, S}, group_host = {Group, LServer}})
226
            when LServer == Host ->
227
              SJID = make_jid_s(U, S),
×
228
              [?SQL("select @(jid)s from sr_user where jid=%(SJID)s"
×
229
                    " and %(Host)H and grp=%(Group)s;"),
230
               ?SQL_INSERT(
×
231
                  "sr_user",
232
                  ["jid=%(SJID)s",
233
                   "server_host=%(Host)s",
234
                   "grp=%(Group)s"])];
235
         (_Host, _R) ->
236
              []
×
237
      end}].
238

239
import(_, _, _) ->
240
    ok.
×
241

242
%%%===================================================================
243
%%% Internal functions
244
%%%===================================================================
245
make_jid_s(U, S) ->
246
    jid:encode(jid:tolower(jid:make(U, S))).
×
247

248
make_jid_s({U, S}) -> make_jid_s(U, S).
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc