forked from galdor/go-mbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbox.go
187 lines (156 loc) · 4.73 KB
/
mbox.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
// Copyright (c) 2017 Nicolas Martyanoff <khaelin@gmail.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/mail"
"os"
"path/filepath"
"text/template"
"time"
"github.com/galdor/go-cmdline"
"github.com/galdor/go-mbox"
)
type msgListEntry struct {
*mbox.Message
Msg *mail.Message
RFC3339Date string
}
func main() {
cmdline := cmdline.New()
cmdline.AddCommand("help", "print help and exit")
cmdline.AddCommand("extract",
"extract messages from a mbox file to a maildir")
cmdline.AddCommand("list", "list messages in a mbox file")
cmdline.Parse(os.Args)
var cmdFn func([]string)
cmdName := cmdline.CommandName()
cmdArgs := cmdline.CommandArgumentsValues()
switch cmdName {
case "help":
cmdline.PrintUsage(os.Stdout)
os.Exit(0)
case "extract":
cmdFn = cmdExtract
case "list":
cmdFn = cmdList
}
cmdArgv0 := os.Args[0] + " " + cmdName
cmdFn(append([]string{cmdArgv0}, cmdArgs...))
}
func cmdExtract(args []string) {
cmdline := cmdline.New()
cmdline.AddArgument("path", "the mbox file")
cmdline.AddOption("f", "format", "format", "the mbox format")
cmdline.SetOptionDefault("f", "mboxrd")
cmdline.AddOption("o", "output", "directory",
"the directory to extract messages to")
cmdline.SetOptionDefault("o", ".")
cmdline.Parse(args)
formatStr := cmdline.OptionValue("format")
var format mbox.Format
if err := format.Parse(formatStr); err != nil {
die("invalid mbox format: %v", err)
}
path := cmdline.ArgumentValue("path")
dirPath := cmdline.OptionValue("output")
mbox, err := mbox.Open(path, format)
if err != nil {
die("cannot open mbox: %v", err)
}
defer mbox.Close()
newDirPath := filepath.Join(dirPath, "new")
if err := os.MkdirAll(newDirPath, 0750); err != nil {
die("cannot create %s: %v", dirPath, err)
}
tmpDirPath := filepath.Join(dirPath, "tmp")
if err := os.MkdirAll(tmpDirPath, 0750); err != nil {
die("cannot create %s: %v", dirPath, err)
}
for {
mboxMsg, err := mbox.Read()
if err != nil {
die("cannot read message: %v", err)
} else if mboxMsg == nil {
break
}
fileName := fmt.Sprintf("%s,S=%d:2,", mboxMsg.Id, len(mboxMsg.Data))
tmpPath := filepath.Join(newDirPath, fileName)
newPath := filepath.Join(newDirPath, fileName)
if err := ioutil.WriteFile(tmpPath, mboxMsg.Data, 0640); err != nil {
die("cannot write %s: %v", tmpPath, err)
}
if err := os.Rename(tmpPath, newPath); err != nil {
die("cannot rename %s to %s: %v", tmpPath, newPath, err)
}
}
}
func cmdList(args []string) {
cmdline := cmdline.New()
cmdline.AddArgument("path", "the mbox file")
cmdline.AddOption("f", "format", "format", "the mbox format")
cmdline.SetOptionDefault("f", "mboxrd")
cmdline.AddOption("t", "template", "template",
"the template used for each message line")
cmdline.SetOptionDefault("t",
`{{.Id}} {{.RFC3339Date}} {{index .Msg.Header.Subject 0}}`)
cmdline.Parse(args)
tplStr := cmdline.OptionValue("template")
tpl, err := template.New("message").Parse(tplStr)
if err != nil {
die("cannot parse template string: %v", err)
}
formatStr := cmdline.OptionValue("format")
var format mbox.Format
if err := format.Parse(formatStr); err != nil {
die("invalid mbox format: %v", err)
}
path := cmdline.ArgumentValue("path")
mbox, err := mbox.Open(path, format)
if err != nil {
die("cannot open mbox: %v", err)
}
defer mbox.Close()
for {
mboxMsg, err := mbox.Read()
if err != nil {
die("cannot read message: %v", err)
} else if mboxMsg == nil {
break
}
msg, err := mail.ReadMessage(bytes.NewReader(mboxMsg.Data))
if err != nil {
warn("cannot parse message %s: %v", mboxMsg.Id, err)
continue
}
entry := msgListEntry{
Message: mboxMsg,
Msg: msg,
RFC3339Date: mboxMsg.Date.Format(time.RFC3339),
}
if err := tpl.Execute(os.Stdout, entry); err != nil {
die("cannot execute template: %v", err)
}
fmt.Println()
}
}
func warn(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
}
func die(format string, args ...interface{}) {
warn(format, args...)
os.Exit(1)
}