-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_original.go
189 lines (161 loc) · 4.07 KB
/
message_original.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
package mboxrd
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
)
type (
ByLineAdmit func(string, chan error) bool
ByLineName func(string, chan error) string
)
func sameFileContent(fA, fB string) (bool, error) {
inA, errA := os.Open(fA)
if errA != nil {
return false, errA
}
defer inA.Close()
scanA := bufio.NewScanner(inA)
scanA.Split(bufio.ScanLines)
inB, errB := os.Open(fB)
if errB != nil {
return false, errB
}
defer inB.Close()
scanB := bufio.NewScanner(inB)
scanB.Split(bufio.ScanLines)
for a, b := scanA.Scan(), scanB.Scan(); a && b; a, b = scanA.Scan(), scanB.Scan() {
if scanA.Text() != scanB.Text() {
return false, nil
}
}
if (scanA.Err() == nil) && (scanB.Err() == nil) {
return true, nil
}
if errA = scanA.Err(); errA != nil {
return false, errA
}
return false, scanB.Err()
}
// WriteOriginal receives a message text from the `message` channel
// and writes it into a file in the destination `dir` directory.
//
// All error are posted in the `error` parameter channel.
//
// An `admit` parameter allows to determine if the message is left
// in the target directory. The function is called for each line
// in the message, uncluding headers. The value returned by the
// `admit` function determines if the message is kept in the
// directory.
//
// The message file name is constructed by the `name` parameter
// function. The function is called for each line in the
// message, uncluding headers, until it returns a non-empty
// string. If `name` parameter is `nill` then messages will stay
// in randomly named temporary files starting with `_msg_` prefix
//
// The `WaitGroup` parameter must be properly initialised and
// incremented prior to calling this function, or be supplied as
// `nil` if not needed.
func WriteOriginal(
message chan string,
emlName chan string,
errors chan error,
dir string,
admit ByLineAdmit,
name ByLineName,
wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
var (
msgFile string
allowed = true
)
tempFile, err := ioutil.TempFile(dir, "_msg_")
if err != nil {
errors <- err
return
}
for line := range message {
allowed = admit(line, errors)
tempFile.WriteString(line + crlf)
if name != nil && msgFile == "" {
msgFile = name(line, errors)
}
}
if !allowed {
defer func() {
if err := tempFile.Close(); err != nil {
errors <- MessageError(
fmt.Sprintf(
"Problem while closing the %s temporary file: %s",
tempFile.Name(),
err.Error()))
}
if err := os.Remove(tempFile.Name()); err != nil {
errors <- MessageError(
fmt.Sprintf(
"Problem while deleting the %s temporary file: %s",
tempFile.Name(),
err.Error()))
}
}()
return
}
tempFileEml := filepath.Base(tempFile.Name()) + ".eml"
if name != nil && msgFile == "" {
msgFile = tempFileEml
errors <- MessageError(
fmt.Sprintf(
"File name did not constuct, moving message to the %q file",
msgFile))
}
msgPath := filepath.Join(dir, msgFile)
_, err = os.Stat(msgPath)
if err == nil {
if ok, err := sameFileContent(msgFile, tempFileEml); ok && (err == nil) {
if err := os.Remove(tempFileEml); err != nil {
errors <- MessageError(
fmt.Sprintf(
"Problem while deleting the %s temporary file: %s",
tempFileEml,
err.Error()))
}
return
}
if msgFile != tempFileEml {
msgFile = tempFileEml
errors <- MessageError(
fmt.Sprintf(
"The message file %q already exists, moving message to the %q file",
msgPath,
msgFile))
msgPath = filepath.Join(dir, msgFile)
_, err = os.Stat(msgPath)
}
if err == nil {
errors <- MessageError(
fmt.Sprintf(
"The message file %q already exists, the message left in the %q file",
msgPath,
tempFile.Name()))
emlName <- tempFile.Name()
return
}
}
tempFile.Close()
err = os.Rename(tempFile.Name(), msgPath)
if err != nil {
errors <- MessageError(
fmt.Sprintf(
"Problem renaming %q into %q, the file may have either of the names. Error: %s",
tempFile.Name(),
msgPath,
err.Error()))
return
}
emlName <- msgPath
}