1 defmodule Pleroma.BBS.Handler do
4 alias Pleroma.Web.ActivityPub.ActivityPub
5 alias Pleroma.Web.CommonAPI
7 def on_shell(username, _pubkey, _ip, _port) do
8 :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
9 user = Pleroma.User.get_cached_by_nickname(to_string(username))
10 Logger.debug("#{inspect(user)}")
11 loop(run_state(user: user))
14 def on_connect(username, ip, port, method) do
17 Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{
19 } using #{inspect(method)}
24 def on_disconnect(username, ip, port) do
26 "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
32 counter = state.counter
36 input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
37 wait_input(state, input)
40 def puts_activity(activity) do
41 status = Pleroma.Web.MastodonAPI.StatusView.render("status.json", %{activity: activity})
42 IO.puts("-- #{status.id} by #{status.account.display_name} (#{status.account.acct})")
43 IO.puts(HtmlSanitizeEx.strip_tags(status.content))
47 def handle_command(state, "help") do
48 IO.puts("Available commands:")
49 IO.puts("help - This help")
50 IO.puts("home - Show the home timeline")
51 IO.puts("p <text> - Post the given text")
52 IO.puts("r <id> <text> - Reply to the post with the given id")
53 IO.puts("quit - Quit")
58 def handle_command(%{user: user} = state, "r " <> text) do
59 text = String.trim(text)
60 [activity_id, rest] = String.split(text, " ", parts: 2)
62 with %Activity{} <- Activity.get_by_id(activity_id),
64 CommonAPI.post(user, %{"status" => rest, "in_reply_to_status_id" => activity_id}) do
67 _e -> IO.puts("Could not reply...")
73 def handle_command(%{user: user} = state, "p " <> text) do
74 text = String.trim(text)
76 with {:ok, _activity} <- CommonAPI.post(user, %{"status" => text}) do
79 _e -> IO.puts("Could not post...")
85 def handle_command(state, "home") do
90 |> Map.put("type", ["Create"])
91 |> Map.put("blocking_user", user)
92 |> Map.put("muting_user", user)
93 |> Map.put("user", user)
96 [user.ap_id | user.following]
97 |> ActivityPub.fetch_activities(params)
99 Enum.each(activities, fn activity ->
100 puts_activity(activity)
106 def handle_command(state, command) do
107 IO.puts("Unknown command '#{command}'")
111 defp wait_input(state, input) do
113 {:input, ^input, "quit\n"} ->
114 IO.puts("Exiting...")
116 {:input, ^input, code} when is_binary(code) ->
117 code = String.trim(code)
119 state = handle_command(state, code)
121 loop(%{state | counter: state.counter + 1})
123 {:error, :interrupted} ->
124 IO.puts("Caught Ctrl+C...")
125 loop(%{state | counter: state.counter + 1})
127 {:input, ^input, msg} ->
128 :ok = Logger.warn("received unknown message: #{inspect(msg)}")
129 loop(%{state | counter: state.counter + 1})
133 defp run_state(opts) do
134 %{prefix: "pleroma", counter: 1, user: opts[:user]}
137 defp io_get(pid, prefix, counter, username) do
138 prompt = prompt(prefix, counter, username)
139 send(pid, {:input, self(), IO.gets(:stdio, prompt)})
142 defp prompt(prefix, counter, username) do
143 prompt = "#{username}@#{prefix}:#{counter}>"