1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 = 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 "i\tfake\t(NULL)\t0\r\n" <>
86 info(HTML.strip_tags(String.replace(activity.data["object"]["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 ActivityPub.fetch_public_activities(%{"type" => ["Create"], "local_only" => true})
102 info("Welcome to the Public Timeline!") <> posts <> ".\r\n"
105 def response("/main/all") do
107 ActivityPub.fetch_public_activities(%{"type" => ["Create"]})
110 info("Welcome to the Federated Timeline!") <> posts <> ".\r\n"
113 def response("/notices/" <> id) do
114 with %Activity{} = activity <- Repo.get(Activity, id),
115 true <- Visibility.is_public?(activity) do
117 ActivityPub.fetch_activities_for_context(activity.data["context"])
120 user = User.get_cached_by_ap_id(activity.data["actor"])
122 info("Post #{activity.id} by #{user.nickname}") <>
123 link("More posts by #{user.nickname}", "/users/#{user.nickname}") <> activities <> ".\r\n"
126 info("Not public") <> ".\r\n"
130 def response("/users/" <> nickname) do
131 with %User{} = user <- User.get_cached_by_nickname(nickname) do
133 "type" => ["Create"],
134 "actor_id" => user.ap_id
138 ActivityPub.fetch_public_activities(params)
141 info("Posts by #{user.nickname}") <> activities <> ".\r\n"
144 info("No such user") <> ".\r\n"
148 def loop(socket, transport) do
149 case transport.recv(socket, 0, 5000) do
151 data = String.trim_trailing(data, "\r\n")
152 transport.send(socket, response(data))
153 :ok = transport.close(socket)
156 :ok = transport.close(socket)