예제 #1
0
def message_send(service):
    text = request.form.get('message')
    if not text:
        return Error.ARGUMENT_MISSING('message')

    subscribers = Subscription.query.filter_by(service=service).count()
    if subscribers == 0:
        # Pretend we did something even though we didn't
        # Nobody is listening so it doesn't really matter
        return Error.NONE

    level = (request.form.get('level') or '3')[0]
    level = int(level) if level in "12345" else 3
    title = request.form.get('title', '').strip()[:255]
    link = request.form.get('link', '').strip()
    msg = Message(service, text, title, level, link)
    db.session.add(msg)
    db.session.commit()

    if google_api_key or current_app.config['TESTING']:
        Gcm.send_message(msg)

    if apns_cert_path or current_app.config['TESTING']:
        Apns.send_message(msg)

    if zeromq_relay_uri:
        queue_zmq_message(json_encode({"message": msg.as_dict()}))

    service.cleanup()
    db.session.commit()
    return Error.NONE
예제 #2
0
def apns_register(client):
    '''This is called in iPushjet's
    application:didRegisterForRemoteNotificationsWithDeviceToken: override.

    We send the UUID along, which PushJet uses for everything internally.
    However, the (variable length) token that Apple sends us is what we must
    send notifs to later on.

    In other words, the handshake looks like this:
      1. The iDevice registers with Apple
      2. When (1) is successful, the device hits this endpoint and sends its
         UUID along with the token it received from Apple in (1).
      3. Both are stored in the `apns` table.

    When subscribing to something, we look attach the subscription to the UUID.

    When sending, we grab all UUIDs subscribed, then key on them to map them to
    device_tokens which get sent to.

    This is basically the same way the GCM one works, except that it's documented.
    '''
    token = request.form.get('device_token', False)

    if not token:
        return Error.ARGUMENT_MISSING('device_token')
    regs = Apns.query.filter_by(uuid=client).all()
    for u in regs:
        db.session.delete(u)
    reg = Apns(client, token)
    db.session.add(reg)
    db.session.commit()
    return Error.NONE
예제 #3
0
def service_create():
    name = request.form.get('name', '').strip()
    icon = request.form.get('icon', '').strip()
    if not name:
        return Error.ARGUMENT_MISSING('name')
    srv = Service(name, icon)
    db.session.add(srv)
    db.session.commit()
    return jsonify({"service": srv.as_dict(True)})
예제 #4
0
def service_create():
    name = request.form.get('name', '').strip()
    icon = request.form.get('icon', '').strip()
    encrypted = request.form.get('encrypted',
                                 '').strip().lower() in ("1", "true", "yes")
    if not name:
        return jsonify(Error.ARGUMENT_MISSING('name'))
    srv = Service(name, icon, encrypted=encrypted)
    db.session.add(srv)
    db.session.commit()
    return jsonify({"service": srv.as_dict(True)})
예제 #5
0
def gcm_register(client):
    registration = request.form.get('regid', False) or request.form.get(
        'regId', False)

    if not registration:
        return Error.ARGUMENT_MISSING('regid')
    regs = Gcm.query.filter_by(uuid=client).all()
    for u in regs:
        db.session.delete(u)
    reg = Gcm(client, registration)
    db.session.add(reg)
    db.session.commit()
    return jsonify(Error.NONE)
예제 #6
0
def gcm_register(client):
    registration = request.form.get('regid', False) or request.form.get(
        'regId', False)
    pubkey_b64 = request.form.get('pubkey', None)
    if pubkey_b64:
        # noinspection PyBroadException
        try:
            rsa.PublicKey.load_pkcs1(b64decode(pubkey_b64), 'DER')
        except:
            return jsonify(Error.INVALID_PUBKEY)

    if not registration:
        return Error.ARGUMENT_MISSING('regid')
    regs = Gcm.query.filter_by(uuid=client).all()
    for u in regs:
        db.session.delete(u)
    reg = Gcm(client, registration, pubkey_b64)
    db.session.add(reg)
    db.session.commit()
    return jsonify(Error.NONE)
예제 #7
0
def message_send(service):
    text = request.form.get('message')
    if not text:
        return jsonify(Error.ARGUMENT_MISSING('message'))
    level = (request.form.get('level') or '3')[0]
    level = int(level) if level in "12345" else 3
    title = request.form.get('title', '').strip()[:255]
    link = request.form.get('link', '').strip()
    msg = Message(service, text, title, level, link)
    db.session.add(msg)
    db.session.commit()

    if google_api_key:
        Gcm.send_message(msg)

    if zeromq_relay_uri:
        queue_zmq_message(json_encode({"message": msg.as_dict()}))

    service.cleanup()
    db.session.commit()
    return jsonify(Error.NONE)
예제 #8
0
def service_info():
    secret = request.form.get('secret', '') or request.args.get('secret', '')
    service_ = request.form.get('service', '') or request.args.get(
        'service', '')

    if service_:
        if not is_service(service_):
            return Error.INVALID_SERVICE

        srv = Service.query.filter_by(public=service_).first()
        if not srv:
            return Error.SERVICE_NOTFOUND
        return jsonify({"service": srv.as_dict()})

    if secret:
        if not is_secret(secret):
            return Error.INVALID_SECRET

        srv = Service.query.filter_by(secret=secret).first()
        if not srv:
            return Error.SERVICE_NOTFOUND
        return jsonify({"service": srv.as_dict()})

    return Error.ARGUMENT_MISSING('service')