2002-12-08 18:23:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_register.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2005-06-20 05:18:13 +02:00
|
|
|
%%% Purpose : Inband registration support
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 8 Dec 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 ProcessOne
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2002-12-08 18:23:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_register).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
2005-07-15 00:28:21 +02:00
|
|
|
stream_feature_register/1,
|
2008-04-22 23:51:32 +02:00
|
|
|
unauthenticated_iq_register/4,
|
2009-11-17 12:14:00 +01:00
|
|
|
try_register/5,
|
2005-06-20 05:18:13 +02:00
|
|
|
process_iq/3]).
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2008-09-23 15:11:05 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2002-12-08 18:23:21 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2003-01-24 21:18:33 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, HostB, ?NS_INBAND_REGISTER,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_iq, IQDisc),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_INBAND_REGISTER,
|
2003-09-19 20:22:44 +02:00
|
|
|
?MODULE, process_iq, IQDisc),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(c2s_stream_features, HostB,
|
2005-07-15 00:28:21 +02:00
|
|
|
?MODULE, stream_feature_register, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(c2s_unauthenticated_iq, HostB,
|
2005-07-15 00:28:21 +02:00
|
|
|
?MODULE, unauthenticated_iq_register, 50),
|
2008-04-22 23:51:32 +02:00
|
|
|
mnesia:create_table(mod_register_ip,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{local_content, true},
|
|
|
|
{attributes, [key, value]}]),
|
2008-04-23 15:14:08 +02:00
|
|
|
mnesia:add_table_copy(mod_register_ip, node(), ram_copies),
|
2002-12-11 21:57:45 +01:00
|
|
|
ok.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(c2s_stream_features, HostB,
|
2005-07-15 00:28:21 +02:00
|
|
|
?MODULE, stream_feature_register, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(c2s_unauthenticated_iq, HostB,
|
2005-07-15 00:28:21 +02:00
|
|
|
?MODULE, unauthenticated_iq_register, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, HostB, ?NS_INBAND_REGISTER),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_INBAND_REGISTER).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2005-07-15 00:28:21 +02:00
|
|
|
|
|
|
|
stream_feature_register(Acc) ->
|
2008-09-23 15:11:05 +02:00
|
|
|
[#xmlel{ns = ?NS_INBAND_REGISTER_FEAT, name = 'register'} | Acc].
|
2005-07-15 00:28:21 +02:00
|
|
|
|
2008-04-22 23:51:32 +02:00
|
|
|
unauthenticated_iq_register(_Acc,
|
2008-09-23 15:11:05 +02:00
|
|
|
Server, #iq{ns = ?NS_INBAND_REGISTER} = IQ_Rec, IP) ->
|
2008-04-22 23:51:32 +02:00
|
|
|
Address = case IP of
|
|
|
|
{A, _Port} -> A;
|
|
|
|
_ -> undefined
|
|
|
|
end,
|
2009-06-01 18:26:00 +02:00
|
|
|
BareJID = exmpp_jid:make(Server),
|
|
|
|
ResIQ = process_iq(exmpp_jid:make(),
|
2009-01-03 16:15:38 +01:00
|
|
|
BareJID,
|
2008-09-23 15:11:05 +02:00
|
|
|
IQ_Rec,
|
2008-04-22 23:51:32 +02:00
|
|
|
Address),
|
2009-01-03 16:15:38 +01:00
|
|
|
exmpp_iq:iq_to_xmlel(ResIQ, BareJID, undefined);
|
2005-07-15 00:28:21 +02:00
|
|
|
|
2008-04-22 23:51:32 +02:00
|
|
|
unauthenticated_iq_register(Acc, _Server, _IQ, _IP) ->
|
2005-07-15 00:28:21 +02:00
|
|
|
Acc.
|
|
|
|
|
2008-04-22 23:51:32 +02:00
|
|
|
process_iq(From, To, IQ) ->
|
2009-06-01 18:40:51 +02:00
|
|
|
process_iq(From, To, IQ, {exmpp_jid:prep_node_as_list(From),
|
2009-06-01 18:38:28 +02:00
|
|
|
exmpp_jid:prep_domain_as_list(From),
|
2009-06-01 18:43:15 +02:00
|
|
|
exmpp_jid:prep_resource_as_list(From)}).
|
2008-04-22 23:51:32 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
process_iq(From, To,
|
2008-09-23 15:11:05 +02:00
|
|
|
#iq{type = Type, lang = Lang, payload = SubEl} = IQ_Rec,
|
2008-04-22 23:51:32 +02:00
|
|
|
Source) ->
|
2002-12-08 18:23:21 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2008-09-23 15:11:05 +02:00
|
|
|
UTag = exmpp_xml:get_element(SubEl, 'username'),
|
|
|
|
PTag = exmpp_xml:get_element(SubEl, 'password'),
|
|
|
|
RTag = exmpp_xml:get_element(SubEl, 'remove'),
|
2009-06-01 18:38:28 +02:00
|
|
|
Server = exmpp_jid:prep_domain_as_list(To),
|
2002-12-08 18:23:21 +01:00
|
|
|
if
|
2008-09-23 15:11:05 +02:00
|
|
|
(UTag /= undefined) and (RTag /= undefined) ->
|
|
|
|
User = exmpp_xml:get_cdata_as_list(UTag),
|
2009-06-01 18:38:28 +02:00
|
|
|
case {exmpp_jid:node_as_list(From), exmpp_jid:prep_domain_as_list(From)} of
|
2009-01-03 16:15:38 +01:00
|
|
|
{User, Server} ->
|
2005-04-17 20:08:34 +02:00
|
|
|
ejabberd_auth:remove_user(User, Server),
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, SubEl);
|
2003-02-21 20:52:15 +01:00
|
|
|
_ ->
|
|
|
|
if
|
2008-09-23 15:11:05 +02:00
|
|
|
PTag /= undefined ->
|
|
|
|
Password = exmpp_xml:get_cdata_as_list(PTag),
|
2003-02-21 20:52:15 +01:00
|
|
|
case ejabberd_auth:remove_user(User,
|
2005-04-17 20:08:34 +02:00
|
|
|
Server,
|
2003-02-21 20:52:15 +01:00
|
|
|
Password) of
|
|
|
|
ok ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, SubEl);
|
2007-07-26 12:13:04 +02:00
|
|
|
%% TODO FIXME: This piece of
|
|
|
|
%% code does not work since
|
|
|
|
%% the code have been changed
|
|
|
|
%% to allow several auth
|
|
|
|
%% modules. lists:foreach can
|
|
|
|
%% only return ok:
|
2003-02-21 20:52:15 +01:00
|
|
|
not_allowed ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec,
|
|
|
|
'not-allowed');
|
2003-02-21 20:52:15 +01:00
|
|
|
not_exists ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec,
|
|
|
|
'item-not-found');
|
2003-02-21 20:52:15 +01:00
|
|
|
_ ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec,
|
|
|
|
'internal-server-error')
|
2003-02-21 20:52:15 +01:00
|
|
|
end;
|
|
|
|
true ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'bad-request')
|
2003-02-21 20:52:15 +01:00
|
|
|
end
|
|
|
|
end;
|
2008-09-23 15:11:05 +02:00
|
|
|
(UTag == undefined) and (RTag /= undefined) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
case {exmpp_jid:node_as_list(From),
|
2009-06-01 18:38:28 +02:00
|
|
|
exmpp_jid:prep_domain_as_list(From),
|
2009-01-03 16:15:38 +01:00
|
|
|
exmpp_jid:resource_as_list(From)}of
|
|
|
|
{User, Server, Resource} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
ResIQ = exmpp_iq:result(IQ_Rec, SubEl),
|
2006-07-28 18:18:50 +02:00
|
|
|
ejabberd_router:route(
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(User,
|
2009-01-03 16:15:38 +01:00
|
|
|
Server,
|
|
|
|
Resource),
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(User,
|
2009-01-03 16:15:38 +01:00
|
|
|
Server,
|
|
|
|
Resource),
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:iq_to_xmlel(ResIQ)),
|
2005-04-17 20:08:34 +02:00
|
|
|
ejabberd_auth:remove_user(User, Server),
|
2006-07-28 18:18:50 +02:00
|
|
|
ignore;
|
2003-10-07 22:31:44 +02:00
|
|
|
_ ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed')
|
2003-10-07 22:31:44 +02:00
|
|
|
end;
|
2008-09-23 15:11:05 +02:00
|
|
|
(UTag /= undefined) and (PTag /= undefined) ->
|
|
|
|
User = exmpp_xml:get_cdata_as_list(UTag),
|
|
|
|
Password = exmpp_xml:get_cdata_as_list(PTag),
|
2009-06-01 18:38:28 +02:00
|
|
|
case {exmpp_jid:node_as_list(From), exmpp_jid:prep_domain_as_list(From)} of
|
2009-01-03 16:15:38 +01:00
|
|
|
{User, Server} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
try_set_password(User, Server, Password, IQ_Rec, SubEl);
|
2002-12-08 18:23:21 +01:00
|
|
|
_ ->
|
2010-02-03 13:20:04 +01:00
|
|
|
case check_from(From, Server) of
|
|
|
|
allow ->
|
|
|
|
case try_register(User, Server, Password,
|
|
|
|
Source, Lang) of
|
|
|
|
ok ->
|
|
|
|
exmpp_iq:result(IQ_Rec, SubEl);
|
|
|
|
{error, Error} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
|
|
|
end;
|
|
|
|
deny ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'forbidden')
|
2002-12-08 18:23:21 +01:00
|
|
|
end
|
2003-07-20 22:35:35 +02:00
|
|
|
end;
|
|
|
|
true ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'bad-request')
|
2002-12-08 18:23:21 +01:00
|
|
|
end;
|
|
|
|
get ->
|
2008-09-23 15:11:05 +02:00
|
|
|
Result = #xmlel{ns = ?NS_INBAND_REGISTER, name = 'query', children =
|
|
|
|
[#xmlel{ns = ?NS_INBAND_REGISTER, name = 'instructions', children =
|
|
|
|
[#xmlcdata{cdata = list_to_binary(translate:translate(Lang,
|
|
|
|
"Choose a username and password "
|
|
|
|
"to register with this server"))}]},
|
|
|
|
#xmlel{ns = ?NS_INBAND_REGISTER, name = 'username'},
|
|
|
|
#xmlel{ns = ?NS_INBAND_REGISTER, name = 'password'}]},
|
|
|
|
exmpp_iq:result(IQ_Rec, Result)
|
2002-12-08 18:23:21 +01:00
|
|
|
end.
|
|
|
|
|
2008-09-16 16:39:57 +02:00
|
|
|
%% @doc Try to change password and return IQ response
|
2008-09-23 15:11:05 +02:00
|
|
|
try_set_password(User, Server, Password, IQ_Rec, SubEl) ->
|
2008-09-16 16:39:57 +02:00
|
|
|
case ejabberd_auth:set_password(User, Server, Password) of
|
|
|
|
ok ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, SubEl);
|
2008-09-16 16:39:57 +02:00
|
|
|
{error, empty_password} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'bad-request');
|
2008-09-16 16:39:57 +02:00
|
|
|
{error, not_allowed} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed');
|
2008-09-16 16:39:57 +02:00
|
|
|
{error, invalid_jid} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'item-not-found');
|
2008-09-16 16:39:57 +02:00
|
|
|
_ ->
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'internal-server-error')
|
2008-09-16 16:39:57 +02:00
|
|
|
end.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2008-09-16 16:39:57 +02:00
|
|
|
try_register(User, Server, Password, Source, Lang) ->
|
2008-09-23 15:11:05 +02:00
|
|
|
case exmpp_stringprep:is_node(User) of
|
2003-01-24 21:18:33 +01:00
|
|
|
false ->
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, 'bad-request'};
|
2003-01-11 21:25:11 +01:00
|
|
|
_ ->
|
2009-06-01 18:26:00 +02:00
|
|
|
JID = exmpp_jid:make(User,
|
2009-01-03 16:15:38 +01:00
|
|
|
Server),
|
2005-06-20 05:18:13 +02:00
|
|
|
Access = gen_mod:get_module_opt(Server, ?MODULE, access, all),
|
|
|
|
case acl:match_rule(Server, Access, JID) of
|
2003-10-11 19:39:36 +02:00
|
|
|
deny ->
|
2009-12-23 17:09:33 +01:00
|
|
|
{error, 'forbidden'};
|
2003-10-11 19:39:36 +02:00
|
|
|
allow ->
|
2008-04-22 23:51:32 +02:00
|
|
|
case check_timeout(Source) of
|
|
|
|
true ->
|
|
|
|
case ejabberd_auth:try_register(User, Server, Password) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
send_welcome_message(JID),
|
2009-11-17 12:14:00 +01:00
|
|
|
send_registration_notifications(JID, Source),
|
2008-04-22 23:51:32 +02:00
|
|
|
ok;
|
2008-04-23 15:14:08 +02:00
|
|
|
Error ->
|
|
|
|
remove_timeout(Source),
|
|
|
|
case Error of
|
|
|
|
{atomic, exists} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, 'conflict'};
|
2008-04-23 15:14:08 +02:00
|
|
|
{error, invalid_jid} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, 'jid-malformed'};
|
2008-04-23 15:14:08 +02:00
|
|
|
{error, not_allowed} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, 'not-allowed'};
|
2008-04-23 15:14:08 +02:00
|
|
|
{error, _Reason} ->
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, 'internal-server-error'}
|
2008-04-23 15:14:08 +02:00
|
|
|
end
|
2008-04-22 23:51:32 +02:00
|
|
|
end;
|
|
|
|
false ->
|
2008-09-16 16:39:57 +02:00
|
|
|
ErrText = "Users are not allowed to register "
|
2009-04-08 21:04:13 +02:00
|
|
|
"accounts so quickly",
|
2008-09-23 15:11:05 +02:00
|
|
|
{error, exmpp_stanza:error(?NS_JABBER_CLIENT, 'resource-constraint', {Lang, ErrText})}
|
2003-10-11 19:39:36 +02:00
|
|
|
end
|
2003-01-11 21:25:11 +01:00
|
|
|
end
|
2002-12-08 18:23:21 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2003-10-14 21:34:17 +02:00
|
|
|
send_welcome_message(JID) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(JID),
|
2007-09-04 09:55:41 +02:00
|
|
|
case gen_mod:get_module_opt(Host, ?MODULE, welcome_message, {"", ""}) of
|
2003-10-14 21:34:17 +02:00
|
|
|
{"", ""} ->
|
|
|
|
ok;
|
|
|
|
{Subj, Body} ->
|
|
|
|
ejabberd_router:route(
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(Host),
|
2003-10-14 21:34:17 +02:00
|
|
|
JID,
|
2008-09-23 15:11:05 +02:00
|
|
|
exmpp_message:normal(Subj, Body));
|
2003-10-14 21:34:17 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2009-11-17 12:14:00 +01:00
|
|
|
send_registration_notifications(UJID, Source) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(UJID),
|
2007-09-04 09:55:41 +02:00
|
|
|
case gen_mod:get_module_opt(Host, ?MODULE, registration_watchers, []) of
|
2003-10-19 18:19:55 +02:00
|
|
|
[] -> ok;
|
|
|
|
JIDs when is_list(JIDs) ->
|
|
|
|
Body = lists:flatten(
|
|
|
|
io_lib:format(
|
2009-11-17 12:14:00 +01:00
|
|
|
"[~s] The account ~s was registered from IP address ~s "
|
|
|
|
"on node ~w using ~p.",
|
|
|
|
[get_time_string(), exmpp_jid:to_list(UJID),
|
|
|
|
ip_to_string(Source), node(), ?MODULE])),
|
2003-10-19 18:19:55 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(S) ->
|
2008-09-23 15:11:05 +02:00
|
|
|
try
|
2009-06-01 18:35:55 +02:00
|
|
|
JID = exmpp_jid:parse(S),
|
2008-09-23 15:11:05 +02:00
|
|
|
ejabberd_router:route(
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(Host),
|
2008-09-23 15:11:05 +02:00
|
|
|
JID,
|
|
|
|
exmpp_message:chat(Body))
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
2003-10-19 18:19:55 +02:00
|
|
|
end
|
|
|
|
end, JIDs);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2008-04-22 23:51:32 +02:00
|
|
|
|
2010-02-03 13:20:04 +01:00
|
|
|
check_from(JID, Server) ->
|
2010-04-19 16:08:01 +02:00
|
|
|
case {exmpp_jid:node(JID), exmpp_jid:prep_domain(JID)} of
|
|
|
|
{undefined, undefined} ->
|
2010-02-03 13:20:04 +01:00
|
|
|
allow;
|
|
|
|
_ ->
|
|
|
|
Access = gen_mod:get_module_opt(Server, ?MODULE, access_from, none),
|
|
|
|
acl:match_rule(Server, Access, JID)
|
|
|
|
end.
|
2008-04-22 23:51:32 +02:00
|
|
|
|
|
|
|
check_timeout(undefined) ->
|
|
|
|
true;
|
|
|
|
check_timeout(Source) ->
|
|
|
|
Timeout = case ejabberd_config:get_local_option(registration_timeout) of
|
|
|
|
undefined -> 600;
|
|
|
|
TO -> TO
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
is_integer(Timeout) ->
|
|
|
|
{MSec, Sec, _USec} = now(),
|
|
|
|
Priority = -(MSec * 1000000 + Sec),
|
|
|
|
CleanPriority = Priority + Timeout,
|
|
|
|
F = fun() ->
|
|
|
|
Treap = case mnesia:read(mod_register_ip, treap,
|
|
|
|
write) of
|
|
|
|
[] ->
|
|
|
|
treap:empty();
|
|
|
|
[{mod_register_ip, treap, T}] -> T
|
|
|
|
end,
|
|
|
|
Treap1 = clean_treap(Treap, CleanPriority),
|
|
|
|
case treap:lookup(Source, Treap1) of
|
|
|
|
error ->
|
|
|
|
Treap2 = treap:insert(Source, Priority, [],
|
|
|
|
Treap1),
|
|
|
|
mnesia:write({mod_register_ip, treap, Treap2}),
|
|
|
|
true;
|
|
|
|
{ok, _, _} ->
|
|
|
|
mnesia:write({mod_register_ip, treap, Treap1}),
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Res} ->
|
|
|
|
Res;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("mod_register: timeout check error: ~p~n",
|
|
|
|
[Reason]),
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
true
|
|
|
|
end.
|
|
|
|
|
|
|
|
clean_treap(Treap, CleanPriority) ->
|
|
|
|
case treap:is_empty(Treap) of
|
|
|
|
true ->
|
|
|
|
Treap;
|
|
|
|
false ->
|
|
|
|
{_Key, Priority, _Value} = treap:get_root(Treap),
|
|
|
|
if
|
|
|
|
Priority > CleanPriority ->
|
|
|
|
clean_treap(treap:delete_root(Treap), CleanPriority);
|
|
|
|
true ->
|
|
|
|
Treap
|
|
|
|
end
|
|
|
|
end.
|
2008-04-23 15:14:08 +02:00
|
|
|
|
|
|
|
remove_timeout(undefined) ->
|
|
|
|
true;
|
|
|
|
remove_timeout(Source) ->
|
|
|
|
Timeout = case ejabberd_config:get_local_option(registration_timeout) of
|
|
|
|
undefined -> 600;
|
|
|
|
TO -> TO
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
is_integer(Timeout) ->
|
|
|
|
F = fun() ->
|
|
|
|
Treap = case mnesia:read(mod_register_ip, treap,
|
|
|
|
write) of
|
|
|
|
[] ->
|
|
|
|
treap:empty();
|
|
|
|
[{mod_register_ip, treap, T}] -> T
|
|
|
|
end,
|
|
|
|
Treap1 = treap:delete(Source, Treap),
|
|
|
|
mnesia:write({mod_register_ip, treap, Treap1}),
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
ok;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("mod_register: timeout remove error: ~p~n",
|
|
|
|
[Reason]),
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2009-11-17 12:14:00 +01:00
|
|
|
ip_to_string(Source) when is_tuple(Source) -> inet_parse:ntoa(Source);
|
|
|
|
ip_to_string(undefined) -> "undefined";
|
|
|
|
ip_to_string(_) -> "unknown".
|
|
|
|
|
|
|
|
get_time_string() -> write_time(erlang:localtime()).
|
|
|
|
%% Function copied from ejabberd_logger_h.erl and customized
|
|
|
|
write_time({{Y,Mo,D},{H,Mi,S}}) ->
|
|
|
|
io_lib:format("~w-~.2.0w-~.2.0w ~.2.0w:~.2.0w:~.2.0w",
|
|
|
|
[Y, Mo, D, H, Mi, S]).
|