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

26

27
%% API
28
-export([init/0, register_stream/2, unregister_stream/1, activate_stream/4]).
29

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

33
%%%===================================================================
34
%%% API
35
%%%===================================================================
36
init() ->
37
    ejabberd_sql_schema:update_schema(
×
38
      ejabberd_config:get_myname(), ?MODULE, schemas()),
39
    NodeS = erlang:atom_to_binary(node(), latin1),
×
40
    ?DEBUG("Cleaning SQL 'proxy65' table...", []),
×
41
    case ejabberd_sql:sql_query(
×
42
           ejabberd_config:get_myname(),
43
           ?SQL("delete from proxy65 where "
×
44
                "node_i=%(NodeS)s or node_t=%(NodeS)s")) of
45
        {updated, _} ->
46
            ok;
×
47
        Err ->
48
            ?ERROR_MSG("Failed to clean 'proxy65' table: ~p", [Err]),
×
49
            Err
×
50
    end.
51

52
schemas() ->
53
    [#sql_schema{
×
54
        version = 1,
55
        tables =
56
            [#sql_table{
57
                name = <<"proxy65">>,
58
                columns =
59
                    [#sql_column{name = <<"sid">>, type = text},
60
                     #sql_column{name = <<"pid_t">>, type = text},
61
                     #sql_column{name = <<"pid_i">>, type = text},
62
                     #sql_column{name = <<"node_t">>, type = text},
63
                     #sql_column{name = <<"node_i">>, type = text},
64
                     #sql_column{name = <<"jid_i">>, type = text}],
65
                indices = [#sql_index{
66
                              columns = [<<"sid">>],
67
                              unique = true},
68
                           #sql_index{
69
                              columns = [<<"jid_i">>]}]}]}].
70

71
register_stream(SID, Pid) ->
72
    PidS = misc:encode_pid(Pid),
×
73
    NodeS = erlang:atom_to_binary(node(Pid), latin1),
×
74
    F = fun() ->
×
75
                case ejabberd_sql:sql_query_t(
×
76
                       ?SQL("update proxy65 set pid_i=%(PidS)s, "
×
77
                            "node_i=%(NodeS)s where sid=%(SID)s")) of
78
                    {updated, 1} ->
79
                        ok;
×
80
                    _ ->
81
                        ejabberd_sql:sql_query_t(
×
82
                          ?SQL("insert into proxy65"
×
83
                               "(sid, pid_t, node_t, pid_i, node_i, jid_i) "
84
                               "values (%(SID)s, %(PidS)s, %(NodeS)s, '', '', '')"))
85
                end
86
        end,
87
    case ejabberd_sql:sql_transaction(ejabberd_config:get_myname(), F) of
×
88
        {atomic, _} ->
89
            ok;
×
90
        {aborted, Reason} ->
91
            {error, Reason}
×
92
    end.
93

94
unregister_stream(SID) ->
95
    F = fun() ->
×
96
                ejabberd_sql:sql_query_t(
×
97
                  ?SQL("delete from proxy65 where sid=%(SID)s"))
×
98
        end,
99
    case ejabberd_sql:sql_transaction(ejabberd_config:get_myname(), F) of
×
100
        {atomic, _} ->
101
            ok;
×
102
        {aborted, Reason} ->
103
            {error, Reason}
×
104
    end.
105

106
activate_stream(SID, IJID, MaxConnections, _Node) ->
107
    F = fun() ->
×
108
                case ejabberd_sql:sql_query_t(
×
109
                       ?SQL("select @(pid_t)s, @(node_t)s, @(pid_i)s, "
×
110
                            "@(node_i)s, @(jid_i)s from proxy65 where "
111
                            "sid=%(SID)s")) of
112
                    {selected, [{TPidS, TNodeS, IPidS, INodeS, <<"">>}]}
113
                      when IPidS /= <<"">> ->
114
                        try {misc:decode_pid(TPidS, TNodeS),
×
115
                             misc:decode_pid(IPidS, INodeS)} of
116
                            {TPid, IPid} ->
117
                                case ejabberd_sql:sql_query_t(
×
118
                                       ?SQL("update proxy65 set jid_i=%(IJID)s "
×
119
                                            "where sid=%(SID)s")) of
120
                                    {updated, 1} when is_integer(MaxConnections) ->
121
                                        case ejabberd_sql:sql_query_t(
×
122
                                               ?SQL("select @(count(*))d from proxy65 "
×
123
                                                    "where jid_i=%(IJID)s")) of
124
                                            {selected, [{Num}]} when Num > MaxConnections ->
125
                                                ejabberd_sql:abort({limit, IPid, TPid});
×
126
                                            {selected, _} ->
127
                                                {ok, IPid, TPid};
×
128
                                            Err ->
129
                                                ejabberd_sql:abort(Err)
×
130
                                        end;
131
                                    {updated, _} ->
132
                                        {ok, IPid, TPid};
×
133
                                    Err ->
134
                                        ejabberd_sql:abort(Err)
×
135
                                end
136
                        catch _:{bad_node, _} ->
137
                                {error, notfound}
×
138
                        end;
139
                    {selected, [{_, _, _, _, JID}]} when JID /= <<"">> ->
140
                        {error, conflict};
×
141
                    {selected, _} ->
142
                        {error, notfound};
×
143
                    Err ->
144
                        ejabberd_sql:abort(Err)
×
145
                end
146
        end,
147
    case ejabberd_sql:sql_transaction(ejabberd_config:get_myname(), F) of
×
148
        {atomic, Result} ->
149
            Result;
×
150
        {aborted, {limit, _, _} = Limit} ->
151
            {error, Limit};
×
152
        {aborted, Reason} ->
153
            {error, Reason}
×
154
    end.
155

156
%%%===================================================================
157
%%% Internal functions
158
%%%===================================================================
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

© 2025 Coveralls, Inc