-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_all_copies
executable file
·56 lines (48 loc) · 2.96 KB
/
find_all_copies
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
#!/bin/sh
#
# find_all_copies.sh - Find all duplicate copies of a messages within a single IMAP account in Claws Mail
#
# Current Requirements:
#
# - An IMAP account
# - The IMAP account's folders must be synchronized for offline use in Claws Mail
#
# Reference:
#
# - <https://www.claws-mail.org/faq/index.php/Actions>
# - <https://www.claws-mail.org/faq/index.php/Using_Claws_Mail_with_other_programs>
DEBUG=false
# we only accept a single parameter: the selected message's full path
$DEBUG && printf "DEBUG: \$1 = '%s'\n" "$1"
[ $# -ne 1 ] && echo "ERROR! Incorrect number of arguments." && exit 2
# parse the Message-ID header from the selected message file
message_id_header="$(grep -Ei "^Message-ID:" "$1" | head -n 1)"
$DEBUG && printf "DEBUG: message_id_header = '%s'\n" "$message_id_header"
[ -z "$message_id_header" ] && echo "ERROR! Couldn't find 'Message-ID' header." && exit 1
message_id_header_regex="$(echo "${message_id_header}" | sed -E "s/\./\\\./g")"
$DEBUG && printf "DEBUG: message_id_header_regex = '%s'\n" "$message_id_header_regex"
[ -z "$message_id_header_regex" ] && echo "ERROR! Couldn't escape 'Message-ID' header for regex." && exit 1
# find the parent IMAP account path to be searched
#
# path format should be: ~/.claws_mail/imapcache/<imap_server><email_address>/
message_relative_path="${1#*.claws-mail/imapcache/*/*/}"
$DEBUG && printf "DEBUG: message_relative_path = '%s'\n" "$message_relative_path"
[ -z "$message_relative_path" ] && echo "ERROR! Couldn't parse relative path from message full path." && exit 1
imap_account_path="${1%%"${message_relative_path}"}"
$DEBUG && printf "DEBUG: imap_account_path = '%s'\n" "$imap_account_path"
[ -z "$imap_account_path" ] && echo "ERROR! Couldn't parse IMAP account path from message full path." && exit 1
# search parent IMAP account for all messages with the Message-ID
$DEBUG && printf "\nSearching '%s' for messages with header '%s'...\n\n" "${imap_account_path}" "${message_id_header}"
#grep -iRl "$message_id_header" "$imap_account_path" | while read -r message_match_path ; do
find "$imap_account_path" -type f ! -name ".*" -print0 | xargs -0 grep -EiRs -m 1 "^Message-ID: (.+)$" | grep -Ei "^(.+):${message_id_header_regex}$" | while read -r message_match_path ; do
# parse the IMAP account name from the matched message full path
imap_account="${message_match_path#*.claws-mail/imapcache/*/}"
imap_account="${imap_account%%/*}"
[ -z "$imap_account" ] && echo "ERROR! Couldn't parse IMAP account from matching message full path." && exit 1
# parse the IMAP folder relative path from the matched message full path
imap_folder="${message_match_path#*.claws-mail/imapcache/*/*/}"
imap_folder="$(dirname "${imap_folder}")"
[ -z "$imap_folder" ] && echo "ERROR! Couldn't parse IMAP folder from matching message full path." && exit 1
# output the matching IMAP folder relative path in Claws Mail mailbox path format ("#<mailbox_type>/<mailbox_name>/<folder_path>")
printf "%s\n" "#imap/${imap_account}/${imap_folder}"
done