This repository has been archived by the owner on Feb 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
inbound_parse.go
98 lines (84 loc) · 2.79 KB
/
inbound_parse.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
package main
import (
"github.com/Disconnect24/Mail-GO/utilities"
"github.com/gin-gonic/gin"
"io/ioutil"
"log"
"net/http"
"net/mail"
"regexp"
"github.com/google/uuid"
)
var mailDomain *regexp.Regexp
func sendGridHandler(c *gin.Context) {
text := c.PostForm("text")
// TODO: Properly verify attachments.
if c.PostForm("from") == "" || c.PostForm("to") == "" {
// something was nil
log.Println("Something happened to SendGrid... is someone else accessing?")
return
}
// If there's no text in the email.
if text == "" {
text = "No message provided."
}
// Figure out who sent it.
fromAddress, err := mail.ParseAddress(c.PostForm("from"))
if err != nil {
log.Printf("given from address is invalid: %v", err)
return
}
toAddress := c.PostForm("to")
// Validate who's being mailed.
potentialMailInformation := mailDomain.FindStringSubmatch(toAddress)
if potentialMailInformation == nil || potentialMailInformation[2] != global.SendGridDomain {
log.Println("to address didn't match")
return
}
// 16 digit ID
recipientMlid := potentialMailInformation[1]
// We "create" a response for the Wii to use, based off attachments and multipart components.
// TODO: potentially handle all attachments until first image type?
var attachedFile []byte
attachment, err := c.FormFile("attachment1")
if err == http.ErrMissingFile {
// We don't care if there's nothing, it'll just stay nil.
} else if err != nil {
utilities.LogError(ravenClient, "Failed to read attachment from form.", err)
c.Status(http.StatusInternalServerError)
return
} else {
file, err := attachment.Open()
if err != nil {
utilities.LogError(ravenClient, "Failed to open attachment from form.", err)
c.Status(http.StatusInternalServerError)
}
attachedFile, err = ioutil.ReadAll(file)
if err != nil {
utilities.LogError(ravenClient, "Failed to read attachment from form.", err)
c.Status(http.StatusInternalServerError)
return
}
}
wiiMail, err := FormulateMail(fromAddress.Address, toAddress, c.PostForm("subject"), text, attachedFile)
if err != nil {
log.Printf("error formulating mail: %v", err)
return
}
// On a normal Wii service, we'd return the cd/msg response.
// This goes to SendGrid, and we hope the database error is resolved
// later on - any non-success tells it to POST again.
stmt, err := db.Prepare("INSERT INTO `mails` (`sender_wiiID`,`mail`, `recipient_id`, `mail_id`, `message_id`) VALUES (?, ?, ?, ?, ?)")
if err != nil {
log.Printf("Database error: %v", err)
c.Status(http.StatusInternalServerError)
return
}
_, err = stmt.Exec(fromAddress.Address, wiiMail, recipientMlid, uuid.New().String(), uuid.New().String())
if err != nil {
log.Printf("Database error: %v", err)
c.Status(http.StatusInternalServerError)
return
}
c.String(http.StatusOK, "thanks sendgrid")
}