2002-12-08 18:23:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_register.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 8 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_register).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2003-01-22 21:40:40 +01:00
|
|
|
-export([start/1, init/0, process_iq/3]).
|
2002-12-08 18:23:21 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2002-12-08 18:23:21 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
start(Opts) ->
|
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2003-01-22 21:40:40 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, ?NS_REGISTER,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_iq, IQDisc),
|
2003-09-19 20:22:44 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, ?NS_REGISTER,
|
|
|
|
?MODULE, process_iq, IQDisc),
|
2002-12-11 21:57:45 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
init() ->
|
|
|
|
ok.
|
|
|
|
|
2002-12-08 18:23:21 +01:00
|
|
|
process_iq(From, To, {iq, ID, Type, XMLNS, SubEl}) ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
2003-01-02 22:01:12 +01:00
|
|
|
UTag = xml:get_subtag(SubEl, "username"),
|
|
|
|
PTag = xml:get_subtag(SubEl, "password"),
|
|
|
|
RTag = xml:get_subtag(SubEl, "remove"),
|
2003-02-21 20:52:15 +01:00
|
|
|
Server = ?MYNAME,
|
2002-12-08 18:23:21 +01:00
|
|
|
if
|
|
|
|
(UTag /= false) and (RTag /= false) ->
|
2003-02-21 20:52:15 +01:00
|
|
|
User = xml:get_tag_cdata(UTag),
|
|
|
|
case From of
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{user = User, lserver = Server} ->
|
2003-02-21 20:52:15 +01:00
|
|
|
ejabberd_auth:remove_user(User),
|
|
|
|
{iq, ID, result, XMLNS, [SubEl]};
|
|
|
|
_ ->
|
|
|
|
if
|
|
|
|
PTag /= false ->
|
|
|
|
Password = xml:get_tag_cdata(PTag),
|
|
|
|
case ejabberd_auth:remove_user(User,
|
|
|
|
Password) of
|
|
|
|
ok ->
|
|
|
|
{iq, ID, result, XMLNS, [SubEl]};
|
|
|
|
not_allowed ->
|
|
|
|
{iq, ID, error, XMLNS,
|
2003-05-09 21:32:59 +02:00
|
|
|
[SubEl, ?ERR_NOT_ALLOWED]};
|
2003-02-21 20:52:15 +01:00
|
|
|
not_exists ->
|
|
|
|
{iq, ID, error, XMLNS,
|
|
|
|
[SubEl, {xmlelement,
|
|
|
|
"error",
|
|
|
|
[{"code", "404"}],
|
|
|
|
[{xmlcdata,
|
|
|
|
"Not Found"}]}]};
|
|
|
|
_ ->
|
|
|
|
{iq, ID, error, XMLNS,
|
|
|
|
[SubEl,
|
2003-05-09 21:32:59 +02:00
|
|
|
?ERR_INTERNAL_SERVER_ERROR]}
|
2003-02-21 20:52:15 +01:00
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
{iq, ID, error, XMLNS,
|
2003-05-09 21:32:59 +02:00
|
|
|
[SubEl, ?ERR_BAD_REQUEST]}
|
2003-02-21 20:52:15 +01:00
|
|
|
end
|
|
|
|
end;
|
2003-09-19 20:22:44 +02:00
|
|
|
(UTag == false) and (RTag /= false) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
case From of
|
|
|
|
#jid{user = User, lserver = Server} ->
|
|
|
|
ejabberd_auth:remove_user(User),
|
|
|
|
{iq, ID, result, XMLNS, [SubEl]};
|
|
|
|
_ ->
|
|
|
|
{iq, ID, error, XMLNS, [SubEl, ?ERR_NOT_ALLOWED]}
|
|
|
|
end;
|
2002-12-08 18:23:21 +01:00
|
|
|
(UTag /= false) and (PTag /= false) ->
|
|
|
|
User = xml:get_tag_cdata(UTag),
|
|
|
|
Password = xml:get_tag_cdata(PTag),
|
|
|
|
case From of
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{user = User, lserver = Server} ->
|
2002-12-08 18:23:21 +01:00
|
|
|
ejabberd_auth:set_password(User, Password),
|
|
|
|
{iq, ID, result, XMLNS, [SubEl]};
|
|
|
|
_ ->
|
|
|
|
case try_register(User, Password) of
|
|
|
|
ok ->
|
|
|
|
{iq, ID, result, XMLNS, [SubEl]};
|
2003-03-09 21:46:47 +01:00
|
|
|
{error, Error} ->
|
2002-12-08 18:23:21 +01:00
|
|
|
{iq, ID, error, XMLNS,
|
2003-03-09 21:46:47 +01:00
|
|
|
[SubEl, Error]}
|
2002-12-08 18:23:21 +01:00
|
|
|
end
|
2003-07-20 22:35:35 +02:00
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
{iq, ID, error, XMLNS,
|
|
|
|
[SubEl, ?ERR_BAD_REQUEST]}
|
2002-12-08 18:23:21 +01:00
|
|
|
end;
|
|
|
|
get ->
|
2003-01-06 20:57:05 +01:00
|
|
|
{iq, ID, result, XMLNS, [{xmlelement,
|
|
|
|
"query",
|
|
|
|
[{"xmlns", "jabber:iq:register"}],
|
|
|
|
[{xmlelement, "instructions", [],
|
|
|
|
[{xmlcdata,
|
|
|
|
"Choose a username and password "
|
|
|
|
"to register with this server."}]},
|
|
|
|
{xmlelement, "username", [], []},
|
|
|
|
{xmlelement, "password", [], []}]}]}
|
2002-12-08 18:23:21 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
try_register(User, Password) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
case jlib:is_nodename(User) of
|
|
|
|
false ->
|
2003-05-09 21:32:59 +02:00
|
|
|
{error, ?ERR_BAD_REQUEST};
|
2003-01-11 21:25:11 +01:00
|
|
|
_ ->
|
2003-10-14 21:34:17 +02:00
|
|
|
JID = jlib:make_jid(User, ?MYNAME, ""),
|
|
|
|
case acl:match_rule(register, JID) of
|
2003-10-11 19:39:36 +02:00
|
|
|
deny ->
|
|
|
|
{error, ?ERR_CONFLICT};
|
|
|
|
allow ->
|
|
|
|
case ejabberd_auth:try_register(User, Password) of
|
|
|
|
{atomic, ok} ->
|
2003-10-14 21:34:17 +02:00
|
|
|
send_welcome_message(JID),
|
2003-10-11 19:39:36 +02:00
|
|
|
ok;
|
|
|
|
{atomic, exists} ->
|
|
|
|
{error, ?ERR_CONFLICT};
|
|
|
|
{error, _Reason} ->
|
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR}
|
|
|
|
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) ->
|
|
|
|
case ejabberd_config:get_local_option(welcome_message) of
|
|
|
|
{"", ""} ->
|
|
|
|
ok;
|
|
|
|
{Subj, Body} ->
|
|
|
|
ejabberd_router:route(
|
|
|
|
jlib:make_jid("", ?MYNAME, ""),
|
|
|
|
JID,
|
|
|
|
{xmlelement, "message", [{"type", "normal"}],
|
|
|
|
[{xmlelement, "subject", [], [{xmlcdata, Subj}]},
|
|
|
|
{xmlelement, "body", [], [{xmlcdata, Body}]}]});
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2002-12-08 18:23:21 +01:00
|
|
|
|