diff options
author | Alexey Shchepin <alexey@process-one.net> | 2002-11-23 20:55:05 +0000 |
---|---|---|
committer | Alexey Shchepin <alexey@process-one.net> | 2002-11-23 20:55:05 +0000 |
commit | 8f7388b317081a5b1fa53b24e691552ada7127cb (patch) | |
tree | 2cda48505bf2b85dfa6c31389c377bd2ee1a5eaf /src/ejabberd_auth.erl | |
parent | *** empty log message *** (diff) |
*** empty log message ***
SVN Revision: 5
Diffstat (limited to 'src/ejabberd_auth.erl')
-rw-r--r-- | src/ejabberd_auth.erl | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/ejabberd_auth.erl b/src/ejabberd_auth.erl new file mode 100644 index 00000000..3a579a41 --- /dev/null +++ b/src/ejabberd_auth.erl @@ -0,0 +1,122 @@ +%%%---------------------------------------------------------------------- +%%% File : ejabberd_auth.erl +%%% Author : Alexey Shchepin <alexey@sevcom.net> +%%% Purpose : +%%% Created : 23 Nov 2002 by Alexey Shchepin <alexey@sevcom.net> +%%% Id : $Id$ +%%%---------------------------------------------------------------------- + +-module(ejabberd_auth). +-author('alexey@sevcom.net'). +-vsn('$Revision$ '). + +%%-compile(export_all). +%%-export([Function/Arity, ...]). + +-behaviour(gen_server). + +%% External exports +-export([start/0, start_link/0, set_password/2, check_password/2]). + +%% gen_server callbacks +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]). + +-record(state, {}). + +-record(passwd, {jid, password}). + +%%%---------------------------------------------------------------------- +%%% API +%%%---------------------------------------------------------------------- +start() -> + gen_server:start({local, ejabberd_auth}, ejabberd_auth, [], []). +start_link() -> + gen_server:start_link({local, ejabberd_auth}, ejabberd_auth, [], []). + +%%%---------------------------------------------------------------------- +%%% Callback functions from gen_server +%%%---------------------------------------------------------------------- + +%%---------------------------------------------------------------------- +%% Func: init/1 +%% Returns: {ok, State} | +%% {ok, State, Timeout} | +%% ignore | +%% {stop, Reason} +%%---------------------------------------------------------------------- +init([]) -> + mnesia:create_table(passwd,[{disc_copies, [node()]}, + {attributes, record_info(fields, passwd)}]), + {ok, #state{}}. + +%%---------------------------------------------------------------------- +%% Func: handle_call/3 +%% Returns: {reply, Reply, State} | +%% {reply, Reply, State, Timeout} | +%% {noreply, State} | +%% {noreply, State, Timeout} | +%% {stop, Reason, Reply, State} | (terminate/2 is called) +%% {stop, Reason, State} (terminate/2 is called) +%%---------------------------------------------------------------------- +handle_call(Request, From, State) -> + Reply = ok, + {reply, Reply, State}. + +%%---------------------------------------------------------------------- +%% Func: handle_cast/2 +%% Returns: {noreply, State} | +%% {noreply, State, Timeout} | +%% {stop, Reason, State} (terminate/2 is called) +%%---------------------------------------------------------------------- +handle_cast(Msg, State) -> + {noreply, State}. + +%%---------------------------------------------------------------------- +%% Func: handle_info/2 +%% Returns: {noreply, State} | +%% {noreply, State, Timeout} | +%% {stop, Reason, State} (terminate/2 is called) +%%---------------------------------------------------------------------- +handle_info(Info, State) -> + {noreply, State}. + +%%---------------------------------------------------------------------- +%% Func: terminate/2 +%% Purpose: Shutdown the server +%% Returns: any (ignored by gen_server) +%%---------------------------------------------------------------------- +terminate(Reason, State) -> + ok. + +%%%---------------------------------------------------------------------- +%%% Internal functions +%%%---------------------------------------------------------------------- + +check_password(Jid, Password) -> + F = fun() -> + case mnesia:read({passwd, Jid}) of + [E] -> + E#passwd.password + end + end, + case mnesia:transaction(F) of + {atomic, Password} -> + true; + _ -> + false + end. + + +set_password(Jid, Password) -> + F = fun() -> + case mnesia:read({passwd, Jid}) of + [] -> + New = #passwd{jid = Jid, password = Password}, + mnesia:write(New); + [E] -> + New = E#passwd{password = Password}, + mnesia:write(New) + end + end, + mnesia:transaction(F). + |