1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Gopher.Server do
10 config = Pleroma.Config.get(:gopher, [])
11 ip = Keyword.get(config, :ip, {0, 0, 0, 0})
12 port = Keyword.get(config, :port, 1234)
14 if Keyword.get(config, :enabled, false) do
15 GenServer.start_link(__MODULE__, [ip, port], [])
17 Logger.info("Gopher server disabled")
22 def init([ip, port]) do
23 Logger.info("Starting gopher server on #{port}")
25 :ranch.start_listener(
30 __MODULE__.ProtocolHandler,
34 {:ok, %{ip: ip, port: port}}
38 defmodule Pleroma.Gopher.Server.ProtocolHandler do
39 alias Pleroma.Activity
43 alias Pleroma.Web.ActivityPub.ActivityPub
44 alias Pleroma.Web.ActivityPub.Visibility
46 def start_link(ref, socket, transport, opts) do
47 pid = spawn_link(__MODULE__, :init, [ref, socket, transport, opts])
51 def init(ref, socket, transport, [] = _Opts) do
52 :ok = :ranch.accept_ack(ref)
53 loop(socket, transport)
57 text = String.replace(text, ~r/[\t\n]/, "")
59 String.split(text, "\r")
60 |> Enum.map(fn text ->
61 "i#{text}\tfake\t(NULL)\t0\r\n"
66 def link(name, selector, type \\ 1) do
67 address = Pleroma.Web.Endpoint.host()
68 port = Pleroma.Config.get([:gopher, :port], 1234)
69 dstport = Pleroma.Config.get([:gopher, :dstport], port)
70 "#{type}#{name}\t#{selector}\t#{address}\t#{dstport}\r\n"
73 def render_activities(activities) do
76 |> Enum.map(fn activity ->
77 user = User.get_cached_by_ap_id(activity.data["actor"])
79 object = Object.normalize(activity, fetch: false)
80 like_count = object.data["like_count"] || 0
81 announcement_count = object.data["announcement_count"] || 0
83 link("Post ##{activity.id} by #{user.nickname}", "/notices/#{activity.id}") <>
84 info("#{like_count} likes, #{announcement_count} repeats") <>
85 "i\tfake\t(NULL)\t0\r\n" <>
86 info(HTML.strip_tags(String.replace(object.data["content"], "<br>", "\r")))
88 |> Enum.join("i\tfake\t(NULL)\t0\r\n")
92 info("Welcome to #{Pleroma.Config.get([:instance, :name], "Pleroma")}!") <>
93 link("Public Timeline", "/main/public") <>
94 link("Federated Timeline", "/main/all") <> ".\r\n"
97 def response("/main/public") do
99 %{type: ["Create"], local_only: true}
100 |> ActivityPub.fetch_public_activities()
101 |> render_activities()
103 info("Welcome to the Public Timeline!") <> posts <> ".\r\n"
106 def response("/main/all") do
109 |> ActivityPub.fetch_public_activities()
110 |> render_activities()
112 info("Welcome to the Federated Timeline!") <> posts <> ".\r\n"
115 def response("/notices/" <> id) do
116 with %Activity{} = activity <- Activity.get_by_id(id),
117 true <- Visibility.is_public?(activity) do
119 ActivityPub.fetch_activities_for_context(activity.data["context"])
122 user = User.get_cached_by_ap_id(activity.data["actor"])
124 info("Post #{activity.id} by #{user.nickname}") <>
125 link("More posts by #{user.nickname}", "/users/#{user.nickname}") <> activities <> ".\r\n"
128 info("Not public") <> ".\r\n"
132 def response("/users/" <> nickname) do
133 with %User{} = user <- User.get_cached_by_nickname(nickname) do
141 |> ActivityPub.fetch_public_activities()
142 |> render_activities()
144 info("Posts by #{user.nickname}") <> activities <> ".\r\n"
147 info("No such user") <> ".\r\n"
151 def loop(socket, transport) do
152 case transport.recv(socket, 0, 5000) do
154 data = String.trim_trailing(data, "\r\n")
155 transport.send(socket, response(data))
156 :ok = transport.close(socket)
159 :ok = transport.close(socket)