1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Captcha do
8 @ets_options [:ordered_set, :private, :named_table, {:read_concurrency, true}]
12 GenServer.start_link(__MODULE__, [], name: __MODULE__)
17 # Create a ETS table to store captchas
18 ets_name = Module.concat(method(), Ets)
19 ^ets_name = :ets.new(Module.concat(method(), Ets), @ets_options)
21 # Clean up old captchas every few minutes
22 seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
23 Process.send_after(self(), :cleanup, 1000 * seconds_retained)
29 Ask the configured captcha service for a new captcha
32 GenServer.call(__MODULE__, :new)
36 Ask the configured captcha service to validate the captcha
38 def validate(token, captcha) do
39 GenServer.call(__MODULE__, {:validate, token, captcha})
43 def handle_call(:new, _from, state) do
44 enabled = Pleroma.Config.get([__MODULE__, :enabled])
47 {:reply, %{type: :none}, state}
49 {:reply, method().new(), state}
54 def handle_call({:validate, token, captcha}, _from, state) do
55 {:reply, method().validate(token, captcha), state}
59 def handle_info(:cleanup, state) do
60 :ok = method().cleanup()
62 seconds_retained = Pleroma.Config.get!([__MODULE__, :seconds_retained])
63 # Schedule the next clenup
64 Process.send_after(self(), :cleanup, 1000 * seconds_retained)
69 defp method, do: Pleroma.Config.get!([__MODULE__, :method])