1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do
6 require Pleroma.Constants
8 @moduledoc "Reject or Word-Replace messages with a keyword or regex"
10 @behaviour Pleroma.Web.ActivityPub.MRF
11 defp string_matches?(string, _) when not is_binary(string) do
15 defp string_matches?(string, pattern) when is_binary(pattern) do
16 String.contains?(string, pattern)
19 defp string_matches?(string, pattern) do
20 String.match?(string, pattern)
23 defp check_reject(%{"object" => %{"content" => content, "summary" => summary}} = message) do
24 if Enum.any?(Pleroma.Config.get([:mrf_keyword, :reject]), fn pattern ->
25 string_matches?(content, pattern) or string_matches?(summary, pattern)
33 defp check_ftl_removal(
34 %{"to" => to, "object" => %{"content" => content, "summary" => summary}} = message
36 if Pleroma.Constants.as_public() in to and
37 Enum.any?(Pleroma.Config.get([:mrf_keyword, :federated_timeline_removal]), fn pattern ->
38 string_matches?(content, pattern) or string_matches?(summary, pattern)
40 to = List.delete(to, Pleroma.Constants.as_public())
41 cc = [Pleroma.Constants.as_public() | message["cc"] || []]
54 defp check_replace(%{"object" => %{"content" => content, "summary" => summary}} = message) do
56 if is_binary(content) do
63 if is_binary(summary) do
71 Pleroma.Config.get([:mrf_keyword, :replace]),
73 fn {pattern, replacement}, {content_acc, summary_acc} ->
74 {String.replace(content_acc, pattern, replacement),
75 String.replace(summary_acc, pattern, replacement)}
81 |> put_in(["object", "content"], content)
82 |> put_in(["object", "summary"], summary)}
86 def filter(%{"type" => "Create", "object" => %{"content" => _content}} = message) do
87 with {:ok, message} <- check_reject(message),
88 {:ok, message} <- check_ftl_removal(message),
89 {:ok, message} <- check_replace(message) do
98 def filter(message), do: {:ok, message}
102 # This horror is needed to convert regex sigils to strings
104 Pleroma.Config.get(:mrf_keyword, [])
105 |> Enum.map(fn {key, value} ->
108 {pattern, replacement} ->
111 if not is_binary(pattern) do
116 "replacement" => replacement
120 if not is_binary(pattern) do
129 {:ok, %{mrf_keyword: mrf_keyword}}