3b9629d77643a4fa3e3f9f817af76626aa4bb542
[akkoma] / lib / pleroma / gopher / server.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Repo
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 = activity.data["object"]
80 like_count = object["like_count"] || 0
81 announcement_count = object["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(activity.data["object"]["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 ActivityPub.fetch_public_activities(%{"type" => ["Create"], "local_only" => true})
100 |> render_activities
101
102 info("Welcome to the Public Timeline!") <> posts <> ".\r\n"
103 end
104
105 def response("/main/all") do
106 posts =
107 ActivityPub.fetch_public_activities(%{"type" => ["Create"]})
108 |> render_activities
109
110 info("Welcome to the Federated Timeline!") <> posts <> ".\r\n"
111 end
112
113 def response("/notices/" <> id) do
114 with %Activity{} = activity <- Repo.get(Activity, id),
115 true <- Visibility.is_public?(activity) do
116 activities =
117 ActivityPub.fetch_activities_for_context(activity.data["context"])
118 |> render_activities
119
120 user = User.get_cached_by_ap_id(activity.data["actor"])
121
122 info("Post #{activity.id} by #{user.nickname}") <>
123 link("More posts by #{user.nickname}", "/users/#{user.nickname}") <> activities <> ".\r\n"
124 else
125 _e ->
126 info("Not public") <> ".\r\n"
127 end
128 end
129
130 def response("/users/" <> nickname) do
131 with %User{} = user <- User.get_cached_by_nickname(nickname) do
132 params = %{
133 "type" => ["Create"],
134 "actor_id" => user.ap_id
135 }
136
137 activities =
138 ActivityPub.fetch_public_activities(params)
139 |> render_activities
140
141 info("Posts by #{user.nickname}") <> activities <> ".\r\n"
142 else
143 _e ->
144 info("No such user") <> ".\r\n"
145 end
146 end
147
148 def loop(socket, transport) do
149 case transport.recv(socket, 0, 5000) do
150 {:ok, data} ->
151 data = String.trim_trailing(data, "\r\n")
152 transport.send(socket, response(data))
153 :ok = transport.close(socket)
154
155 _ ->
156 :ok = transport.close(socket)
157 end
158 end
159 end