-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab-attachments.sh
executable file
·353 lines (299 loc) · 11.9 KB
/
grab-attachments.sh
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/bin/bash
###################################################################################################
# !!!! Create a .grabrc in your home directory. For example:
#
# USER="me@gmail.com:mypassword"
# MSGFOLDER="MyFolder"
# FROM=someone@gmail.com
# DOWNLOAD_DIR="$HOME/Downloads/Grab"
# DOWNLOAD_DIR_TMP="$HOME/Downloads/GrabTmp"
#
# This script searches for messages from in the "Inbox" folder. For each matching message, it
# downloads it's attachments (if any). After downloading the attachments for all matching
# messages, the messages are moved to the DOWNLOAD_DIR folder. The messages are moved regardless
# of whether any attachments are found.
###################################################################################################
#
# Definitions
#
# This variable should be set to your gmail username and password. If you have multi-factor authentication
# enabled for your gmail/google account then you will need to generate an application specific password
# for use by this script. If you do not have multi-factor authentication enabled, then you will need
# to turn on access for less secure apps in your google account.
USER="USERNAME:PASSWORD"
# The gmail folder where the matching messages should be moved after the attachments are downloaded.
# This folder needs to exist in your gmail account.
MSGFOLDER="Folder"
# "From" email address
FROM=myaddress@gmail.com
# These variables determine the directories where attachments are downloaded.
DOWNLOAD_DIR="$HOME/Downloads/GrabMedia"
DOWNLOAD_DIR_TMP="$HOME/Downloads/GrabMediaTmp"
GMAIL="imaps://imap.gmail.com:993/"
INBOX="${GMAIL}INBOX"
# !!!! Create a .grabrc in your home directory.
if [ -f ~/.grabrc ] ; then
source ~/.grabrc
fi
#
# Define functions
#
ansi() { echo -e "\033[${1}m${*:2}\033[0m"; }
ansiStart() { echo -e "\033[${1}m${*:2}"; }
ansiEnd() { echo -e "${*:1}\033[0m"; }
ansi2() { echo -e "\033[${1};${2}m${*:3}\033[0m"; }
ansi2Start() { echo -e "\033[${1};${2}m${*:3}"; }
8bit() { echo -e "\033[38;5;${1}m${*:2}\033[0m"; }
8bit2() { echo -e "\033[${1};38;5;${2}m${*:3}\033[0m"; }
bold() { ansi 1 "$@"; }
italic() { ansi 3 "$@"; }
underline() { ansi 4 "$@"; }
strikethrough() { ansi 9 "$@"; }
brightgreen() { ansi 92 "$@"; }
red() { 8bit2 1 9 "$@"; }
boldbrightgreen() { ansi2 1 92 "$@"; }
boldbrightcyan() { ansi2 1 96 "$@"; }
highlight() { ansi2 1 92 "$@"; }
startHighlight() { ansi2Start 1 92 "$@"; }
endHighlight() { ansiEnd "$@"; }
join_by() {
local IFS="$1"
shift
echo "$*"
}
trim() {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
echo -n "$var"
}
# Immediately exit if any error is encountered
set -e
# Wget and Mpack are installed in /usr/local/bin
export PATH="/usr/local/bin:$PATH"
#
# Process messages
#
# Ensure Homebrew, Wget and Mpack are installed
set +e
(hash brew 2>/dev/null)
HAS_BREW=$?
(hash wget 2>/dev/null)
HAS_WGET=$?
(hash mpack 2>/dev/null)
HAS_MPACK=$?
set -e
if [ $HAS_BREW != 0 -o $HAS_MPACK != 0 -o $HAS_WGET != 0 ] ; then
echo $(highlight "This script requires 'Homebrew' (The missing package manager for macOS), 'Wget' (for downloading files")
echo $(highlight "from the internet) and 'Mpack' (MIME mail packing and unpacking).")
echo ""
echo $(highlight "The Administrator password may be requested by the 'Homebrew', 'Wget' and 'Mpack' installers.")
echo ""
echo "Press RETURN or SPACEBAR to proceed with installation of 'Homebrew', 'Wget' and 'Mpack'."
read -rsn1 -p"Press any other key to abort: " RESULT;echo
if [ "$RESULT" != "" ] ; then
exit 0
fi
hash brew 2>/dev/null || { echo "Installing 'Homebrew'" ; /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ; }
hash wget 2>/dev/null || { echo "" ; echo "Installing 'Wget'" ; brew install wget ; }
hash mpack 2>/dev/null || { echo "" ; echo "Installing 'Mpack'" ; brew install mpack ; }
echo ""
fi
mkdir -p "$DOWNLOAD_DIR"
mkdir -p "$DOWNLOAD_DIR_TMP"
cd "$DOWNLOAD_DIR_TMP"
finish() {
rmdir "$DOWNLOAD_DIR_TMP"
}
trap finish EXIT
# Abort the script if there are leftover files.
# Commenting out because it's ok to overwrite the leftovers.
# SUFFIXES="eml EML desc DESC"
# shopt -s nullglob
# for s in $SUFFIXES ; do
# if [ -n "$(trim *.$s)" ] ; then
# echo Error: found files with \"$s\" suffix >&2
# exit 1
# fi
# done
# shopt -u nullglob
# Example: ask about capabilities
# curl --user "$USER" --url "$GMAIL" --request 'CAPABILITY'
# Example: list all folders
# curl --user "$USER" --url "$GMAIL" --request 'LIST "" "*"'
# Example: search all UIDs
# curl --user "$USER" --url "$INBOX" --request 'UID SEARCH ALL'
# Example: fetch header fields from first message
# curl --progress-bar --user "$USER" --url "${INBOX}/;UID=1/;SECTION=HEADER.FIELDS%20(DATE%20FROM%20TO%20SUBJECT)" --output "TEST.eml"
# Example: fetch header fields from first message using a request
# Note: this does not produce any header output because of a known curl issue
# curl --progress-bar --user "$USER" --url "$INBOX" --request "FETCH 1 BODY[HEADER.FIELDS (DATE FROM TO SUBJECT)]" --output "TEST.eml"
# Example: fetch first message
# curl --progress-bar --user "$USER" --url "${INBOX};UID=1" --output "TEST.eml"
# Example: move a list of message ids
# curl --user "$USER" --url "$INBOX" --request "MOVE 39001,39151 Test"
# Example: move a list of unique ids
# curl --user "$USER" --url "$INBOX" --request "UID MOVE 39001,39151 Test"
echo $(highlight "Searching for messages from \"$FROM\"...")
set +e
MSGIDS=$(curl --no-verbose --progress-bar --user "$USER" --url "$INBOX" --request "SEARCH FROM $FROM")
MSGSTATUS=$?
set -e
if [ $MSGSTATUS -eq 67 ] ; then
echo $(highlight "")
echo $(red "Error: gmail login failed for \"$USER\"")
echo $(highlight "Check to make sure your username and password are correct.")
echo $(highlight "")
echo $(highlight "If you have multi-factor authentication enabled for your gmail account")
echo $(highlight "then you will need to generate an application specific password for use")
echo $(highlight "by this script.")
echo $(highlight "")
echo $(highlight "If you do not have multi-factor authentication enabled then you have")
echo $(highlight "likely received an email message stating that a sign-in attempt was")
echo $(highlight "blocked for your google account. To use this script you will need to")
echo $(highlight "turn on access for less secure apps in your google account settings here:")
echo $(highlight "")
echo $(boldbrightcyan "https://myaccount.google.com/lesssecureapps")
fi
if [ $MSGSTATUS -ne 0 ] ; then
exit $MSGSTATUS
fi
MSGIDS=$(echo "$MSGIDS" | cut -f3- -d" ")
MSGIDS=$(trim "$MSGIDS")
MSGSEQUENCE=$(join_by , $MSGIDS)
SUCCESS_IDS=""
if [ -n "$MSGIDS" ] ; then
echo $(highlight "Message IDs: $MSGIDS")
else
echo $(highlight "No messages from $FROM found")
exit 0
fi
# I would prefer to use unique ids, but curl does not support fetching messages using unique ids
# UNIQUEIDS=$(curl --no-verbose --progress-bar --user "$USER" --url "$INBOX" --request "UID SEARCH FROM $FROM" | cut -f3- -d" ")
# UNIQUEIDS=$(trim "$UNIQUEIDS")
for id in $MSGIDS ; do
ERROR_ID=0
MSG="Message${id}.eml"
echo " "
echo $(startHighlight "Message ${id}...")
curl --url "$INBOX;UID=${id}" --no-verbose --progress-bar --user "$USER" --output "$MSG"
SUBJECT=$(grep -m 1 "^Subject: " "$MSG" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')
echo $(endHighlight "${SUBJECT}")
grep -m 1 "^From: " "$MSG" | while read line ; do
FROM_HEADER=$(echo "$line" | cut -d":" -f2 | awk -F '[<>]' '{print $2}')
FROM_HEADER=$(trim $FROM_HEADER)
if [ "$FROM_HEADER" != "$FROM" ] ; then
echo $(red "Mismatched \"From\" line: \"$FROM_HEADER\" vs \"$FROM\"")
echo $(red "One or more Inbox mail messages may have been deleted during script execution.")
echo $(red "Deleting or moving messages from the Inbox will cause the script to fail due")
echo $(red "to a limitation with IMAP mail ids and \"curl\".")
rm -f "$MSG"
fi
done
echo Extract attachments...
munpack -f "$MSG" 2>&1 | grep -v "tempdesc.txt: File exists"
rm -f *.desc
set +e
grep "^Image-Archive-Url: " "$MSG" | while read line ; do
URL=$(echo "$line" | cut -d';' -f1 | cut -d' ' -f2)
URL=$(trim $URL)
FILE=Images.zip
wget "$URL" --no-verbose --show-progress --output-document="$FILE"
# curl --no-verbose --progress-bar --output "$FILE" "$URL"
STATUS=$?
if [ $STATUS -eq 0 ] ; then
echo "$FILE"
unzip -o "$FILE"
fi
rm -f "$FILE"
exit $STATUS
done
STATUS=${PIPESTATUS[1]}
if [ $STATUS -ne 0 ] ; then
ERROR_ID=$STATUS
fi
set -e
set +e
grep "^Remote-Attachment-Url: " "$MSG" | while read line ; do
URL=$(echo "$line" | cut -d';' -f1 | cut -d' ' -f2)
FILE=$(echo "$line" | cut -d';' -f2 | cut -d'=' -f2)
wget "$URL" --no-verbose --show-progress --output-document="$FILE"
# curl --no-verbose --progress-bar --output "$FILE" "$URL"
STATUS=$?
if [ $STATUS -eq 0 ] ; then
echo "$FILE"
case "$FILE" in
*.zip)
unzip -o "$FILE"
rm -f "$FILE"
;;
*.ZIP)
unzip -o "$FILE"
rm -f "$FILE"
;;
esac
fi
exit $STATUS
done
STATUS=${PIPESTATUS[1]}
if [ $STATUS -ne 0 ] ; then
ERROR_ID=$STATUS
fi
set -e
rm -f "$MSG"
if [ $ERROR_ID -eq 0 ] ; then
if [ -n "$SUCCESS_IDS" ] ; then
SUCCESS_IDS="$SUCCESS_IDS $id"
else
SUCCESS_IDS="$id"
fi
fi
done
MSGIDS_AFTER=$(curl --no-verbose --progress-bar --user "$USER" --url "$INBOX" --request "SEARCH FROM $FROM" | cut -f3- -d" ")
MSGIDS_AFTER=$(trim "$MSGIDS")
MSGSEQUENCE_AFTER=$(join_by , $MSGIDS)
if [[ "$MSGSEQUENCE_AFTER" != "$MSGSEQUENCE"* ]] ; then
echo $(red "Unable to move email messages to \"$MSGFOLDER\" folder due to mismatched")
echo $(red "message ids (before \"$MSGSEQUENCE_AFTER\" vs after \"$MSGSEQUENCE\")")
echo $(red "")
echo $(red "One or more Inbox mail messages may have been deleted during script")
echo $(red "execution. Deleting or moving any messages from the Inbox will cause")
echo $(red "the script to fail due to a limitation with IMAP mail ids and \"curl\".")
exit 1
fi
SUCCESS_SEQUENCE=$(join_by , $SUCCESS_IDS)
set +e
if [ -n "$SUCCESS_SEQUENCE" ] ; then
echo -n Move email messages from \"$FROM\" to folder \"$MSGFOLDER\"...
curl --user "$USER" --url "$INBOX" --request "MOVE $SUCCESS_SEQUENCE $MSGFOLDER"
if [ $? != 0 ] ; then
echo $(red "Move email messages failed, likely because the email folder \"$MSGFOLDER\" does not exist.")
else
echo " done"
fi
fi
set -e
shopt -s nullglob
DOWNLOAD_FILES=$(trim "$DOWNLOAD_DIR_TMP"/*)
if [ -n "$DOWNLOAD_FILES" ] ; then
echo -n Move attachments to \"$DOWNLOAD_DIR\" folder...
mv -f "$DOWNLOAD_DIR_TMP"/* "$DOWNLOAD_DIR"
echo " done"
fi
if [ -n "$AUTOMATOR_WORKAROUND" ] ; then
DDFILES=$(trim "$DOWNLOAD_DIR/"*)
while [ -n "$DDFILES" ] ; do
echo -n $(highlight "Press any key to try importing $(basename $DOWNLOAD_DIR) files to Photos again: ")
read line
DDFILES=$(trim "$DOWNLOAD_DIR/"*)
if [ -n "$DDFILES" ] ; then
mv -f "$DOWNLOAD_DIR"/* "$DOWNLOAD_DIR_TMP"
sleep 3
mv -f "$DOWNLOAD_DIR_TMP"/* "$DOWNLOAD_DIR"
fi
done
fi
shopt -u nullglob
echo $(highlight "Done!")