From 14399e3d57fbdf7211dc6960d22ebc853e3249a0 Mon Sep 17 00:00:00 2001
From: Badlop ejabberd is a free and open source instant messaging server written in Erlang. ejabberd is cross-platform, distributed, fault-tolerant, and based on open standards to achieve real-time communication. ejabberd is designed to be a rock-solid and feature rich XMPP server. ejabberd is suitable for small deployments, whether they need to be scalable or not, as well as extremely big deployments. This module implements offline message storage. This means that all messages
+ This module implements offline message storage (XEP-0160).
+This means that all messages
sent to an offline user will be stored on the server until that user comes
online again. Thus it is very similar to how email works. Note that
ejabberdctl has a command to delete expired messages
@@ -2535,8 +2538,36 @@ and all the other users up to 100.
{mod_offline, [ {access_max_user_messages, max_user_offline_messages} ]},
...
]}.
+ This module implements support for XMPP Ping (XEP-0199) and periodic keepalives.
+When this module is enabled ejabberd responds correctly to
+ping requests, as defined in the protocol. Configuration options:
+ This example enables Ping responses, configures the module to send pings
+to client connections that are inactive for 4 minutes,
+and if a client does not answer to the ping in less than 32 seconds, its connection is closed:
+ This module implements Blocking Communication (also known as Privacy Rules)
as defined in section 10 from XMPP IM. If end users have support for it in
their Jabber client, they will be able to:
@@ -2564,7 +2595,7 @@ subscription type (or globally).
iqdisc This module adds support for Private XML Storage (XEP-0049):
This module implements SOCKS5 Bytestreams (XEP-0065).
It allows ejabberd to act as a file transfer proxy between two
XMPP clients. Options:
@@ -2631,7 +2662,7 @@ The simpliest configuration of the module:
...
]}.
This module offers a Publish-Subscribe Service (XEP-0060).
The functionality in mod_pubsub can be extended using plugins.
The plugin that implements PEP (Personal Eventing via Pubsub) (XEP-0163)
@@ -2677,7 +2708,7 @@ The following example will use node_tune instead of node_pep for every PEP node
...
]}.
This module adds support for In-Band Registration (XEP-0077). This protocol
enables end users to use a Jabber client to:
This module implements roster management as defined in RFC 3921: XMPP IM. Options:
This module adds support for logging end user packets via a Jabber message
auditing service such as
Bandersnatch. All user
@@ -2786,7 +2817,7 @@ To log all end user packets to the Bandersnatch service running on
...
]}.
This module enables you to create shared roster groups. This means that you can
create groups of people that can see members from (other) groups in their
rosters. The big advantages of this feature are that end users do not need to
@@ -2861,7 +2892,7 @@ roster groups as shown in the following table:
This module adds support for Statistics Gathering (XEP-0039). This protocol
allows you to retrieve next statistics from your ejabberd deployment:
This module features support for Entity Time (XEP-0202). By using this XEP,
you are able to discover the time at another entity’s location. Options:
This module allows end users to store and retrieve their vCard, and to retrieve
other users vCards, as defined in vcard-temp (XEP-0054). The module also
implements an uncomplicated Jabber User Directory based on the vCards of
@@ -2955,7 +2986,7 @@ and that all virtual hosts will be searched instead of only the current one:
...
]}.
ejabberd can map LDAP attributes to vCard fields. This behaviour is
implemented in the mod_vcard_ldap module. This module does not depend on the
authentication method (see 3.2.5). Note that ejabberd treats LDAP as a read-only storage:
@@ -3131,7 +3162,7 @@ searching his info in LDAP. This module implements Software Version (XEP-0092). Consequently, it
answers ejabberd’s version when queried. Options:
With the ejabberdctl command line administration script
+ With the ejabberdctl command line administration script
you can execute ejabberdctl commands (described in the next section, 4.1.1)
and also many general ejabberd commands (described in section 4.2).
This means you can start, stop and perform many other administrative tasks
@@ -3153,7 +3184,7 @@ and other codes may be used for specific results.
This can be used by other scripts to determine automatically
if a command succeeded or failed,
for example using: echo $? When ejabberdctl is executed without any parameter,
+ When ejabberdctl is executed without any parameter,
it displays the available options. If there isn’t an ejabberd server running,
the available parameters are:
ejabberd is an Erlang/OTP application that runs inside an Erlang runtime system.
+ ejabberd is an Erlang/OTP application that runs inside an Erlang runtime system.
This system is configured using environment variables and command line parameters.
The ejabberdctl administration script uses many of those possibilities.
You can configure some of them with the file ejabberdctl.cfg,
@@ -3262,7 +3293,7 @@ Starts the Erlang system detached from the system console.
Note that some characters need to be escaped when used in shell scripts, for instance An ejabberd command is an abstract function identified by a name,
+ An ejabberd command is an abstract function identified by a name,
with a defined number and type of calling arguments and type of result
that is registered in the ejabberd_commands service.
Those commands can be defined in any Erlang module and executed using any valid frontend. ejabberd includes a frontend to execute ejabberd commands: the script ejabberdctl.
@@ -3270,7 +3301,7 @@ Other known frontends that can be installed to execute ejabberd commands in diff
ejabberd_xmlrpc (XML-RPC service),
mod_rest (HTTP POST service),
mod_shcommands (ejabberd WebAdmin page). ejabberd includes a few ejabberd Commands by default.
+ ejabberd includes a few ejabberd Commands by default.
When more modules are installed, new commands may be available in the frontends. The easiest way to get a list of the available commands, and get help for them is to use
the ejabberdctl script:
The frontends can be configured to restrict access to certain commands.
+ The frontends can be configured to restrict access to certain commands.
In that case, authentication information must be provided.
In each frontend the AccessCommands option is defined
in a different place. But in all cases the option syntax is the same:
@@ -3356,7 +3387,7 @@ and the provided arguments do not contradict Arguments. As an example to u
{_bot_reg_test, [register, unregister], [{host, "test.org"}]}
]
The ejabberd Web Admin allows to administer most of ejabberd using a web browser. This feature is enabled by default:
a ejabberd_http listener with the option web_admin (see
section 3.1.3) is included in the listening ports. Then you can open
@@ -3428,13 +3459,13 @@ The file is searched by default in
The directory of the documentation can be specified in
the environment variable EJABBERD_DOC_PATH.
See section 4.1.2. If you enable mod_configure and mod_adhoc,
+ If you enable mod_configure and mod_adhoc,
you can perform several administrative tasks in ejabberd
with a Jabber client.
The client must support Ad-Hoc Commands (XEP-0050),
and you must login in the Jabber server with
an account with proper privileges. ejabberd uses the distributed Mnesia database.
+ ejabberd uses the distributed Mnesia database.
Being distributed, Mnesia enforces consistency of its file,
so it stores the name of the Erlang node in it (see section 5.4).
The name of an Erlang node includes the hostname of the computer.
@@ -3471,8 +3502,8 @@ mv /var/lib/ejabberd/*.* /var/lib/ejabberd/oldfiles/
You need to take the following TCP ports in mind when configuring your firewall:
epmd (Erlang Port Mapper Daemon)
+ epmd (Erlang Port Mapper Daemon)
is a small name server included in Erlang/OTP
and used by Erlang programs when establishing distributed Erlang communications.
ejabberd needs epmd to use ejabberdctl and also when clustering ejabberd nodes.
@@ -3508,7 +3539,7 @@ but can be configured in the file ejabberdctl.cfg.
The Erlang command-line parameter used internally is, for example:
The Erlang cookie is a string with numbers and letters.
+ The Erlang cookie is a string with numbers and letters.
An Erlang node reads the cookie at startup from the command-line parameter -setcookie.
If not indicated, the cookie is read from the cookie file $HOME/.erlang.cookie.
If this file does not exist, it is created immediately with a random cookie.
@@ -3522,7 +3553,7 @@ to prevent unauthorized access or intrusion to an Erlang node.
The communication between Erlang nodes are not encrypted,
so the cookie could be read sniffing the traffic on the network.
The recommended way to secure the Erlang node is to block the port 4369. An Erlang node may have a node name.
+ An Erlang node may have a node name.
The name can be short (if indicated with the command-line parameter -sname)
or long (if indicated with the parameter -name).
Starting an Erlang node with -sname limits the communication between Erlang nodes to the LAN. Using the option -sname instead of -name is a simple method
@@ -3531,7 +3562,7 @@ However, it is not ultimately effective to prevent access to the Erlang node,
because it may be possible to fake the fact that you are on another network
using a modified version of Erlang epmd.
The recommended way to secure the Erlang node is to block the port 4369. ejabberd stores sensible data in the file system either in plain text or binary files.
+ ejabberd stores sensible data in the file system either in plain text or binary files.
The file system permissions should be set to only allow the proper user to read,
write and execute those files and directories. A Jabber domain is served by one or more ejabberd nodes. These nodes can
be run on different machines that are connected via a network. They all
must have the ability to connect to port 4369 of all another nodes, and must
@@ -3567,29 +3598,29 @@ router,
This module is the main router of Jabber packets on each node. It
routes them based on their destination’s domains. It uses a global
routing table. The domain of the packet’s destination is searched in the
routing table, and if it is found, the packet is routed to the
appropriate process. If not, it is sent to the s2s manager. This module routes packets which have a destination domain equal to
one of this server’s host names. If the destination JID has a non-empty user
part, it is routed to the session manager, otherwise it is processed depending
on its content. This module routes packets to local users. It looks up to which user
resource a packet must be sent via a presence table. Then the packet is
either routed to the appropriate c2s process, or stored in offline
storage, or bounced back. This module routes packets to other Jabber servers. First, it
checks if an opened s2s connection from the domain of the packet’s
source to the domain of the packet’s destination exists. If that is the case,
the s2s manager routes the packet to the process
serving this connection, otherwise a new connection is opened. Suppose you already configured ejabberd on one machine named (first),
and you need to setup another one to make an ejabberd cluster. Then do
following steps: You can repeat these steps for other machines supposed to serve this
domain. ejabberd includes an algorithm to load balance the components that are plugged on an ejabberd cluster. It means that you can plug one or several instances of the same component on each ejabberd cluster and that the traffic will be automatically distributed. The default distribution algorithm try to deliver to a local instance of a component. If several local instances are available, one instance is chosen randomly. If no instance is available locally, one instance is chosen randomly among the remote component instances. If you need a different behaviour, you can change the load balancing behaviour with the option domain_balancing. The syntax of the option is the following: Several balancing criteria are available:
Chapter 1 Introduction
ejabberdctl register admin1 example.org FgT5bk3
-
{acl, admins, {user, "admin1", "example.org"}}.
@@ -1786,6 +1787,7 @@ all entries end with a comma:
mod_muc_log Multi-User Chat room logging mod_muc mod_offline Offline message storage (XEP-0160)
+mod_offline_odbc Offline message storage (XEP-0160) supported DB (*) mod_ping XMPP Ping and periodic keepalives (XEP-0199) mod_privacy Blocking Communication (XMPP IM) mod_privacy_odbc Blocking Communication (XMPP IM) supported DB (*)
@@ -2506,7 +2508,8 @@ top link will be the default mod_private Private XML Storage (XEP-0049) <a href="/">Home</a>
.
]}.
3.3.11 mod_offline
3.3.12 mod_ping
{modules,
+ [
+ ...
+ {mod_ping, [{send_pings, true}, {ping_interval, 240}, {timeout_action, kill}]},
+ ...
+ ]}.
-3.3.12 mod_privacy
3.3.13 mod_privacy
3.3.13 mod_private
3.3.14 mod_private
Using this method, Jabber entities can store private data on the server and
@@ -2576,7 +2607,7 @@ of client-specific preferences; another is Bookmark Storage ( This specifies
the processing discipline for Private XML Storage (jabber:iq:private) IQ queries (see section 3.3.2).
-
-3.3.14 mod_proxy65
3.3.15 mod_proxy65
3.3.15 mod_pubsub
3.3.16 mod_pubsub
3.3.16 mod_register
3.3.17 mod_register
-3.3.17 mod_roster
3.3.18 mod_roster
-3.3.18 mod_service_log
3.3.19 mod_service_log
3.3.19 mod_shared_roster
3.3.20 mod_shared_roster
3.3.20 mod_stats
3.3.21 mod_stats
-3.3.21 mod_time
3.3.22 mod_time
-3.3.22 mod_vcard
3.3.23 mod_vcard
3.3.23 mod_vcard_ldap
3.3.24 mod_vcard_ldap
3.3.24 mod_version
3.3.25 mod_version
-Chapter 4 Managing an ejabberd Server
-4.1 ejabberdctl
Chapter 4 Managing an ejabberd Server
+4.1 ejabberdctl
4.1.1 ejabberdctl Commands
4.1.1 ejabberdctl Commands
4.1.2 Erlang Runtime System
4.1.2 Erlang Runtime System
"
and {}
.
You can find other options in the Erlang manual page (erl -man erl).4.2 ejabberd Commands
4.2 ejabberd Commands
4.2.1 List of ejabberd Commands
4.2.1 List of ejabberd Commands
$ ejabberdctl help
@@ -3310,7 +3341,7 @@ exist tutorials to migrate
in offline storage. This might be useful when the number of offline messages
is very high.
-
-4.2.2 Restrict Execution with AccessCommands
4.2.2 Restrict Execution with AccessCommands
4.3 Web Admin
4.3 Web Admin
4.4 Ad-hoc Commands
4.4 Ad-hoc Commands
4.5 Change Computer Hostname
4.5 Change Computer Hostname
Chapter 5 Securing ejabberd
-5.1 Firewall Settings
Chapter 5 Securing ejabberd
+5.1 Firewall Settings
-
@@ -3483,7 +3514,7 @@ After you finish, remember to delete the temporary backup files from public dire
Port Description port range Used for connections between Erlang nodes. This range is configurable (see section 5.2). 5.2 epmd
5.2 epmd
erl ... -kernel inet_dist_listen_min 4370 inet_dist_listen_max 4375
-5.3 Erlang Cookie
5.3 Erlang Cookie
5.4 Erlang Node Name
5.4 Erlang Node Name
5.5 Securing Sensible Files
5.5 Securing Sensible Files
-Chapter 6 Clustering
Chapter 6 Clustering
-6.1 How it Works
6.1 How it Works
6.1.1 Router
6.1.1 Router
6.1.2 Local Router
6.1.2 Local Router
6.1.3 Session Manager
6.1.3 Session Manager
6.1.4 s2s Manager
6.1.4 s2s Manager
6.2 Clustering Setup
6.2 Clustering Setup
access
’ options because they will be taken from
enabled only on one machine in the cluster.
6.3 Service Load-Balancing
6.3 Service Load-Balancing
-6.3.1 Components Load-Balancing
-6.3.2 Domain Load-Balancing Algorithm
6.3.1 Components Load-Balancing
+6.3.2 Domain Load-Balancing Algorithm
{domain_balancing, "component.example.com", <balancing_criterium>}.
When there is a risk of failure for a given component, domain balancing can cause service trouble. If one component is failing the service will not work correctly unless the sessions are rebalanced.
In this case, it is best to limit the problem to the sessions handled by the failing component. This is what the domain_balancing_component_number option does, making the load balancing algorithm not dynamic, but sticky on a fix number of component instances.
The syntax is the following:
{domain_balancing_component_number, "component.example.com", N}-
An ejabberd node writes two log files: +
An ejabberd node writes two log files:
The Debug Console is an Erlang shell attached to an already running ejabberd server. +
The Debug Console is an Erlang shell attached to an already running ejabberd server. With this Erlang shell, an experienced administrator can perform complex tasks.
This shell gives complete control over the ejabberd server, so it is important to use it with extremely care. There are some simple and safe examples in the article Interconnecting Erlang Nodes
To exit the shell, close the window or press the keys: control+c control+c.
-ejabberd includes a watchdog mechanism that may be useful to developers when troubleshooting a problem related to memory usage. If a process in the ejabberd server consumes more memory than the configured threshold, @@ -3690,7 +3721,7 @@ or in a conversation with the watchdog alert bot.
Example configuration: To remove all watchdog admins, set the option with an empty list:
{watchdog_admins, []}.-
The source code of ejabberd supports localization. The translators can edit the gettext .po files @@ -3725,9 +3756,9 @@ HTTP header ‘Accept-Language: ru’
Release notes are available from ejabberd Home Page
-Thanks to all people who contributed to this guide: +
Thanks to all people who contributed to this guide:
Ejabberd Installation and Operation Guide.
+
Ejabberd Installation and Operation Guide.
Copyright © 2003 — 2009 ProcessOne
This document is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
diff --git a/doc/guide.tex b/doc/guide.tex
index fbd4d0c49..ecdc6cd7b 100644
--- a/doc/guide.tex
+++ b/doc/guide.tex
@@ -76,6 +76,7 @@
\newcommand{\modmuclog}{\module{mod\_muc\_log}}
\newcommand{\modoffline}{\module{mod\_offline}}
\newcommand{\modofflineodbc}{\module{mod\_offline\_odbc}}
+\newcommand{\modping}{\module{mod\_ping}}
\newcommand{\modprivacy}{\module{mod\_privacy}}
\newcommand{\modprivacyodbc}{\module{mod\_privacy\_odbc}}
\newcommand{\modprivate}{\module{mod\_private}}
@@ -2375,6 +2376,7 @@ The following table lists all modules included in \ejabberd{}.
\hline \ahrefloc{modmuclog}{\modmuclog{}} & Multi-User Chat room logging & \modmuc{} \\
\hline \ahrefloc{modoffline}{\modoffline{}} & Offline message storage (\xepref{0160}) & \\
\hline \ahrefloc{modoffline}{\modofflineodbc{}} & Offline message storage (\xepref{0160}) & supported DB (*) \\
+ \hline \ahrefloc{modping}{\modping{}} & XMPP Ping and periodic keepalives (\xepref{0199}) & \\
\hline \ahrefloc{modprivacy}{\modprivacy{}} & Blocking Communication (XMPP IM) & \\
\hline \ahrefloc{modprivacy}{\modprivacyodbc{}} & Blocking Communication (XMPP IM) & supported DB (*) \\
\hline \ahrefloc{modprivate}{\modprivate{}} & Private XML Storage (\xepref{0049}) & \\
@@ -3224,7 +3226,8 @@ Examples:
\makesubsection{modoffline}{\modoffline{}}
\ind{modules!\modoffline{}}
-This module implements offline message storage. This means that all messages
+This module implements offline message storage (\xepref{0160}).
+This means that all messages
sent to an offline user will be stored on the server until that user comes
online again. Thus it is very similar to how email works. Note that
\term{ejabberdctl}\ind{ejabberdctl} has a command to delete expired messages
@@ -3260,6 +3263,43 @@ and all the other users up to 100.
]}.
\end{verbatim}
+\makesubsection{modping}{\modping{}}
+\ind{modules!\modping{}}
+
+This module implements support for XMPP Ping (\xepref{0199}) and periodic keepalives.
+When this module is enabled ejabberd responds correctly to
+ping requests, as defined in the protocol.
+
+Configuration options:
+\begin{description}
+ \titem{\{send\_pings, true | false\}}\ind{options!send\_pings}
+ If this option is set to \term{true}, the server sends pings to connected clients
+ that are not active in a given interval \term{ping\_interval}.
+ This is useful to keep client connections alive or checking availability.
+ By default this option is disabled.
+ % because it is mostly not needed and consumes resources.
+ \titem{\{ping\_interval, Seconds\}}\ind{options!ping\_interval}
+ How often to send pings to connected clients, if the previous option is enabled.
+ If a client connection does not send or receive any stanza in this interval,
+ a ping request is sent to the client.
+ The default value is 60 seconds.
+ \titem{\{timeout\_action, none | kill\}}\ind{options!timeout\_action}
+ What to do when a client does not answer to a server ping request in less than 32 seconds.
+ % Those 32 seconds are defined in ejabberd_local.erl: -define(IQ_TIMEOUT, 32000).
+ The default is to do nothing.
+\end{description}
+
+This example enables Ping responses, configures the module to send pings
+to client connections that are inactive for 4 minutes,
+and if a client does not answer to the ping in less than 32 seconds, its connection is closed:
+\begin{verbatim}
+{modules,
+ [
+ ...
+ {mod_ping, [{send_pings, true}, {ping_interval, 240}, {timeout_action, kill}]},
+ ...
+ ]}.
+\end{verbatim}
\makesubsection{modprivacy}{\modprivacy{}}
\ind{modules!\modprivacy{}}\ind{Blocking Communication}\ind{Privacy Rules}\ind{protocols!RFC 3921: XMPP IM}
diff --git a/src/ejabberd.cfg.example b/src/ejabberd.cfg.example
index 07b9010c8..a23b714be 100644
--- a/src/ejabberd.cfg.example
+++ b/src/ejabberd.cfg.example
@@ -494,6 +494,7 @@
]},
%%{mod_muc_log,[]},
{mod_offline, [{access_max_user_messages, max_user_offline_messages}]},
+ {mod_ping, []},
{mod_privacy, []},
{mod_private, []},
%%{mod_proxy65,[]},
diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl
index a3af8205a..f77c35a9d 100644
--- a/src/ejabberd_c2s.erl
+++ b/src/ejabberd_c2s.erl
@@ -32,6 +32,7 @@
%% External exports
-export([start/2,
+ stop/1,
start_link/2,
send_text/2,
send_element/2,
@@ -150,6 +151,9 @@ get_presence(FsmRef) ->
get_state(FsmRef) ->
gen_fsm:sync_send_all_state_event(FsmRef, get_state, 1000).
+stop(FsmRef) ->
+ gen_fsm:send_event(FsmRef, closed).
+
%%%----------------------------------------------------------------------
%%% Callback functions from gen_fsm
%%%----------------------------------------------------------------------
diff --git a/src/ejabberd_local.erl b/src/ejabberd_local.erl
index 275449deb..e7a0014af 100644
--- a/src/ejabberd_local.erl
+++ b/src/ejabberd_local.erl
@@ -33,6 +33,8 @@
-export([start_link/0]).
-export([route/3,
+ route_iq/4,
+ process_iq_reply/3,
register_iq_handler/4,
register_iq_handler/5,
register_iq_response_handler/4,
@@ -52,10 +54,13 @@
-record(state, {}).
--record(iq_response, {id, module, function}).
+-record(iq_response, {id, module, function, timer}).
-define(IQTABLE, local_iqtable).
+%% This value is used in SIP and Megaco for a transaction lifetime.
+-define(IQ_TIMEOUT, 32000).
+
% These are the namespace already declared by the stream opening. This is
% used at serialization time.
-define(DEFAULT_NS, ?NS_JABBER_CLIENT).
@@ -94,37 +99,27 @@ process_iq(From, To, Packet) ->
Err = exmpp_iq:error(Packet, 'feature-not-implemented'),
ejabberd_router:route(To, From, Err)
end;
- #iq{kind = response} = IQ_Rec ->
- process_iq_reply(From, To, IQ_Rec);
+ #iq{kind = response} = IQReply ->
+ %%IQReply = jlib:iq_query_or_response_info(IQ_Rec),
+ process_iq_reply(From, To, IQReply);
_ ->
Err = exmpp_iq:error(Packet, 'bad-request'),
ejabberd_router:route(To, From, Err),
ok
end.
-process_iq_reply(From, To, #iq{id = ID} = IQ_Rec) ->
- case catch mnesia:dirty_read(iq_response, ID) of
- [] ->
+process_iq_reply(From, To, #iq{id = ID} = IQ) ->
+ case get_iq_callback(ID) of
+ {ok, undefined, Function} ->
+ Function(IQ),
+ ok;
+ {ok, Module, Function} ->
+ Module:Function(From, To, IQ),
ok;
_ ->
- F = fun() ->
- case mnesia:read({iq_response, ID}) of
- [] ->
- nothing;
- [#iq_response{module = Module,
- function = Function}] ->
- mnesia:delete({iq_response, ID}),
- {Module, Function}
- end
- end,
- case mnesia:transaction(F) of
- {atomic, {Module, Function}} ->
- Module:Function(From, To, IQ_Rec);
- _ ->
- ok
- end
+ nothing
end.
-
+
route(FromOld, ToOld, #xmlelement{} = PacketOld) ->
catch throw(for_stacktrace), % To have a stacktrace.
io:format("~nLOCAL: old #xmlelement:~n~p~n~p~n~n",
@@ -144,8 +139,21 @@ route(From, To, Packet) ->
ok
end.
+route_iq(From, To, #iq{type = Type} = IQ, F) when is_function(F) ->
+ Packet = if Type == set; Type == get ->
+ ID = list_to_binary(randoms:get_string()),
+ Host = exmpp_jid:prep_domain(From),
+ register_iq_response_handler(Host, ID, undefined, F),
+ exmpp_iq:iq_to_xmlel(IQ#iq{id = ID});
+ true ->
+ exmpp_iq:iq_to_xmlel(IQ)
+ end,
+ ejabberd_router:route(From, To, Packet).
+
register_iq_response_handler(Host, ID, Module, Fun) ->
- ejabberd_local ! {register_iq_response_handler, Host, ID, Module, Fun}.
+ gen_server:call(ejabberd_local,
+ {register_iq_response_handler,
+ Host, ID, Module, Fun}).
register_iq_handler(Host, XMLNS, Module, Fun) ->
ejabberd_local ! {register_iq_handler, Host, XMLNS, Module, Fun}.
@@ -153,8 +161,9 @@ register_iq_handler(Host, XMLNS, Module, Fun) ->
register_iq_handler(Host, XMLNS, Module, Fun, Opts) ->
ejabberd_local ! {register_iq_handler, Host, XMLNS, Module, Fun, Opts}.
-unregister_iq_response_handler(Host, ID) ->
- ejabberd_local ! {unregister_iq_response_handler, Host, ID}.
+unregister_iq_response_handler(_Host, ID) ->
+ catch get_iq_callback(ID),
+ ok.
unregister_iq_handler(Host, XMLNS) ->
ejabberd_local ! {unregister_iq_handler, Host, XMLNS}.
@@ -186,6 +195,7 @@ init([]) ->
?MODULE, bounce_resource_packet, 100)
end, ?MYHOSTS),
catch ets:new(?IQTABLE, [named_table, public]),
+ update_table(),
mnesia:create_table(iq_response,
[{ram_copies, [node()]},
{attributes, record_info(fields, iq_response)}]),
@@ -201,6 +211,14 @@ init([]) ->
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
+handle_call({register_iq_response_handler, _Host,
+ ID, Module, Function}, _From, State) ->
+ TRef = erlang:start_timer(?IQ_TIMEOUT, self(), ID),
+ mnesia:dirty_write(#iq_response{id = ID,
+ module = Module,
+ function = Function,
+ timer = TRef}),
+ {reply, ok, State};
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
@@ -239,12 +257,6 @@ handle_info({route, From, To, Packet}, State) ->
ok
end,
{noreply, State};
-handle_info({register_iq_response_handler, _Host, ID, Module, Function}, State) ->
- mnesia:dirty_write(#iq_response{id = ID, module = Module, function = Function}),
- {noreply, State};
-handle_info({unregister_iq_response_handler, _Host, ID}, State) ->
- mnesia:dirty_delete({iq_response, ID}),
- {noreply, State};
handle_info({register_iq_handler, Host, XMLNS, Module, Function}, State) ->
ets:insert(?IQTABLE, {{XMLNS, Host}, Module, Function}),
catch mod_disco:register_feature(Host, XMLNS),
@@ -276,6 +288,9 @@ handle_info(refresh_iq_handlers, State) ->
end
end, ets:tab2list(?IQTABLE)),
{noreply, State};
+handle_info({timeout, _TRef, ID}, State) ->
+ process_iq_timeout(ID),
+ {noreply, State};
handle_info(_Info, State) ->
{noreply, State}.
@@ -330,3 +345,52 @@ do_route(From, To, Packet) ->
end
end.
+update_table() ->
+ case catch mnesia:table_info(iq_response, attributes) of
+ [id, module, function] ->
+ mnesia:delete_table(iq_response);
+ [id, module, function, timer] ->
+ ok;
+ {'EXIT', _} ->
+ ok
+ end.
+
+get_iq_callback(ID) ->
+ case mnesia:dirty_read(iq_response, ID) of
+ [#iq_response{module = Module, timer = TRef,
+ function = Function}] ->
+ cancel_timer(TRef),
+ mnesia:dirty_delete(iq_response, ID),
+ {ok, Module, Function};
+ _ ->
+ error
+ end.
+
+process_iq_timeout(ID) ->
+ spawn(fun process_iq_timeout/0) ! ID.
+
+process_iq_timeout() ->
+ receive
+ ID ->
+ case get_iq_callback(ID) of
+ {ok, undefined, Function} ->
+ Function(timeout);
+ _ ->
+ ok
+ end
+ after 5000 ->
+ ok
+ end.
+
+cancel_timer(TRef) ->
+ case erlang:cancel_timer(TRef) of
+ false ->
+ receive
+ {timeout, TRef, _} ->
+ ok
+ after 0 ->
+ ok
+ end;
+ _ ->
+ ok
+ end.
diff --git a/src/mod_ping.erl b/src/mod_ping.erl
new file mode 100644
index 000000000..e22894333
--- /dev/null
+++ b/src/mod_ping.erl
@@ -0,0 +1,243 @@
+%%%----------------------------------------------------------------------
+%%% File : mod_ping.erl
+%%% Author : Brian Cully