-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_name.go
160 lines (128 loc) · 3.48 KB
/
message_name.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
package mboxrd
import (
"fmt"
"log"
"regexp"
"strings"
"time"
)
const timestampFormat = "060102150405"
var (
utc *time.Location
addrRE = regexp.MustCompile(`(\w{1,})["']?(@.*)`)
headRE = regexp.MustCompile(`^From (.*)(@.*)`)
dateRE = regexp.MustCompile(`(?i)^date: (.*)`)
fromRE = regexp.MustCompile(`(?i)^(from:)(.*)`)
indentRE = regexp.MustCompile(`^\s`)
)
func init() {
var err error
utc, err = time.LoadLocation("UTC")
if err != nil {
log.Fatal(err)
}
}
func TimeFromLine(line string, lc *time.Location) (string, error) {
parsedTS := dateRE.FindStringSubmatch(line)
if parsedTS == nil {
return "", fmt.Errorf("Failed to parse the timestamp from the line %q", line)
}
return TimeNorm(parsedTS[1], lc)
}
func TimeNorm(line string, loc *time.Location) (string, error) {
var l string
if li := strings.LastIndex(line, ` (`); li == -1 {
l = line
} else {
l = line[:li]
}
l = strings.TrimSpace(strings.Replace(strings.TrimSuffix(l, " UT"), `GMT`, ``, 1))
t, er := time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", l)
if er == nil {
return t.In(loc).Format(timestampFormat), nil
}
t, er = time.Parse("2 Jan 2006 15:04:05 -0700", l)
if er == nil {
return t.In(loc).Format(timestampFormat), nil
}
t, er = time.Parse("2 Jan 2006 15:04:05 MST", l)
if er == nil {
return t.In(loc).Format(timestampFormat), nil
}
t, er = time.Parse("Mon, 2 Jan 2006 15:04:05 MST", l)
if er == nil {
return t.In(loc).Format(timestampFormat), nil
}
t, er = time.Parse("Mon, 2 Jan 2006 15:04:05", l)
if er == nil {
return t.In(loc).Format(timestampFormat), nil
}
t, er = time.Parse("2006-01-02 15:04:05 -0700", l)
if er == nil {
return t.In(loc).Format(timestampFormat), nil
}
//Wed, 6 Aug 2014 09:59:18 GMT-07:00
t, er = time.Parse("Mon, 2 Jan 2006 15:04:05 Z07:00", l)
if er == nil {
return t.In(loc).Format(timestampFormat), nil
}
return "", er
}
// NameFromTimeUser returns a closed function used to extract
// a message file name based on the message timestamp and sender's
// username part of the email.
//
// It is an example on how to construct the file name from multiple
// headers.
func NameFromTimeUser(format string, errors chan error) ByLineName {
const headPrefix = "From "
var (
ts, fr, fr_acc, hd string
in_from = false
)
return func(line string, errors chan error) string {
var er error
if ts == "" && dateRE.MatchString(line) {
ts, er = TimeFromLine(line, utc)
if er != nil {
errors <- fmt.Errorf(
"Failed to parse the timestamp. Error: %s",
er.Error())
return ""
}
}
if hd == "" && strings.HasPrefix(line, headPrefix) {
parsedHead := headRE.FindStringSubmatch(line)
if parsedHead == nil {
errors <- fmt.Errorf(
"Failed to extract sender ID from the header line %q",
line)
return ""
}
hd_temp := parsedHead[1]
hd = hd_temp[len(hd_temp)-8:]
}
if fr == "" {
if parsedFrom := fromRE.FindStringSubmatch(line); parsedFrom != nil {
in_from = true
fr_acc = strings.TrimSpace(parsedFrom[2])
} else if in_from && indentRE.MatchString(line) {
fr_acc = fr_acc + strings.TrimSpace(line)
} else if in_from {
in_from = false
parsedEmail := addrRE.FindStringSubmatch(fr_acc)
if parsedEmail == nil {
errors <- fmt.Errorf(
"Failed to extract user name from the email address %q",
fr_acc)
return ""
}
fr = parsedEmail[1]
}
}
if ts == "" || fr == "" || hd == "" {
return ""
}
return fmt.Sprintf(format, ts, hd, fr)
}
}