1 defmodule Pleroma.Gopher.Server do
4 @gopher Application.get_env(:pleroma, :gopher)
7 ip = Keyword.get(@gopher, :ip, {0, 0, 0, 0})
8 port = Keyword.get(@gopher, :port, 1234)
9 GenServer.start_link(__MODULE__, [ip, port], [])
12 def init([ip, port]) do
13 if Keyword.get(@gopher, :enabled, false) do
14 Logger.info("Starting gopher server on #{port}")
16 :ranch.start_listener(
21 __MODULE__.ProtocolHandler,
25 {:ok, %{ip: ip, port: port}}
27 Logger.info("Gopher server disabled")
33 defmodule Pleroma.Gopher.Server.ProtocolHandler do
34 alias Pleroma.Web.ActivityPub.ActivityPub
36 alias Pleroma.Activity
39 @instance Application.get_env(:pleroma, :instance)
40 @gopher Application.get_env(:pleroma, :gopher)
42 def start_link(ref, socket, transport, opts) do
43 pid = spawn_link(__MODULE__, :init, [ref, socket, transport, opts])
47 def init(ref, socket, transport, _Opts = []) do
48 :ok = :ranch.accept_ack(ref)
49 loop(socket, transport)
53 text = String.replace(text, ~r/[\t\n]/, "")
54 String.split(text, "\r")
55 |> Enum.map(fn (text) ->
56 "i#{text}\tfake\(NULL)\t0\r\n"
61 def link(name, selector, type \\ 1) do
62 address = Pleroma.Web.Endpoint.host()
63 port = Keyword.get(@gopher, :port, 1234)
64 "#{type}#{name}\t#{selector}\t#{address}\t#{port}\r\n"
68 info("Welcome to #{Keyword.get(@instance, :name, "Pleroma")}!") <>
69 link("Public Timeline", "/main/public") <>
70 link("Federated Timeline", "/main/all") <> ".\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 = activity.data["object"]
80 like_count = object["like_count"] || 0
81 announcement_count = object["announcement_count"] || 0
83 link("Post ##{activity.id} by #{user.nickname}", "/notices/#{activity.id}") <>
84 info("#{like_count} likes, #{announcement_count} repeats") <>
85 "\r\n" <> info(HtmlSanitizeEx.strip_tags(String.replace(activity.data["object"]["content"], "<br>", "\n")))
90 def response("/main/public") do
92 ActivityPub.fetch_public_activities(%{"type" => ["Create"], "local_only" => true})
95 info("Welcome to the Public Timeline!") <> posts <> ".\r\n"
98 def response("/main/all") do
100 ActivityPub.fetch_public_activities(%{"type" => ["Create"]})
103 info("Welcome to the Federated Timeline!") <> posts <> ".\r\n"
106 def response("/notices/" <> id) do
107 with %Activity{} = activity <- Repo.get(Activity, id),
108 true <- ActivityPub.is_public?(activity) do
110 ActivityPub.fetch_activities_for_context(activity.data["context"])
113 user = User.get_cached_by_ap_id(activity.data["actor"])
115 info("Post #{activity.id} by #{user.nickname}") <>
116 link("More posts by #{user.nickname}", "/users/#{user.nickname}") <> activities <> ".\r\n"
119 info("Not public") <> ".\r\n"
123 def response("/users/" <> nickname) do
124 with %User{} = user <- User.get_cached_by_nickname(nickname) do
126 "type" => ["Create"],
127 "actor_id" => user.ap_id
131 ActivityPub.fetch_public_activities(params)
134 info("Posts by #{user.nickname}") <> activities <> ".\r\n"
137 info("No such user") <> ".\r\n"
141 def loop(socket, transport) do
142 case transport.recv(socket, 0, 5000) do
144 data = String.trim_trailing(data, "\r\n")
145 transport.send(socket, response(data))
146 :ok = transport.close(socket)
149 :ok = transport.close(socket)