1 defmodule Pleroma.BBS.Handler do
3 An example implementation of `Sshd.ShellHandler`, implementing a very simple
4 Read-Eval-Loop, that does nothing.
7 alias Pleroma.Web.CommonAPI
8 alias Pleroma.Web.ActivityPub.ActivityPub
10 def on_shell(username, _pubkey, _ip, _port) do
11 :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
12 user = Pleroma.User.get_by_nickname(to_string(username))
13 Logger.debug("#{inspect(user)}")
14 loop(run_state(user: user))
17 def on_connect(username, ip, port, method) do
20 Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{
22 } using #{inspect(method)}
27 def on_disconnect(username, ip, port) do
29 "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
35 counter = state.counter
39 input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
40 wait_input(state, input)
43 def puts_activity(activity) do
44 status = Pleroma.Web.MastodonAPI.StatusView.render("status.json", %{activity: activity})
45 IO.puts("#{status.id} by #{status.account.display_name} (#{status.account.acct}):")
46 IO.puts(HtmlSanitizeEx.strip_tags(status.content))
50 def handle_command(state, "help") do
51 IO.puts("Available commands:")
52 IO.puts("help - This help")
53 IO.puts("home - Show the home timeline")
54 IO.puts("p <text> - Post the given text")
55 IO.puts("quit - Quit")
60 def handle_command(%{user: user} = state, "p " <> text) do
61 text = String.trim(text)
63 with {:ok, _activity} <- CommonAPI.post(user, %{"status" => text}) do
66 _e -> IO.puts("Could not post...")
72 def handle_command(state, "home") do
77 |> Map.put("type", ["Create", "Announce"])
78 |> Map.put("blocking_user", user)
79 |> Map.put("muting_user", user)
80 |> Map.put("user", user)
83 [user.ap_id | user.following]
84 |> ActivityPub.fetch_activities(params)
85 |> ActivityPub.contain_timeline(user)
88 Enum.each(activities, fn activity ->
89 puts_activity(activity)
95 def handle_command(_state, command) do
96 IO.puts("Unknown command '#{command}'")
99 defp wait_input(state, input) do
101 {:input, ^input, "quit\n"} ->
102 IO.puts("Exiting...")
104 {:input, ^input, code} when is_binary(code) ->
105 code = String.trim(code)
107 handle_command(state, code)
109 loop(%{state | counter: state.counter + 1})
111 {:error, :interrupted} ->
112 IO.puts("Caught Ctrl+C...")
113 loop(%{state | counter: state.counter + 1})
115 {:input, ^input, msg} ->
116 :ok = Logger.warn("received unknown message: #{inspect(msg)}")
117 loop(%{state | counter: state.counter + 1})
121 defp run_state(opts) do
122 %{prefix: "pleroma", counter: 1, user: opts[:user]}
125 defp io_get(pid, prefix, counter, username) do
126 prompt = prompt(prefix, counter, username)
127 send(pid, {:input, self(), IO.gets(:stdio, prompt)})
130 defp prompt(prefix, counter, username) do
131 prompt = "#{username}@#{prefix}:#{counter}>"