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
11 def on_shell(username, _pubkey, _ip, _port) do
12 :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
13 user = Pleroma.User.get_by_nickname(to_string(username))
14 Logger.debug("#{inspect(user)}")
15 loop(run_state(user: user))
18 def on_connect(username, ip, port, method) do
21 Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{
23 } using #{inspect(method)}
28 def on_disconnect(username, ip, port) do
30 "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
36 counter = state.counter
40 input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
41 wait_input(state, input)
44 def puts_activity(activity) do
45 status = Pleroma.Web.MastodonAPI.StatusView.render("status.json", %{activity: activity})
46 IO.puts("-- #{status.id} by #{status.account.display_name} (#{status.account.acct})")
47 IO.puts(HtmlSanitizeEx.strip_tags(status.content))
51 def handle_command(state, "help") do
52 IO.puts("Available commands:")
53 IO.puts("help - This help")
54 IO.puts("home - Show the home timeline")
55 IO.puts("p <text> - Post the given text")
56 IO.puts("r <id> <text> - Reply to the post with the given id")
57 IO.puts("quit - Quit")
62 def handle_command(%{user: user} = state, "r " <> text) do
63 text = String.trim(text)
64 [activity_id, rest] = String.split(text, " ", parts: 2)
66 with %Activity{} <- Activity.get_by_id(activity_id),
68 CommonAPI.post(user, %{"status" => rest, "in_reply_to_status_id" => activity_id}) do
71 _e -> IO.puts("Could not reply...")
77 def handle_command(%{user: user} = state, "p " <> text) do
78 text = String.trim(text)
80 with {:ok, _activity} <- CommonAPI.post(user, %{"status" => text}) do
83 _e -> IO.puts("Could not post...")
89 def handle_command(state, "home") do
94 |> Map.put("type", ["Create", "Announce"])
95 |> Map.put("blocking_user", user)
96 |> Map.put("muting_user", user)
97 |> Map.put("user", user)
100 [user.ap_id | user.following]
101 |> ActivityPub.fetch_activities(params)
102 |> ActivityPub.contain_timeline(user)
105 Enum.each(activities, fn activity ->
106 puts_activity(activity)
112 def handle_command(_state, command) do
113 IO.puts("Unknown command '#{command}'")
116 defp wait_input(state, input) do
118 {:input, ^input, "quit\n"} ->
119 IO.puts("Exiting...")
121 {:input, ^input, code} when is_binary(code) ->
122 code = String.trim(code)
124 state = handle_command(state, code)
126 loop(%{state | counter: state.counter + 1})
128 {:error, :interrupted} ->
129 IO.puts("Caught Ctrl+C...")
130 loop(%{state | counter: state.counter + 1})
132 {:input, ^input, msg} ->
133 :ok = Logger.warn("received unknown message: #{inspect(msg)}")
134 loop(%{state | counter: state.counter + 1})
138 defp run_state(opts) do
139 %{prefix: "pleroma", counter: 1, user: opts[:user]}
142 defp io_get(pid, prefix, counter, username) do
143 prompt = prompt(prefix, counter, username)
144 send(pid, {:input, self(), IO.gets(:stdio, prompt)})
147 defp prompt(prefix, counter, username) do
148 prompt = "#{username}@#{prefix}:#{counter}>"