1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 payload = object_payload(object)
32 if Enum.any?(Pleroma.Config.get([:mrf_keyword, :reject]), fn pattern ->
33 string_matches?(payload, pattern)
35 {:reject, "[KeywordPolicy] Matches with rejected keyword"}
41 defp check_ftl_removal(%{"to" => to, "object" => %{} = object} = message) do
42 payload = object_payload(object)
44 if Pleroma.Constants.as_public() in to and
45 Enum.any?(Pleroma.Config.get([:mrf_keyword, :federated_timeline_removal]), fn pattern ->
46 string_matches?(payload, pattern)
48 to = List.delete(to, Pleroma.Constants.as_public())
49 cc = [Pleroma.Constants.as_public() | message["cc"] || []]
62 defp check_replace(%{"object" => %{} = object} = message) do
64 ["content", "name", "summary"]
65 |> Enum.filter(fn field -> Map.has_key?(object, field) && object[field] end)
66 |> Enum.reduce(object, fn field, object ->
69 Pleroma.Config.get([:mrf_keyword, :replace]),
71 fn {pat, repl}, acc -> String.replace(acc, pat, repl) end
74 Map.put(object, field, data)
77 message = Map.put(message, "object", object)
83 def filter(%{"type" => "Create", "object" => %{"content" => _content}} = message) do
84 with {:ok, message} <- check_reject(message),
85 {:ok, message} <- check_ftl_removal(message),
86 {:ok, message} <- check_replace(message) do
89 {:reject, nil} -> {:reject, "[KeywordPolicy] "}
91 _e -> {:reject, "[KeywordPolicy] "}
96 def filter(message), do: {:ok, message}
100 # This horror is needed to convert regex sigils to strings
102 Pleroma.Config.get(:mrf_keyword, [])
103 |> Enum.map(fn {key, value} ->
106 {pattern, replacement} ->
109 if not is_binary(pattern) do
114 "replacement" => replacement
118 if not is_binary(pattern) do
127 {:ok, %{mrf_keyword: mrf_keyword}}
131 def config_description do
134 related_policy: "Pleroma.Web.ActivityPub.MRF.KeywordPolicy",
135 label: "MRF Keyword",
137 "Reject or Word-Replace messages matching a keyword or [Regex](https://hexdocs.pm/elixir/Regex.html).",
141 type: {:list, :string},
143 A list of patterns which result in message being rejected.
145 Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
147 suggestions: ["foo", ~r/foo/iu]
150 key: :federated_timeline_removal,
151 type: {:list, :string},
153 A list of patterns which result in message being removed from federated timelines (a.k.a unlisted).
155 Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
157 suggestions: ["foo", ~r/foo/iu]
161 type: {:list, :tuple},
163 **Pattern**: a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
165 **Replacement**: a string. Leaving the field empty is permitted.