-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
248 lines (220 loc) · 5.77 KB
/
console.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"crypto/tls"
"errors"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"
"codeberg.org/FiskFan1999/gemini"
bolt "go.etcd.io/bbolt"
)
func LogConsoleCommand(user string, priv UserPriviledge, command string) {
if err := db.Update(func(tx *bolt.Tx) error {
logs := tx.Bucket(DBCONSOLELOG)
key := []byte(time.Now().Format(time.RFC3339Nano))
val := []byte(fmt.Sprintf("%s/%s:%s", user, priv, command))
return logs.Put(key, val)
}); err != nil {
fmt.Println("Error during logging console command:", err.Error())
}
}
var ErrNotImplementedYet = errors.New("Not implemented yet")
var ErrUserNotFound = errors.New("User not found")
func DoCommand(command string) (string, gemini.Status) {
/*
ConsoleCommand wrapper
*/
a, b := ConsoleCommand("*internal*", Admin, command)
return a, b
}
func ConsoleCommand(user string, priv UserPriviledge, command string) (string, gemini.Status) {
/*
Log this command in the database.
*/
defer LogConsoleCommand(user, priv, command)
fields := strings.Fields(command)
if len(fields) == 0 {
return "Please enter a command", gemini.BadRequest
}
switch fields[0] {
case "lock":
/*
lock <thread ID>
*/
if len(fields) != 2 {
return "lock <thread ID>", gemini.BadRequest
}
if err := MuteOrUnmuteThread([]byte(fields[1]), []byte("1")); err != nil {
return err.Error(), gemini.TemporaryFailure
}
return "thread has been locked.", gemini.Success
case "unlock":
/*
lock <thread ID>
*/
if len(fields) != 2 {
return "unlock <thread ID>", gemini.BadRequest
}
if err := MuteOrUnmuteThread([]byte(fields[1]), []byte("0")); err != nil {
return err.Error(), gemini.TemporaryFailure
}
return "thread has been unlocked.", gemini.Success
case "mute":
/*
The number amount is in DAYS
*/
if len(fields) < 3 {
return "mute <username> <\"permanent\"/#days>", gemini.BadRequest
}
/*
Don't allow user to write new threads or posts
*/
if err := db.Update(func(tx *bolt.Tx) error {
allusers := tx.Bucket(DBUSERS)
user := allusers.Bucket([]byte(fields[1]))
if user == nil {
// name not found
return ErrUserNotFound
}
if fields[2] == "permanent" {
user.Put([]byte("muted"), []byte("permanent"))
} else {
/*
Attempt to convert fields[2] into
an integer, then find the time
that many days from now, and
write that to the database in
RFC3339 form
*/
days, err := strconv.Atoi(fields[2])
if err != nil {
return errors.New("Invalid field: not \"permanent\" or number of days.")
}
if days <= 0 {
return errors.New("You may not specify a number of days <= 0.")
}
whenToUnban := time.Now().Add(time.Hour * 24 * time.Duration(days))
fmt.Println(whenToUnban)
user.Put([]byte("muted"), []byte(whenToUnban.Format(time.RFC3339)))
}
return nil
}); err != nil {
return err.Error(), gemini.BadRequest
}
return "User has been muted.", gemini.Success
case "unmute":
if len(fields) < 2 {
return "unmute <username>", gemini.BadRequest
}
if err := db.Update(func(tx *bolt.Tx) error {
allusers := tx.Bucket(DBUSERS)
user := allusers.Bucket([]byte(fields[1]))
if user == nil {
// name not found
return ErrUserNotFound
}
user.Put([]byte("muted"), []byte(""))
return nil
}); err != nil {
return err.Error(), gemini.BadRequest
}
return "User has been unmuted.", gemini.Success
case "read":
/*
Read the console command log
*/
dontShowTime := false
numCommands := 32
allCommands := false
if len(fields) > 1 {
if fields[1] == "all" {
allCommands = true
}
nc, err := strconv.Atoi(fields[1])
if err == nil {
numCommands = nc
}
}
if fields[len(fields)-1] == "notime" {
dontShowTime = true
}
var commands []string
if err := db.View(func(tx *bolt.Tx) error {
logs := tx.Bucket(DBCONSOLELOG)
c := logs.Cursor()
k, v := c.Last()
for i := 0; i < numCommands || allCommands; i++ {
if k == nil {
break
}
s := fmt.Sprintf("%s - %s", k, v)
if dontShowTime {
s = string(v)
}
commands = append(commands, s) // log only
k, v = c.Prev()
}
return nil
}); err != nil {
return err.Error(), gemini.TemporaryFailure
}
plain := strings.Join(commands, "\n")
return plain, gemini.Success
case "help":
helpContents, err := os.ReadFile("consolehelp.txt")
if err != nil {
return err.Error(), gemini.TemporaryFailure
}
return string(helpContents), gemini.Success
case "log":
/*
Basic command to write anything into the log.
*/
return "Logged.", gemini.Success
}
return "Unknown command", gemini.BadRequest
}
func MuteOrUnmuteThread(id []byte, status []byte) error {
return db.Update(func(tx *bolt.Tx) error {
allThreads := tx.Bucket(DBALLTHREADS)
thread := allThreads.Bucket(id)
if thread == nil {
return errors.New("Thread not found.")
}
return thread.Put([]byte("locked"), status)
})
}
func ConsoleHandler(u *url.URL, c *tls.Conn) gemini.Response {
var user string
var priv UserPriviledge
if fp := GetFingerprint(c); fp != nil {
user, priv, _, _ = GetUsernameFromFP(fp)
} else {
// no certificate
return gemini.ClientCertificateRequired.Response("Client certificate required")
}
if !priv.Is(Mod) {
/*
Not a moderator
*/
return gemini.CertificateNotAuthorised.Response("Unauthorized")
}
/*
We know it is a moderator or administrator. continue.
*/
if u.RawQuery == "" {
return gemini.Input.Response("Enter command")
}
if unesc, err := url.QueryUnescape(u.RawQuery); err != nil {
return gemini.BadRequest.Error(err)
} else {
commandResponse, status := ConsoleCommand(user, priv, unesc)
if status == gemini.Success {
return gemini.ResponsePlain(fmt.Sprintf("20 text/plain\r\n%s", commandResponse))
}
return status.Response(commandResponse)
}
}