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 alias Pleroma.Web.ActivityPub.MRF.Utils
10 @moduledoc "Reject or Word-Replace messages with a keyword or regex"
12 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
14 defp string_matches?(string, pattern) when is_binary(pattern) do
15 String.contains?(string, pattern)
18 defp string_matches?(string, %Regex{} = pattern) do
19 String.match?(string, pattern)
22 defp object_payload(%{} = object) do
23 [object["content"], object["summary"], object["name"]]
28 defp check_reject(%{"object" => %{} = object} = message) do
29 with {:ok, _new_object} <-
30 Pleroma.Object.Updater.do_with_history(object, fn object ->
31 payload = object_payload(object)
33 if Enum.any?(Pleroma.Config.get([:mrf_keyword, :reject]), fn pattern ->
34 string_matches?(payload, pattern)
36 {:reject, "[KeywordPolicy] Matches with rejected keyword"}
47 defp check_ftl_removal(%{"type" => "Create", "to" => to, "object" => %{} = object} = message) do
48 check_keyword = fn object ->
49 payload = object_payload(object)
51 if Enum.any?(Pleroma.Config.get([:mrf_keyword, :federated_timeline_removal]), fn pattern ->
52 string_matches?(payload, pattern)
60 should_delist? = fn object ->
61 with {:ok, _} <- Pleroma.Object.Updater.do_with_history(object, check_keyword) do
68 if Pleroma.Constants.as_public() in to and should_delist?.(object) do
69 to = List.delete(to, Pleroma.Constants.as_public())
70 cc = [Pleroma.Constants.as_public() | message["cc"] || []]
83 defp check_ftl_removal(message) do
87 defp check_replace(%{"object" => %{} = object} = message) do
88 replace_kw = fn object ->
89 ["content", "name", "summary"]
90 |> Enum.filter(fn field -> Map.has_key?(object, field) && object[field] end)
91 |> Enum.reduce(object, fn field, object ->
94 Pleroma.Config.get([:mrf_keyword, :replace]),
96 fn {pat, repl}, acc -> String.replace(acc, pat, repl) end
99 Map.put(object, field, data)
101 |> (fn object -> {:ok, object} end).()
104 {:ok, object} = Pleroma.Object.Updater.do_with_history(object, replace_kw)
106 message = Map.put(message, "object", object)
112 def filter(%{"type" => type, "object" => %{"content" => _content}} = message)
113 when type in ["Create", "Update"] do
114 with {:ok, message} <- check_reject(message),
115 {:ok, message} <- check_ftl_removal(message),
116 {:ok, message} <- check_replace(message) do
119 {:reject, nil} -> {:reject, "[KeywordPolicy] "}
120 {:reject, _} = e -> e
121 _e -> {:reject, "[KeywordPolicy] "}
126 def filter(message), do: {:ok, message}
131 Pleroma.Config.get(:mrf_keyword, [])
132 |> Enum.map(fn {key, value} ->
135 {pattern, replacement} ->
137 "pattern" => Utils.describe_regex_or_string(pattern),
138 "replacement" => replacement
142 Utils.describe_regex_or_string(pattern)
147 {:ok, %{mrf_keyword: mrf_keyword}}
151 def config_description do
154 related_policy: "Pleroma.Web.ActivityPub.MRF.KeywordPolicy",
155 label: "MRF Keyword",
157 "Reject or Word-Replace messages matching a keyword or [Regex](https://hexdocs.pm/elixir/Regex.html).",
161 type: {:list, :string},
163 A list of patterns which result in message being rejected.
165 Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
167 suggestions: ["foo", ~r/foo/iu]
170 key: :federated_timeline_removal,
171 type: {:list, :string},
173 A list of patterns which result in message being removed from federated timelines (a.k.a unlisted).
175 Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
177 suggestions: ["foo", ~r/foo/iu]
181 type: {:list, :tuple},
182 key_placeholder: "instance",
183 value_placeholder: "reason",
185 **Pattern**: a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
187 **Replacement**: a string. Leaving the field empty is permitted.