Merge branch 'mrf-docs-update' into 'develop'
[akkoma] / docs / configuration / mrf.md
1 # Message Rewrite Facility
2
3 The Message Rewrite Facility (MRF) is a subsystem that is implemented as a series of hooks that allows the administrator to rewrite or discard messages.
4
5 Possible uses include:
6
7 * marking incoming messages with media from a given account or instance as sensitive
8 * rejecting messages from a specific instance
9 * rejecting reports (flags) from a specific instance
10 * removing/unlisting messages from the public timelines
11 * removing media from messages
12 * sending only public messages to a specific instance
13
14 The MRF provides user-configurable policies. The default policy is `NoOpPolicy`, which disables the MRF functionality. Pleroma also includes an easy to use policy called `SimplePolicy` which maps messages matching certain pre-defined criterion to actions built into the policy module.
15
16 It is possible to use multiple, active MRF policies at the same time.
17
18 ## Quarantine Instances
19
20 You have the ability to prevent from private / followers-only messages from federating with specific instances. Which means they will only get the public or unlisted messages from your instance.
21
22 If, for example, you're using `MIX_ENV=prod` aka using production mode, you would open your configuration file located in `config/prod.secret.exs` and edit or add the option under your `:instance` config object. Then you would specify the instance within quotes.
23
24 ```elixir
25 config :pleroma, :instance,
26 [...]
27 quarantined_instances: ["instance.example", "other.example"]
28 ```
29
30 ## Using `SimplePolicy`
31
32 `SimplePolicy` is capable of handling most common admin tasks.
33
34 To use `SimplePolicy`, you must enable it. Do so by adding the following to your `:instance` config object, so that it looks like this:
35
36 ```elixir
37 config :pleroma, :instance,
38 [...]
39 rewrite_policy: Pleroma.Web.ActivityPub.MRF.SimplePolicy
40 ```
41
42 Once `SimplePolicy` is enabled, you can configure various groups in the `:mrf_simple` config object. These groups are:
43
44 * `reject`: Servers in this group will have their messages rejected.
45 * `accept`: If not empty, only messages from these instances will be accepted (whitelist federation).
46 * `media_nsfw`: Servers in this group will have the #nsfw tag and sensitive setting injected into incoming messages which contain media.
47 * `media_removal`: Servers in this group will have media stripped from incoming messages.
48 * `avatar_removal`: Avatars from these servers will be stripped from incoming messages.
49 * `banner_removal`: Banner images from these servers will be stripped from incoming messages.
50 * `report_removal`: Servers in this group will have their reports (flags) rejected.
51 * `federated_timeline_removal`: Servers in this group will have their messages unlisted from the public timelines by flipping the `to` and `cc` fields.
52
53 Servers should be configured as lists.
54
55 ### Example
56
57 This example will enable `SimplePolicy`, block media from `illegalporn.biz`, mark media as NSFW from `porn.biz` and `porn.business`, reject messages from `spam.com`, remove messages from `spam.university` from the federated timeline and block reports (flags) from `whiny.whiner`:
58
59 ```elixir
60 config :pleroma, :instance,
61 rewrite_policy: [Pleroma.Web.ActivityPub.MRF.SimplePolicy]
62
63 config :pleroma, :mrf_simple,
64 media_removal: ["illegalporn.biz"],
65 media_nsfw: ["porn.biz", "porn.business"],
66 reject: ["spam.com"],
67 federated_timeline_removal: ["spam.university"],
68 report_removal: ["whiny.whiner"]
69 ```
70
71 ### Use with Care
72
73 The effects of MRF policies can be very drastic. It is important to use this functionality carefully. Always try to talk to an admin before writing an MRF policy concerning their instance.
74
75 ## Writing your own MRF Policy
76
77 As discussed above, the MRF system is a modular system that supports pluggable policies. This means that an admin may write a custom MRF policy in Elixir or any other language that runs on the Erlang VM, by specifying the module name in the `rewrite_policy` config setting.
78
79 For example, here is a sample policy module which rewrites all messages to "new message content":
80
81 ```elixir
82 defmodule Pleroma.Web.ActivityPub.MRF.RewritePolicy do
83 @moduledoc "MRF policy which rewrites all Notes to have 'new message content'."
84 @behaviour Pleroma.Web.ActivityPub.MRF
85
86 # Catch messages which contain Note objects with actual data to filter.
87 # Capture the object as `object`, the message content as `content` and the
88 # message itself as `message`.
89 @impl true
90 def filter(
91 %{"type" => "Create", "object" => %{"type" => "Note", "content" => content} = object} =
92 message
93 )
94 when is_binary(content) do
95 # Subject / CW is stored as summary instead of `name` like other AS2 objects
96 # because of Mastodon doing it that way.
97 summary = object["summary"]
98
99 # Message edits go here.
100 content = "new message content"
101
102 # Assemble the mutated object.
103 object =
104 object
105 |> Map.put("content", content)
106 |> Map.put("summary", summary)
107
108 # Assemble the mutated message.
109 message = Map.put(message, "object", object)
110 {:ok, message}
111 end
112
113 # Let all other messages through without modifying them.
114 @impl true
115 def filter(message), do: {:ok, message}
116
117 @impl true
118 def describe do
119 {:ok, %{mrf_sample: %{content: "new message content"}}}
120 end
121 end
122 ```
123
124 If you save this file as `lib/pleroma/web/activity_pub/mrf/rewrite_policy.ex`, it will be included when you next rebuild Pleroma. You can enable it in the configuration like so:
125
126 ```elixir
127 config :pleroma, :instance,
128 rewrite_policy: [
129 Pleroma.Web.ActivityPub.MRF.SimplePolicy,
130 Pleroma.Web.ActivityPub.MRF.RewritePolicy
131 ]
132 ```
133
134 Please note that the Pleroma developers consider custom MRF policy modules to fall under the purview of the AGPL. As such, you are obligated to release the sources to your custom MRF policy modules upon request.