d6d1396bc773fe4af53126d447c8845c62b70747
[akkoma] / lib / pleroma / web / activity_pub / mrf / keyword_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do
6 require Pleroma.Constants
7
8 @moduledoc "Reject or Word-Replace messages with a keyword or regex"
9
10 @behaviour Pleroma.Web.ActivityPub.MRF
11 defp string_matches?(string, _) when not is_binary(string) do
12 false
13 end
14
15 defp string_matches?(string, pattern) when is_binary(pattern) do
16 String.contains?(string, pattern)
17 end
18
19 defp string_matches?(string, pattern) do
20 String.match?(string, pattern)
21 end
22
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)
26 end) do
27 {:reject, nil}
28 else
29 {:ok, message}
30 end
31 end
32
33 defp check_ftl_removal(
34 %{"to" => to, "object" => %{"content" => content, "summary" => summary}} = message
35 ) do
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)
39 end) do
40 to = List.delete(to, Pleroma.Constants.as_public())
41 cc = [Pleroma.Constants.as_public() | message["cc"] || []]
42
43 message =
44 message
45 |> Map.put("to", to)
46 |> Map.put("cc", cc)
47
48 {:ok, message}
49 else
50 {:ok, message}
51 end
52 end
53
54 defp check_replace(%{"object" => %{"content" => content, "summary" => summary}} = message) do
55 content =
56 if is_binary(content) do
57 content
58 else
59 ""
60 end
61
62 summary =
63 if is_binary(summary) do
64 summary
65 else
66 ""
67 end
68
69 {content, summary} =
70 Enum.reduce(
71 Pleroma.Config.get([:mrf_keyword, :replace]),
72 {content, summary},
73 fn {pattern, replacement}, {content_acc, summary_acc} ->
74 {String.replace(content_acc, pattern, replacement),
75 String.replace(summary_acc, pattern, replacement)}
76 end
77 )
78
79 {:ok,
80 message
81 |> put_in(["object", "content"], content)
82 |> put_in(["object", "summary"], summary)}
83 end
84
85 @impl true
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
90 {:ok, message}
91 else
92 _e ->
93 {:reject, nil}
94 end
95 end
96
97 @impl true
98 def filter(message), do: {:ok, message}
99
100 @impl true
101 def describe do
102 # This horror is needed to convert regex sigils to strings
103 mrf_keyword =
104 Pleroma.Config.get(:mrf_keyword, [])
105 |> Enum.map(fn {key, value} ->
106 {key,
107 Enum.map(value, fn
108 {pattern, replacement} ->
109 %{
110 "pattern" =>
111 if not is_binary(pattern) do
112 inspect(pattern)
113 else
114 pattern
115 end,
116 "replacement" => replacement
117 }
118
119 pattern ->
120 if not is_binary(pattern) do
121 inspect(pattern)
122 else
123 pattern
124 end
125 end)}
126 end)
127 |> Enum.into(%{})
128
129 {:ok, %{mrf_keyword: mrf_keyword}}
130 end
131 end