| 1 |
package mboxer |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
func createMailboxContainer(path string) (*mailboxContainer, error) { |
| 11 |
file, err := os.Open(path) |
| 12 |
if err != nil { |
| 13 |
return nil, err |
| 14 |
} |
| 15 |
|
| 16 |
defer file.Close() |
| 17 |
|
| 18 |
fileInfo, err := file.Stat() |
| 19 |
if err != nil { |
| 20 |
return nil, err |
| 21 |
} |
| 22 |
|
| 23 |
mbdata := &mailboxContainer{mailboxes: make(map[string]*container)} |
| 24 |
|
| 25 |
openMboxFile := func(path string, r io.Reader) error { |
| 26 |
// read mbox file |
| 27 |
messages, err := Read(r) |
| 28 |
if err != nil { |
| 29 |
return err |
| 30 |
} |
| 31 |
_, name := filepath.Split(path) |
| 32 |
name = strings.TrimSuffix(name, ".mbox") |
| 33 |
mbdata.mailboxes[name] = &container{filename: path, messages: messages} |
| 34 |
return nil |
| 35 |
} |
| 36 |
|
| 37 |
if fileInfo.IsDir() { |
| 38 |
files, err := filepath.Glob(filepath.Join(path, "*.mbox")) |
| 39 |
if err != nil { |
| 40 |
return nil, err |
| 41 |
} |
| 42 |
for _, file := range files { |
| 43 |
f, err := os.Open(file) |
| 44 |
if err != nil { |
| 45 |
continue |
| 46 |
} |
| 47 |
if err := openMboxFile(file, f); err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
f.Close() |
| 51 |
} |
| 52 |
} else { |
| 53 |
if err := openMboxFile(path, file); err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return mbdata, nil |
| 59 |
} |