1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.BBS.Handler do
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.CommonAPI
12 def on_shell(username, _pubkey, _ip, _port) do
13 :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
14 user = Pleroma.User.get_cached_by_nickname(to_string(username))
15 Logger.debug("#{inspect(user)}")
16 loop(run_state(user: user))
19 def on_connect(username, ip, port, method) do
22 Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{inspect(port)} 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("show.json", %{activity: activity})
46 IO.puts("-- #{status.id} by #{status.account.display_name} (#{status.account.acct})")
49 |> String.split("<br/>")
50 |> Enum.map(&HTML.strip_tags/1)
51 |> Enum.map(&HtmlEntities.decode/1)
52 |> Enum.map(&IO.puts/1)
55 def puts_notification(activity, user) do
57 Pleroma.Web.MastodonAPI.NotificationView.render("show.json", %{
58 notification: activity,
63 "== (#{notification.type}) #{notification.status.id} by #{notification.account.display_name} (#{notification.account.acct})"
66 notification.status.content
67 |> String.split("<br/>")
68 |> Enum.map(&HTML.strip_tags/1)
69 |> Enum.map(&HtmlEntities.decode/1)
78 |> String.slice(1, 80)
79 |> (fn x -> "> " <> x <> "..." end).()
87 def handle_command(state, "help") do
88 IO.puts("Available commands:")
89 IO.puts("help - This help")
90 IO.puts("home - Show the home timeline")
91 IO.puts("p <text> - Post the given text")
92 IO.puts("r <id> <text> - Reply to the post with the given id")
93 IO.puts("t <id> - Show a thread from the given id")
94 IO.puts("n - Show notifications")
95 IO.puts("n read - Mark all notifactions as read")
96 IO.puts("f <id> - Favourites the post with the given id")
97 IO.puts("R <id> - Repeat the post with the given id")
98 IO.puts("quit - Quit")
103 def handle_command(%{user: user} = state, "r " <> text) do
104 text = String.trim(text)
105 [activity_id, rest] = String.split(text, " ", parts: 2)
107 with %Activity{} <- Activity.get_by_id(activity_id),
109 CommonAPI.post(user, %{status: rest, in_reply_to_status_id: activity_id}) do
112 _e -> IO.puts("Could not reply...")
118 def handle_command(%{user: user} = state, "t " <> activity_id) do
119 with %Activity{} = activity <- Activity.get_by_id(activity_id) do
121 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
124 exclude_id: activity.id
130 |> Activity.get_by_id()
136 |> Enum.each(&puts_activity/1)
139 _e -> IO.puts("Could not show this thread...")
145 def handle_command(%{user: user} = state, "n read") do
146 Pleroma.Notification.clear(user)
147 IO.puts("All notifications were marked as read")
152 def handle_command(%{user: user} = state, "n") do
154 |> Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(%{})
155 |> Enum.each(&puts_notification(&1, user))
160 def handle_command(%{user: user} = state, "p " <> text) do
161 text = String.trim(text)
163 with {:ok, activity} <- CommonAPI.post(user, %{status: text}) do
164 IO.puts("Posted! ID: #{activity.id}")
166 _e -> IO.puts("Could not post...")
172 def handle_command(%{user: user} = state, "f " <> id) do
175 with %Activity{} = activity <- Activity.get_by_id(id),
176 {:ok, _activity} <- CommonAPI.favorite(user, activity) do
177 IO.puts("Favourited!")
179 _e -> IO.puts("Could not Favourite...")
185 def handle_command(state, "home") do
190 |> Map.put(:type, ["Create"])
191 |> Map.put(:blocking_user, user)
192 |> Map.put(:muting_user, user)
193 |> Map.put(:user, user)
196 [user.ap_id | Pleroma.User.following(user)]
197 |> ActivityPub.fetch_activities(params)
199 Enum.each(activities, fn activity ->
200 puts_activity(activity)
206 def handle_command(state, command) do
207 IO.puts("Unknown command '#{command}'")
211 defp wait_input(state, input) do
213 {:input, ^input, "quit\n"} ->
214 IO.puts("Exiting...")
216 {:input, ^input, code} when is_binary(code) ->
217 code = String.trim(code)
219 state = handle_command(state, code)
221 loop(%{state | counter: state.counter + 1})
223 {:input, ^input, {:error, :interrupted}} ->
224 IO.puts("Caught Ctrl+C...")
225 loop(%{state | counter: state.counter + 1})
227 {:input, ^input, msg} ->
228 :ok = Logger.warn("received unknown message: #{inspect(msg)}")
229 loop(%{state | counter: state.counter + 1})
233 defp run_state(opts) do
234 %{prefix: "pleroma", counter: 1, user: opts[:user]}
237 defp io_get(pid, prefix, counter, username) do
238 prompt = prompt(prefix, counter, username)
239 send(pid, {:input, self(), IO.gets(:stdio, prompt)})
242 defp prompt(prefix, counter, username) do
243 prompt = "#{username}@#{prefix}:#{counter}>"