-
Notifications
You must be signed in to change notification settings - Fork 5
/
mail_actions.go
149 lines (137 loc) · 4.22 KB
/
mail_actions.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
// Copyright 2017, 2022 The Agostle Authors. All rights reserved.
// Use of this source code is governed by an Apache 2.0
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"io"
"strings"
"context"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/tgulacsi/agostle/converter"
)
func mailToPdfZip(ctx context.Context, outfn, inpfn string, splitted bool, outimg string, imgsize string, pages []uint16) error {
input, err := openIn(inpfn)
if err != nil {
return err
}
defer func() { _ = input.Close() }()
splitted = splitted || len(pages) != 0
if !splitted && outimg == "" {
return converter.MailToPdfZip(ctx, outfn, input, "message/rfc822")
}
return converter.MailToSplittedPdfZip(ctx, outfn, input, "message/rfc822",
splitted, outimg, imgsize, pages)
}
func mailToTree(ctx context.Context, outdir, inpfn string) error {
input, err := openIn(inpfn)
if err != nil {
return err
}
if strings.HasSuffix(outdir, ".zip") && !isDir(outdir) {
return converter.MailToZip(ctx, outdir, input, "message/rfc822")
}
return converter.MailToTree(ctx, outdir, input)
}
func outlookToEmail(ctx context.Context, outfn, inpfn string) error {
logger := logger.With("fn", "outlookToEmail")
inp, err := openIn(inpfn)
if err != nil {
return err
}
out, err := openOut(outfn)
if err != nil {
return err
}
defer func() {
if closeErr := out.Close(); closeErr != nil {
logger.Info("Close")
if err == nil {
err = closeErr
}
}
}()
var r io.ReadCloser
r, err = converter.NewOLEStorageReader(ctx, inp)
if err != nil {
return err
}
_, err = io.Copy(out, r)
_ = r.Close()
return err
}
func init() {
inp := "-"
var out string
withOutFlag := func(name string) *flag.FlagSet {
fs := newFlagSet(name)
fs.StringVar(&out, "o", "", "output file")
return fs
}
{
var (
split bool
outimg, pageS string
imgsize = "640x640"
)
fs := withOutFlag("mail")
fs.BoolVar(&split, "split", false, "split PDF to pages")
fs.BoolVar(&converter.SaveOriginalHTML, "save-original-html", converter.SaveOriginalHTML, "save original html")
fs.StringVar(&outimg, "outimg", "", "output image format")
fs.StringVar(&imgsize, "imgsize", imgsize, "image size")
fs.StringVar(&pageS, "pages", "", "pages (comma separated)")
mailToPdfZipCmd := ffcli.Command{Name: "mail", ShortHelp: "convert mail to zip of PDFs",
ShortUsage: "mail [-split] [-outimg=image/gif] [-imgsize=640x640] mailfile.eml",
LongHelp: `reads a message/rfc822 email, converts all of it to PDF files
(including attachments), and outputs a zip file containing these pdfs,
optionally splits the PDFs to separate pages, and converts these pages to images.
Usage:
mail2pdfzip [-split] [-outimg=image/gif] [-imgsize=640x640] mailfile.eml
Examples:
mail2pdfzip -split --outimg=image/gif --imgsize=800x800 -o=/tmp/email.pdf.zip email.eml
`,
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
if len(args) != 0 {
inp = args[0]
}
pages := parseUint16s(strings.Split(pageS, ","))
if err := mailToPdfZip(ctx, out, inp, split, outimg, imgsize, pages); err != nil {
return fmt.Errorf("mailToPdfZip out=%s: %w", out, err)
}
return nil
},
}
subcommands = append(subcommands, &mailToPdfZipCmd)
}
fs := withOutFlag("mail2tree")
mailToTreeCmd := ffcli.Command{Name: "mail2tree", ShortHelp: "extract mail tree to a directory",
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
if len(args) != 0 {
inp = args[0]
}
if err := mailToTree(ctx, out, inp); err != nil {
return fmt.Errorf("mailToTree out=%s: %w", out, err)
}
return nil
},
}
subcommands = append(subcommands, &mailToTreeCmd)
fs = withOutFlag("outlook2email")
outlookToEmailCmd := ffcli.Command{Name: "outlook2email", ShortHelp: "convert outlook .msg to standard .eml",
LongHelp: "uses libemail-outlook-message-perl if installed, or docker to install && run that script",
FlagSet: fs,
Exec: func(ctx context.Context, args []string) error {
if len(args) != 0 {
inp = args[0]
}
if err := outlookToEmail(ctx, out, inp); err != nil {
return fmt.Errorf("outlookToEmail out=%s: %w", out, err)
}
return nil
},
}
subcommands = append(subcommands, &outlookToEmailCmd)
}