1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Emoji do
8 @shortdoc "Manages emoji packs"
14 mix pleroma.emoji ls-packs [OPTION...]
16 Lists the emoji packs and metadata specified in the manifest.
20 - `-m, --manifest PATH/URL` - path to a custom manifest, it can either be an URL
21 starting with `http`, in that case the manifest will be fetched from that address,
26 mix pleroma.emoji get-packs [OPTION...] PACKS
28 Fetches, verifies and installs the specified PACKS from the manifest into
29 the `STATIC-DIR/emoji/PACK-NAME
33 - `-m, --manifest PATH/URL` - same as ls-packs
37 mix pleroma.emoji gen-pack PACK-URL
39 Creates a new manifest entry and a file list from the specified remote pack file.
40 Currently, only .zip archives are recognized as remote pack files and packs are therefore
41 assumed to be zip archives. This command is intended to run interactively and
42 will first ask you some basic questions about the pack, then download the remote
43 file and generate an MD5 signature for it, then generate an emoji file list for you.
45 The manifest entry will either be written to a newly created `index.json` file or appended to the existing one,
46 *replacing* the old pack with the same name if it was in the file previously.
48 The file list will be written to the file specified previously, *replacing* that file.
49 You _should_ check that the file list doesn't contain anything you don't need in the pack, that is,
50 anything that is not an emoji (the whole pack is downloaded, but only emoji files are extracted).
54 @default_manifest Pleroma.Config.get!([:emoji, :default_manifest])
56 def run(["ls-packs" | args]) do
57 Application.ensure_all_started(:hackney)
59 {options, [], []} = parse_global_opts(args)
62 fetch_manifest(if options[:manifest], do: options[:manifest], else: @default_manifest)
64 Enum.each(manifest, fn {name, info} ->
67 {"Homepage", info["homepage"]},
68 {"Description", info["description"]},
69 {"License", info["license"]},
70 {"Source", info["src"]}
73 for {param, value} <- to_print do
74 IO.puts(IO.ANSI.format([:bright, param, :normal, ": ", value]))
82 def run(["get-packs" | args]) do
83 Application.ensure_all_started(:hackney)
85 {options, pack_names, []} = parse_global_opts(args)
87 manifest_url = if options[:manifest], do: options[:manifest], else: @default_manifest
89 manifest = fetch_manifest(manifest_url)
91 for pack_name <- pack_names do
92 if Map.has_key?(manifest, pack_name) do
93 pack = manifest[pack_name]
108 binary_archive = Tesla.get!(src_url).body
109 archive_md5 = :crypto.hash(:md5, binary_archive) |> Base.encode16()
111 md5_status_text = ["MD5 of ", :bright, pack_name, :normal, " source file is ", :bright]
113 if archive_md5 == String.upcase(pack["src_md5"]) do
114 IO.puts(IO.ANSI.format(md5_status_text ++ [:green, "OK"]))
116 IO.puts(IO.ANSI.format(md5_status_text ++ [:red, "BAD"]))
118 raise "Bad MD5 for #{pack_name}"
121 # The url specified in files should be in the same directory
122 files_url = Path.join(Path.dirname(manifest_url), pack["files"])
126 "Fetching the file list for ",
136 files = Tesla.get!(files_url).body |> Poison.decode!()
138 IO.puts(IO.ANSI.format(["Unpacking ", :bright, pack_name]))
142 Pleroma.Config.get!([:instance, :static_dir]),
150 fn {_, f} -> to_charlist(f) end
154 :zip.unzip(binary_archive,
156 file_list: files_to_unzip
159 IO.puts(IO.ANSI.format(["Writing emoji.txt for ", :bright, pack_name]))
164 fn {shortcode, path} ->
165 emojo_path = Path.join("/emoji/#{pack_name}", path)
166 "#{shortcode}, #{emojo_path}"
171 File.write!(Path.join(pack_path, "emoji.txt"), emoji_txt_str)
173 IO.puts(IO.ANSI.format([:bright, :red, "No pack named \"#{pack_name}\" found"]))
178 def run(["gen-pack", src]) do
179 Application.ensure_all_started(:hackney)
181 proposed_name = Path.basename(src) |> Path.rootname()
182 name = String.trim(IO.gets("Pack name [#{proposed_name}]: "))
183 # If there's no name, use the default one
184 name = if String.length(name) > 0, do: name, else: proposed_name
186 license = String.trim(IO.gets("License: "))
187 homepage = String.trim(IO.gets("Homepage: "))
188 description = String.trim(IO.gets("Description: "))
190 proposed_files_name = "#{name}.json"
191 files_name = String.trim(IO.gets("Save file list to [#{proposed_files_name}]: "))
192 files_name = if String.length(files_name) > 0, do: files_name, else: proposed_files_name
194 default_exts = [".png", ".gif"]
195 default_exts_str = Enum.join(default_exts, " ")
199 IO.gets("Emoji file extensions (separated with spaces) [#{default_exts_str}]: ")
203 if String.length(exts) > 0 do
204 String.split(exts, " ")
205 |> Enum.filter(fn e -> e |> String.trim() |> String.length() > 0 end)
210 IO.puts("Downloading the pack and generating MD5")
212 binary_archive = Tesla.get!(src).body
213 archive_md5 = :crypto.hash(:md5, binary_archive) |> Base.encode16()
215 IO.puts("MD5 is #{archive_md5}")
221 description: description,
223 src_md5: archive_md5,
228 tmp_pack_dir = Path.join(System.tmp_dir!(), "emoji-pack-#{name}")
236 emoji_map = Pleroma.Emoji.make_shortcode_to_file_map(tmp_pack_dir, exts)
238 File.write!(files_name, Poison.encode!(emoji_map, pretty: true))
242 #{files_name} has been created and contains the list of all found emojis in the pack.
243 Please review the files in the remove those not needed.
246 if File.exists?("index.json") do
247 existing_data = File.read!("index.json") |> Poison.decode!()
260 IO.puts("index.json file has been update with the #{name} pack")
262 File.write!("index.json", Poison.encode!(pack_json, pretty: true))
264 IO.puts("index.json has been created with the #{name} pack")
268 defp fetch_manifest(from) do
270 if String.starts_with?(from, "http") do
271 Tesla.get!(from).body
278 defp parse_global_opts(args) do