From c810fb81a489dc3faeb1f21092c89a3131188ac1 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 31 Mar 2019 14:55:09 +0200 Subject: [PATCH 01/10] Basic SSH daemon. --- lib/pleroma/bbs/bbs.ex | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lib/pleroma/bbs/bbs.ex diff --git a/lib/pleroma/bbs/bbs.ex b/lib/pleroma/bbs/bbs.ex new file mode 100644 index 000000000..486ab9183 --- /dev/null +++ b/lib/pleroma/bbs/bbs.ex @@ -0,0 +1,34 @@ +defmodule Pleroma.BBS do + def start_daemon do + :ok = :ssh.start() + + options = [ + system_dir: 'ssh_keys', + auth_method_kb_interactive_data: fn (_, user, _) -> { + 'Welcome to Pleroma BBS', + 'Hello #{user}', + 'Password: ', + false } + end, + auth_methods: 'keyboard-interactive,password', + pwdfun: fn(user, password) -> true end, + shell: &start_prompt/1 + ] + :ssh.daemon(13121, options) + end + + def start_prompt(user) do + spawn(__MODULE__, :prompt, [user]) + end + + def prompt(user) do + IO.puts("Hey #{user}.\n") + IO.puts("Here's your timeline:\n") + + user = Pleroma.User.get_cached_by_nickname(to_string(user)) + Pleroma.Web.TwitterAPI.TwitterAPI.fetch_friend_statuses(user) + |> Enum.each(fn (status) -> + IO.puts("#{status["user"]["name"]} (#{status["user"]["screen_name"]}): #{status["text"]}") + end) + end +end From 17ab9fa45bf8a359746d30f4a91cb57a9c94f206 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 31 Mar 2019 18:01:16 +0200 Subject: [PATCH 02/10] BBS: Some more functionality. --- lib/pleroma/bbs/authenticator.ex | 16 ++++ lib/pleroma/bbs/bbs.ex | 34 -------- lib/pleroma/bbs/handler.ex | 130 +++++++++++++++++++++++++++++++ mix.exs | 5 +- mix.lock | 1 + 5 files changed, 150 insertions(+), 36 deletions(-) create mode 100644 lib/pleroma/bbs/authenticator.ex delete mode 100644 lib/pleroma/bbs/bbs.ex create mode 100644 lib/pleroma/bbs/handler.ex diff --git a/lib/pleroma/bbs/authenticator.ex b/lib/pleroma/bbs/authenticator.ex new file mode 100644 index 000000000..a2c153720 --- /dev/null +++ b/lib/pleroma/bbs/authenticator.ex @@ -0,0 +1,16 @@ +defmodule Pleroma.BBS.Authenticator do + use Sshd.PasswordAuthenticator + alias Comeonin.Pbkdf2 + alias Pleroma.User + + def authenticate(username, password) do + username = to_string(username) + password = to_string(password) + + with %User{} = user <- User.get_by_nickname(username) do + Pbkdf2.checkpw(password, user.password_hash) + else + _e -> false + end + end +end diff --git a/lib/pleroma/bbs/bbs.ex b/lib/pleroma/bbs/bbs.ex deleted file mode 100644 index 486ab9183..000000000 --- a/lib/pleroma/bbs/bbs.ex +++ /dev/null @@ -1,34 +0,0 @@ -defmodule Pleroma.BBS do - def start_daemon do - :ok = :ssh.start() - - options = [ - system_dir: 'ssh_keys', - auth_method_kb_interactive_data: fn (_, user, _) -> { - 'Welcome to Pleroma BBS', - 'Hello #{user}', - 'Password: ', - false } - end, - auth_methods: 'keyboard-interactive,password', - pwdfun: fn(user, password) -> true end, - shell: &start_prompt/1 - ] - :ssh.daemon(13121, options) - end - - def start_prompt(user) do - spawn(__MODULE__, :prompt, [user]) - end - - def prompt(user) do - IO.puts("Hey #{user}.\n") - IO.puts("Here's your timeline:\n") - - user = Pleroma.User.get_cached_by_nickname(to_string(user)) - Pleroma.Web.TwitterAPI.TwitterAPI.fetch_friend_statuses(user) - |> Enum.each(fn (status) -> - IO.puts("#{status["user"]["name"]} (#{status["user"]["screen_name"]}): #{status["text"]}") - end) - end -end diff --git a/lib/pleroma/bbs/handler.ex b/lib/pleroma/bbs/handler.ex new file mode 100644 index 000000000..010929ed2 --- /dev/null +++ b/lib/pleroma/bbs/handler.ex @@ -0,0 +1,130 @@ +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.Web.ActivityPub.ActivityPub + + 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])) + end + + def on_connect(username, ip, port, method) do + Logger.debug fn -> + """ + Incoming SSH shell #{inspect self()} requested for #{username} from #{inspect ip}:#{inspect port} using #{inspect method} + """ + end + end + + def on_disconnect(username, ip, port) do + 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 + + input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end) + 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(HtmlSanitizeEx.strip_tags(status.content)) + IO.puts("") + end + + def handle_command(state, "help") do + IO.puts("Available commands:") + IO.puts("help - This help") + IO.puts("home - Show the home timeline") + IO.puts("p - Post the given text") + IO.puts("quit - Quit") + + state + end + + def handle_command(%{user: user} = state, "p " <> text) do + text = String.trim(text) + + with {:ok, _activity} <- CommonAPI.post(user, %{"status" => text}) do + IO.puts("Posted!") + 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("blocking_user", user) + |> Map.put("muting_user", user) + |> Map.put("user", user) + + activities = + [user.ap_id | user.following] + |> ActivityPub.fetch_activities(params) + |> ActivityPub.contain_timeline(user) + |> Enum.reverse() + + Enum.each(activities, fn (activity) -> + puts_activity(activity) + end) + + state + end + + def handle_command(_state, command) do + IO.puts("Unknown command '#{command}'") + end + + defp wait_input(state, input) do + receive do + {:input, ^input, "quit\n"} -> + IO.puts "Exiting..." + + {:input, ^input, code} when is_binary(code) -> + code = String.trim(code) + + handle_command(state, code) + + loop(%{state | counter: state.counter + 1}) + + {:error, :interrupted} -> + IO.puts "Caught Ctrl+C..." + loop(%{state | counter: state.counter + 1}) + + {:input, ^input, msg} -> + :ok = Logger.warn "received unknown message: #{inspect msg}" + loop(%{state | counter: state.counter + 1}) + end + end + + defp run_state(opts) do + %{prefix: "pleroma", counter: 1, user: opts[:user]} + end + + defp io_get(pid, prefix, counter, username) do + prompt = prompt(prefix, counter, username) + send pid, {:input, self(), IO.gets(:stdio, prompt)} + end + + defp prompt(prefix, counter, username) do + prompt = "#{username}@#{prefix}:#{counter}>" + prompt <> " " + end +end diff --git a/mix.exs b/mix.exs index 333f21a91..58b0db9a1 100644 --- a/mix.exs +++ b/mix.exs @@ -41,7 +41,7 @@ defmodule Pleroma.Mixfile do def application do [ mod: {Pleroma.Application, []}, - extra_applications: [:logger, :runtime_tools, :comeonin], + extra_applications: [:logger, :runtime_tools, :comeonin, :esshd], included_applications: [:ex_syslogger] ] end @@ -94,7 +94,8 @@ defmodule Pleroma.Mixfile do {:auto_linker, git: "https://git.pleroma.social/pleroma/auto_linker.git", ref: "94193ca5f97c1f9fdf3d1469653e2d46fac34bcd"}, - {:pleroma_job_queue, "~> 0.2.0"} + {:pleroma_job_queue, "~> 0.2.0"}, + {:esshd, "~> 0.1.0"} ] end diff --git a/mix.lock b/mix.lock index f401258e9..a8b06b433 100644 --- a/mix.lock +++ b/mix.lock @@ -18,6 +18,7 @@ "earmark": {:hex, :earmark, "1.3.0", "17f0c38eaafb4800f746b457313af4b2442a8c2405b49c645768680f900be603", [:mix], [], "hexpm"}, "ecto": {:hex, :ecto, "3.0.7", "44dda84ac6b17bbbdeb8ac5dfef08b7da253b37a453c34ab1a98de7f7e5fec7f", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm"}, "ecto_sql": {:hex, :ecto_sql, "3.0.5", "7e44172b4f7aca4469f38d7f6a3da394dbf43a1bcf0ca975e958cb957becd74e", [:mix], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.0.6", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.1", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.3.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"}, + "esshd": {:hex, :esshd, "0.1.0", "6f93a2062adb43637edad0ea7357db2702a4b80dd9683482fe00f5134e97f4c1", [:mix], [], "hexpm"}, "eternal": {:hex, :eternal, "1.2.0", "e2a6b6ce3b8c248f7dc31451aefca57e3bdf0e48d73ae5043229380a67614c41", [:mix], [], "hexpm"}, "ex_aws": {:hex, :ex_aws, "2.1.0", "b92651527d6c09c479f9013caa9c7331f19cba38a650590d82ebf2c6c16a1d8a", [:mix], [{:configparser_ex, "~> 2.0", [hex: :configparser_ex, repo: "hexpm", optional: true]}, {:hackney, "1.6.3 or 1.6.5 or 1.7.1 or 1.8.6 or ~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:jsx, "~> 2.8", [hex: :jsx, repo: "hexpm", optional: true]}, {:poison, ">= 1.2.0", [hex: :poison, repo: "hexpm", optional: true]}, {:sweet_xml, "~> 0.6", [hex: :sweet_xml, repo: "hexpm", optional: true]}, {:xml_builder, "~> 0.1.0", [hex: :xml_builder, repo: "hexpm", optional: true]}], "hexpm"}, "ex_aws_s3": {:hex, :ex_aws_s3, "2.0.1", "9e09366e77f25d3d88c5393824e613344631be8db0d1839faca49686e99b6704", [:mix], [{:ex_aws, "~> 2.0", [hex: :ex_aws, repo: "hexpm", optional: false]}, {:sweet_xml, ">= 0.0.0", [hex: :sweet_xml, repo: "hexpm", optional: true]}], "hexpm"}, From 3fc4ea45df240c84135727a239d6bf20fd016a3f Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 31 Mar 2019 18:14:51 +0200 Subject: [PATCH 03/10] BBS: Add documentation. --- .gitignore | 1 + docs/config.md | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/.gitignore b/.gitignore index 04c61ede7..e8052d817 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ /test/tmp/ /doc /instance +/priv/ssh_keys # Prevent committing custom emojis /priv/static/emoji/custom/* diff --git a/docs/config.md b/docs/config.md index 3624e295b..4fb4f530b 100644 --- a/docs/config.md +++ b/docs/config.md @@ -356,3 +356,21 @@ Pleroma account will be created with the same name as the LDAP user name. * `Pleroma.Web.Auth.PleromaAuthenticator`: default database authenticator * `Pleroma.Web.Auth.LDAPAuthenticator`: LDAP authentication + +## BBS / SSH access + +To enable simple command line interface accessible over ssh, add a setting like this to your configuration file: + +```exs +app_dir = File.cwd! +priv_dir = Path.join([app_dir, "priv/ssh_keys"]) + +config :esshd, + enabled: true, + priv_dir: priv_dir, + handler: "Pleroma.BBS.Handler", + port: 10_022, + password_authenticator: "Pleroma.BBS.Authenticator" +``` + +Feel free to adjust the priv_dir and port number. Then you will have to create the key for the keys (in the example `priv/ssh_keys`) and create the host keys with `ssh-keygen -N "" -b 2048 -t rsa -f ssh_host_rsa_key`. After restarting, you should be able to connect to your Pleroma instance with `ssh username@server -p $PORT` From 10fdc080a0048a4776abb4bd1b5aa22d8c65e2da Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 31 Mar 2019 20:35:10 +0200 Subject: [PATCH 04/10] BBS: Tests and formatting. --- config/config.exs | 3 +++ lib/pleroma/bbs/handler.ex | 40 +++++++++++++++++++++----------------- test/bbs/handler_test.exs | 30 ++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 test/bbs/handler_test.exs diff --git a/config/config.exs b/config/config.exs index 0df38d75a..33724346e 100644 --- a/config/config.exs +++ b/config/config.exs @@ -378,6 +378,9 @@ config :pleroma, :ldap, base: System.get_env("LDAP_BASE") || "dc=example,dc=com", uid: System.get_env("LDAP_UID") || "cn" +config :esshd, + enabled: false + # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{Mix.env()}.exs" diff --git a/lib/pleroma/bbs/handler.ex b/lib/pleroma/bbs/handler.ex index 010929ed2..d999dcf76 100644 --- a/lib/pleroma/bbs/handler.ex +++ b/lib/pleroma/bbs/handler.ex @@ -8,34 +8,36 @@ defmodule Pleroma.BBS.Handler do alias Pleroma.Web.ActivityPub.ActivityPub def on_shell(username, _pubkey, _ip, _port) do - :ok = IO.puts "Welcome to #{Pleroma.Config.get([:instance, :name])}!" + :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])) + 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 @@ -63,11 +65,13 @@ 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"]) @@ -81,7 +85,7 @@ defmodule Pleroma.BBS.Handler do |> ActivityPub.contain_timeline(user) |> Enum.reverse() - Enum.each(activities, fn (activity) -> + Enum.each(activities, fn activity -> puts_activity(activity) end) @@ -95,7 +99,7 @@ defmodule Pleroma.BBS.Handler do 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) @@ -105,11 +109,11 @@ defmodule Pleroma.BBS.Handler do 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 +124,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 diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs new file mode 100644 index 000000000..ee5f194bb --- /dev/null +++ b/test/bbs/handler_test.exs @@ -0,0 +1,30 @@ +defmodule Pleroma.BBS.HandlerTest do + use Pleroma.DataCase + alias Pleroma.BBS.Handler + alias Pleroma.Web.CommonAPI + alias Pleroma.User + + import ExUnit.CaptureIO + import Pleroma.Factory + + test "getting the home timeline" do + user = insert(:user) + followed = insert(:user) + + {:ok, user} = User.follow(user, followed) + + {:ok, _first} = CommonAPI.post(user, %{"status" => "hey"}) + {:ok, _second} = CommonAPI.post(followed, %{"status" => "hello"}) + + output = + capture_io(fn -> + Handler.handle_command(%{user: user}, "home") + end) + + assert output =~ user.nickname + assert output =~ followed.nickname + + assert output =~ "hey" + assert output =~ "hello" + end +end From e3bf6655ba412268e4f5fee645609c9738e453ef Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 31 Mar 2019 21:14:21 +0200 Subject: [PATCH 05/10] Add replying. --- lib/pleroma/bbs/handler.ex | 21 ++++++++++++++-- test/bbs/handler_test.exs | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/bbs/handler.ex b/lib/pleroma/bbs/handler.ex index d999dcf76..7749eb3af 100644 --- a/lib/pleroma/bbs/handler.ex +++ b/lib/pleroma/bbs/handler.ex @@ -6,6 +6,7 @@ defmodule Pleroma.BBS.Handler do use Sshd.ShellHandler alias Pleroma.Web.CommonAPI alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Activity def on_shell(username, _pubkey, _ip, _port) do :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!") @@ -42,7 +43,7 @@ defmodule Pleroma.BBS.Handler do 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 @@ -52,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) @@ -104,7 +121,7 @@ defmodule Pleroma.BBS.Handler do {: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}) diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs index ee5f194bb..a22c6d64d 100644 --- a/test/bbs/handler_test.exs +++ b/test/bbs/handler_test.exs @@ -3,9 +3,12 @@ defmodule Pleroma.BBS.HandlerTest do alias Pleroma.BBS.Handler alias Pleroma.Web.CommonAPI alias Pleroma.User + alias Pleroma.Repo + alias Pleroma.Activity import ExUnit.CaptureIO import Pleroma.Factory + import Ecto.Query test "getting the home timeline" do user = insert(:user) @@ -27,4 +30,51 @@ defmodule Pleroma.BBS.HandlerTest do assert output =~ "hey" assert output =~ "hello" end + + test "posting" do + user = insert(:user) + + output = + capture_io(fn -> + Handler.handle_command(%{user: user}, "p this is a test post") + end) + + assert output =~ "Posted" + + activity = + Repo.one( + from(a in Activity, + where: fragment("?->>'type' = ?", a.data, "Create") + ) + ) + + assert activity.actor == user.ap_id + assert activity.data["object"]["content"] == "this is a test post" + end + + test "replying" do + user = insert(:user) + another_user = insert(:user) + + {:ok, activity} = CommonAPI.post(another_user, %{"status" => "this is a test post"}) + + output = + capture_io(fn -> + Handler.handle_command(%{user: user}, "r #{activity.id} this is a reply") + end) + + assert output =~ "Replied" + + reply = + Repo.one( + from(a in Activity, + where: fragment("?->>'type' = ?", a.data, "Create"), + where: a.actor == ^user.ap_id + ) + ) + + assert reply.actor == user.ap_id + assert reply.data["object"]["content"] == "this is a reply" + assert reply.data["object"]["inReplyTo"] == activity.data["object"]["id"] + end end From 629ad1766ce5da434bf095f6baa81a460334e1b2 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 31 Mar 2019 21:53:17 +0200 Subject: [PATCH 06/10] BBS: Some fixes. --- lib/pleroma/bbs/handler.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/bbs/handler.ex b/lib/pleroma/bbs/handler.ex index 7749eb3af..1ebba77d2 100644 --- a/lib/pleroma/bbs/handler.ex +++ b/lib/pleroma/bbs/handler.ex @@ -91,7 +91,7 @@ defmodule Pleroma.BBS.Handler do params = %{} - |> Map.put("type", ["Create", "Announce"]) + |> Map.put("type", ["Create"]) |> Map.put("blocking_user", user) |> Map.put("muting_user", user) |> Map.put("user", user) @@ -100,7 +100,6 @@ defmodule Pleroma.BBS.Handler do [user.ap_id | user.following] |> ActivityPub.fetch_activities(params) |> ActivityPub.contain_timeline(user) - |> Enum.reverse() Enum.each(activities, fn activity -> puts_activity(activity) @@ -109,8 +108,9 @@ defmodule Pleroma.BBS.Handler do 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 From 4c76f49e60287e6618f87a2bae3e0d4e8c444895 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 4 May 2019 15:06:18 +0200 Subject: [PATCH 07/10] BBS: small fixes. --- lib/pleroma/bbs/handler.ex | 6 +----- mix.exs | 2 +- test/bbs/handler_test.exs | 4 ++-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/pleroma/bbs/handler.ex b/lib/pleroma/bbs/handler.ex index 1ebba77d2..75ba35dc2 100644 --- a/lib/pleroma/bbs/handler.ex +++ b/lib/pleroma/bbs/handler.ex @@ -1,12 +1,8 @@ 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.Activity alias Pleroma.Web.CommonAPI alias Pleroma.Web.ActivityPub.ActivityPub - alias Pleroma.Activity def on_shell(username, _pubkey, _ip, _port) do :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!") diff --git a/mix.exs b/mix.exs index 38e83e679..47f7c2903 100644 --- a/mix.exs +++ b/mix.exs @@ -113,7 +113,7 @@ defmodule Pleroma.Mixfile do {:recon, github: "ferd/recon", tag: "2.4.0"}, {:quack, "~> 0.1.1"}, {:benchee, "~> 1.0"}, - {:esshd, "~> 0.1.0"} + {:esshd, "~> 0.1.0"} ] ++ oauth_deps end diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs index a22c6d64d..1386a2d16 100644 --- a/test/bbs/handler_test.exs +++ b/test/bbs/handler_test.exs @@ -1,10 +1,10 @@ defmodule Pleroma.BBS.HandlerTest do use Pleroma.DataCase + alias Pleroma.Activity alias Pleroma.BBS.Handler alias Pleroma.Web.CommonAPI - alias Pleroma.User alias Pleroma.Repo - alias Pleroma.Activity + alias Pleroma.User import ExUnit.CaptureIO import Pleroma.Factory From c9d1cb2dcefb850357b953a7a2d308de39b35e07 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 4 May 2019 15:08:07 +0200 Subject: [PATCH 08/10] BBS: Use cached user fetcher. --- lib/pleroma/bbs/handler.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/bbs/handler.ex b/lib/pleroma/bbs/handler.ex index 75ba35dc2..14e6a6807 100644 --- a/lib/pleroma/bbs/handler.ex +++ b/lib/pleroma/bbs/handler.ex @@ -6,7 +6,7 @@ defmodule Pleroma.BBS.Handler do 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)) + user = Pleroma.User.get_cached_by_nickname(to_string(username)) Logger.debug("#{inspect(user)}") loop(run_state(user: user)) end From c58fd4c038da8305d8840c38f525ceb9f13a644d Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 4 May 2019 15:36:48 +0200 Subject: [PATCH 09/10] BBS: Fix tests. --- test/bbs/handler_test.exs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs index 1386a2d16..148df6ddd 100644 --- a/test/bbs/handler_test.exs +++ b/test/bbs/handler_test.exs @@ -3,6 +3,7 @@ defmodule Pleroma.BBS.HandlerTest do alias Pleroma.Activity alias Pleroma.BBS.Handler alias Pleroma.Web.CommonAPI + alias Pleroma.Object alias Pleroma.Repo alias Pleroma.User @@ -49,7 +50,8 @@ defmodule Pleroma.BBS.HandlerTest do ) assert activity.actor == user.ap_id - assert activity.data["object"]["content"] == "this is a test post" + object = Object.normalize(activity) + assert object.data["content"] == "this is a test post" end test "replying" do @@ -74,7 +76,8 @@ defmodule Pleroma.BBS.HandlerTest do ) assert reply.actor == user.ap_id - assert reply.data["object"]["content"] == "this is a reply" - assert reply.data["object"]["inReplyTo"] == activity.data["object"]["id"] + object = Object.normalize(reply) + assert object.data["content"] == "this is a reply" + assert object.data["inReplyTo"] == activity.data["object"] end end From eb0fb73ddbed109ca4dcd758b60a25ff0dafc883 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 4 May 2019 15:47:50 +0200 Subject: [PATCH 10/10] BBS: Credo fixes. --- lib/pleroma/bbs/handler.ex | 2 +- test/bbs/handler_test.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/bbs/handler.ex b/lib/pleroma/bbs/handler.ex index 14e6a6807..106fe5d18 100644 --- a/lib/pleroma/bbs/handler.ex +++ b/lib/pleroma/bbs/handler.ex @@ -1,8 +1,8 @@ defmodule Pleroma.BBS.Handler do use Sshd.ShellHandler alias Pleroma.Activity - alias Pleroma.Web.CommonAPI 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])}!") diff --git a/test/bbs/handler_test.exs b/test/bbs/handler_test.exs index 148df6ddd..7d5d68d11 100644 --- a/test/bbs/handler_test.exs +++ b/test/bbs/handler_test.exs @@ -2,10 +2,10 @@ defmodule Pleroma.BBS.HandlerTest do use Pleroma.DataCase alias Pleroma.Activity alias Pleroma.BBS.Handler - alias Pleroma.Web.CommonAPI alias Pleroma.Object alias Pleroma.Repo alias Pleroma.User + alias Pleroma.Web.CommonAPI import ExUnit.CaptureIO import Pleroma.Factory