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}:#{inspect port} using #{inspect method}
25 def on_disconnect(username, ip, port) do
27 "Disconnecting SSH shell for #{username} from #{inspect ip}:#{inspect port}"
33 counter = state.counter
37 input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
38 wait_input state, input
41 def puts_activity(activity) do
42 status = Pleroma.Web.MastodonAPI.StatusView.render("status.json", %{activity: activity})
43 IO.puts("#{status.id} by #{status.account.display_name} (#{status.account.acct}):")
44 IO.puts(HtmlSanitizeEx.strip_tags(status.content))
48 def handle_command(state, "help") do
49 IO.puts("Available commands:")
50 IO.puts("help - This help")
51 IO.puts("home - Show the home timeline")
52 IO.puts("p <text> - Post the given text")
53 IO.puts("quit - Quit")
58 def handle_command(%{user: user} = state, "p " <> text) do
59 text = String.trim(text)
61 with {:ok, _activity} <- CommonAPI.post(user, %{"status" => text}) do
64 _e -> IO.puts("Could not post...")
69 def handle_command(state, "home") do
73 |> Map.put("type", ["Create", "Announce"])
74 |> Map.put("blocking_user", user)
75 |> Map.put("muting_user", user)
76 |> Map.put("user", user)
79 [user.ap_id | user.following]
80 |> ActivityPub.fetch_activities(params)
81 |> ActivityPub.contain_timeline(user)
84 Enum.each(activities, fn (activity) ->
85 puts_activity(activity)
91 def handle_command(_state, command) do
92 IO.puts("Unknown command '#{command}'")
95 defp wait_input(state, input) do
97 {:input, ^input, "quit\n"} ->
100 {:input, ^input, code} when is_binary(code) ->
101 code = String.trim(code)
103 handle_command(state, code)
105 loop(%{state | counter: state.counter + 1})
107 {:error, :interrupted} ->
108 IO.puts "Caught Ctrl+C..."
109 loop(%{state | counter: state.counter + 1})
111 {:input, ^input, msg} ->
112 :ok = Logger.warn "received unknown message: #{inspect msg}"
113 loop(%{state | counter: state.counter + 1})
117 defp run_state(opts) do
118 %{prefix: "pleroma", counter: 1, user: opts[:user]}
121 defp io_get(pid, prefix, counter, username) do
122 prompt = prompt(prefix, counter, username)
123 send pid, {:input, self(), IO.gets(:stdio, prompt)}
126 defp prompt(prefix, counter, username) do
127 prompt = "#{username}@#{prefix}:#{counter}>"