Auto convert the base64 code in Abook

Abook is a text-based addressbook program designed to use with mutt mail client.

There is a chapter about using abook with mutt in the ArchWiki.

If you want to use Abook instead of aliases, remove the aliases configuration in .muttrc and add this:

1
2
3
set query_command= "abook --mutt-query '%s'"
macro index,pager a "<pipe-message>abook --add-email-quiet<return>" "Add this sender to Abook"
bind editor <Tab> complete-query

But sadly, the command abook --add-email-quiet always gets names like “=?utf-8?B?55m+5ZCI5LuZ5a2Q?=” which is base64 encoded.

To convert the base64 string and automatically fit the correct encoding, you can use a shell function:

1
2
3
conv() {
eval `echo $1 | awk -F '?' '{ print "echo " $4 " | base64 -d | iconv -f " $2 }'`
}

Then:

1
$ conv name==?utf-8?B?55m+5ZCI5LuZ5a2Q?=

Or a python script like this:

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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import email
import email.header
import sys
import re
def abookdecode(origin_name):
name=email.Header.decode_header(origin_name)
if name[0][1] is None:
return name[0][0]
else:
return name[0][0].decode(name[0][1]).encode('utf-8')
if '__main__'==__name__:
infile=open(sys.argv[1],"r")
content=infile.readlines()
infile.close()
outfile=open(sys.argv[1],"wb")
for line in content:
if line[0:4]=='name':
outfile.write(line[0:5]+abookdecode(line[5:-1])+'\n')
else:
outfile.write(line)
outfile.close()

And then change your .muttrc to call the script:

1
macro index,pager a "<pipe-message>abook --add-email-quiet && abook-decode ~/.abook/addressbook<return>" "Add this sender to Abook"

Now the addressbook will be written correctly.