1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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.Policy
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 object_payload(%{} = object) do
24 [object["content"], object["summary"], object["name"]]
29 defp check_reject(%{"object" => %{} = object} = message) do
30 with {:ok, _new_object} <-
31 Pleroma.Object.Updater.do_with_history(object, fn object ->
32 payload = object_payload(object)
34 if Enum.any?(Pleroma.Config.get([:mrf_keyword, :reject]), fn pattern ->
35 string_matches?(payload, pattern)
37 {:reject, "[KeywordPolicy] Matches with rejected keyword"}
48 defp check_ftl_removal(%{"type" => "Create", "to" => to, "object" => %{} = object} = message) do
49 check_keyword = fn object ->
50 payload = object_payload(object)
52 if Enum.any?(Pleroma.Config.get([:mrf_keyword, :federated_timeline_removal]), fn pattern ->
53 string_matches?(payload, pattern)
61 should_delist? = fn object ->
62 with {:ok, _} <- Pleroma.Object.Updater.do_with_history(object, check_keyword) do
69 if Pleroma.Constants.as_public() in to and should_delist?.(object) do
70 to = List.delete(to, Pleroma.Constants.as_public())
71 cc = [Pleroma.Constants.as_public() | message["cc"] || []]
84 defp check_ftl_removal(message) do
88 defp check_replace(%{"object" => %{} = object} = message) do
89 replace_kw = fn object ->
90 ["content", "name", "summary"]
91 |> Enum.filter(fn field -> Map.has_key?(object, field) && object[field] end)
92 |> Enum.reduce(object, fn field, object ->
95 Pleroma.Config.get([:mrf_keyword, :replace]),
97 fn {pat, repl}, acc -> String.replace(acc, pat, repl) end
100 Map.put(object, field, data)
102 |> (fn object -> {:ok, object} end).()
105 {:ok, object} = Pleroma.Object.Updater.do_with_history(object, replace_kw)
107 message = Map.put(message, "object", object)
113 def filter(%{"type" => type, "object" => %{"content" => _content}} = message)
114 when type in ["Create", "Update"] do
115 with {:ok, message} <- check_reject(message),
116 {:ok, message} <- check_ftl_removal(message),
117 {:ok, message} <- check_replace(message) do
120 {:reject, nil} -> {:reject, "[KeywordPolicy] "}
121 {:reject, _} = e -> e
122 _e -> {:reject, "[KeywordPolicy] "}
127 def filter(message), do: {:ok, message}
131 # This horror is needed to convert regex sigils to strings
133 Pleroma.Config.get(:mrf_keyword, [])
134 |> Enum.map(fn {key, value} ->
137 {pattern, replacement} ->
140 if not is_binary(pattern) do
145 "replacement" => replacement
149 if not is_binary(pattern) do
158 {:ok, %{mrf_keyword: mrf_keyword}}
162 def config_description do
165 related_policy: "Pleroma.Web.ActivityPub.MRF.KeywordPolicy",
166 label: "MRF Keyword",
168 "Reject or Word-Replace messages matching a keyword or [Regex](https://hexdocs.pm/elixir/Regex.html).",
172 type: {:list, :string},
174 A list of patterns which result in message being rejected.
176 Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
178 suggestions: ["foo", ~r/foo/iu]
181 key: :federated_timeline_removal,
182 type: {:list, :string},
184 A list of patterns which result in message being removed from federated timelines (a.k.a unlisted).
186 Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
188 suggestions: ["foo", ~r/foo/iu]
192 type: {:list, :tuple},
193 key_placeholder: "instance",
194 value_placeholder: "reason",
196 **Pattern**: a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
198 **Replacement**: a string. Leaving the field empty is permitted.