1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
10 @moduledoc "Detect new emojis by their shortcode and steals them"
11 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
13 defp accept_host?(host), do: host in Config.get([:mrf_steal_emoji, :hosts], [])
15 defp shortcode_matches?(shortcode, pattern) when is_binary(pattern) do
19 defp shortcode_matches?(shortcode, pattern) do
20 String.match?(shortcode, pattern)
23 defp steal_emoji({shortcode, url}, emoji_dir_path) do
24 url = Pleroma.Web.MediaProxy.url(url)
26 with {:ok, %{status: status} = response} when status in 200..299 <- Pleroma.HTTP.get(url) do
27 size_limit = Config.get([:mrf_steal_emoji, :size_limit], 50_000)
29 if byte_size(response.body) <= size_limit do
37 file_path = Path.join(emoji_dir_path, shortcode <> (extension || ".png"))
39 case File.write(file_path, response.body) do
44 Logger.warn("MRF.StealEmojiPolicy: Failed to write to #{file_path}: #{inspect(e)}")
49 "MRF.StealEmojiPolicy: :#{shortcode}: at #{url} (#{byte_size(response.body)} B) over size limit (#{size_limit} B)"
56 Logger.warn("MRF.StealEmojiPolicy: Failed to fetch #{url}: #{inspect(e)}")
62 def filter(%{"object" => %{"emoji" => foreign_emojis, "actor" => actor}} = message) do
63 host = URI.parse(actor).host
65 if host != Pleroma.Web.Endpoint.host() and accept_host?(host) do
66 installed_emoji = Pleroma.Emoji.get_all() |> Enum.map(fn {k, _} -> k end)
70 [:mrf_steal_emoji, :path],
71 Path.join(Config.get([:instance, :static_dir]), "emoji/stolen")
74 File.mkdir_p(emoji_dir_path)
78 |> Enum.reject(fn {shortcode, _url} -> shortcode in installed_emoji end)
79 |> Enum.filter(fn {shortcode, _url} ->
81 [:mrf_steal_emoji, :rejected_shortcodes]
83 |> Enum.find(false, fn pattern -> shortcode_matches?(shortcode, pattern) end)
87 |> Enum.map(&steal_emoji(&1, emoji_dir_path))
90 if !Enum.empty?(new_emojis) do
91 Logger.info("Stole new emojis: #{inspect(new_emojis)}")
92 Pleroma.Emoji.reload()
99 def filter(message), do: {:ok, message}
102 @spec config_description :: %{
105 description: <<_::272, _::_*256>>,
106 key: :hosts | :rejected_shortcodes | :size_limit,
107 suggestions: [any(), ...],
108 type: {:list, :string} | {:list, :string} | :integer
112 description: <<_::448>>,
113 key: :mrf_steal_emoji,
115 related_policy: <<_::352>>
117 def config_description do
119 key: :mrf_steal_emoji,
120 related_policy: "Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy",
122 description: "Steals emojis from selected instances when it sees them.",
126 type: {:list, :string},
127 description: "List of hosts to steal emojis from",
131 key: :rejected_shortcodes,
132 type: {:list, :string},
134 A list of patterns or matches to reject shortcodes with.
136 Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
138 suggestions: ["foo", ~r/foo/]
143 description: "File size limit (in bytes), checked before an emoji is saved to the disk",
144 suggestions: ["100000"]