def presence_handler(stream, type_, from_, cb):
    """
    Context manager to temporarily register a callback to handle presence
    stanzas on a :class:`StanzaStream`.

    :param stream: Stanza stream to register the coroutine at
    :type stream: :class:`StanzaStream`
    :param type_: Presence type to listen for.
    :type type_: :class:`~.PresenceType`
    :param from_: Sender JID to listen for, or :data:`None` for a wildcard
                  match.
    :type from_: :class:`~aioxmpp.JID` or :data:`None`.
    :param cb: Callback to register

    The callback is registered when the context is entered and unregistered
    when the context is exited.

    .. versionadded:: 0.8
    """

    stream.register_presence_callback(
        type_,
        from_,
        cb,
    )
    try:
        yield
    finally:
        stream.unregister_presence_callback(
            type_,
            from_,
        )