2004-08-13 00:34:19 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_announce.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-08-13 00:34:19 +02:00
|
|
|
%%% Purpose : Manage announce messages
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 11 Aug 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2004-08-13 00:34:19 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Implements a small subset of XEP-0133: Service Administration
|
|
|
|
%%% Version 1.1 (2005-08-19)
|
2007-09-04 09:55:41 +02:00
|
|
|
|
2004-08-13 00:34:19 +02:00
|
|
|
-module(mod_announce).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2004-08-13 00:34:19 +02:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
2004-08-13 00:34:19 +02:00
|
|
|
init/0,
|
2005-06-20 05:18:13 +02:00
|
|
|
stop/1,
|
2004-08-13 00:34:19 +02:00
|
|
|
announce/3,
|
2006-01-19 03:17:31 +01:00
|
|
|
send_motd/1,
|
|
|
|
disco_identity/5,
|
|
|
|
disco_features/5,
|
|
|
|
disco_items/5,
|
|
|
|
announce_commands/4,
|
|
|
|
announce_items/4]).
|
2004-08-13 00:34:19 +02:00
|
|
|
|
2008-07-25 16:26:59 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2004-08-13 00:34:19 +02:00
|
|
|
-include("ejabberd.hrl").
|
2006-01-19 03:17:31 +01:00
|
|
|
-include("adhoc.hrl").
|
2004-08-13 00:34:19 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(motd, {server, packet}).
|
|
|
|
-record(motd_users, {us, dummy = []}).
|
2004-08-13 00:34:19 +02:00
|
|
|
|
|
|
|
-define(PROCNAME, ejabberd_announce).
|
|
|
|
|
2008-07-25 16:26:59 +02:00
|
|
|
-define(NS_ADMIN_s, "http://jabber.org/protocol/admin").
|
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
-define(NS_ADMINL(Sub), ["http:","jabber.org","protocol","admin", Sub]).
|
|
|
|
tokenize(Node) -> string:tokens(Node, "/#").
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, _Opts) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2004-08-13 00:34:19 +02:00
|
|
|
mnesia:create_table(motd, [{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, motd)}]),
|
|
|
|
mnesia:create_table(motd_users, [{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, motd_users)}]),
|
2005-04-17 20:08:34 +02:00
|
|
|
update_tables(),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(local_send_to_resource_hook, HostB,
|
2004-08-13 00:34:19 +02:00
|
|
|
?MODULE, announce, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(disco_local_identity, HostB, ?MODULE, disco_identity, 50),
|
|
|
|
ejabberd_hooks:add(disco_local_features, HostB, ?MODULE, disco_features, 50),
|
|
|
|
ejabberd_hooks:add(disco_local_items, HostB, ?MODULE, disco_items, 50),
|
|
|
|
ejabberd_hooks:add(adhoc_local_items, HostB, ?MODULE, announce_items, 50),
|
|
|
|
ejabberd_hooks:add(adhoc_local_commands, HostB, ?MODULE, announce_commands, 50),
|
|
|
|
ejabberd_hooks:add(user_available_hook, HostB,
|
2004-08-13 00:34:19 +02:00
|
|
|
?MODULE, send_motd, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
register(gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
proc_lib:spawn(?MODULE, init, [])).
|
2004-08-13 00:34:19 +02:00
|
|
|
|
|
|
|
init() ->
|
|
|
|
loop().
|
|
|
|
|
|
|
|
loop() ->
|
|
|
|
receive
|
2004-08-22 23:54:14 +02:00
|
|
|
{announce_all, From, To, Packet} ->
|
|
|
|
announce_all(From, To, Packet),
|
|
|
|
loop();
|
2007-08-03 10:53:05 +02:00
|
|
|
{announce_all_hosts_all, From, To, Packet} ->
|
|
|
|
announce_all_hosts_all(From, To, Packet),
|
|
|
|
loop();
|
2004-08-13 00:34:19 +02:00
|
|
|
{announce_online, From, To, Packet} ->
|
|
|
|
announce_online(From, To, Packet),
|
|
|
|
loop();
|
2005-04-17 20:08:34 +02:00
|
|
|
{announce_all_hosts_online, From, To, Packet} ->
|
|
|
|
announce_all_hosts_online(From, To, Packet),
|
|
|
|
loop();
|
2004-08-13 00:34:19 +02:00
|
|
|
{announce_motd, From, To, Packet} ->
|
|
|
|
announce_motd(From, To, Packet),
|
|
|
|
loop();
|
2007-08-03 10:53:05 +02:00
|
|
|
{announce_all_hosts_motd, From, To, Packet} ->
|
|
|
|
announce_all_hosts_motd(From, To, Packet),
|
|
|
|
loop();
|
2004-08-13 00:34:19 +02:00
|
|
|
{announce_motd_update, From, To, Packet} ->
|
|
|
|
announce_motd_update(From, To, Packet),
|
|
|
|
loop();
|
2007-08-03 10:53:05 +02:00
|
|
|
{announce_all_hosts_motd_update, From, To, Packet} ->
|
|
|
|
announce_all_hosts_motd_update(From, To, Packet),
|
|
|
|
loop();
|
2004-08-13 00:34:19 +02:00
|
|
|
{announce_motd_delete, From, To, Packet} ->
|
|
|
|
announce_motd_delete(From, To, Packet),
|
|
|
|
loop();
|
2007-08-03 10:53:05 +02:00
|
|
|
{announce_all_hosts_motd_delete, From, To, Packet} ->
|
|
|
|
announce_all_hosts_motd_delete(From, To, Packet),
|
|
|
|
loop();
|
2004-08-13 00:34:19 +02:00
|
|
|
_ ->
|
|
|
|
loop()
|
|
|
|
end.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(adhoc_local_commands, HostB, ?MODULE, announce_commands, 50),
|
|
|
|
ejabberd_hooks:delete(adhoc_local_items, HostB, ?MODULE, announce_items, 50),
|
|
|
|
ejabberd_hooks:delete(disco_local_identity, HostB, ?MODULE, disco_identity, 50),
|
|
|
|
ejabberd_hooks:delete(disco_local_features, HostB, ?MODULE, disco_features, 50),
|
|
|
|
ejabberd_hooks:delete(disco_local_items, HostB, ?MODULE, disco_items, 50),
|
|
|
|
ejabberd_hooks:delete(local_send_to_resource_hook, HostB,
|
2004-08-13 00:34:19 +02:00
|
|
|
?MODULE, announce, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(user_available_hook, HostB,
|
2004-08-13 00:34:19 +02:00
|
|
|
?MODULE, send_motd, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
exit(whereis(Proc), stop),
|
|
|
|
{wait, Proc}.
|
2004-08-13 00:34:19 +02:00
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
%% Announcing via messages to a custom resource
|
2004-08-13 00:34:19 +02:00
|
|
|
announce(From, To, Packet) ->
|
2009-06-01 18:42:07 +02:00
|
|
|
case {exmpp_jid:prep_node(To), exmpp_jid:prep_resource(To)} of
|
2009-01-03 16:15:38 +01:00
|
|
|
{undefined, Res} ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Name = Packet#xmlel.name,
|
2009-06-01 18:38:28 +02:00
|
|
|
Proc = gen_mod:get_module_proc(exmpp_jid:prep_domain_as_list(To), ?PROCNAME),
|
2005-06-20 05:18:13 +02:00
|
|
|
case {Res, Name} of
|
2009-01-03 16:15:38 +01:00
|
|
|
{<<"announce/all">>, 'message'} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Proc ! {announce_all, From, To, Packet},
|
|
|
|
stop;
|
2009-01-03 16:15:38 +01:00
|
|
|
{<<"announce/all-hosts/all">>, 'message'} ->
|
2007-08-03 10:53:05 +02:00
|
|
|
Proc ! {announce_all_hosts_all, From, To, Packet},
|
|
|
|
stop;
|
2009-01-03 16:15:38 +01:00
|
|
|
{<<"announce/online">>, 'message'} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Proc ! {announce_online, From, To, Packet},
|
|
|
|
stop;
|
2009-01-03 16:15:38 +01:00
|
|
|
{<<"announce/all-hosts/online">>, 'message'} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Proc ! {announce_all_hosts_online, From, To, Packet},
|
|
|
|
stop;
|
2009-01-03 16:15:38 +01:00
|
|
|
{<<"announce/motd">>, 'message'} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Proc ! {announce_motd, From, To, Packet},
|
|
|
|
stop;
|
2009-01-03 16:15:38 +01:00
|
|
|
{<<"announce/all-hosts/motd">>, 'message'} ->
|
2007-08-03 10:53:05 +02:00
|
|
|
Proc ! {announce_all_hosts_motd, From, To, Packet},
|
|
|
|
stop;
|
2009-01-03 16:15:38 +01:00
|
|
|
{<<"announce/motd/update">>, 'message'} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Proc ! {announce_motd_update, From, To, Packet},
|
|
|
|
stop;
|
2009-01-03 16:15:38 +01:00
|
|
|
{<<"announce/all-hosts/motd/update">>, 'message'} ->
|
2007-08-03 10:53:05 +02:00
|
|
|
Proc ! {announce_all_hosts_motd_update, From, To, Packet},
|
|
|
|
stop;
|
2009-01-03 16:15:38 +01:00
|
|
|
{<<"announce/motd/delete">>, 'message'} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Proc ! {announce_motd_delete, From, To, Packet},
|
|
|
|
stop;
|
2009-01-03 16:15:38 +01:00
|
|
|
{<<"announce/all-hosts/motd/delete">>, 'message'} ->
|
2007-08-03 10:53:05 +02:00
|
|
|
Proc ! {announce_all_hosts_motd_delete, From, To, Packet},
|
|
|
|
stop;
|
2005-06-20 05:18:13 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
2004-08-13 00:34:19 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
%%-------------------------------------------------------------------------
|
|
|
|
%% Announcing via ad-hoc commands
|
2006-01-19 03:17:31 +01:00
|
|
|
-define(INFO_COMMAND(Lang, Node),
|
2008-07-25 16:26:59 +02:00
|
|
|
[#xmlel{ns = ?NS_DISCO_INFO, name = 'identity', attrs =
|
2009-01-21 14:34:26 +01:00
|
|
|
[?XMLATTR('category', <<"automation">>),
|
|
|
|
?XMLATTR('type', <<"command-node">>),
|
|
|
|
?XMLATTR('name', get_title(Lang, Node))]}]).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
disco_identity(Acc, _From, _To, Node, Lang) ->
|
2009-01-08 15:54:00 +01:00
|
|
|
LNode = tokenize(binary_to_list(Node)),
|
2007-09-04 09:55:41 +02:00
|
|
|
case LNode of
|
2007-12-22 13:26:17 +01:00
|
|
|
?NS_ADMINL("announce") ->
|
|
|
|
?INFO_COMMAND(Lang, Node);
|
|
|
|
?NS_ADMINL("announce-allhosts") ->
|
|
|
|
?INFO_COMMAND(Lang, Node);
|
2007-09-04 09:55:41 +02:00
|
|
|
?NS_ADMINL("announce-all") ->
|
2006-01-19 03:17:31 +01:00
|
|
|
?INFO_COMMAND(Lang, Node);
|
2007-09-04 09:55:41 +02:00
|
|
|
?NS_ADMINL("announce-all-allhosts") ->
|
2006-01-19 03:17:31 +01:00
|
|
|
?INFO_COMMAND(Lang, Node);
|
2007-12-22 13:26:17 +01:00
|
|
|
?NS_ADMINL("set-motd") ->
|
2006-01-19 03:17:31 +01:00
|
|
|
?INFO_COMMAND(Lang, Node);
|
2007-12-22 13:26:17 +01:00
|
|
|
?NS_ADMINL("set-motd-allhosts") ->
|
2007-08-03 10:53:05 +02:00
|
|
|
?INFO_COMMAND(Lang, Node);
|
2007-12-22 13:26:17 +01:00
|
|
|
?NS_ADMINL("edit-motd") ->
|
2006-01-19 03:17:31 +01:00
|
|
|
?INFO_COMMAND(Lang, Node);
|
2007-12-22 13:26:17 +01:00
|
|
|
?NS_ADMINL("edit-motd-allhosts") ->
|
2007-08-03 10:53:05 +02:00
|
|
|
?INFO_COMMAND(Lang, Node);
|
2007-09-04 09:55:41 +02:00
|
|
|
?NS_ADMINL("delete-motd") ->
|
2006-01-19 03:17:31 +01:00
|
|
|
?INFO_COMMAND(Lang, Node);
|
2007-09-04 09:55:41 +02:00
|
|
|
?NS_ADMINL("delete-motd-allhosts") ->
|
2007-08-03 10:53:05 +02:00
|
|
|
?INFO_COMMAND(Lang, Node);
|
2006-01-19 03:17:31 +01:00
|
|
|
_ ->
|
|
|
|
Acc
|
|
|
|
end.
|
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
%%-------------------------------------------------------------------------
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
-define(INFO_RESULT(Allow, Feats),
|
2007-09-04 09:55:41 +02:00
|
|
|
case Allow of
|
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
{error, 'forbidden'};
|
2007-09-04 09:55:41 +02:00
|
|
|
allow ->
|
|
|
|
{result, Feats}
|
|
|
|
end).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
disco_features(Acc, From, To, <<"announce">>, _Lang) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2006-01-19 03:17:31 +01:00
|
|
|
case gen_mod:is_loaded(LServer, mod_adhoc) of
|
|
|
|
false ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
Access1 = gen_mod:get_module_opt(LServer, ?MODULE, access, none),
|
|
|
|
Access2 = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
case {acl:match_rule(LServer, Access1, From),
|
|
|
|
acl:match_rule(global, Access2, From)} of
|
|
|
|
{deny, deny} ->
|
2008-07-25 16:26:59 +02:00
|
|
|
{error, 'forbidden'};
|
2006-01-19 03:17:31 +01:00
|
|
|
_ ->
|
|
|
|
{result, []}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
disco_features(Acc, From, To, Node, _Lang) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2006-01-19 03:17:31 +01:00
|
|
|
case gen_mod:is_loaded(LServer, mod_adhoc) of
|
|
|
|
false ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
Access = gen_mod:get_module_opt(LServer, ?MODULE, access, none),
|
|
|
|
Allow = acl:match_rule(LServer, Access, From),
|
2007-12-22 13:26:17 +01:00
|
|
|
AccessGlobal = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
AllowGlobal = acl:match_rule(global, AccessGlobal, From),
|
2006-01-19 03:17:31 +01:00
|
|
|
case Node of
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#announce">> ->
|
2008-07-25 16:26:59 +02:00
|
|
|
?INFO_RESULT(Allow, [?NS_ADHOC_s]);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#announce-all">> ->
|
2008-07-25 16:26:59 +02:00
|
|
|
?INFO_RESULT(Allow, [?NS_ADHOC_s]);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#set-motd">> ->
|
2008-07-25 16:26:59 +02:00
|
|
|
?INFO_RESULT(Allow, [?NS_ADHOC_s]);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#edit-motd">> ->
|
2008-07-25 16:26:59 +02:00
|
|
|
?INFO_RESULT(Allow, [?NS_ADHOC_s]);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#delete-motd">> ->
|
2008-07-25 16:26:59 +02:00
|
|
|
?INFO_RESULT(Allow, [?NS_ADHOC_s]);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#announce-allhosts">> ->
|
2008-07-25 16:26:59 +02:00
|
|
|
?INFO_RESULT(AllowGlobal, [?NS_ADHOC_s]);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#announce-all-allhosts">> ->
|
2008-07-25 16:26:59 +02:00
|
|
|
?INFO_RESULT(AllowGlobal, [?NS_ADHOC_s]);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#set-motd-allhosts">> ->
|
2008-07-25 16:26:59 +02:00
|
|
|
?INFO_RESULT(AllowGlobal, [?NS_ADHOC_s]);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#edit-motd-allhosts">> ->
|
2008-07-25 16:26:59 +02:00
|
|
|
?INFO_RESULT(AllowGlobal, [?NS_ADHOC_s]);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#delete-motd-allhosts">> ->
|
2008-07-25 16:26:59 +02:00
|
|
|
?INFO_RESULT(AllowGlobal, [?NS_ADHOC_s]);
|
2006-01-19 03:17:31 +01:00
|
|
|
_ ->
|
|
|
|
Acc
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
%%-------------------------------------------------------------------------
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
-define(NODE_TO_ITEM(Lang, Server, Node),
|
2008-07-25 16:26:59 +02:00
|
|
|
#xmlel{ns = ?NS_DISCO_ITEMS, name = 'item', attrs =
|
2009-01-21 14:34:26 +01:00
|
|
|
[?XMLATTR('jid', Server),
|
|
|
|
?XMLATTR('node', Node),
|
|
|
|
?XMLATTR('name', get_title(Lang, Node))]}).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
-define(ITEMS_RESULT(Allow, Items),
|
2007-09-04 09:55:41 +02:00
|
|
|
case Allow of
|
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
{error, 'forbidden'};
|
2007-09-04 09:55:41 +02:00
|
|
|
allow ->
|
|
|
|
{result, Items}
|
|
|
|
end).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
disco_items(Acc, From, To, <<>>, Lang) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2009-01-08 15:54:00 +01:00
|
|
|
Server = exmpp_jid:domain(To),
|
2009-01-03 16:15:38 +01:00
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
case gen_mod:is_loaded(LServer, mod_adhoc) of
|
|
|
|
false ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
Access1 = gen_mod:get_module_opt(LServer, ?MODULE, access, none),
|
|
|
|
Access2 = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
case {acl:match_rule(LServer, Access1, From),
|
|
|
|
acl:match_rule(global, Access2, From)} of
|
|
|
|
{deny, deny} ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
Items = case Acc of
|
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2009-01-08 15:54:00 +01:00
|
|
|
Nodes = [?NODE_TO_ITEM(Lang, Server, <<"announce">>)],
|
2006-01-19 03:17:31 +01:00
|
|
|
{result, Items ++ Nodes}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
disco_items(Acc, From, To, <<"announce">>, Lang) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2006-01-19 03:17:31 +01:00
|
|
|
case gen_mod:is_loaded(LServer, mod_adhoc) of
|
|
|
|
false ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
announce_items(Acc, From, To, Lang)
|
|
|
|
end;
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
disco_items(Acc, From, To, Node, _Lang) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2006-01-19 03:17:31 +01:00
|
|
|
case gen_mod:is_loaded(LServer, mod_adhoc) of
|
|
|
|
false ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
Access = gen_mod:get_module_opt(LServer, ?MODULE, access, none),
|
|
|
|
Allow = acl:match_rule(LServer, Access, From),
|
2007-12-22 13:26:17 +01:00
|
|
|
AccessGlobal = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
AllowGlobal = acl:match_rule(global, AccessGlobal, From),
|
2006-01-19 03:17:31 +01:00
|
|
|
case Node of
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#announce">> ->
|
2006-01-19 03:17:31 +01:00
|
|
|
?ITEMS_RESULT(Allow, []);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#announce-all">> ->
|
2006-01-19 03:17:31 +01:00
|
|
|
?ITEMS_RESULT(Allow, []);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#set-motd">> ->
|
2006-01-19 03:17:31 +01:00
|
|
|
?ITEMS_RESULT(Allow, []);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#edit-motd">> ->
|
2006-01-19 03:17:31 +01:00
|
|
|
?ITEMS_RESULT(Allow, []);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#delete-motd">> ->
|
2007-12-22 13:26:17 +01:00
|
|
|
?ITEMS_RESULT(Allow, []);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#announce-allhosts">> ->
|
2007-12-22 13:26:17 +01:00
|
|
|
?ITEMS_RESULT(AllowGlobal, []);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#announce-all-allhosts">> ->
|
2007-12-22 13:26:17 +01:00
|
|
|
?ITEMS_RESULT(AllowGlobal, []);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#set-motd-allhosts">> ->
|
2007-12-22 13:26:17 +01:00
|
|
|
?ITEMS_RESULT(AllowGlobal, []);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#edit-motd-allhosts">> ->
|
2007-12-22 13:26:17 +01:00
|
|
|
?ITEMS_RESULT(AllowGlobal, []);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<?NS_ADMIN_s, "#delete-motd-allhosts">> ->
|
2007-12-22 13:26:17 +01:00
|
|
|
?ITEMS_RESULT(AllowGlobal, []);
|
2006-01-19 03:17:31 +01:00
|
|
|
_ ->
|
|
|
|
Acc
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
%%-------------------------------------------------------------------------
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
announce_items(Acc, From, To, Lang) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2009-01-08 15:54:00 +01:00
|
|
|
Server = exmpp_jid:domain(To),
|
2006-01-19 03:17:31 +01:00
|
|
|
Access1 = gen_mod:get_module_opt(LServer, ?MODULE, access, none),
|
|
|
|
Nodes1 = case acl:match_rule(LServer, Access1, From) of
|
|
|
|
allow ->
|
2009-01-08 15:54:00 +01:00
|
|
|
[?NODE_TO_ITEM(Lang, Server, <<?NS_ADMIN_s, "#announce">>),
|
|
|
|
?NODE_TO_ITEM(Lang, Server, <<?NS_ADMIN_s, "#announce-all">>),
|
|
|
|
?NODE_TO_ITEM(Lang, Server, <<?NS_ADMIN_s, "#set-motd">>),
|
|
|
|
?NODE_TO_ITEM(Lang, Server, <<?NS_ADMIN_s, "#edit-motd">>),
|
|
|
|
?NODE_TO_ITEM(Lang, Server, <<?NS_ADMIN_s, "#delete-motd">>)];
|
2006-01-19 03:17:31 +01:00
|
|
|
deny ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
Access2 = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
Nodes2 = case acl:match_rule(global, Access2, From) of
|
|
|
|
allow ->
|
2009-01-08 15:54:00 +01:00
|
|
|
[?NODE_TO_ITEM(Lang, Server, <<?NS_ADMIN_s, "#announce-allhosts">>),
|
|
|
|
?NODE_TO_ITEM(Lang, Server, <<?NS_ADMIN_s, "#announce-all-allhosts">>),
|
|
|
|
?NODE_TO_ITEM(Lang, Server, <<?NS_ADMIN_s, "#set-motd-allhosts">>),
|
|
|
|
?NODE_TO_ITEM(Lang, Server, <<?NS_ADMIN_s, "#edit-motd-allhosts">>),
|
|
|
|
?NODE_TO_ITEM(Lang, Server, <<?NS_ADMIN_s, "#delete-motd-allhosts">>)];
|
2006-01-19 03:17:31 +01:00
|
|
|
deny ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
case {Nodes1, Nodes2} of
|
|
|
|
{[], []} ->
|
|
|
|
Acc;
|
|
|
|
_ ->
|
|
|
|
Items = case Acc of
|
|
|
|
{result, I} -> I;
|
|
|
|
_ -> []
|
|
|
|
end,
|
|
|
|
{result, Items ++ Nodes1 ++ Nodes2}
|
|
|
|
end.
|
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
%%-------------------------------------------------------------------------
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
commands_result(Allow, From, To, Request) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
case Allow of
|
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
{error, 'forbidden'};
|
2006-01-19 03:17:31 +01:00
|
|
|
allow ->
|
|
|
|
announce_commands(From, To, Request)
|
2007-09-04 09:55:41 +02:00
|
|
|
end.
|
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
announce_commands(Acc, From, To, #adhoc_request{ node = Node} = Request) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2007-09-04 09:55:41 +02:00
|
|
|
LNode = tokenize(Node),
|
|
|
|
F = fun() ->
|
|
|
|
Access = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
Allow = acl:match_rule(global, Access, From),
|
|
|
|
commands_result(Allow, From, To, Request)
|
|
|
|
end,
|
|
|
|
R = case LNode of
|
2007-12-22 13:26:17 +01:00
|
|
|
?NS_ADMINL("announce-allhosts") -> F();
|
2007-09-04 09:55:41 +02:00
|
|
|
?NS_ADMINL("announce-all-allhosts") -> F();
|
2007-12-22 13:26:17 +01:00
|
|
|
?NS_ADMINL("set-motd-allhosts") -> F();
|
2007-09-04 09:55:41 +02:00
|
|
|
?NS_ADMINL("edit-motd-allhosts") -> F();
|
|
|
|
?NS_ADMINL("delete-motd-allhosts") -> F();
|
|
|
|
_ ->
|
|
|
|
Access = gen_mod:get_module_opt(LServer, ?MODULE, access, none),
|
|
|
|
Allow = acl:match_rule(LServer, Access, From),
|
|
|
|
case LNode of
|
|
|
|
?NS_ADMINL("announce") ->
|
|
|
|
commands_result(Allow, From, To, Request);
|
2007-12-22 13:26:17 +01:00
|
|
|
?NS_ADMINL("announce-all") ->
|
2007-09-04 09:55:41 +02:00
|
|
|
commands_result(Allow, From, To, Request);
|
2007-12-22 13:26:17 +01:00
|
|
|
?NS_ADMINL("set-motd") ->
|
2007-09-04 09:55:41 +02:00
|
|
|
commands_result(Allow, From, To, Request);
|
|
|
|
?NS_ADMINL("edit-motd") ->
|
|
|
|
commands_result(Allow, From, To, Request);
|
2007-12-22 13:26:17 +01:00
|
|
|
?NS_ADMINL("delete-motd") ->
|
|
|
|
commands_result(Allow, From, To, Request);
|
2007-09-04 09:55:41 +02:00
|
|
|
_ ->
|
|
|
|
unknown
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case R of
|
|
|
|
unknown -> Acc;
|
|
|
|
_ -> {stop, R}
|
2006-01-19 03:17:31 +01:00
|
|
|
end.
|
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
%%-------------------------------------------------------------------------
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
announce_commands(From, To,
|
|
|
|
#adhoc_request{lang = Lang,
|
|
|
|
node = Node,
|
|
|
|
action = Action,
|
|
|
|
xdata = XData} = Request) ->
|
|
|
|
%% If the "action" attribute is not present, it is
|
|
|
|
%% understood as "execute". If there was no <actions/>
|
|
|
|
%% element in the first response (which there isn't in our
|
|
|
|
%% case), "execute" and "complete" are equivalent.
|
|
|
|
ActionIsExecute = lists:member(Action,
|
|
|
|
["", "execute", "complete"]),
|
|
|
|
if Action == "cancel" ->
|
|
|
|
%% User cancels request
|
|
|
|
adhoc:produce_response(Request,
|
|
|
|
#adhoc_response{status = canceled});
|
|
|
|
XData == false, ActionIsExecute ->
|
|
|
|
%% User requests form
|
2009-06-01 18:38:28 +02:00
|
|
|
Elements = generate_adhoc_form(Lang, Node, exmpp_jid:prep_domain_as_list(To)),
|
2006-01-19 03:17:31 +01:00
|
|
|
adhoc:produce_response(
|
|
|
|
Request,
|
|
|
|
#adhoc_response{status = executing,
|
2007-09-04 09:55:41 +02:00
|
|
|
elements = [Elements]});
|
2006-01-19 03:17:31 +01:00
|
|
|
XData /= false, ActionIsExecute ->
|
|
|
|
%% User returns form.
|
|
|
|
case jlib:parse_xdata_submit(XData) of
|
|
|
|
invalid ->
|
2008-07-25 16:26:59 +02:00
|
|
|
{error, 'bad-request'};
|
2006-01-19 03:17:31 +01:00
|
|
|
Fields ->
|
|
|
|
handle_adhoc_form(From, To, Request, Fields)
|
|
|
|
end;
|
|
|
|
true ->
|
2008-07-25 16:26:59 +02:00
|
|
|
{error, 'bad-request'}
|
2006-01-19 03:17:31 +01:00
|
|
|
end.
|
|
|
|
|
2007-12-22 13:26:17 +01:00
|
|
|
-define(VVALUE(Val),
|
2009-01-08 15:54:00 +01:00
|
|
|
#xmlel{ns = ?NS_DATA_FORMS, name = 'value', children = [#xmlcdata{cdata = Val}]}).
|
2007-12-22 14:58:19 +01:00
|
|
|
-define(VVALUEL(Val),
|
|
|
|
case Val of
|
2009-01-08 15:54:00 +01:00
|
|
|
<<>> -> [];
|
2007-12-22 14:58:19 +01:00
|
|
|
_ -> [?VVALUE(Val)]
|
|
|
|
end).
|
2007-09-04 09:55:41 +02:00
|
|
|
-define(TVFIELD(Type, Var, Val),
|
2009-01-21 14:34:26 +01:00
|
|
|
#xmlel{ns = ?NS_DATA_FORMS, name = 'field', attrs = [?XMLATTR('type', Type),
|
|
|
|
?XMLATTR('var', Var)], children =
|
2007-12-22 14:58:19 +01:00
|
|
|
?VVALUEL(Val)}).
|
2009-01-08 15:54:00 +01:00
|
|
|
-define(HFIELD(), ?TVFIELD(<<"hidden">>, <<"FORM_TYPE">>, list_to_binary(?NS_ADMIN_s))).
|
2007-09-04 09:55:41 +02:00
|
|
|
|
2007-12-22 14:58:19 +01:00
|
|
|
generate_adhoc_form(Lang, Node, ServerHost) ->
|
2007-09-04 09:55:41 +02:00
|
|
|
LNode = tokenize(Node),
|
2007-12-22 14:58:19 +01:00
|
|
|
{OldSubject, OldBody} = if (LNode == ?NS_ADMINL("edit-motd"))
|
|
|
|
or (LNode == ?NS_ADMINL("edit-motd-allhosts")) ->
|
|
|
|
get_stored_motd(ServerHost);
|
|
|
|
true ->
|
|
|
|
{[], []}
|
|
|
|
end,
|
2008-07-25 16:26:59 +02:00
|
|
|
#xmlel{ns = ?NS_DATA_FORMS, name = 'x', attrs =
|
2009-01-21 14:34:26 +01:00
|
|
|
[?XMLATTR('type', <<"form">>)], children =
|
2007-09-04 09:55:41 +02:00
|
|
|
[?HFIELD(),
|
2008-07-25 16:26:59 +02:00
|
|
|
#xmlel{ns = ?NS_DATA_FORMS, name = 'title', children = [#xmlcdata{cdata = list_to_binary(get_title(Lang, Node))}]}]
|
2006-01-19 03:17:31 +01:00
|
|
|
++
|
2007-09-04 09:55:41 +02:00
|
|
|
if (LNode == ?NS_ADMINL("delete-motd"))
|
|
|
|
or (LNode == ?NS_ADMINL("delete-motd-allhosts")) ->
|
2008-07-25 16:26:59 +02:00
|
|
|
[#xmlel{ns = ?NS_DATA_FORMS, name = 'field', attrs =
|
2009-01-21 14:34:26 +01:00
|
|
|
[?XMLATTR('var', <<"confirm">>),
|
|
|
|
?XMLATTR('type', <<"boolean">>),
|
|
|
|
?XMLATTR('label', translate:translate(Lang, "Really delete message of the day?"))], children =
|
2008-07-25 16:26:59 +02:00
|
|
|
[#xmlel{ns = ?NS_DATA_FORMS, name = 'value', children =
|
|
|
|
[#xmlcdata{cdata = <<"true">>}]}]}];
|
2007-09-04 09:55:41 +02:00
|
|
|
true ->
|
2008-07-25 16:26:59 +02:00
|
|
|
[#xmlel{ns = ?NS_DATA_FORMS, name = 'field', attrs =
|
2009-01-21 14:34:26 +01:00
|
|
|
[?XMLATTR('var', <<"subject">>),
|
|
|
|
?XMLATTR('type', <<"text-single">>),
|
|
|
|
?XMLATTR('label', translate:translate(Lang, "Subject"))], children =
|
2009-01-08 15:54:00 +01:00
|
|
|
?VVALUEL(list_to_binary(OldSubject))},
|
2008-07-25 16:26:59 +02:00
|
|
|
#xmlel{ns = ?NS_DATA_FORMS, name = 'field', attrs =
|
2009-01-21 14:34:26 +01:00
|
|
|
[?XMLATTR('var', <<"body">>),
|
|
|
|
?XMLATTR('type', <<"text-multi">>),
|
|
|
|
?XMLATTR('label', translate:translate(Lang, "Message body"))], children =
|
2009-01-08 15:54:00 +01:00
|
|
|
?VVALUEL(list_to_binary(OldBody))}]
|
2007-09-04 09:55:41 +02:00
|
|
|
end}.
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
join_lines([]) ->
|
|
|
|
[];
|
|
|
|
join_lines(Lines) ->
|
|
|
|
join_lines(Lines, []).
|
|
|
|
join_lines([Line|Lines], Acc) ->
|
|
|
|
join_lines(Lines, ["\n",Line|Acc]);
|
|
|
|
join_lines([], Acc) ->
|
|
|
|
%% Remove last newline
|
|
|
|
lists:flatten(lists:reverse(tl(Acc))).
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
handle_adhoc_form(From, To,
|
2006-01-19 03:17:31 +01:00
|
|
|
#adhoc_request{lang = Lang,
|
|
|
|
node = Node,
|
|
|
|
sessionid = SessionID},
|
|
|
|
Fields) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
2006-01-19 03:17:31 +01:00
|
|
|
Confirm = case lists:keysearch("confirm", 1, Fields) of
|
|
|
|
{value, {"confirm", ["true"]}} ->
|
|
|
|
true;
|
|
|
|
{value, {"confirm", ["1"]}} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end,
|
|
|
|
Subject = case lists:keysearch("subject", 1, Fields) of
|
|
|
|
{value, {"subject", SubjectLines}} ->
|
|
|
|
%% There really shouldn't be more than one
|
|
|
|
%% subject line, but can we stop them?
|
|
|
|
join_lines(SubjectLines);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
Body = case lists:keysearch("body", 1, Fields) of
|
|
|
|
{value, {"body", BodyLines}} ->
|
|
|
|
join_lines(BodyLines);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
Response = #adhoc_response{lang = Lang,
|
|
|
|
node = Node,
|
|
|
|
sessionid = SessionID,
|
|
|
|
status = completed},
|
2009-01-21 14:34:26 +01:00
|
|
|
Packet = #xmlel{ns = ?NS_JABBER_CLIENT, name = 'message', attrs = [?XMLATTR('type', <<"normal">>)], children =
|
2006-01-19 03:17:31 +01:00
|
|
|
if Subject /= [] ->
|
2008-07-25 16:26:59 +02:00
|
|
|
[#xmlel{ns = ?NS_JABBER_CLIENT, name = 'subject', children =
|
|
|
|
[#xmlcdata{cdata = list_to_binary(Subject)}]}];
|
2006-01-19 03:17:31 +01:00
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end ++
|
|
|
|
if Body /= [] ->
|
2008-07-25 16:26:59 +02:00
|
|
|
[#xmlel{ns = ?NS_JABBER_CLIENT, name = 'body', children =
|
|
|
|
[#xmlcdata{cdata = list_to_binary(Body)}]}];
|
2006-01-19 03:17:31 +01:00
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end},
|
|
|
|
|
|
|
|
Proc = gen_mod:get_module_proc(LServer, ?PROCNAME),
|
|
|
|
case {Node, Body} of
|
2008-07-25 16:26:59 +02:00
|
|
|
{?NS_ADMIN_s ++ "#delete-motd", _} ->
|
2006-01-19 03:17:31 +01:00
|
|
|
if Confirm ->
|
|
|
|
Proc ! {announce_motd_delete, From, To, Packet},
|
|
|
|
adhoc:produce_response(Response);
|
2007-09-04 09:55:41 +02:00
|
|
|
true ->
|
2006-01-19 03:17:31 +01:00
|
|
|
adhoc:produce_response(Response)
|
|
|
|
end;
|
2008-07-25 16:26:59 +02:00
|
|
|
{?NS_ADMIN_s ++ "#delete-motd-allhosts", _} ->
|
2007-08-03 10:53:05 +02:00
|
|
|
if Confirm ->
|
|
|
|
Proc ! {announce_all_hosts_motd_delete, From, To, Packet},
|
|
|
|
adhoc:produce_response(Response);
|
2007-09-04 09:55:41 +02:00
|
|
|
true ->
|
2007-08-03 10:53:05 +02:00
|
|
|
adhoc:produce_response(Response)
|
|
|
|
end;
|
2006-01-19 03:17:31 +01:00
|
|
|
{_, []} ->
|
|
|
|
%% An announce message with no body is definitely an operator error.
|
|
|
|
%% Throw an error and give him/her a chance to send message again.
|
2008-07-25 16:26:59 +02:00
|
|
|
{error, exmpp_stanza:error(?NS_JABBER_CLIENT, 'not-acceptable',
|
|
|
|
{"en", "No body provided for announce message"})};
|
2006-01-19 03:17:31 +01:00
|
|
|
%% Now send the packet to ?PROCNAME.
|
|
|
|
%% We don't use direct announce_* functions because it
|
|
|
|
%% leads to large delay in response and <iq/> queries processing
|
2008-07-25 16:26:59 +02:00
|
|
|
{?NS_ADMIN_s ++ "#announce", _} ->
|
2007-12-22 13:26:17 +01:00
|
|
|
Proc ! {announce_online, From, To, Packet},
|
|
|
|
adhoc:produce_response(Response);
|
2008-07-25 16:26:59 +02:00
|
|
|
{?NS_ADMIN_s ++ "#announce-allhosts", _} ->
|
2007-12-22 13:26:17 +01:00
|
|
|
Proc ! {announce_all_hosts_online, From, To, Packet},
|
|
|
|
adhoc:produce_response(Response);
|
2008-07-25 16:26:59 +02:00
|
|
|
{?NS_ADMIN_s ++ "#announce-all", _} ->
|
2006-01-19 03:17:31 +01:00
|
|
|
Proc ! {announce_all, From, To, Packet},
|
|
|
|
adhoc:produce_response(Response);
|
2008-07-25 16:26:59 +02:00
|
|
|
{?NS_ADMIN_s ++ "#announce-all-allhosts", _} ->
|
2007-08-03 10:53:05 +02:00
|
|
|
Proc ! {announce_all_hosts_all, From, To, Packet},
|
|
|
|
adhoc:produce_response(Response);
|
2008-07-25 16:26:59 +02:00
|
|
|
{?NS_ADMIN_s ++ "#set-motd", _} ->
|
2006-01-19 03:17:31 +01:00
|
|
|
Proc ! {announce_motd, From, To, Packet},
|
|
|
|
adhoc:produce_response(Response);
|
2008-07-25 16:26:59 +02:00
|
|
|
{?NS_ADMIN_s ++ "#set-motd-allhosts", _} ->
|
2007-08-03 10:53:05 +02:00
|
|
|
Proc ! {announce_all_hosts_motd, From, To, Packet},
|
|
|
|
adhoc:produce_response(Response);
|
2008-07-25 16:26:59 +02:00
|
|
|
{?NS_ADMIN_s ++ "#edit-motd", _} ->
|
2006-01-19 03:17:31 +01:00
|
|
|
Proc ! {announce_motd_update, From, To, Packet},
|
|
|
|
adhoc:produce_response(Response);
|
2008-07-25 16:26:59 +02:00
|
|
|
{?NS_ADMIN_s ++ "#edit-motd-allhosts", _} ->
|
2007-08-03 10:53:05 +02:00
|
|
|
Proc ! {announce_all_hosts_motd_update, From, To, Packet},
|
|
|
|
adhoc:produce_response(Response);
|
2006-01-19 03:17:31 +01:00
|
|
|
_ ->
|
|
|
|
%% This can't happen, as we haven't registered any other
|
|
|
|
%% command nodes.
|
2008-07-25 16:26:59 +02:00
|
|
|
{error, 'internal-server-error'}
|
2006-01-19 03:17:31 +01:00
|
|
|
end.
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, Node) when is_list(Node) ->
|
|
|
|
get_title(Lang, list_to_binary(Node));
|
|
|
|
get_title(Lang, <<"announce">>) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
translate:translate(Lang, "Announcements");
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, <<?NS_ADMIN_s, "#announce-all">>) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
translate:translate(Lang, "Send announcement to all users");
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, <<?NS_ADMIN_s , "#announce-all-allhosts">>) ->
|
2007-08-03 10:53:05 +02:00
|
|
|
translate:translate(Lang, "Send announcement to all users on all hosts");
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, <<?NS_ADMIN_s , "#announce">>) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
translate:translate(Lang, "Send announcement to all online users");
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, <<?NS_ADMIN_s , "#announce-allhosts">>) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
translate:translate(Lang, "Send announcement to all online users on all hosts");
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, <<?NS_ADMIN_s , "#set-motd">>) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
translate:translate(Lang, "Set message of the day and send to online users");
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, <<?NS_ADMIN_s , "#set-motd-allhosts">>) ->
|
2007-08-03 10:53:05 +02:00
|
|
|
translate:translate(Lang, "Set message of the day on all hosts and send to online users");
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, <<?NS_ADMIN_s , "#edit-motd">>) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
translate:translate(Lang, "Update message of the day (don't send)");
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, <<?NS_ADMIN_s , "#edit-motd-allhosts">>) ->
|
2007-08-03 10:53:05 +02:00
|
|
|
translate:translate(Lang, "Update message of the day on all hosts (don't send)");
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, <<?NS_ADMIN_s , "#delete-motd">>) ->
|
2007-08-03 10:53:05 +02:00
|
|
|
translate:translate(Lang, "Delete message of the day");
|
2009-02-18 19:14:06 +01:00
|
|
|
get_title(Lang, <<?NS_ADMIN_s , "#delete-motd-allhosts">>) ->
|
2007-08-03 10:53:05 +02:00
|
|
|
translate:translate(Lang, "Delete message of the day on all hosts").
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
%%-------------------------------------------------------------------------
|
2006-01-19 03:17:31 +01:00
|
|
|
|
2004-08-22 23:54:14 +02:00
|
|
|
announce_all(From, To, Packet) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(To),
|
2005-06-20 05:18:13 +02:00
|
|
|
Access = gen_mod:get_module_opt(Host, ?MODULE, access, none),
|
|
|
|
case acl:match_rule(Host, Access, From) of
|
2004-08-22 23:54:14 +02:00
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'forbidden'),
|
2004-08-22 23:54:14 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
2009-06-01 18:26:00 +02:00
|
|
|
Local = exmpp_jid:make(exmpp_jid:domain(To)),
|
2004-08-22 23:54:14 +02:00
|
|
|
lists:foreach(
|
2007-09-04 09:55:41 +02:00
|
|
|
fun({User, Server}) ->
|
2009-06-01 18:26:00 +02:00
|
|
|
Dest = exmpp_jid:make(User, Server),
|
2007-09-04 09:55:41 +02:00
|
|
|
ejabberd_router:route(Local, Dest, Packet)
|
|
|
|
end, ejabberd_auth:get_vh_registered_users(Host))
|
2004-08-22 23:54:14 +02:00
|
|
|
end.
|
|
|
|
|
2007-08-03 10:53:05 +02:00
|
|
|
announce_all_hosts_all(From, To, Packet) ->
|
|
|
|
Access = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
case acl:match_rule(global, Access, From) of
|
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'forbidden'),
|
2007-08-03 10:53:05 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
2009-06-01 18:26:00 +02:00
|
|
|
Local = exmpp_jid:make(exmpp_jid:domain(To)),
|
2007-08-03 10:53:05 +02:00
|
|
|
lists:foreach(
|
2007-09-04 09:55:41 +02:00
|
|
|
fun({User, Server}) ->
|
2009-06-01 18:26:00 +02:00
|
|
|
Dest = exmpp_jid:make(User, Server),
|
2007-09-04 09:55:41 +02:00
|
|
|
ejabberd_router:route(Local, Dest, Packet)
|
|
|
|
end, ejabberd_auth:dirty_get_registered_users())
|
2007-08-03 10:53:05 +02:00
|
|
|
end.
|
|
|
|
|
2004-08-13 00:34:19 +02:00
|
|
|
announce_online(From, To, Packet) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(To),
|
2005-06-20 05:18:13 +02:00
|
|
|
Access = gen_mod:get_module_opt(Host, ?MODULE, access, none),
|
|
|
|
case acl:match_rule(Host, Access, From) of
|
2004-08-13 00:34:19 +02:00
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'forbidden'),
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
2009-06-01 18:37:15 +02:00
|
|
|
announce_online1(ejabberd_sm:get_vh_session_list(exmpp_jid:prep_domain(To)),
|
2009-01-03 16:15:38 +01:00
|
|
|
exmpp_jid:domain_as_list(To),
|
2005-04-17 20:08:34 +02:00
|
|
|
Packet)
|
2004-08-13 00:34:19 +02:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
announce_all_hosts_online(From, To, Packet) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Access = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
case acl:match_rule(global, Access, From) of
|
2005-04-17 20:08:34 +02:00
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'forbidden'),
|
2005-04-17 20:08:34 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
|
|
|
announce_online1(ejabberd_sm:dirty_get_sessions_list(),
|
2009-01-03 16:15:38 +01:00
|
|
|
exmpp_jid:domain_as_list(To),
|
2005-04-17 20:08:34 +02:00
|
|
|
Packet)
|
|
|
|
end.
|
|
|
|
|
|
|
|
announce_online1(Sessions, Server, Packet) ->
|
2009-06-01 18:26:00 +02:00
|
|
|
Local = exmpp_jid:make(Server),
|
2004-08-13 00:34:19 +02:00
|
|
|
lists:foreach(
|
2005-04-17 20:08:34 +02:00
|
|
|
fun({U, S, R}) ->
|
2009-06-01 18:26:00 +02:00
|
|
|
Dest = exmpp_jid:make(U, S, R),
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_router:route(Local, Dest, Packet)
|
|
|
|
end, Sessions).
|
|
|
|
|
|
|
|
announce_motd(From, To, Packet) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(To),
|
2005-06-20 05:18:13 +02:00
|
|
|
Access = gen_mod:get_module_opt(Host, ?MODULE, access, none),
|
|
|
|
case acl:match_rule(Host, Access, From) of
|
2004-08-13 00:34:19 +02:00
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'forbidden'),
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
2007-09-04 09:55:41 +02:00
|
|
|
announce_motd(Host, Packet)
|
2007-08-03 10:53:05 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
announce_all_hosts_motd(From, To, Packet) ->
|
|
|
|
Access = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
case acl:match_rule(global, Access, From) of
|
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'forbidden'),
|
2007-08-03 10:53:05 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
|
|
|
Hosts = ?MYHOSTS,
|
|
|
|
[announce_motd(Host, Packet) || Host <- Hosts]
|
2004-08-13 00:34:19 +02:00
|
|
|
end.
|
|
|
|
|
2007-08-03 10:53:05 +02:00
|
|
|
announce_motd(Host, Packet) ->
|
|
|
|
announce_motd_update(Host, Packet),
|
2009-01-03 16:15:38 +01:00
|
|
|
Sessions = ejabberd_sm:get_vh_session_list(list_to_binary(Host)),
|
2007-08-03 10:53:05 +02:00
|
|
|
announce_online1(Sessions, Host, Packet),
|
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({U, S, _R}) ->
|
|
|
|
mnesia:write(#motd_users{us = {U, S}})
|
|
|
|
end, Sessions)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2004-08-13 00:34:19 +02:00
|
|
|
announce_motd_update(From, To, Packet) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(To),
|
2005-06-20 05:18:13 +02:00
|
|
|
Access = gen_mod:get_module_opt(Host, ?MODULE, access, none),
|
|
|
|
case acl:match_rule(Host, Access, From) of
|
2004-08-13 00:34:19 +02:00
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'forbidden'),
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
2005-06-20 05:18:13 +02:00
|
|
|
announce_motd_update(Host, Packet)
|
2004-08-13 00:34:19 +02:00
|
|
|
end.
|
|
|
|
|
2007-08-03 10:53:05 +02:00
|
|
|
announce_all_hosts_motd_update(From, To, Packet) ->
|
|
|
|
Access = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
case acl:match_rule(global, Access, From) of
|
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'forbidden'),
|
2007-08-03 10:53:05 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
|
|
|
Hosts = ?MYHOSTS,
|
|
|
|
[announce_motd_update(Host, Packet) || Host <- Hosts]
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
announce_motd_update(LServer, Packet) ->
|
|
|
|
announce_motd_delete(LServer),
|
2004-08-13 00:34:19 +02:00
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:write(#motd{server = LServer, packet = Packet})
|
2004-08-13 00:34:19 +02:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
announce_motd_delete(From, To, Packet) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(To),
|
2005-06-20 05:18:13 +02:00
|
|
|
Access = gen_mod:get_module_opt(Host, ?MODULE, access, none),
|
|
|
|
case acl:match_rule(Host, Access, From) of
|
2004-08-13 00:34:19 +02:00
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'forbidden'),
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
2005-06-20 05:18:13 +02:00
|
|
|
announce_motd_delete(Host)
|
2004-08-13 00:34:19 +02:00
|
|
|
end.
|
|
|
|
|
2007-08-03 10:53:05 +02:00
|
|
|
announce_all_hosts_motd_delete(From, To, Packet) ->
|
|
|
|
Access = gen_mod:get_module_opt(global, ?MODULE, access, none),
|
|
|
|
case acl:match_rule(global, Access, From) of
|
|
|
|
deny ->
|
2008-07-25 16:26:59 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'forbidden'),
|
2007-08-03 10:53:05 +02:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
|
|
|
Hosts = ?MYHOSTS,
|
|
|
|
[announce_motd_delete(Host) || Host <- Hosts]
|
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
announce_motd_delete(LServer) ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({motd, LServer}),
|
|
|
|
mnesia:write_lock_table(motd_users),
|
|
|
|
Users = mnesia:select(
|
|
|
|
motd_users,
|
|
|
|
[{#motd_users{us = '$1', _ = '_'},
|
|
|
|
[{'==', {element, 2, '$1'}, LServer}],
|
|
|
|
['$1']}]),
|
|
|
|
lists:foreach(fun(US) ->
|
|
|
|
mnesia:delete({motd_users, US})
|
|
|
|
end, Users)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
2004-08-13 00:34:19 +02:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
send_motd(JID) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(JID),
|
2009-06-01 18:40:51 +02:00
|
|
|
LUser = exmpp_jid:prep_node_as_list(JID),
|
2005-04-17 20:08:34 +02:00
|
|
|
case catch mnesia:dirty_read({motd, LServer}) of
|
2004-08-13 00:34:19 +02:00
|
|
|
[#motd{packet = Packet}] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
case catch mnesia:dirty_read({motd_users, US}) of
|
2004-08-13 00:34:19 +02:00
|
|
|
[#motd_users{}] ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
2009-06-01 18:37:15 +02:00
|
|
|
Local = exmpp_jid:make(exmpp_jid:prep_domain(JID)),
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_router:route(Local, JID, Packet),
|
|
|
|
F = fun() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:write(#motd_users{us = US})
|
2004-08-13 00:34:19 +02:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2007-12-22 14:58:19 +01:00
|
|
|
get_stored_motd(LServer) ->
|
|
|
|
case catch mnesia:dirty_read({motd, LServer}) of
|
|
|
|
[#motd{packet = Packet}] ->
|
2008-10-02 15:34:52 +02:00
|
|
|
{exmpp_xml:get_cdata_as_list(exmpp_xml:get_element(Packet, 'subject')),
|
|
|
|
exmpp_xml:get_cdata_as_list(exmpp_xml:get_element(Packet, 'body'))};
|
2007-12-22 14:58:19 +01:00
|
|
|
_ ->
|
|
|
|
{"", ""}
|
|
|
|
end.
|
|
|
|
|
2007-09-04 09:55:41 +02:00
|
|
|
%%-------------------------------------------------------------------------
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
update_tables() ->
|
|
|
|
update_motd_table(),
|
|
|
|
update_motd_users_table().
|
|
|
|
|
|
|
|
update_motd_table() ->
|
|
|
|
Fields = record_info(fields, motd),
|
|
|
|
case mnesia:table_info(motd, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
|
|
|
[id, packet] ->
|
|
|
|
?INFO_MSG("Converting motd table from "
|
|
|
|
"{id, packet} format", []),
|
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_announce_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, motd},
|
|
|
|
{attributes, record_info(fields, motd)}]),
|
|
|
|
mnesia:transform_table(motd, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_announce_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#motd{server = _} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_announce_tmp_table,
|
|
|
|
R#motd{server = Host})
|
|
|
|
end, ok, motd)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(motd),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(motd),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_announce_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_announce_tmp_table);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating motd table", []),
|
|
|
|
mnesia:transform_table(motd, ignore, Fields)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
update_motd_users_table() ->
|
|
|
|
Fields = record_info(fields, motd_users),
|
|
|
|
case mnesia:table_info(motd_users, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
|
|
|
[luser, dummy] ->
|
|
|
|
?INFO_MSG("Converting motd_users table from "
|
|
|
|
"{luser, dummy} format", []),
|
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_announce_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, motd_users},
|
|
|
|
{attributes, record_info(fields, motd_users)}]),
|
|
|
|
mnesia:transform_table(motd_users, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_announce_tmp_table),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(#motd_users{us = U} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_announce_tmp_table,
|
|
|
|
R#motd_users{us = {U, Host}})
|
|
|
|
end, ok, motd_users)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(motd_users),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(motd_users),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_announce_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_announce_tmp_table);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating motd_users table", []),
|
|
|
|
mnesia:transform_table(motd_users, ignore, Fields)
|
|
|
|
end.
|