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-01-22 21:40:40 +01:00
|
|
|
-include("namespaces.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),
|
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"),
|
2002-12-08 18:23:21 +01:00
|
|
|
if
|
|
|
|
(UTag /= false) and (RTag /= false) ->
|
|
|
|
{iq, ID, error, XMLNS,
|
|
|
|
[SubEl, {xmlelement,
|
|
|
|
"error",
|
|
|
|
[{"code", "501"}],
|
|
|
|
[{xmlcdata, "Not Implemented"}]}]};
|
|
|
|
(UTag /= false) and (PTag /= false) ->
|
|
|
|
User = xml:get_tag_cdata(UTag),
|
|
|
|
Password = xml:get_tag_cdata(PTag),
|
|
|
|
Server = ?MYNAME,
|
|
|
|
case From of
|
|
|
|
{User, Server, _} ->
|
|
|
|
ejabberd_auth:set_password(User, Password),
|
|
|
|
{iq, ID, result, XMLNS, [SubEl]};
|
|
|
|
_ ->
|
|
|
|
case try_register(User, Password) of
|
|
|
|
ok ->
|
|
|
|
{iq, ID, result, XMLNS, [SubEl]};
|
|
|
|
{error, Code, Reason} ->
|
|
|
|
{iq, ID, error, XMLNS,
|
|
|
|
[SubEl, {xmlelement,
|
|
|
|
"error",
|
|
|
|
[{"code", Code}],
|
|
|
|
[{xmlcdata, Reason}]}]}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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-01-11 21:25:11 +01:00
|
|
|
{error, "406", "Not Acceptable"};
|
|
|
|
_ ->
|
|
|
|
case ejabberd_auth:try_register(User, Password) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
ok;
|
|
|
|
{atomic, exists} ->
|
|
|
|
{error, "400", "Bad Request"};
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, "500", "Internal Server Error"}
|
|
|
|
end
|
2002-12-08 18:23:21 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|