summaryrefslogtreecommitdiff
path: root/lib/chessh/shell.ex
blob: cf65ad43d593c7504d37899748ea4ef1b61bdb19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
defmodule Chessh.Shell do
  use Sshd.ShellHandler

  def on_shell(_username, _pubkey, _ip, _port) do
    IO.puts("Looks like you're on #{inspect(self())}")
    loop()
  end

  def on_connect(username, ip, port, method) do
    Logger.debug(fn ->
      """
      Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{inspect(port)} using #{inspect(method)}
      """
    end)
  end

  def on_disconnect(username, ip, port) do
    Logger.debug(fn ->
      "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
    end)
  end

  defp loop() do
    self_pid = self()
    IO.write([IO.ANSI.home(), IO.ANSI.clear()])
    IO.puts("#{inspect(:io.columns())}")
    IO.puts("#{inspect(:io.rows())}")
    input = spawn(fn -> io_get(self_pid) end)
    wait_input(input)
  end

  defp wait_input(input) do
    receive do
      {:hello, message} ->
        IO.puts(message)
        loop()

      {:input, ^input, x} ->
        IO.puts(x)
        loop()

      x ->
        Logger.debug(inspect(x))
        loop()
    end
  end

  defp io_get(pid) do
    send(pid, {:input, self(), IO.gets("")})
  end
end