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)
21 Ask the configured captcha service for a new captcha
24 GenServer.call(__MODULE__, :new)
28 Ask the configured captcha service to validate the captcha
30 def validate(token, captcha) do
31 GenServer.call(__MODULE__, {:validate, token, captcha})
35 def handle_call(:new, _from, state) do
36 enabled = Pleroma.Config.get([__MODULE__, :enabled])
39 {:reply, %{type: :none}, state}
41 new_captcha = method().new()
43 seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
44 # Wait several minutes and if the captcha is still there, delete it
45 Process.send_after(self(), {:cleanup, new_captcha.token}, 1000 * seconds_retained)
47 {:reply, new_captcha, state}
52 def handle_call({:validate, token, captcha}, _from, state) do
53 {:reply, method().validate(token, captcha), state}
57 def handle_info({:cleanup, token}, state) do
58 method().cleanup(token)
63 defp method, do: Pleroma.Config.get!([__MODULE__, :method])