akkoma/lib/pleroma/bbs/handler.ex

135 lines
3.5 KiB
Elixir
Raw Normal View History

2019-03-31 16:01:16 +00:00
defmodule Pleroma.BBS.Handler do
@moduledoc """
An example implementation of `Sshd.ShellHandler`, implementing a very simple
Read-Eval-Loop, that does nothing.
"""
use Sshd.ShellHandler
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.ActivityPub.ActivityPub
def on_shell(username, _pubkey, _ip, _port) do
2019-03-31 18:35:10 +00:00
:ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
2019-03-31 16:01:16 +00:00
user = Pleroma.User.get_by_nickname(to_string(username))
2019-03-31 18:35:10 +00:00
Logger.debug("#{inspect(user)}")
loop(run_state(user: user))
2019-03-31 16:01:16 +00:00
end
def on_connect(username, ip, port, method) do
2019-03-31 18:35:10 +00:00
Logger.debug(fn ->
2019-03-31 16:01:16 +00:00
"""
2019-03-31 18:35:10 +00:00
Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{
inspect(port)
} using #{inspect(method)}
2019-03-31 16:01:16 +00:00
"""
2019-03-31 18:35:10 +00:00
end)
2019-03-31 16:01:16 +00:00
end
def on_disconnect(username, ip, port) do
2019-03-31 18:35:10 +00:00
Logger.debug(fn ->
"Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
end)
2019-03-31 16:01:16 +00:00
end
defp loop(state) do
self_pid = self()
2019-03-31 18:35:10 +00:00
counter = state.counter
prefix = state.prefix
user = state.user
2019-03-31 16:01:16 +00:00
input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
2019-03-31 18:35:10 +00:00
wait_input(state, input)
2019-03-31 16:01:16 +00:00
end
def puts_activity(activity) do
status = Pleroma.Web.MastodonAPI.StatusView.render("status.json", %{activity: activity})
IO.puts("#{status.id} by #{status.account.display_name} (#{status.account.acct}):")
IO.puts(HtmlSanitizeEx.strip_tags(status.content))
IO.puts("")
end
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")
IO.puts("quit - Quit")
state
end
def handle_command(%{user: user} = state, "p " <> text) do
text = String.trim(text)
with {:ok, _activity} <- CommonAPI.post(user, %{"status" => text}) do
IO.puts("Posted!")
else
_e -> IO.puts("Could not post...")
end
2019-03-31 18:35:10 +00:00
2019-03-31 16:01:16 +00:00
state
end
def handle_command(state, "home") do
user = state.user
2019-03-31 18:35:10 +00:00
2019-03-31 16:01:16 +00:00
params =
%{}
|> Map.put("type", ["Create", "Announce"])
|> Map.put("blocking_user", user)
|> Map.put("muting_user", user)
|> Map.put("user", user)
activities =
[user.ap_id | user.following]
|> ActivityPub.fetch_activities(params)
|> ActivityPub.contain_timeline(user)
|> Enum.reverse()
2019-03-31 18:35:10 +00:00
Enum.each(activities, fn activity ->
2019-03-31 16:01:16 +00:00
puts_activity(activity)
end)
state
end
def handle_command(_state, command) do
IO.puts("Unknown command '#{command}'")
end
defp wait_input(state, input) do
receive do
{:input, ^input, "quit\n"} ->
2019-03-31 18:35:10 +00:00
IO.puts("Exiting...")
2019-03-31 16:01:16 +00:00
{:input, ^input, code} when is_binary(code) ->
code = String.trim(code)
handle_command(state, code)
loop(%{state | counter: state.counter + 1})
{:error, :interrupted} ->
2019-03-31 18:35:10 +00:00
IO.puts("Caught Ctrl+C...")
2019-03-31 16:01:16 +00:00
loop(%{state | counter: state.counter + 1})
{:input, ^input, msg} ->
2019-03-31 18:35:10 +00:00
:ok = Logger.warn("received unknown message: #{inspect(msg)}")
2019-03-31 16:01:16 +00: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 18:35:10 +00:00
send(pid, {:input, self(), IO.gets(:stdio, prompt)})
2019-03-31 16:01:16 +00:00
end
defp prompt(prefix, counter, username) do
prompt = "#{username}@#{prefix}:#{counter}>"
prompt <> " "
end
end