1 |
|
%%%------------------------------------------------------------------- |
2 |
|
%%% File : ejabberd_admin.erl |
3 |
|
%%% Author : Mickael Remond <mremond@process-one.net> |
4 |
|
%%% Purpose : Administrative functions and commands |
5 |
|
%%% Created : 7 May 2006 by Mickael Remond <mremond@process-one.net> |
6 |
|
%%% |
7 |
|
%%% |
8 |
|
%%% ejabberd, Copyright (C) 2002-2011 ProcessOne |
9 |
|
%%% |
10 |
|
%%% This program is free software; you can redistribute it and/or |
11 |
|
%%% modify it under the terms of the GNU General Public License as |
12 |
|
%%% published by the Free Software Foundation; either version 2 of the |
13 |
|
%%% License, or (at your option) any later version. |
14 |
|
%%% |
15 |
|
%%% This program is distributed in the hope that it will be useful, |
16 |
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 |
|
%%% General Public License for more details. |
19 |
|
%%% |
20 |
|
%%% You should have received a copy of the GNU General Public License |
21 |
|
%%% along with this program; if not, write to the Free Software |
22 |
|
%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 |
|
%%% |
24 |
|
%%%------------------------------------------------------------------- |
25 |
|
|
26 |
|
-module(ejabberd_admin). |
27 |
|
-author('mickael.remond@process-one.net'). |
28 |
|
|
29 |
|
-export([start/0, stop/0, |
30 |
|
%% Server |
31 |
|
status/0, |
32 |
|
%% Accounts |
33 |
|
register/3, register/2, unregister/2, |
34 |
|
registered_users/1, |
35 |
|
import_users/1, |
36 |
|
%% Purge DB |
37 |
|
delete_expired_messages/1, delete_old_messages/2, |
38 |
|
remove_from_cluster/1]). |
39 |
|
|
40 |
|
-ignore_xref([ |
41 |
|
backup_mnesia/1, delete_expired_messages/1, delete_old_messages/2, |
42 |
|
dump_mnesia/1, dump_table/2, |
43 |
|
import_users/1, install_fallback_mnesia/1, |
44 |
|
load_mnesia/1, mnesia_change_nodename/4, |
45 |
|
register/2, register/3, registered_users/1, remove_from_cluster/1, |
46 |
|
restore_mnesia/1, status/0, |
47 |
|
stop/0, unregister/2]). |
48 |
|
|
49 |
|
-include("mongoose.hrl"). |
50 |
|
|
51 |
|
start() -> |
52 |
80 |
ok. |
53 |
|
|
54 |
|
stop() -> |
55 |
:-( |
ok. |
56 |
|
|
57 |
|
%%% |
58 |
|
%%% Commands |
59 |
|
%%% |
60 |
|
-spec status() -> {ok, {boolean(), iolist()}}. |
61 |
|
status() -> |
62 |
:-( |
{ok, {Status, Message, _}} = mongoose_server_api:status(), |
63 |
:-( |
{ok, {Status, Message}}. |
64 |
|
|
65 |
|
%%% |
66 |
|
%%% Server management |
67 |
|
%%% |
68 |
|
-spec remove_from_cluster(string()) -> {ok, string()} | |
69 |
|
{node_is_alive, string()} | |
70 |
|
{mnesia_error, string()} | |
71 |
|
{rpc_error, string()}. |
72 |
|
remove_from_cluster(NodeString) -> |
73 |
:-( |
Node = list_to_atom(NodeString), |
74 |
:-( |
IsNodeAlive = mongoose_cluster:is_node_alive(Node), |
75 |
:-( |
case IsNodeAlive of |
76 |
|
true -> |
77 |
:-( |
remove_rpc_alive_node(Node); |
78 |
|
false -> |
79 |
:-( |
remove_dead_node(Node) |
80 |
|
end. |
81 |
|
|
82 |
|
remove_dead_node(DeadNode) -> |
83 |
:-( |
try mongoose_cluster:remove_from_cluster(DeadNode) of |
84 |
|
ok -> |
85 |
:-( |
String = io_lib:format("The dead node ~p has been removed from the cluster~n", [DeadNode]), |
86 |
:-( |
{ok, String} |
87 |
|
catch |
88 |
|
error:{node_is_alive, DeadNode} -> |
89 |
:-( |
String = io_lib:format("The node ~p is alive but shoud not be.~n", [DeadNode]), |
90 |
:-( |
{node_is_alive, String}; |
91 |
|
error:{del_table_copy_schema, R} -> |
92 |
:-( |
String = io_lib:format("Cannot delete table schema~n. Reason: ~p", [R]), |
93 |
:-( |
{mnesia_error, String} |
94 |
|
end. |
95 |
|
|
96 |
|
remove_rpc_alive_node(AliveNode) -> |
97 |
:-( |
case rpc:call(AliveNode, mongoose_cluster, leave, []) of |
98 |
|
{badrpc, Reason} -> |
99 |
:-( |
String = io_lib:format("Cannot remove the node ~p~n. RPC Reason: ~p", [AliveNode, Reason]), |
100 |
:-( |
{rpc_error, String}; |
101 |
|
ok -> |
102 |
:-( |
String = io_lib:format("The node ~p has been removed from the cluster~n", [AliveNode]), |
103 |
:-( |
{ok, String}; |
104 |
|
Unknown -> |
105 |
:-( |
String = io_lib:format("Unknown error: ~p~n", [Unknown]), |
106 |
:-( |
{rpc_error, String} |
107 |
|
end. |
108 |
|
|
109 |
|
|
110 |
|
%%% |
111 |
|
%%% Account management |
112 |
|
%%% |
113 |
|
|
114 |
|
-spec register(Host :: jid:server(), |
115 |
|
Password :: binary()) -> mongoose_account_api:register_result(). |
116 |
|
register(Host, Password) -> |
117 |
:-( |
{Result, _} = mongoose_account_api:register_generated_user(Host, Password), |
118 |
:-( |
Result. |
119 |
|
|
120 |
|
-spec register(User :: jid:user(), |
121 |
|
Host :: jid:server(), |
122 |
|
Password :: binary()) -> mongoose_account_api:register_result(). |
123 |
|
register(User, Host, Password) -> |
124 |
2752 |
mongoose_account_api:register_user(User, Host, Password). |
125 |
|
|
126 |
|
-spec unregister(User :: jid:user(), |
127 |
|
Host :: jid:server()) -> mongoose_account_api:unregister_result(). |
128 |
|
unregister(User, Host) -> |
129 |
2797 |
mongoose_account_api:unregister_user(User, Host). |
130 |
|
|
131 |
|
|
132 |
|
-spec registered_users(Host :: jid:server()) -> mongoose_account_api:list_user_result(). |
133 |
|
registered_users(Host) -> |
134 |
:-( |
mongoose_account_api:list_users(Host). |
135 |
|
|
136 |
|
-spec import_users(file:filename()) -> [{binary(), jid:user() | binary()}]. |
137 |
|
import_users(Filename) -> |
138 |
:-( |
{ok, Result} = mongoose_import_users:run(Filename), |
139 |
:-( |
maps:to_list(Result). |
140 |
|
|
141 |
|
%%% |
142 |
|
%%% Purge DB |
143 |
|
%%% |
144 |
|
|
145 |
|
-spec delete_expired_messages(binary()) -> {ok, iolist()} | {error, iolist()}. |
146 |
|
delete_expired_messages(Domain) -> |
147 |
:-( |
case mod_offline_api:delete_expired_messages(jid:nameprep(Domain)) of |
148 |
:-( |
{ok, _} = Result -> Result; |
149 |
:-( |
{_, Message} -> {error, Message} |
150 |
|
end. |
151 |
|
|
152 |
|
-spec delete_old_messages(binary(), Days :: integer()) -> {ok, iolist()} | {error, iolist()}. |
153 |
|
delete_old_messages(Domain, Days) -> |
154 |
:-( |
case mod_offline_api:delete_old_messages(jid:nameprep(Domain), Days) of |
155 |
:-( |
{ok, _} = Result -> Result; |
156 |
:-( |
{_, Message} -> {error, Message} |
157 |
|
end. |