1 defmodule Pleroma.Captcha do
4 @ets_options [:ordered_set, :private, :named_table, {:read_concurrency, true}]
8 GenServer.start_link(__MODULE__, [], name: __MODULE__)
13 # Create a ETS table to store captchas
14 ets_name = Module.concat(method(), Ets)
15 ^ets_name = :ets.new(Module.concat(method(), Ets), @ets_options)
17 # Clean up old captchas every few minutes
18 seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
19 Process.send_after(self(), :cleanup, 1000 * seconds_retained)
25 Ask the configured captcha service for a new captcha
28 GenServer.call(__MODULE__, :new)
32 Ask the configured captcha service to validate the captcha
34 def validate(token, captcha) do
35 GenServer.call(__MODULE__, {:validate, token, captcha})
39 def handle_call(:new, _from, state) do
40 enabled = Pleroma.Config.get([__MODULE__, :enabled])
43 {:reply, %{type: :none}, state}
45 {:reply, method().new(), state}
50 def handle_call({:validate, token, captcha}, _from, state) do
51 {:reply, method().validate(token, captcha), state}
55 def handle_info(:cleanup, state) do
56 :ok = method().cleanup()
58 seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
59 # Schedule the next clenup
60 Process.send_after(self(), :cleanup, 1000 * seconds_retained)
65 defp method, do: Pleroma.Config.get!([__MODULE__, :method])