5b714be6ae879373689c34ac7b14c2aa6c49652d
[akkoma] / lib / pleroma / gopher / server.ex
1 defmodule Pleroma.Gopher.Server do
2 use GenServer
3 require Logger
4 @gopher Application.get_env(:pleroma, :gopher)
5
6 def start_link() do
7 ip = Keyword.get(@gopher, :ip, {0, 0, 0, 0})
8 port = Keyword.get(@gopher, :port, 1234)
9 GenServer.start_link(__MODULE__, [ip, port], [])
10 end
11
12 def init([ip, port]) do
13 if Keyword.get(@gopher, :enabled, false) do
14 Logger.info("Starting gopher server on #{port}")
15
16 :ranch.start_listener(
17 :gopher,
18 100,
19 :ranch_tcp,
20 [port: port],
21 __MODULE__.ProtocolHandler,
22 []
23 )
24
25 {:ok, %{ip: ip, port: port}}
26 else
27 Logger.info("Gopher server disabled")
28 {:ok, nil}
29 end
30 end
31 end
32
33 defmodule Pleroma.Gopher.Server.ProtocolHandler do
34 alias Pleroma.Web.ActivityPub.ActivityPub
35 alias Pleroma.User
36 alias Pleroma.Activity
37 alias Pleroma.Repo
38
39 @instance Application.get_env(:pleroma, :instance)
40 @gopher Application.get_env(:pleroma, :gopher)
41
42 def start_link(ref, socket, transport, opts) do
43 pid = spawn_link(__MODULE__, :init, [ref, socket, transport, opts])
44 {:ok, pid}
45 end
46
47 def init(ref, socket, transport, _Opts = []) do
48 :ok = :ranch.accept_ack(ref)
49 loop(socket, transport)
50 end
51
52 def info(text) do
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"
57 end)
58 |> Enum.join("")
59 end
60
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"
65 end
66
67 def response("") do
68 info("Welcome to #{Keyword.get(@instance, :name, "Pleroma")}!") <>
69 link("Public Timeline", "/main/public") <>
70 link("Federated Timeline", "/main/all") <> ".\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 "\r\n" <> info(HtmlSanitizeEx.strip_tags(String.replace(activity.data["object"]["content"], "<br>", "\n")))
86 end)
87 |> Enum.join("\r\n")
88 end
89
90 def response("/main/public") do
91 posts =
92 ActivityPub.fetch_public_activities(%{"type" => ["Create"], "local_only" => true})
93 |> render_activities
94
95 info("Welcome to the Public Timeline!") <> posts <> ".\r\n"
96 end
97
98 def response("/main/all") do
99 posts =
100 ActivityPub.fetch_public_activities(%{"type" => ["Create"]})
101 |> render_activities
102
103 info("Welcome to the Federated Timeline!") <> posts <> ".\r\n"
104 end
105
106 def response("/notices/" <> id) do
107 with %Activity{} = activity <- Repo.get(Activity, id),
108 true <- ActivityPub.is_public?(activity) do
109 activities =
110 ActivityPub.fetch_activities_for_context(activity.data["context"])
111 |> render_activities
112
113 user = User.get_cached_by_ap_id(activity.data["actor"])
114
115 info("Post #{activity.id} by #{user.nickname}") <>
116 link("More posts by #{user.nickname}", "/users/#{user.nickname}") <> activities <> ".\r\n"
117 else
118 _e ->
119 info("Not public") <> ".\r\n"
120 end
121 end
122
123 def response("/users/" <> nickname) do
124 with %User{} = user <- User.get_cached_by_nickname(nickname) do
125 params = %{
126 "type" => ["Create"],
127 "actor_id" => user.ap_id
128 }
129
130 activities =
131 ActivityPub.fetch_public_activities(params)
132 |> render_activities
133
134 info("Posts by #{user.nickname}") <> activities <> ".\r\n"
135 else
136 _e ->
137 info("No such user") <> ".\r\n"
138 end
139 end
140
141 def loop(socket, transport) do
142 case transport.recv(socket, 0, 5000) do
143 {:ok, data} ->
144 data = String.trim_trailing(data, "\r\n")
145 transport.send(socket, response(data))
146 :ok = transport.close(socket)
147
148 _ ->
149 :ok = transport.close(socket)
150 end
151 end
152 end