Merge branch 'develop' into feature/matstodon-statuses-by-name
[akkoma] / lib / pleroma / bbs / handler.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Web.ActivityPub.ActivityPub
9 alias Pleroma.Web.CommonAPI
10
11 def on_shell(username, _pubkey, _ip, _port) do
12 :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
13 user = Pleroma.User.get_cached_by_nickname(to_string(username))
14 Logger.debug("#{inspect(user)}")
15 loop(run_state(user: user))
16 end
17
18 def on_connect(username, ip, port, method) do
19 Logger.debug(fn ->
20 """
21 Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{
22 inspect(port)
23 } using #{inspect(method)}
24 """
25 end)
26 end
27
28 def on_disconnect(username, ip, port) do
29 Logger.debug(fn ->
30 "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
31 end)
32 end
33
34 defp loop(state) do
35 self_pid = self()
36 counter = state.counter
37 prefix = state.prefix
38 user = state.user
39
40 input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
41 wait_input(state, input)
42 end
43
44 def puts_activity(activity) do
45 status = Pleroma.Web.MastodonAPI.StatusView.render("status.json", %{activity: activity})
46 IO.puts("-- #{status.id} by #{status.account.display_name} (#{status.account.acct})")
47 IO.puts(HtmlSanitizeEx.strip_tags(status.content))
48 IO.puts("")
49 end
50
51 def handle_command(state, "help") do
52 IO.puts("Available commands:")
53 IO.puts("help - This help")
54 IO.puts("home - Show the home timeline")
55 IO.puts("p <text> - Post the given text")
56 IO.puts("r <id> <text> - Reply to the post with the given id")
57 IO.puts("quit - Quit")
58
59 state
60 end
61
62 def handle_command(%{user: user} = state, "r " <> text) do
63 text = String.trim(text)
64 [activity_id, rest] = String.split(text, " ", parts: 2)
65
66 with %Activity{} <- Activity.get_by_id(activity_id),
67 {:ok, _activity} <-
68 CommonAPI.post(user, %{"status" => rest, "in_reply_to_status_id" => activity_id}) do
69 IO.puts("Replied!")
70 else
71 _e -> IO.puts("Could not reply...")
72 end
73
74 state
75 end
76
77 def handle_command(%{user: user} = state, "p " <> text) do
78 text = String.trim(text)
79
80 with {:ok, _activity} <- CommonAPI.post(user, %{"status" => text}) do
81 IO.puts("Posted!")
82 else
83 _e -> IO.puts("Could not post...")
84 end
85
86 state
87 end
88
89 def handle_command(state, "home") do
90 user = state.user
91
92 params =
93 %{}
94 |> Map.put("type", ["Create"])
95 |> Map.put("blocking_user", user)
96 |> Map.put("muting_user", user)
97 |> Map.put("user", user)
98
99 activities =
100 [user.ap_id | user.following]
101 |> ActivityPub.fetch_activities(params)
102
103 Enum.each(activities, fn activity ->
104 puts_activity(activity)
105 end)
106
107 state
108 end
109
110 def handle_command(state, command) do
111 IO.puts("Unknown command '#{command}'")
112 state
113 end
114
115 defp wait_input(state, input) do
116 receive do
117 {:input, ^input, "quit\n"} ->
118 IO.puts("Exiting...")
119
120 {:input, ^input, code} when is_binary(code) ->
121 code = String.trim(code)
122
123 state = handle_command(state, code)
124
125 loop(%{state | counter: state.counter + 1})
126
127 {:error, :interrupted} ->
128 IO.puts("Caught Ctrl+C...")
129 loop(%{state | counter: state.counter + 1})
130
131 {:input, ^input, msg} ->
132 :ok = Logger.warn("received unknown message: #{inspect(msg)}")
133 loop(%{state | counter: state.counter + 1})
134 end
135 end
136
137 defp run_state(opts) do
138 %{prefix: "pleroma", counter: 1, user: opts[:user]}
139 end
140
141 defp io_get(pid, prefix, counter, username) do
142 prompt = prompt(prefix, counter, username)
143 send(pid, {:input, self(), IO.gets(:stdio, prompt)})
144 end
145
146 defp prompt(prefix, counter, username) do
147 prompt = "#{username}@#{prefix}:#{counter}>"
148 prompt <> " "
149 end
150 end