Ejabberd 1.0.0 Developers Guide
|
I can thoroughly recommend ejabberd for ease of setup –
Kevin Smith, Current maintainer of the Psi project
Contents
1 Introduction
ejabberd is a free (GPL) distributed fault-tolerant Jabber/XMPP server and is mainly written in Erlang.
ejabberd is designed to be a stable, standards compliant, and feature rich Jabber/XMPP server.
ejabberd is suitable for small servers, whether they need to be scalable or not, as well as extremely big servers.
1.1 Key Features
ejabberd is:
-
Multiplatform: ejabberd runs under Microsoft Windows and Unix derived systems such as Linux, FreeBSD and NetBSD.
- Distributed: You can run ejabberd on a cluster of machines and all of them will serve one Jabber domain. When you need more capacity you can simply add a new cheap node to your cluster. Accordingly, you do not need to buy an expensive high-end machine to support tens of thousands concurrent users.
- Fault-tolerant: You can deploy an ejabberd cluster so that all the information required for a properly working service will be replicated permanently on all nodes. This means that if one of the nodes crashes, the others will continue working without disruption. In addition, nodes also can be added or replaced “on the fly”.
- Administrator Friendly: ejabberd is built on top of the Open Source Erlang. As a result you do not need to install an external database, an external web server, amongst others because everything is already included, and ready to run out of the box. Other administrator benefits include:
-
Comprehensive documentation.
- Straightforward installers for Windows and Linux.
- Web interface for administration tasks.
- Shared Roster groups.
- Command line administration tool.
- Can integrate with existing authentication mechanisms.
- Capability to send announce messages.
- Internationalized: ejabberd leads in internationalization. Hence it is very well suited in a globalized world. Related features are:
-
Translated in 11 languages.
- Support for IDNA.
- Open Standards: ejabberd is the first Open Source Jabber server claiming to fully comply to the XMPP standard.
1.2 Additional Features
Besides common Jabber server features, ejabberd comes with a wide range of other features:
-
Modular: ejabberd's modular architecture allows easy customization:
-
Load only the modules you want.
- Extend ejabberd with your own custom modules.
- Security
-
SASL and STARTTLS for c2s and s2s connections.
- STARTTLS and Dialback s2s connections.
- Obsolete SSL for c2s connections also supported.
- Web interface accessible via HTTPS secure access.
- Databases
-
Native PostgreSQL support.
- Mnesia.
- ODBC data storage support.
- Authentication
-
LDAP.
- External Authentication script.
- Internal Authentication.
- The ability to interface via external components with networks such as:
- Others
2 How it works
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 have
the same magic cookie (see Erlang/OTP documentation, in other words the file
~ejabberd/.erlang.cookie must be the same on all nodes). This is
needed because all nodes exchange information about connected users, S2S
connections, registered services, etc...
Each ejabberd node have following modules:
-
router;
- local router.
- session manager;
- S2S manager;
2.1 Router
This module is the main router of Jabber packets on each node. It routes
them based on their destinations domains. It has two tables: local and global
routes. First, domain of packet destination searched in local table, and if it
found, then the packet is routed to appropriate process. If no, then it
searches in global table, and is routed to the appropriate ejabberd node or
process. If it does not exists in either tables, then it sent to the S2S
manager.
2.2 Local Router
This module routes packets which have a destination domain equal to this server
name. If destination JID has a non-empty user part, then it routed to the
session manager, else it is processed depending on it's content.
2.3 Session Manager
This module routes packets to local users. It searches for what user resource
packet must be sended via presence table. If this resource is connected to
this node, it is routed to C2S process, if it connected via another node, then
the packet is sent to session manager on that node.
2.4 S2S Manager
This module routes packets to other Jabber servers. First, it checks if an
open S2S connection from the domain of the packet source to the domain of
packet destination already exists. If it is open on another node, then it
routes the packet to S2S manager on that node, if it is open on this node, then
it is routed to the process that serves this connection, and if a connection
does not exist, then it is opened and registered.
3 XML representation
Each XML stanza is represented as the following tuple:
XMLElement = {xmlelement, Name, Attrs, [ElementOrCDATA]}
Name = string()
Attrs = [Attr]
Attr = {Key, Val}
Key = string()
Val = string()
ElementOrCDATA = XMLElement | CDATA
CDATA = {xmlcdata, string()}
E. g. this stanza:
<message to='test@conference.example.org' type='groupchat'>
<body>test</body>
</message>
is represented as the following structure:
{xmlelement, "message",
[{"to", "test@conference.example.org"},
{"type", "groupchat"}],
[{xmlelement, "body",
[],
[{xmlcdata, "test"}]}]}}
4 Module xml
element_to_string(El) -> string()
El = XMLElement
Returns string representation of XML stanza El.
crypt(S) -> string()
S = string()
Returns string which correspond to S with encoded XML special
characters.
remove_cdata(ECList) -> EList
ECList = [ElementOrCDATA]
EList = [XMLElement]
EList is a list of all non-CDATA elements of ECList.
get_path_s(El, Path) -> Res
El = XMLElement
Path = [PathItem]
PathItem = PathElem | PathAttr | PathCDATA
PathElem = {elem, Name}
PathAttr = {attr, Name}
PathCDATA = cdata
Name = string()
Res = string() | XMLElement
If Path is empty, then returns El. Else sequentially
consider elements of Path. Each element is one of:
{elem, Name}
Name is name of subelement of
El, if such element exists, then this element considered in
following steps, else returns empty string.
{attr, Name}
If El have attribute Name, then
returns value of this attribute, else returns empty string.
cdata
Returns CDATA of El.
- TODO:
get_cdata/1, get_tag_cdata/1
get_attr/2, get_attr_s/2
get_tag_attr/2, get_tag_attr_s/2
get_subtag/2
5 Module xml_stream
parse_element(Str) -> XMLElement | {error, Err}
Str = string()
Err = term()
Parses Str using XML parser, returns either parsed element or error
tuple.
6 ejabberd modules
6.1 gen_mod behaviour
TBD
6.2 Module gen_iq_handler
The module gen_iq_handler
allows to easily write handlers for IQ packets
of particular XML namespaces that addressed to server or to users bare JIDs.
In this module the following functions are defined:
add_iq_handler(Component, Host, NS, Module, Function, Type)
Component = Module = Function = atom()
Host = NS = string()
Type = no_queue | one_queue | parallel
Registers function Module:Function
as handler for IQ packets on
virtual host Host
that contain child of namespace NS
in
Component
. Queueing discipline is Type
. There are at least
two components defined:
ejabberd_local
Handles packets that addressed to server JID;
ejabberd_sm
Handles packets that addressed to users bare JIDs.
remove_iq_handler(Component, Host, NS)
Component = atom()
Host = NS = string()
Removes IQ handler on virtual host Host
for namespace NS
from
Component
.
Handler function must have the following type:
Module:Function(From, To, IQ)
From = To = jid()
-module(mod_cputime).
-behaviour(gen_mod).
-export([start/2,
stop/1,
process_local_iq/3]).
-include("ejabberd.hrl").
-include("jlib.hrl").
-define(NS_CPUTIME, "ejabberd:cputime").
start(Host, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_CPUTIME,
?MODULE, process_local_iq, IQDisc).
stop(Host) ->
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_CPUTIME).
process_local_iq(From, To, {iq, ID, Type, XMLNS, SubEl}) ->
case Type of
set ->
{iq, ID, error, XMLNS,
[SubEl, ?ERR_NOT_ALLOWED]};
get ->
CPUTime = element(1, erlang:statistics(runtime))/1000,
SCPUTime = lists:flatten(io_lib:format("~.3f", CPUTime)),
{iq, ID, result, XMLNS,
[{xmlelement, "query",
[{"xmlns", ?NS_CPUTIME}],
[{xmlelement, "cputime", [], [{xmlcdata, SCPUTime}]}]}]}
end.
6.3 Services
TBD
TODO: use proc_lib
-module(mod_echo).
-behaviour(gen_mod).
-export([start/2, init/1, stop/1]).
-include("ejabberd.hrl").
-include("jlib.hrl").
start(Host, Opts) ->
MyHost = gen_mod:get_opt(host, Opts, "echo." ++ Host),
register(gen_mod:get_module_proc(Host, ?PROCNAME),
spawn(?MODULE, init, [MyHost])).
init(Host) ->
ejabberd_router:register_local_route(Host),
loop(Host).
loop(Host) ->
receive
{route, From, To, Packet} ->
ejabberd_router:route(To, From, Packet),
loop(Host);
stop ->
ejabberd_router:unregister_route(Host),
ok;
_ ->
loop(Host)
end.
stop(Host) ->
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
Proc ! stop,
{wait, Proc}.
This document was translated from LATEX by
HEVEA.