BBS: Tests and formatting.
[akkoma] / lib / pleroma / bbs / handler.ex
1 defmodule Pleroma.BBS.Handler do
2 @moduledoc """
3 An example implementation of `Sshd.ShellHandler`, implementing a very simple
4 Read-Eval-Loop, that does nothing.
5 """
6 use Sshd.ShellHandler
7 alias Pleroma.Web.CommonAPI
8 alias Pleroma.Web.ActivityPub.ActivityPub
9
10 def on_shell(username, _pubkey, _ip, _port) do
11 :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
12 user = Pleroma.User.get_by_nickname(to_string(username))
13 Logger.debug("#{inspect(user)}")
14 loop(run_state(user: user))
15 end
16
17 def on_connect(username, ip, port, method) do
18 Logger.debug(fn ->
19 """
20 Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{
21 inspect(port)
22 } using #{inspect(method)}
23 """
24 end)
25 end
26
27 def on_disconnect(username, ip, port) do
28 Logger.debug(fn ->
29 "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
30 end)
31 end
32
33 defp loop(state) do
34 self_pid = self()
35 counter = state.counter
36 prefix = state.prefix
37 user = state.user
38
39 input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
40 wait_input(state, input)
41 end
42
43 def puts_activity(activity) do
44 status = Pleroma.Web.MastodonAPI.StatusView.render("status.json", %{activity: activity})
45 IO.puts("#{status.id} by #{status.account.display_name} (#{status.account.acct}):")
46 IO.puts(HtmlSanitizeEx.strip_tags(status.content))
47 IO.puts("")
48 end
49
50 def handle_command(state, "help") do
51 IO.puts("Available commands:")
52 IO.puts("help - This help")
53 IO.puts("home - Show the home timeline")
54 IO.puts("p <text> - Post the given text")
55 IO.puts("quit - Quit")
56
57 state
58 end
59
60 def handle_command(%{user: user} = state, "p " <> text) do
61 text = String.trim(text)
62
63 with {:ok, _activity} <- CommonAPI.post(user, %{"status" => text}) do
64 IO.puts("Posted!")
65 else
66 _e -> IO.puts("Could not post...")
67 end
68
69 state
70 end
71
72 def handle_command(state, "home") do
73 user = state.user
74
75 params =
76 %{}
77 |> Map.put("type", ["Create", "Announce"])
78 |> Map.put("blocking_user", user)
79 |> Map.put("muting_user", user)
80 |> Map.put("user", user)
81
82 activities =
83 [user.ap_id | user.following]
84 |> ActivityPub.fetch_activities(params)
85 |> ActivityPub.contain_timeline(user)
86 |> Enum.reverse()
87
88 Enum.each(activities, fn activity ->
89 puts_activity(activity)
90 end)
91
92 state
93 end
94
95 def handle_command(_state, command) do
96 IO.puts("Unknown command '#{command}'")
97 end
98
99 defp wait_input(state, input) do
100 receive do
101 {:input, ^input, "quit\n"} ->
102 IO.puts("Exiting...")
103
104 {:input, ^input, code} when is_binary(code) ->
105 code = String.trim(code)
106
107 handle_command(state, code)
108
109 loop(%{state | counter: state.counter + 1})
110
111 {:error, :interrupted} ->
112 IO.puts("Caught Ctrl+C...")
113 loop(%{state | counter: state.counter + 1})
114
115 {:input, ^input, msg} ->
116 :ok = Logger.warn("received unknown message: #{inspect(msg)}")
117 loop(%{state | counter: state.counter + 1})
118 end
119 end
120
121 defp run_state(opts) do
122 %{prefix: "pleroma", counter: 1, user: opts[:user]}
123 end
124
125 defp io_get(pid, prefix, counter, username) do
126 prompt = prompt(prefix, counter, username)
127 send(pid, {:input, self(), IO.gets(:stdio, prompt)})
128 end
129
130 defp prompt(prefix, counter, username) do
131 prompt = "#{username}@#{prefix}:#{counter}>"
132 prompt <> " "
133 end
134 end