-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmessage.go
188 lines (150 loc) · 4.49 KB
/
message.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
package smtpmock
import "sync"
// Structure for storing the result of SMTP client-server interaction. Context-included
// commands should be represented as request/response structure fields
type Message struct {
heloRequest, heloResponse string
mailfromRequest, mailfromResponse string
rcpttoRequestResponse [][]string
dataRequest, dataResponse string
msgRequest, msgResponse string
rsetRequest, rsetResponse string
helo, mailfrom, rcptto, data, msg, rset, noop, quitSent bool
}
// message methods
// message getters
// Getter for heloRequest field
func (message Message) HeloRequest() string {
return message.heloRequest
}
// Getter for heloResponse field
func (message Message) HeloResponse() string {
return message.heloResponse
}
// Getter for helo field
func (message Message) Helo() bool {
return message.helo
}
// Getter for mailfromRequest field
func (message Message) MailfromRequest() string {
return message.mailfromRequest
}
// Getter for mailfromResponse field
func (message Message) MailfromResponse() string {
return message.mailfromResponse
}
// Getter for mailfrom field
func (message Message) Mailfrom() bool {
return message.mailfrom
}
// Getter for rcpttoRequestResponse field
func (message Message) RcpttoRequestResponse() [][]string {
return message.rcpttoRequestResponse
}
// Getter for rcptto field
func (message Message) Rcptto() bool {
return message.rcptto
}
// Getter for dataRequest field
func (message Message) DataRequest() string {
return message.dataRequest
}
// Getter for dataResponse field
func (message Message) DataResponse() string {
return message.dataResponse
}
// Getter for data field
func (message Message) Data() bool {
return message.data
}
// Getter for msgRequest field
func (message Message) MsgRequest() string {
return message.msgRequest
}
// Getter for msgResponse field
func (message Message) MsgResponse() string {
return message.msgResponse
}
// Getter for msg field
func (message Message) Msg() bool {
return message.msg
}
// Getter for rsetRequest field
func (message Message) RsetRequest() string {
return message.rsetRequest
}
// Getter for rsetResponse field
func (message Message) RsetResponse() string {
return message.rsetResponse
}
// Getter for rset field
func (message Message) Rset() bool {
return message.rset
}
// Getter for noop field
func (message Message) Noop() bool {
return message.noop
}
// Getter for quitSent field
func (message Message) QuitSent() bool {
return message.quitSent
}
// Getter for message consistency status predicate. Returns true
// for case when message struct is consistent. It means that
// MAILFROM, RCPTTO, DATA commands and message context
// were successful. Otherwise returns false
func (message Message) IsConsistent() bool {
return message.mailfrom && message.rcptto && message.data && message.msg
}
// Message RCPTTO successful response predicate. Returns true when at least one
// successful RCPTTO response exists. Otherwise returns false
func (message *Message) isIncludesSuccessfulRcpttoResponse(targetSuccessfulResponse string) bool {
for _, slice := range message.rcpttoRequestResponse {
if slice[1] == targetSuccessfulResponse {
return true
}
}
return false
}
// Pointer to empty message
var zeroMessage = &Message{}
// Concurrent type that can be safely shared between goroutines
type messages struct {
sync.RWMutex
items []*Message
}
// messages methods
// Adds new message pointer into concurrent messages slice
func (messages *messages) append(item *Message) {
messages.Lock()
defer messages.Unlock()
messages.items = append(messages.items, item)
}
// Returns a copy of all messages
func (messages *messages) copy() []Message {
messages.RLock()
defer messages.RUnlock()
return messages.copyInternal()
}
// Copy messages without a lock
func (messages *messages) copyInternal() []Message {
copiedMessages := []Message{}
for index := range messages.items {
copiedMessages = append(copiedMessages, *messages.items[index])
}
return copiedMessages
}
// Returns all messages and removes them at the same time
func (messages *messages) purge() []Message {
messages.Lock()
defer messages.Unlock()
copiedMessages := messages.copyInternal()
messages.items = nil
return copiedMessages
}
// Clears the messages slice
func (messages *messages) clear() {
messages.Lock()
defer messages.Unlock()
messages.items = nil
}