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([%% Server |
30 |
|
status/0, |
31 |
|
%% Accounts |
32 |
|
register/3, unregister/2, |
33 |
|
import_users/1, |
34 |
|
%% Purge DB |
35 |
|
remove_from_cluster/1]). |
36 |
|
|
37 |
|
-ignore_xref([ |
38 |
|
import_users/1, |
39 |
|
register/3, remove_from_cluster/1, |
40 |
|
status/0, unregister/2]). |
41 |
|
|
42 |
|
-include("mongoose.hrl"). |
43 |
|
|
44 |
|
%%% |
45 |
|
%%% Commands |
46 |
|
%%% |
47 |
|
-spec status() -> {ok, {boolean(), iolist()}}. |
48 |
|
status() -> |
49 |
:-( |
{ok, {Status, Message, _, _}} = mongoose_server_api:status(), |
50 |
:-( |
{ok, {Status, Message}}. |
51 |
|
|
52 |
|
%%% |
53 |
|
%%% Server management |
54 |
|
%%% |
55 |
|
-spec remove_from_cluster(string()) -> {ok, string()} | |
56 |
|
{node_is_alive, string()} | |
57 |
|
{mnesia_error, string()} | |
58 |
|
{rpc_error, string()}. |
59 |
|
remove_from_cluster(NodeString) -> |
60 |
:-( |
Node = list_to_atom(NodeString), |
61 |
:-( |
IsNodeAlive = mongoose_cluster:is_node_alive(Node), |
62 |
:-( |
case IsNodeAlive of |
63 |
|
true -> |
64 |
:-( |
remove_rpc_alive_node(Node); |
65 |
|
false -> |
66 |
:-( |
remove_dead_node(Node) |
67 |
|
end. |
68 |
|
|
69 |
|
remove_dead_node(DeadNode) -> |
70 |
:-( |
try mongoose_cluster:remove_from_cluster(DeadNode) of |
71 |
|
ok -> |
72 |
:-( |
String = io_lib:format("The dead node ~p has been removed from the cluster~n", [DeadNode]), |
73 |
:-( |
{ok, String} |
74 |
|
catch |
75 |
|
error:{node_is_alive, DeadNode} -> |
76 |
:-( |
String = io_lib:format("The node ~p is alive but shoud not be.~n", [DeadNode]), |
77 |
:-( |
{node_is_alive, String}; |
78 |
|
error:{del_table_copy_schema, R} -> |
79 |
:-( |
String = io_lib:format("Cannot delete table schema~n. Reason: ~p", [R]), |
80 |
:-( |
{mnesia_error, String} |
81 |
|
end. |
82 |
|
|
83 |
|
remove_rpc_alive_node(AliveNode) -> |
84 |
:-( |
case rpc:call(AliveNode, mongoose_cluster, leave, []) of |
85 |
|
{badrpc, Reason} -> |
86 |
:-( |
String = io_lib:format("Cannot remove the node ~p~n. RPC Reason: ~p", [AliveNode, Reason]), |
87 |
:-( |
{rpc_error, String}; |
88 |
|
ok -> |
89 |
:-( |
String = io_lib:format("The node ~p has been removed from the cluster~n", [AliveNode]), |
90 |
:-( |
{ok, String}; |
91 |
|
Unknown -> |
92 |
:-( |
String = io_lib:format("Unknown error: ~p~n", [Unknown]), |
93 |
:-( |
{rpc_error, String} |
94 |
|
end. |
95 |
|
|
96 |
|
|
97 |
|
%%% |
98 |
|
%%% Account management |
99 |
|
%%% |
100 |
|
|
101 |
|
-spec register(User :: jid:user(), |
102 |
|
Host :: jid:server(), |
103 |
|
Password :: binary()) -> mongoose_account_api:register_result(). |
104 |
|
register(User, Host, Password) -> |
105 |
5963 |
mongoose_account_api:register_user(User, Host, Password). |
106 |
|
|
107 |
|
-spec unregister(User :: jid:user(), |
108 |
|
Host :: jid:server()) -> mongoose_account_api:unregister_result(). |
109 |
|
unregister(User, Host) -> |
110 |
6025 |
mongoose_account_api:unregister_user(User, Host). |
111 |
|
|
112 |
|
-spec import_users(file:filename()) -> [{binary(), jid:user() | binary()}]. |
113 |
|
import_users(Filename) -> |
114 |
:-( |
{ok, Result} = mongoose_import_users:run(Filename), |
115 |
:-( |
maps:to_list(Result). |