-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.go
444 lines (375 loc) · 11.9 KB
/
post.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package main
import (
"encoding/base64"
"fmt"
"io"
"log"
"net/http"
"net/mail"
"os"
"strings"
"unicode/utf16"
"github.com/WiiLink24/nwc24"
"github.com/gin-gonic/gin"
)
var (
InsertMail = `INSERT INTO mail (snowflake, data, sender, recipient, is_sent) VALUES ($1, $2, $3, $4, false)`
CheckRegistration = `SELECT EXISTS(SELECT 1 FROM accounts WHERE mlid = $1)`
CheckInboundOutbound = `SELECT (SELECT COUNT(*) FROM mail WHERE recipient = $1 AND is_sent = false) AS inbound_count, (SELECT COUNT(*) FROM mail WHERE sender = $1 AND is_sent = false) AS outbound_count`
CheckOutbound = `SELECT COUNT(*) FROM mail WHERE sender = $1 AND is_sent = false`
DeleteInbound = `DELETE FROM mail WHERE recipient = $1 AND is_sent = false`
DeleteOutbound = `DELETE FROM mail WHERE sender = $1 AND is_sent = false`
DeleteAccount = `DELETE FROM accounts WHERE mlid = $1`
)
func SendMessage(c *gin.Context) {
//fetch the message from the form
recipient_type := c.PostForm("recipient_type")
subject := c.PostForm("subject")
message := c.PostForm("message_content")
recipient := c.PostForm("recipient")
attachment, _ := c.FormFile("attachment")
mii, _ := c.FormFile("mii")
conv_message := utf16.Encode([]rune(message))
message = nwc24.UTF16ToString(conv_message)
formatted_recipient := strings.ReplaceAll(recipient, "-", "")
//validations
//check if the recipient is valid
if !validateFriendCode(formatted_recipient) {
c.HTML(http.StatusInternalServerError, "send_message.html", gin.H{
"Title": "Send Message | WiiLink Mail",
"Error": "This Wii Number is invalid (most likely a default Dolphin number).",
})
}
//check if the recipient is registered
var exists bool
row, err := wiiMailPool.Query(ctx, CheckRegistration, formatted_recipient)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Title": "Error | WiiLink Mail",
"Error": "Couldn't query the database.",
})
}
for row.Next() {
err = row.Scan(&exists)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Title": "Error | WiiLink Mail",
"Error": "Couldn't scan the rows.",
})
}
}
if !exists {
c.HTML(http.StatusInternalServerError, "send_message.html", gin.H{
"Title": "Send Message | WiiLink Mail",
"Error": "This Wii Number is not registered in the database.",
})
}
sender_address, err := mail.ParseAddress("[email protected]")
if err != nil {
fmt.Println(err)
}
var recipient_address *mail.Address
if recipient == "" && recipient_type == "all" {
recipient_address, err = mail.ParseAddress("[email protected]")
if err != nil {
fmt.Println(err)
}
} else if recipient != "" && recipient_type == "single" {
recipient_address, err = mail.ParseAddress("w" + formatted_recipient + "@rc24.xyz")
if err != nil {
fmt.Println(err)
}
} else {
fmt.Println("Invalid recipient type")
}
//initialize the message
data := nwc24.NewMessage(sender_address, recipient_address)
data.SetSubject(subject)
data.SetBoundary(generateBoundary())
data.SetContentType(nwc24.MultipartMixed)
data.SetTag("X-Wii-MB-NoReply", "1")
// Attach the Mii if it exists
if mii != nil {
// get the Mii data into base64 utf-16be
mii_data := base64.StdEncoding.EncodeToString(encodeToUTF16BE(mii.Filename))
data.SetTag("X-WiiFace", mii_data)
} else {
fmt.Println("No Mii uploaded, skipping...")
}
//create the text multipart
text_multipart := nwc24.NewMultipart()
//now we append the data
text_multipart.SetText(message, nwc24.UTF16BE)
text_multipart.SetContentType(nwc24.PlainText)
//add the multipart to the message
data.AddMultipart(text_multipart)
// Attach the attachment image if it exists
if err != nil {
fmt.Println("Error retrieving the file:", err)
return
}
if attachment != nil {
attachmentMultipart := nwc24.NewMultipart()
attachmentMultipart.SetContentType(nwc24.Jpeg)
file, err := attachment.Open()
if err != nil {
fmt.Println("Error opening the file:", err)
return
}
defer file.Close()
attachmentBytes, err := io.ReadAll(file)
if err != nil {
fmt.Println("Error reading the file:", err)
return
}
attachmentMultipart.AddFile("attachment", attachmentBytes, nwc24.Jpeg)
data.AddMultipart(attachmentMultipart)
} else {
fmt.Println("No attachment uploaded, skipping...")
}
// Fetch the message from the form only if letter and thumbnail are uploaded
letterFile, _ := c.FormFile("letter")
thumbnailFile, _ := c.FormFile("thumbnail")
if letterFile != nil || thumbnailFile != nil {
uploadToGenerator(c, "letter", "letter.png")
uploadToGenerator(c, "thumbnail", "thumbnail.png")
audioFile, _, _ := c.Request.FormFile("audio")
if audioFile != nil {
uploadToGenerator(c, "audio", "sound.wav")
}
letterheadContent, _ := generateLetterhead()
log.Printf("Letterhead content: %s", letterheadContent)
// Include the letterhead in the message
letterheadMultipart := nwc24.NewMultipart()
letterheadMultipart.AddFile("letterhead", []byte(letterheadContent), nwc24.WiiMessageBoard)
data.AddMultipart(letterheadMultipart)
} else {
fmt.Println("No letter or thumbnail uploaded, skipping...")
}
// Generate the message
var content string
if recipient_type == "all" {
content, err = nwc24.CreateMessageToSend(generateBoundary(), data)
} else {
content, err = data.ToString()
// Remove the Content type at the top
content = strings.Replace(content, "Content-Type: text/plain\r\n\r\n", "", 1)
}
if err != nil {
fmt.Println(err)
} else {
fmt.Println(content)
// Write into txt file
file, err := os.Create("generated_message.txt")
if err != nil {
fmt.Println("Error creating file:", err)
} else {
defer file.Close()
_, err = file.WriteString(content)
if err != nil {
fmt.Println("Error writing to file:", err)
}
}
}
if recipient_type == "all" {
//insert null value for recipient
_, err = encryptMessage(content)
if err != nil {
fmt.Println(err)
}
} else {
_, err = wiiMailPool.Exec(ctx, InsertMail, flakeNode.Generate(), content, "9999999900000000", formatted_recipient)
}
if err != nil {
fmt.Println(err)
}
c.Redirect(http.StatusTemporaryRedirect, "/send#success")
}
func CheckInOutMessages(c *gin.Context) {
wiiNumber := c.PostForm("wii_number")
formatted_number := strings.ReplaceAll(wiiNumber, "-", "")
if !validateFriendCode(formatted_number) {
c.HTML(http.StatusInternalServerError, "inbound.html", gin.H{
"Error": "This Wii Number is invalid (most likely a default Dolphin number).",
})
}
var exists bool
row, err := wiiMailPool.Query(ctx, CheckRegistration, formatted_number)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Error": "Couldn't query the database.",
})
}
for row.Next() {
err = row.Scan(&exists)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Error": "Couldn't scan the rows.",
})
}
}
if !exists {
c.HTML(http.StatusInternalServerError, "send_message.html", gin.H{
"Error": "This Wii Number is not registered in the database.",
})
}
var inbound, outbound int
rows, err := wiiMailPool.Query(ctx, CheckInboundOutbound, formatted_number)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Error": "Couldn't query the database.",
})
}
for rows.Next() {
err = rows.Scan(&inbound, &outbound)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Error": "Couldn't scan the rows.",
})
}
}
c.HTML(http.StatusOK, "inbound.html", gin.H{
"Title": "Check Messages | WiiLink Mail",
"Inbound": inbound,
"Outbound": outbound,
})
}
func DeleteMessages(c *gin.Context) {
action_type := c.PostForm("type")
wiiNumber := c.PostForm("wii_number")
formatted_number := strings.ReplaceAll(wiiNumber, "-", "")
if !validateFriendCode(formatted_number) {
c.HTML(http.StatusInternalServerError, "inbound.html", gin.H{
"Error": "This Wii Number is invalid (most likely a default Dolphin number).",
})
}
var exists bool
row, err := wiiMailPool.Query(ctx, CheckRegistration, formatted_number)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Error": "Couldn't query the database.",
})
}
for row.Next() {
err = row.Scan(&exists)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Error": "Couldn't scan the rows.",
})
}
}
if !exists {
c.HTML(http.StatusInternalServerError, "send_message.html", gin.H{
"Error": "This Wii Number is not registered in the database.",
})
}
if action_type == "inbound" {
_, err = wiiMailPool.Exec(ctx, DeleteInbound, formatted_number)
if err != nil {
fmt.Println(err)
}
} else if action_type == "outbound" {
_, err = wiiMailPool.Exec(ctx, DeleteOutbound, formatted_number)
if err != nil {
fmt.Println(err)
}
}
c.Redirect(http.StatusTemporaryRedirect, "/clear#success")
}
func checkIsValidNumber(c *gin.Context) {
wiiNumber := c.PostForm("wii_number")
formatted_number := strings.ReplaceAll(wiiNumber, "-", "")
if !validateFriendCode(formatted_number) {
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"TestName": "Wii Number Validation Result",
"Result": "This Wii Number is invalid. It could be either a default Dolphin number, or a mistyped number.",
})
} else {
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"TestName": "Wii Number Validation Result",
"Result": "This Wii Number is valid.",
})
}
}
func checkIsRegistered(c *gin.Context) {
wiiNumber := c.PostForm("wii_number")
formatted_number := strings.ReplaceAll(wiiNumber, "-", "")
var exists bool
row, err := wiiMailPool.Query(ctx, CheckRegistration, formatted_number)
if err != nil {
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"Error": "Couldn't query the database.",
})
}
for row.Next() {
err = row.Scan(&exists)
if err != nil {
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"Error": "Couldn't scan the rows.",
})
}
}
if exists {
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"TestName": "Wii Number Registration Check",
"Result": "This Wii Number is registered in the database.",
})
} else {
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"TestName": "Wii Number Registration Check",
"Result": "This Wii Number is not registered.",
})
}
}
func RemoveAccount(c *gin.Context) {
wiiNumber := c.PostForm("wii_number")
formatted_number := strings.ReplaceAll(wiiNumber, "-", "")
if !validateFriendCode(formatted_number) {
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"Error": "This Wii Number is invalid (most likely a default Dolphin number).",
})
}
var exists bool
row, err := wiiMailPool.Query(ctx, CheckRegistration, formatted_number)
if err != nil {
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"Error": "Couldn't query the database.",
})
}
for row.Next() {
err = row.Scan(&exists)
if err != nil {
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"Error": "Couldn't scan the rows.",
})
}
}
if !exists {
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"Error": "This Wii Number is not registered in the database.",
})
} else {
_, err = wiiMailPool.Exec(ctx, DeleteAccount, formatted_number)
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Title": "Error | WiiLink Mail",
"Error": "Couldn't delete the account.",
})
}
c.HTML(http.StatusOK, "misc.html", gin.H{
"Title": "Miscellaneous | WiiLink Mail",
"TestName": "Account Removal",
"Result": "The account has been removed.",
})
}
}