First
[anni] / lib / pleroma / gopher / server.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Gopher.Server do
6   use GenServer
7   require Logger
8
9   def start_link(_) do
10     config = Pleroma.Config.get(:gopher, [])
11     ip = Keyword.get(config, :ip, {0, 0, 0, 0})
12     port = Keyword.get(config, :port, 1234)
13
14     if Keyword.get(config, :enabled, false) do
15       GenServer.start_link(__MODULE__, [ip, port], [])
16     else
17       Logger.info("Gopher server disabled")
18       :ignore
19     end
20   end
21
22   def init([ip, port]) do
23     Logger.info("Starting gopher server on #{port}")
24
25     :ranch.start_listener(
26       :gopher,
27       100,
28       :ranch_tcp,
29       [ip: ip, port: port],
30       __MODULE__.ProtocolHandler,
31       []
32     )
33
34     {:ok, %{ip: ip, port: port}}
35   end
36 end
37
38 defmodule Pleroma.Gopher.Server.ProtocolHandler do
39   alias Pleroma.Activity
40   alias Pleroma.HTML
41   alias Pleroma.Object
42   alias Pleroma.User
43   alias Pleroma.Web.ActivityPub.ActivityPub
44   alias Pleroma.Web.ActivityPub.Visibility
45
46   def start_link(ref, socket, transport, opts) do
47     pid = spawn_link(__MODULE__, :init, [ref, socket, transport, opts])
48     {:ok, pid}
49   end
50
51   def init(ref, socket, transport, [] = _Opts) do
52     :ok = :ranch.accept_ack(ref)
53     loop(socket, transport)
54   end
55
56   def info(text) do
57     text = String.replace(text, ~r/[\t\n]/, "")
58
59     String.split(text, "\r")
60     |> Enum.map(fn text ->
61       "i#{text}\tfake\t(NULL)\t0\r\n"
62     end)
63     |> Enum.join("")
64   end
65
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"
71   end
72
73   def render_activities(activities) do
74     activities
75     |> Enum.reverse()
76     |> Enum.map(fn activity ->
77       user = User.get_cached_by_ap_id(activity.data["actor"])
78
79       object = Object.normalize(activity, fetch: false)
80       like_count = object.data["like_count"] || 0
81       announcement_count = object.data["announcement_count"] || 0
82
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")))
87     end)
88     |> Enum.join("i\tfake\t(NULL)\t0\r\n")
89   end
90
91   def response("") do
92     info("Welcome to #{Pleroma.Config.get([:instance, :name], "Pleroma")}!") <>
93       link("Public Timeline", "/main/public") <>
94       link("Federated Timeline", "/main/all") <> ".\r\n"
95   end
96
97   def response("/main/public") do
98     posts =
99       %{type: ["Create"], local_only: true}
100       |> ActivityPub.fetch_public_activities()
101       |> render_activities()
102
103     info("Welcome to the Public Timeline!") <> posts <> ".\r\n"
104   end
105
106   def response("/main/all") do
107     posts =
108       %{type: ["Create"]}
109       |> ActivityPub.fetch_public_activities()
110       |> render_activities()
111
112     info("Welcome to the Federated Timeline!") <> posts <> ".\r\n"
113   end
114
115   def response("/notices/" <> id) do
116     with %Activity{} = activity <- Activity.get_by_id(id),
117          true <- Visibility.is_public?(activity) do
118       activities =
119         ActivityPub.fetch_activities_for_context(activity.data["context"])
120         |> render_activities
121
122       user = User.get_cached_by_ap_id(activity.data["actor"])
123
124       info("Post #{activity.id} by #{user.nickname}") <>
125         link("More posts by #{user.nickname}", "/users/#{user.nickname}") <> activities <> ".\r\n"
126     else
127       _e ->
128         info("Not public") <> ".\r\n"
129     end
130   end
131
132   def response("/users/" <> nickname) do
133     with %User{} = user <- User.get_cached_by_nickname(nickname) do
134       params = %{
135         type: ["Create"],
136         actor_id: user.ap_id
137       }
138
139       activities =
140         params
141         |> ActivityPub.fetch_public_activities()
142         |> render_activities()
143
144       info("Posts by #{user.nickname}") <> activities <> ".\r\n"
145     else
146       _e ->
147         info("No such user") <> ".\r\n"
148     end
149   end
150
151   def loop(socket, transport) do
152     case transport.recv(socket, 0, 5000) do
153       {:ok, data} ->
154         data = String.trim_trailing(data, "\r\n")
155         transport.send(socket, response(data))
156         :ok = transport.close(socket)
157
158       _ ->
159         :ok = transport.close(socket)
160     end
161   end
162 end