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

25
%% API
26
-export([init/2]).
27
-export([set_channel/6, get_channels/2, get_channel/3, del_channel/3]).
28
-export([set_participant/6, get_participant/4, get_participants/3, del_participant/4]).
29
-export([subscribe/5, unsubscribe/4, unsubscribe/5, get_subscribed/4]).
30

31
-include("logger.hrl").
32
-include("ejabberd_sql_pt.hrl").
33

34
%%%===================================================================
35
%%% API
36
%%%===================================================================
37
init(Host, _Opts) ->
38
    ejabberd_sql_schema:update_schema(Host, ?MODULE, schemas()),
×
39
    ok.
×
40

41
schemas() ->
42
    [#sql_schema{
×
43
        version = 1,
44
        tables =
45
            [#sql_table{
46
                name = <<"mix_channel">>,
47
                columns =
48
                    [#sql_column{name = <<"channel">>, type = text},
49
                     #sql_column{name = <<"service">>, type = text},
50
                     #sql_column{name = <<"username">>, type = text},
51
                     #sql_column{name = <<"domain">>, type = text},
52
                     #sql_column{name = <<"jid">>, type = text},
53
                     #sql_column{name = <<"hidden">>, type = boolean},
54
                     #sql_column{name = <<"hmac_key">>, type = text},
55
                     #sql_column{name = <<"created_at">>, type = timestamp,
56
                                 default = true}],
57
                indices = [#sql_index{
58
                              columns = [<<"channel">>, <<"service">>],
59
                              unique = true},
60
                           #sql_index{
61
                              columns = [<<"service">>]}]},
62
             #sql_table{
63
                name = <<"mix_participant">>,
64
                columns =
65
                    [#sql_column{name = <<"channel">>, type = text},
66
                     #sql_column{name = <<"service">>, type = text},
67
                     #sql_column{name = <<"username">>, type = text},
68
                     #sql_column{name = <<"domain">>, type = text},
69
                     #sql_column{name = <<"jid">>, type = text},
70
                     #sql_column{name = <<"id">>, type = text},
71
                     #sql_column{name = <<"nick">>, type = text},
72
                     #sql_column{name = <<"created_at">>, type = timestamp,
73
                                 default = true}],
74
                indices = [#sql_index{
75
                              columns = [<<"channel">>, <<"service">>,
76
                                         <<"username">>, <<"domain">>],
77
                              unique = true}]},
78
             #sql_table{
79
                name = <<"mix_subscription">>,
80
                columns =
81
                    [#sql_column{name = <<"channel">>, type = text},
82
                     #sql_column{name = <<"service">>, type = {text, 75}},
83
                     #sql_column{name = <<"username">>, type = text},
84
                     #sql_column{name = <<"domain">>, type = {text, 75}},
85
                     #sql_column{name = <<"node">>, type = text},
86
                     #sql_column{name = <<"jid">>, type = text}],
87
                indices = [#sql_index{
88
                              columns = [<<"channel">>, <<"service">>,
89
                                         <<"username">>, <<"domain">>,
90
                                         <<"node">>],
91
                              unique = true},
92
                           #sql_index{
93
                              columns = [<<"channel">>, <<"service">>,
94
                                         <<"node">>]}]}]}].
95

96
set_channel(LServer, Channel, Service, CreatorJID, Hidden, Key) ->
97
    {User, Domain, _} = jid:tolower(CreatorJID),
×
98
    RawJID = jid:encode(jid:remove_resource(CreatorJID)),
×
99
    case ?SQL_UPSERT(LServer, "mix_channel",
×
100
                     ["!channel=%(Channel)s",
101
                      "!service=%(Service)s",
102
                      "username=%(User)s",
103
                      "domain=%(Domain)s",
104
                      "jid=%(RawJID)s",
105
                      "hidden=%(Hidden)b",
106
                      "hmac_key=%(Key)s"]) of
107
        ok -> ok;
×
108
        _Err -> {error, db_failure}
×
109
    end.
110

111
get_channels(LServer, Service) ->
112
    case ejabberd_sql:sql_query(
×
113
           LServer,
114
           ?SQL("select @(channel)s, @(hidden)b from mix_channel "
×
115
                "where service=%(Service)s")) of
116
        {selected, Ret} ->
117
            {ok, [Channel || {Channel, Hidden} <- Ret, Hidden == false]};
×
118
        _Err ->
119
            {error, db_failure}
×
120
    end.
121

122
get_channel(LServer, Channel, Service) ->
123
    SQL = ?SQL("select @(jid)s, @(hidden)b, @(hmac_key)s from mix_channel "
×
124
               "where channel=%(Channel)s and service=%(Service)s"),
125
    case ejabberd_sql:sql_query(LServer, SQL) of
×
126
        {selected, [{RawJID, Hidden, Key}]} ->
127
            try jid:decode(RawJID) of
×
128
                JID -> {ok, {JID, Hidden, Key}}
×
129
            catch _:{bad_jid, _} ->
130
                    report_corrupted(jid, SQL),
×
131
                    {error, db_failure}
×
132
            end;
133
        {selected, []} -> {error, notfound};
×
134
        _Err -> {error, db_failure}
×
135
    end.
136

137
del_channel(LServer, Channel, Service) ->
138
    F = fun() ->
×
139
                ejabberd_sql:sql_query_t(
×
140
                  ?SQL("delete from mix_channel where "
×
141
                       "channel=%(Channel)s and service=%(Service)s")),
142
                ejabberd_sql:sql_query_t(
×
143
                  ?SQL("delete from mix_participant where "
×
144
                       "channel=%(Channel)s and service=%(Service)s")),
145
                ejabberd_sql:sql_query_t(
×
146
                  ?SQL("delete from mix_subscription where "
×
147
                       "channel=%(Channel)s and service=%(Service)s"))
148
        end,
149
    case ejabberd_sql:sql_transaction(LServer, F) of
×
150
        {atomic, _} -> ok;
×
151
        _Err -> {error, db_failure}
×
152
    end.
153

154
set_participant(LServer, Channel, Service, JID, ID, Nick) ->
155
    {User, Domain, _} = jid:tolower(JID),
×
156
    RawJID = jid:encode(jid:remove_resource(JID)),
×
157
    case ?SQL_UPSERT(LServer, "mix_participant",
×
158
                     ["!channel=%(Channel)s",
159
                      "!service=%(Service)s",
160
                      "!username=%(User)s",
161
                      "!domain=%(Domain)s",
162
                      "jid=%(RawJID)s",
163
                      "id=%(ID)s",
164
                      "nick=%(Nick)s"]) of
165
        ok -> ok;
×
166
        _Err -> {error, db_failure}
×
167
    end.
168

169
get_participant(LServer, Channel, Service, JID) ->
170
    {User, Domain, _} = jid:tolower(JID),
×
171
    case ejabberd_sql:sql_query(
×
172
           LServer,
173
           ?SQL("select @(id)s, @(nick)s from mix_participant "
×
174
                "where channel=%(Channel)s and service=%(Service)s "
175
                "and username=%(User)s and domain=%(Domain)s")) of
176
        {selected, [Ret]} -> {ok, Ret};
×
177
        {selected, []} -> {error, notfound};
×
178
        _Err -> {error, db_failure}
×
179
    end.
180

181
get_participants(LServer, Channel, Service) ->
182
    SQL = ?SQL("select @(jid)s, @(id)s, @(nick)s from mix_participant "
×
183
               "where channel=%(Channel)s and service=%(Service)s"),
184
    case ejabberd_sql:sql_query(LServer, SQL) of
×
185
        {selected, Ret} ->
186
            {ok, lists:filtermap(
×
187
                   fun({RawJID, ID, Nick}) ->
188
                           try jid:decode(RawJID) of
×
189
                               JID -> {true, {JID, ID, Nick}}
×
190
                           catch _:{bad_jid, _} ->
191
                                   report_corrupted(jid, SQL),
×
192
                                   false
×
193
                           end
194
                   end, Ret)};
195
        _Err ->
196
            {error, db_failure}
×
197
    end.
198

199
del_participant(LServer, Channel, Service, JID) ->
200
    {User, Domain, _} = jid:tolower(JID),
×
201
    case ejabberd_sql:sql_query(
×
202
           LServer,
203
           ?SQL("delete from mix_participant where "
×
204
                "channel=%(Channel)s and service=%(Service)s "
205
                "and username=%(User)s and domain=%(Domain)s")) of
206
        {updated, _} -> ok;
×
207
        _Err -> {error, db_failure}
×
208
    end.
209

210
subscribe(_LServer, _Channel, _Service, _JID, []) ->
211
    ok;
×
212
subscribe(LServer, Channel, Service, JID, Nodes) ->
213
    {User, Domain, _} = jid:tolower(JID),
×
214
    RawJID = jid:encode(jid:remove_resource(JID)),
×
215
    F = fun() ->
×
216
                lists:foreach(
×
217
                  fun(Node) ->
218
                          ?SQL_UPSERT_T(
×
219
                             "mix_subscription",
220
                             ["!channel=%(Channel)s",
221
                              "!service=%(Service)s",
222
                              "!username=%(User)s",
223
                              "!domain=%(Domain)s",
224
                              "!node=%(Node)s",
225
                              "jid=%(RawJID)s"])
226
                  end, Nodes)
227
        end,
228
    case ejabberd_sql:sql_transaction(LServer, F) of
×
229
        {atomic, _} -> ok;
×
230
        _Err -> {error, db_failure}
×
231
    end.
232

233
get_subscribed(LServer, Channel, Service, Node) ->
234
    SQL = ?SQL("select @(jid)s from mix_subscription "
×
235
               "where channel=%(Channel)s and service=%(Service)s "
236
               "and node=%(Node)s"),
237
    case ejabberd_sql:sql_query(LServer, SQL) of
×
238
        {selected, Ret} ->
239
            {ok, lists:filtermap(
×
240
                   fun({RawJID}) ->
241
                           try jid:decode(RawJID) of
×
242
                               JID -> {true, JID}
×
243
                           catch _:{bad_jid, _} ->
244
                                   report_corrupted(jid, SQL),
×
245
                                   false
×
246
                           end
247
                   end, Ret)};
248
        _Err ->
249
            {error, db_failure}
×
250
    end.
251

252
unsubscribe(LServer, Channel, Service, JID) ->
253
    {User, Domain, _} = jid:tolower(JID),
×
254
    case ejabberd_sql:sql_query(
×
255
           LServer,
256
           ?SQL("delete from mix_subscription "
×
257
                "where channel=%(Channel)s and service=%(Service)s "
258
                "and username=%(User)s and domain=%(Domain)s")) of
259
        {updated, _} -> ok;
×
260
        _Err -> {error, db_failure}
×
261
    end.
262

263
unsubscribe(_LServer, _Channel, _Service, _JID, []) ->
264
    ok;
×
265
unsubscribe(LServer, Channel, Service, JID, Nodes) ->
266
    {User, Domain, _} = jid:tolower(JID),
×
267
    F = fun() ->
×
268
                lists:foreach(
×
269
                  fun(Node) ->
270
                          ejabberd_sql:sql_query_t(
×
271
                            ?SQL("delete from mix_subscription "
×
272
                                 "where channel=%(Channel)s "
273
                                 "and service=%(Service)s "
274
                                 "and username=%(User)s "
275
                                 "and domain=%(Domain)s "
276
                                 "and node=%(Node)s"))
277
                  end, Nodes)
278
        end,
279
    case ejabberd_sql:sql_transaction(LServer, F) of
×
280
        {atomic, ok} -> ok;
×
281
        _Err -> {error, db_failure}
×
282
    end.
283

284
%%%===================================================================
285
%%% Internal functions
286
%%%===================================================================
287
-spec report_corrupted(atom(), #sql_query{}) -> ok.
288
report_corrupted(Column, SQL) ->
289
    ?ERROR_MSG("Corrupted value of '~ts' column returned by "
×
290
               "SQL request: ~ts", [Column, SQL#sql_query.hash]).
×
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