logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

mrf.md (5348B)


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