25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

Fix Captcha library and MUC use for exmpp

This commit is contained in:
Badlop 2010-10-06 13:05:21 +02:00
parent 645ddcb749
commit 6c74c67069
2 changed files with 35 additions and 18 deletions

View File

@ -24,6 +24,22 @@
%%% %%%
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
%%% Database schema (version / storage / table)
%%%
%%% 2.1.x / mnesia / captcha
%%% id = string()
%%% pid = pid()
%%% key = string()
%%% tref = any()
%%% args = any()
%%%
%%% 3.0.0-alpha / ets / captcha
%%% id = string()
%%% pid = pid()
%%% key = string()
%%% tref = any()
%%% args = any()
-module(ejabberd_captcha). -module(ejabberd_captcha).
-behaviour(gen_server). -behaviour(gen_server).
@ -63,7 +79,7 @@
} }
]}). ]}).
-define(CAPTCHA_TEXT(Lang), translate:translate(Lang, "Enter the text you see")). -define(CAPTCHA_TEXT(Lang), list_to_binary(translate:translate(Lang, "Enter the text you see"))).
-define(CAPTCHA_LIFETIME, 120000). % two minutes -define(CAPTCHA_LIFETIME, 120000). % two minutes
-define(RPC_TIMEOUT, 5000). -define(RPC_TIMEOUT, 5000).
@ -84,7 +100,7 @@ create_captcha(SID, From, To, Lang, Args)
when is_binary(Lang), is_binary(SID) -> when is_binary(Lang), is_binary(SID) ->
case create_image() of case create_image() of
{ok, Type, Key, Image} -> {ok, Type, Key, Image} ->
Id = list_to_binary(randoms:get_string() ++ "-" ++ ejabberd_cluster:node_id()), Id = randoms:get_string() ++ "-" ++ ejabberd_cluster:node_id(),
B64Image = list_to_binary(jlib:encode_base64(binary_to_list(Image))), B64Image = list_to_binary(jlib:encode_base64(binary_to_list(Image))),
JID = exmpp_jid:to_list(From), JID = exmpp_jid:to_list(From),
CID = list_to_binary(["sha1+", sha:sha(Image), "@bob.xmpp.org"]), CID = list_to_binary(["sha1+", sha:sha(Image), "@bob.xmpp.org"]),
@ -138,9 +154,9 @@ create_captcha(SID, From, To, Lang, Args)
} }
], ],
children = [ children = [
?VFIELD(<<"hidden">>, <<"FORM_TYPE">>, #xmlcdata{cdata = ?NS_CAPTCHA}), ?VFIELD(<<"hidden">>, <<"FORM_TYPE">>, #xmlcdata{cdata = ?NS_CAPTCHA_b}),
?VFIELD(<<"hidden">>, <<"from">>, #xmlcdata{cdata = exmpp_jid:to_binary(To)}), ?VFIELD(<<"hidden">>, <<"from">>, #xmlcdata{cdata = exmpp_jid:to_binary(To)}),
?VFIELD(<<"hidden">>, <<"challenge">>, #xmlcdata{cdata = Id}), ?VFIELD(<<"hidden">>, <<"challenge">>, #xmlcdata{cdata = list_to_binary(Id)}),
?VFIELD(<<"hidden">>, <<"sid">>, #xmlcdata{cdata = SID}), ?VFIELD(<<"hidden">>, <<"sid">>, #xmlcdata{cdata = SID}),
#xmlel{ #xmlel{
name = 'field', name = 'field',
@ -186,7 +202,7 @@ create_captcha(SID, From, To, Lang, Args)
name = 'body', name = 'body',
children = [ children = [
#xmlcdata{ #xmlcdata{
cdata = BodyString cdata = list_to_binary(BodyString)
} }
] ]
}, },
@ -268,15 +284,15 @@ build_captcha_html(Id, Lang) ->
attrs = [ attrs = [
#xmlattr{ #xmlattr{
name = 'type', name = 'type',
value = "text" value = <<"text">>
}, },
#xmlattr{ #xmlattr{
name = 'name', name = 'name',
value = "key" value = <<"key">>
}, },
#xmlattr{ #xmlattr{
name = 'size', name = 'size',
value = "10" value = <<"10">>
} }
] ]
}, },
@ -304,11 +320,11 @@ build_captcha_html(Id, Lang) ->
}, },
#xmlattr{ #xmlattr{
name = 'name', name = 'name',
value = "captcha" value = <<"captcha">>
}, },
#xmlattr{ #xmlattr{
name = 'method', name = 'method',
value = "POST" value = <<"POST">>
} }
], ],
children = [ children = [
@ -330,15 +346,15 @@ build_captcha_html(Id, Lang) ->
attrs = [ attrs = [
#xmlattr{ #xmlattr{
name = 'type', name = 'type',
value = "submit" value = <<"submit">>
}, },
#xmlattr{ #xmlattr{
name = 'name', name = 'name',
value = "enter" value = <<"enter">>
}, },
#xmlattr{ #xmlattr{
name = 'value', name = 'value',
value = "OK" value = <<"OK">>
} }
] ]
} }
@ -408,7 +424,7 @@ process(_Handlers, #request{method='GET', lang=Lang, path=[_, Id]}) ->
attrs = [ attrs = [
#xmlattr{ #xmlattr{
name = 'align', name = 'align',
value = "center" value = <<"center">>
} }
], ],
children = [FormEl] children = [FormEl]
@ -508,12 +524,12 @@ code_change(_OldVsn, State, _Extra) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
create_image() -> create_image() ->
%% Six numbers from 1 to 9. %% Six numbers from 1 to 9.
Key = list_to_binary(string:substr(randoms:get_string(), 1, 6)), Key = string:substr(randoms:get_string(), 1, 6),
create_image(Key). create_image(Key).
create_image(Key) -> create_image(Key) ->
FileName = get_prog_name(), FileName = get_prog_name(),
Cmd = lists:flatten(io_lib:format("~s ~p", [FileName, Key])), Cmd = lists:flatten(io_lib:format("~s ~s", [FileName, Key])),
case cmd(Cmd) of case cmd(Cmd) of
{ok, <<16#89, $P, $N, $G, $\r, $\n, 16#1a, $\n, _/binary>> = Img} -> {ok, <<16#89, $P, $N, $G, $\r, $\n, 16#1a, $\n, _/binary>> = Img} ->
{ok, <<"image/png">>, Key, Img}; {ok, <<"image/png">>, Key, Img};
@ -547,6 +563,7 @@ get_prog_name() ->
throw({error, option_not_configured_captcha_cmd}) throw({error, option_not_configured_captcha_cmd})
end. end.
%% @doc (Str::string()) -> string()
get_url(Str) -> get_url(Str) ->
case ejabberd_config:get_local_option(captcha_host) of case ejabberd_config:get_local_option(captcha_host) of
Host when is_list(Host) -> Host when is_list(Host) ->

View File

@ -1684,7 +1684,7 @@ add_new_user(From, Nick, Packet, StateData) ->
StateData; StateData;
captcha_required -> captcha_required ->
SID = case exmpp_stanza:get_id(Packet) of SID = case exmpp_stanza:get_id(Packet) of
undefined -> ""; undefined -> <<"">>;
SID1 -> SID1 SID1 -> SID1
end, end,
RoomJID = StateData#state.jid, RoomJID = StateData#state.jid,
@ -1693,7 +1693,7 @@ add_new_user(From, Nick, Packet, StateData) ->
SID, RoomJID, To, Lang, From) of SID, RoomJID, To, Lang, From) of
{ok, ID, CaptchaEls} -> {ok, ID, CaptchaEls} ->
MsgPkt = #xmlel{name = 'message', MsgPkt = #xmlel{name = 'message',
attrs = [#xmlattr{name = 'id', value = ID}], attrs = [#xmlattr{name = 'id', value = list_to_binary(ID)}],
children = CaptchaEls}, children = CaptchaEls},
Robots = ?DICT:store(From, Robots = ?DICT:store(From,
{Nick, Packet}, StateData#state.robots), {Nick, Packet}, StateData#state.robots),