'This method will choose a from address from the available
profiles, using the given config and lists of addresses as
a guideline. An address is chosen by assigning each potential
from address a cumulative score, where scores express roughly
the following preferences.
1. If one of the profiles\' e-mail addresses is present in the
headers, prefer that so replies come from the address they
were sent to.
2. Else, if we have a preferred profile for communicating
with a given contact, use that.
3. Else, if any of the profiles lists one of the addresses
or their domains as being "in scope", use that.
4. Else, try and match on domain names.
5. Finally, use the global default or pick a profile at random.
>>> vcs = VCardStore(cfg, \'/tmp\')
>>> vcs.add_vcards(MailpileVCard(VCardLine(\'FN:Evil Dude\'),
...                              VCardLine(\'EMAIL:d@evil.com\'),
...                              VCardLine(\'KIND:profile\')),
...                MailpileVCard(VCardLine(\'FN:Guy\'),
...                              VCardLine(\'EMAIL;TYPE=PREF:g@f.com\'),
...                              VCardLine(\'EMAIL:ok@foo.com\'),
...                              VCardLine(\'X-MAILPILE-PROFILE-SCOPE;zzz\'),
...                              VCardLine(\'X-MAILPILE-PROFILE-SCOPE;\'
...                                        \'address=ok@foo.com:x.y\'),
...                              VCardLine(\'KIND:profile\')),
...                MailpileVCard(VCardLine(\'FN:Icelander\'),
...                              VCardLine(\'EMAIL:x@bla.is\'),
...                              VCardLine(\'KIND:individual\')))
>>> c41 = AddressInfo(u\'dude@evil.com\', \'Dude\')
>>> c42 = AddressInfo(u\'dude@f.com\', \'Other dude\')
>>> c31 = AddressInfo(u\'d@x.y\', \'D at X dot Y\')
>>> c32 = AddressInfo(u\'d@zzz\', \'D at ZZZ\')
>>> c21 = AddressInfo(u\'x@bla.is\', \'Icelander\')
>>> c11 = AddressInfo(u\'d@evil.com\', \'Evil dude\')
# Case 5
>>> \'@\' in vcs.choose_from_address(None, [c21]).address
True
# Case 4
>>> vcs.choose_from_address(None, [c41]).address
u\'d@evil.com\'
>>> vcs.choose_from_address(None, [c42]).address
u\'g@f.com\'
# Case 3
>>> vcs.choose_from_address(None, [c31], [c42, c41]).address
u\'ok@foo.com\'
>>> vcs.choose_from_address(None, [c32], [c42, c41]).address
u\'g@f.com\'
# Case 2
>>> vcs.get_vcard(c21.address).add(
...    VCardLine(name=\'X-MAILPILE-PREFER-PROFILE\',
...              value=vcs.get_vcard(\'g@f.com\').random_uid))
>>> vcs.choose_from_address(None, [c42, c41], [c31, c32],
...                               [c21]).address
u\'g@f.com\'
# Case 1
>>> vcs.choose_from_address(None, [c42, c41], [c31, c32],
...                               [c21, c11]).address
u\'d@evil.com\''

def choose_from_address(vcards, *args, **kwargs):

    fa_list = vcards.choose_from_addresses(*args, **kwargs)
    return ((fa_list and fa_list[0]) or None)
