'Decodes incoming GMCP data on the form \'varname <structure>\'.
Args:
data (str or list): GMCP data.
Notes:
Clients send data on the form "Module.Submodule.Cmdname <structure>".
We assume the structure is valid JSON.
The following is parsed into Evennia\'s formal structure:
Core.Name                         -> [name, [], {}]
Core.Name string                  -> [name, [string], {}]
Core.Name [arg, arg,...]          -> [name, [args], {}]
Core.Name {key:arg, key:arg, ...} -> [name, [], {kwargs}]
Core.Name [[args], {kwargs}]      -> [name, [args], {kwargs}]'

def decode_gmcp(self, data):

    if hasattr(data, '__iter__'):
        data = ''.join(data)
    if data:
        try:
            (cmdname, structure) = data.split(None, 1)
        except ValueError:
            (cmdname, structure) = (data, '')
        cmdname = cmdname.replace('.', '_')
        try:
            structure = json.loads(structure)
        except ValueError:
            pass
        (args, kwargs) = ([], {})
        if hasattr(structure, '__iter__'):
            if isinstance(structure, dict):
                kwargs = {key: value for (key, value) in structure.iteritems() if key}
            else:
                args = list(structure)
        else:
            args = (structure,)
        if cmdname.lower().startswith('core_'):
            cmdname = cmdname[5:]
        self.protocol.data_in(**{cmdname.lower(): [args, kwargs]})
