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

processone / ejabberd / 1162

20 Oct 2025 08:53AM UTC coverage: 33.87% (+0.1%) from 33.752%
1162

push

github

badlop
CI: Fix updating ubuntu packages

15494 of 45746 relevant lines covered (33.87%)

1082.17 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-2025   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
-export([sql_schemas/0]).
35

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

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

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

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

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

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

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

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

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

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

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

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

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