aboutsummaryrefslogtreecommitdiff
path: root/lib/cli/cli.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/cli/cli.ex')
-rw-r--r--lib/cli/cli.ex32
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/cli/cli.ex b/lib/cli/cli.ex
index c281d57..8928040 100644
--- a/lib/cli/cli.ex
+++ b/lib/cli/cli.ex
@@ -8,6 +8,8 @@ defmodule SCLI do
end
defp run(pid) do
+ handle_messages()
+
nick = Shard.Identity.get_nickname
prompt = case pid do
nil -> "(no channel) #{nick}: "
@@ -25,11 +27,25 @@ defmodule SCLI do
pid2 = handle_command(pid, command)
run(pid2)
true ->
- GenServer.cast(pid, {:chat_send, str})
+ if str != "" do
+ GenServer.cast(pid, {:chat_send, str})
+ end
run(pid)
end
end
+ defp handle_messages() do
+ receive do
+ {:chat_recv, chan, {ts, nick, msg}} ->
+ IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} ##{chan} <#{nick}> #{msg}"
+ handle_messages()
+ {:chat_send, _, _} ->
+ # do nothing
+ handle_messages()
+ after 10 -> nil
+ end
+ end
+
defp handle_command(pid, ["connect", ipstr, portstr]) do
{:ok, ip} = :inet.parse_address (to_charlist ipstr)
{port, _} = Integer.parse portstr
@@ -47,6 +63,19 @@ defmodule SCLI do
pid
end
+ defp handle_command(pid, ["hist"]) do
+ case GenServer.call(pid, {:read_history, nil, 100}) do
+ {:ok, list, _rest} ->
+ list
+ |> Enum.reverse
+ |> Enum.each(fn {ts, nick, msg} ->
+ IO.puts "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} <#{nick}> #{msg}"
+ end)
+ _ -> nil
+ end
+ pid
+ end
+
defp handle_command(_pid, ["join", qchan]) do
list = for {_chid, manifest, chpid} <- :ets.tab2list(:shard_db),
{:chat, chan} = manifest,
@@ -54,6 +83,7 @@ defmodule SCLI do
case List.keyfind(list, qchan, 0) do
nil ->
{:ok, pid} = DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SApp.Chat, qchan})
+ GenServer.cast(pid, {:subscribe, self()})
pid
{_, pid} ->
IO.puts "Switching to ##{qchan}"