-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_singleMail.go
118 lines (104 loc) · 3.5 KB
/
type_singleMail.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
package main
import (
"crypto/sha256"
"fmt"
"strconv"
"strings"
)
// Stores parsed information for a single e-mail.
type singleMail struct {
MailID string `json:"mailID"`
QueueID string `json:"queueID"`
Date string `json:"date"`
Time string `json:"time"`
From string `json:"from"`
HostFrom string `json:"hostFrom"`
UserFrom string `json:"userFrom"`
TypeFrom string `json:"typeFrom"`
To string `json:"to"`
HostTo string `json:"hostTo"`
UserTo string `json:"userTo"`
TypeTo string `json:"typeTo"`
Size int64 `json:"size"`
Subject string `json:"subject"`
}
// SetDate sets the Date value of a singleMail object.
// No additional parsing is done.
func (sm *singleMail) SetDate(date string) {
sm.Date = date
}
// SetTime sets the Time value of a singleMail object.
// No additional parsing is done.
func (sm *singleMail) SetTime(time string) {
sm.Time = time
}
// SetFrom sets the From value of a singleMail object.
// It also splits up the given address and populates the HostFrom and UserFrom values.
func (sm *singleMail) SetFrom(from string) {
sm.From = from
fromParts := strings.Split(from, `@`)
sm.HostFrom = fromParts[1]
sm.UserFrom = fromParts[0]
sm.TypeFrom = sm.GetHostType(sm.HostFrom)
}
// SetTo sets the To value of a singleMail object.
// It also splits up the given address and populates the HostTo and UserTo values.
func (sm *singleMail) SetTo(to string) {
sm.To = to
toParts := strings.Split(to, `@`)
sm.HostTo = toParts[1]
sm.UserTo = toParts[0]
sm.TypeTo = sm.GetHostType(sm.HostTo)
}
// SetSubject sets the Subject value of a singleMail object.
// No additional parsing is done.
func (sm *singleMail) SetSubject(subject string) {
sm.Subject = subject
}
// SetSize sets the Size value of a singleMail object.
// No additional parsing - apart from converting the given string into an int - is done.
func (sm *singleMail) SetSize(size string) {
mailSize, mailSizeErr := strconv.Atoi(size)
if mailSizeErr != nil {
sm.Size = -1
}
sm.Size = int64(mailSize)
}
// SetQueueID sets the QueueID value of a singleMail object.
// No additional parsing is done.
func (sm *singleMail) SetQueueID(queueID string) {
sm.QueueID = queueID
}
// GenerateMailID computes and sets the MailID value of a singleMail object.
// The MailID is generated by sha256'ing a string consisting of the QueueID, Date, Time, From and To values. The values are seperated by spaces.
func (sm *singleMail) GenerateMailID() {
idString := fmt.Sprintf("%s %s %s %s %s", sm.QueueID, sm.Date, sm.Time, sm.From, sm.To)
mailID := sha256.Sum256([]byte(idString))
sm.MailID = fmt.Sprintf("%x", mailID)
}
// GetPartnerKey returns the partner key of a singleMail object.
// The partner key is generated by sorting the host parts. If the host parts are equal, it is generated by sorting the full addresses.
func (sm *singleMail) GetPartnerKey() string {
commPartnerA := sm.From
commPartnerB := sm.To
if sm.HostFrom == sm.HostTo {
if sm.From > sm.To {
commPartnerA = sm.To
commPartnerB = sm.From
}
} else if sm.HostFrom > sm.HostTo {
commPartnerA = sm.To
commPartnerB = sm.From
}
return fmt.Sprintf("%s %s", commPartnerA, commPartnerB)
}
// GetHostType returns the type of a given host, either "internal" or "external".
// Internal hosts are defined by providing the matching CLI argument; every other host is considered as external.
func (sm *singleMail) GetHostType(host string) string {
for _, intHost := range config.InternalHosts {
if intHost == host {
return "internal"
}
}
return "external"
}