diff options
Diffstat (limited to 'src/ejabberd_captcha.erl')
-rw-r--r-- | src/ejabberd_captcha.erl | 72 |
1 files changed, 26 insertions, 46 deletions
diff --git a/src/ejabberd_captcha.erl b/src/ejabberd_captcha.erl index 110da1f69..157700c47 100644 --- a/src/ejabberd_captcha.erl +++ b/src/ejabberd_captcha.erl @@ -5,7 +5,7 @@ %%% Created : 26 Apr 2008 by Evgeniy Khramtsov <xramtsov@gmail.com> %%% %%% -%%% ejabberd, Copyright (C) 2002-2015 ProcessOne +%%% ejabberd, Copyright (C) 2002-2016 ProcessOne %%% %%% This program is free software; you can redistribute it and/or %%% modify it under the terms of the GNU General Public License as @@ -25,6 +25,10 @@ -module(ejabberd_captcha). +-behaviour(ejabberd_config). + +-protocol({xep, 158, '1.0'}). + -behaviour(gen_server). %% API @@ -37,7 +41,7 @@ -export([create_captcha/6, build_captcha_html/2, check_captcha/2, process_reply/1, process/2, is_feature_available/0, create_captcha_x/5, - create_captcha_x/6]). + create_captcha_x/6, opt_type/1]). -include("jlib.hrl"). @@ -71,13 +75,6 @@ tref :: reference(), args :: any()}). -%%==================================================================== -%% API -%%==================================================================== -%%-------------------------------------------------------------------- -%% Function: start_link() -> {ok,Pid} | ignore | {error,Error} -%% Description: Starts the server -%%-------------------------------------------------------------------- start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). @@ -91,7 +88,7 @@ create_captcha(SID, From, To, Lang, Limiter, Args) -> {ok, Type, Key, Image} -> Id = <<(randoms:get_string())/binary>>, B64Image = jlib:encode_base64((Image)), - JID = jlib:jid_to_string(From), + JID = jid:to_string(From), CID = <<"sha1+", (p1_sha:sha(Image))/binary, "@bob.xmpp.org">>, Data = #xmlel{name = <<"data">>, @@ -112,7 +109,7 @@ create_captcha(SID, From, To, Lang, Limiter, Args) -> {xmlcdata, ?NS_CAPTCHA}), ?VFIELD(<<"hidden">>, <<"from">>, {xmlcdata, - jlib:jid_to_string(To)}), + jid:to_string(To)}), ?VFIELD(<<"hidden">>, <<"challenge">>, {xmlcdata, Id}), @@ -236,7 +233,7 @@ create_captcha_x(SID, To, Lang, Limiter, HeadEls, [{xmlcdata, Imageurl}]}]}, ?VFIELD(<<"hidden">>, <<"from">>, - {xmlcdata, jlib:jid_to_string(To)}), + {xmlcdata, jid:to_string(To)}), ?VFIELD(<<"hidden">>, <<"challenge">>, {xmlcdata, Id}), ?VFIELD(<<"hidden">>, <<"sid">>, @@ -272,12 +269,6 @@ create_captcha_x(SID, To, Lang, Limiter, HeadEls, Err -> Err end. -%% @spec (Id::string(), Lang::string()) -> {FormEl, {ImgEl, TextEl, IdEl, KeyEl}} | captcha_not_found -%% where FormEl = xmlelement() -%% ImgEl = xmlelement() -%% TextEl = xmlelement() -%% IdEl = xmlelement() -%% KeyEl = xmlelement() -spec build_captcha_html(binary(), binary()) -> captcha_not_found | {xmlel(), {xmlel(), xmlel(), @@ -326,16 +317,10 @@ build_captcha_html(Id, Lang) -> _ -> captcha_not_found end. -%% @spec (Id::string(), ProvidedKey::string()) -> captcha_valid | captcha_non_valid | captcha_not_found --spec check_captcha(binary(), binary()) -> captcha_not_found | - captcha_valid | - captcha_non_valid. - - -spec process_reply(xmlel()) -> ok | {error, bad_match | not_found | malformed}. process_reply(#xmlel{} = El) -> - case xml:get_subtag(El, <<"x">>) of + case fxml:get_subtag(El, <<"x">>) of false -> {error, malformed}; Xdata -> Fields = jlib:parse_xdata_submit(Xdata), @@ -401,9 +386,6 @@ process(_Handlers, process(_Handlers, _Request) -> ejabberd_web:error(not_found). -%%==================================================================== -%% gen_server callbacks -%%==================================================================== init([]) -> mnesia:delete_table(captcha), ets:new(captcha, @@ -450,16 +432,6 @@ terminate(_Reason, _State) -> ok. code_change(_OldVsn, State, _Extra) -> {ok, State}. -%%-------------------------------------------------------------------- -%%% Internal functions -%%-------------------------------------------------------------------- -%%-------------------------------------------------------------------- -%% Function: create_image() -> {ok, Type, Key, Image} | {error, Reason} -%% Type = "image/png" | "image/jpeg" | "image/gif" -%% Key = string() -%% Image = binary() -%% Reason = atom() -%%-------------------------------------------------------------------- create_image() -> create_image(undefined). create_image(Limiter) -> @@ -592,12 +564,6 @@ is_limited(Limiter) -> end end. -%%-------------------------------------------------------------------- -%% Function: cmd(Cmd) -> Data | {error, Reason} -%% Cmd = string() -%% Data = binary() -%% Description: os:cmd/1 replacement -%%-------------------------------------------------------------------- -define(CMD_TIMEOUT, 5000). -define(MAX_FILE_SIZE, 64 * 1024). @@ -659,6 +625,10 @@ lookup_captcha(Id) -> _ -> {error, enoent} end. +-spec check_captcha(binary(), binary()) -> captcha_not_found | + captcha_valid | + captcha_non_valid. + check_captcha(Id, ProvidedKey) -> case ets:lookup(captcha, Id) of [#captcha{pid = Pid, args = Args, key = ValidKey, @@ -691,5 +661,15 @@ clean_treap(Treap, CleanPriority) -> end. now_priority() -> - {MSec, Sec, USec} = now(), - -((MSec * 1000000 + Sec) * 1000000 + USec). + -p1_time_compat:system_time(micro_seconds). + +opt_type(captcha_cmd) -> + fun (FileName) -> + F = iolist_to_binary(FileName), if F /= <<"">> -> F end + end; +opt_type(captcha_host) -> fun iolist_to_binary/1; +opt_type(captcha_limit) -> + fun (I) when is_integer(I), I > 0 -> I end; +opt_type(listen) -> fun (V) -> V end; +opt_type(_) -> + [captcha_cmd, captcha_host, captcha_limit, listen]. |