Fix headers and add !929 docs to extras
[akkoma] / docs / Message-Rewrite-Facility-configuration.md
1 # Message Rewrite Facility configuration
2 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.
3
4 Possible uses include:
5
6 * marking incoming messages with media from a given account or instance as sensitive
7 * rejecting messages from a specific instance
8 * removing/unlisting messages from the public timelines
9 * removing media from messages
10 * sending only public messages to a specific instance
11
12 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.
13 It is possible to use multiple, active MRF policies at the same time.
14
15 ## Quarantine Instances
16
17 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.
18
19 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.
20 ```
21 config :pleroma, :instance,
22 [...]
23 quarantined_instances: ["instance.example", "other.example"]
24 ```
25
26 ## Using `SimplePolicy`
27
28 `SimplePolicy` is capable of handling most common admin tasks.
29
30 To use `SimplePolicy`, you must enable it. Do so by adding the following to your `:instance` config object, so that it looks like this:
31
32 ```
33 config :pleroma, :instance,
34 [...]
35 rewrite_policy: Pleroma.Web.ActivityPub.MRF.SimplePolicy
36 ```
37
38 Once `SimplePolicy` is enabled, you can configure various groups in the `:mrf_simple` config object. These groups are:
39
40 * `media_removal`: Servers in this group will have media stripped from incoming messages.
41 * `media_nsfw`: Servers in this group will have the #nsfw tag and sensitive setting injected into incoming messages which contain media.
42 * `reject`: Servers in this group will have their messages rejected.
43 * `federated_timeline_removal`: Servers in this group will have their messages unlisted from the public timelines by flipping the `to` and `cc` fields.
44
45 Servers should be configured as lists.
46
47 ### Example
48
49 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` and remove messages from `spam.university` from the federated timeline:
50
51 ```
52 config :pleroma, :instance,
53 rewrite_policy: [Pleroma.Web.ActivityPub.MRF.SimplePolicy]
54
55 config :pleroma, :mrf_simple,
56 media_removal: ["illegalporn.biz"],
57 media_nsfw: ["porn.biz", "porn.business"],
58 reject: ["spam.com"],
59 federated_timeline_removal: ["spam.university"]
60
61 ```
62
63 ### Use with Care
64
65 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.
66
67 ## Writing your own MRF Policy
68
69 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.
70
71 For example, here is a sample policy module which rewrites all messages to "new message content":
72
73 ```!elixir
74 # This is a sample MRF policy which rewrites all Notes to have "new message
75 # content."
76 defmodule Site.RewritePolicy do
77 @behavior Pleroma.Web.ActivityPub.MRF
78
79 # Catch messages which contain Note objects with actual data to filter.
80 # Capture the object as `object`, the message content as `content` and the
81 # message itself as `message`.
82 @impl true
83 def filter(%{"type" => Create", "object" => {"type" => "Note", "content" => content} = object} = message)
84 when is_binary(content) do
85 # Subject / CW is stored as summary instead of `name` like other AS2 objects
86 # because of Mastodon doing it that way.
87 summary = object["summary"]
88
89 # Message edits go here.
90 content = "new message content"
91
92 # Assemble the mutated object.
93 object =
94 object
95 |> Map.put("content", content)
96 |> Map.put("summary", summary)
97
98 # Assemble the mutated message.
99 message = Map.put(message, "object", object)
100 {:ok, message}
101 end
102
103 # Let all other messages through without modifying them.
104 @impl true
105 def filter(message), do: {:ok, message}
106 end
107 ```
108
109 If you save this file as `lib/site/mrf/rewrite_policy.ex`, it will be included when you next rebuild Pleroma. You can enable it in the configuration like so:
110
111 ```
112 config :pleroma, :instance,
113 rewrite_policy: [
114 Pleroma.Web.ActivityPub.MRF.SimplePolicy,
115 Site.RewritePolicy
116 ]
117 ```
118
119 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.