logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

handler.ex (6583B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.BBS.Handler do
  5. use Sshd.ShellHandler
  6. alias Pleroma.Activity
  7. alias Pleroma.HTML
  8. alias Pleroma.Web.ActivityPub.ActivityPub
  9. alias Pleroma.Web.CommonAPI
  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_cached_by_nickname(to_string(username))
  13. Logger.debug("#{inspect(user)}")
  14. loop(run_state(user: user))
  15. end
  16. def on_connect(username, ip, port, method) do
  17. Logger.debug(fn ->
  18. """
  19. Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{inspect(port)} using #{inspect(method)}
  20. """
  21. end)
  22. end
  23. def on_disconnect(username, ip, port) do
  24. Logger.debug(fn ->
  25. "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
  26. end)
  27. end
  28. defp loop(state) do
  29. self_pid = self()
  30. counter = state.counter
  31. prefix = state.prefix
  32. user = state.user
  33. input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
  34. wait_input(state, input)
  35. end
  36. def puts_activity(activity) do
  37. status = Pleroma.Web.MastodonAPI.StatusView.render("show.json", %{activity: activity})
  38. IO.puts("-- #{status.id} by #{status.account.display_name} (#{status.account.acct})")
  39. status.content
  40. |> String.split("<br/>")
  41. |> Enum.map(&HTML.strip_tags/1)
  42. |> Enum.map(&HtmlEntities.decode/1)
  43. |> Enum.map(&IO.puts/1)
  44. end
  45. def puts_notification(activity, user) do
  46. notification =
  47. Pleroma.Web.MastodonAPI.NotificationView.render("show.json", %{
  48. notification: activity,
  49. for: user
  50. })
  51. IO.puts(
  52. "== (#{notification.type}) #{notification.status.id} by #{notification.account.display_name} (#{notification.account.acct})"
  53. )
  54. notification.status.content
  55. |> String.split("<br/>")
  56. |> Enum.map(&HTML.strip_tags/1)
  57. |> Enum.map(&HtmlEntities.decode/1)
  58. |> (fn x ->
  59. case x do
  60. [content] ->
  61. "> " <> content
  62. [head | _tail] ->
  63. # "> " <> hd <> "..."
  64. head
  65. |> String.slice(1, 80)
  66. |> (fn x -> "> " <> x <> "..." end).()
  67. end
  68. end).()
  69. |> IO.puts()
  70. IO.puts("")
  71. end
  72. def handle_command(state, "help") do
  73. IO.puts("Available commands:")
  74. IO.puts("help - This help")
  75. IO.puts("home - Show the home timeline")
  76. IO.puts("p <text> - Post the given text")
  77. IO.puts("r <id> <text> - Reply to the post with the given id")
  78. IO.puts("t <id> - Show a thread from the given id")
  79. IO.puts("n - Show notifications")
  80. IO.puts("n read - Mark all notifactions as read")
  81. IO.puts("f <id> - Favourites the post with the given id")
  82. IO.puts("R <id> - Repeat the post with the given id")
  83. IO.puts("quit - Quit")
  84. state
  85. end
  86. def handle_command(%{user: user} = state, "r " <> text) do
  87. text = String.trim(text)
  88. [activity_id, rest] = String.split(text, " ", parts: 2)
  89. with %Activity{} <- Activity.get_by_id(activity_id),
  90. {:ok, _activity} <-
  91. CommonAPI.post(user, %{status: rest, in_reply_to_status_id: activity_id}) do
  92. IO.puts("Replied!")
  93. else
  94. _e -> IO.puts("Could not reply...")
  95. end
  96. state
  97. end
  98. def handle_command(%{user: user} = state, "t " <> activity_id) do
  99. with %Activity{} = activity <- Activity.get_by_id(activity_id) do
  100. activities =
  101. ActivityPub.fetch_activities_for_context(activity.data["context"], %{
  102. blocking_user: user,
  103. user: user,
  104. exclude_id: activity.id
  105. })
  106. case activities do
  107. [] ->
  108. activity_id
  109. |> Activity.get_by_id()
  110. |> puts_activity()
  111. _ ->
  112. activities
  113. |> Enum.reverse()
  114. |> Enum.each(&puts_activity/1)
  115. end
  116. else
  117. _e -> IO.puts("Could not show this thread...")
  118. end
  119. state
  120. end
  121. def handle_command(%{user: user} = state, "n read") do
  122. Pleroma.Notification.clear(user)
  123. IO.puts("All notifications were marked as read")
  124. state
  125. end
  126. def handle_command(%{user: user} = state, "n") do
  127. user
  128. |> Pleroma.Web.MastodonAPI.MastodonAPI.get_notifications(%{})
  129. |> Enum.each(&puts_notification(&1, user))
  130. state
  131. end
  132. def handle_command(%{user: user} = state, "p " <> text) do
  133. text = String.trim(text)
  134. with {:ok, activity} <- CommonAPI.post(user, %{status: text}) do
  135. IO.puts("Posted! ID: #{activity.id}")
  136. else
  137. _e -> IO.puts("Could not post...")
  138. end
  139. state
  140. end
  141. def handle_command(%{user: user} = state, "f " <> id) do
  142. id = String.trim(id)
  143. with %Activity{} = activity <- Activity.get_by_id(id),
  144. {:ok, _activity} <- CommonAPI.favorite(user, activity) do
  145. IO.puts("Favourited!")
  146. else
  147. _e -> IO.puts("Could not Favourite...")
  148. end
  149. state
  150. end
  151. def handle_command(state, "home") do
  152. user = state.user
  153. params =
  154. %{}
  155. |> Map.put(:type, ["Create"])
  156. |> Map.put(:blocking_user, user)
  157. |> Map.put(:muting_user, user)
  158. |> Map.put(:user, user)
  159. activities =
  160. [user.ap_id | Pleroma.User.following(user)]
  161. |> ActivityPub.fetch_activities(params)
  162. Enum.each(activities, fn activity ->
  163. puts_activity(activity)
  164. end)
  165. state
  166. end
  167. def handle_command(state, command) do
  168. IO.puts("Unknown command '#{command}'")
  169. state
  170. end
  171. defp wait_input(state, input) do
  172. receive do
  173. {:input, ^input, "quit\n"} ->
  174. IO.puts("Exiting...")
  175. {:input, ^input, code} when is_binary(code) ->
  176. code = String.trim(code)
  177. state = handle_command(state, code)
  178. loop(%{state | counter: state.counter + 1})
  179. {:input, ^input, {:error, :interrupted}} ->
  180. IO.puts("Caught Ctrl+C...")
  181. loop(%{state | counter: state.counter + 1})
  182. {:input, ^input, msg} ->
  183. :ok = Logger.warn("received unknown message: #{inspect(msg)}")
  184. loop(%{state | counter: state.counter + 1})
  185. end
  186. end
  187. defp run_state(opts) do
  188. %{prefix: "pleroma", counter: 1, user: opts[:user]}
  189. end
  190. defp io_get(pid, prefix, counter, username) do
  191. prompt = prompt(prefix, counter, username)
  192. send(pid, {:input, self(), IO.gets(:stdio, prompt)})
  193. end
  194. defp prompt(prefix, counter, username) do
  195. prompt = "#{username}@#{prefix}:#{counter}>"
  196. prompt <> " "
  197. end
  198. end