mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-22 16:20:52 +01:00
*** empty log message ***
SVN Revision: 14
This commit is contained in:
parent
6c96829311
commit
534b6925a3
@ -16,14 +16,17 @@
|
|||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
%% External exports
|
%% External exports
|
||||||
-export([start/0, start_link/0, set_password/2, check_password/2]).
|
-export([start/0, start_link/0,
|
||||||
|
set_password/2,
|
||||||
|
check_password/2,
|
||||||
|
try_register/2]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
|
||||||
|
|
||||||
-record(state, {}).
|
-record(state, {}).
|
||||||
|
|
||||||
-record(passwd, {jid, password}).
|
-record(passwd, {user, password}).
|
||||||
|
|
||||||
%%%----------------------------------------------------------------------
|
%%%----------------------------------------------------------------------
|
||||||
%%% API
|
%%% API
|
||||||
@ -92,9 +95,11 @@ terminate(Reason, State) ->
|
|||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
%%%----------------------------------------------------------------------
|
%%%----------------------------------------------------------------------
|
||||||
|
|
||||||
check_password(Jid, Password) ->
|
% TODO: lowercase user name
|
||||||
|
|
||||||
|
check_password(User, Password) ->
|
||||||
F = fun() ->
|
F = fun() ->
|
||||||
case mnesia:read({passwd, Jid}) of
|
case mnesia:read({passwd, User}) of
|
||||||
[E] ->
|
[E] ->
|
||||||
E#passwd.password
|
E#passwd.password
|
||||||
end
|
end
|
||||||
@ -107,15 +112,20 @@ check_password(Jid, Password) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
set_password(Jid, Password) ->
|
set_password(User, Password) ->
|
||||||
F = fun() ->
|
F = fun() ->
|
||||||
case mnesia:read({passwd, Jid}) of
|
mnesia:write(#passwd{user = User, password = Password})
|
||||||
|
end,
|
||||||
|
mnesia:transaction(F).
|
||||||
|
|
||||||
|
try_register(User, Password) ->
|
||||||
|
F = fun() ->
|
||||||
|
case mnesia:read({passwd, User}) of
|
||||||
[] ->
|
[] ->
|
||||||
New = #passwd{jid = Jid, password = Password},
|
mnesia:write(#passwd{user = User, password = Password}),
|
||||||
mnesia:write(New);
|
ok;
|
||||||
[E] ->
|
[E] ->
|
||||||
New = E#passwd{password = Password},
|
exists
|
||||||
mnesia:write(New)
|
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
mnesia:transaction(F).
|
mnesia:transaction(F).
|
||||||
|
@ -117,8 +117,21 @@ wait_for_auth({xmlstreamelement, El}, StateData) ->
|
|||||||
send_element(StateData#state.sender, Err),
|
send_element(StateData#state.sender, Err),
|
||||||
{next_state, wait_for_auth, StateData}
|
{next_state, wait_for_auth, StateData}
|
||||||
end;
|
end;
|
||||||
|
_ ->
|
||||||
|
case jlib:iq_query_info(El) of
|
||||||
|
{iq, ID, Type, "jabber:iq:register", SubEl} ->
|
||||||
|
ResIQ = mod_register:process_iq(
|
||||||
|
{"", "", ""}, {"", ?MYNAME, ""},
|
||||||
|
{iq, ID, Type, "jabber:iq:register", SubEl}),
|
||||||
|
Res1 = jlib:replace_from_to({"", ?MYNAME, ""},
|
||||||
|
{"", "", ""},
|
||||||
|
jlib:iq_to_xml(ResIQ)),
|
||||||
|
Res = jlib:remove_attr("to", Res1),
|
||||||
|
send_element(StateData#state.sender, Res),
|
||||||
|
{next_state, wait_for_auth, StateData};
|
||||||
_ ->
|
_ ->
|
||||||
{next_state, wait_for_auth, StateData}
|
{next_state, wait_for_auth, StateData}
|
||||||
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
wait_for_auth({xmlstreamend, Name}, StateData) ->
|
wait_for_auth({xmlstreamend, Name}, StateData) ->
|
||||||
|
85
src/jlib.erl
85
src/jlib.erl
@ -14,9 +14,15 @@
|
|||||||
make_error_reply/3,
|
make_error_reply/3,
|
||||||
make_correct_from_to_attrs/3,
|
make_correct_from_to_attrs/3,
|
||||||
replace_from_to_attrs/3,
|
replace_from_to_attrs/3,
|
||||||
|
replace_from_to/3,
|
||||||
|
remove_attr/2,
|
||||||
string_to_jid/1,
|
string_to_jid/1,
|
||||||
jid_to_string/1,
|
jid_to_string/1,
|
||||||
tolower/1]).
|
tolower/1,
|
||||||
|
get_iq_namespace/1,
|
||||||
|
iq_query_info/1,
|
||||||
|
iq_to_xml/1,
|
||||||
|
get_subtag/2]).
|
||||||
|
|
||||||
|
|
||||||
%send_iq(From, To, ID, SubTags) ->
|
%send_iq(From, To, ID, SubTags) ->
|
||||||
@ -93,6 +99,16 @@ replace_from_to_attrs(From, To, Attrs) ->
|
|||||||
Attrs4 = [{"from", From} | Attrs3],
|
Attrs4 = [{"from", From} | Attrs3],
|
||||||
Attrs4.
|
Attrs4.
|
||||||
|
|
||||||
|
replace_from_to(From, To, {xmlelement, Name, Attrs, Els}) ->
|
||||||
|
NewAttrs = replace_from_to_attrs(jlib:jid_to_string(From),
|
||||||
|
jlib:jid_to_string(To),
|
||||||
|
Attrs),
|
||||||
|
{xmlelement, Name, NewAttrs, Els}.
|
||||||
|
|
||||||
|
|
||||||
|
remove_attr(Attr, {xmlelement, Name, Attrs, Els}) ->
|
||||||
|
NewAttrs = lists:keydelete(Attr, 1, Attrs),
|
||||||
|
{xmlelement, Name, NewAttrs, Els}.
|
||||||
|
|
||||||
string_to_jid(J) ->
|
string_to_jid(J) ->
|
||||||
string_to_jid1(J, "").
|
string_to_jid1(J, "").
|
||||||
@ -155,3 +171,70 @@ tolower_c(C) ->
|
|||||||
tolower(S) ->
|
tolower(S) ->
|
||||||
lists:map(fun tolower_c/1, S).
|
lists:map(fun tolower_c/1, S).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
get_iq_namespace({xmlelement, Name, Attrs, Els}) when Name == "iq" ->
|
||||||
|
case xml:remove_cdata(Els) of
|
||||||
|
[{xmlelement, Name2, Attrs2, Els2}] ->
|
||||||
|
xml:get_attr_s("xmlns", Attrs2);
|
||||||
|
_ ->
|
||||||
|
""
|
||||||
|
end;
|
||||||
|
get_iq_namespace(_) ->
|
||||||
|
"".
|
||||||
|
|
||||||
|
iq_query_info({xmlelement, Name, Attrs, Els}) when Name == "iq" ->
|
||||||
|
ID = xml:get_attr_s("id", Attrs),
|
||||||
|
Type = xml:get_attr_s("type", Attrs),
|
||||||
|
case xml:remove_cdata(Els) of
|
||||||
|
[{xmlelement, Name2, Attrs2, Els2}] ->
|
||||||
|
XMLNS = xml:get_attr_s("xmlns", Attrs2),
|
||||||
|
Type1 = case Type of
|
||||||
|
"set" -> set;
|
||||||
|
"get" -> get;
|
||||||
|
_ -> invalid
|
||||||
|
end,
|
||||||
|
if
|
||||||
|
(Type1 /= invalid) and (XMLNS /= "") ->
|
||||||
|
{iq, ID, Type1, XMLNS, {xmlelement, Name2, Attrs2, Els2}};
|
||||||
|
true ->
|
||||||
|
invalid
|
||||||
|
end;
|
||||||
|
_ ->
|
||||||
|
invalid
|
||||||
|
end;
|
||||||
|
iq_query_info(_) ->
|
||||||
|
not_iq.
|
||||||
|
|
||||||
|
iq_type_to_string(set) -> "set";
|
||||||
|
iq_type_to_string(get) -> "get";
|
||||||
|
iq_type_to_string(result) -> "result";
|
||||||
|
iq_type_to_string(error) -> "error";
|
||||||
|
iq_type_to_string(_) -> invalid.
|
||||||
|
|
||||||
|
|
||||||
|
iq_to_xml({iq, ID, Type, _, SubEl}) ->
|
||||||
|
if
|
||||||
|
ID /= "" ->
|
||||||
|
{xmlelement, "iq",
|
||||||
|
[{"id", ID}, {"type", iq_type_to_string(Type)}], SubEl};
|
||||||
|
true ->
|
||||||
|
{xmlelement, "iq",
|
||||||
|
[{"type", iq_type_to_string(Type)}], SubEl}
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
get_subtag({xmlelement, _, _, Els}, Name) ->
|
||||||
|
get_subtag1(Els, Name).
|
||||||
|
|
||||||
|
get_subtag1([El | Els], Name) ->
|
||||||
|
case El of
|
||||||
|
{xmlelement, Name, _, _} ->
|
||||||
|
El;
|
||||||
|
_ ->
|
||||||
|
get_subtag1(Els, Name)
|
||||||
|
end;
|
||||||
|
get_subtag1([], _) ->
|
||||||
|
false.
|
||||||
|
|
||||||
|
|
||||||
|
75
src/mod_register.erl
Normal file
75
src/mod_register.erl
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
%%%----------------------------------------------------------------------
|
||||||
|
%%% 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$ ').
|
||||||
|
|
||||||
|
-export([process_iq/3]).
|
||||||
|
|
||||||
|
-include("ejabberd.hrl").
|
||||||
|
|
||||||
|
process_iq(From, To, {iq, ID, Type, XMLNS, SubEl}) ->
|
||||||
|
case Type of
|
||||||
|
set ->
|
||||||
|
UTag = jlib:get_subtag(SubEl, "username"),
|
||||||
|
PTag = jlib:get_subtag(SubEl, "password"),
|
||||||
|
RTag = jlib:get_subtag(SubEl, "remove"),
|
||||||
|
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 ->
|
||||||
|
{iq, ID, error, XMLNS, [{xmlelement,
|
||||||
|
"query",
|
||||||
|
[{"xmlns", "jabber:iq:register"}],
|
||||||
|
[{xmlelement, "instructions", [],
|
||||||
|
{xmlcdata,
|
||||||
|
"Choose a username and password "
|
||||||
|
"to register with this server."}},
|
||||||
|
{xmlelement, "username", [], []},
|
||||||
|
{xmlelement, "password", [], []}]}]}
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
try_register(User, Password) ->
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -10,7 +10,8 @@
|
|||||||
-author('alexey@sevcom.net').
|
-author('alexey@sevcom.net').
|
||||||
-vsn('$Revision$ ').
|
-vsn('$Revision$ ').
|
||||||
|
|
||||||
-export([element_to_string/1, crypt/1, remove_cdata/1, get_cdata/1,
|
-export([element_to_string/1, crypt/1, remove_cdata/1,
|
||||||
|
get_cdata/1, get_tag_cdata/1,
|
||||||
get_attr/2, get_attr_s/2]).
|
get_attr/2, get_attr_s/2]).
|
||||||
|
|
||||||
element_to_string(El) ->
|
element_to_string(El) ->
|
||||||
@ -73,6 +74,8 @@ get_cdata([_ | L], S) ->
|
|||||||
get_cdata([], S) ->
|
get_cdata([], S) ->
|
||||||
S.
|
S.
|
||||||
|
|
||||||
|
get_tag_cdata({xmlelement, Name, Attrs, Els}) ->
|
||||||
|
get_cdata(Els).
|
||||||
|
|
||||||
get_attr(AttrName, Attrs) ->
|
get_attr(AttrName, Attrs) ->
|
||||||
case lists:keysearch(AttrName, 1, Attrs) of
|
case lists:keysearch(AttrName, 1, Attrs) of
|
||||||
|
Loading…
Reference in New Issue
Block a user