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
190
191
|
#! /bin/sh
# usage: parts list
# usage: parts add PATH [NAME [DESCRIPTION]]
set -efu
list_parts() {
jq -r '
def replicate(n; x):
if n == 0 then "" else n * x end
;
def flattenMap(inner):
def addDepth(depth):
if type == "array" then
map(addDepth(depth + 1))
else
{ depth: depth, value: . }
end
;
def addIndex:
to_entries | map(.value + { index: .key })
;
def getPrefix:
match("( *).*").captures[0].string
;
def stripPrefix:
match(" *(.*)").captures[0].string
;
def shiftPrefix:
match("( *[^ ]+)( *)( .*)").captures|map(.string)|.[1]+.[0]+.[2]
;
addDepth(0) | flatten | addIndex | map(inner) | map(shiftPrefix)
;
def parseContentType:
if . != null then . else "text/plain; charset=UTF-8" end |
split(";\\s*";"") |
(.[0] | split("/")) as $compoundType |
$compoundType[0] as $type |
$compoundType[1] as $subtype |
(.[1:] | map(split("=") | { key: .[0], value: .[1] }) | from_entries)
as $params |
{
$type, $subtype, $params,
}
;
def formatPart:
.body as $body |
((.headers // []) | from_entries) as $headers |
($headers["content-type"] | parseContentType) as $ct |
"\($ct.type)/\($ct.subtype)" as $compoundType |
$ct.params.name as $name |
[ "\($compoundType) \($name | tojson) \($body | length)" ] +
if $ct.type == "multipart" then .body | map(formatPart) else [] end
;
formatPart |
flattenMap("\(replicate(.depth - 1; " "))part#\(.index + 1) \(.value)")[]
'
}
add_part() {(
filepath=$1
filename=${2-$(basename "$filepath")}
description=${3-$filename}
contentType="$(file -Lib "$filepath"); name=$filename"
case $contentType in
text/plain|text/plain\;*)
contentTransferEncoding=8bit
content() {
cat "$filepath"
}
;;
*)
contentTransferEncoding=base64
content() {
base64 "$filepath"
}
;;
esac
contentDisposition="attachment; filename=$filename"
contentDescription=$description
boundary=$(date -Ins | sha1sum | cut -d\ -f1)
{
jq '{ mail: . }'
content | jq -Rs '{ content: . }'
} |
jq -sr \
--arg boundary "$boundary" \
--arg contentType "$contentType" \
--arg contentTransferEncoding "$contentTransferEncoding" \
--arg contentDisposition "$contentDisposition" \
--arg contentDescription "$contentDescription" \
'
add |
.mail as $mail |
.content as $content |
def parseContentType:
if . == null then null else
split(";\\s*";"") |
(.[0] | split("/")) as $type |
(.[1:] | map(split("=") | { key: .[0], value: .[1] }) | from_entries)
as $parameters |
{
type: $type[0],
subtype: $type[1],
parameters: $parameters,
}
end;
($mail.headers | from_entries) as $headers |
($headers["content-type"] | if . == null then
{
type: "text",
subtype: "plain",
parameters: {
charset: "UTF-8",
},
}
else parseContentType
end)
as $ct |
def add_part:
{
headers: ({
"content-type": $contentType,
"content-transfer-encoding": $contentTransferEncoding,
"content-disposition": $contentDisposition,
"content-description": $contentDescription,
} | to_entries),
body: $content,
}
as $part |
{ headers: .headers
, body: (.body + [$part])
};
($mail.headers | from_entries) as $headers |
def is_multipart_mixed:
$headers["content-type"] // "" | test("^multipart/mixed\\b.*");
if is_multipart_mixed then
$mail | add_part
else
{ headers: (
$mail.headers |
map(select(.key != "mime-version")) |
map(select(.key != "content-type")) |
map(select(.key != "content-transfer-encoding")) |
. + ({
"mime-version": "1.0",
"Content-Type": "multipart/mixed; boundary=\"----=\($boundary)\"",
} | to_entries)
)
, body: [{
headers: ({
"content-type":
($headers["content-type"] // "text/plain; charset=UTF-8"),
"content-transfer-encoding":
($headers["content-transfer-encoding"] // "8bit"),
} | to_entries),
body: $mail.body,
}]
} |
add_part
end
'
)}
command=$1; shift
case $command in
list)
mailaid | list_parts
;;
add)
mailaid | add_part "$@" | mailaid -d
;;
*)
echo "$0: error: unknown command: $command" >&2
exit 1
;;
esac
|