• 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

51.11
/src/mod_announce_sql.erl
1
%%%-------------------------------------------------------------------
2
%%% File    : mod_announce_sql.erl
3
%%% Author  : Evgeny Khramtsov <ekhramtsov@process-one.net>
4
%%% Created : 13 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_announce_sql).
26

27
-behaviour(mod_announce).
28

29

30
%% API
31
-export([init/2, set_motd_users/2, set_motd/2, delete_motd/1,
32
         get_motd/1, is_motd_user/2, set_motd_user/2, import/3,
33
         export/1]).
34

35
-include_lib("xmpp/include/xmpp.hrl").
36
-include("mod_announce.hrl").
37
-include("ejabberd_sql_pt.hrl").
38
-include("logger.hrl").
39

40
%%%===================================================================
41
%%% API
42
%%%===================================================================
43
init(Host, _Opts) ->
44
    ejabberd_sql_schema:update_schema(Host, ?MODULE, schemas()),
3✔
45
    ok.
3✔
46

47
schemas() ->
48
    [#sql_schema{
3✔
49
        version = 1,
50
        tables =
51
            [#sql_table{
52
                name = <<"motd">>,
53
                columns =
54
                    [#sql_column{name = <<"username">>, type = text},
55
                     #sql_column{name = <<"server_host">>, type = text},
56
                     #sql_column{name = <<"xml">>, type = text},
57
                     #sql_column{name = <<"created_at">>, type = timestamp,
58
                                 default = true}],
59
                indices = [#sql_index{
60
                              columns = [<<"server_host">>, <<"username">>],
61
                              unique = true}]}]}].
62

63
set_motd_users(LServer, USRs) ->
64
    F = fun() ->
3✔
65
                lists:foreach(
3✔
66
                  fun({U, _S, _R}) ->
67
                          ?SQL_UPSERT_T(
6✔
68
                             "motd",
69
                             ["!username=%(U)s",
70
                              "!server_host=%(LServer)s",
71
                              "xml=''"])
72
                  end, USRs)
73
        end,
74
    transaction(LServer, F).
3✔
75

76
set_motd(LServer, Packet) ->
77
    XML = fxml:element_to_binary(Packet),
3✔
78
    F = fun() ->
3✔
79
                ?SQL_UPSERT_T(
3✔
80
                   "motd",
81
                   ["!username=''",
82
                    "!server_host=%(LServer)s",
83
                    "xml=%(XML)s"])
84
        end,
85
    transaction(LServer, F).
3✔
86

87
delete_motd(LServer) ->
88
    F = fun() ->
3✔
89
                ejabberd_sql:sql_query_t(
3✔
90
                  ?SQL("delete from motd where %(LServer)H"))
3✔
91
        end,
92
    transaction(LServer, F).
3✔
93

94
get_motd(LServer) ->
95
    case catch ejabberd_sql:sql_query(
3✔
96
                 LServer,
97
                 ?SQL("select @(xml)s from motd"
3✔
98
                      " where username='' and %(LServer)H")) of
99
        {selected, [{XML}]} ->
100
            parse_element(XML);
×
101
        {selected, []} ->
102
            error;
3✔
103
        _ ->
104
            {error, db_failure}
×
105
    end.
106

107
is_motd_user(LUser, LServer) ->
108
    case catch ejabberd_sql:sql_query(
3✔
109
                 LServer,
110
                 ?SQL("select @(username)s from motd"
3✔
111
                      " where username=%(LUser)s and %(LServer)H")) of
112
        {selected, [_|_]} ->
113
            {ok, true};
3✔
114
        {selected, []} ->
115
            {ok, false};
×
116
        _ ->
117
            {error, db_failure}
×
118
    end.
119

120
set_motd_user(LUser, LServer) ->
121
    F = fun() ->
×
122
                ?SQL_UPSERT_T(
×
123
                   "motd",
124
                   ["!username=%(LUser)s",
125
                    "!server_host=%(LServer)s",
126
                    "xml=''"])
127
        end,
128
    transaction(LServer, F).
×
129

130
export(_Server) ->
131
    [{motd,
×
132
      fun(Host, #motd{server = LServer, packet = El})
133
            when LServer == Host ->
134
              XML = fxml:element_to_binary(El),
×
135
              [?SQL("delete from motd where username='' and %(LServer)H;"),
×
136
               ?SQL_INSERT(
×
137
                  "motd",
138
                  ["username=''",
139
                   "server_host=%(LServer)s",
140
                   "xml=%(XML)s"])];
141
         (_Host, _R) ->
142
              []
×
143
      end},
144
     {motd_users,
145
      fun(Host, #motd_users{us = {LUser, LServer}})
146
            when LServer == Host, LUser /= <<"">> ->
147
              [?SQL("delete from motd where username=%(LUser)s and %(LServer)H;"),
×
148
               ?SQL_INSERT(
×
149
                  "motd",
150
                  ["username=%(LUser)s",
151
                   "server_host=%(LServer)s",
152
                   "xml=''"])];
153
         (_Host, _R) ->
154
              []
×
155
      end}].
156

157
import(_, _, _) ->
158
    ok.
×
159

160
%%%===================================================================
161
%%% Internal functions
162
%%%===================================================================
163
transaction(LServer, F) ->
164
    case ejabberd_sql:sql_transaction(LServer, F) of
9✔
165
        {atomic, _} -> ok;
9✔
166
        _ -> {error, db_failure}
×
167
    end.
168

169
parse_element(XML) ->
170
    case fxml_stream:parse_element(XML) of
×
171
        El when is_record(El, xmlel) ->
172
            {ok, El};
×
173
        _ ->
174
            ?ERROR_MSG("Malformed XML element in SQL table "
×
175
                       "'motd' for username='': ~ts", [XML]),
×
176
            {error, db_failure}
×
177
    end.
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