from mopidy import settings
from mopidy.frontends.mpd.protocol import handle_pattern
from mopidy.frontends.mpd.exceptions import MpdPasswordError

@handle_pattern(r'^close$')
def close(context):
    """
    *musicpd.org, connection section:*

        ``close``

        Closes the connection to MPD.
    """
    context.session.close()

@handle_pattern(r'^kill$')
def kill(context):
    """
    *musicpd.org, connection section:*

        ``kill``

        Kills MPD.
    """
    pass # TODO

@handle_pattern(r'^password "(?P<password>[^"]+)"$')
def password_(context, password):
    """
    *musicpd.org, connection section:*

        ``password {PASSWORD}``

        This is used for authentication with the server. ``PASSWORD`` is
        simply the plaintext password.
    """
    # You will not get to this code without being authenticated. This is for
    # when you are already authenticated, and are sending additional 'password'
    # requests.
    if settings.MPD_SERVER_PASSWORD != password:
        raise MpdPasswordError(u'incorrect password', command=u'password')

@handle_pattern(r'^ping$')
def ping(context):
    """
    *musicpd.org, connection section:*

        ``ping``

        Does nothing but return ``OK``.
    """
    pass

from mopidy import settings
from mopidy.frontends.mpd.protocol import handle_pattern
from mopidy.frontends.mpd.exceptions import MpdPasswordError

@handle_pattern(r'^close$')
def close(context):
    """
    *musicpd.org, connection section:*

        ``close``

        Closes the connection to MPD.
    """
    context.session.close()

@handle_pattern(r'^kill$')
def kill(context):
    """
    *musicpd.org, connection section:*

        ``kill``

        Kills MPD.
    """
    # TODO We do not want to allow remote killing of Mopidy. We should throw an
    # MPD exception here. Maybe using ACK_ERROR_PERMISSION.
    pass

@handle_pattern(r'^password "(?P<password>[^"]+)"$')
def password_(context, password):
    """
    *musicpd.org, connection section:*

        ``password {PASSWORD}``

        This is used for authentication with the server. ``PASSWORD`` is
        simply the plaintext password.
    """
    # You will not get to this code without being authenticated. This is for
    # when you are already authenticated, and are sending additional 'password'
    # requests.
    if settings.MPD_SERVER_PASSWORD != password:
        raise MpdPasswordError(u'incorrect password', command=u'password')

@handle_pattern(r'^ping$')
def ping(context):
    """
    *musicpd.org, connection section:*

        ``ping``

        Does nothing but return ``OK``.
    """
    pass

Update TODO for MPD command 'kill'
Update TODO for MPD command 'kill'
