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

processone / ejabberd / 1258

12 Dec 2025 03:57PM UTC coverage: 33.638% (-0.006%) from 33.644%
1258

push

github

badlop
Container: Apply commit a22c88a

ejabberdctl.template: Show meaningful error when ERL_DIST_PORT is in use

15554 of 46240 relevant lines covered (33.64%)

1078.28 hits per line

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

50.0
/src/mod_announce_mnesia.erl
1
%%%-------------------------------------------------------------------
2
%%% File    : mod_announce_mnesia.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_mnesia).
26

27
-behaviour(mod_announce).
28

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

34
-include_lib("xmpp/include/xmpp.hrl").
35
-include("mod_announce.hrl").
36
-include("logger.hrl").
37

38
%%%===================================================================
39
%%% API
40
%%%===================================================================
41
init(_Host, _Opts) ->
42
    ejabberd_mnesia:create(?MODULE, motd,
92✔
43
                        [{disc_only_copies, [node()]},
44
                         {attributes,
45
                          record_info(fields, motd)}]),
46
    ejabberd_mnesia:create(?MODULE, motd_users,
92✔
47
                        [{disc_only_copies, [node()]},
48
                         {attributes,
49
                          record_info(fields, motd_users)}]).
50

51
set_motd_users(_LServer, USRs) ->
52
    F = fun() ->
2✔
53
                lists:foreach(
2✔
54
                  fun({U, S, _R}) ->
55
                          mnesia:write(#motd_users{us = {U, S}})
4✔
56
                  end, USRs)
57
        end,
58
    transaction(F).
2✔
59

60
set_motd(LServer, Packet) ->
61
    F = fun() ->
2✔
62
                mnesia:write(#motd{server = LServer, packet = Packet})
2✔
63
        end,
64
    transaction(F).
2✔
65

66
delete_motd(LServer) ->
67
    F = fun() ->
2✔
68
                mnesia:delete({motd, LServer}),
2✔
69
                mnesia:write_lock_table(motd_users),
2✔
70
                Users = mnesia:select(
2✔
71
                          motd_users,
72
                          [{#motd_users{us = '$1', _ = '_'},
73
                            [{'==', {element, 2, '$1'}, LServer}],
74
                            ['$1']}]),
75
                lists:foreach(fun(US) ->
2✔
76
                                      mnesia:delete({motd_users, US})
×
77
                              end, Users)
78
        end,
79
    transaction(F).
2✔
80

81
get_motd(LServer) ->
82
    case mnesia:dirty_read({motd, LServer}) of
3✔
83
        [#motd{packet = Packet}] ->
84
            {ok, Packet};
×
85
        [] ->
86
            error
3✔
87
    end.
88

89
is_motd_user(LUser, LServer) ->
90
    case mnesia:dirty_read({motd_users, {LUser, LServer}}) of
2✔
91
        [#motd_users{}] -> {ok, true};
2✔
92
        _ -> {ok, false}
×
93
    end.
94

95
set_motd_user(LUser, LServer) ->
96
    F = fun() ->
×
97
                mnesia:write(#motd_users{us = {LUser, LServer}})
×
98
        end,
99
    transaction(F).
×
100

101
need_transform({motd, S, _}) when is_list(S) ->
102
    ?INFO_MSG("Mnesia table 'motd' will be converted to binary", []),
×
103
    true;
×
104
need_transform({motd_users, {U, S}, _}) when is_list(U) orelse is_list(S) ->
105
    ?INFO_MSG("Mnesia table 'motd_users' will be converted to binary", []),
×
106
    true;
×
107
need_transform(_) ->
108
    false.
×
109

110
transform(#motd{server = S, packet = P} = R) ->
111
    NewS = iolist_to_binary(S),
×
112
    NewP = fxml:to_xmlel(P),
×
113
    R#motd{server = NewS, packet = NewP};
×
114
transform(#motd_users{us = {U, S}} = R) ->
115
    NewUS = {iolist_to_binary(U), iolist_to_binary(S)},
×
116
    R#motd_users{us = NewUS}.
×
117

118
import(LServer, <<"motd">>, [<<>>, XML, _TimeStamp]) ->
119
    El = fxml_stream:parse_element(XML),
×
120
    mnesia:dirty_write(#motd{server = LServer, packet = El});
×
121
import(LServer, <<"motd">>, [LUser, <<>>, _TimeStamp]) ->
122
    mnesia:dirty_write(#motd_users{us = {LUser, LServer}}).
×
123

124
%%%===================================================================
125
%%% Internal functions
126
%%%===================================================================
127
transaction(F) ->
128
    case mnesia:transaction(F) of
6✔
129
        {atomic, Res} ->
130
            Res;
6✔
131
        {aborted, Reason} ->
132
            ?ERROR_MSG("Mnesia transaction failed: ~p", [Reason]),
×
133
            {error, db_failure}
×
134
    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