#!/usr/bin/env python

# local imports
import sanity

import bottle
import json


def run_cmd(conf, cache):
    '''
    Execute an administrative command.
    '''
    data = bottle.request.json
    admin_key = conf.get('bottle', 'admin_key')

    # Supported commands
    commands = {
            'blacklist_paste': _cmd_blacklist_paste,
            'bl': _cmd_blacklist_paste,
            'delete_paste': _cmd_delete_paste,
            'del': _cmd_delete_paste,
            'whitelist_address': _cmd_whitelist_address,
            'wl': _cmd_whitelist_address,
            'greylist_address': _cmd_greylist_address,
            'gl': _cmd_greylist_address}

    # Pre-flight checks
    err = None
    if not admin_key:
        err = 'No admin key configured.'
    elif not 'token' in data:
        err = 'No auth provided.'
    elif data['token'] != admin_key:
        err = 'Invalid auth.'
    elif 'cmd' not in data:
        err = 'No command provided.'
    elif data['cmd'] not in commands:
        err = 'Command not supported.'
    if err:
        return {'message': err, 'status': 'error'}

    # Flight
    return commands[data['cmd']](cache, data)


def _cmd_blacklist_paste(cache, data):
    # Delete paste
    _cmd_delete_paste(cache, data)

    # Block address
    addr = bottle.request.environ.get('REMOTE_ADDR', 'undef').strip()
    if sanity.blacklist_address(cache, addr):
        return {'message': 'Address "{}" blacklisted; paste removed.'.format(addr), 'status': 'success'}
    return {'message': 'unexpected error; task already complete?', 'status': 'error'}


def _cmd_delete_paste(cache, data):
    paste = data.get('target')
    if not paste:
        return {'message': 'No paste provided.', 'status': 'error'}
    paste_id = 'paste:{}'.format(data['target'])

    # Find paste origin address
    paste = cache.get(paste_id)
    if not paste:
        return {'message': 'Paste not found.', 'status': 'error'}
    addr = json.loads(paste)['origin_addr']

    # Remove paste
    cache.delete(paste_id)


def _cmd_whitelist_address(cache, data):
    addr = data.get('target')
    if not addr:
        return {'message': 'No address provided.', 'status': 'error'}

    if sanity.whitelist_address(cache, addr):
        return {'message': 'Address subnet white listed.', 'status': 'success'}
    return {'message': 'unexpected error; task already complete?', 'status': 'error'}


def _cmd_greylist_address(cache, data):
    '''
    Don't block an address from using the service, but disable IRC relay.
    '''
    paste = data.get('target')
    if not paste:
        return {'message': 'No paste provided.', 'status': 'error'}
    paste_id = 'paste:{}'.format(data['paste'])

    # Find paste origin address
    paste = cache.get(paste_id)
    if not paste:
        return {'message': 'Paste not found.', 'status': 'error'}
    addr = json.loads(paste)['origin_addr']

    # Greylist address
    if sanity.greylist_address(cache, addr):
        return {'message': 'Address subnet grey listed.', 'status': 'success'}
    return {'message': 'unexpected error; task already complete?', 'status': 'error'}

@@ -767,23 +767,78 @@
 lif not 
+data:%0A        err = 'No payload decoded.'%0A    elif 
 'token'
+ not
  in data
@@ -951,17 +951,21 @@
  elif 'c
-m
+omman
 d' not i
@@ -1025,17 +1025,21 @@
  data%5B'c
-m
+omman
 d'%5D not 
@@ -1171,22 +1171,87 @@
 ght%0A    
-return
+bottle.response.headers%5B'Content-Type'%5D = 'application/json'%0A    resp =
  command
@@ -1259,17 +1259,21 @@
 %5Bdata%5B'c
-m
+omman
 d'%5D%5D(cac
@@ -1282,16 +1282,44 @@
 , data)%0A
+    return json.dumps(resp)%0A
 %0A%0Adef _c
@@ -1375,19 +1375,19 @@
 te%0A    _
-cmd
+ = 
 _delete_
@@ -1577,29 +1577,24 @@
 'Add
-ress %22%7B%7D%22
+ed to
  black
+ 
 list
-ed
 ; pa
@@ -1762,24 +1762,188 @@
 che, data):%0A
+    ret = _delete_paste(cache, data)%0A    if ret:%0A        return ret%0A    return %7B'message': 'Paste deleted.', 'status': 'success'%7D%0A%0A%0Adef _delete_paste(cache, data):%0A
     paste = 
@@ -2575,35 +2575,30 @@
 ': '
-Address subnet white listed
+Removed from filtering
 .', 
@@ -2990,21 +2990,13 @@
 mat(
-data%5B'
 paste
-'%5D
 )%0A%0A 
@@ -3022,32 +3022,33 @@
 gin address%0A    
+c
 paste = cache.ge
@@ -3062,32 +3062,33 @@
 _id)%0A    if not 
+c
 paste:%0A        r
@@ -3158,32 +3158,33 @@
 dr = json.loads(
+c
 paste)%5B'origin_a
@@ -3293,19 +3293,13 @@
 'Add
-ress subnet
+ed to
  gre
@@ -3303,19 +3303,16 @@
 grey lis
-ted
 .', 'sta

Clean up admin api; finally tested and working.