2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-25 23:11:42 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 23:13:23 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-03-31 10:01:16 -06:00
|
|
|
defmodule Pleroma.BBS.Handler do
|
|
|
|
use Sshd.ShellHandler
|
2019-05-04 07:06:18 -06:00
|
|
|
alias Pleroma.Activity
|
2019-10-30 00:20:13 -06:00
|
|
|
alias Pleroma.HTML
|
2019-03-31 10:01:16 -06:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2019-05-04 07:47:50 -06:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2019-03-31 10:01:16 -06:00
|
|
|
|
|
|
|
def on_shell(username, _pubkey, _ip, _port) do
|
2019-03-31 12:35:10 -06:00
|
|
|
:ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
|
2019-05-04 07:08:07 -06:00
|
|
|
user = Pleroma.User.get_cached_by_nickname(to_string(username))
|
2019-03-31 12:35:10 -06:00
|
|
|
Logger.debug("#{inspect(user)}")
|
|
|
|
loop(run_state(user: user))
|
2019-03-31 10:01:16 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_connect(username, ip, port, method) do
|
2019-03-31 12:35:10 -06:00
|
|
|
Logger.debug(fn ->
|
2019-03-31 10:01:16 -06:00
|
|
|
"""
|
2021-10-06 00:08:21 -06:00
|
|
|
Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{inspect(port)} using #{inspect(method)}
|
2019-03-31 10:01:16 -06:00
|
|
|
"""
|
2019-03-31 12:35:10 -06:00
|
|
|
end)
|
2019-03-31 10:01:16 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_disconnect(username, ip, port) do
|
2019-03-31 12:35:10 -06:00
|
|
|
Logger.debug(fn ->
|
|
|
|
"Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
|
|
|
|
end)
|
2019-03-31 10:01:16 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp loop(state) do
|
|
|
|
self_pid = self()
|
2019-03-31 12:35:10 -06:00
|
|
|
counter = state.counter
|
|
|
|
prefix = state.prefix
|
|
|
|
user = state.user
|
2019-03-31 10:01:16 -06:00
|
|
|
|
|
|
|
input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
|
2019-03-31 12:35:10 -06:00
|
|
|
wait_input(state, input)
|
2019-03-31 10:01:16 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def puts_activity(activity) do
|
2019-09-09 08:49:02 -06:00
|
|
|
status = Pleroma.Web.MastodonAPI.StatusView.render("show.json", %{activity: activity})
|
2022-05-20 20:17:34 -06:00
|
|
|
|
2019-03-31 13:14:21 -06:00
|
|
|
IO.puts("-- #{status.id} by #{status.account.display_name} (#{status.account.acct})")
|
2022-05-20 20:17:34 -06:00
|
|
|
|
|
|
|
status.content
|
|
|
|
|> String.split("<br/>")
|
|
|
|
|> Enum.map(&HTML.strip_tags/1)
|
|
|
|
|> Enum.map(&HtmlEntities.decode/1)
|
|
|
|
|> Enum.map(&IO.puts/1)
|
2019-03-31 10:01:16 -06:00
|
|
|
end
|
|
|
|
|
2022-05-20 21:10:22 -06:00
|
|
|
def puts_notification(activity, user) do
|
|
|
|
notification =
|
|
|
|
Pleroma.Web.MastodonAPI.NotificationView.render("show.json", %{
|
|
|
|
notification: activity,
|
|
|
|
for: user
|
|
|
|
})
|
|
|
|
|
|
|
|
IO.puts(
|
|
|
|
"== (#{notification.type}) #{notification.status.id} by #{notification.account.display_name} (#{notification.account.acct})"
|
|
|
|
)
|
|
|
|
|
|
|
|
notification.status.content
|
|
|
|
|> String.split("<br/>")
|
|
|
|
|> Enum.map(&HTML.strip_tags/1)
|
|
|
|
|> Enum.map(&HtmlEntities.decode/1)
|
|
|
|
|> (fn x ->
|
|
|
|
case x do
|
|
|
|
[content] ->
|
|
|
|
"> " <> content
|
|
|
|
|
|
|
|
[head | _tail] ->
|
|
|
|
# "> " <> hd <> "..."
|
|
|
|
head
|
2022-05-21 17:23:55 -06:00
|
|
|
|> String.slice(1, 80)
|
2022-05-20 21:10:22 -06:00
|
|
|
|> (fn x -> "> " <> x <> "..." end).()
|
|
|
|
end
|
|
|
|
end).()
|
|
|
|
|> IO.puts()
|
|
|
|
|
|
|
|
IO.puts("")
|
|
|
|
end
|
|
|
|
|
2019-03-31 10:01:16 -06:00
|
|
|
def handle_command(state, "help") do
|
|
|
|
IO.puts("Available commands:")
|
|
|
|
IO.puts("help - This help")
|
|
|
|
IO.puts("home - Show the home timeline")
|
|
|
|
IO.puts("p <text> - Post the given text")
|
2019-03-31 13:14:21 -06:00
|
|
|
IO.puts("r <id> <text> - Reply to the post with the given id")
|
2022-05-18 16:56:20 -06:00
|
|
|
IO.puts("t <id> - Show a thread from the given id")
|
2022-05-20 21:10:22 -06:00
|
|
|
IO.puts("n - Show notifications")
|
2022-05-20 21:10:48 -06:00
|
|
|
IO.puts("n read - Mark all notifactions as read")
|
2022-05-21 18:39:38 -06:00
|
|
|
IO.puts("f <id> - Favourites the post with the given id")
|
2019-03-31 10:01:16 -06:00
|
|
|
IO.puts("quit - Quit")
|
|
|
|
|
|
|
|
state
|
|
|
|
end
|
|
|
|
|
2019-03-31 13:14:21 -06:00
|
|
|
def handle_command(%{user: user} = state, "r " <> text) do
|
|
|
|
text = String.trim(text)
|
|
|
|
[activity_id, rest] = String.split(text, " ", parts: 2)
|
|
|
|
|
|
|
|
with %Activity{} <- Activity.get_by_id(activity_id),
|
|
|
|
{:ok, _activity} <-
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(user, %{status: rest, in_reply_to_status_id: activity_id}) do
|
2019-03-31 13:14:21 -06:00
|
|
|
IO.puts("Replied!")
|
|
|
|
else
|
|
|
|
_e -> IO.puts("Could not reply...")
|
|
|
|
end
|
|
|
|
|
|
|
|
state
|
|
|
|
end
|
|
|
|
|
2022-05-18 16:56:20 -06:00
|
|
|
def handle_command(%{user: user} = state, "t " <> activity_id) do
|
|
|
|
with %Activity{} = activity <- Activity.get_by_id(activity_id) do
|
|
|
|
activities =
|
|
|
|
ActivityPub.fetch_activities_for_context(activity.data["context"], %{
|
|
|
|
blocking_user: user,
|
|
|
|
user: user,
|
|
|
|
exclude_id: activity.id
|
|
|
|
})
|
|
|
|
|
|
|
|
case activities do
|
|
|
|
[] ->
|
|
|
|
activity_id
|
|
|
|
|> Activity.get_by_id()
|
|
|
|
|> puts_activity()
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
activities
|
|
|
|
|> Enum.reverse()
|
|
|
|
|> Enum.each(&puts_activity/1)
|
|
|
|
end
|
|
|
|
else
|
2022-05-21 17:23:55 -06:00
|
|
|
_e -> IO.puts("Could not show this thread...")
|
2022-05-18 16:56:20 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
state
|
|
|
|
end
|
|
|
|
|
2022-05-20 21:10:48 -06:00
|
|
|
def handle_command(%{user: user} = state, "n read") do
|
|
|
|
Pleroma.Notification.clear(user)
|
2022-05-21 17:23:55 -06:00
|
|
|
IO.puts("All notifications were marked as read")
|
2022-05-20 21:10:48 -06:00
|
|
|
|
|
|
|
state
|
|
|
|
end
|
|
|
|
|
2022-05-20 21:10:22 -06:00
|
|
|
def handle_command(%{user: user} = state, "n") do
|
|
|
|
user
|
|
|
|
|> Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(%{})
|
|
|
|
|> Enum.each(&puts_notification(&1, user))
|
|
|
|
|
|
|
|
state
|
|
|
|
end
|
|
|
|
|
2019-03-31 10:01:16 -06:00
|
|
|
def handle_command(%{user: user} = state, "p " <> text) do
|
|
|
|
text = String.trim(text)
|
|
|
|
|
2022-05-21 18:40:56 -06:00
|
|
|
with {:ok, activity} <- CommonAPI.post(user, %{status: text}) do
|
|
|
|
IO.puts("Posted! ID: #{activity.id}")
|
2019-03-31 10:01:16 -06:00
|
|
|
else
|
|
|
|
_e -> IO.puts("Could not post...")
|
|
|
|
end
|
2019-03-31 12:35:10 -06:00
|
|
|
|
2019-03-31 10:01:16 -06:00
|
|
|
state
|
|
|
|
end
|
|
|
|
|
2022-05-21 18:39:38 -06:00
|
|
|
def handle_command(%{user: user} = state, "f " <> id) do
|
|
|
|
id = String.trim(id)
|
|
|
|
|
|
|
|
with %Activity{} = activity <- Activity.get_by_id(id),
|
|
|
|
{:ok, _activity} <- CommonAPI.favorite(user, activity) do
|
|
|
|
IO.puts("Favourited!")
|
|
|
|
else
|
|
|
|
_e -> IO.puts("Could not Favourite...")
|
|
|
|
end
|
|
|
|
|
|
|
|
state
|
|
|
|
end
|
|
|
|
|
2019-03-31 10:01:16 -06:00
|
|
|
def handle_command(state, "home") do
|
|
|
|
user = state.user
|
2019-03-31 12:35:10 -06:00
|
|
|
|
2019-03-31 10:01:16 -06:00
|
|
|
params =
|
|
|
|
%{}
|
2020-06-04 11:33:16 -06:00
|
|
|
|> Map.put(:type, ["Create"])
|
|
|
|
|> Map.put(:blocking_user, user)
|
|
|
|
|> Map.put(:muting_user, user)
|
|
|
|
|> Map.put(:user, user)
|
2019-03-31 10:01:16 -06:00
|
|
|
|
|
|
|
activities =
|
2019-10-10 13:35:32 -06:00
|
|
|
[user.ap_id | Pleroma.User.following(user)]
|
2019-03-31 10:01:16 -06:00
|
|
|
|> ActivityPub.fetch_activities(params)
|
|
|
|
|
2019-03-31 12:35:10 -06:00
|
|
|
Enum.each(activities, fn activity ->
|
2019-03-31 10:01:16 -06:00
|
|
|
puts_activity(activity)
|
|
|
|
end)
|
|
|
|
|
|
|
|
state
|
|
|
|
end
|
|
|
|
|
2019-03-31 13:53:17 -06:00
|
|
|
def handle_command(state, command) do
|
2019-03-31 10:01:16 -06:00
|
|
|
IO.puts("Unknown command '#{command}'")
|
2019-03-31 13:53:17 -06:00
|
|
|
state
|
2019-03-31 10:01:16 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp wait_input(state, input) do
|
|
|
|
receive do
|
|
|
|
{:input, ^input, "quit\n"} ->
|
2019-03-31 12:35:10 -06:00
|
|
|
IO.puts("Exiting...")
|
2019-03-31 10:01:16 -06:00
|
|
|
|
|
|
|
{:input, ^input, code} when is_binary(code) ->
|
|
|
|
code = String.trim(code)
|
|
|
|
|
2019-03-31 13:14:21 -06:00
|
|
|
state = handle_command(state, code)
|
2019-03-31 10:01:16 -06:00
|
|
|
|
|
|
|
loop(%{state | counter: state.counter + 1})
|
|
|
|
|
2022-05-18 12:06:16 -06:00
|
|
|
{:input, ^input, {:error, :interrupted}} ->
|
2019-03-31 12:35:10 -06:00
|
|
|
IO.puts("Caught Ctrl+C...")
|
2019-03-31 10:01:16 -06:00
|
|
|
loop(%{state | counter: state.counter + 1})
|
|
|
|
|
|
|
|
{:input, ^input, msg} ->
|
2019-03-31 12:35:10 -06:00
|
|
|
:ok = Logger.warn("received unknown message: #{inspect(msg)}")
|
2019-03-31 10:01:16 -06:00
|
|
|
loop(%{state | counter: state.counter + 1})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp run_state(opts) do
|
|
|
|
%{prefix: "pleroma", counter: 1, user: opts[:user]}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp io_get(pid, prefix, counter, username) do
|
|
|
|
prompt = prompt(prefix, counter, username)
|
2019-03-31 12:35:10 -06:00
|
|
|
send(pid, {:input, self(), IO.gets(:stdio, prompt)})
|
2019-03-31 10:01:16 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp prompt(prefix, counter, username) do
|
|
|
|
prompt = "#{username}@#{prefix}:#{counter}>"
|
|
|
|
prompt <> " "
|
|
|
|
end
|
|
|
|
end
|