BBS: Some more functionality.
[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}:#{inspect port} using #{inspect method}
21 """
22 end
23 end
24
25 def on_disconnect(username, ip, port) do
26 Logger.debug fn ->
27 "Disconnecting SSH shell for #{username} from #{inspect ip}:#{inspect port}"
28 end
29 end
30
31 defp loop(state) do
32 self_pid = self()
33 counter = state.counter
34 prefix = state.prefix
35 user = state.user
36
37 input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
38 wait_input state, input
39 end
40
41 def puts_activity(activity) do
42 status = Pleroma.Web.MastodonAPI.StatusView.render("status.json", %{activity: activity})
43 IO.puts("#{status.id} by #{status.account.display_name} (#{status.account.acct}):")
44 IO.puts(HtmlSanitizeEx.strip_tags(status.content))
45 IO.puts("")
46 end
47
48 def handle_command(state, "help") do
49 IO.puts("Available commands:")
50 IO.puts("help - This help")
51 IO.puts("home - Show the home timeline")
52 IO.puts("p <text> - Post the given text")
53 IO.puts("quit - Quit")
54
55 state
56 end
57
58 def handle_command(%{user: user} = state, "p " <> text) do
59 text = String.trim(text)
60
61 with {:ok, _activity} <- CommonAPI.post(user, %{"status" => text}) do
62 IO.puts("Posted!")
63 else
64 _e -> IO.puts("Could not post...")
65 end
66 state
67 end
68
69 def handle_command(state, "home") do
70 user = state.user
71 params =
72 %{}
73 |> Map.put("type", ["Create", "Announce"])
74 |> Map.put("blocking_user", user)
75 |> Map.put("muting_user", user)
76 |> Map.put("user", user)
77
78 activities =
79 [user.ap_id | user.following]
80 |> ActivityPub.fetch_activities(params)
81 |> ActivityPub.contain_timeline(user)
82 |> Enum.reverse()
83
84 Enum.each(activities, fn (activity) ->
85 puts_activity(activity)
86 end)
87
88 state
89 end
90
91 def handle_command(_state, command) do
92 IO.puts("Unknown command '#{command}'")
93 end
94
95 defp wait_input(state, input) do
96 receive do
97 {:input, ^input, "quit\n"} ->
98 IO.puts "Exiting..."
99
100 {:input, ^input, code} when is_binary(code) ->
101 code = String.trim(code)
102
103 handle_command(state, code)
104
105 loop(%{state | counter: state.counter + 1})
106
107 {:error, :interrupted} ->
108 IO.puts "Caught Ctrl+C..."
109 loop(%{state | counter: state.counter + 1})
110
111 {:input, ^input, msg} ->
112 :ok = Logger.warn "received unknown message: #{inspect msg}"
113 loop(%{state | counter: state.counter + 1})
114 end
115 end
116
117 defp run_state(opts) do
118 %{prefix: "pleroma", counter: 1, user: opts[:user]}
119 end
120
121 defp io_get(pid, prefix, counter, username) do
122 prompt = prompt(prefix, counter, username)
123 send pid, {:input, self(), IO.gets(:stdio, prompt)}
124 end
125
126 defp prompt(prefix, counter, username) do
127 prompt = "#{username}@#{prefix}:#{counter}>"
128 prompt <> " "
129 end
130 end