-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcreate-alias.sh
executable file
·46 lines (38 loc) · 1.55 KB
/
create-alias.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
#!/bin/bash
###############################################################################
# Read email from stdin and identify sender address. Create an alias for that #
# address in the format mutt expects. Then, if no alias for that address #
# exists yet, append it to the alias file. Also handles duplicate nicknames. #
# Inspired by this: http://wcaleb.org/blog/mutt-tips #
###############################################################################
shopt -s extglob
alias_file=~/.config/mutt/aliases
function create_alias(){
local line words nickname count
while read -ra line; do
# find line with "From:" filed and check if it ends with an email
if [[ ${line[0]} == From: && ${line[-1]} == *@*.* ]]; then
# remove punctuation and email suffix
words=("${line[@]//?(@*.*|[[:punct:]])}")
case ${#line[@]} in
1) ;;
2|3) nickname=${words[1],,} ;;
*) nickname=${words[-2],,}-${words[1],,} ;;
esac
mapfile
break
fi
done
# check if nickname is set and if the email is not already present
if [[ -v nickname ]] && ! grep -Fqs -- "${line[-1]}" $alias_file; then
count=$(grep -cs "^alias $nickname\(-[[:digit:]]\+\)\? " $alias_file)
# if nickname already exist append count as suffix
if [[ -z $count || $count == 0 ]]; then
printf "%s\n" "alias $nickname ${line[*]:1}" >> $alias_file
else
printf "%s\n" "alias $nickname-$count ${line[*]:1}" >> $alias_file
fi
fi
}
# pass stdin to create_alias and stdout
tee >(create_alias)