defmodule SCLI do @moduledoc """ Small command line interface for the chat application """ defmodule State do defstruct [:room_pid, :id_pid, :pk] end def run() do for {_chid, %SApp.Chat.Manifest{}, chpid} <- Shard.Manager.list_shards do SApp.Chat.subscribe(chpid) end pk = Shard.Keys.get_any_identity room_pid = Shard.Manager.find_or_start %SApp.Chat.Manifest{channel: "lobby"} run(%State{room_pid: room_pid, id_pid: nil, pk: pk}) end defp run(state) do handle_messages(state) id_pid = case state.id_pid do nil -> SApp.Identity.find_proc(state.pk) x -> x end state = put_in(state.id_pid, id_pid) nick = case id_pid do nil -> SApp.Identity.default_nick(state.pk) _ -> SApp.Identity.get_info(id_pid).nick end prompt = case state.room_pid do nil -> "(no channel) #{nick}: " _ -> case SApp.Chat.get_manifest(state.room_pid) do %SApp.Chat.Manifest{channel: chan} -> "##{chan} #{nick}: " %SApp.Chat.PrivChat.Manifest{pk_list: pk_list} -> nicks = pk_list |> Enum.filter(&(&1 != state.pk)) |> Enum.map(&("#{SApp.Identity.get_nick &1} #{Shard.Keys.pk_display &1}")) |> Enum.join(", ") "PM #{nicks} #{nick}: " end end str = prompt |> IO.gets |> String.trim cond do str == "/quit" -> nil String.slice(str, 0..0) == "/" -> command = str |> String.slice(1..-1) |> String.split(" ") state = handle_command(state, command) run(state) true -> if str != "" do SApp.Chat.chat_send(state.room_pid, state.pk, str) end run(state) end end defp handle_messages(state) do receive do {:chat_recv, manifest, {pk, msgbin, _sign}} -> {ts, msg} = SData.term_unbin msgbin nick = SApp.Identity.get_nick pk case manifest do %SApp.Chat.Manifest{channel: chan} -> IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} ##{chan} <#{nick} #{Shard.Keys.pk_display pk}> #{msg}" %SApp.Chat.PrivChat.Manifest{pk_list: pk_list} -> IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} PM(#{Enum.count pk_list}) <#{nick} #{Shard.Keys.pk_display pk}> #{msg}" end handle_messages(state) {:chat_send, _, _} -> # do nothing handle_messages(state) after 10 -> nil end end defp handle_command(state, ["connect", ipstr, portstr]) do {:ok, ip} = :inet.parse_address (to_charlist ipstr) {port, _} = Integer.parse portstr SNet.Manager.add_peer({:inet, ip, port}) state end defp handle_command(state, ["list"]) do IO.puts "List of known channels:" for {_chid, %SApp.Chat.Manifest{channel: chan}, _chpid} <- Shard.Manager.list_shards do IO.puts "##{chan}" end state end defp handle_command(state, ["hist"]) do if state.room_pid == nil do IO.puts "Not currently on a channel!" else SApp.Chat.read_history(state.room_pid, nil, 25) |> Enum.each(fn {{pk, msgbin, _sign}, true} -> {ts, msg} = SData.term_unbin msgbin nick = SApp.Identity.get_nick pk IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} <#{nick} #{Shard.Keys.pk_display pk}> #{msg}" end) end state end defp handle_command(state, ["join", qchan]) do pid = Shard.Manager.find_or_start %SApp.Chat.Manifest{channel: qchan} SApp.Chat.subscribe(pid) IO.puts "Switching to ##{qchan}" %{state | room_pid: pid} end defp handle_command(state, ["pm" | people_list]) do known_people = for {_, %SApp.Identity.Manifest{pk: pk}, pid} <- Shard.Manager.list_shards() do info = SApp.Identity.get_info(pid) {pk, info.nick} end pk_list = for qname <- people_list do candidates = for {pk, nick} <- known_people, :binary.longest_common_prefix([qname, nick]) == byte_size(qname) or :binary.longest_common_prefix([qname, Shard.Keys.pk_display pk]) == byte_size(qname), do: {pk, nick} case candidates do [] -> IO.puts "Not found: #{qname}" :error [{pk, _}] -> pk _ -> IO.puts "Several people matching for #{qname}:" for {pk, nick} <- candidates do IO.puts "- #{nick} #{Shard.Keys.pk_display pk}" end :error end end if Enum.all?(pk_list, &(&1 != :error)) do manifest = SApp.Chat.PrivChat.Manifest.new([state.pk | pk_list]) pid = Shard.Manager.find_or_start manifest SApp.Chat.subscribe(pid) IO.puts "Switching to private conversation." %{state | room_pid: pid} else state end end defp handle_command(state, ["nick", nick]) do pid = case state.id_pid do nil -> SApp.Identity.find_proc state.pk x -> x end if pid == nil do IO.puts "Sorry, we have a problem with the identity shard" else info = SApp.Identity.get_info(pid) SApp.Identity.set_info(pid, %{info | nick: nick}) end state end defp handle_command(state, _cmd) do IO.puts "Invalid command" state end end