CI: Bump lint stage to elixir-1.12
[akkoma] / lib / pleroma / bbs / handler.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.BBS.Handler do
6 use Sshd.ShellHandler
7 alias Pleroma.Activity
8 alias Pleroma.HTML
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.CommonAPI
11
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))
17 end
18
19 def on_connect(username, ip, port, method) do
20 Logger.debug(fn ->
21 """
22 Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{inspect(port)} 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("show.json", %{activity: activity})
45 IO.puts("-- #{status.id} by #{status.account.display_name} (#{status.account.acct})")
46 IO.puts(HTML.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("r <id> <text> - Reply to the post with the given id")
56 IO.puts("quit - Quit")
57
58 state
59 end
60
61 def handle_command(%{user: user} = state, "r " <> text) do
62 text = String.trim(text)
63 [activity_id, rest] = String.split(text, " ", parts: 2)
64
65 with %Activity{} <- Activity.get_by_id(activity_id),
66 {:ok, _activity} <-
67 CommonAPI.post(user, %{status: rest, in_reply_to_status_id: activity_id}) do
68 IO.puts("Replied!")
69 else
70 _e -> IO.puts("Could not reply...")
71 end
72
73 state
74 end
75
76 def handle_command(%{user: user} = state, "p " <> text) do
77 text = String.trim(text)
78
79 with {:ok, _activity} <- CommonAPI.post(user, %{status: text}) do
80 IO.puts("Posted!")
81 else
82 _e -> IO.puts("Could not post...")
83 end
84
85 state
86 end
87
88 def handle_command(state, "home") do
89 user = state.user
90
91 params =
92 %{}
93 |> Map.put(:type, ["Create"])
94 |> Map.put(:blocking_user, user)
95 |> Map.put(:muting_user, user)
96 |> Map.put(:user, user)
97
98 activities =
99 [user.ap_id | Pleroma.User.following(user)]
100 |> ActivityPub.fetch_activities(params)
101
102 Enum.each(activities, fn activity ->
103 puts_activity(activity)
104 end)
105
106 state
107 end
108
109 def handle_command(state, command) do
110 IO.puts("Unknown command '#{command}'")
111 state
112 end
113
114 defp wait_input(state, input) do
115 receive do
116 {:input, ^input, "quit\n"} ->
117 IO.puts("Exiting...")
118
119 {:input, ^input, code} when is_binary(code) ->
120 code = String.trim(code)
121
122 state = handle_command(state, code)
123
124 loop(%{state | counter: state.counter + 1})
125
126 {:error, :interrupted} ->
127 IO.puts("Caught Ctrl+C...")
128 loop(%{state | counter: state.counter + 1})
129
130 {:input, ^input, msg} ->
131 :ok = Logger.warn("received unknown message: #{inspect(msg)}")
132 loop(%{state | counter: state.counter + 1})
133 end
134 end
135
136 defp run_state(opts) do
137 %{prefix: "pleroma", counter: 1, user: opts[:user]}
138 end
139
140 defp io_get(pid, prefix, counter, username) do
141 prompt = prompt(prefix, counter, username)
142 send(pid, {:input, self(), IO.gets(:stdio, prompt)})
143 end
144
145 defp prompt(prefix, counter, username) do
146 prompt = "#{username}@#{prefix}:#{counter}>"
147 prompt <> " "
148 end
149 end