annotate src/add_server.html.luan @ 15:d0b8e1a8fc69

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 01 Nov 2023 00:12:08 -0600
parents 80105b716a62
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
3 local Html = require "luan:Html.luan"
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
4 local html_encode = Html.encode or error()
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
5 local Parsers = require "luan:Parsers.luan"
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
6 local json_parse = Parsers.json_parse or error()
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Io = require "luan:Io.luan"
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
8 local uri = Io.uri or error()
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Http = require "luan:http/Http.luan"
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local Shared = require "site:/lib/Shared.luan"
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local head = Shared.head or error()
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local header = Shared.header or error()
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local footer = Shared.footer or error()
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
14 local Server = require "site:/lib/Server.luan"
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
15 local Config = require "site:/lib/Config.luan"
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
18 local function error_page(msg)
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
19 Io.stdout = Http.response.text_writer()
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
20 %>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
21 <!doctype html>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
22 <html>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
23 <head>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
24 <% head() %>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
25 <title>Disearch</title>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
26 </head>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
27 <body>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
28 <% header() %>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
29 <p><%=msg%></p>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
30 <% footer() %>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
31 </body>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
32 </html>
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
33 <%
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
34 end
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
35
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 return function()
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
37 local parameters = Http.request.parameters
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
38 if parameters.error == "access_denied" then
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
39 Http.response.send_redirect("/servers.html")
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
40 return
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
41 end
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
42 local guild_id = parameters.guild_id or error()
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
43 if Server.get_by_discord_id(guild_id) ~= nil then
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
44 error_page("already added")
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
45 return
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
46 end
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
47
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
48 local url = "https://discord.com/api/guilds/"..guild_id
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
49 local config = Config.get()
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
50 local options = {
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
51 headers = {
15
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
52 ["User-Agent"] = "Disearch" -- for retarded Cloudflare
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
53 Authorization = "Bot "..config.discord.bot_token
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
54 }
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
55 }
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
56 local result = uri(url,options).read_text()
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
57 --logger.info(result)
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
58 result = json_parse(result)
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
59
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
60 local name = result.name or error()
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
61 name = html_encode(name)
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
62 local icon = result.icon
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
63 if icon == nil then
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
64 error_page("server must have icon")
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
65 return
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
66 end
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 Io.stdout = Http.response.text_writer()
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 %>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 <!doctype html>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 <html>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 <head>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 <% head() %>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 <title>Disearch</title>
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
74 <style>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
75 input[type="url"] {
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
76 width: 100%;
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
77 }
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
78 </style>
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 </head>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 <body>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 <% header() %>
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
82
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
83 <h1>Add server</h1>
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
84
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
85 <form page onsubmit="ajaxForm('/add_server.js',this); return false">
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
86 <input type=hidden name=id value="<%=guild_id%>">
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
87 <input type=hidden name=name value="<%=name%>">
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
88 <input type=hidden name=icon value="<%=icon%>">
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
89 <p><%=name%></p>
14
80105b716a62 start bot
Franklin Schmidt <fschmidt@gmail.com>
parents: 12
diff changeset
90 <p><img icon src="https://cdn.discordapp.com/icons/<%=guild_id%>/<%=icon%>.png"></p>
12
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
91 <p>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
92 Invite URL:<br>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
93 <input type=url required name=invite>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
94 <span error=invite></span>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
95 </p>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
96 <p><input type=submit></p>
3e2cb946d511 form handling
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
97 </form>
11
62dd23f0b549 start add server
Franklin Schmidt <fschmidt@gmail.com>
parents: 3
diff changeset
98
1
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 <% footer() %>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 </body>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 </html>
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 <%
d19b150ecb83 add Shared
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 end