X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fbbs%2Fhandler.ex;h=0a381f5920c954517b3dfdf1a31bf022cb0b4601;hb=4fabf83ad01352442906d79187aeab4c777f4df8;hp=010929ed228e4fb5830b0a0d3ad5d32d21cce166;hpb=17ab9fa45bf8a359746d30f4a91cb57a9c94f206;p=akkoma
diff --git a/lib/pleroma/bbs/handler.ex b/lib/pleroma/bbs/handler.ex
index 010929ed2..0a381f592 100644
--- a/lib/pleroma/bbs/handler.ex
+++ b/lib/pleroma/bbs/handler.ex
@@ -1,46 +1,49 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors
+# SPDX-License-Identifier: AGPL-3.0-only
+
defmodule Pleroma.BBS.Handler do
- @moduledoc """
- An example implementation of `Sshd.ShellHandler`, implementing a very simple
- Read-Eval-Loop, that does nothing.
- """
use Sshd.ShellHandler
- alias Pleroma.Web.CommonAPI
+ alias Pleroma.Activity
alias Pleroma.Web.ActivityPub.ActivityPub
+ alias Pleroma.Web.CommonAPI
def on_shell(username, _pubkey, _ip, _port) do
- :ok = IO.puts "Welcome to #{Pleroma.Config.get([:instance, :name])}!"
- user = Pleroma.User.get_by_nickname(to_string(username))
- Logger.debug("#{inspect user}")
- loop(run_state([user: user]))
+ :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
+ user = Pleroma.User.get_cached_by_nickname(to_string(username))
+ Logger.debug("#{inspect(user)}")
+ loop(run_state(user: user))
end
def on_connect(username, ip, port, method) do
- Logger.debug fn ->
+ Logger.debug(fn ->
"""
- Incoming SSH shell #{inspect self()} requested for #{username} from #{inspect ip}:#{inspect port} using #{inspect method}
+ Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{
+ inspect(port)
+ } using #{inspect(method)}
"""
- end
+ end)
end
def on_disconnect(username, ip, port) do
- Logger.debug fn ->
- "Disconnecting SSH shell for #{username} from #{inspect ip}:#{inspect port}"
- end
+ Logger.debug(fn ->
+ "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
+ end)
end
defp loop(state) do
self_pid = self()
- counter = state.counter
- prefix = state.prefix
- user = state.user
+ counter = state.counter
+ prefix = state.prefix
+ user = state.user
input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
- wait_input state, input
+ wait_input(state, input)
end
def puts_activity(activity) do
status = Pleroma.Web.MastodonAPI.StatusView.render("status.json", %{activity: activity})
- IO.puts("#{status.id} by #{status.account.display_name} (#{status.account.acct}):")
+ IO.puts("-- #{status.id} by #{status.account.display_name} (#{status.account.acct})")
IO.puts(HtmlSanitizeEx.strip_tags(status.content))
IO.puts("")
end
@@ -50,11 +53,27 @@ defmodule Pleroma.BBS.Handler do
IO.puts("help - This help")
IO.puts("home - Show the home timeline")
IO.puts("p - Post the given text")
+ IO.puts("r - Reply to the post with the given id")
IO.puts("quit - Quit")
state
end
+ def handle_command(%{user: user} = state, "r " <> text) do
+ text = String.trim(text)
+ [activity_id, rest] = String.split(text, " ", parts: 2)
+
+ with %Activity{} <- Activity.get_by_id(activity_id),
+ {:ok, _activity} <-
+ CommonAPI.post(user, %{"status" => rest, "in_reply_to_status_id" => activity_id}) do
+ IO.puts("Replied!")
+ else
+ _e -> IO.puts("Could not reply...")
+ end
+
+ state
+ end
+
def handle_command(%{user: user} = state, "p " <> text) do
text = String.trim(text)
@@ -63,14 +82,16 @@ defmodule Pleroma.BBS.Handler do
else
_e -> IO.puts("Could not post...")
end
+
state
end
def handle_command(state, "home") do
user = state.user
+
params =
%{}
- |> Map.put("type", ["Create", "Announce"])
+ |> Map.put("type", ["Create"])
|> Map.put("blocking_user", user)
|> Map.put("muting_user", user)
|> Map.put("user", user)
@@ -78,38 +99,37 @@ defmodule Pleroma.BBS.Handler do
activities =
[user.ap_id | user.following]
|> ActivityPub.fetch_activities(params)
- |> ActivityPub.contain_timeline(user)
- |> Enum.reverse()
- Enum.each(activities, fn (activity) ->
+ Enum.each(activities, fn activity ->
puts_activity(activity)
end)
state
end
- def handle_command(_state, command) do
+ def handle_command(state, command) do
IO.puts("Unknown command '#{command}'")
+ state
end
defp wait_input(state, input) do
receive do
{:input, ^input, "quit\n"} ->
- IO.puts "Exiting..."
+ IO.puts("Exiting...")
{:input, ^input, code} when is_binary(code) ->
code = String.trim(code)
- handle_command(state, code)
+ state = handle_command(state, code)
loop(%{state | counter: state.counter + 1})
{:error, :interrupted} ->
- IO.puts "Caught Ctrl+C..."
+ IO.puts("Caught Ctrl+C...")
loop(%{state | counter: state.counter + 1})
{:input, ^input, msg} ->
- :ok = Logger.warn "received unknown message: #{inspect msg}"
+ :ok = Logger.warn("received unknown message: #{inspect(msg)}")
loop(%{state | counter: state.counter + 1})
end
end
@@ -120,7 +140,7 @@ defmodule Pleroma.BBS.Handler do
defp io_get(pid, prefix, counter, username) do
prompt = prompt(prefix, counter, username)
- send pid, {:input, self(), IO.gets(:stdio, prompt)}
+ send(pid, {:input, self(), IO.gets(:stdio, prompt)})
end
defp prompt(prefix, counter, username) do