2007-12-01 06:16:30 +01:00
%%% ====================================================================
2007-12-24 14:57:53 +01:00
%%% ``The contents of this file are subject to the Erlang Public License,
%%% Version 1.1, (the "License"); you may not use this file except in
%%% compliance with the License. You should have received a copy of the
%%% Erlang Public License along with this software. If not, it can be
%%% retrieved via the world wide web at http://www.erlang.org/.
%%%
%%% Software distributed under the License is distributed on an "AS IS"
%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%%% the License for the specific language governing rights and limitations
%%% under the License.
%%%
2008-07-31 13:18:49 +02:00
%%% The Initial Developer of the Original Code is ProcessOne.
2010-01-12 17:11:32 +01:00
%%% Portions created by ProcessOne are Copyright 2006-2010, ProcessOne
2007-12-24 14:57:53 +01:00
%%% All Rights Reserved.''
2010-01-12 17:11:32 +01:00
%%% This software is copyright 2006-2010, ProcessOne.
2007-12-01 06:16:30 +01:00
%%%
2010-01-12 17:11:32 +01:00
%%% @copyright 2006-2010 ProcessOne
2007-12-01 06:16:30 +01:00
%%% @author Christophe Romain <christophe.romain@process-one.net>
%%% [http://www.process-one.net/]
%%% @version {@vsn}, {@date} {@time}
%%% @end
%%% ====================================================================
%%% @doc The module <strong>{@module}</strong> is the core of the PubSub
%%% extension. It relies on PubSub plugins for a large part of its functions.
%%%
%%% @headerfile "pubsub.hrl"
%%%
%%% @reference See <a href="http://www.xmpp.org/extensions/xep-0060.html">XEP-0060: Pubsub</a> for
%%% the latest version of the PubSub specification.
2009-01-03 01:25:40 +01:00
%%% This module uses version 1.12 of the specification as a base.
2007-12-01 06:16:30 +01:00
%%% Most of the specification is implemented.
2008-07-03 11:56:31 +02:00
%%% Functions concerning configuration should be rewritten.
2009-06-15 15:58:40 +02:00
%%%
%%% Support for subscription-options and multi-subscribe features was
2009-08-17 19:16:43 +02:00
%%% added by Brian Cully (bjc AT kublai.com). Subscriptions and options are
2009-06-15 15:58:40 +02:00
%%% stored in the pubsub_subscription table, with a link to them provided
%%% by the subscriptions field of pubsub_state. For information on
%%% subscription-options and mulit-subscribe see XEP-0060 sections 6.1.6,
%%% 6.2.3.1, 6.2.3.5, and 6.3. For information on subscription leases see
%%% XEP-0060 section 12.18.
2007-12-01 06:16:30 +01:00
2003-07-08 22:11:27 +02:00
- module ( mod_pubsub ) .
2008-07-03 11:56:31 +02:00
- author ( 'christophe.romain@process-one.net' ) .
2009-09-24 21:28:43 +02:00
- version ( '1.13-0' ) .
2003-07-08 22:11:27 +02:00
2006-02-02 06:00:27 +01:00
- behaviour ( gen_server ) .
2003-07-08 22:11:27 +02:00
- behaviour ( gen_mod ) .
- include ( " ejabberd.hrl " ) .
2009-07-20 10:53:56 +02:00
- include ( " adhoc.hrl " ) .
2003-07-08 22:11:27 +02:00
- include ( " jlib.hrl " ) .
2007-12-01 06:16:30 +01:00
- include ( " pubsub.hrl " ) .
2009-05-29 00:30:43 +02:00
- define ( STDTREE , " tree " ) .
- define ( STDNODE , " flat " ) .
2007-12-10 15:09:31 +01:00
- define ( PEPNODE , " pep " ) .
2007-12-01 06:16:30 +01:00
%% exports for hooks
2007-12-22 01:04:05 +01:00
- export ( [ presence_probe / 3 ,
2009-05-08 01:35:59 +02:00
in_subscription / 6 ,
2009-03-10 11:35:57 +01:00
out_subscription / 4 ,
2010-03-05 11:11:44 +01:00
on_user_offline / 3 ,
2007-12-18 15:45:26 +01:00
remove_user / 2 ,
2010-03-05 10:34:15 +01:00
feature_check_packet / 6 ,
2007-12-01 06:16:30 +01:00
disco_local_identity / 5 ,
2007-12-11 17:19:17 +01:00
disco_local_features / 5 ,
disco_local_items / 5 ,
2007-12-01 06:16:30 +01:00
disco_sm_identity / 5 ,
disco_sm_features / 5 ,
disco_sm_items / 5
] ) .
%% exported iq handlers
- export ( [ iq_local / 3 ,
iq_sm / 3
] ) .
%% exports for console debug manual use
- export ( [ create_node / 5 ,
delete_node / 3 ,
2009-05-29 04:14:07 +02:00
subscribe_node / 5 ,
2007-12-01 06:16:30 +01:00
unsubscribe_node / 5 ,
publish_item / 6 ,
delete_item / 4 ,
2009-04-30 07:18:06 +02:00
send_items / 6 ,
2009-05-29 00:30:43 +02:00
get_items / 2 ,
get_item / 3 ,
get_cached_item / 2 ,
2010-01-12 11:33:39 +01:00
broadcast_stanza / 9 ,
2009-03-20 01:08:38 +01:00
get_configure / 5 ,
2007-12-01 06:16:30 +01:00
set_configure / 5 ,
2008-09-25 19:26:06 +02:00
tree_action / 3 ,
2007-12-01 06:16:30 +01:00
node_action / 4
] ) .
%% general helpers for plugins
- export ( [ node_to_string / 1 ,
string_to_node / 1 ,
subscription_to_string / 1 ,
affiliation_to_string / 1 ,
string_to_subscription / 1 ,
string_to_affiliation / 1 ,
extended_error / 2 ,
2009-08-25 16:13:26 +02:00
extended_error / 3 ,
rename_default_nodeplugin / 0
2007-12-01 06:16:30 +01:00
] ) .
%% API and gen_server callbacks
- export ( [ start_link / 2 ,
start / 2 ,
stop / 1 ,
init / 1 ,
handle_call / 3 ,
handle_cast / 2 ,
handle_info / 2 ,
terminate / 2 ,
code_change / 3
] ) .
2003-07-08 22:11:27 +02:00
2009-04-23 00:19:41 +02:00
%% calls for parallel sending of last items
- export ( [ send_loop / 1
] ) .
2005-05-23 02:30:29 +02:00
- define ( PROCNAME , ejabberd_mod_pubsub ) .
2010-01-12 16:14:47 +01:00
- define ( LOOPNAME , ejabberd_mod_pubsub_loop ) .
2007-12-01 06:16:30 +01:00
- define ( PLUGIN_PREFIX , " node_ " ) .
- define ( TREE_PREFIX , " nodetree_ " ) .
- record ( state , { server_host ,
host ,
access ,
2008-09-25 19:26:06 +02:00
pep_mapping = [ ] ,
2009-09-23 23:54:58 +02:00
ignore_pep_from_offline = true ,
2009-05-29 00:30:43 +02:00
last_item_cache = false ,
2009-09-23 17:56:06 +02:00
max_items_node = ? MAXITEMS ,
2007-12-01 06:16:30 +01:00
nodetree = ? STDTREE ,
2010-01-13 11:22:55 +01:00
plugins = [ ? STDNODE ] } ) .
2003-07-08 22:11:27 +02:00
2006-02-02 06:00:27 +01:00
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link ( Host , Opts ) - >
Proc = gen_mod : get_module_proc ( Host , ? PROCNAME ) ,
gen_server : start_link ( { local , Proc } , ? MODULE , [ Host , Opts ] , [ ] ) .
2005-06-20 05:18:13 +02:00
2006-02-02 06:00:27 +01:00
start ( Host , Opts ) - >
Proc = gen_mod : get_module_proc ( Host , ? PROCNAME ) ,
2007-12-01 06:16:30 +01:00
ChildSpec = { Proc ,
{ ? MODULE , start_link , [ Host , Opts ] } ,
transient , 1000 , worker , [ ? MODULE ] } ,
2006-02-02 06:00:27 +01:00
supervisor : start_child ( ejabberd_sup , ChildSpec ) .
2003-07-08 22:11:27 +02:00
2006-02-02 06:00:27 +01:00
stop ( Host ) - >
Proc = gen_mod : get_module_proc ( Host , ? PROCNAME ) ,
gen_server : call ( Proc , stop ) ,
2007-02-22 08:39:05 +01:00
supervisor : delete_child ( ejabberd_sup , Proc ) .
2005-07-20 05:09:34 +02:00
2006-02-02 06:00:27 +01:00
%%====================================================================
%% gen_server callbacks
%%====================================================================
%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
2009-07-31 16:55:05 +02:00
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
2006-02-02 06:00:27 +01:00
%% Description: Initiates the server
%%--------------------------------------------------------------------
2006-04-07 15:39:48 +02:00
init ( [ ServerHost , Opts ] ) - >
2008-04-28 15:02:07 +02:00
? DEBUG ( " pubsub init ~p ~p " , [ ServerHost , Opts ] ) ,
2007-08-25 19:24:00 +02:00
Host = gen_mod : get_opt_host ( ServerHost , Opts , " pubsub.@HOST@ " ) ,
2006-06-07 10:38:37 +02:00
Access = gen_mod : get_opt ( access_createnode , Opts , all ) ,
2009-09-23 23:54:58 +02:00
PepOffline = gen_mod : get_opt ( ignore_pep_from_offline , Opts , true ) ,
2009-04-10 09:36:17 +02:00
IQDisc = gen_mod : get_opt ( iqdisc , Opts , one_queue ) ,
2009-05-29 00:30:43 +02:00
LastItemCache = gen_mod : get_opt ( last_item_cache , Opts , false ) ,
2009-09-23 17:56:06 +02:00
MaxItemsNode = gen_mod : get_opt ( max_items_node , Opts , ? MAXITEMS ) ,
2009-04-30 07:18:06 +02:00
pubsub_index : init ( Host , ServerHost , Opts ) ,
2009-05-29 00:30:43 +02:00
ets : new ( gen_mod : get_module_proc ( Host , config ) , [ set , named_table ] ) ,
ets : new ( gen_mod : get_module_proc ( ServerHost , config ) , [ set , named_table ] ) ,
ets : new ( gen_mod : get_module_proc ( Host , last_items ) , [ set , named_table ] ) ,
ets : new ( gen_mod : get_module_proc ( ServerHost , last_items ) , [ set , named_table ] ) ,
2009-04-24 12:27:31 +02:00
{ Plugins , NodeTree , PepMapping } = init_plugins ( Host , ServerHost , Opts ) ,
2009-04-30 07:18:06 +02:00
mod_disco : register_feature ( ServerHost , ? NS_PUBSUB ) ,
2009-05-29 00:30:43 +02:00
ets : insert ( gen_mod : get_module_proc ( Host , config ) , { nodetree , NodeTree } ) ,
ets : insert ( gen_mod : get_module_proc ( Host , config ) , { plugins , Plugins } ) ,
ets : insert ( gen_mod : get_module_proc ( Host , config ) , { last_item_cache , LastItemCache } ) ,
2009-09-23 17:56:06 +02:00
ets : insert ( gen_mod : get_module_proc ( Host , config ) , { max_items_node , MaxItemsNode } ) ,
2009-05-29 00:30:43 +02:00
ets : insert ( gen_mod : get_module_proc ( ServerHost , config ) , { nodetree , NodeTree } ) ,
ets : insert ( gen_mod : get_module_proc ( ServerHost , config ) , { plugins , Plugins } ) ,
2009-11-06 12:01:40 +01:00
ets : insert ( gen_mod : get_module_proc ( ServerHost , config ) , { last_item_cache , LastItemCache } ) ,
ets : insert ( gen_mod : get_module_proc ( ServerHost , config ) , { max_items_node , MaxItemsNode } ) ,
2009-05-29 00:30:43 +02:00
ets : insert ( gen_mod : get_module_proc ( ServerHost , config ) , { pep_mapping , PepMapping } ) ,
2010-01-13 11:22:55 +01:00
ets : insert ( gen_mod : get_module_proc ( ServerHost , config ) , { ignore_pep_from_offline , PepOffline } ) ,
2010-01-12 16:14:47 +01:00
ets : insert ( gen_mod : get_module_proc ( ServerHost , config ) , { host , Host } ) ,
2010-03-05 11:11:44 +01:00
ejabberd_hooks : add ( sm_remove_connection_hook , ServerHost , ? MODULE , on_user_offline , 75 ) ,
2007-12-11 17:19:17 +01:00
ejabberd_hooks : add ( disco_sm_identity , ServerHost , ? MODULE , disco_sm_identity , 75 ) ,
ejabberd_hooks : add ( disco_sm_features , ServerHost , ? MODULE , disco_sm_features , 75 ) ,
ejabberd_hooks : add ( disco_sm_items , ServerHost , ? MODULE , disco_sm_items , 75 ) ,
2009-05-11 19:16:25 +02:00
ejabberd_hooks : add ( presence_probe_hook , ServerHost , ? MODULE , presence_probe , 80 ) ,
2009-05-08 01:35:59 +02:00
ejabberd_hooks : add ( roster_in_subscription , ServerHost , ? MODULE , in_subscription , 50 ) ,
2009-03-10 11:35:57 +01:00
ejabberd_hooks : add ( roster_out_subscription , ServerHost , ? MODULE , out_subscription , 50 ) ,
2007-12-18 15:45:26 +01:00
ejabberd_hooks : add ( remove_user , ServerHost , ? MODULE , remove_user , 50 ) ,
2009-05-07 02:09:02 +02:00
ejabberd_hooks : add ( anonymous_purge_hook , ServerHost , ? MODULE , remove_user , 50 ) ,
2009-04-10 09:36:17 +02:00
gen_iq_handler : add_iq_handler ( ejabberd_sm , ServerHost , ? NS_PUBSUB , ? MODULE , iq_sm , IQDisc ) ,
gen_iq_handler : add_iq_handler ( ejabberd_sm , ServerHost , ? NS_PUBSUB_OWNER , ? MODULE , iq_sm , IQDisc ) ,
2009-04-10 23:11:35 +02:00
case lists : member ( ? PEPNODE , Plugins ) of
2009-04-08 21:30:24 +02:00
true - >
2010-03-05 10:34:15 +01:00
ejabberd_hooks : add ( feature_check_packet , ServerHost , ? MODULE , feature_check_packet , 75 ) ,
2009-04-08 21:30:24 +02:00
ejabberd_hooks : add ( disco_local_identity , ServerHost , ? MODULE , disco_local_identity , 75 ) ,
ejabberd_hooks : add ( disco_local_features , ServerHost , ? MODULE , disco_local_features , 75 ) ,
ejabberd_hooks : add ( disco_local_items , ServerHost , ? MODULE , disco_local_items , 75 ) ,
2009-04-10 09:36:17 +02:00
gen_iq_handler : add_iq_handler ( ejabberd_local , ServerHost , ? NS_PUBSUB , ? MODULE , iq_local , IQDisc ) ,
gen_iq_handler : add_iq_handler ( ejabberd_local , ServerHost , ? NS_PUBSUB_OWNER , ? MODULE , iq_local , IQDisc ) ;
2009-04-08 21:30:24 +02:00
false - >
ok
end ,
2009-04-24 12:27:31 +02:00
ejabberd_router : register_route ( Host ) ,
2009-05-29 04:14:07 +02:00
update_node_database ( Host , ServerHost ) ,
update_state_database ( Host , ServerHost ) ,
2009-09-25 15:12:29 +02:00
init_nodes ( Host , ServerHost , NodeTree , Plugins ) ,
2009-04-23 00:19:41 +02:00
State = #state { host = Host ,
2007-12-01 06:16:30 +01:00
server_host = ServerHost ,
access = Access ,
2008-09-25 19:26:06 +02:00
pep_mapping = PepMapping ,
2009-09-23 23:54:58 +02:00
ignore_pep_from_offline = PepOffline ,
2009-05-29 00:30:43 +02:00
last_item_cache = LastItemCache ,
2009-09-23 17:56:06 +02:00
max_items_node = MaxItemsNode ,
2007-12-01 06:16:30 +01:00
nodetree = NodeTree ,
2009-04-23 00:19:41 +02:00
plugins = Plugins } ,
2010-01-13 11:22:55 +01:00
init_send_loop ( ServerHost , State ) ,
{ ok , State } .
init_send_loop ( ServerHost , State ) - >
Proc = gen_mod : get_module_proc ( ServerHost , ? LOOPNAME ) ,
SendLoop = spawn ( ? MODULE , send_loop , [ State ] ) ,
register ( Proc , SendLoop ) ,
SendLoop .
2007-12-01 06:16:30 +01:00
2008-04-01 12:11:39 +02:00
%% @spec (Host, ServerHost, Opts) -> Plugins
2007-12-01 06:16:30 +01:00
%% Host = mod_pubsub:host() Opts = [{Key,Value}]
%% ServerHost = host()
%% Key = atom()
%% Value = term()
%% Plugins = [Plugin::string()]
%% @doc Call the init/1 function for each plugin declared in the config file.
%% The default plugin module is implicit.
%% <p>The Erlang code for the plugin is located in a module called
%% <em>node_plugin</em>. The 'node_' prefix is mandatory.</p>
%% <p>The modules are initialized in alphetical order and the list is checked
%% and sorted to ensure that each module is initialized only once.</p>
2009-05-29 02:27:26 +02:00
%% <p>See {@link node_hometree:init/1} for an example implementation.</p>
2007-12-01 06:16:30 +01:00
init_plugins ( Host , ServerHost , Opts ) - >
TreePlugin = list_to_atom ( ? TREE_PREFIX ++
gen_mod : get_opt ( nodetree , Opts , ? STDTREE ) ) ,
2008-04-28 15:02:07 +02:00
? DEBUG ( " ** tree plugin is ~p " , [ TreePlugin ] ) ,
2007-12-01 06:16:30 +01:00
TreePlugin : init ( Host , ServerHost , Opts ) ,
2009-05-19 23:59:15 +02:00
Plugins = gen_mod : get_opt ( plugins , Opts , [ ? STDNODE ] ) ,
PepMapping = gen_mod : get_opt ( pep_mapping , Opts , [ ] ) ,
? DEBUG ( " ** PEP Mapping : ~p ~n " , [ PepMapping ] ) ,
2007-12-01 06:16:30 +01:00
lists : foreach ( fun ( Name ) - >
2008-04-28 15:02:07 +02:00
? DEBUG ( " ** init ~s plugin " , [ Name ] ) ,
2007-12-01 06:16:30 +01:00
Plugin = list_to_atom ( ? PLUGIN_PREFIX ++ Name ) ,
Plugin : init ( Host , ServerHost , Opts )
end , Plugins ) ,
2008-09-25 19:26:06 +02:00
{ Plugins , TreePlugin , PepMapping } .
2007-12-01 06:16:30 +01:00
terminate_plugins ( Host , ServerHost , Plugins , TreePlugin ) - >
lists : foreach ( fun ( Name ) - >
2008-04-28 15:02:07 +02:00
? DEBUG ( " ** terminate ~s plugin " , [ Name ] ) ,
2007-12-01 06:16:30 +01:00
Plugin = list_to_atom ( ? PLUGIN_PREFIX ++ Name ) ,
Plugin : terminate ( Host , ServerHost )
end , Plugins ) ,
TreePlugin : terminate ( Host , ServerHost ) ,
ok .
2009-09-25 15:12:29 +02:00
init_nodes ( Host , ServerHost , _ NodeTree , Plugins ) - >
2009-10-20 17:03:07 +02:00
%% TODO, this call should be done plugin side
2009-09-25 15:12:29 +02:00
case lists : member ( " hometree " , Plugins ) of
true - >
2009-10-20 17:03:07 +02:00
create_node ( Host , ServerHost , string_to_node ( " /home " ) , service_jid ( Host ) , " hometree " ) ,
create_node ( Host , ServerHost , string_to_node ( " /home/ " ++ ServerHost ) , service_jid ( Host ) , " hometree " ) ;
2009-09-25 15:12:29 +02:00
false - >
ok
end .
2007-12-01 06:16:30 +01:00
2009-05-29 04:14:07 +02:00
update_node_database ( Host , ServerHost ) - >
2009-02-05 22:23:16 +01:00
mnesia : del_table_index ( pubsub_node , type ) ,
mnesia : del_table_index ( pubsub_node , parentid ) ,
2007-12-01 06:16:30 +01:00
case catch mnesia : table_info ( pubsub_node , attributes ) of
[ host_node , host_parent , info ] - >
2009-05-29 04:14:07 +02:00
? INFO_MSG ( " upgrade node pubsub tables " , [ ] ) ,
2007-12-01 06:16:30 +01:00
F = fun ( ) - >
2009-08-20 17:50:24 +02:00
{ Result , LastIdx } = lists : foldl (
2009-04-30 07:18:06 +02:00
fun ( { pubsub_node , NodeId , ParentId , { nodeinfo , Items , Options , Entities } } , { RecList , NodeIdx } ) - >
2008-12-16 18:44:48 +01:00
ItemsList =
lists : foldl (
fun ( { item , IID , Publisher , Payload } , Acc ) - >
2009-04-30 07:18:06 +02:00
C = { unknown , Publisher } ,
M = { now ( ) , Publisher } ,
2008-12-16 18:44:48 +01:00
mnesia : write (
2009-04-30 07:18:06 +02:00
#pubsub_item { itemid = { IID , NodeIdx } ,
2008-12-16 18:44:48 +01:00
creation = C ,
modification = M ,
payload = Payload } ) ,
[ { Publisher , IID } | Acc ]
end , [ ] , Items ) ,
Owners =
dict : fold (
fun ( JID , { entity , Aff , Sub } , Acc ) - >
UsrItems =
lists : foldl (
fun ( { P , I } , IAcc ) - >
case P of
JID - > [ I | IAcc ] ;
_ - > IAcc
end
end , [ ] , ItemsList ) ,
2009-05-29 04:14:07 +02:00
mnesia : write ( { pubsub_state ,
{ JID , NodeIdx } ,
UsrItems ,
Aff ,
Sub } ) ,
2008-12-16 18:44:48 +01:00
case Aff of
owner - > [ JID | Acc ] ;
_ - > Acc
end
end , [ ] , Entities ) ,
mnesia : delete ( { pubsub_node , NodeId } ) ,
2009-04-30 07:18:06 +02:00
{ [ #pubsub_node { nodeid = NodeId ,
id = NodeIdx ,
2009-06-15 15:45:40 +02:00
parents = [ element ( 2 , ParentId ) ] ,
2008-12-16 18:44:48 +01:00
owners = Owners ,
options = Options } |
2009-04-30 07:18:06 +02:00
RecList ] , NodeIdx + 1 }
end , { [ ] , 1 } ,
2008-12-16 18:44:48 +01:00
mnesia : match_object (
2009-08-20 17:50:24 +02:00
{ pubsub_node , { Host , '_' } , '_' , '_' } ) ) ,
mnesia : write ( #pubsub_index { index = node , last = LastIdx , free = [ ] } ) ,
Result
2007-12-01 06:16:30 +01:00
end ,
2008-12-16 18:44:48 +01:00
{ atomic , NewRecords } = mnesia : transaction ( F ) ,
{ atomic , ok } = mnesia : delete_table ( pubsub_node ) ,
{ atomic , ok } = mnesia : create_table ( pubsub_node ,
[ { disc_copies , [ node ( ) ] } ,
{ attributes , record_info ( fields , pubsub_node ) } ] ) ,
FNew = fun ( ) - > lists : foreach ( fun ( Record ) - >
mnesia : write ( Record )
end , NewRecords )
end ,
case mnesia : transaction ( FNew ) of
{ atomic , Result } - >
2009-08-25 16:13:26 +02:00
? INFO_MSG ( " Pubsub node tables updated correctly: ~p " , [ Result ] ) ;
2008-12-16 18:44:48 +01:00
{ aborted , Reason } - >
2009-08-25 16:13:26 +02:00
? ERROR_MSG ( " Problem updating Pubsub node tables: ~n ~p " , [ Reason ] )
2008-12-16 18:44:48 +01:00
end ;
2009-04-30 07:18:06 +02:00
[ nodeid , parentid , type , owners , options ] - >
F = fun ( { pubsub_node , NodeId , { _ , Parent } , Type , Owners , Options } ) - >
#pubsub_node {
nodeid = NodeId ,
id = 0 ,
2009-06-15 15:45:40 +02:00
parents = [ Parent ] ,
2009-04-30 07:18:06 +02:00
type = Type ,
owners = Owners ,
options = Options }
end ,
2009-06-15 15:45:40 +02:00
mnesia : transform_table ( pubsub_node , F , [ nodeid , id , parents , type , owners , options ] ) ,
2009-04-30 07:18:06 +02:00
FNew = fun ( ) - >
2009-08-20 17:50:24 +02:00
LastIdx = lists : foldl ( fun ( #pubsub_node { nodeid = NodeId } = PubsubNode , NodeIdx ) - >
2009-04-30 07:18:06 +02:00
mnesia : write ( PubsubNode #pubsub_node { id = NodeIdx } ) ,
lists : foreach ( fun ( #pubsub_state { stateid = StateId } = State ) - >
{ JID , _ } = StateId ,
mnesia : delete ( { pubsub_state , StateId } ) ,
mnesia : write ( State #pubsub_state { stateid = { JID , NodeIdx } } )
end , mnesia : match_object ( #pubsub_state { stateid = { '_' , NodeId } , _ = '_' } ) ) ,
lists : foreach ( fun ( #pubsub_item { itemid = ItemId } = Item ) - >
{ IID , _ } = ItemId ,
{ M1 , M2 } = Item #pubsub_item.modification ,
{ C1 , C2 } = Item #pubsub_item.creation ,
mnesia : delete ( { pubsub_item , ItemId } ) ,
mnesia : write ( Item #pubsub_item { itemid = { IID , NodeIdx } ,
modification = { M2 , M1 } ,
creation = { C2 , C1 } } )
end , mnesia : match_object ( #pubsub_item { itemid = { '_' , NodeId } , _ = '_' } ) ) ,
NodeIdx + 1
end , 1 , mnesia : match_object (
{ pubsub_node , { Host , '_' } , '_' , '_' , '_' , '_' , '_' } )
++ mnesia : match_object (
2009-08-20 17:50:24 +02:00
{ pubsub_node , { { '_' , ServerHost , '_' } , '_' } , '_' , '_' , '_' , '_' , '_' } ) ) ,
mnesia : write ( #pubsub_index { index = node , last = LastIdx , free = [ ] } )
2009-04-30 07:18:06 +02:00
end ,
case mnesia : transaction ( FNew ) of
{ atomic , Result } - >
2009-08-25 16:13:26 +02:00
rename_default_nodeplugin ( ) ,
? INFO_MSG ( " Pubsub node tables updated correctly: ~p " , [ Result ] ) ;
2009-05-29 04:14:07 +02:00
{ aborted , Reason } - >
2009-08-25 16:13:26 +02:00
? ERROR_MSG ( " Problem updating Pubsub node tables: ~n ~p " , [ Reason ] )
2009-05-29 04:14:07 +02:00
end ;
2009-06-15 15:45:40 +02:00
[ nodeid , id , parent , type , owners , options ] - >
F = fun ( { pubsub_node , NodeId , Id , Parent , Type , Owners , Options } ) - >
#pubsub_node {
nodeid = NodeId ,
id = Id ,
parents = [ Parent ] ,
type = Type ,
owners = Owners ,
options = Options }
end ,
2009-08-25 16:13:26 +02:00
mnesia : transform_table ( pubsub_node , F , [ nodeid , id , parents , type , owners , options ] ) ,
rename_default_nodeplugin ( ) ;
2009-05-29 04:14:07 +02:00
_ - >
ok
2009-10-20 17:03:07 +02:00
end ,
mnesia : transaction ( fun ( ) - >
case catch mnesia : first ( pubsub_node ) of
{ _ , L } when is_list ( L ) - >
lists : foreach (
fun ( { H , N } ) when is_list ( N ) - >
[ Node ] = mnesia : read ( { pubsub_node , { H , N } } ) ,
Type = Node #pubsub_node.type ,
BN = element ( 2 , node_call ( Type , path_to_node , [ N ] ) ) ,
BP = case [ element ( 2 , node_call ( Type , path_to_node , [ P ] ) ) | | P < - Node #pubsub_node.parents ] of
[ < < > > ] - > [ ] ;
Parents - > Parents
end ,
mnesia : write ( Node #pubsub_node { nodeid = { H , BN } , parents = BP } ) ,
mnesia : delete ( { pubsub_node , { H , N } } ) ;
( _ ) - >
ok
end , mnesia : all_keys ( pubsub_node ) ) ;
_ - >
ok
end
end ) .
2009-05-29 04:14:07 +02:00
2009-08-25 16:13:26 +02:00
rename_default_nodeplugin ( ) - >
lists : foreach ( fun ( Node ) - >
mnesia : dirty_write ( Node #pubsub_node { type = " hometree " } )
end , mnesia : dirty_match_object ( #pubsub_node { type = " default " , _ = '_' } ) ) .
2009-05-29 04:14:07 +02:00
update_state_database ( _ Host , _ ServerHost ) - >
case catch mnesia : table_info ( pubsub_state , attributes ) of
[ stateid , items , affiliation , subscription ] - >
? INFO_MSG ( " upgrade state pubsub tables " , [ ] ) ,
F = fun ( { pubsub_state , { JID , NodeID } , Items , Aff , Sub } , Acc ) - >
Subs = case Sub of
none - >
[ ] ;
_ - >
{ result , SubID } = pubsub_subscription : subscribe_node ( JID , NodeID , [ ] ) ,
[ { Sub , SubID } ]
end ,
NewState = #pubsub_state { stateid = { JID , NodeID } ,
items = Items ,
affiliation = Aff ,
subscriptions = Subs } ,
[ NewState | Acc ]
end ,
{ atomic , NewRecs } = mnesia : transaction ( fun mnesia : foldl / 3 ,
[ F , [ ] , pubsub_state ] ) ,
{ atomic , ok } = mnesia : delete_table ( pubsub_state ) ,
{ atomic , ok } = mnesia : create_table ( pubsub_state ,
[ { disc_copies , [ node ( ) ] } ,
{ attributes , record_info ( fields , pubsub_state ) } ] ) ,
FNew = fun ( ) - >
lists : foreach ( fun mnesia : write / 1 , NewRecs )
end ,
case mnesia : transaction ( FNew ) of
{ atomic , Result } - >
? INFO_MSG ( " Pubsub state tables updated correctly: ~p " ,
[ Result ] ) ;
2009-04-30 07:18:06 +02:00
{ aborted , Reason } - >
2009-05-29 04:14:07 +02:00
? ERROR_MSG ( " Problem updating Pubsub state tables: ~n ~p " ,
[ Reason ] )
2009-04-30 07:18:06 +02:00
end ;
2007-12-01 06:16:30 +01:00
_ - >
ok
end .
2009-04-23 00:19:41 +02:00
send_loop ( State ) - >
receive
{ presence , JID , Pid } - >
Host = State #state.host ,
ServerHost = State #state.server_host ,
LJID = jlib : jid_tolower ( JID ) ,
BJID = jlib : jid_remove_resource ( LJID ) ,
%% for each node From is subscribed to
%% and if the node is so configured, send the last published item to From
2009-04-30 07:18:06 +02:00
lists : foreach ( fun ( PType ) - >
{ result , Subscriptions } = node_action ( Host , PType , get_entity_subscriptions , [ Host , JID ] ) ,
2009-04-23 00:19:41 +02:00
lists : foreach (
2009-07-20 17:19:51 +02:00
fun ( { Node , subscribed , _ , SubJID } ) - >
2009-04-23 00:19:41 +02:00
if ( SubJID == LJID ) or ( SubJID == BJID ) - >
2009-08-28 02:06:28 +02:00
#pubsub_node { nodeid = { H , N } , type = Type , id = NodeId , options = Options } = Node ,
2009-05-19 23:59:15 +02:00
case get_option ( Options , send_last_published_item ) of
on_sub_and_presence - >
2009-09-24 21:28:43 +02:00
send_items ( H , N , NodeId , Type , LJID , last ) ;
2009-04-23 00:19:41 +02:00
_ - >
ok
end ;
true - >
% resource not concerned about that subscription
ok
end ;
( _ ) - >
ok
end , Subscriptions )
end , State #state.plugins ) ,
%% and force send the last PEP events published by its offline and local contacts
%% only if pubsub is explicitely configured for that.
%% this is a hack in a sense that PEP should only be based on presence
%% and is not able to "store" events of remote users (via s2s)
%% this makes that hack only work for local domain by now
2009-09-23 23:54:58 +02:00
if not State #state.ignore_pep_from_offline - >
2009-05-07 02:09:02 +02:00
{ User , Server , Resource } = jlib : jid_tolower ( JID ) ,
case catch ejabberd_c2s : get_subscribed ( Pid ) of
Contacts when is_list ( Contacts ) - >
lists : foreach (
fun ( { U , S , R } ) - >
case S of
ServerHost - > %% local contacts
2010-03-05 10:34:15 +01:00
case user_resources ( U , S ) of
2009-05-07 02:09:02 +02:00
[ ] - > %% offline
PeerJID = jlib : make_jid ( U , S , R ) ,
self ( ) ! { presence , User , Server , [ Resource ] , PeerJID } ;
_ - > %% online
% this is already handled by presence probe
ok
end ;
_ - > %% remote contacts
% we can not do anything in any cases
ok
end
end , Contacts ) ;
_ - >
ok
2010-03-05 10:34:15 +01:00
end ;
2009-04-23 00:19:41 +02:00
true - >
ok
end ,
send_loop ( State ) ;
{ presence , User , Server , Resources , JID } - >
2009-05-07 02:09:02 +02:00
%% get resources caps and check if processing is needed
2009-05-11 19:16:25 +02:00
spawn ( fun ( ) - >
2010-03-05 11:53:26 +01:00
Host = State #state.host ,
Owner = jlib : jid_remove_resource ( jlib : jid_tolower ( JID ) ) ,
lists : foreach ( fun ( #pubsub_node { nodeid = { _ , Node } , type = Type , id = NodeId , options = Options } ) - >
2009-05-11 19:16:25 +02:00
case get_option ( Options , send_last_published_item ) of
on_sub_and_presence - >
2010-03-05 10:34:15 +01:00
lists : foreach (
fun ( Resource ) - >
LJID = { User , Server , Resource } ,
Subscribed = case get_option ( Options , access_model ) of
open - > true ;
presence - > true ;
whitelist - > false ; % subscribers are added manually
authorize - > false ; % likewise
roster - >
Grps = get_option ( Options , roster_groups_allowed , [ ] ) ,
{ OU , OS , _ } = Owner ,
element ( 2 , get_roster_info ( OU , OS , LJID , Grps ) )
end ,
if Subscribed - >
2010-03-05 11:35:20 +01:00
send_items ( Owner , Node , NodeId , Type , LJID , last ) ;
2010-03-05 10:34:15 +01:00
true - >
ok
end
end , Resources ) ;
2009-05-11 19:16:25 +02:00
_ - >
ok
end
2010-03-05 10:34:15 +01:00
end , tree_action ( Host , get_nodes , [ Owner , JID ] ) )
2010-03-05 11:53:26 +01:00
end ) ,
2009-04-23 00:19:41 +02:00
send_loop ( State ) ;
stop - >
ok
end .
2007-12-01 06:16:30 +01:00
%% -------
2007-12-11 17:19:17 +01:00
%% disco hooks handling functions
2007-12-01 06:16:30 +01:00
%%
2008-04-02 11:06:28 +02:00
identity ( Host ) - >
Identity = case lists : member ( ? PEPNODE , plugins ( Host ) ) of
true - > [ { " category " , " pubsub " } , { " type " , " pep " } ] ;
2008-04-10 16:11:06 +02:00
false - > [ { " category " , " pubsub " } , { " type " , " service " } ]
2008-04-02 11:06:28 +02:00
end ,
{ xmlelement , " identity " , Identity , [ ] } .
disco_local_identity ( Acc , _ From , To , [ ] , _ Lang ) - >
Acc ++ [ identity ( To #jid.lserver ) ] ;
2007-12-01 06:16:30 +01:00
disco_local_identity ( Acc , _ From , _ To , _ Node , _ Lang ) - >
Acc .
2007-12-21 01:08:59 +01:00
disco_local_features ( Acc , _ From , To , [ ] , _ Lang ) - >
2007-12-11 17:19:17 +01:00
Host = To #jid.lserver ,
Feats = case Acc of
{ result , I } - > I ;
2007-12-21 01:08:59 +01:00
_ - > [ ]
end ,
2007-12-11 17:19:17 +01:00
{ result , Feats ++ lists : map ( fun ( Feature ) - >
? NS_PUBSUB ++ " # " ++ Feature
2009-12-04 19:35:14 +01:00
end , features ( Host , < < > > ) ) } ;
2007-12-21 01:08:59 +01:00
disco_local_features ( Acc , _ From , _ To , _ Node , _ Lang ) - >
Acc .
2007-12-11 17:19:17 +01:00
disco_local_items ( Acc , _ From , _ To , [ ] , _ Lang ) - >
Acc ;
disco_local_items ( Acc , _ From , _ To , _ Node , _ Lang ) - >
Acc .
2008-04-02 11:06:28 +02:00
disco_sm_identity ( Acc , _ From , To , [ ] , _ Lang ) - >
Acc ++ [ identity ( To #jid.lserver ) ] ;
2007-12-01 06:16:30 +01:00
disco_sm_identity ( Acc , From , To , Node , _ Lang ) - >
LOwner = jlib : jid_tolower ( jlib : jid_remove_resource ( To ) ) ,
Acc ++ case node_disco_identity ( LOwner , From , Node ) of
{ result , I } - > I ;
_ - > [ ]
end .
disco_sm_features ( Acc , _ From , _ To , [ ] , _ Lang ) - >
Acc ;
disco_sm_features ( Acc , From , To , Node , _ Lang ) - >
LOwner = jlib : jid_tolower ( jlib : jid_remove_resource ( To ) ) ,
Features = node_disco_features ( LOwner , From , Node ) ,
case { Acc , Features } of
{ { result , AccFeatures } , { result , AddFeatures } } - >
{ result , AccFeatures ++ AddFeatures } ;
{ _ , { result , AddFeatures } } - >
{ result , AddFeatures } ;
{ _ , _ } - >
Acc
end .
2008-12-08 20:50:50 +01:00
disco_sm_items ( Acc , From , To , [ ] , _ Lang ) - >
2007-12-01 06:16:30 +01:00
Host = To #jid.lserver ,
2009-10-20 17:03:07 +02:00
case tree_action ( Host , get_subnodes , [ Host , < < > > , From ] ) of
2007-12-01 06:16:30 +01:00
[ ] - >
Acc ;
Nodes - >
2009-04-30 07:18:06 +02:00
SBJID = jlib : jid_to_string ( jlib : jid_remove_resource ( To ) ) ,
2007-12-01 06:16:30 +01:00
Items = case Acc of
{ result , I } - > I ;
_ - > [ ]
end ,
NodeItems = lists : map (
2009-04-30 07:18:06 +02:00
fun ( #pubsub_node { nodeid = { _ , Node } } ) - >
2007-12-01 06:16:30 +01:00
{ xmlelement , " item " ,
2009-05-08 03:18:02 +02:00
[ { " jid " , SBJID } | nodeAttr ( Node ) ] ,
2007-12-01 06:16:30 +01:00
[ ] }
end , Nodes ) ,
{ result , NodeItems ++ Items }
end ;
2009-10-20 17:03:07 +02:00
disco_sm_items ( Acc , From , To , SNode , _ Lang ) - >
2007-12-01 06:16:30 +01:00
Host = To #jid.lserver ,
2009-10-20 17:03:07 +02:00
Node = string_to_node ( SNode ) ,
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { type = Type , id = NodeId } ) - >
2009-09-09 23:40:21 +02:00
% TODO call get_items/6 instead for access control (EJAB-1033)
2009-04-30 07:18:06 +02:00
case node_call ( Type , get_items , [ NodeId , From ] ) of
{ result , [ ] } - >
none ;
{ result , AllItems } - >
SBJID = jlib : jid_to_string ( jlib : jid_remove_resource ( To ) ) ,
Items = case Acc of
2007-12-01 06:16:30 +01:00
{ result , I } - > I ;
_ - > [ ]
end ,
2009-04-30 07:18:06 +02:00
NodeItems = lists : map (
fun ( #pubsub_item { itemid = { Id , _ } } ) - >
{ result , Name } = node_call ( Type , get_item_name , [ Host , Node , Id ] ) ,
2009-10-20 17:03:07 +02:00
{ xmlelement , " item " , [ { " jid " , SBJID } , { " name " , Name } ] , [ ] }
2007-12-01 06:16:30 +01:00
end , AllItems ) ,
2009-04-30 07:18:06 +02:00
{ result , NodeItems ++ Items } ;
_ - >
none
end
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ , Items } } - > { result , Items } ;
_ - > Acc
2007-12-01 06:16:30 +01:00
end .
2007-12-11 17:19:17 +01:00
%% -------
%% presence hooks handling functions
%%
2007-12-01 06:16:30 +01:00
2009-04-23 00:19:41 +02:00
presence_probe ( #jid { luser = User , lserver = Server , lresource = Resource } = JID , JID , Pid ) - >
2009-12-07 00:18:17 +01:00
%%?DEBUG("presence probe self ~s@~s/~s ~s@~s/~s",[User,Server,Resource,element(2,JID),element(3,JID),element(4,JID)]),
2010-01-12 16:14:47 +01:00
presence ( Server , { presence , JID , Pid } ) ,
presence ( Server , { presence , User , Server , [ Resource ] , JID } ) ;
2009-12-07 00:18:17 +01:00
presence_probe ( #jid { luser = User , lserver = Server } , #jid { luser = User , lserver = Server } , _ Pid ) - >
%% ignore presence_probe from other ressources for the current user
%% this way, we do not send duplicated last items if user already connected with other clients
ok ;
2009-03-10 11:35:57 +01:00
presence_probe ( #jid { luser = User , lserver = Server , lresource = Resource } , #jid { lserver = Host } = JID , _ Pid ) - >
2009-12-07 00:18:17 +01:00
%%?DEBUG("presence probe peer ~s@~s/~s ~s@~s/~s",[User,Server,Resource,element(2,JID),element(3,JID),element(4,JID)]),
2010-01-12 16:14:47 +01:00
presence ( Host , { presence , User , Server , [ Resource ] , JID } ) .
presence ( ServerHost , Presence ) - >
2010-01-13 11:22:55 +01:00
SendLoop = case whereis ( gen_mod : get_module_proc ( ServerHost , ? LOOPNAME ) ) of
undefined - >
% in case send_loop process died, we rebuild a minimal State record and respawn it
Host = host ( ServerHost ) ,
Plugins = plugins ( Host ) ,
PepOffline = case catch ets : lookup ( gen_mod : get_module_proc ( ServerHost , config ) , ignore_pep_from_offline ) of
[ { ignore_pep_from_offline , PO } ] - > PO ;
_ - > true
end ,
State = #state { host = Host ,
server_host = ServerHost ,
ignore_pep_from_offline = PepOffline ,
plugins = Plugins } ,
init_send_loop ( ServerHost , State ) ;
Pid - >
Pid
end ,
SendLoop ! Presence .
2006-02-02 06:00:27 +01:00
2009-03-03 23:42:37 +01:00
%% -------
%% subscription hooks handling functions
%%
2009-03-10 11:35:57 +01:00
out_subscription ( User , Server , JID , subscribed ) - >
Owner = jlib : make_jid ( User , Server , " " ) ,
{ PUser , PServer , PResource } = jlib : jid_tolower ( JID ) ,
PResources = case PResource of
[ ] - > user_resources ( PUser , PServer ) ;
_ - > [ PResource ]
end ,
2010-01-12 16:14:47 +01:00
presence ( Server , { presence , PUser , PServer , PResources , Owner } ) ;
2009-05-08 01:35:59 +02:00
out_subscription ( _ , _ , _ , _ ) - >
ok .
2009-05-29 00:30:43 +02:00
in_subscription ( _ , User , Server , Owner , unsubscribed , _ ) - >
2010-01-12 16:14:47 +01:00
unsubscribe_user ( jlib : make_jid ( User , Server , " " ) , Owner ) ;
2009-05-08 01:35:59 +02:00
in_subscription ( _ , _ , _ , _ , _ , _ ) - >
2009-03-10 11:35:57 +01:00
ok .
2009-03-03 23:42:37 +01:00
2010-01-12 16:14:47 +01:00
unsubscribe_user ( Entity , Owner ) - >
BJID = jlib : jid_tolower ( jlib : jid_remove_resource ( Owner ) ) ,
Host = host ( element ( 2 , BJID ) ) ,
spawn ( fun ( ) - >
lists : foreach ( fun ( PType ) - >
{ result , Subscriptions } = node_action ( Host , PType , get_entity_subscriptions , [ Host , Entity ] ) ,
lists : foreach ( fun
( { #pubsub_node { options = Options , owners = Owners , id = NodeId } , subscribed , _ , JID } ) - >
case get_option ( Options , access_model ) of
presence - >
case lists : member ( BJID , Owners ) of
true - >
node_action ( Host , PType , unsubscribe_node , [ NodeId , Entity , JID , all ] ) ;
false - >
{ result , ok }
end ;
_ - >
{ result , ok }
end ;
( _ ) - >
ok
end , Subscriptions )
end , plugins ( Host ) )
end ) .
2007-12-18 15:45:26 +01:00
%% -------
%% user remove hook handling function
%%
remove_user ( User , Server ) - >
LUser = jlib : nodeprep ( User ) ,
LServer = jlib : nameprep ( Server ) ,
2010-01-12 16:14:47 +01:00
Entity = jlib : make_jid ( LUser , LServer , " " ) ,
Host = host ( LServer ) ,
2010-03-11 16:00:42 +01:00
HomeTreeBase = string_to_node ( " /home/ " ++ LServer ++ " / " ++ LUser ) ,
2010-01-12 16:14:47 +01:00
spawn ( fun ( ) - >
lists : foreach ( fun ( PType ) - >
{ result , Subscriptions } = node_action ( Host , PType , get_entity_subscriptions , [ Host , Entity ] ) ,
lists : foreach ( fun
2010-03-11 16:00:42 +01:00
( { #pubsub_node { id = NodeId } , _ , _ , JID } ) - > node_action ( Host , PType , unsubscribe_node , [ NodeId , Entity , JID , all ] )
2010-01-12 16:14:47 +01:00
end , Subscriptions ) ,
{ result , Affiliations } = node_action ( Host , PType , get_entity_affiliations , [ Host , Entity ] ) ,
lists : foreach ( fun
2010-03-11 16:00:42 +01:00
( { #pubsub_node { nodeid = { H , N } , parents = [ ] } , owner } ) - > delete_node ( H , N , Entity ) ;
( { #pubsub_node { nodeid = { H , N } , type = " hometree " } , owner } ) when N == HomeTreeBase - > delete_node ( H , N , Entity ) ;
( { #pubsub_node { id = NodeId } , publisher } ) - > node_action ( Host , PType , set_affiliation , [ NodeId , Entity , none ] ) ;
( _ ) - > ok
2010-01-12 16:14:47 +01:00
end , Affiliations )
end , plugins ( Host ) )
end ) .
2007-12-18 15:45:26 +01:00
2006-02-02 06:00:27 +01:00
%%--------------------------------------------------------------------
2007-12-01 06:16:30 +01:00
%% Function:
%% handle_call(Request, From, State) -> {reply, Reply, State} |
2009-07-31 16:55:05 +02:00
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
2006-02-02 06:00:27 +01:00
%% Description: Handling call messages
%%--------------------------------------------------------------------
2007-12-01 06:16:30 +01:00
%% @private
handle_call ( server_host , _ From , State ) - >
{ reply , State #state.server_host , State } ;
handle_call ( plugins , _ From , State ) - >
{ reply , State #state.plugins , State } ;
2008-09-25 19:26:06 +02:00
handle_call ( pep_mapping , _ From , State ) - >
{ reply , State #state.pep_mapping , State } ;
2007-12-01 06:16:30 +01:00
handle_call ( nodetree , _ From , State ) - >
{ reply , State #state.nodetree , State } ;
2006-02-02 06:00:27 +01:00
handle_call ( stop , _ From , State ) - >
{ stop , normal , ok , State } .
%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
2009-07-31 16:55:05 +02:00
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
2006-02-02 06:00:27 +01:00
%% Description: Handling cast messages
%%--------------------------------------------------------------------
2007-12-01 06:16:30 +01:00
%% @private
2006-02-02 06:00:27 +01:00
handle_cast ( _ Msg , State ) - >
{ noreply , State } .
%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
2009-07-31 16:55:05 +02:00
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
2006-02-02 06:00:27 +01:00
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
2007-12-01 06:16:30 +01:00
%% @private
handle_info ( { route , From , To , Packet } ,
#state { server_host = ServerHost ,
access = Access ,
plugins = Plugins } = State ) - >
case catch do_route ( ServerHost , Access , Plugins , To #jid.lserver , From , To , Packet ) of
{ 'EXIT' , Reason } - > ? ERROR_MSG ( " ~p " , [ Reason ] ) ;
_ - > ok
2006-02-02 06:00:27 +01:00
end ,
{ noreply , State } ;
handle_info ( _ Info , State ) - >
{ noreply , State } .
%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------
2007-12-01 06:16:30 +01:00
%% @private
terminate ( _ Reason , #state { host = Host ,
server_host = ServerHost ,
nodetree = TreePlugin ,
2010-01-13 11:22:55 +01:00
plugins = Plugins } ) - >
2007-12-01 06:16:30 +01:00
ejabberd_router : unregister_route ( Host ) ,
2009-04-10 23:11:35 +02:00
case lists : member ( ? PEPNODE , Plugins ) of
2009-04-10 09:36:17 +02:00
true - >
2010-03-05 10:34:15 +01:00
ejabberd_hooks : delete ( feature_check_packet , ServerHost , ? MODULE , feature_check_packet , 75 ) ,
2009-04-10 09:36:17 +02:00
ejabberd_hooks : delete ( disco_local_identity , ServerHost , ? MODULE , disco_local_identity , 75 ) ,
ejabberd_hooks : delete ( disco_local_features , ServerHost , ? MODULE , disco_local_features , 75 ) ,
ejabberd_hooks : delete ( disco_local_items , ServerHost , ? MODULE , disco_local_items , 75 ) ,
gen_iq_handler : remove_iq_handler ( ejabberd_local , ServerHost , ? NS_PUBSUB ) ,
gen_iq_handler : remove_iq_handler ( ejabberd_local , ServerHost , ? NS_PUBSUB_OWNER ) ;
false - >
ok
end ,
2010-03-05 11:11:44 +01:00
ejabberd_hooks : delete ( sm_remove_connection_hook , ServerHost , ? MODULE , on_user_offline , 75 ) ,
2007-12-11 17:19:17 +01:00
ejabberd_hooks : delete ( disco_sm_identity , ServerHost , ? MODULE , disco_sm_identity , 75 ) ,
ejabberd_hooks : delete ( disco_sm_features , ServerHost , ? MODULE , disco_sm_features , 75 ) ,
ejabberd_hooks : delete ( disco_sm_items , ServerHost , ? MODULE , disco_sm_items , 75 ) ,
2009-05-11 19:16:25 +02:00
ejabberd_hooks : delete ( presence_probe_hook , ServerHost , ? MODULE , presence_probe , 80 ) ,
2009-05-08 01:35:59 +02:00
ejabberd_hooks : delete ( roster_in_subscription , ServerHost , ? MODULE , in_subscription , 50 ) ,
2009-03-10 11:35:57 +01:00
ejabberd_hooks : delete ( roster_out_subscription , ServerHost , ? MODULE , out_subscription , 50 ) ,
2007-12-18 15:45:26 +01:00
ejabberd_hooks : delete ( remove_user , ServerHost , ? MODULE , remove_user , 50 ) ,
2009-05-07 02:09:02 +02:00
ejabberd_hooks : delete ( anonymous_purge_hook , ServerHost , ? MODULE , remove_user , 50 ) ,
2009-04-10 09:36:17 +02:00
gen_iq_handler : remove_iq_handler ( ejabberd_sm , ServerHost , ? NS_PUBSUB ) ,
gen_iq_handler : remove_iq_handler ( ejabberd_sm , ServerHost , ? NS_PUBSUB_OWNER ) ,
2007-12-01 06:16:30 +01:00
mod_disco : unregister_feature ( ServerHost , ? NS_PUBSUB ) ,
2010-01-13 11:22:55 +01:00
gen_mod : get_module_proc ( ServerHost , ? LOOPNAME ) ! stop ,
2009-05-07 02:09:02 +02:00
terminate_plugins ( Host , ServerHost , Plugins , TreePlugin ) .
2006-02-02 06:00:27 +01:00
%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
2007-12-01 06:16:30 +01:00
%% @private
2006-02-02 06:00:27 +01:00
code_change ( _ OldVsn , State , _ Extra ) - >
{ ok , State } .
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
2007-12-01 06:16:30 +01:00
do_route ( ServerHost , Access , Plugins , Host , From , To , Packet ) - >
{ xmlelement , Name , Attrs , _ Els } = Packet ,
2003-07-08 22:11:27 +02:00
case To of
2003-10-07 22:31:44 +02:00
#jid { luser = " " , lresource = " " } - >
2003-07-08 22:11:27 +02:00
case Name of
" iq " - >
case jlib : iq_query_info ( Packet ) of
2007-12-01 06:16:30 +01:00
#iq { type = get , xmlns = ? NS_DISCO_INFO ,
2007-05-29 15:14:09 +02:00
sub_el = SubEl , lang = Lang } = IQ - >
2003-07-08 22:11:27 +02:00
{ xmlelement , _ , QAttrs , _ } = SubEl ,
Node = xml : get_attr_s ( " node " , QAttrs ) ,
2009-07-17 22:45:44 +02:00
Info = ejabberd_hooks : run_fold (
disco_info , ServerHost , [ ] ,
[ ServerHost , ? MODULE , " " , " " ] ) ,
2007-12-01 06:16:30 +01:00
Res = case iq_disco_info ( Host , Node , From , Lang ) of
{ result , IQRes } - >
jlib : iq_to_xml (
IQ #iq { type = result ,
sub_el = [ { xmlelement , " query " ,
2009-07-17 22:45:44 +02:00
QAttrs , IQRes ++ Info } ] } ) ;
2007-12-01 06:16:30 +01:00
{ error , Error } - >
jlib : make_error_reply ( Packet , Error )
end ,
ejabberd_router : route ( To , From , Res ) ;
#iq { type = get , xmlns = ? NS_DISCO_ITEMS ,
2003-12-17 21:13:21 +01:00
sub_el = SubEl } = IQ - >
2003-07-08 22:11:27 +02:00
{ xmlelement , _ , QAttrs , _ } = SubEl ,
Node = xml : get_attr_s ( " node " , QAttrs ) ,
2007-12-01 06:16:30 +01:00
Res = case iq_disco_items ( Host , Node , From ) of
{ result , IQRes } - >
jlib : iq_to_xml (
IQ #iq { type = result ,
sub_el = [ { xmlelement , " query " ,
QAttrs , IQRes } ] } ) ;
{ error , Error } - >
jlib : make_error_reply ( Packet , Error )
end ,
2003-10-07 22:31:44 +02:00
ejabberd_router : route ( To , From , Res ) ;
2007-12-01 06:16:30 +01:00
#iq { type = IQType , xmlns = ? NS_PUBSUB ,
lang = Lang , sub_el = SubEl } = IQ - >
2003-07-08 22:11:27 +02:00
Res =
2007-12-01 06:16:30 +01:00
case iq_pubsub ( Host , ServerHost , From , IQType , SubEl , Lang , Access , Plugins ) of
2003-07-08 22:11:27 +02:00
{ result , IQRes } - >
jlib : iq_to_xml (
2003-12-17 21:13:21 +01:00
IQ #iq { type = result ,
sub_el = IQRes } ) ;
2003-07-08 22:11:27 +02:00
{ error , Error } - >
2007-12-01 06:16:30 +01:00
jlib : make_error_reply ( Packet , Error )
2003-07-08 22:11:27 +02:00
end ,
2003-10-07 22:31:44 +02:00
ejabberd_router : route ( To , From , Res ) ;
2007-12-01 06:16:30 +01:00
#iq { type = IQType , xmlns = ? NS_PUBSUB_OWNER ,
2005-07-20 05:09:34 +02:00
lang = Lang , sub_el = SubEl } = IQ - >
Res =
2009-03-20 01:08:38 +01:00
case iq_pubsub_owner ( Host , ServerHost , From , IQType , SubEl , Lang ) of
2005-07-20 05:09:34 +02:00
{ result , IQRes } - >
jlib : iq_to_xml (
IQ #iq { type = result ,
sub_el = IQRes } ) ;
{ error , Error } - >
2007-12-01 06:16:30 +01:00
jlib : make_error_reply ( Packet , Error )
2005-07-20 05:09:34 +02:00
end ,
ejabberd_router : route ( To , From , Res ) ;
2003-12-17 21:13:21 +01:00
#iq { type = get , xmlns = ? NS_VCARD = XMLNS ,
2007-12-01 06:16:30 +01:00
lang = Lang , sub_el = _ SubEl } = IQ - >
2003-12-17 21:13:21 +01:00
Res = IQ #iq { type = result ,
2007-12-01 06:16:30 +01:00
sub_el = [ { xmlelement , " vCard " , [ { " xmlns " , XMLNS } ] ,
2003-12-17 21:13:21 +01:00
iq_get_vcard ( Lang ) } ] } ,
2007-12-22 01:04:05 +01:00
ejabberd_router : route ( To , From , jlib : iq_to_xml ( Res ) ) ;
2009-07-31 16:55:05 +02:00
#iq { type = set , xmlns = ? NS_COMMANDS } = IQ - >
Res = case iq_command ( Host , ServerHost , From , IQ , Access , Plugins ) of
{ error , Error } - >
jlib : make_error_reply ( Packet , Error ) ;
{ result , IQRes } - >
2009-07-20 10:53:56 +02:00
jlib : iq_to_xml ( IQ #iq { type = result ,
2009-07-31 16:55:05 +02:00
sub_el = IQRes } )
end ,
ejabberd_router : route ( To , From , Res ) ;
2003-12-17 21:13:21 +01:00
#iq { } - >
2003-07-08 22:11:27 +02:00
Err = jlib : make_error_reply (
Packet ,
? ERR_FEATURE_NOT_IMPLEMENTED ) ,
2003-12-17 21:13:21 +01:00
ejabberd_router : route ( To , From , Err ) ;
_ - >
ok
2003-07-08 22:11:27 +02:00
end ;
2007-12-01 06:16:30 +01:00
" message " - >
case xml : get_attr_s ( " type " , Attrs ) of
" error " - >
ok ;
_ - >
case find_authorization_response ( Packet ) of
none - >
ok ;
invalid - >
ejabberd_router : route ( To , From ,
jlib : make_error_reply ( Packet , ? ERR_BAD_REQUEST ) ) ;
XFields - >
handle_authorization_response ( Host , From , To , Packet , XFields )
end
end ;
2003-07-08 22:11:27 +02:00
_ - >
ok
end ;
_ - >
case xml : get_attr_s ( " type " , Attrs ) of
" error " - >
ok ;
" result " - >
ok ;
_ - >
2007-12-01 06:16:30 +01:00
Err = jlib : make_error_reply ( Packet , ? ERR_ITEM_NOT_FOUND ) ,
2003-07-08 22:11:27 +02:00
ejabberd_router : route ( To , From , Err )
end
end .
2009-11-04 20:54:15 +01:00
command_disco_info ( _ Host , < < ? NS_COMMANDS > > , _ From ) - >
2010-03-05 11:35:20 +01:00
IdentityEl = { xmlelement , " identity " , [ { " category " , " automation " } , { " type " , " command-list " } ] , [ ] } ,
2009-11-04 20:54:15 +01:00
{ result , [ IdentityEl ] } ;
command_disco_info ( _ Host , < < ? NS_PUBSUB_GET_PENDING > > , _ From ) - >
2010-03-05 11:35:20 +01:00
IdentityEl = { xmlelement , " identity " , [ { " category " , " automation " } , { " type " , " command-node " } ] , [ ] } ,
2009-11-04 20:54:15 +01:00
FeaturesEl = { xmlelement , " feature " , [ { " var " , ? NS_COMMANDS } ] , [ ] } ,
{ result , [ IdentityEl , FeaturesEl ] } .
2007-12-01 06:16:30 +01:00
node_disco_info ( Host , Node , From ) - >
node_disco_info ( Host , Node , From , true , true ) .
node_disco_identity ( Host , Node , From ) - >
node_disco_info ( Host , Node , From , true , false ) .
node_disco_features ( Host , Node , From ) - >
node_disco_info ( Host , Node , From , false , true ) .
2008-02-02 15:15:37 +01:00
node_disco_info ( Host , Node , From , Identity , Features ) - >
2007-12-01 06:16:30 +01:00
Action =
2009-04-30 07:18:06 +02:00
fun ( #pubsub_node { type = Type , id = NodeId } ) - >
2007-12-01 06:16:30 +01:00
I = case Identity of
false - >
[ ] ;
true - >
Types =
2008-02-02 15:15:37 +01:00
case tree_call ( Host , get_subnodes , [ Host , Node , From ] ) of
2007-12-01 06:16:30 +01:00
[ ] - >
[ " leaf " ] ; %% No sub-nodes: it's a leaf node
_ - >
2009-04-30 07:18:06 +02:00
case node_call ( Type , get_items , [ NodeId , From ] ) of
2007-12-01 06:16:30 +01:00
{ result , [ ] } - > [ " collection " ] ;
{ result , _ } - > [ " leaf " , " collection " ] ;
_ - > [ ]
end
end ,
lists : map ( fun ( T ) - >
{ xmlelement , " identity " , [ { " category " , " pubsub " } ,
{ " type " , T } ] , [ ] }
end , Types )
end ,
F = case Features of
false - >
[ ] ;
true - >
[ { xmlelement , " feature " , [ { " var " , ? NS_PUBSUB } ] , [ ] } |
lists : map ( fun ( T ) - >
{ xmlelement , " feature " , [ { " var " , ? NS_PUBSUB ++ " # " ++ T } ] , [ ] }
end , features ( Type ) ) ]
end ,
%% TODO: add meta-data info (spec section 5.4)
{ result , I ++ F }
end ,
2009-04-30 07:18:06 +02:00
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ , Result } } - > { result , Result } ;
Other - > Other
end .
2003-07-08 22:11:27 +02:00
2007-12-01 06:16:30 +01:00
iq_disco_info ( Host , SNode , From , Lang ) - >
2009-04-30 07:18:06 +02:00
[ RealSNode | _ ] = case SNode of
[ ] - > [ [ ] ] ;
_ - > string : tokens ( SNode , " ! " )
end ,
Node = string_to_node ( RealSNode ) ,
2003-07-08 22:11:27 +02:00
case Node of
2009-10-20 17:03:07 +02:00
< < > > - >
2007-12-01 06:16:30 +01:00
{ result ,
[ { xmlelement , " identity " ,
[ { " category " , " pubsub " } ,
{ " type " , " service " } ,
{ " name " , translate : translate ( Lang , " Publish-Subscribe " ) } ] , [ ] } ,
2009-02-19 12:25:56 +01:00
{ xmlelement , " feature " , [ { " var " , ? NS_DISCO_INFO } ] , [ ] } ,
{ xmlelement , " feature " , [ { " var " , ? NS_DISCO_ITEMS } ] , [ ] } ,
{ xmlelement , " feature " , [ { " var " , ? NS_PUBSUB } ] , [ ] } ,
2009-11-04 20:54:15 +01:00
{ xmlelement , " feature " , [ { " var " , ? NS_COMMANDS } ] , [ ] } ,
2009-02-19 12:25:56 +01:00
{ xmlelement , " feature " , [ { " var " , ? NS_VCARD } ] , [ ] } ] ++
2007-12-01 06:16:30 +01:00
lists : map ( fun ( Feature ) - >
2007-12-11 17:19:17 +01:00
{ xmlelement , " feature " , [ { " var " , ? NS_PUBSUB ++ " # " ++ Feature } ] , [ ] }
2009-04-30 07:18:06 +02:00
end , features ( Host , Node ) ) } ;
2009-11-04 20:54:15 +01:00
< < ? NS_COMMANDS > > - >
command_disco_info ( Host , Node , From ) ;
< < ? NS_PUBSUB_GET_PENDING > > - >
command_disco_info ( Host , Node , From ) ;
2003-07-08 22:11:27 +02:00
_ - >
2007-12-01 06:16:30 +01:00
node_disco_info ( Host , Node , From )
2003-07-08 22:11:27 +02:00
end .
2008-02-02 15:15:37 +01:00
iq_disco_items ( Host , [ ] , From ) - >
2009-10-27 15:07:35 +01:00
case tree_action ( Host , get_subnodes , [ Host , < < > > , From ] ) of
2010-03-05 11:35:20 +01:00
Nodes when is_list ( Nodes ) - >
{ result , lists : map (
fun ( #pubsub_node { nodeid = { _ , SubNode } , options = Options } ) - >
Attrs =
case get_option ( Options , title ) of
false - >
[ { " jid " , Host } | nodeAttr ( SubNode ) ] ;
Title - >
[ { " jid " , Host } , { " name " , Title } | nodeAttr ( SubNode ) ]
end ,
{ xmlelement , " item " , Attrs , [ ] }
end , Nodes ) } ;
Other - >
Other
2009-10-27 15:07:35 +01:00
end ;
2009-11-04 20:54:15 +01:00
iq_disco_items ( Host , ? NS_COMMANDS , _ From ) - >
%% TODO: support localization of this string
CommandItems = [ { xmlelement , " item " , [ { " jid " , Host } , { " node " , ? NS_PUBSUB_GET_PENDING } , { " name " , " Get Pending " } ] , [ ] } ] ,
{ result , CommandItems } ;
iq_disco_items ( _ Host , ? NS_PUBSUB_GET_PENDING , _ From ) - >
CommandItems = [ ] ,
{ result , CommandItems } ;
2008-02-02 15:15:37 +01:00
iq_disco_items ( Host , Item , From ) - >
2007-12-01 06:16:30 +01:00
case string : tokens ( Item , " ! " ) of
[ _ SNode , _ ItemID ] - >
{ result , [ ] } ;
[ SNode ] - >
Node = string_to_node ( SNode ) ,
Action =
2009-04-30 07:18:06 +02:00
fun ( #pubsub_node { type = Type , id = NodeId } ) - >
2009-09-09 23:40:21 +02:00
% TODO call get_items/6 instead for access control (EJAB-1033)
2009-04-30 07:18:06 +02:00
NodeItems = case node_call ( Type , get_items , [ NodeId , From ] ) of
2007-12-01 06:16:30 +01:00
{ result , I } - > I ;
_ - > [ ]
end ,
Nodes = lists : map (
2010-03-05 11:35:20 +01:00
fun ( #pubsub_node { nodeid = { _ , SubNode } , options = Options } ) - >
Attrs =
case get_option ( Options , title ) of
false - >
[ { " jid " , Host } | nodeAttr ( SubNode ) ] ;
Title - >
[ { " jid " , Host } , { " name " , Title } | nodeAttr ( SubNode ) ]
end ,
{ xmlelement , " item " , Attrs , [ ] }
end , tree_call ( Host , get_subnodes , [ Host , Node , From ] ) ) ,
2007-12-01 06:16:30 +01:00
Items = lists : map (
2010-03-05 11:35:20 +01:00
fun ( #pubsub_item { itemid = { RN , _ } } ) - >
{ result , Name } = node_call ( Type , get_item_name , [ Host , Node , RN ] ) ,
{ xmlelement , " item " , [ { " jid " , Host } , { " name " , Name } ] , [ ] }
end , NodeItems ) ,
2007-12-01 06:16:30 +01:00
{ result , Nodes ++ Items }
2006-06-07 16:07:18 +02:00
end ,
2009-04-30 07:18:06 +02:00
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ , Result } } - > { result , Result } ;
Other - > Other
end
2003-07-08 22:11:27 +02:00
end .
2009-04-08 17:53:46 +02:00
iq_local ( From , To , #iq { type = Type , sub_el = SubEl , xmlns = XMLNS , lang = Lang } = IQ ) - >
2007-12-01 06:16:30 +01:00
ServerHost = To #jid.lserver ,
%% Accept IQs to server only from our own users.
if
From #jid.lserver / = ServerHost - >
IQ #iq { type = error , sub_el = [ ? ERR_FORBIDDEN , SubEl ] } ;
true - >
LOwner = jlib : jid_tolower ( jlib : jid_remove_resource ( From ) ) ,
Res = case XMLNS of
? NS_PUBSUB - > iq_pubsub ( LOwner , ServerHost , From , Type , SubEl , Lang ) ;
2009-03-20 01:08:38 +01:00
? NS_PUBSUB_OWNER - > iq_pubsub_owner ( LOwner , ServerHost , From , Type , SubEl , Lang )
2007-12-01 06:16:30 +01:00
end ,
case Res of
{ result , IQRes } - > IQ #iq { type = result , sub_el = IQRes } ;
{ error , Error } - > IQ #iq { type = error , sub_el = [ Error , SubEl ] }
end
end .
2003-07-08 22:11:27 +02:00
2007-12-01 06:16:30 +01:00
iq_sm ( From , To , #iq { type = Type , sub_el = SubEl , xmlns = XMLNS , lang = Lang } = IQ ) - >
ServerHost = To #jid.lserver ,
LOwner = jlib : jid_tolower ( jlib : jid_remove_resource ( To ) ) ,
Res = case XMLNS of
? NS_PUBSUB - > iq_pubsub ( LOwner , ServerHost , From , Type , SubEl , Lang ) ;
2009-03-20 01:08:38 +01:00
? NS_PUBSUB_OWNER - > iq_pubsub_owner ( LOwner , ServerHost , From , Type , SubEl , Lang )
2007-12-01 06:16:30 +01:00
end ,
case Res of
{ result , IQRes } - > IQ #iq { type = result , sub_el = IQRes } ;
{ error , Error } - > IQ #iq { type = error , sub_el = [ Error , SubEl ] }
end .
2003-07-08 22:11:27 +02:00
2007-12-01 06:16:30 +01:00
iq_get_vcard ( Lang ) - >
[ { xmlelement , " FN " , [ ] , [ { xmlcdata , " ejabberd/mod_pubsub " } ] } ,
2007-12-04 22:35:41 +01:00
{ xmlelement , " URL " , [ ] , [ { xmlcdata , ? EJABBERD_URI } ] } ,
2007-12-01 06:16:30 +01:00
{ xmlelement , " DESC " , [ ] ,
[ { xmlcdata ,
translate : translate ( Lang ,
2007-12-15 13:02:00 +01:00
" ejabberd Publish-Subscribe module " ) ++
2010-01-12 17:11:32 +01:00
" \n Copyright (c) 2004-2010 ProcessOne " } ] } ] .
2007-12-01 06:16:30 +01:00
iq_pubsub ( Host , ServerHost , From , IQType , SubEl , Lang ) - >
2008-04-02 11:06:28 +02:00
iq_pubsub ( Host , ServerHost , From , IQType , SubEl , Lang , all , plugins ( ServerHost ) ) .
2007-12-01 06:16:30 +01:00
2009-05-29 04:14:07 +02:00
iq_pubsub ( Host , ServerHost , From , IQType , SubEl , Lang , Access , Plugins ) - >
2003-07-08 22:11:27 +02:00
{ xmlelement , _ , _ , SubEls } = SubEl ,
2009-05-29 04:14:07 +02:00
case xml : remove_cdata ( SubEls ) of
[ { xmlelement , Name , Attrs , Els } | Rest ] - >
2009-10-20 17:03:07 +02:00
Node = string_to_node ( xml : get_attr_s ( " node " , Attrs ) ) ,
2007-12-01 06:16:30 +01:00
case { IQType , Name } of
2003-07-08 22:11:27 +02:00
{ set , " create " } - >
2009-05-29 04:14:07 +02:00
Config = case Rest of
2009-03-04 02:12:10 +01:00
[ { xmlelement , " configure " , _ , C } ] - > C ;
_ - > [ ]
end ,
%% Get the type of the node
Type = case xml : get_attr_s ( " type " , Attrs ) of
[ ] - > hd ( Plugins ) ;
T - > T
end ,
%% we use Plugins list matching because we do not want to allocate
%% atoms for non existing type, this prevent atom allocation overflow
case lists : member ( Type , Plugins ) of
false - >
{ error , extended_error (
? ERR_FEATURE_NOT_IMPLEMENTED ,
unsupported , " create-nodes " ) } ;
true - >
create_node ( Host , ServerHost , Node , From ,
Type , Access , Config )
2007-12-01 06:16:30 +01:00
end ;
2003-07-08 22:11:27 +02:00
{ set , " publish " } - >
case xml : remove_cdata ( Els ) of
[ { xmlelement , " item " , ItemAttrs , Payload } ] - >
2007-12-01 06:16:30 +01:00
ItemId = xml : get_attr_s ( " id " , ItemAttrs ) ,
publish_item ( Host , ServerHost , Node , From , ItemId , Payload ) ;
[ ] - >
%% Publisher attempts to publish to persistent node with no item
{ error , extended_error ( ? ERR_BAD_REQUEST ,
" item-required " ) } ;
2003-07-08 22:11:27 +02:00
_ - >
2007-12-01 06:16:30 +01:00
%% Entity attempts to publish item with multiple payload elements or namespace does not match
{ error , extended_error ( ? ERR_BAD_REQUEST ,
" invalid-payload " ) }
2003-07-08 22:11:27 +02:00
end ;
{ set , " retract " } - >
2007-12-01 06:16:30 +01:00
ForceNotify = case xml : get_attr_s ( " notify " , Attrs ) of
" 1 " - > true ;
" true " - > true ;
_ - > false
end ,
2003-07-08 22:11:27 +02:00
case xml : remove_cdata ( Els ) of
[ { xmlelement , " item " , ItemAttrs , _ } ] - >
2007-12-01 06:16:30 +01:00
ItemId = xml : get_attr_s ( " id " , ItemAttrs ) ,
delete_item ( Host , Node , From , ItemId , ForceNotify ) ;
2003-07-08 22:11:27 +02:00
_ - >
2007-12-01 06:16:30 +01:00
%% Request does not specify an item
{ error , extended_error ( ? ERR_BAD_REQUEST ,
" item-required " ) }
2003-07-08 22:11:27 +02:00
end ;
{ set , " subscribe " } - >
2009-05-29 04:14:07 +02:00
Config = case Rest of
[ { xmlelement , " options " , _ , C } ] - > C ;
_ - > [ ]
end ,
2003-07-08 22:11:27 +02:00
JID = xml : get_attr_s ( " jid " , Attrs ) ,
2009-05-29 04:14:07 +02:00
subscribe_node ( Host , Node , From , JID , Config ) ;
2003-07-08 22:11:27 +02:00
{ set , " unsubscribe " } - >
JID = xml : get_attr_s ( " jid " , Attrs ) ,
2007-12-01 06:16:30 +01:00
SubId = xml : get_attr_s ( " subid " , Attrs ) ,
unsubscribe_node ( Host , Node , From , JID , SubId ) ;
2003-07-08 22:11:27 +02:00
{ get , " items " } - >
MaxItems = xml : get_attr_s ( " max_items " , Attrs ) ,
2008-07-03 11:56:31 +02:00
SubId = xml : get_attr_s ( " subid " , Attrs ) ,
ItemIDs = lists : foldl ( fun
( { xmlelement , " item " , ItemAttrs , _ } , Acc ) - >
case xml : get_attr_s ( " id " , ItemAttrs ) of
" " - > Acc ;
2008-08-26 00:54:48 +02:00
ItemID - > [ ItemID | Acc ]
2008-07-03 11:56:31 +02:00
end ;
( _ , Acc ) - >
Acc
end , [ ] , xml : remove_cdata ( Els ) ) ,
get_items ( Host , Node , From , SubId , MaxItems , ItemIDs ) ;
2007-12-01 06:16:30 +01:00
{ get , " subscriptions " } - >
2009-05-08 01:35:59 +02:00
get_subscriptions ( Host , Node , From , Plugins ) ;
2007-12-01 06:16:30 +01:00
{ get , " affiliations " } - >
get_affiliations ( Host , From , Plugins ) ;
2008-12-08 22:17:46 +01:00
{ get , " options " } - >
2009-05-29 04:14:07 +02:00
SubID = xml : get_attr_s ( " subid " , Attrs ) ,
JID = xml : get_attr_s ( " jid " , Attrs ) ,
get_options ( Host , Node , JID , SubID , Lang ) ;
2008-12-08 22:17:46 +01:00
{ set , " options " } - >
2009-05-29 04:14:07 +02:00
SubID = xml : get_attr_s ( " subid " , Attrs ) ,
JID = xml : get_attr_s ( " jid " , Attrs ) ,
set_options ( Host , Node , JID , SubID , Els ) ;
2007-12-01 06:16:30 +01:00
_ - >
{ error , ? ERR_FEATURE_NOT_IMPLEMENTED }
end ;
2009-05-29 04:14:07 +02:00
Other - >
? INFO_MSG ( " Too many actions: ~p " , [ Other ] ) ,
2007-12-01 06:16:30 +01:00
{ error , ? ERR_BAD_REQUEST }
end .
2009-03-20 01:08:38 +01:00
iq_pubsub_owner ( Host , ServerHost , From , IQType , SubEl , Lang ) - >
2007-12-01 06:16:30 +01:00
{ xmlelement , _ , _ , SubEls } = SubEl ,
Action = xml : remove_cdata ( SubEls ) ,
case Action of
[ { xmlelement , Name , Attrs , Els } ] - >
2009-10-20 17:03:07 +02:00
Node = string_to_node ( xml : get_attr_s ( " node " , Attrs ) ) ,
2007-12-01 06:16:30 +01:00
case { IQType , Name } of
{ get , " configure " } - >
2009-03-20 01:08:38 +01:00
get_configure ( Host , ServerHost , Node , From , Lang ) ;
2007-12-01 06:16:30 +01:00
{ set , " configure " } - >
set_configure ( Host , Node , From , Els , Lang ) ;
{ get , " default " } - >
get_default ( Host , Node , From , Lang ) ;
2003-07-08 22:11:27 +02:00
{ set , " delete " } - >
2007-12-01 06:16:30 +01:00
delete_node ( Host , Node , From ) ;
2003-07-08 22:11:27 +02:00
{ set , " purge " } - >
2007-12-01 06:16:30 +01:00
purge_node ( Host , Node , From ) ;
{ get , " subscriptions " } - >
get_subscriptions ( Host , Node , From ) ;
{ set , " subscriptions " } - >
set_subscriptions ( Host , Node , From , xml : remove_cdata ( Els ) ) ;
2005-07-20 05:09:34 +02:00
{ get , " affiliations " } - >
2007-12-01 06:16:30 +01:00
get_affiliations ( Host , Node , From ) ;
{ set , " affiliations " } - >
set_affiliations ( Host , Node , From , xml : remove_cdata ( Els ) ) ;
2003-07-08 22:11:27 +02:00
_ - >
{ error , ? ERR_FEATURE_NOT_IMPLEMENTED }
end ;
_ - >
2007-12-01 06:16:30 +01:00
? INFO_MSG ( " Too many actions: ~p " , [ Action ] ) ,
2003-07-08 22:11:27 +02:00
{ error , ? ERR_BAD_REQUEST }
end .
2009-07-20 10:53:56 +02:00
iq_command ( Host , ServerHost , From , IQ , Access , Plugins ) - >
case adhoc : parse_request ( IQ ) of
2009-07-31 16:55:05 +02:00
Req when is_record ( Req , adhoc_request ) - >
case adhoc_request ( Host , ServerHost , From , Req , Access , Plugins ) of
Resp when is_record ( Resp , adhoc_response ) - >
{ result , [ adhoc : produce_response ( Req , Resp ) ] } ;
Error - >
Error
end ;
Err - >
Err
2009-07-20 10:53:56 +02:00
end .
%% @doc <p>Processes an Ad Hoc Command.</p>
adhoc_request ( Host , _ ServerHost , Owner ,
2009-07-31 16:55:05 +02:00
#adhoc_request { node = ? NS_PUBSUB_GET_PENDING ,
lang = Lang ,
action = " execute " ,
xdata = false } ,
_ Access , Plugins ) - >
2009-07-20 10:53:56 +02:00
send_pending_node_form ( Host , Owner , Lang , Plugins ) ;
adhoc_request ( Host , _ ServerHost , Owner ,
2009-07-31 16:55:05 +02:00
#adhoc_request { node = ? NS_PUBSUB_GET_PENDING ,
action = " execute " ,
xdata = XData } ,
_ Access , _ Plugins ) - >
2009-07-20 10:53:56 +02:00
ParseOptions = case XData of
{ xmlelement , " x " , _ Attrs , _ SubEls } = XEl - >
case jlib : parse_xdata_submit ( XEl ) of
invalid - >
{ error , ? ERR_BAD_REQUEST } ;
XData2 - >
2009-09-23 17:56:06 +02:00
case set_xoption ( Host , XData2 , [ ] ) of
2009-07-20 10:53:56 +02:00
NewOpts when is_list ( NewOpts ) - >
{ result , NewOpts } ;
Err - >
Err
end
end ;
_ - >
? INFO_MSG ( " Bad XForm: ~p " , [ XData ] ) ,
{ error , ? ERR_BAD_REQUEST }
end ,
case ParseOptions of
2009-07-31 16:55:05 +02:00
{ result , XForm } - >
case lists : keysearch ( node , 1 , XForm ) of
{ value , { _ , Node } } - >
send_pending_auth_events ( Host , Node , Owner ) ;
false - >
2009-10-12 11:59:36 +02:00
{ error , extended_error ( ? ERR_BAD_REQUEST , " bad-payload " ) }
2009-07-31 16:55:05 +02:00
end ;
Error - >
Error
2009-07-20 10:53:56 +02:00
end ;
2009-11-04 20:54:15 +01:00
adhoc_request ( _ Host , _ ServerHost , _ Owner , #adhoc_request { action = " cancel " } ,
_ Access , _ Plugins ) - >
#adhoc_response { status = canceled } ;
adhoc_request ( Host , ServerHost , Owner , #adhoc_request { action = [ ] } = R ,
Access , Plugins ) - >
adhoc_request ( Host , ServerHost , Owner , R #adhoc_request { action = " execute " } ,
Access , Plugins ) ;
2009-07-20 10:53:56 +02:00
adhoc_request ( _ Host , _ ServerHost , _ Owner , Other , _ Access , _ Plugins ) - >
? DEBUG ( " Couldn't process ad hoc command: ~n ~p " , [ Other ] ) ,
{ error , ? ERR_ITEM_NOT_FOUND } .
2009-08-17 19:16:43 +02:00
%% @spec (Host, Owner, Lang, Plugins) -> iqRes()
2009-07-20 10:53:56 +02:00
%% @doc <p>Sends the process pending subscriptions XForm for Host to
%% Owner.</p>
send_pending_node_form ( Host , Owner , _ Lang , Plugins ) - >
Filter =
2009-07-31 16:55:05 +02:00
fun ( Plugin ) - >
lists : member ( " get-pending " , features ( Plugin ) )
end ,
2009-07-20 10:53:56 +02:00
case lists : filter ( Filter , Plugins ) of
2009-07-31 16:55:05 +02:00
[ ] - >
{ error , ? ERR_FEATURE_NOT_IMPLEMENTED } ;
Ps - >
XOpts = lists : map ( fun ( Node ) - >
{ xmlelement , " option " , [ ] ,
[ { xmlelement , " value " , [ ] ,
[ { xmlcdata , node_to_string ( Node ) } ] } ] }
end , get_pending_nodes ( Host , Owner , Ps ) ) ,
XForm = { xmlelement , " x " , [ { " xmlns " , ? NS_XDATA } , { " type " , " form " } ] ,
[ { xmlelement , " field " ,
[ { " type " , " list-single " } , { " var " , " pubsub#node " } ] ,
lists : usort ( XOpts ) } ] } ,
#adhoc_response { status = executing ,
defaultaction = " execute " ,
elements = [ XForm ] }
2009-07-20 10:53:56 +02:00
end .
get_pending_nodes ( Host , Owner , Plugins ) - >
Tr =
2009-07-31 16:55:05 +02:00
fun ( Type ) - >
case node_call ( Type , get_pending_nodes , [ Host , Owner ] ) of
{ result , Nodes } - > Nodes ;
_ - > [ ]
end
end ,
2009-07-20 10:53:56 +02:00
case transaction ( fun ( ) - > { result , lists : flatmap ( Tr , Plugins ) } end ,
2009-07-31 16:55:05 +02:00
sync_dirty ) of
{ result , Res } - > Res ;
Err - > Err
2009-07-20 10:53:56 +02:00
end .
%% @spec (Host, Node, Owner) -> iqRes()
%% @doc <p>Send a subscription approval form to Owner for all pending
%% subscriptions on Host and Node.</p>
send_pending_auth_events ( Host , Node , Owner ) - >
? DEBUG ( " Sending pending auth events for ~s on ~s : ~s " ,
2009-07-31 16:55:05 +02:00
[ jlib : jid_to_string ( Owner ) , Host , node_to_string ( Node ) ] ) ,
2009-07-20 10:53:56 +02:00
Action =
2009-11-04 18:38:51 +01:00
fun ( #pubsub_node { id = NodeID , type = Type } ) - >
2009-07-31 16:55:05 +02:00
case lists : member ( " get-pending " , features ( Type ) ) of
true - >
case node_call ( Type , get_affiliation , [ NodeID , Owner ] ) of
{ result , owner } - >
2010-03-05 11:35:20 +01:00
node_call ( Type , get_node_subscriptions , [ NodeID ] ) ;
2009-07-31 16:55:05 +02:00
_ - >
{ error , ? ERR_FORBIDDEN }
end ;
false - >
{ error , ? ERR_FEATURE_NOT_IMPLEMENTED }
end
end ,
2009-07-20 10:53:56 +02:00
case transaction ( Host , Node , Action , sync_dirty ) of
2009-11-04 18:38:51 +01:00
{ result , { N , Subscriptions } } - >
2010-03-05 11:35:20 +01:00
lists : foreach ( fun ( { J , pending , _ SubID } ) - > send_authorization_request ( N , jlib : make_jid ( J ) ) ;
( { J , pending } ) - > send_authorization_request ( N , jlib : make_jid ( J ) ) ;
( _ ) - > ok
end , Subscriptions ) ,
2009-07-31 16:55:05 +02:00
#adhoc_response { } ;
Err - >
Err
2009-07-20 10:53:56 +02:00
end .
2007-12-01 06:16:30 +01:00
%%% authorization handling
2009-04-30 07:18:06 +02:00
send_authorization_request ( #pubsub_node { owners = Owners , nodeid = { Host , Node } } , Subscriber ) - >
2007-12-01 06:16:30 +01:00
Lang = " en " , %% TODO fix
Stanza = { xmlelement , " message " ,
[ ] ,
[ { xmlelement , " x " , [ { " xmlns " , ? NS_XDATA } , { " type " , " form " } ] ,
[ { xmlelement , " title " , [ ] ,
[ { xmlcdata , translate : translate ( Lang , " PubSub subscriber request " ) } ] } ,
{ xmlelement , " instructions " , [ ] ,
[ { xmlcdata , translate : translate ( Lang , " Choose whether to approve this entity's subscription. " ) } ] } ,
{ xmlelement , " field " ,
[ { " var " , " FORM_TYPE " } , { " type " , " hidden " } ] ,
[ { xmlelement , " value " , [ ] , [ { xmlcdata , ? NS_PUBSUB_SUB_AUTH } ] } ] } ,
{ xmlelement , " field " ,
[ { " var " , " pubsub#node " } , { " type " , " text-single " } ,
{ " label " , translate : translate ( Lang , " Node ID " ) } ] ,
[ { xmlelement , " value " , [ ] ,
[ { xmlcdata , node_to_string ( Node ) } ] } ] } ,
{ xmlelement , " field " , [ { " var " , " pubsub#subscriber_jid " } ,
{ " type " , " jid-single " } ,
{ " label " , translate : translate ( Lang , " Subscriber Address " ) } ] ,
[ { xmlelement , " value " , [ ] ,
[ { xmlcdata , jlib : jid_to_string ( Subscriber ) } ] } ] } ,
{ xmlelement , " field " ,
[ { " var " , " pubsub#allow " } ,
{ " type " , " boolean " } ,
2009-01-12 19:41:46 +01:00
{ " label " , translate : translate ( Lang , " Allow this Jabber ID to subscribe to this pubsub node? " ) } ] ,
2007-12-01 06:16:30 +01:00
[ { xmlelement , " value " , [ ] , [ { xmlcdata , " false " } ] } ] } ] } ] } ,
2009-04-30 07:18:06 +02:00
lists : foreach ( fun ( Owner ) - >
2009-12-04 17:31:25 +01:00
ejabberd_router : route ( service_jid ( Host ) , jlib : make_jid ( Owner ) , Stanza )
2009-04-30 07:18:06 +02:00
end , Owners ) .
2007-12-01 06:16:30 +01:00
find_authorization_response ( Packet ) - >
{ xmlelement , _ Name , _ Attrs , Els } = Packet ,
XData1 = lists : map ( fun ( { xmlelement , " x " , XAttrs , _ } = XEl ) - >
case xml : get_attr_s ( " xmlns " , XAttrs ) of
? NS_XDATA - >
case xml : get_attr_s ( " type " , XAttrs ) of
" cancel " - >
none ;
_ - >
jlib : parse_xdata_submit ( XEl )
end ;
_ - >
none
end ;
( _ ) - >
none
end , xml : remove_cdata ( Els ) ) ,
XData = lists : filter ( fun ( E ) - > E / = none end , XData1 ) ,
case XData of
[ invalid ] - > invalid ;
[ ] - > none ;
[ XFields ] when is_list ( XFields ) - >
2009-02-27 04:09:00 +01:00
? DEBUG ( " XFields: ~p " , [ XFields ] ) ,
2007-12-01 06:16:30 +01:00
case lists : keysearch ( " FORM_TYPE " , 1 , XFields ) of
2008-12-08 20:50:50 +01:00
{ value , { _ , [ ? NS_PUBSUB_SUB_AUTH ] } } - >
2007-12-01 06:16:30 +01:00
XFields ;
_ - >
invalid
end
end .
2008-12-08 20:50:50 +01:00
%% @spec (Host, JID, Node, Subscription) -> void
%% Host = mod_pubsub:host()
%% JID = jlib:jid()
2009-04-08 17:53:46 +02:00
%% SNode = string()
2009-05-29 04:14:07 +02:00
%% Subscription = atom() | {atom(), mod_pubsub:subid()}
2008-12-08 20:50:50 +01:00
%% @doc Send a message to JID with the supplied Subscription
2009-04-08 17:53:46 +02:00
send_authorization_approval ( Host , JID , SNode , Subscription ) - >
2009-05-29 04:14:07 +02:00
SubAttrs = case Subscription of
{ S , SID } - > [ { " subscription " , subscription_to_string ( S ) } ,
{ " subid " , SID } ] ;
S - > [ { " subscription " , subscription_to_string ( S ) } ]
end ,
2009-01-03 01:25:40 +01:00
Stanza = event_stanza (
[ { xmlelement , " subscription " ,
2009-10-20 17:03:07 +02:00
[ { " jid " , jlib : jid_to_string ( JID ) } | nodeAttr ( SNode ) ] ++ SubAttrs ,
2009-05-29 04:14:07 +02:00
[ ] } ] ) ,
2009-12-04 17:31:25 +01:00
ejabberd_router : route ( service_jid ( Host ) , JID , Stanza ) .
2008-12-08 20:50:50 +01:00
2007-12-01 06:16:30 +01:00
handle_authorization_response ( Host , From , To , Packet , XFields ) - >
case { lists : keysearch ( " pubsub#node " , 1 , XFields ) ,
lists : keysearch ( " pubsub#subscriber_jid " , 1 , XFields ) ,
lists : keysearch ( " pubsub#allow " , 1 , XFields ) } of
2008-12-08 20:50:50 +01:00
{ { value , { _ , [ SNode ] } } , { value , { _ , [ SSubscriber ] } } ,
{ value , { _ , [ SAllow ] } } } - >
2009-10-20 17:03:07 +02:00
Node = string_to_node ( SNode ) ,
2007-12-01 06:16:30 +01:00
Subscriber = jlib : string_to_jid ( SSubscriber ) ,
Allow = case SAllow of
" 1 " - > true ;
" true " - > true ;
_ - > false
end ,
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { type = Type , owners = Owners , id = NodeId } ) - >
2007-12-01 06:16:30 +01:00
IsApprover = lists : member ( jlib : jid_tolower ( jlib : jid_remove_resource ( From ) ) , Owners ) ,
2009-05-29 04:14:07 +02:00
{ result , Subscriptions } = node_call ( Type , get_subscriptions , [ NodeId , Subscriber ] ) ,
2007-12-01 06:16:30 +01:00
if
not IsApprover - >
{ error , ? ERR_FORBIDDEN } ;
true - >
2009-05-29 04:14:07 +02:00
update_auth ( Host , SNode , Type , NodeId ,
Subscriber , Allow ,
Subscriptions )
2007-12-01 06:16:30 +01:00
end
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
{ error , Error } - >
2007-12-22 01:04:05 +01:00
ejabberd_router : route (
To , From ,
jlib : make_error_reply ( Packet , Error ) ) ;
2009-04-30 07:18:06 +02:00
{ result , { _ , _ NewSubscription } } - >
2007-12-01 06:16:30 +01:00
%% XXX: notify about subscription state change, section 12.11
ok ;
_ - >
ejabberd_router : route (
To , From ,
jlib : make_error_reply ( Packet , ? ERR_INTERNAL_SERVER_ERROR ) )
end ;
_ - >
ejabberd_router : route (
To , From ,
jlib : make_error_reply ( Packet , ? ERR_NOT_ACCEPTABLE ) )
end .
2003-07-08 22:11:27 +02:00
2009-05-29 04:14:07 +02:00
update_auth ( Host , Node , Type , NodeId , Subscriber ,
Allow , Subscriptions ) - >
Subscription = lists : filter ( fun ( { pending , _ } ) - > true ;
( _ ) - > false
end , Subscriptions ) ,
case Subscription of
[ { pending , SubID } ] - > %% TODO does not work if several pending
NewSubscription = case Allow of
2009-07-17 21:58:42 +02:00
true - > subscribed ;
false - > none
2009-05-29 04:14:07 +02:00
end ,
2009-07-31 16:55:05 +02:00
node_call ( Type , set_subscriptions ,
[ NodeId , Subscriber , NewSubscription , SubID ] ) ,
2009-05-29 04:14:07 +02:00
send_authorization_approval ( Host , Subscriber , Node ,
NewSubscription ) ,
{ result , ok } ;
_ - >
{ error , ? ERR_UNEXPECTED_REQUEST }
end .
2003-07-09 20:58:43 +02:00
- define ( XFIELD ( Type , Label , Var , Val ) ,
{ xmlelement , " field " , [ { " type " , Type } ,
{ " label " , translate : translate ( Lang , Label ) } ,
{ " var " , Var } ] ,
[ { xmlelement , " value " , [ ] , [ { xmlcdata , Val } ] } ] } ) .
2003-07-08 22:11:27 +02:00
2005-07-20 05:09:34 +02:00
- define ( BOOLXFIELD ( Label , Var , Val ) ,
? XFIELD ( " boolean " , Label , Var ,
case Val of
true - > " 1 " ;
_ - > " 0 "
end ) ) .
- define ( STRINGXFIELD ( Label , Var , Val ) ,
? XFIELD ( " text-single " , Label , Var , Val ) ) .
2009-06-15 15:45:40 +02:00
- define ( STRINGMXFIELD ( Label , Var , Vals ) ,
{ xmlelement , " field " , [ { " type " , " text-multi " } ,
{ " label " , translate : translate ( Lang , Label ) } ,
{ " var " , Var } ] ,
[ { xmlelement , " value " , [ ] , [ { xmlcdata , V } ] } | | V < - Vals ] } ) .
2005-07-20 05:09:34 +02:00
- define ( XFIELDOPT ( Type , Label , Var , Val , Opts ) ,
{ xmlelement , " field " , [ { " type " , Type } ,
{ " label " , translate : translate ( Lang , Label ) } ,
{ " var " , Var } ] ,
lists : map ( fun ( Opt ) - >
{ xmlelement , " option " , [ ] ,
[ { xmlelement , " value " , [ ] ,
[ { xmlcdata , Opt } ] } ] }
end , Opts ) ++
[ { xmlelement , " value " , [ ] , [ { xmlcdata , Val } ] } ] } ) .
- define ( LISTXFIELD ( Label , Var , Val , Opts ) ,
? XFIELDOPT ( " list-single " , Label , Var , Val , Opts ) ) .
2009-03-20 01:08:38 +01:00
- define ( LISTMXFIELD ( Label , Var , Vals , Opts ) ,
{ xmlelement , " field " , [ { " type " , " list-multi " } ,
{ " label " , translate : translate ( Lang , Label ) } ,
{ " var " , Var } ] ,
lists : map ( fun ( Opt ) - >
{ xmlelement , " option " , [ ] ,
[ { xmlelement , " value " , [ ] ,
[ { xmlcdata , Opt } ] } ] }
end , Opts ) ++
lists : map ( fun ( Val ) - >
{ xmlelement , " value " , [ ] ,
[ { xmlcdata , Val } ] }
end , Vals ) } ) .
2007-12-01 06:16:30 +01:00
%% @spec (Host::host(), ServerHost::host(), Node::pubsubNode(), Owner::jid(), NodeType::nodeType()) ->
%% {error, Reason::stanzaError()} |
%% {result, []}
%% @doc <p>Create new pubsub nodes</p>
%%<p>In addition to method-specific error conditions, there are several general reasons why the node creation request might fail:</p>
%%<ul>
2008-04-01 12:11:39 +02:00
%%<li>The service does not support node creation.</li>
%%<li>Only entities that are registered with the service are allowed to create nodes but the requesting entity is not registered.</li>
%%<li>The requesting entity does not have sufficient privileges to create nodes.</li>
%%<li>The requested NodeID already exists.</li>
%%<li>The request did not include a NodeID and "instant nodes" are not supported.</li>
2007-12-01 06:16:30 +01:00
%%</ul>
%%<p>ote: node creation is a particular case, error return code is evaluated at many places:</p>
%%<ul>
2008-04-01 12:11:39 +02:00
%%<li>iq_pubsub checks if service supports node creation (type exists)</li>
%%<li>create_node checks if instant nodes are supported</li>
%%<li>create_node asks node plugin if entity have sufficient privilege</li>
%%<li>nodetree create_node checks if nodeid already exists</li>
%%<li>node plugin create_node just sets default affiliation/subscription</li>
2007-12-01 06:16:30 +01:00
%%</ul>
create_node ( Host , ServerHost , Node , Owner , Type ) - >
create_node ( Host , ServerHost , Node , Owner , Type , all , [ ] ) .
2009-10-20 17:03:07 +02:00
create_node ( Host , ServerHost , < < > > , Owner , Type , Access , Configuration ) - >
2007-12-01 06:16:30 +01:00
case lists : member ( " instant-nodes " , features ( Type ) ) of
true - >
2009-10-20 17:03:07 +02:00
NewNode = string_to_node ( randoms : get_string ( ) ) ,
2007-12-01 06:16:30 +01:00
case create_node ( Host , ServerHost ,
NewNode , Owner , Type , Access , Configuration ) of
{ result , [ ] } - >
{ result ,
[ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB } ] ,
2009-05-08 03:18:02 +02:00
[ { xmlelement , " create " , nodeAttr ( NewNode ) , [ ] } ] } ] } ;
2009-10-20 17:03:07 +02:00
Error - >
Error
2007-12-01 06:16:30 +01:00
end ;
false - >
%% Service does not support instant nodes
{ error , extended_error ( ? ERR_NOT_ACCEPTABLE , " nodeid-required " ) }
end ;
create_node ( Host , ServerHost , Node , Owner , GivenType , Access , Configuration ) - >
2008-09-25 19:26:06 +02:00
Type = select_type ( ServerHost , Host , Node , GivenType ) ,
2007-12-01 06:16:30 +01:00
%% TODO, check/set node_type = Type
ParseOptions = case xml : remove_cdata ( Configuration ) of
[ ] - >
{ result , node_options ( Type ) } ;
[ { xmlelement , " x " , _ Attrs , _ SubEls } = XEl ] - >
case jlib : parse_xdata_submit ( XEl ) of
invalid - >
{ error , ? ERR_BAD_REQUEST } ;
XData - >
2009-09-23 17:56:06 +02:00
case set_xoption ( Host , XData , node_options ( Type ) ) of
2007-12-01 06:16:30 +01:00
NewOpts when is_list ( NewOpts ) - >
{ result , NewOpts } ;
Err - >
Err
end
end ;
_ - >
? INFO_MSG ( " Node ~p ; bad configuration: ~p " , [ Node , Configuration ] ) ,
{ error , ? ERR_BAD_REQUEST }
end ,
case ParseOptions of
{ result , NodeOptions } - >
CreateNode =
fun ( ) - >
2009-10-20 17:03:07 +02:00
SNode = node_to_string ( Node ) ,
Parent = case node_call ( Type , node_to_path , [ Node ] ) of
{ result , [ SNode ] } - > < < > > ;
{ result , Path } - > element ( 2 , node_call ( Type , path_to_node , [ lists : sublist ( Path , length ( Path ) - 1 ) ] ) )
end ,
Parents = case Parent of
< < > > - > [ ] ;
_ - > [ Parent ]
end ,
2007-12-01 06:16:30 +01:00
case node_call ( Type , create_node_permission , [ Host , ServerHost , Node , Parent , Owner , Access ] ) of
{ result , true } - >
2009-10-20 17:03:07 +02:00
case tree_call ( Host , create_node , [ Host , Node , Type , Owner , NodeOptions , Parents ] ) of
2009-04-30 07:18:06 +02:00
{ ok , NodeId } - >
node_call ( Type , create_node , [ NodeId , Owner ] ) ;
{ error , { virtual , NodeId } } - >
node_call ( Type , create_node , [ NodeId , Owner ] ) ;
2007-12-01 06:16:30 +01:00
Error - >
Error
end ;
2003-07-08 22:11:27 +02:00
_ - >
2007-12-01 06:16:30 +01:00
{ error , ? ERR_FORBIDDEN }
2003-07-08 22:11:27 +02:00
end
end ,
2007-12-01 06:16:30 +01:00
Reply = [ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB } ] ,
2009-05-08 03:18:02 +02:00
[ { xmlelement , " create " , nodeAttr ( Node ) ,
2007-12-01 06:16:30 +01:00
[ ] } ] } ] ,
case transaction ( CreateNode , transaction ) of
{ result , { Result , broadcast } } - >
%%Lang = "en", %% TODO: fix
%%OwnerKey = jlib:jid_tolower(jlib:jid_remove_resource(Owner)),
%%broadcast_publish_item(Host, Node, uniqid(), Owner,
%% [{xmlelement, "x", [{"xmlns", ?NS_XDATA}, {"type", "result"}],
%% [?XFIELD("hidden", "", "FORM_TYPE", ?NS_PUBSUB_NMI),
%% ?XFIELD("jid-single", "Node Creator", "creator", jlib:jid_to_string(OwnerKey))]}]),
case Result of
default - > { result , Reply } ;
_ - > { result , Result }
2003-07-08 22:11:27 +02:00
end ;
2007-12-01 06:16:30 +01:00
{ result , default } - >
{ result , Reply } ;
{ result , Result } - >
2009-04-30 07:18:06 +02:00
{ result , Result } ;
Error - >
%% in case we change transaction to sync_dirty...
%% node_call(Type, delete_node, [Host, Node]),
%% tree_call(Host, delete_node, [Host, Node]),
Error
2007-12-01 06:16:30 +01:00
end ;
Error - >
Error
2003-07-08 22:11:27 +02:00
end .
2007-12-01 06:16:30 +01:00
%% @spec (Host, Node, Owner) ->
%% {error, Reason} | {result, []}
%% Host = host()
%% Node = pubsubNode()
%% Owner = jid()
%% Reason = stanzaError()
%% @doc <p>Delete specified node and all childs.</p>
%%<p>There are several reasons why the node deletion request might fail:</p>
%%<ul>
2008-04-01 12:11:39 +02:00
%%<li>The requesting entity does not have sufficient privileges to delete the node.</li>
%%<li>The node is the root collection node, which cannot be deleted.</li>
%%<li>The specified node does not exist.</li>
2007-12-01 06:16:30 +01:00
%%</ul>
2009-11-05 18:36:15 +01:00
delete_node ( _ Host , < < > > , _ Owner ) - >
2007-12-01 06:16:30 +01:00
%% Node is the root
{ error , ? ERR_NOT_ALLOWED } ;
delete_node ( Host , Node , Owner ) - >
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { type = Type , id = NodeId } ) - >
2009-08-27 23:38:23 +02:00
case node_call ( Type , get_affiliation , [ NodeId , Owner ] ) of
{ result , owner } - >
2009-08-28 00:36:46 +02:00
ParentTree = tree_call ( Host , get_parentnodes_tree , [ Host , Node , service_jid ( Host ) ] ) ,
SubsByDepth = [ { Depth , [ { N , get_node_subs ( N ) } | | N < - Nodes ] } | | { Depth , Nodes } < - ParentTree ] ,
2009-08-27 23:38:23 +02:00
Removed = tree_call ( Host , delete_node , [ Host , Node ] ) ,
case node_call ( Type , delete_node , [ Removed ] ) of
2009-06-15 15:45:40 +02:00
{ result , Res } - > { result , { SubsByDepth , Res } } ;
2009-08-27 23:38:23 +02:00
Error - > Error
end ;
_ - >
%% Entity is not an owner
{ error , ? ERR_FORBIDDEN }
end
2007-12-01 06:16:30 +01:00
end ,
Reply = [ ] ,
case transaction ( Host , Node , Action , transaction ) of
2009-06-15 15:45:40 +02:00
{ result , { _ , { SubsByDepth , { Result , broadcast , Removed } } } } - >
lists : foreach ( fun ( { RNode , _ RSubscriptions } ) - >
2009-04-30 07:18:06 +02:00
{ RH , RN } = RNode #pubsub_node.nodeid ,
NodeId = RNode #pubsub_node.id ,
Type = RNode #pubsub_node.type ,
Options = RNode #pubsub_node.options ,
2009-06-15 15:45:40 +02:00
broadcast_removed_node ( RH , RN , NodeId , Type , Options , SubsByDepth )
2009-01-03 01:25:40 +01:00
end , Removed ) ,
2007-12-01 06:16:30 +01:00
case Result of
default - > { result , Reply } ;
_ - > { result , Result }
end ;
2009-06-15 15:45:40 +02:00
{ result , { _ , { _ , { Result , _ Removed } } } } - >
2007-12-01 06:16:30 +01:00
case Result of
default - > { result , Reply } ;
_ - > { result , Result }
end ;
2009-06-15 15:45:40 +02:00
{ result , { _ , { _ , default } } } - >
2007-12-01 06:16:30 +01:00
{ result , Reply } ;
2009-06-15 15:45:40 +02:00
{ result , { _ , { _ , Result } } } - >
2009-04-30 07:18:06 +02:00
{ result , Result } ;
Error - >
Error
2003-07-08 22:11:27 +02:00
end .
2009-08-17 19:16:43 +02:00
%% @spec (Host, Node, From, JID, Configuration) ->
2007-12-01 06:16:30 +01:00
%% {error, Reason::stanzaError()} |
%% {result, []}
%% Host = host()
%% Node = pubsubNode()
%% From = jid()
%% JID = jid()
2009-05-29 02:27:26 +02:00
%% @see node_hometree:subscribe_node/5
2008-04-01 12:11:39 +02:00
%% @doc <p>Accepts or rejects subcription requests on a PubSub node.</p>
2007-12-01 06:16:30 +01:00
%%<p>There are several reasons why the subscription request might fail:</p>
%%<ul>
2008-04-01 12:11:39 +02:00
%%<li>The bare JID portions of the JIDs do not match.</li>
%%<li>The node has an access model of "presence" and the requesting entity is not subscribed to the owner's presence.</li>
%%<li>The node has an access model of "roster" and the requesting entity is not in one of the authorized roster groups.</li>
%%<li>The node has an access model of "whitelist" and the requesting entity is not on the whitelist.</li>
%%<li>The service requires payment for subscriptions to the node.</li>
%%<li>The requesting entity is anonymous and the service does not allow anonymous entities to subscribe.</li>
%%<li>The requesting entity has a pending subscription.</li>
%%<li>The requesting entity is blocked from subscribing (e.g., because having an affiliation of outcast).</li>
%%<li>The node does not support subscriptions.</li>
%%<li>The node does not exist.</li>
2007-12-01 06:16:30 +01:00
%%</ul>
2009-05-29 04:14:07 +02:00
subscribe_node ( Host , Node , From , JID , Configuration ) - >
2009-10-12 11:59:36 +02:00
SubOpts = case pubsub_subscription : parse_options_xform ( Configuration ) of
{ result , GoodSubOpts } - > GoodSubOpts ;
_ - > invalid
end ,
2007-12-01 06:16:30 +01:00
Subscriber = case jlib : string_to_jid ( JID ) of
error - > { " " , " " , " " } ;
J - > jlib : jid_tolower ( J )
end ,
2009-05-08 01:35:59 +02:00
Action = fun ( #pubsub_node { options = Options , owners = [ Owner | _ ] , type = Type , id = NodeId } ) - >
Features = features ( Type ) ,
SubscribeFeature = lists : member ( " subscribe " , Features ) ,
2009-05-29 04:14:07 +02:00
OptionsFeature = lists : member ( " subscription-options " , Features ) ,
HasOptions = not ( SubOpts == [ ] ) ,
2009-05-08 01:35:59 +02:00
SubscribeConfig = get_option ( Options , subscribe ) ,
AccessModel = get_option ( Options , access_model ) ,
SendLast = get_option ( Options , send_last_published_item ) ,
AllowedGroups = get_option ( Options , roster_groups_allowed , [ ] ) ,
{ PresenceSubscription , RosterGroup } =
case Host of
{ OUser , OServer , _ } - >
get_roster_info ( OUser , OServer ,
Subscriber , AllowedGroups ) ;
_ - >
case Subscriber of
{ " " , " " , " " } - >
{ false , false } ;
_ - >
{ OU , OS , _ } = Owner ,
get_roster_info ( OU , OS ,
Subscriber , AllowedGroups )
end
end ,
if
not SubscribeFeature - >
%% Node does not support subscriptions
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " subscribe " ) } ;
not SubscribeConfig - >
%% Node does not support subscriptions
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " subscribe " ) } ;
2009-05-29 04:14:07 +02:00
HasOptions andalso not OptionsFeature - >
%% Node does not support subscription options
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " subscription-options " ) } ;
2009-10-12 11:59:36 +02:00
SubOpts == invalid - >
%% Passed invalit options submit form
{ error , extended_error ( ? ERR_BAD_REQUEST , " invalid-options " ) } ;
2009-05-08 01:35:59 +02:00
true - >
node_call ( Type , subscribe_node ,
[ NodeId , From , Subscriber ,
2007-12-01 06:16:30 +01:00
AccessModel , SendLast ,
2009-05-29 04:14:07 +02:00
PresenceSubscription , RosterGroup ,
SubOpts ] )
2009-05-08 01:35:59 +02:00
end
end ,
2007-12-01 06:16:30 +01:00
Reply = fun ( Subscription ) - >
%% TODO, this is subscription-notification, should depends on node features
2009-05-29 04:14:07 +02:00
SubAttrs = case Subscription of
{ subscribed , SubId } - >
[ { " subscription " , subscription_to_string ( subscribed ) } ,
{ " subid " , SubId } ] ;
Other - >
[ { " subscription " , subscription_to_string ( Other ) } ]
end ,
2007-12-01 06:16:30 +01:00
Fields =
2009-05-29 04:14:07 +02:00
[ { " jid " , jlib : jid_to_string ( Subscriber ) } | SubAttrs ] ,
2008-03-21 17:05:32 +01:00
[ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB } ] ,
2009-05-29 04:14:07 +02:00
[ { xmlelement , " subscription " , Fields , [ ] } ] } ]
2007-12-01 06:16:30 +01:00
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
2009-05-29 04:14:07 +02:00
{ result , { TNode , { Result , subscribed , SubId , send_last } } } - >
2009-04-30 07:18:06 +02:00
NodeId = TNode #pubsub_node.id ,
Type = TNode #pubsub_node.type ,
send_items ( Host , Node , NodeId , Type , Subscriber , last ) ,
2007-12-01 06:16:30 +01:00
case Result of
2009-05-29 04:14:07 +02:00
default - > { result , Reply ( { subscribed , SubId } ) } ;
2007-12-01 06:16:30 +01:00
_ - > { result , Result }
end ;
2009-05-29 04:14:07 +02:00
{ result , { _ TNode , { default , subscribed , SubId } } } - >
{ result , Reply ( { subscribed , SubId } ) } ;
{ result , { _ TNode , { Result , subscribed , _ SubId } } } - >
{ result , Result } ;
{ result , { TNode , { default , pending , _ SubId } } } - >
send_authorization_request ( TNode , Subscriber ) ,
{ result , Reply ( pending ) } ;
{ result , { TNode , { Result , pending } } } - >
send_authorization_request ( TNode , Subscriber ) ,
{ result , Result } ;
2009-04-30 07:18:06 +02:00
{ result , { _ , Result } } - >
2007-12-01 06:16:30 +01:00
%% this case should never occure anyway
2009-04-30 07:18:06 +02:00
{ result , Result } ;
Error - >
Error
2003-07-08 22:11:27 +02:00
end .
2008-04-01 12:11:39 +02:00
%% @spec (Host, Noce, From, JID, SubId) -> {error, Reason} | {result, []}
2007-12-01 06:16:30 +01:00
%% Host = host()
%% Node = pubsubNode()
%% From = jid()
%% JID = string()
%% SubId = string()
%% Reason = stanzaError()
%% @doc <p>Unsubscribe <tt>JID</tt> from the <tt>Node</tt>.</p>
%%<p>There are several reasons why the unsubscribe request might fail:</p>
%%<ul>
2008-04-01 12:11:39 +02:00
%%<li>The requesting entity has multiple subscriptions to the node but does not specify a subscription ID.</li>
%%<li>The request does not specify an existing subscriber.</li>
%%<li>The requesting entity does not have sufficient privileges to unsubscribe the specified JID.</li>
%%<li>The node does not exist.</li>
%%<li>The request specifies a subscription ID that is not valid or current.</li>
%%</ul>
2009-01-08 15:06:35 +01:00
unsubscribe_node ( Host , Node , From , JID , SubId ) when is_list ( JID ) - >
2007-12-01 06:16:30 +01:00
Subscriber = case jlib : string_to_jid ( JID ) of
error - > { " " , " " , " " } ;
J - > jlib : jid_tolower ( J )
end ,
2009-01-08 15:06:35 +01:00
unsubscribe_node ( Host , Node , From , Subscriber , SubId ) ;
unsubscribe_node ( Host , Node , From , Subscriber , SubId ) - >
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { type = Type , id = NodeId } ) - >
node_call ( Type , unsubscribe_node , [ NodeId , From , Subscriber , SubId ] )
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ , default } } - >
2007-12-01 06:16:30 +01:00
{ result , [ ] } ;
2009-04-30 07:18:06 +02:00
{ result , { _ , Result } } - >
{ result , Result } ;
Error - >
Error
2007-12-01 06:16:30 +01:00
end .
2003-07-08 22:11:27 +02:00
2007-12-01 06:16:30 +01:00
%% @spec (Host::host(), ServerHost::host(), JID::jid(), Node::pubsubNode(), ItemId::string(), Payload::term()) ->
%% {error, Reason::stanzaError()} |
%% {result, []}
%% @doc <p>Publish item to a PubSub node.</p>
%% <p>The permission to publish an item must be verified by the plugin implementation.</p>
%%<p>There are several reasons why the publish request might fail:</p>
%%<ul>
2008-04-01 12:11:39 +02:00
%%<li>The requesting entity does not have sufficient privileges to publish.</li>
%%<li>The node does not support item publication.</li>
%%<li>The node does not exist.</li>
%%<li>The payload size exceeds a service-defined limit.</li>
%%<li>The item contains more than one payload element or the namespace of the root payload element does not match the configured namespace for the node.</li>
%%<li>The request does not match the node configuration.</li>
2007-12-01 06:16:30 +01:00
%%</ul>
publish_item ( Host , ServerHost , Node , Publisher , " " , Payload ) - >
%% if publisher does not specify an ItemId, the service MUST generate the ItemId
publish_item ( Host , ServerHost , Node , Publisher , uniqid ( ) , Payload ) ;
publish_item ( Host , ServerHost , Node , Publisher , ItemId , Payload ) - >
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { options = Options , type = Type , id = NodeId } ) - >
2008-12-08 20:50:50 +01:00
Features = features ( Type ) ,
PublishFeature = lists : member ( " publish " , Features ) ,
PublishModel = get_option ( Options , publish_model ) ,
2009-07-30 23:37:19 +02:00
MaxItems = max_items ( Host , Options ) ,
2009-02-25 22:17:14 +01:00
DeliverPayloads = get_option ( Options , deliver_payloads ) ,
PersistItems = get_option ( Options , persist_items ) ,
2008-12-09 23:02:47 +01:00
PayloadCount = payload_xmlelements ( Payload ) ,
2008-12-08 20:50:50 +01:00
PayloadSize = size ( term_to_binary ( Payload ) ) ,
PayloadMaxSize = get_option ( Options , max_payload_size ) ,
% pubsub#deliver_payloads true
% pubsub#persist_items true -> 1 item; false -> 0 item
if
not PublishFeature - >
%% Node does not support item publication
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " publish " ) } ;
PayloadSize > PayloadMaxSize - >
%% Entity attempts to publish very large payload
{ error , extended_error ( ? ERR_NOT_ACCEPTABLE , " payload-too-big " ) } ;
2009-05-08 01:35:59 +02:00
( PayloadCount == 0 ) and ( Payload == [ ] ) - >
2008-12-08 20:50:50 +01:00
%% Publisher attempts to publish to payload node with no payload
{ error , extended_error ( ? ERR_BAD_REQUEST , " payload-required " ) } ;
2009-05-08 01:35:59 +02:00
( PayloadCount > 1 ) or ( PayloadCount == 0 ) - >
2009-04-10 09:36:17 +02:00
%% Entity attempts to publish item with multiple payload elements
{ error , extended_error ( ? ERR_BAD_REQUEST , " invalid-payload " ) } ;
2009-02-25 22:17:14 +01:00
( DeliverPayloads == 0 ) and ( PersistItems == 0 ) and ( PayloadSize > 0 ) - >
%% Publisher attempts to publish to transient notification node with item
2008-12-08 20:50:50 +01:00
{ error , extended_error ( ? ERR_BAD_REQUEST , " item-forbidden " ) } ;
2009-02-25 22:17:14 +01:00
( ( DeliverPayloads == 1 ) or ( PersistItems == 1 ) ) and ( PayloadSize == 0 ) - >
%% Publisher attempts to publish to persistent node with no item
2008-12-08 20:50:50 +01:00
{ error , extended_error ( ? ERR_BAD_REQUEST , " item-required " ) } ;
true - >
2009-04-30 07:18:06 +02:00
node_call ( Type , publish_item , [ NodeId , Publisher , PublishModel , MaxItems , ItemId , Payload ] )
2008-12-08 20:50:50 +01:00
end
end ,
2008-10-14 14:41:46 +02:00
ejabberd_hooks : run ( pubsub_publish_item , ServerHost , [ ServerHost , Node , Publisher , service_jid ( Host ) , ItemId , Payload ] ) ,
2009-05-08 03:18:02 +02:00
Reply = [ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB } ] ,
[ { xmlelement , " publish " , nodeAttr ( Node ) ,
[ { xmlelement , " item " , itemAttr ( ItemId ) , [ ] } ] } ] } ] ,
2007-12-01 06:16:30 +01:00
case transaction ( Host , Node , Action , sync_dirty ) of
2009-10-13 18:43:02 +02:00
{ result , { TNode , { Result , Broadcast , Removed } } } - >
2009-04-30 07:18:06 +02:00
NodeId = TNode #pubsub_node.id ,
Type = TNode #pubsub_node.type ,
Options = TNode #pubsub_node.options ,
2009-10-13 18:43:02 +02:00
BroadcastPayload = case Broadcast of
default - > Payload ;
broadcast - > Payload ;
PluginPayload - > PluginPayload
end ,
broadcast_publish_item ( Host , Node , NodeId , Type , Options , Removed , ItemId , jlib : jid_tolower ( Publisher ) , BroadcastPayload ) ,
2009-05-29 00:30:43 +02:00
set_cached_item ( Host , NodeId , ItemId , Payload ) ,
2009-04-30 07:18:06 +02:00
case Result of
default - > { result , Reply } ;
_ - > { result , Result }
end ;
{ result , { TNode , { default , Removed } } } - >
NodeId = TNode #pubsub_node.id ,
Type = TNode #pubsub_node.type ,
Options = TNode #pubsub_node.options ,
broadcast_retract_items ( Host , Node , NodeId , Type , Options , Removed ) ,
2009-05-29 00:30:43 +02:00
set_cached_item ( Host , NodeId , ItemId , Payload ) ,
2009-04-30 07:18:06 +02:00
{ result , Reply } ;
{ result , { TNode , { Result , Removed } } } - >
NodeId = TNode #pubsub_node.id ,
Type = TNode #pubsub_node.type ,
Options = TNode #pubsub_node.options ,
broadcast_retract_items ( Host , Node , NodeId , Type , Options , Removed ) ,
2009-05-29 00:30:43 +02:00
set_cached_item ( Host , NodeId , ItemId , Payload ) ,
2009-04-30 07:18:06 +02:00
{ result , Result } ;
{ result , { _ , default } } - >
{ result , Reply } ;
{ result , { _ , Result } } - >
{ result , Result } ;
2007-12-01 06:16:30 +01:00
{ error , ? ERR_ITEM_NOT_FOUND } - >
%% handles auto-create feature
%% for automatic node creation. we'll take the default node type:
2007-12-10 15:09:31 +01:00
%% first listed into the plugins configuration option, or pep
2008-09-25 19:26:06 +02:00
Type = select_type ( ServerHost , Host , Node ) ,
2007-12-01 06:16:30 +01:00
case lists : member ( " auto-create " , features ( Type ) ) of
true - >
case create_node ( Host , ServerHost , Node , Publisher , Type ) of
2007-12-10 15:09:31 +01:00
{ result , _ } - >
publish_item ( Host , ServerHost , Node , Publisher , ItemId , Payload ) ;
2007-12-01 06:16:30 +01:00
_ - >
{ error , ? ERR_ITEM_NOT_FOUND }
end ;
false - >
{ error , ? ERR_ITEM_NOT_FOUND }
2003-07-08 22:11:27 +02:00
end ;
2009-04-30 07:18:06 +02:00
Error - >
Error
2003-07-08 22:11:27 +02:00
end .
2007-12-01 06:16:30 +01:00
%% @spec (Host::host(), JID::jid(), Node::pubsubNode(), ItemId::string()) ->
%% {error, Reason::stanzaError()} |
%% {result, []}
%% @doc <p>Delete item from a PubSub node.</p>
%% <p>The permission to delete an item must be verified by the plugin implementation.</p>
%%<p>There are several reasons why the item retraction request might fail:</p>
%%<ul>
2008-04-01 12:11:39 +02:00
%%<li>The publisher does not have sufficient privileges to delete the requested item.</li>
%%<li>The node or item does not exist.</li>
%%<li>The request does not specify a node.</li>
%%<li>The request does not include an <item/> element or the <item/> element does not specify an ItemId.</li>
%%<li>The node does not support persistent items.</li>
%%<li>The service does not support the deletion of items.</li>
2007-12-01 06:16:30 +01:00
%%</ul>
delete_item ( Host , Node , Publisher , ItemId ) - >
delete_item ( Host , Node , Publisher , ItemId , false ) .
delete_item ( _ , " " , _ , _ , _ ) - >
%% Request does not specify a node
{ error , extended_error ( ? ERR_BAD_REQUEST , " node-required " ) } ;
delete_item ( Host , Node , Publisher , ItemId , ForceNotify ) - >
2009-05-07 02:09:02 +02:00
Action = fun ( #pubsub_node { options = Options , type = Type , id = NodeId } ) - >
Features = features ( Type ) ,
PersistentFeature = lists : member ( " persistent-items " , Features ) ,
DeleteFeature = lists : member ( " delete-items " , Features ) ,
PublishModel = get_option ( Options , publish_model ) ,
if
%%-> iq_pubsub just does that matchs
%% %% Request does not specify an item
%% {error, extended_error(?ERR_BAD_REQUEST, "item-required")};
not PersistentFeature - >
%% Node does not support persistent items
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " persistent-items " ) } ;
not DeleteFeature - >
%% Service does not support item deletion
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " delete-items " ) } ;
true - >
node_call ( Type , delete_item , [ NodeId , Publisher , PublishModel , ItemId ] )
end
2007-12-01 06:16:30 +01:00
end ,
Reply = [ ] ,
case transaction ( Host , Node , Action , sync_dirty ) of
2009-04-30 07:18:06 +02:00
{ result , { TNode , { Result , broadcast } } } - >
NodeId = TNode #pubsub_node.id ,
Type = TNode #pubsub_node.type ,
Options = TNode #pubsub_node.options ,
broadcast_retract_items ( Host , Node , NodeId , Type , Options , [ ItemId ] , ForceNotify ) ,
2009-05-29 00:30:43 +02:00
case get_cached_item ( Host , NodeId ) of
#pubsub_item { itemid = { ItemId , NodeId } , _ = '_' } - > unset_cached_item ( Host , NodeId ) ;
_ - > ok
end ,
2007-12-01 06:16:30 +01:00
case Result of
default - > { result , Reply } ;
_ - > { result , Result }
2003-07-08 22:11:27 +02:00
end ;
2009-04-30 07:18:06 +02:00
{ result , { _ , default } } - >
2007-12-01 06:16:30 +01:00
{ result , Reply } ;
2009-04-30 07:18:06 +02:00
{ result , { _ , Result } } - >
{ result , Result } ;
Error - >
Error
2003-07-08 22:11:27 +02:00
end .
2007-12-01 06:16:30 +01:00
%% @spec (Host, JID, Node) ->
%% {error, Reason} | {result, []}
%% Host = host()
%% Node = pubsubNode()
%% JID = jid()
%% Reason = stanzaError()
%% @doc <p>Delete all items of specified node owned by JID.</p>
%%<p>There are several reasons why the node purge request might fail:</p>
%%<ul>
2008-04-01 12:11:39 +02:00
%%<li>The node or service does not support node purging.</li>
%%<li>The requesting entity does not have sufficient privileges to purge the node.</li>
%%<li>The node is not configured to persist items.</li>
%%<li>The specified node does not exist.</li>
2007-12-01 06:16:30 +01:00
%%</ul>
purge_node ( Host , Node , Owner ) - >
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { options = Options , type = Type , id = NodeId } ) - >
2007-12-01 06:16:30 +01:00
Features = features ( Type ) ,
PurgeFeature = lists : member ( " purge-nodes " , Features ) ,
PersistentFeature = lists : member ( " persistent-items " , Features ) ,
PersistentConfig = get_option ( Options , persist_items ) ,
if
not PurgeFeature - >
%% Service does not support node purging
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " purge-nodes " ) } ;
not PersistentFeature - >
%% Node does not support persistent items
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " persistent-items " ) } ;
not PersistentConfig - >
%% Node is not configured for persistent items
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " persistent-items " ) } ;
true - >
2009-04-30 07:18:06 +02:00
node_call ( Type , purge_node , [ NodeId , Owner ] )
2007-12-01 06:16:30 +01:00
end
end ,
Reply = [ ] ,
case transaction ( Host , Node , Action , sync_dirty ) of
2009-04-30 07:18:06 +02:00
{ result , { TNode , { Result , broadcast } } } - >
NodeId = TNode #pubsub_node.id ,
Type = TNode #pubsub_node.type ,
Options = TNode #pubsub_node.options ,
broadcast_purge_node ( Host , Node , NodeId , Type , Options ) ,
2009-05-29 00:30:43 +02:00
unset_cached_item ( Host , NodeId ) ,
2007-12-01 06:16:30 +01:00
case Result of
default - > { result , Reply } ;
_ - > { result , Result }
end ;
2009-04-30 07:18:06 +02:00
{ result , { _ , default } } - >
2007-12-01 06:16:30 +01:00
{ result , Reply } ;
2009-04-30 07:18:06 +02:00
{ result , { _ , Result } } - >
{ result , Result } ;
Error - >
Error
2007-12-01 06:16:30 +01:00
end .
2003-07-08 22:11:27 +02:00
2007-12-01 06:16:30 +01:00
%% @doc <p>Return the items of a given node.</p>
%% <p>The number of items to return is limited by MaxItems.</p>
%% <p>The permission are not checked in this function.</p>
%% @todo We probably need to check that the user doing the query has the right
%% to read the items.
2008-07-03 11:56:31 +02:00
get_items ( Host , Node , From , SubId , SMaxItems , ItemIDs ) - >
2003-07-08 22:11:27 +02:00
MaxItems =
if
2009-09-23 17:56:06 +02:00
SMaxItems == " " - > get_max_items_node ( Host ) ;
2003-07-08 22:11:27 +02:00
true - >
case catch list_to_integer ( SMaxItems ) of
2007-12-01 06:16:30 +01:00
{ 'EXIT' , _ } - > { error , ? ERR_BAD_REQUEST } ;
Val - > Val
2003-07-08 22:11:27 +02:00
end
end ,
case MaxItems of
2007-12-01 06:16:30 +01:00
{ error , Error } - >
{ error , Error } ;
2003-07-08 22:11:27 +02:00
_ - >
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { options = Options , type = Type , id = NodeId } ) - >
2008-07-03 11:56:31 +02:00
Features = features ( Type ) ,
RetreiveFeature = lists : member ( " retrieve-items " , Features ) ,
PersistentFeature = lists : member ( " persistent-items " , Features ) ,
AccessModel = get_option ( Options , access_model ) ,
2008-10-17 00:12:05 +02:00
AllowedGroups = get_option ( Options , roster_groups_allowed , [ ] ) ,
2008-07-03 11:56:31 +02:00
{ PresenceSubscription , RosterGroup } =
case Host of
{ OUser , OServer , _ } - >
get_roster_info ( OUser , OServer ,
jlib : jid_tolower ( From ) , AllowedGroups ) ;
_ - >
{ true , true }
end ,
if
not RetreiveFeature - >
%% Item Retrieval Not Supported
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " retrieve-items " ) } ;
not PersistentFeature - >
%% Persistent Items Not Supported
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " persistent-items " ) } ;
true - >
node_call ( Type , get_items ,
2009-04-30 07:18:06 +02:00
[ NodeId , From ,
2008-07-03 11:56:31 +02:00
AccessModel , PresenceSubscription , RosterGroup ,
SubId ] )
end
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
2009-04-30 07:18:06 +02:00
{ result , { _ , Items } } - >
2008-07-03 11:56:31 +02:00
SendItems = case ItemIDs of
[ ] - >
Items ;
_ - >
2008-08-26 00:54:48 +02:00
lists : filter ( fun ( #pubsub_item { itemid = { ItemId , _ } } ) - >
lists : member ( ItemId , ItemIDs )
2008-07-03 11:56:31 +02:00
end , Items )
end ,
2007-12-01 06:16:30 +01:00
%% Generate the XML response (Item list), limiting the
%% number of items sent to MaxItems:
2008-02-11 16:21:24 +01:00
{ result , [ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB } ] ,
2009-05-08 03:18:02 +02:00
[ { xmlelement , " items " , nodeAttr ( Node ) ,
2009-04-30 07:18:06 +02:00
itemsEls ( lists : sublist ( SendItems , MaxItems ) ) } ] } ] } ;
Error - >
Error
2003-07-08 22:11:27 +02:00
end
end .
2009-05-29 00:30:43 +02:00
get_items ( Host , Node ) - >
Action = fun ( #pubsub_node { type = Type , id = NodeId } ) - >
node_call ( Type , get_items , [ NodeId , service_jid ( Host ) ] )
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ , Items } } - > Items ;
Error - > Error
end .
get_item ( Host , Node , ItemId ) - >
Action = fun ( #pubsub_node { type = Type , id = NodeId } ) - >
node_call ( Type , get_item , [ NodeId , ItemId ] )
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ , Items } } - > Items ;
Error - > Error
end .
2003-07-08 22:11:27 +02:00
2009-08-17 19:16:43 +02:00
%% @spec (Host, Node, NodeId, Type, LJID, Number) -> any()
2009-04-30 07:18:06 +02:00
%% Host = pubsubHost()
2009-05-07 02:09:02 +02:00
%% Node = pubsubNode()
2009-04-30 07:18:06 +02:00
%% NodeId = pubsubNodeId()
%% Type = pubsubNodeType()
2008-12-20 01:00:24 +01:00
%% LJID = {U, S, []}
2009-01-03 22:29:54 +01:00
%% Number = last | integer()
2008-12-20 01:00:24 +01:00
%% @doc <p>Resend the items of a node to the user.</p>
%% @todo use cache-last-item feature
2009-05-27 00:50:11 +02:00
send_items ( Host , Node , NodeId , Type , LJID , last ) - >
2009-05-29 00:30:43 +02:00
case get_cached_item ( Host , NodeId ) of
undefined - >
send_items ( Host , Node , NodeId , Type , LJID , 1 ) ;
LastItem - >
2009-09-23 11:27:12 +02:00
{ ModifNow , ModifLjid } = LastItem #pubsub_item.modification ,
Stanza = event_stanza_with_delay (
2009-05-29 00:30:43 +02:00
[ { xmlelement , " items " , nodeAttr ( Node ) ,
2009-09-23 11:27:12 +02:00
itemsEls ( [ LastItem ] ) } ] , ModifNow , ModifLjid ) ,
2009-12-04 17:31:25 +01:00
ejabberd_router : route ( service_jid ( Host ) , jlib : make_jid ( LJID ) , Stanza )
2009-05-29 00:30:43 +02:00
end ;
2009-04-30 07:18:06 +02:00
send_items ( Host , Node , NodeId , Type , LJID , Number ) - >
ToSend = case node_action ( Host , Type , get_items , [ NodeId , LJID ] ) of
{ result , [ ] } - >
2008-07-23 03:14:02 +02:00
[ ] ;
2009-04-30 07:18:06 +02:00
{ result , Items } - >
2008-07-23 03:14:02 +02:00
case Number of
2009-05-27 00:50:11 +02:00
N when N > 0 - > lists : sublist ( Items , N ) ;
_ - > Items
2009-04-30 07:18:06 +02:00
end ;
_ - >
[ ]
2008-07-23 03:14:02 +02:00
end ,
2009-09-23 11:27:12 +02:00
Stanza = case ToSend of
[ LastItem ] - >
{ ModifNow , ModifLjid } = LastItem #pubsub_item.modification ,
event_stanza_with_delay (
[ { xmlelement , " items " , nodeAttr ( Node ) ,
itemsEls ( ToSend ) } ] , ModifNow , ModifLjid ) ;
_ - >
event_stanza (
[ { xmlelement , " items " , nodeAttr ( Node ) ,
itemsEls ( ToSend ) } ] )
end ,
2009-12-04 17:31:25 +01:00
ejabberd_router : route ( service_jid ( Host ) , jlib : make_jid ( LJID ) , Stanza ) .
2007-12-01 06:16:30 +01:00
%% @spec (Host, JID, Plugins) -> {error, Reason} | {result, Response}
%% Host = host()
%% JID = jid()
%% Plugins = [Plugin::string()]
%% Reason = stanzaError()
%% Response = [pubsubIQResponse()]
%% @doc <p>Return the list of affiliations as an XMPP response.</p>
get_affiliations ( Host , JID , Plugins ) when is_list ( Plugins ) - >
Result = lists : foldl (
fun ( Type , { Status , Acc } ) - >
Features = features ( Type ) ,
RetrieveFeature = lists : member ( " retrieve-affiliations " , Features ) ,
if
not RetrieveFeature - >
%% Service does not support retreive affiliatons
{ { error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " retrieve-affiliations " ) } , Acc } ;
true - >
2009-04-30 07:18:06 +02:00
{ result , Affiliations } = node_action ( Host , Type , get_entity_affiliations , [ Host , JID ] ) ,
2007-12-01 06:16:30 +01:00
{ Status , [ Affiliations | Acc ] }
end
end , { ok , [ ] } , Plugins ) ,
case Result of
{ ok , Affiliations } - >
Entities = lists : flatmap (
fun ( { _ , none } ) - > [ ] ;
2009-05-19 23:59:15 +02:00
( { #pubsub_node { nodeid = { _ , Node } } , Affiliation } ) - >
2007-12-01 06:16:30 +01:00
[ { xmlelement , " affiliation " ,
2009-05-08 03:18:02 +02:00
[ { " affiliation " , affiliation_to_string ( Affiliation ) } | nodeAttr ( Node ) ] ,
2007-12-01 06:16:30 +01:00
[ ] } ]
2007-12-11 17:19:17 +01:00
end , lists : usort ( lists : flatten ( Affiliations ) ) ) ,
2007-12-01 06:16:30 +01:00
{ result , [ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB } ] ,
[ { xmlelement , " affiliations " , [ ] ,
Entities } ] } ] } ;
{ Error , _ } - >
Error
end ;
get_affiliations ( Host , Node , JID ) - >
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { type = Type , id = NodeId } ) - >
2007-12-01 06:16:30 +01:00
Features = features ( Type ) ,
RetrieveFeature = lists : member ( " modify-affiliations " , Features ) ,
2009-05-08 01:35:59 +02:00
{ result , Affiliation } = node_call ( Type , get_affiliation , [ NodeId , JID ] ) ,
2007-12-01 06:16:30 +01:00
if
not RetrieveFeature - >
%% Service does not support modify affiliations
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " modify-affiliations " ) } ;
2009-05-08 01:35:59 +02:00
Affiliation / = owner - >
2007-12-01 06:16:30 +01:00
%% Entity is not an owner
{ error , ? ERR_FORBIDDEN } ;
true - >
2009-04-30 07:18:06 +02:00
node_call ( Type , get_node_affiliations , [ NodeId ] )
2007-12-01 06:16:30 +01:00
end
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
2009-04-30 07:18:06 +02:00
{ result , { _ , [ ] } } - >
2007-12-01 06:16:30 +01:00
{ error , ? ERR_ITEM_NOT_FOUND } ;
2009-04-30 07:18:06 +02:00
{ result , { _ , Affiliations } } - >
2007-12-01 06:16:30 +01:00
Entities = lists : flatmap (
fun ( { _ , none } ) - > [ ] ;
( { AJID , Affiliation } ) - >
[ { xmlelement , " affiliation " ,
[ { " jid " , jlib : jid_to_string ( AJID ) } ,
{ " affiliation " , affiliation_to_string ( Affiliation ) } ] ,
[ ] } ]
end , Affiliations ) ,
{ result , [ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB_OWNER } ] ,
2009-05-08 03:18:02 +02:00
[ { xmlelement , " affiliations " , nodeAttr ( Node ) ,
2009-04-30 07:18:06 +02:00
Entities } ] } ] } ;
Error - >
Error
2003-07-08 22:11:27 +02:00
end .
2007-12-01 06:16:30 +01:00
set_affiliations ( Host , Node , From , EntitiesEls ) - >
Owner = jlib : jid_tolower ( jlib : jid_remove_resource ( From ) ) ,
2003-07-08 22:11:27 +02:00
Entities =
lists : foldl (
fun ( El , Acc ) - >
case Acc of
error - >
error ;
_ - >
case El of
2007-12-01 06:16:30 +01:00
{ xmlelement , " affiliation " , Attrs , _ } - >
2003-07-08 22:11:27 +02:00
JID = jlib : string_to_jid (
xml : get_attr_s ( " jid " , Attrs ) ) ,
2007-12-01 06:16:30 +01:00
Affiliation = string_to_affiliation (
xml : get_attr_s ( " affiliation " , Attrs ) ) ,
2003-07-08 22:11:27 +02:00
if
( JID == error ) or
2007-12-01 06:16:30 +01:00
( Affiliation == false ) - >
2003-07-08 22:11:27 +02:00
error ;
true - >
2007-12-01 06:16:30 +01:00
[ { jlib : jid_tolower ( JID ) , Affiliation } | Acc ]
2003-07-08 22:11:27 +02:00
end
end
end
end , [ ] , EntitiesEls ) ,
case Entities of
error - >
{ error , ? ERR_BAD_REQUEST } ;
_ - >
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { owners = Owners , type = Type , id = NodeId } = N ) - >
2009-01-11 02:41:06 +01:00
case lists : member ( Owner , Owners ) of
true - >
lists : foreach (
fun ( { JID , Affiliation } ) - >
2009-04-30 07:18:06 +02:00
node_call ( Type , set_affiliation , [ NodeId , JID , Affiliation ] ) ,
2009-01-11 02:41:06 +01:00
case Affiliation of
owner - >
NewOwner = jlib : jid_tolower ( jlib : jid_remove_resource ( JID ) ) ,
NewOwners = [ NewOwner | Owners ] ,
tree_call ( Host , set_node , [ N #pubsub_node { owners = NewOwners } ] ) ;
none - >
OldOwner = jlib : jid_tolower ( jlib : jid_remove_resource ( JID ) ) ,
case lists : member ( OldOwner , Owners ) of
true - >
NewOwners = Owners -- [ OldOwner ] ,
tree_call ( Host , set_node , [ N #pubsub_node { owners = NewOwners } ] ) ;
_ - >
ok
end ;
_ - >
ok
end
end , Entities ) ,
{ result , [ ] } ;
_ - >
{ error , ? ERR_FORBIDDEN }
end
2007-12-01 06:16:30 +01:00
end ,
2009-04-30 07:18:06 +02:00
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ , Result } } - > { result , Result } ;
Other - > Other
end
2003-07-08 22:11:27 +02:00
end .
2009-05-29 04:14:07 +02:00
get_options ( Host , Node , JID , SubID , Lang ) - >
Action = fun ( #pubsub_node { type = Type , id = NodeID } ) - >
case lists : member ( " subscription-options " , features ( Type ) ) of
true - >
2009-09-01 15:16:10 +02:00
get_options_helper ( JID , Lang , Node , NodeID , SubID , Type ) ;
2009-05-29 04:14:07 +02:00
false - >
{ error , extended_error (
? ERR_FEATURE_NOT_IMPLEMENTED ,
unsupported , " subscription-options " ) }
end
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ Node , XForm } } - > { result , [ XForm ] } ;
Error - > Error
end .
2009-09-01 15:16:10 +02:00
get_options_helper ( JID , Lang , Node , NodeID , SubID , Type ) - >
2009-05-29 04:14:07 +02:00
Subscriber = case jlib : string_to_jid ( JID ) of
error - > { " " , " " , " " } ;
J - > jlib : jid_tolower ( J )
end ,
{ result , Subs } = node_call ( Type , get_subscriptions ,
[ NodeID , Subscriber ] ) ,
SubIDs = lists : foldl ( fun ( { subscribed , SID } , Acc ) - >
[ SID | Acc ] ;
( _ , Acc ) - >
Acc
end , [ ] , Subs ) ,
case { SubID , SubIDs } of
{ _ , [ ] } - >
2009-10-12 11:59:36 +02:00
{ error , extended_error ( ? ERR_NOT_ACCEPTABLE , " not-subscribed " ) } ;
2009-05-29 04:14:07 +02:00
{ [ ] , [ SID ] } - >
2009-09-01 15:16:10 +02:00
read_sub ( Subscriber , Node , NodeID , SID , Lang ) ;
2009-05-29 04:14:07 +02:00
{ [ ] , _ } - >
2009-10-12 11:59:36 +02:00
{ error , extended_error ( ? ERR_NOT_ACCEPTABLE , " subid-required " ) } ;
2009-05-29 04:14:07 +02:00
{ _ , _ } - >
2009-09-01 15:16:10 +02:00
read_sub ( Subscriber , Node , NodeID , SubID , Lang )
2009-05-29 04:14:07 +02:00
end .
2009-09-01 15:16:10 +02:00
read_sub ( Subscriber , Node , NodeID , SubID , Lang ) - >
2009-05-29 04:14:07 +02:00
case pubsub_subscription : get_subscription ( Subscriber , NodeID , SubID ) of
{ error , notfound } - >
2009-10-12 11:59:36 +02:00
{ error , extended_error ( ? ERR_NOT_ACCEPTABLE , " invalid-subid " ) } ;
2009-05-29 04:14:07 +02:00
{ result , #pubsub_subscription { options = Options } } - >
2009-09-01 15:16:10 +02:00
{ result , XdataEl } = pubsub_subscription : get_options_xform ( Lang , Options ) ,
2009-10-20 17:03:07 +02:00
OptionsEl = { xmlelement , " options " , [ { " jid " , jlib : jid_to_string ( Subscriber ) } ,
{ " subid " , SubID } | nodeAttr ( Node ) ] ,
2009-09-01 10:18:30 +02:00
[ XdataEl ] } ,
PubsubEl = { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB } ] , [ OptionsEl ] } ,
{ result , PubsubEl }
2009-05-29 04:14:07 +02:00
end .
set_options ( Host , Node , JID , SubID , Configuration ) - >
Action = fun ( #pubsub_node { type = Type , id = NodeID } ) - >
case lists : member ( " subscription-options " , features ( Type ) ) of
true - >
set_options_helper ( Configuration , JID , NodeID ,
SubID , Type ) ;
false - >
{ error , extended_error (
? ERR_FEATURE_NOT_IMPLEMENTED ,
unsupported , " subscription-options " ) }
end
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ Node , Result } } - > { result , Result } ;
Error - > Error
end .
set_options_helper ( Configuration , JID , NodeID , SubID , Type ) - >
2009-10-12 11:59:36 +02:00
SubOpts = case pubsub_subscription : parse_options_xform ( Configuration ) of
{ result , GoodSubOpts } - > GoodSubOpts ;
_ - > invalid
end ,
2009-05-29 04:14:07 +02:00
Subscriber = case jlib : string_to_jid ( JID ) of
error - > { " " , " " , " " } ;
J - > jlib : jid_tolower ( J )
end ,
{ result , Subs } = node_call ( Type , get_subscriptions ,
[ NodeID , Subscriber ] ) ,
SubIDs = lists : foldl ( fun ( { subscribed , SID } , Acc ) - >
[ SID | Acc ] ;
( _ , Acc ) - >
Acc
end , [ ] , Subs ) ,
case { SubID , SubIDs } of
{ _ , [ ] } - >
2009-10-12 11:59:36 +02:00
{ error , extended_error ( ? ERR_NOT_ACCEPTABLE , " not-subscribed " ) } ;
2009-05-29 04:14:07 +02:00
{ [ ] , [ SID ] } - >
write_sub ( Subscriber , NodeID , SID , SubOpts ) ;
{ [ ] , _ } - >
2009-10-12 11:59:36 +02:00
{ error , extended_error ( ? ERR_NOT_ACCEPTABLE , " subid-required " ) } ;
2009-05-29 04:14:07 +02:00
{ _ , _ } - >
write_sub ( Subscriber , NodeID , SubID , SubOpts )
end .
2009-10-12 11:59:36 +02:00
write_sub ( _ Subscriber , _ NodeID , _ SubID , invalid ) - >
{ error , extended_error ( ? ERR_BAD_REQUEST , " invalid-options " ) } ;
2009-05-29 04:14:07 +02:00
write_sub ( Subscriber , NodeID , SubID , Options ) - >
2009-08-28 00:14:48 +02:00
case pubsub_subscription : set_subscription ( Subscriber , NodeID , SubID , Options ) of
2009-05-29 04:14:07 +02:00
{ error , notfound } - >
2009-10-12 11:59:36 +02:00
{ error , extended_error ( ? ERR_NOT_ACCEPTABLE , " invalid-subid " ) } ;
2009-05-29 04:14:07 +02:00
{ result , _ } - >
{ result , [ ] }
end .
2003-07-08 22:11:27 +02:00
2009-05-08 01:35:59 +02:00
%% @spec (Host, Node, JID, Plugins) -> {error, Reason} | {result, Response}
2007-12-01 06:16:30 +01:00
%% Host = host()
2009-05-08 01:35:59 +02:00
%% Node = pubsubNode()
2007-12-01 06:16:30 +01:00
%% JID = jid()
%% Plugins = [Plugin::string()]
%% Reason = stanzaError()
%% Response = [pubsubIQResponse()]
%% @doc <p>Return the list of subscriptions as an XMPP response.</p>
2009-05-08 01:35:59 +02:00
get_subscriptions ( Host , Node , JID , Plugins ) when is_list ( Plugins ) - >
2007-12-01 06:16:30 +01:00
Result = lists : foldl (
fun ( Type , { Status , Acc } ) - >
Features = features ( Type ) ,
RetrieveFeature = lists : member ( " retrieve-subscriptions " , Features ) ,
if
not RetrieveFeature - >
%% Service does not support retreive subscriptions
{ { error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " retrieve-subscriptions " ) } , Acc } ;
true - >
2009-01-08 15:06:35 +01:00
Subscriber = jlib : jid_remove_resource ( JID ) ,
2009-04-30 07:18:06 +02:00
{ result , Subscriptions } = node_action ( Host , Type , get_entity_subscriptions , [ Host , Subscriber ] ) ,
2007-12-01 06:16:30 +01:00
{ Status , [ Subscriptions | Acc ] }
end
end , { ok , [ ] } , Plugins ) ,
case Result of
{ ok , Subscriptions } - >
Entities = lists : flatmap (
2009-05-08 01:35:59 +02:00
fun ( { _ , none } ) - >
[ ] ;
2009-05-19 23:59:15 +02:00
( { #pubsub_node { nodeid = { _ , SubsNode } } , Subscription } ) - >
2009-05-08 01:35:59 +02:00
case Node of
2009-10-20 17:03:07 +02:00
< < > > - >
2007-12-01 06:16:30 +01:00
[ { xmlelement , " subscription " ,
2009-05-08 03:18:02 +02:00
[ { " subscription " , subscription_to_string ( Subscription ) } | nodeAttr ( SubsNode ) ] ,
2007-12-01 06:16:30 +01:00
[ ] } ] ;
2009-05-08 01:35:59 +02:00
SubsNode - >
2007-12-01 06:16:30 +01:00
[ { xmlelement , " subscription " ,
2009-05-08 01:35:59 +02:00
[ { " subscription " , subscription_to_string ( Subscription ) } ] ,
[ ] } ] ;
_ - >
[ ]
end ;
( { _ , none , _ } ) - >
[ ] ;
2009-07-20 10:53:56 +02:00
( { #pubsub_node { nodeid = { _ , SubsNode } } , Subscription , SubID , SubJID } ) - >
2009-05-29 04:14:07 +02:00
case Node of
2009-10-20 17:03:07 +02:00
< < > > - >
2009-05-29 04:14:07 +02:00
[ { xmlelement , " subscription " ,
[ { " jid " , jlib : jid_to_string ( SubJID ) } ,
{ " subid " , SubID } ,
2009-07-20 10:53:56 +02:00
{ " subscription " , subscription_to_string ( Subscription ) } | nodeAttr ( SubsNode ) ] ,
2009-05-29 04:14:07 +02:00
[ ] } ] ;
SubsNode - >
[ { xmlelement , " subscription " ,
[ { " jid " , jlib : jid_to_string ( SubJID ) } ,
{ " subid " , SubID } ,
2009-07-20 10:53:56 +02:00
{ " subscription " , subscription_to_string ( Subscription ) } ] ,
2009-05-29 04:14:07 +02:00
[ ] } ] ;
_ - >
[ ]
end ;
2009-05-19 23:59:15 +02:00
( { #pubsub_node { nodeid = { _ , SubsNode } } , Subscription , SubJID } ) - >
2009-05-08 01:35:59 +02:00
case Node of
2009-10-20 17:03:07 +02:00
< < > > - >
2009-05-08 01:35:59 +02:00
[ { xmlelement , " subscription " ,
2009-05-08 03:18:02 +02:00
[ { " jid " , jlib : jid_to_string ( SubJID ) } ,
{ " subscription " , subscription_to_string ( Subscription ) } | nodeAttr ( SubsNode ) ] ,
2009-05-08 01:35:59 +02:00
[ ] } ] ;
SubsNode - >
[ { xmlelement , " subscription " ,
[ { " jid " , jlib : jid_to_string ( SubJID ) } ,
{ " subscription " , subscription_to_string ( Subscription ) } ] ,
[ ] } ] ;
_ - >
[ ]
end
2007-12-11 17:19:17 +01:00
end , lists : usort ( lists : flatten ( Subscriptions ) ) ) ,
2007-12-01 06:16:30 +01:00
{ result , [ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB } ] ,
[ { xmlelement , " subscriptions " , [ ] ,
Entities } ] } ] } ;
{ Error , _ } - >
Error
2009-05-08 01:35:59 +02:00
end .
2007-12-01 06:16:30 +01:00
get_subscriptions ( Host , Node , JID ) - >
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { type = Type , id = NodeId } ) - >
2007-12-01 06:16:30 +01:00
Features = features ( Type ) ,
RetrieveFeature = lists : member ( " manage-subscriptions " , Features ) ,
2009-05-08 01:35:59 +02:00
{ result , Affiliation } = node_call ( Type , get_affiliation , [ NodeId , JID ] ) ,
2007-12-01 06:16:30 +01:00
if
not RetrieveFeature - >
%% Service does not support manage subscriptions
2009-02-09 14:37:58 +01:00
{ error , extended_error ( ? ERR_FEATURE_NOT_IMPLEMENTED , unsupported , " manage-subscriptions " ) } ;
2009-05-08 01:35:59 +02:00
Affiliation / = owner - >
2008-07-03 11:56:31 +02:00
%% Entity is not an owner
2007-12-01 06:16:30 +01:00
{ error , ? ERR_FORBIDDEN } ;
true - >
2009-04-30 07:18:06 +02:00
node_call ( Type , get_node_subscriptions , [ NodeId ] )
2007-12-01 06:16:30 +01:00
end
end ,
case transaction ( Host , Node , Action , sync_dirty ) of
2009-04-30 07:18:06 +02:00
{ result , { _ , Subscriptions } } - >
2007-12-01 06:16:30 +01:00
Entities = lists : flatmap (
fun ( { _ , none } ) - > [ ] ;
2009-07-31 16:55:05 +02:00
( { _ , pending , _ } ) - > [ ] ;
2007-12-01 06:16:30 +01:00
( { AJID , Subscription } ) - >
[ { xmlelement , " subscription " ,
[ { " jid " , jlib : jid_to_string ( AJID ) } ,
{ " subscription " , subscription_to_string ( Subscription ) } ] ,
[ ] } ] ;
( { AJID , Subscription , SubId } ) - >
[ { xmlelement , " subscription " ,
[ { " jid " , jlib : jid_to_string ( AJID ) } ,
{ " subscription " , subscription_to_string ( Subscription ) } ,
{ " subid " , SubId } ] ,
[ ] } ]
end , Subscriptions ) ,
{ result , [ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB_OWNER } ] ,
2009-05-08 03:18:02 +02:00
[ { xmlelement , " subscriptions " , nodeAttr ( Node ) ,
2009-04-30 07:18:06 +02:00
Entities } ] } ] } ;
Error - >
Error
2005-07-20 05:09:34 +02:00
end .
2003-07-08 22:11:27 +02:00
2007-12-01 06:16:30 +01:00
set_subscriptions ( Host , Node , From , EntitiesEls ) - >
Owner = jlib : jid_tolower ( jlib : jid_remove_resource ( From ) ) ,
Entities =
2003-07-08 22:11:27 +02:00
lists : foldl (
2007-12-01 06:16:30 +01:00
fun ( El , Acc ) - >
case Acc of
error - >
error ;
2003-07-08 22:11:27 +02:00
_ - >
2007-12-01 06:16:30 +01:00
case El of
2008-01-12 20:47:19 +01:00
{ xmlelement , " subscription " , Attrs , _ } - >
2007-12-01 06:16:30 +01:00
JID = jlib : string_to_jid (
xml : get_attr_s ( " jid " , Attrs ) ) ,
Subscription = string_to_subscription (
xml : get_attr_s ( " subscription " , Attrs ) ) ,
2009-07-31 16:55:05 +02:00
SubId = xml : get_attr_s ( " subid " , Attrs ) ,
2007-12-01 06:16:30 +01:00
if
( JID == error ) or
( Subscription == false ) - >
error ;
true - >
2009-07-17 11:29:11 +02:00
[ { jlib : jid_tolower ( JID ) , Subscription , SubId } | Acc ]
2007-12-01 06:16:30 +01:00
end
end
2003-07-08 22:11:27 +02:00
end
2007-12-01 06:16:30 +01:00
end , [ ] , EntitiesEls ) ,
case Entities of
error - >
{ error , ? ERR_BAD_REQUEST } ;
2003-07-08 22:11:27 +02:00
_ - >
2009-08-25 18:35:43 +02:00
Notify = fun ( JID , Sub , _ SubId ) - >
Stanza = { xmlelement , " message " , [ ] ,
[ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB } ] ,
[ { xmlelement , " subscription " ,
[ { " jid " , jlib : jid_to_string ( JID ) } ,
%{"subid", SubId},
{ " subscription " , subscription_to_string ( Sub ) } | nodeAttr ( Node ) ] , [ ] } ] } ] } ,
2009-12-04 17:31:25 +01:00
ejabberd_router : route ( service_jid ( Host ) , jlib : make_jid ( JID ) , Stanza )
2009-08-25 18:35:43 +02:00
end ,
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { owners = Owners , type = Type , id = NodeId } ) - >
2009-08-25 00:40:40 +02:00
case lists : member ( Owner , Owners ) of
true - >
Result = lists : foldl ( fun ( { JID , Subscription , SubId } , Acc ) - >
case node_call ( Type , set_subscriptions , [ NodeId , JID , Subscription , SubId ] ) of
{ error , Err } - > [ { error , Err } | Acc ] ;
2009-08-25 18:35:43 +02:00
_ - > Notify ( JID , Subscription , SubId ) , Acc
2009-08-25 00:40:40 +02:00
end
end , [ ] , Entities ) ,
case Result of
[ ] - > { result , [ ] } ;
_ - > { error , ? ERR_NOT_ACCEPTABLE }
end ;
_ - >
{ error , ? ERR_FORBIDDEN }
end
end ,
2009-04-30 07:18:06 +02:00
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ , Result } } - > { result , Result } ;
Other - > Other
end
2003-07-08 22:11:27 +02:00
end .
2008-04-01 12:11:39 +02:00
%% @spec (OwnerUser, OwnerServer, {SubscriberUser, SubscriberServer, SubscriberResource}, AllowedGroups)
%% -> {PresenceSubscription, RosterGroup}
2007-12-01 06:16:30 +01:00
get_roster_info ( OwnerUser , OwnerServer , { SubscriberUser , SubscriberServer , _ } , AllowedGroups ) - >
{ Subscription , Groups } =
ejabberd_hooks : run_fold (
roster_get_jid_info , OwnerServer ,
{ none , [ ] } ,
[ OwnerUser , OwnerServer , { SubscriberUser , SubscriberServer , " " } ] ) ,
2008-07-24 16:07:05 +02:00
PresenceSubscription = ( Subscription == both ) orelse ( Subscription == from )
orelse ( { OwnerUser , OwnerServer } == { SubscriberUser , SubscriberServer } ) ,
2007-12-01 06:16:30 +01:00
RosterGroup = lists : any ( fun ( Group ) - >
lists : member ( Group , AllowedGroups )
end , Groups ) ,
{ PresenceSubscription , RosterGroup } .
%% @spec (AffiliationStr) -> Affiliation
%% AffiliationStr = string()
%% Affiliation = atom()
%% @doc <p>Convert an affiliation type from string to atom.</p>
string_to_affiliation ( " owner " ) - > owner ;
string_to_affiliation ( " publisher " ) - > publisher ;
2009-01-03 01:55:05 +01:00
string_to_affiliation ( " member " ) - > member ;
2007-12-01 06:16:30 +01:00
string_to_affiliation ( " outcast " ) - > outcast ;
string_to_affiliation ( " none " ) - > none ;
string_to_affiliation ( _ ) - > false .
%% @spec (SubscriptionStr) -> Subscription
%% SubscriptionStr = string()
%% Subscription = atom()
%% @doc <p>Convert a subscription type from string to atom.</p>
string_to_subscription ( " subscribed " ) - > subscribed ;
string_to_subscription ( " pending " ) - > pending ;
string_to_subscription ( " unconfigured " ) - > unconfigured ;
string_to_subscription ( " none " ) - > none ;
string_to_subscription ( _ ) - > false .
%% @spec (Affiliation) -> AffiliationStr
%% Affiliation = atom()
%% AffiliationStr = string()
%% @doc <p>Convert an affiliation type from atom to string.</p>
affiliation_to_string ( owner ) - > " owner " ;
affiliation_to_string ( publisher ) - > " publisher " ;
2009-01-03 01:55:05 +01:00
affiliation_to_string ( member ) - > " member " ;
2007-12-01 06:16:30 +01:00
affiliation_to_string ( outcast ) - > " outcast " ;
affiliation_to_string ( _ ) - > " none " .
%% @spec (Subscription) -> SubscriptionStr
%% Subscription = atom()
%% SubscriptionStr = string()
%% @doc <p>Convert a subscription type from atom to string.</p>
subscription_to_string ( subscribed ) - > " subscribed " ;
subscription_to_string ( pending ) - > " pending " ;
subscription_to_string ( unconfigured ) - > " unconfigured " ;
subscription_to_string ( _ ) - > " none " .
%% @spec (Node) -> NodeStr
%% Node = pubsubNode()
%% NodeStr = string()
%% @doc <p>Convert a node type from pubsubNode to string.</p>
2009-10-20 17:03:07 +02:00
node_to_string ( Node ) - > binary_to_list ( Node ) .
string_to_node ( SNode ) - > list_to_binary ( SNode ) .
2007-12-01 06:16:30 +01:00
2007-12-22 01:04:05 +01:00
%% @spec (Host) -> jid()
2007-12-11 17:19:17 +01:00
%% Host = host()
2007-12-22 01:04:05 +01:00
%% @doc <p>Generate pubsub service JID.</p>
service_jid ( Host ) - >
case Host of
{ U , S , _ } - > { jid , U , S , " " , U , S , " " } ;
_ - > { jid , " " , Host , " " , " " , Host , " " }
end .
2009-08-17 19:16:43 +02:00
%% @spec (LJID, NotifyType, Depth, NodeOptions, SubOptions) -> boolean()
2007-12-22 01:04:05 +01:00
%% LJID = jid()
2009-06-15 15:45:40 +02:00
%% NotifyType = items | nodes
%% Depth = integer()
2009-05-29 04:14:07 +02:00
%% NodeOptions = [{atom(), term()}]
%% SubOptions = [{atom(), term()}]
%% @doc <p>Check if a notification must be delivered or not based on
%% node and subscription options.</p>
2009-06-15 15:45:40 +02:00
is_to_deliver ( LJID , NotifyType , Depth , NodeOptions , SubOptions ) - >
sub_to_deliver ( LJID , NotifyType , Depth , SubOptions )
andalso node_to_deliver ( LJID , NodeOptions ) .
sub_to_deliver ( _ LJID , NotifyType , Depth , SubOptions ) - >
lists : all ( fun ( Option ) - >
sub_option_can_deliver ( NotifyType , Depth , Option )
end , SubOptions ) .
sub_option_can_deliver ( items , _ , { subscription_type , nodes } ) - > false ;
sub_option_can_deliver ( nodes , _ , { subscription_type , items } ) - > false ;
sub_option_can_deliver ( _ , _ , { subscription_depth , all } ) - > true ;
2009-06-29 23:46:13 +02:00
sub_option_can_deliver ( _ , Depth , { subscription_depth , D } ) - > Depth =< D ;
2009-06-15 15:45:40 +02:00
sub_option_can_deliver ( _ , _ , { deliver , false } ) - > false ;
sub_option_can_deliver ( _ , _ , { expire , When } ) - > now ( ) < When ;
sub_option_can_deliver ( _ , _ , _ ) - > true .
2009-05-29 04:14:07 +02:00
node_to_deliver ( LJID , NodeOptions ) - >
PresenceDelivery = get_option ( NodeOptions , presence_based_delivery ) ,
presence_can_deliver ( LJID , PresenceDelivery ) .
presence_can_deliver ( _ , false ) - > true ;
2010-05-05 15:42:49 +02:00
presence_can_deliver ( { User , Server , Resource } , true ) - >
2007-12-22 01:04:05 +01:00
case mnesia : dirty_match_object ( { session , '_' , '_' , { User , Server } , '_' , '_' } ) of
[ ] - > false ;
Ss - >
2010-05-05 15:42:49 +02:00
lists : foldl ( fun ( _ , true ) - > true ;
( { session , _ , _ , _ , undefined , _ } , _ Acc ) - > false ;
( { session , _ , { _ , _ , R } , _ , _ Priority , _ } , _ Acc ) - >
case Resource of
[ ] - > true ;
R - > true ;
_ - > false
end
2007-12-22 01:04:05 +01:00
end , false , Ss )
2007-12-11 17:19:17 +01:00
end .
2007-12-01 06:16:30 +01:00
2010-05-12 13:45:56 +02:00
state_can_deliver ( { U , S , R } , [ ] ) - > [ { U , S , R } ] ;
state_can_deliver ( { U , S , R } , SubOptions ) - >
%% Check SubOptions for 'show_values'
case lists : keysearch ( 'show_values' , 1 , SubOptions ) of
%% If not in suboptions, item can be delivered, case doesn't apply
false - > [ { U , S , R } ] ;
%% If in a suboptions ...
{ _ , { _ , ShowValues } } - >
%% Get subscriber resources
Resources = case R of
%% If the subscriber JID is a bare one, get all its resources
[ ] - > user_resources ( U , S ) ;
%% If the subscriber JID is a full one, use its resource
R - > [ R ]
end ,
%% For each resource, test if the item is allowed to be delivered
%% based on resource state
lists : foldl (
fun ( Resource , Acc ) - >
get_resource_state ( { U , S , Resource } , ShowValues , Acc )
end , [ ] , Resources )
end .
get_resource_state ( { U , S , R } , ShowValues , JIDs ) - >
%% Get user session PID
case ejabberd_sm : get_session_pid ( U , S , R ) of
%% If no PID, item can be delivered
none - > lists : append ( [ { U , S , R } ] , JIDs ) ;
%% If PID ...
Pid - >
%% Get user resource state
%% TODO : add a catch clause
Show = case ejabberd_c2s : get_presence ( Pid ) of
{ _ , _ , " available " , _ } - > " online " ;
{ _ , _ , State , _ } - > State
end ,
%% Is current resource state listed in 'show-values' suboption ?
case lists : member ( Show , ShowValues ) of %andalso Show =/= "online" of
%% If yes, item can be delivered
true - > lists : append ( [ { U , S , R } ] , JIDs ) ;
%% If no, item can't be delivered
false - > JIDs
end
end .
2009-01-03 22:29:54 +01:00
%% @spec (Payload) -> int()
2008-12-08 20:50:50 +01:00
%% Payload = term()
2009-01-03 22:29:54 +01:00
%% @doc <p>Count occurence of XML elements in payload.</p>
2008-12-09 23:02:47 +01:00
payload_xmlelements ( Payload ) - > payload_xmlelements ( Payload , 0 ) .
payload_xmlelements ( [ ] , Count ) - > Count ;
payload_xmlelements ( [ { xmlelement , _ , _ , _ } | Tail ] , Count ) - > payload_xmlelements ( Tail , Count + 1 ) ;
payload_xmlelements ( [ _ | Tail ] , Count ) - > payload_xmlelements ( Tail , Count ) .
2008-12-08 20:50:50 +01:00
2009-01-03 01:25:40 +01:00
%% @spec (Els) -> stanza()
%% Els = [xmlelement()]
2009-01-03 22:29:54 +01:00
%% @doc <p>Build pubsub event stanza</p>
2009-01-03 01:25:40 +01:00
event_stanza ( Els ) - >
2009-09-23 11:27:12 +02:00
event_stanza_withmoreels ( Els , [ ] ) .
event_stanza_with_delay ( Els , ModifNow , ModifLjid ) - >
DateTime = calendar : now_to_datetime ( ModifNow ) ,
MoreEls = [ jlib : timestamp_to_xml ( DateTime , utc , ModifLjid , " " ) ] ,
event_stanza_withmoreels ( Els , MoreEls ) .
event_stanza_withmoreels ( Els , MoreEls ) - >
2009-01-03 01:25:40 +01:00
{ xmlelement , " message " , [ ] ,
2009-09-23 11:27:12 +02:00
[ { xmlelement , " event " , [ { " xmlns " , ? NS_PUBSUB_EVENT } ] , Els } | MoreEls ] } .
2009-01-03 01:25:40 +01:00
2007-12-01 06:16:30 +01:00
%%%%%% broadcast functions
2010-03-05 17:52:41 +01:00
broadcast_publish_item ( Host , Node , NodeId , Type , NodeOptions , Removed , ItemId , From , Payload ) - >
2009-06-15 15:45:40 +02:00
case get_collection_subscriptions ( Host , Node ) of
SubsByDepth when is_list ( SubsByDepth ) - >
2009-05-29 04:14:07 +02:00
Content = case get_option ( NodeOptions , deliver_payloads ) of
2009-04-30 07:18:06 +02:00
true - > Payload ;
false - > [ ]
end ,
Stanza = event_stanza (
2009-05-08 03:18:02 +02:00
[ { xmlelement , " items " , nodeAttr ( Node ) ,
2009-04-30 07:18:06 +02:00
[ { xmlelement , " item " , itemAttr ( ItemId ) , Content } ] } ] ) ,
2010-03-05 17:52:41 +01:00
broadcast_stanza ( Host , From , Node , NodeId , Type ,
2010-01-12 11:33:39 +01:00
NodeOptions , SubsByDepth , items , Stanza , true ) ,
2009-04-30 07:18:06 +02:00
case Removed of
[ ] - >
ok ;
_ - >
2009-05-29 04:14:07 +02:00
case get_option ( NodeOptions , notify_retract ) of
2009-04-30 07:18:06 +02:00
true - >
RetractStanza = event_stanza (
2009-05-08 03:18:02 +02:00
[ { xmlelement , " items " , nodeAttr ( Node ) ,
2009-04-30 07:18:06 +02:00
[ { xmlelement , " retract " , itemAttr ( RId ) , [ ] } | | RId < - Removed ] } ] ) ,
2009-05-29 04:14:07 +02:00
broadcast_stanza ( Host , Node , NodeId , Type ,
2009-06-15 15:45:40 +02:00
NodeOptions , SubsByDepth ,
2010-01-12 11:33:39 +01:00
items , RetractStanza , true ) ;
2009-04-30 07:18:06 +02:00
_ - >
ok
end
end ,
{ result , true } ;
_ - >
{ result , false }
end .
2009-05-29 04:14:07 +02:00
broadcast_retract_items ( Host , Node , NodeId , Type , NodeOptions , ItemIds ) - >
broadcast_retract_items ( Host , Node , NodeId , Type , NodeOptions , ItemIds , false ) .
broadcast_retract_items ( _ Host , _ Node , _ NodeId , _ Type , _ NodeOptions , [ ] , _ ForceNotify ) - >
2009-04-30 07:18:06 +02:00
{ result , false } ;
2009-05-29 04:14:07 +02:00
broadcast_retract_items ( Host , Node , NodeId , Type , NodeOptions , ItemIds , ForceNotify ) - >
case ( get_option ( NodeOptions , notify_retract ) or ForceNotify ) of
2009-04-30 07:18:06 +02:00
true - >
2009-06-15 15:45:40 +02:00
case get_collection_subscriptions ( Host , Node ) of
SubsByDepth when is_list ( SubsByDepth ) - >
2009-01-03 01:25:40 +01:00
Stanza = event_stanza (
2009-05-08 03:18:02 +02:00
[ { xmlelement , " items " , nodeAttr ( Node ) ,
2009-04-30 07:18:06 +02:00
[ { xmlelement , " retract " , itemAttr ( ItemId ) , [ ] } | | ItemId < - ItemIds ] } ] ) ,
2009-05-29 04:14:07 +02:00
broadcast_stanza ( Host , Node , NodeId , Type ,
2010-01-12 11:33:39 +01:00
NodeOptions , SubsByDepth , items , Stanza , true ) ,
2009-01-03 01:25:40 +01:00
{ result , true } ;
_ - >
{ result , false }
2009-04-30 07:18:06 +02:00
end ;
_ - >
{ result , false }
end .
2007-12-01 06:16:30 +01:00
2009-05-29 04:14:07 +02:00
broadcast_purge_node ( Host , Node , NodeId , Type , NodeOptions ) - >
case get_option ( NodeOptions , notify_retract ) of
2009-04-30 07:18:06 +02:00
true - >
2009-06-15 15:45:40 +02:00
case get_collection_subscriptions ( Host , Node ) of
SubsByDepth when is_list ( SubsByDepth ) - >
2009-04-30 07:18:06 +02:00
Stanza = event_stanza (
2009-05-08 03:18:02 +02:00
[ { xmlelement , " purge " , nodeAttr ( Node ) ,
2009-04-30 07:18:06 +02:00
[ ] } ] ) ,
2009-05-29 04:14:07 +02:00
broadcast_stanza ( Host , Node , NodeId , Type ,
2010-01-12 11:33:39 +01:00
NodeOptions , SubsByDepth , nodes , Stanza , false ) ,
2009-04-30 07:18:06 +02:00
{ result , true } ;
_ - >
2009-01-03 01:25:40 +01:00
{ result , false }
2009-04-30 07:18:06 +02:00
end ;
_ - >
{ result , false }
end .
2003-07-08 22:11:27 +02:00
2009-06-15 15:45:40 +02:00
broadcast_removed_node ( Host , Node , NodeId , Type , NodeOptions , SubsByDepth ) - >
2009-05-29 04:14:07 +02:00
case get_option ( NodeOptions , notify_delete ) of
2009-04-30 07:18:06 +02:00
true - >
2009-06-15 15:45:40 +02:00
case SubsByDepth of
2009-04-30 07:18:06 +02:00
[ ] - >
{ result , false } ;
2009-01-03 01:25:40 +01:00
_ - >
2009-04-30 07:18:06 +02:00
Stanza = event_stanza (
2009-05-08 03:18:02 +02:00
[ { xmlelement , " delete " , nodeAttr ( Node ) ,
2009-04-30 07:18:06 +02:00
[ ] } ] ) ,
2009-05-29 04:14:07 +02:00
broadcast_stanza ( Host , Node , NodeId , Type ,
2010-01-12 11:33:39 +01:00
NodeOptions , SubsByDepth , nodes , Stanza , false ) ,
2009-04-30 07:18:06 +02:00
{ result , true }
end ;
_ - >
{ result , false }
end .
2003-07-08 22:11:27 +02:00
2009-05-29 04:14:07 +02:00
broadcast_config_notification ( Host , Node , NodeId , Type , NodeOptions , Lang ) - >
case get_option ( NodeOptions , notify_config ) of
2009-04-30 07:18:06 +02:00
true - >
2009-06-15 15:45:40 +02:00
case get_collection_subscriptions ( Host , Node ) of
SubsByDepth when is_list ( SubsByDepth ) - >
2009-05-29 04:14:07 +02:00
Content = case get_option ( NodeOptions , deliver_payloads ) of
2009-04-30 07:18:06 +02:00
true - >
2009-07-30 15:13:22 +02:00
[ { xmlelement , " x " , [ { " xmlns " , ? NS_XDATA } , { " type " , " result " } ] ,
2009-05-29 04:14:07 +02:00
get_configure_xfields ( Type , NodeOptions , Lang , [ ] ) } ] ;
2009-04-30 07:18:06 +02:00
false - >
[ ]
end ,
Stanza = event_stanza (
2009-07-30 15:13:22 +02:00
[ { xmlelement , " configuration " , nodeAttr ( Node ) , Content } ] ) ,
2009-05-29 04:14:07 +02:00
broadcast_stanza ( Host , Node , NodeId , Type ,
2010-01-12 11:33:39 +01:00
NodeOptions , SubsByDepth , nodes , Stanza , false ) ,
2009-04-30 07:18:06 +02:00
{ result , true } ;
_ - >
2009-01-03 01:25:40 +01:00
{ result , false }
2009-04-30 07:18:06 +02:00
end ;
_ - >
{ result , false }
end .
2007-12-01 06:16:30 +01:00
2009-06-15 15:45:40 +02:00
get_collection_subscriptions ( Host , Node ) - >
2009-08-27 23:38:23 +02:00
Action = fun ( ) - >
{ result , lists : map ( fun ( { Depth , Nodes } ) - >
{ Depth , [ { N , get_node_subs ( N ) } | | N < - Nodes ] }
end , tree_call ( Host , get_parentnodes_tree , [ Host , Node , service_jid ( Host ) ] ) ) }
end ,
case transaction ( Action , sync_dirty ) of
{ result , CollSubs } - > CollSubs ;
_ - > [ ]
end .
2009-06-15 15:45:40 +02:00
get_node_subs ( #pubsub_node { type = Type ,
id = NodeID } ) - >
2009-08-27 23:38:23 +02:00
case node_call ( Type , get_node_subscriptions , [ NodeID ] ) of
{ result , Subs } - > get_options_for_subs ( NodeID , Subs ) ;
Other - > Other
2009-06-15 15:45:40 +02:00
end .
2009-08-27 23:38:23 +02:00
get_options_for_subs ( NodeID , Subs ) - >
2009-07-31 16:55:05 +02:00
lists : foldl ( fun ( { JID , subscribed , SubID } , Acc ) - >
2009-08-27 23:38:23 +02:00
case pubsub_subscription : read_subscription ( JID , NodeID , SubID ) of
{ error , notfound } - > [ { JID , SubID , [ ] } | Acc ] ;
#pubsub_subscription { options = Options } - > [ { JID , SubID , Options } | Acc ] ;
2009-08-20 15:07:58 +02:00
_ - > Acc
end ;
2009-07-31 16:55:05 +02:00
( _ , Acc ) - >
Acc
end , [ ] , Subs ) .
2010-03-05 11:53:26 +01:00
broadcast_stanza ( Host , _ Node , _ NodeId , _ Type , NodeOptions , SubsByDepth , NotifyType , BaseStanza , SHIM ) - >
2009-10-12 11:05:26 +02:00
NotificationType = get_option ( NodeOptions , notification_type , headline ) ,
2009-05-29 04:14:07 +02:00
BroadcastAll = get_option ( NodeOptions , broadcast_all_resources ) , %% XXX this is not standard, but usefull
2009-01-03 01:25:40 +01:00
From = service_jid ( Host ) ,
2009-09-24 21:28:43 +02:00
Stanza = case NotificationType of
normal - > BaseStanza ;
MsgType - > add_message_type ( BaseStanza , atom_to_list ( MsgType ) )
end ,
2009-04-08 17:53:46 +02:00
%% Handles explicit subscriptions
2010-01-12 11:33:39 +01:00
SubIDsByJID = subscribed_nodes_by_jid ( NotifyType , SubsByDepth ) ,
2010-01-12 14:27:04 +01:00
lists : foreach ( fun ( { LJID , NodeName , SubIDs } ) - >
2009-05-29 04:14:07 +02:00
LJIDs = case BroadcastAll of
true - >
{ U , S , _ } = LJID ,
[ { U , S , R } | | R < - user_resources ( U , S ) ] ;
false - >
[ LJID ]
end ,
2010-01-12 11:33:39 +01:00
%% Determine if the stanza should have SHIM ('SubID' and 'name') headers
2010-03-05 17:52:41 +01:00
StanzaToSend = case { SHIM , SubIDs } of
{ false , _ } - >
Stanza ;
%% If there's only one SubID, don't add it
{ true , [ _ ] } - >
add_shim_headers ( Stanza , collection_shim ( NodeName ) ) ;
{ true , SubIDs } - >
add_shim_headers ( Stanza , lists : append ( collection_shim ( NodeName ) , subid_shim ( SubIDs ) ) )
end ,
2009-05-29 04:14:07 +02:00
lists : foreach ( fun ( To ) - >
2010-03-05 17:52:41 +01:00
ejabberd_router : route ( From , jlib : make_jid ( To ) , StanzaToSend )
end , LJIDs )
end , SubIDsByJID ) .
broadcast_stanza ( { LUser , LServer , LResource } , Publisher , Node , NodeId , Type , NodeOptions , SubsByDepth , NotifyType , BaseStanza , SHIM ) - >
broadcast_stanza ( { LUser , LServer , LResource } , Node , NodeId , Type , NodeOptions , SubsByDepth , NotifyType , BaseStanza , SHIM ) ,
2009-04-08 17:53:46 +02:00
%% Handles implicit presence subscriptions
2010-03-05 17:52:41 +01:00
SenderResource = case LResource of
[ ] - >
case user_resources ( LUser , LServer ) of
[ Resource | _ ] - > Resource ;
_ - > " "
end ;
_ - >
LResource
end ,
case ejabberd_sm : get_session_pid ( LUser , LServer , SenderResource ) of
C2SPid when is_pid ( C2SPid ) - >
Stanza = case get_option ( NodeOptions , notification_type , headline ) of
normal - > BaseStanza ;
MsgType - > add_message_type ( BaseStanza , atom_to_list ( MsgType ) )
end ,
%% set the from address on the notification to the bare JID of the account owner
%% Also, add "replyto" if entity has presence subscription to the account owner
%% See XEP-0163 1.1 section 4.3.1
Sender = jlib : make_jid ( LUser , LServer , " " ) ,
ReplyTo = jlib : jid_to_string ( Publisher ) ,
StanzaToSend = add_extended_headers ( Stanza , extended_headers ( [ ReplyTo ] ) ) ,
case catch ejabberd_c2s : get_subscribed ( C2SPid ) of
Contacts when is_list ( Contacts ) - >
lists : foreach ( fun ( { U , S , _ } ) - >
spawn ( fun ( ) - >
2010-03-08 15:16:58 +01:00
case lists : member ( S , ? MYHOSTS ) of
true - >
lists : foreach ( fun ( To ) - >
ejabberd_router : route ( Sender , jlib : make_jid ( To ) , StanzaToSend )
end , [ { U , S , R } | | R < - user_resources ( U , S ) ] ) ;
false - >
ejabberd_router : route ( Sender , jlib : make_jid ( U , S , " " ) , StanzaToSend )
2010-03-08 15:40:08 +01:00
end
2010-03-05 17:52:41 +01:00
end )
end , Contacts ) ;
2009-04-08 17:53:46 +02:00
_ - >
ok
end ;
2008-12-08 15:10:55 +01:00
_ - >
2010-03-05 17:52:41 +01:00
? DEBUG ( " ~p @ ~p has no session; can't deliver ~p to contacts " , [ LUser , LServer , BaseStanza ] )
end ;
2010-03-08 12:43:38 +01:00
broadcast_stanza ( Host , _ Publisher , Node , NodeId , Type , NodeOptions , SubsByDepth , NotifyType , BaseStanza , SHIM ) - >
2010-03-05 18:20:22 +01:00
broadcast_stanza ( Host , Node , NodeId , Type , NodeOptions , SubsByDepth , NotifyType , BaseStanza , SHIM ) .
2005-07-20 05:09:34 +02:00
2009-08-27 23:38:23 +02:00
subscribed_nodes_by_jid ( NotifyType , SubsByDepth ) - >
NodesToDeliver = fun ( Depth , Node , Subs , Acc ) - >
2010-01-12 14:27:04 +01:00
NodeName = case Node #pubsub_node.nodeid of
{ _ , N } - > N ;
Other - > Other
end ,
2010-01-12 13:31:08 +01:00
NodeOptions = Node #pubsub_node.options ,
lists : foldl ( fun ( { LJID , SubID , SubOptions } , { JIDs , Recipients } ) - >
case is_to_deliver ( LJID , NotifyType , Depth , NodeOptions , SubOptions ) of
2010-05-12 13:45:56 +02:00
true - >
%% If is to deliver :
case state_can_deliver ( LJID , SubOptions ) of
[ ] - > { JIDs , Recipients } ;
JIDsToDeliver - >
lists : foldl (
fun ( JIDToDeliver , { JIDsAcc , RecipientsAcc } ) - >
case lists : member ( JIDToDeliver , JIDs ) of
2010-01-12 13:31:08 +01:00
%% check if the JIDs co-accumulator contains the Subscription Jid,
2010-05-12 13:45:56 +02:00
false - >
2010-01-12 13:31:08 +01:00
%% - if not,
%% - add the Jid to JIDs list co-accumulator ;
%% - create a tuple of the Jid, NodeId, and SubID (as list),
%% and add the tuple to the Recipients list co-accumulator
2010-05-12 13:45:56 +02:00
{ [ JIDToDeliver | JIDsAcc ] , [ { JIDToDeliver , NodeName , [ SubID ] } | RecipientsAcc ] } ;
true - >
2010-01-12 13:31:08 +01:00
%% - if the JIDs co-accumulator contains the Jid
%% get the tuple containing the Jid from the Recipient list co-accumulator
2010-05-12 13:45:56 +02:00
{ _ , { JIDToDeliver , NodeName1 , SubIDs } } = lists : keysearch ( JIDToDeliver , 1 , RecipientsAcc ) ,
2010-01-12 13:31:08 +01:00
%% delete the tuple from the Recipients list
% v1 : Recipients1 = lists:keydelete(LJID, 1, Recipients),
% v2 : Recipients1 = lists:keyreplace(LJID, 1, Recipients, {LJID, NodeId1, [SubID | SubIDs]}),
%% add the SubID to the SubIDs list in the tuple,
%% and add the tuple back to the Recipients list co-accumulator
% v1.1 : {JIDs, lists:append(Recipients1, [{LJID, NodeId1, lists:append(SubIDs, [SubID])}])}
% v1.2 : {JIDs, [{LJID, NodeId1, [SubID | SubIDs]} | Recipients1]}
% v2: {JIDs, Recipients1}
2010-05-12 13:45:56 +02:00
{ JIDsAcc , lists : keyreplace ( JIDToDeliver , 1 , RecipientsAcc , { JIDToDeliver , NodeName1 , [ SubID | SubIDs ] } ) }
end
end , { JIDs , Recipients } , JIDsToDeliver )
end ;
2010-01-12 13:31:08 +01:00
false - >
{ JIDs , Recipients }
end
end , Acc , Subs )
2009-06-15 15:45:40 +02:00
end ,
2010-05-12 13:45:56 +02:00
DepthsToDeliver = fun ( { Depth , SubsByNode } , Acc1 ) - >
2010-01-12 13:31:08 +01:00
lists : foldl ( fun ( { Node , Subs } , Acc2 ) - >
NodesToDeliver ( Depth , Node , Subs , Acc2 )
2010-05-12 13:45:56 +02:00
end , Acc1 , SubsByNode )
2009-06-15 15:45:40 +02:00
end ,
2010-01-12 13:31:08 +01:00
{ _ , JIDSubs } = lists : foldl ( DepthsToDeliver , { [ ] , [ ] } , SubsByDepth ) ,
JIDSubs .
2009-06-15 15:45:40 +02:00
2009-03-03 23:42:37 +01:00
user_resources ( User , Server ) - >
2010-03-05 10:34:15 +01:00
ejabberd_sm : get_user_resources ( User , Server ) .
2007-12-22 01:04:05 +01:00
2007-12-01 06:16:30 +01:00
%%%%%%% Configuration handling
%%<p>There are several reasons why the default node configuration options request might fail:</p>
%%<ul>
2008-04-01 12:11:39 +02:00
%%<li>The service does not support node configuration.</li>
%%<li>The service does not support retrieval of default node configuration.</li>
2007-12-01 06:16:30 +01:00
%%</ul>
2009-03-20 01:08:38 +01:00
get_configure ( Host , ServerHost , Node , From , Lang ) - >
2007-12-01 06:16:30 +01:00
Action =
2009-04-30 07:18:06 +02:00
fun ( #pubsub_node { options = Options , type = Type , id = NodeId } ) - >
case node_call ( Type , get_affiliation , [ NodeId , From ] ) of
2007-12-01 06:16:30 +01:00
{ result , owner } - >
2009-03-20 01:08:38 +01:00
Groups = ejabberd_hooks : run_fold ( roster_groups , ServerHost , [ ] , [ ServerHost ] ) ,
2007-12-01 06:16:30 +01:00
{ result ,
[ { xmlelement , " pubsub " ,
[ { " xmlns " , ? NS_PUBSUB_OWNER } ] ,
2009-05-08 03:18:02 +02:00
[ { xmlelement , " configure " , nodeAttr ( Node ) ,
2007-12-01 06:16:30 +01:00
[ { xmlelement , " x " ,
[ { " xmlns " , ? NS_XDATA } , { " type " , " form " } ] ,
2009-04-30 07:18:06 +02:00
get_configure_xfields ( Type , Options , Lang , Groups )
2007-12-01 06:16:30 +01:00
} ] } ] } ] } ;
_ - >
{ error , ? ERR_FORBIDDEN }
end
end ,
2009-04-30 07:18:06 +02:00
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , { _ , Result } } - > { result , Result } ;
Other - > Other
end .
2007-12-01 06:16:30 +01:00
2008-09-25 19:26:06 +02:00
get_default ( Host , Node , _ From , Lang ) - >
2008-12-09 00:28:50 +01:00
Type = select_type ( Host , Host , Node ) ,
2008-06-18 18:19:18 +02:00
Options = node_options ( Type ) ,
{ result , [ { xmlelement , " pubsub " , [ { " xmlns " , ? NS_PUBSUB_OWNER } ] ,
[ { xmlelement , " default " , [ ] ,
[ { xmlelement , " x " , [ { " xmlns " , ? NS_XDATA } , { " type " , " form " } ] ,
2009-07-31 16:55:05 +02:00
get_configure_xfields ( Type , Options , Lang , [ ] )
2008-06-18 18:19:18 +02:00
} ] } ] } ] } .
2007-12-01 06:16:30 +01:00
%% Get node option
%% The result depend of the node type plugin system.
get_option ( [ ] , _ ) - > false ;
get_option ( Options , Var ) - >
2008-07-23 01:41:44 +02:00
get_option ( Options , Var , false ) .
get_option ( Options , Var , Def ) - >
2007-12-01 06:16:30 +01:00
case lists : keysearch ( Var , 1 , Options ) of
{ value , { _ Val , Ret } } - > Ret ;
2008-07-23 01:41:44 +02:00
_ - > Def
2007-12-01 06:16:30 +01:00
end .
2003-07-20 22:35:35 +02:00
2007-12-01 06:16:30 +01:00
%% Get default options from the module plugin.
node_options ( Type ) - >
Module = list_to_atom ( ? PLUGIN_PREFIX ++ Type ) ,
case catch Module : options ( ) of
{ 'EXIT' , { undef , _ } } - >
DefaultModule = list_to_atom ( ? PLUGIN_PREFIX ++ ? STDNODE ) ,
DefaultModule : options ( ) ;
Result - >
Result
2005-07-20 05:09:34 +02:00
end .
2009-08-17 19:16:43 +02:00
%% @spec (Host, Options) -> MaxItems
2009-07-30 23:37:19 +02:00
%% Host = host()
2007-12-01 06:16:30 +01:00
%% Options = [Option]
%% Option = {Key::atom(), Value::term()}
%% MaxItems = integer() | unlimited
%% @doc <p>Return the maximum number of items for a given node.</p>
%% <p>Unlimited means that there is no limit in the number of items that can
%% be stored.</p>
%% @todo In practice, the current data structure means that we cannot manage
%% millions of items on a given node. This should be addressed in a new
%% version.
2009-07-30 23:37:19 +02:00
max_items ( Host , Options ) - >
2007-12-01 06:16:30 +01:00
case get_option ( Options , persist_items ) of
true - >
case get_option ( Options , max_items ) of
false - > unlimited ;
Result when ( Result < 0 ) - > 0 ;
Result - > Result
2005-07-20 05:09:34 +02:00
end ;
2007-12-01 06:16:30 +01:00
false - >
case get_option ( Options , send_last_published_item ) of
2009-07-30 23:00:44 +02:00
never - >
0 ;
_ - >
case is_last_item_cache_enabled ( Host ) of
true - > 0 ;
false - > 1
end
2007-12-01 06:16:30 +01:00
end
2005-07-20 05:09:34 +02:00
end .
- define ( BOOL_CONFIG_FIELD ( Label , Var ) ,
? BOOLXFIELD ( Label , " pubsub# " ++ atom_to_list ( Var ) ,
2007-12-01 06:16:30 +01:00
get_option ( Options , Var ) ) ) .
2005-07-20 05:09:34 +02:00
- define ( STRING_CONFIG_FIELD ( Label , Var ) ,
? STRINGXFIELD ( Label , " pubsub# " ++ atom_to_list ( Var ) ,
2008-07-23 01:41:44 +02:00
get_option ( Options , Var , " " ) ) ) .
2005-07-20 05:09:34 +02:00
- define ( INTEGER_CONFIG_FIELD ( Label , Var ) ,
? STRINGXFIELD ( Label , " pubsub# " ++ atom_to_list ( Var ) ,
2007-12-01 06:16:30 +01:00
integer_to_list ( get_option ( Options , Var ) ) ) ) .
2005-07-20 05:09:34 +02:00
- define ( JLIST_CONFIG_FIELD ( Label , Var , Opts ) ,
? LISTXFIELD ( Label , " pubsub# " ++ atom_to_list ( Var ) ,
2007-12-01 06:16:30 +01:00
jlib : jid_to_string ( get_option ( Options , Var ) ) ,
2005-07-20 05:09:34 +02:00
[ jlib : jid_to_string ( O ) | | O < - Opts ] ) ) .
- define ( ALIST_CONFIG_FIELD ( Label , Var , Opts ) ,
? LISTXFIELD ( Label , " pubsub# " ++ atom_to_list ( Var ) ,
2007-12-01 06:16:30 +01:00
atom_to_list ( get_option ( Options , Var ) ) ,
2005-07-20 05:09:34 +02:00
[ atom_to_list ( O ) | | O < - Opts ] ) ) .
2009-03-20 01:08:38 +01:00
- define ( LISTM_CONFIG_FIELD ( Label , Var , Opts ) ,
? LISTMXFIELD ( Label , " pubsub# " ++ atom_to_list ( Var ) ,
get_option ( Options , Var ) , Opts ) ) .
2009-06-15 15:45:40 +02:00
- define ( NLIST_CONFIG_FIELD ( Label , Var ) ,
? STRINGMXFIELD ( Label , " pubsub# " ++ atom_to_list ( Var ) ,
2009-07-24 22:09:25 +02:00
[ node_to_string ( N ) | | N < - get_option ( Options , Var , [ ] ) ] ) ) .
2009-06-15 15:45:40 +02:00
2009-04-30 07:18:06 +02:00
get_configure_xfields ( _ Type , Options , Lang , Groups ) - >
2005-07-20 05:09:34 +02:00
[ ? XFIELD ( " hidden " , " " , " FORM_TYPE " , ? NS_PUBSUB_NODE_CONFIG ) ,
? BOOL_CONFIG_FIELD ( " Deliver payloads with event notifications " , deliver_payloads ) ,
2007-12-01 06:16:30 +01:00
? BOOL_CONFIG_FIELD ( " Deliver event notifications " , deliver_notifications ) ,
2005-07-20 05:09:34 +02:00
? BOOL_CONFIG_FIELD ( " Notify subscribers when the node configuration changes " , notify_config ) ,
? BOOL_CONFIG_FIELD ( " Notify subscribers when the node is deleted " , notify_delete ) ,
? BOOL_CONFIG_FIELD ( " Notify subscribers when items are removed from the node " , notify_retract ) ,
? BOOL_CONFIG_FIELD ( " Persist items to storage " , persist_items ) ,
2008-07-03 11:56:31 +02:00
? STRING_CONFIG_FIELD ( " A friendly name for the node " , title ) ,
2005-07-20 05:09:34 +02:00
? INTEGER_CONFIG_FIELD ( " Max # of items to persist " , max_items ) ,
? BOOL_CONFIG_FIELD ( " Whether to allow subscriptions " , subscribe ) ,
2007-12-01 06:16:30 +01:00
? ALIST_CONFIG_FIELD ( " Specify the access model " , access_model ,
[ open , authorize , presence , roster , whitelist ] ) ,
%% XXX: change to list-multi, include current roster groups as options
2009-03-20 01:08:38 +01:00
? LISTM_CONFIG_FIELD ( " Roster groups allowed to subscribe " , roster_groups_allowed , Groups ) ,
2005-07-20 05:09:34 +02:00
? ALIST_CONFIG_FIELD ( " Specify the publisher model " , publish_model ,
[ publishers , subscribers , open ] ) ,
2010-03-05 11:11:44 +01:00
? BOOL_CONFIG_FIELD ( " Purge all items when the relevant publisher goes offline " , purge_offline ) ,
2009-09-24 21:28:43 +02:00
? ALIST_CONFIG_FIELD ( " Specify the event message type " , notification_type ,
[ headline , normal ] ) ,
2005-07-20 05:09:34 +02:00
? INTEGER_CONFIG_FIELD ( " Max payload size in bytes " , max_payload_size ) ,
2007-12-01 06:16:30 +01:00
? ALIST_CONFIG_FIELD ( " When to send the last published item " , send_last_published_item ,
[ never , on_sub , on_sub_and_presence ] ) ,
2009-06-15 15:45:40 +02:00
? BOOL_CONFIG_FIELD ( " Only deliver notifications to available users " , presence_based_delivery ) ,
? NLIST_CONFIG_FIELD ( " The collections with which a node is affiliated " , collection )
2005-07-20 05:09:34 +02:00
] .
2007-12-01 06:16:30 +01:00
%%<p>There are several reasons why the node configuration request might fail:</p>
%%<ul>
2008-04-01 12:11:39 +02:00
%%<li>The service does not support node configuration.</li>
%%<li>The requesting entity does not have sufficient privileges to configure the node.</li>
%%<li>The request did not specify a node.</li>
%%<li>The node has no configuration options.</li>
%%<li>The specified node does not exist.</li>
2007-12-01 06:16:30 +01:00
%%</ul>
set_configure ( Host , Node , From , Els , Lang ) - >
case xml : remove_cdata ( Els ) of
[ { xmlelement , " x " , _ Attrs1 , _ Els1 } = XEl ] - >
case { xml : get_tag_attr_s ( " xmlns " , XEl ) , xml : get_tag_attr_s ( " type " , XEl ) } of
{ ? NS_XDATA , " cancel " } - >
{ result , [ ] } ;
{ ? NS_XDATA , " submit " } - >
Action =
2009-04-30 07:18:06 +02:00
fun ( #pubsub_node { options = Options , type = Type , id = NodeId } = N ) - >
case node_call ( Type , get_affiliation , [ NodeId , From ] ) of
2007-12-01 06:16:30 +01:00
{ result , owner } - >
case jlib : parse_xdata_submit ( XEl ) of
invalid - >
{ error , ? ERR_BAD_REQUEST } ;
XData - >
OldOpts = case Options of
[ ] - > node_options ( Type ) ;
_ - > Options
end ,
2009-09-23 17:56:06 +02:00
case set_xoption ( Host , XData , OldOpts ) of
2007-12-01 06:16:30 +01:00
NewOpts when is_list ( NewOpts ) - >
2009-06-15 15:45:40 +02:00
case tree_call ( Host , set_node , [ N #pubsub_node { options = NewOpts } ] ) of
ok - > { result , ok } ;
Err - > Err
end ;
2007-12-01 06:16:30 +01:00
Err - >
Err
end
end ;
_ - >
{ error , ? ERR_FORBIDDEN }
end
end ,
case transaction ( Host , Node , Action , transaction ) of
2009-04-30 07:18:06 +02:00
{ result , { TNode , ok } } - >
NodeId = TNode #pubsub_node.id ,
Type = TNode #pubsub_node.type ,
Options = TNode #pubsub_node.options ,
broadcast_config_notification ( Host , Node , NodeId , Type , Options , Lang ) ,
2007-12-01 06:16:30 +01:00
{ result , [ ] } ;
Other - >
Other
2005-07-20 05:09:34 +02:00
end ;
_ - >
2007-12-01 06:16:30 +01:00
{ error , ? ERR_BAD_REQUEST }
2005-07-20 05:09:34 +02:00
end ;
_ - >
2007-12-01 06:16:30 +01:00
{ error , ? ERR_BAD_REQUEST }
2005-07-20 05:09:34 +02:00
end .
add_opt ( Key , Value , Opts ) - >
Opts1 = lists : keydelete ( Key , 1 , Opts ) ,
[ { Key , Value } | Opts1 ] .
- define ( SET_BOOL_XOPT ( Opt , Val ) ,
2007-12-01 06:16:30 +01:00
BoolVal = case Val of
" 0 " - > false ;
" 1 " - > true ;
" false " - > false ;
" true " - > true ;
_ - > error
end ,
case BoolVal of
error - > { error , ? ERR_NOT_ACCEPTABLE } ;
2009-09-23 17:56:06 +02:00
_ - > set_xoption ( Host , Opts , add_opt ( Opt , BoolVal , NewOpts ) )
2005-07-20 05:09:34 +02:00
end ) .
- define ( SET_STRING_XOPT ( Opt , Val ) ,
2009-09-23 17:56:06 +02:00
set_xoption ( Host , Opts , add_opt ( Opt , Val , NewOpts ) ) ) .
2005-07-20 05:09:34 +02:00
- define ( SET_INTEGER_XOPT ( Opt , Val , Min , Max ) ,
case catch list_to_integer ( Val ) of
IVal when is_integer ( IVal ) ,
2007-12-01 06:16:30 +01:00
IVal > = Min ,
IVal =< Max - >
2009-09-23 17:56:06 +02:00
set_xoption ( Host , Opts , add_opt ( Opt , IVal , NewOpts ) ) ;
2005-07-20 05:09:34 +02:00
_ - >
2007-12-01 06:16:30 +01:00
{ error , ? ERR_NOT_ACCEPTABLE }
2005-07-20 05:09:34 +02:00
end ) .
- define ( SET_ALIST_XOPT ( Opt , Val , Vals ) ,
case lists : member ( Val , [ atom_to_list ( V ) | | V < - Vals ] ) of
2009-09-23 17:56:06 +02:00
true - > set_xoption ( Host , Opts , add_opt ( Opt , list_to_atom ( Val ) , NewOpts ) ) ;
2007-12-01 06:16:30 +01:00
false - > { error , ? ERR_NOT_ACCEPTABLE }
2005-07-20 05:09:34 +02:00
end ) .
2007-12-01 06:16:30 +01:00
- define ( SET_LIST_XOPT ( Opt , Val ) ,
2009-09-23 17:56:06 +02:00
set_xoption ( Host , Opts , add_opt ( Opt , Val , NewOpts ) ) ) .
2005-07-20 05:09:34 +02:00
2009-09-23 17:56:06 +02:00
set_xoption ( _ Host , [ ] , NewOpts ) - >
2005-07-20 05:09:34 +02:00
NewOpts ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " FORM_TYPE " , _ } | Opts ] , NewOpts ) - >
set_xoption ( Host , Opts , NewOpts ) ;
set_xoption ( Host , [ { " pubsub#roster_groups_allowed " , Value } | Opts ] , NewOpts ) - >
2009-03-20 01:08:38 +01:00
? SET_LIST_XOPT ( roster_groups_allowed , Value ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#deliver_payloads " , [ Val ] } | Opts ] , NewOpts ) - >
2005-07-20 05:09:34 +02:00
? SET_BOOL_XOPT ( deliver_payloads , Val ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#deliver_notifications " , [ Val ] } | Opts ] , NewOpts ) - >
2007-12-01 06:16:30 +01:00
? SET_BOOL_XOPT ( deliver_notifications , Val ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#notify_config " , [ Val ] } | Opts ] , NewOpts ) - >
2005-07-20 05:09:34 +02:00
? SET_BOOL_XOPT ( notify_config , Val ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#notify_delete " , [ Val ] } | Opts ] , NewOpts ) - >
2005-07-20 05:09:34 +02:00
? SET_BOOL_XOPT ( notify_delete , Val ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#notify_retract " , [ Val ] } | Opts ] , NewOpts ) - >
2005-07-20 05:09:34 +02:00
? SET_BOOL_XOPT ( notify_retract , Val ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#persist_items " , [ Val ] } | Opts ] , NewOpts ) - >
2005-07-20 05:09:34 +02:00
? SET_BOOL_XOPT ( persist_items , Val ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#max_items " , [ Val ] } | Opts ] , NewOpts ) - >
MaxItems = get_max_items_node ( Host ) ,
? SET_INTEGER_XOPT ( max_items , Val , 0 , MaxItems ) ;
set_xoption ( Host , [ { " pubsub#subscribe " , [ Val ] } | Opts ] , NewOpts ) - >
2005-07-20 05:09:34 +02:00
? SET_BOOL_XOPT ( subscribe , Val ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#access_model " , [ Val ] } | Opts ] , NewOpts ) - >
2007-12-01 06:16:30 +01:00
? SET_ALIST_XOPT ( access_model , Val , [ open , authorize , presence , roster , whitelist ] ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#publish_model " , [ Val ] } | Opts ] , NewOpts ) - >
2005-07-20 05:09:34 +02:00
? SET_ALIST_XOPT ( publish_model , Val , [ publishers , subscribers , open ] ) ;
2009-09-24 21:28:43 +02:00
set_xoption ( Host , [ { " pubsub#notification_type " , [ Val ] } | Opts ] , NewOpts ) - >
? SET_ALIST_XOPT ( notification_type , Val , [ headline , normal ] ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#node_type " , [ Val ] } | Opts ] , NewOpts ) - >
2007-12-01 06:16:30 +01:00
? SET_ALIST_XOPT ( node_type , Val , [ leaf , collection ] ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#max_payload_size " , [ Val ] } | Opts ] , NewOpts ) - >
2005-07-20 05:09:34 +02:00
? SET_INTEGER_XOPT ( max_payload_size , Val , 0 , ? MAX_PAYLOAD_SIZE ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#send_last_published_item " , [ Val ] } | Opts ] , NewOpts ) - >
2007-12-01 06:16:30 +01:00
? SET_ALIST_XOPT ( send_last_published_item , Val , [ never , on_sub , on_sub_and_presence ] ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#presence_based_delivery " , [ Val ] } | Opts ] , NewOpts ) - >
2005-07-20 05:09:34 +02:00
? SET_BOOL_XOPT ( presence_based_delivery , Val ) ;
2010-03-05 11:11:44 +01:00
set_xoption ( Host , [ { " pubsub#purge_offline " , [ Val ] } | Opts ] , NewOpts ) - >
? SET_BOOL_XOPT ( purge_offline , Val ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#title " , Value } | Opts ] , NewOpts ) - >
2007-12-01 06:16:30 +01:00
? SET_STRING_XOPT ( title , Value ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#type " , Value } | Opts ] , NewOpts ) - >
2007-12-01 06:16:30 +01:00
? SET_STRING_XOPT ( type , Value ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#body_xslt " , Value } | Opts ] , NewOpts ) - >
2007-12-01 06:16:30 +01:00
? SET_STRING_XOPT ( body_xslt , Value ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#collection " , Value } | Opts ] , NewOpts ) - >
2009-06-15 15:45:40 +02:00
NewValue = [ string_to_node ( V ) | | V < - Value ] ,
? SET_LIST_XOPT ( collection , NewValue ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ { " pubsub#node " , [ Value ] } | Opts ] , NewOpts ) - >
2009-07-20 10:53:56 +02:00
NewValue = string_to_node ( Value ) ,
? SET_LIST_XOPT ( node , NewValue ) ;
2009-09-23 17:56:06 +02:00
set_xoption ( Host , [ _ | Opts ] , NewOpts ) - >
2008-12-08 20:50:50 +01:00
% skip unknown field
2009-09-23 17:56:06 +02:00
set_xoption ( Host , Opts , NewOpts ) .
get_max_items_node ( { _ , ServerHost , _ } ) - >
get_max_items_node ( ServerHost ) ;
get_max_items_node ( Host ) - >
case catch ets : lookup ( gen_mod : get_module_proc ( Host , config ) , max_items_node ) of
[ { max_items_node , Integer } ] - > Integer ;
_ - > ? MAXITEMS
end .
2007-12-01 06:16:30 +01:00
2009-05-29 00:30:43 +02:00
%%%% last item cache handling
2009-07-31 16:55:05 +02:00
is_last_item_cache_enabled ( { _ , ServerHost , _ } ) - >
is_last_item_cache_enabled ( ServerHost ) ;
2009-07-30 23:00:44 +02:00
is_last_item_cache_enabled ( Host ) - >
2009-08-28 18:22:37 +02:00
case catch ets : lookup ( gen_mod : get_module_proc ( Host , config ) , last_item_cache ) of
2009-07-30 23:00:44 +02:00
[ { last_item_cache , true } ] - > true ;
2009-07-30 23:37:19 +02:00
_ - > false
2009-07-30 23:00:44 +02:00
end .
2009-05-29 00:30:43 +02:00
set_cached_item ( { _ , ServerHost , _ } , NodeId , ItemId , Payload ) - >
set_cached_item ( ServerHost , NodeId , ItemId , Payload ) ;
set_cached_item ( Host , NodeId , ItemId , Payload ) - >
2009-07-30 23:00:44 +02:00
case is_last_item_cache_enabled ( Host ) of
true - > ets : insert ( gen_mod : get_module_proc ( Host , last_items ) , { NodeId , { ItemId , Payload } } ) ;
_ - > ok
2009-05-29 00:30:43 +02:00
end .
unset_cached_item ( { _ , ServerHost , _ } , NodeId ) - >
unset_cached_item ( ServerHost , NodeId ) ;
unset_cached_item ( Host , NodeId ) - >
2009-07-30 23:00:44 +02:00
case is_last_item_cache_enabled ( Host ) of
true - > ets : delete ( gen_mod : get_module_proc ( Host , last_items ) , NodeId ) ;
_ - > ok
2009-05-29 00:30:43 +02:00
end .
get_cached_item ( { _ , ServerHost , _ } , NodeId ) - >
get_cached_item ( ServerHost , NodeId ) ;
get_cached_item ( Host , NodeId ) - >
2009-07-30 23:00:44 +02:00
case is_last_item_cache_enabled ( Host ) of
true - >
2009-08-28 18:22:37 +02:00
case catch ets : lookup ( gen_mod : get_module_proc ( Host , last_items ) , NodeId ) of
2009-05-29 00:30:43 +02:00
[ { NodeId , { ItemId , Payload } } ] - >
#pubsub_item { itemid = { ItemId , NodeId } , payload = Payload } ;
_ - >
undefined
end ;
_ - >
undefined
end .
2007-12-01 06:16:30 +01:00
%%%% plugin handling
2010-01-12 16:14:47 +01:00
host ( ServerHost ) - >
case catch ets : lookup ( gen_mod : get_module_proc ( ServerHost , config ) , host ) of
[ { host , Host } ] - > Host ;
_ - > " pubsub. " ++ ServerHost
end .
2008-04-02 11:06:28 +02:00
plugins ( Host ) - >
2009-08-28 18:22:37 +02:00
case catch ets : lookup ( gen_mod : get_module_proc ( Host , config ) , plugins ) of
2009-05-29 00:30:43 +02:00
[ { plugins , [ ] } ] - > [ ? STDNODE ] ;
2008-04-02 11:06:28 +02:00
[ { plugins , PL } ] - > PL ;
_ - > [ ? STDNODE ]
end .
2008-09-25 19:26:06 +02:00
select_type ( ServerHost , Host , Node , Type ) - >
2009-05-29 00:30:43 +02:00
SelectedType = case Host of
2008-12-09 00:28:50 +01:00
{ _ User , _ Server , _ Resource } - >
2009-08-28 18:22:37 +02:00
case catch ets : lookup ( gen_mod : get_module_proc ( ServerHost , config ) , pep_mapping ) of
2009-12-21 16:13:44 +01:00
[ { pep_mapping , PM } ] - > proplists : get_value ( node_to_string ( Node ) , PM , ? PEPNODE ) ;
2009-05-29 00:30:43 +02:00
_ - > ? PEPNODE
2008-12-09 00:28:50 +01:00
end ;
_ - >
Type
2009-05-29 00:30:43 +02:00
end ,
ConfiguredTypes = plugins ( ServerHost ) ,
case lists : member ( SelectedType , ConfiguredTypes ) of
true - > SelectedType ;
false - > hd ( ConfiguredTypes )
2008-09-25 19:26:06 +02:00
end .
select_type ( ServerHost , Host , Node ) - >
2008-12-09 00:28:50 +01:00
select_type ( ServerHost , Host , Node , hd ( plugins ( ServerHost ) ) ) .
2008-04-02 11:06:28 +02:00
2007-12-01 06:16:30 +01:00
features ( ) - >
[
2009-05-29 04:14:07 +02:00
% see plugin "access-authorize", % OPTIONAL
2009-05-29 02:27:26 +02:00
" access-open " , % OPTIONAL this relates to access_model option in node_hometree
2008-07-03 11:56:31 +02:00
" access-presence " , % OPTIONAL this relates to access_model option in node_pep
2009-01-03 01:25:40 +01:00
%TODO "access-roster", % OPTIONAL
2009-01-03 01:55:05 +01:00
" access-whitelist " , % OPTIONAL
2007-12-01 06:16:30 +01:00
% see plugin "auto-create", % OPTIONAL
% see plugin "auto-subscribe", % RECOMMENDED
" collections " , % RECOMMENDED
" config-node " , % RECOMMENDED
" create-and-configure " , % RECOMMENDED
% see plugin "create-nodes", % RECOMMENDED
2009-02-09 14:37:58 +01:00
% see plugin "delete-items", % RECOMMENDED
2007-12-01 06:16:30 +01:00
% see plugin "delete-nodes", % RECOMMENDED
% see plugin "filtered-notifications", % RECOMMENDED
2009-07-20 10:53:56 +02:00
% see plugin "get-pending", % OPTIONAL
2007-12-01 06:16:30 +01:00
% see plugin "instant-nodes", % RECOMMENDED
2008-07-03 11:56:31 +02:00
" item-ids " , % RECOMMENDED
2007-12-01 06:16:30 +01:00
" last-published " , % RECOMMENDED
%TODO "cache-last-item",
%TODO "leased-subscription", % OPTIONAL
% see plugin "manage-subscriptions", % OPTIONAL
2009-01-03 01:55:05 +01:00
" member-affiliation " , % RECOMMENDED
2007-12-01 06:16:30 +01:00
%TODO "meta-data", % RECOMMENDED
% see plugin "modify-affiliations", % OPTIONAL
2009-06-15 15:45:40 +02:00
% see plugin "multi-collection", % OPTIONAL
2009-05-29 04:14:07 +02:00
% see plugin "multi-subscribe", % OPTIONAL
2007-12-01 06:16:30 +01:00
% see plugin "outcast-affiliation", % RECOMMENDED
% see plugin "persistent-items", % RECOMMENDED
" presence-notifications " , % OPTIONAL
" presence-subscribe " , % RECOMMENDED
% see plugin "publish", % REQUIRED
%TODO "publish-options", % OPTIONAL
" publisher-affiliation " , % RECOMMENDED
% see plugin "purge-nodes", % OPTIONAL
% see plugin "retract-items", % OPTIONAL
% see plugin "retrieve-affiliations", % RECOMMENDED
" retrieve-default " % RECOMMENDED
% see plugin "retrieve-items", % RECOMMENDED
% see plugin "retrieve-subscriptions", % RECOMMENDED
2009-05-29 04:14:07 +02:00
%TODO "shim", % OPTIONAL
2007-12-01 06:16:30 +01:00
% see plugin "subscribe", % REQUIRED
2009-05-29 04:14:07 +02:00
% see plugin "subscription-options", % OPTIONAL
2007-12-01 06:16:30 +01:00
% see plugin "subscription-notifications" % OPTIONAL
] .
features ( Type ) - >
Module = list_to_atom ( ? PLUGIN_PREFIX ++ Type ) ,
features ( ) ++ case catch Module : features ( ) of
{ 'EXIT' , { undef , _ } } - > [ ] ;
Result - > Result
end .
2009-11-04 20:54:15 +01:00
features ( Host , < < > > ) - >
2008-04-02 11:06:28 +02:00
lists : usort ( lists : foldl ( fun ( Plugin , Acc ) - >
Acc ++ features ( Plugin )
end , [ ] , plugins ( Host ) ) ) ;
2007-12-01 06:16:30 +01:00
features ( Host , Node ) - >
2009-04-30 07:18:06 +02:00
Action = fun ( #pubsub_node { type = Type } ) - > { result , features ( Type ) } end ,
case transaction ( Host , Node , Action , sync_dirty ) of
{ result , Features } - > lists : usort ( features ( ) ++ Features ) ;
_ - > features ( )
end .
2007-12-01 06:16:30 +01:00
%% @doc <p>node tree plugin call.</p>
tree_call ( { _ User , Server , _ Resource } , Function , Args ) - >
tree_call ( Server , Function , Args ) ;
tree_call ( Host , Function , Args ) - >
2009-04-30 07:18:06 +02:00
? DEBUG ( " tree_call ~p ~p ~p " , [ Host , Function , Args ] ) ,
2009-08-28 18:22:37 +02:00
Module = case catch ets : lookup ( gen_mod : get_module_proc ( Host , config ) , nodetree ) of
2007-12-08 16:24:38 +01:00
[ { nodetree , N } ] - > N ;
2009-02-19 12:25:56 +01:00
_ - > list_to_atom ( ? TREE_PREFIX ++ ? STDTREE )
2007-12-08 16:24:38 +01:00
end ,
2007-12-01 06:16:30 +01:00
catch apply ( Module , Function , Args ) .
tree_action ( Host , Function , Args ) - >
2009-05-26 23:50:13 +02:00
? DEBUG ( " tree_action ~p ~p ~p " , [ Host , Function , Args ] ) ,
2007-12-01 06:16:30 +01:00
Fun = fun ( ) - > tree_call ( Host , Function , Args ) end ,
catch mnesia : sync_dirty ( Fun ) .
%% @doc <p>node plugin call.</p>
node_call ( Type , Function , Args ) - >
2009-04-30 07:18:06 +02:00
? DEBUG ( " node_call ~p ~p ~p " , [ Type , Function , Args ] ) ,
2007-12-01 06:16:30 +01:00
Module = list_to_atom ( ? PLUGIN_PREFIX ++ Type ) ,
case catch apply ( Module , Function , Args ) of
{ result , Result } - > { result , Result } ;
{ error , Error } - > { error , Error } ;
{ 'EXIT' , { undef , Undefined } } - >
case Type of
? STDNODE - > { error , { undef , Undefined } } ;
_ - > node_call ( ? STDNODE , Function , Args )
end ;
{ 'EXIT' , Reason } - > { error , Reason } ;
Result - > { result , Result } %% any other return value is forced as result
2005-07-20 05:09:34 +02:00
end .
2009-05-26 23:50:13 +02:00
node_action ( Host , Type , Function , Args ) - >
? DEBUG ( " node_action ~p ~p ~p ~p " , [ Host , Type , Function , Args ] ) ,
2007-12-01 06:16:30 +01:00
transaction ( fun ( ) - >
node_call ( Type , Function , Args )
end , sync_dirty ) .
%% @doc <p>plugin transaction handling.</p>
transaction ( Host , Node , Action , Trans ) - >
transaction ( fun ( ) - >
case tree_call ( Host , get_node , [ Host , Node ] ) of
2009-04-30 07:18:06 +02:00
N when is_record ( N , pubsub_node ) - >
case Action ( N ) of
{ result , Result } - > { result , { N , Result } } ;
{ atomic , { result , Result } } - > { result , { N , Result } } ;
Other - > Other
end ;
Error - >
Error
2007-12-01 06:16:30 +01:00
end
end , Trans ) .
transaction ( Fun , Trans ) - >
case catch mnesia : Trans ( Fun ) of
{ result , Result } - > { result , Result } ;
{ error , Error } - > { error , Error } ;
{ atomic , { result , Result } } - > { result , Result } ;
{ atomic , { error , Error } } - > { error , Error } ;
{ aborted , Reason } - >
? ERROR_MSG ( " transaction return internal error: ~p ~n " , [ { aborted , Reason } ] ) ,
{ error , ? ERR_INTERNAL_SERVER_ERROR } ;
{ 'EXIT' , Reason } - >
? ERROR_MSG ( " transaction return internal error: ~p ~n " , [ { 'EXIT' , Reason } ] ) ,
{ error , ? ERR_INTERNAL_SERVER_ERROR } ;
Other - >
? ERROR_MSG ( " transaction return internal error: ~p ~n " , [ Other ] ) ,
{ error , ? ERR_INTERNAL_SERVER_ERROR }
2005-04-17 20:08:34 +02:00
end .
2007-12-01 06:16:30 +01:00
%%%% helpers
%% Add pubsub-specific error element
extended_error ( Error , Ext ) - >
2009-02-27 04:09:00 +01:00
extended_error ( Error , Ext ,
[ { " xmlns " , ? NS_PUBSUB_ERRORS } ] ) .
2007-12-01 06:16:30 +01:00
extended_error ( Error , unsupported , Feature ) - >
extended_error ( Error , " unsupported " ,
[ { " xmlns " , ? NS_PUBSUB_ERRORS } ,
{ " feature " , Feature } ] ) ;
extended_error ( { xmlelement , Error , Attrs , SubEls } , Ext , ExtAttrs ) - >
{ xmlelement , Error , Attrs ,
lists : reverse ( [ { xmlelement , Ext , ExtAttrs , [ ] } | SubEls ] ) } .
%% Give a uniq identifier
uniqid ( ) - >
{ T1 , T2 , T3 } = now ( ) ,
lists : flatten ( io_lib : fwrite ( " ~.16B ~.16B ~.16B " , [ T1 , T2 , T3 ] ) ) .
2008-02-06 19:04:23 +01:00
2009-05-08 03:18:02 +02:00
% node attributes
2009-10-20 17:03:07 +02:00
nodeAttr ( Node ) when is_list ( Node ) - >
[ { " node " , Node } ] ;
2009-04-08 17:53:46 +02:00
nodeAttr ( Node ) - >
[ { " node " , node_to_string ( Node ) } ] .
% item attributes
itemAttr ( [ ] ) - > [ ] ;
itemAttr ( ItemId ) - > [ { " id " , ItemId } ] .
% build item elements from item list
itemsEls ( Items ) - >
lists : map ( fun ( #pubsub_item { itemid = { ItemId , _ } , payload = Payload } ) - >
{ xmlelement , " item " , itemAttr ( ItemId ) , Payload }
end , Items ) .
2009-06-15 15:45:40 +02:00
2009-09-24 21:28:43 +02:00
add_message_type ( { xmlelement , " message " , Attrs , Els } , Type ) - >
{ xmlelement , " message " , [ { " type " , Type } | Attrs ] , Els } ;
add_message_type ( XmlEl , _ Type ) - >
XmlEl .
2010-01-12 11:33:39 +01:00
%% Place of <headers/> changed at the bottom of the stanza
%% cf. http://xmpp.org/extensions/xep-0060.html#publisher-publish-success-subid
%%
%% "[SHIM Headers] SHOULD be included after the event notification information
%% (i.e., as the last child of the <message/> stanza)".
2010-03-05 17:52:41 +01:00
add_shim_headers ( Stanza , HeaderEls ) - >
add_headers ( Stanza , " headers " , ? NS_SHIM , HeaderEls ) .
add_extended_headers ( Stanza , HeaderEls ) - >
add_headers ( Stanza , " addresses " , ? NS_ADDRESS , HeaderEls ) .
add_headers ( { xmlelement , Name , Attrs , Els } , HeaderName , HeaderNS , HeaderEls ) - >
HeaderEl = { xmlelement , HeaderName , [ { " xmlns " , HeaderNS } ] , HeaderEls } ,
2010-01-12 11:33:39 +01:00
{ xmlelement , Name , Attrs , lists : append ( Els , [ HeaderEl ] ) } .
2009-06-15 15:45:40 +02:00
2010-01-12 11:33:39 +01:00
%% Removed multiple <header name=Collection>Foo</header/> elements
%% Didn't seem compliant, but not sure. Confirmation required.
%% cf. http://xmpp.org/extensions/xep-0248.html#notify
%%
%% "If an item is published to a node which is also included by a collection,
%% and an entity is subscribed to that collection with a subscription type of
%% "items" (Is there a way to check that currently ?), then the notifications
%% generated by the service MUST contain additional information. The <items/>
%% element contained in the notification message MUST specify the node
%% identifier of the node that generated the notification (not the collection)
%% and the <item/> element MUST contain a SHIM header that specifies the node
%% identifier of the collection".
collection_shim ( Node ) - >
2009-06-15 15:45:40 +02:00
[ { xmlelement , " header " , [ { " name " , " Collection " } ] ,
2010-01-12 11:33:39 +01:00
[ { xmlcdata , node_to_string ( Node ) } ] } ] .
subid_shim ( SubIDs ) - >
[ { xmlelement , " header " , [ { " name " , " SubID " } ] ,
[ { xmlcdata , SubID } ] } | | SubID < - SubIDs ] .
2010-03-05 17:52:41 +01:00
%% The argument is a list of Jids because this function could be used
%% with the 'pubsub#replyto' (type=jid-multi) node configuration.
extended_headers ( Jids ) - >
[ { xmlelement , " address " , [ { " type " , " replyto " } , { " jid " , Jid } ] , [ ] } | | Jid < - Jids ] .
2010-03-09 06:03:27 +01:00
feature_check_packet ( allow , _ User , Server , Pres ,
{ #jid { lserver = LServer } , _ To ,
{ xmlelement , " message " , MsgAttrs , _ } = El } , in ) - >
2010-03-05 11:35:20 +01:00
Host = host ( Server ) ,
case LServer of
%% If the sender Server equals Host, the message comes from the Pubsub server
Host - >
allow ;
%% Else, the message comes from PEP
_ - >
case xml : get_subtag ( El , " event " ) of
{ xmlelement , _ , Attrs , _ } = EventEl - >
case xml : get_attr_s ( " xmlns " , Attrs ) of
? NS_PUBSUB_EVENT - >
2010-03-09 06:03:27 +01:00
case xml : get_attr_s ( " type " , MsgAttrs ) of
" error " - >
%% Filter error-repsonse of PEP message
%% to avoid routing it to client
deny ;
_ when Pres / = undefined - >
%% Yes, sometimes Pres = undefined,
%% very rare though.
%% Seems like this is a bug: should
%% be fixed in ejabberd_s2s.erl
Feature = xml : get_path_s (
EventEl , [ { elem , " items " } ,
{ attr , " node " } ] ) ,
case is_feature_supported ( Pres , Feature ) of
true - >
allow ;
false - >
deny
end ;
_ - >
allow
2010-03-05 11:35:20 +01:00
end ;
_ - >
allow
end ;
_ - >
allow
end
2010-03-05 10:34:15 +01:00
end ;
feature_check_packet ( Acc , _ User , _ Server , _ Pres , _ Packet , _ Direction ) - >
2010-03-05 11:35:20 +01:00
Acc .
2010-03-05 10:34:15 +01:00
is_feature_supported ( { xmlelement , " presence " , _ , Els } , Feature ) - >
2010-03-05 11:35:20 +01:00
case mod_caps : read_caps ( Els ) of
nothing - > false ;
Caps - > lists : member ( Feature ++ " +notify " , mod_caps : get_features ( Caps ) )
end .
2010-03-05 11:11:44 +01:00
on_user_offline ( _ , JID , _ ) - >
2010-03-05 11:35:20 +01:00
{ User , Server , Resource } = jlib : jid_tolower ( JID ) ,
case ejabberd_sm : get_user_resources ( User , Server ) of
[ ] - > purge_offline ( { User , Server , Resource } ) ;
_ - > true
end .
2010-03-05 11:11:44 +01:00
purge_offline ( { User , Server , _ } = LJID ) - >
2010-03-05 11:35:20 +01:00
Host = host ( element ( 2 , LJID ) ) ,
Plugins = plugins ( Host ) ,
Result = lists : foldl (
fun ( Type , { Status , Acc } ) - >
case lists : member ( " retrieve-affiliations " , features ( Type ) ) of
false - >
{ { error , extended_error ( 'feature-not-implemented' , unsupported , " retrieve-affiliations " ) } , Acc } ;
true - >
{ result , Affiliations } = node_action ( Host , Type , get_entity_affiliations , [ Host , LJID ] ) ,
{ Status , [ Affiliations | Acc ] }
end
end , { ok , [ ] } , Plugins ) ,
case Result of
{ ok , Affiliations } - >
lists : foreach (
2010-03-05 11:53:26 +01:00
fun ( { #pubsub_node { nodeid = { _ , NodeId } , options = Options , type = Type } , Affiliation } )
2010-03-05 11:35:20 +01:00
when Affiliation == 'owner' orelse Affiliation == 'publisher' - >
2010-03-05 11:53:26 +01:00
Action = fun ( #pubsub_node { type = NType , id = NodeIdx } ) - >
node_call ( NType , get_items , [ NodeIdx , service_jid ( Host ) ] )
2010-03-05 11:35:20 +01:00
end ,
case transaction ( Host , NodeId , Action , sync_dirty ) of
{ result , { _ , [ ] } } - >
true ;
{ result , { _ , Items } } - >
Features = features ( Type ) ,
case
{ lists : member ( " retract-items " , Features ) ,
lists : member ( " persistent-items " , Features ) ,
get_option ( Options , persist_items ) ,
get_option ( Options , purge_offline ) }
of
{ true , true , true , true } - >
ForceNotify = get_option ( Options , notify_retract ) ,
lists : foreach (
fun ( #pubsub_item { itemid = { ItemId , _ } , modification = { _ , Modification } } ) - >
case Modification of
{ User , Server , _ } - >
delete_item ( Host , NodeId , LJID , ItemId , ForceNotify ) ;
_ - >
true
end ;
( _ ) - >
true
end , Items ) ;
_ - >
true
end ;
Error - >
Error
2010-03-05 11:53:26 +01:00
end ;
2010-03-05 11:35:20 +01:00
( _ ) - >
true
end , lists : usort ( lists : flatten ( Affiliations ) ) ) ;
{ Error , _ } - >
? DEBUG ( " on_user_offline ~p " , [ Error ] )
end .