-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
114 lines (97 loc) · 2.66 KB
/
json.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
package mailgun
import (
"regexp"
)
type Config struct {
ApiKey string
Domain string
Eu bool
}
type EventsResponse struct {
Items []Item `json:"items"`
Paging Paging `json:"paging"`
}
type Item struct {
Storage Storage `json:"storage"`
Severity string `json:"severity"`
DeliveryStatus DeliveryStatus `json:"delivery-status"`
RecipientDomain string `json:"recipient-domain"`
Reason string `json:"reason"`
Flags Flags `json:"flags"`
Timestamp float64 `json:"timestamp"`
Envelope Envelope `json:"envelope"`
Message Message `json:"message"`
Recipient string `json:"recipient"`
Event string `json:"event"`
}
type Storage struct {
Key string `json:"key"`
}
type DeliveryStatus struct {
Tls bool `json:"tls"`
MxHost string `json:"mx-host"`
Code int `json:"code"`
Description string `json:"description"`
AttemptNo int `json:"attempt-no"`
Message string `json:"message"`
CertificateVerified bool `json:"certificate-verified"`
}
type Flags struct {
IsRouted bool `json:"is-routed"`
IsAuthenticated bool `json:"is-authenticated"`
IsSystemTest bool `json:"is-system-test"`
IsTestMode bool `json:"is-test-mode"`
}
type Envelope struct {
Transport string `json:"transport"`
Sender string `json:"sender"`
SendingIp string `json:"sending-ip"`
Targets string `json:"targets"`
}
type Message struct {
Headers Headers `json:"headers"`
Size int `json:"size"`
}
type Headers struct {
To string `json:"to"`
MessageId string `json:"message-id"`
From string `json:"from"`
Subject string `json:"subject"`
}
type Paging struct {
Previous string `json:"previous"`
First string `json:"first"`
Last string `json:"last"`
Next string `json:"next"`
}
type Email struct {
From string `json:"From"`
To string `json:"To"`
Subject string `json:"Subject"`
Text string `json:"body-plain"`
References string `json:"References"`
MessageId string `json:"Message-Id"`
InReplyTo string `json:"In-Reply-To"`
Cc string
Bcc string
}
var emailRegexp = regexp.MustCompile(`<[^<>]*>`)
func (i *Item) From() string {
if len(i.Envelope.Sender) > 0 {
return i.Envelope.Sender
}
return findEmail(i.Message.Headers.From)
}
func (i *Item) To() string {
if len(i.Envelope.Targets) > 0 {
return i.Envelope.Targets
}
return findEmail(i.Message.Headers.To)
}
func findEmail(header string) string {
a := emailRegexp.FindString(header)
if len(a) < 2 {
return ""
}
return a[1 : len(a)-1]
}