1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Emoji do
9 @shortdoc "Manages emoji packs"
10 @moduledoc File.read!("docs/administration/CLI_tasks/emoji.md")
12 def run(["ls-packs" | args]) do
15 {options, [], []} = parse_global_opts(args)
17 url_or_path = options[:manifest] || default_manifest()
18 manifest = fetch_and_decode!(url_or_path)
20 Enum.each(manifest, fn {name, info} ->
23 {"Homepage", info["homepage"]},
24 {"Description", info["description"]},
25 {"License", info["license"]},
26 {"Source", info["src"]}
29 for {param, value} <- to_print do
30 IO.puts(IO.ANSI.format([:bright, param, :normal, ": ", value]))
38 def run(["get-packs" | args]) do
41 {options, pack_names, []} = parse_global_opts(args)
43 url_or_path = options[:manifest] || default_manifest()
45 manifest = fetch_and_decode!(url_or_path)
47 for pack_name <- pack_names do
48 if Map.has_key?(manifest, pack_name) do
49 pack = manifest[pack_name]
64 {:ok, binary_archive} = fetch(src)
65 archive_sha = :crypto.hash(:sha256, binary_archive) |> Base.encode16()
67 sha_status_text = ["SHA256 of ", :bright, pack_name, :normal, " source file is ", :bright]
69 if archive_sha == String.upcase(pack["src_sha256"]) do
70 IO.puts(IO.ANSI.format(sha_status_text ++ [:green, "OK"]))
72 IO.puts(IO.ANSI.format(sha_status_text ++ [:red, "BAD"]))
74 raise "Bad SHA256 for #{pack_name}"
77 # The location specified in files should be in the same directory
81 |> Path.join(pack["files"])
85 "Fetching the file list for ",
95 files = fetch_and_decode!(files_loc)
97 IO.puts(IO.ANSI.format(["Unpacking ", :bright, pack_name]))
101 Pleroma.Config.get!([:instance, :static_dir]),
109 fn {_, f} -> to_charlist(f) end
113 :zip.unzip(binary_archive,
115 file_list: files_to_unzip
118 IO.puts(IO.ANSI.format(["Writing pack.json for ", :bright, pack_name]))
122 "license" => pack["license"],
123 "homepage" => pack["homepage"],
124 "description" => pack["description"],
125 "fallback-src" => pack["src"],
126 "fallback-src-sha256" => pack["src_sha256"],
127 "share-files" => true
132 File.write!(Path.join(pack_path, "pack.json"), Jason.encode!(pack_json, pretty: true))
134 IO.puts(IO.ANSI.format([:bright, :red, "No pack named \"#{pack_name}\" found"]))
139 def run(["gen-pack" | args]) do
149 description: :string,
155 proposed_name = Path.basename(src) |> Path.rootname()
156 name = get_option(opts, :name, "Pack name:", proposed_name)
157 license = get_option(opts, :license, "License:")
158 homepage = get_option(opts, :homepage, "Homepage:")
159 description = get_option(opts, :description, "Description:")
161 proposed_files_name = "#{name}_files.json"
162 files_name = get_option(opts, :files, "Save file list to:", proposed_files_name)
164 default_exts = [".png", ".gif"]
170 "Emoji file extensions (separated with spaces):",
171 Enum.join(default_exts, " ")
173 |> String.split(" ", trim: true)
176 if MapSet.equal?(MapSet.new(default_exts), MapSet.new(custom_exts)) do
182 IO.puts("Using #{Enum.join(exts, " ")} extensions")
184 IO.puts("Downloading the pack and generating SHA256")
186 binary_archive = Tesla.get!(client(), src).body
187 archive_sha = :crypto.hash(:sha256, binary_archive) |> Base.encode16()
189 IO.puts("SHA256 is #{archive_sha}")
195 description: description,
197 src_sha256: archive_sha,
202 tmp_pack_dir = Path.join(System.tmp_dir!(), "emoji-pack-#{name}")
204 {:ok, _} = :zip.unzip(binary_archive, cwd: String.to_charlist(tmp_pack_dir))
206 emoji_map = Pleroma.Emoji.Loader.make_shortcode_to_file_map(tmp_pack_dir, exts)
208 File.write!(files_name, Jason.encode!(emoji_map, pretty: true))
212 #{files_name} has been created and contains the list of all found emojis in the pack.
213 Please review the files in the pack and remove those not needed.
216 pack_file = "#{name}.json"
218 if File.exists?(pack_file) do
219 existing_data = File.read!(pack_file) |> Jason.decode!()
232 IO.puts("#{pack_file} has been updated with the #{name} pack")
234 File.write!(pack_file, Jason.encode!(pack_json, pretty: true))
236 IO.puts("#{pack_file} has been created with the #{name} pack")
240 def run(["reload"]) do
242 Pleroma.Emoji.reload()
243 IO.puts("Emoji packs have been reloaded.")
246 defp fetch_and_decode!(from) do
247 with {:ok, json} <- fetch(from) do
250 {:error, error} -> raise "#{from} cannot be fetched. Error: #{error} occur."
254 defp fetch("http" <> _ = from) do
255 with {:ok, %{body: body}} <- Tesla.get(client(), from) do
260 defp fetch(path), do: File.read(path)
262 defp parse_global_opts(args) do
276 {Tesla.Middleware.FollowRedirects, [max_redirects: 3]}
279 Tesla.client(middleware)
282 defp default_manifest, do: Pleroma.Config.get!([:emoji, :default_manifest])