]> granicus.if.org Git - ejabberd/commitdiff
*** empty log message ***
authorAlexey Shchepin <alexey@process-one.net>
Sun, 8 Dec 2002 17:23:21 +0000 (17:23 +0000)
committerAlexey Shchepin <alexey@process-one.net>
Sun, 8 Dec 2002 17:23:21 +0000 (17:23 +0000)
SVN Revision: 14

src/ejabberd_auth.erl
src/ejabberd_c2s.erl
src/jlib.erl
src/mod_register.erl [new file with mode: 0644]
src/xml.erl

index 3a579a41b43b9fcb2266490c2573fc4bf7fcfffc..6d150eae8aa5fa67c9ec45c36901db9008d5f98a 100644 (file)
 -behaviour(gen_server).
 
 %% 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
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
 
 -record(state, {}).
 
--record(passwd, {jid, password}).
+-record(passwd, {user, password}).
 
 %%%----------------------------------------------------------------------
 %%% API
@@ -92,9 +95,11 @@ terminate(Reason, State) ->
 %%% Internal functions
 %%%----------------------------------------------------------------------
 
-check_password(Jid, Password) ->
+% TODO: lowercase user name
+
+check_password(User, Password) ->
     F = fun() ->
-               case mnesia:read({passwd, Jid}) of
+               case mnesia:read({passwd, User}) of
                    [E] ->
                        E#passwd.password
                end
@@ -107,16 +112,21 @@ check_password(Jid, Password) ->
     end.
 
 
-set_password(Jid, Password) ->
+set_password(User, Password) ->
+    F = fun() ->
+               mnesia:write(#passwd{user = User, password = Password})
+        end,
+    mnesia:transaction(F).
+
+try_register(User, Password) ->
     F = fun() ->
-               case mnesia:read({passwd, Jid}) of
+               case mnesia:read({passwd, User}) of
                    [] ->
-                       New = #passwd{jid = Jid, password = Password},
-                       mnesia:write(New);
+                       mnesia:write(#passwd{user = User, password = Password}),
+                       ok;
                    [E] ->
-                       New = E#passwd{password = Password},
-                       mnesia:write(New)
+                       exists
                end
         end,
     mnesia:transaction(F).
-
+    
index 5c32bbde017c4d301ba544bf185b3853cee5991b..79b30de1633db4f016c073c2b6fbd4341e4b527b 100644 (file)
@@ -118,7 +118,20 @@ wait_for_auth({xmlstreamelement, El}, StateData) ->
                    {next_state, wait_for_auth, StateData}
            end;
        _ ->
-           {next_state, wait_for_auth, StateData}
+           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}
+           end
     end;
 
 wait_for_auth({xmlstreamend, Name}, StateData) ->
index b1262e0ef64adaac47358ad147cb09a30db84492..c4c510f4d627334dc34369b580950f2a84da2222 100644 (file)
         make_error_reply/3,
         make_correct_from_to_attrs/3,
         replace_from_to_attrs/3,
+        replace_from_to/3,
+        remove_attr/2,
         string_to_jid/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) ->
@@ -93,6 +99,16 @@ replace_from_to_attrs(From, To, Attrs) ->
     Attrs4 = [{"from", From} | Attrs3],
     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_jid1(J, "").
@@ -155,3 +171,70 @@ tolower_c(C) ->
 tolower(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.
+
+
diff --git a/src/mod_register.erl b/src/mod_register.erl
new file mode 100644 (file)
index 0000000..ecd064b
--- /dev/null
@@ -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.
+
+
+
index 9ae4d509148c65d771eb852e3256d4072cb7d680..3bdcb62f7563d5355265b9ac4bf843fab4c22a36 100644 (file)
@@ -10,7 +10,8 @@
 -author('alexey@sevcom.net').
 -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]).
 
 element_to_string(El) ->
@@ -72,7 +73,9 @@ get_cdata([_ | L], S) ->
     get_cdata(L, S);
 get_cdata([], S) ->
     S.
-    
+
+get_tag_cdata({xmlelement, Name, Attrs, Els}) ->
+    get_cdata(Els).
 
 get_attr(AttrName, Attrs) ->
     case lists:keysearch(AttrName, 1, Attrs) of