"""Parse bounce messages generated by Exim.

Exim adds an X-Failed-Recipients: header to bounce messages containing
an `addresslist' of failed addresses.
"""

from email.utils import getaddresses
from email.message import Message

from public import public

from flufl.bounce.interfaces import NoTemporaryFailures, Recipients


@public
class Exim:
    """Parse bounce messages generated by Exim."""

    def process(self, msg: Message) -> tuple[Recipients, Recipients]:
        """See `BounceDetector`."""
        all_failed = msg.get_all('x-failed-recipients', [])
        # all_failed will contain string/unicode values, but the flufl.bounce
        # API requires these to be bytes.  We don't know the encoding, but
        # assume it must be ascii, per the relevant RFCs.
        return (
            NoTemporaryFailures,
            frozenset(address.encode('us-ascii') for name, address in getaddresses(all_failed)),
        )
