<commit_before>/*
 * VoIP.ms SMS Server
 * Copyright (C) 2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var database = require("./database.js");
var express = require("express");
var http = require("http");
var log = require("./log.js");
var util = require("util");

var TAG = "gcm";
var KEY = "insert key here";

var app = express();

database.connect(function(connection) {
    // Input validation for DIDs
    app.use(/\/(register|sms_callback)/, function(req, res, next) {
        var did = req.query["did"];
        if (typeof did === "undefined") {
            res.json({
                status: "did_missing"
            });
        }
        else if (did === "") {
            res.json({
                status: "did_empty"
            });
        }
        else if (isNaN(did) || did < 0 || did.length != 10) {
            res.json({
                status: "did_invalid"
            });
        }
        else {
            next();
        }
    });

    // Input validation for registration IDs
    app.use("/register", function(req, res, next) {
        var registrationId = req.query["reg_id"];
        if (typeof registrationId === "undefined") {
            res.json({
                status: "reg_id_missing"
            });
        }
        else if (registrationId === "") {
            res.json({
                status: "reg_id_empty"
            });
        }
        else if (!registrationId.match(/^[0-9a-zA-Z\-_]+$/) || registrationId.length > 4096) {
            res.json({
                status: "reg_id_invalid"
            });
        }
        else {
            next();
        }
    });

    // Handler for GCM registration
    app.use("/register", function(req, res) {
        var did = req.query["did"];
        var registrationId = req.query["reg_id"];
        var tag = TAG + " " + did;

        log.info(tag, "received registration request: registration ID " + registrationId);
        connection.query('REPLACE INTO main (RegistrationId, Did) VALUES (' + connection.escape(registrationId) + ',' +
            connection.escape(did) + ');', function(err) {
                if (err) {
                    res.json({
                        status: "database_error"
                    });<commit_msg>Registration IDs no longer need be alphanumeric<commit_after>/*
 * VoIP.ms SMS Server
 * Copyright (C) 2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var database = require("./database.js");
var express = require("express");
var http = require("http");
var log = require("./log.js");
var util = require("util");

var TAG = "gcm";
var KEY = "insert key here";

var app = express();

database.connect(function(connection) {
    // Input validation for DIDs
    app.use(/\/(register|sms_callback)/, function(req, res, next) {
        var did = req.query["did"];
        if (typeof did === "undefined") {
            res.json({
                status: "did_missing"
            });
        }
        else if (did === "") {
            res.json({
                status: "did_empty"
            });
        }
        else if (isNaN(did) || did < 0 || did.length != 10) {
            res.json({
                status: "did_invalid"
            });
        }
        else {
            next();
        }
    });

    // Input validation for registration IDs
    app.use("/register", function(req, res, next) {
        var registrationId = req.query["reg_id"];
        if (typeof registrationId === "undefined") {
            res.json({
                status: "reg_id_missing"
            });
        }
        else if (registrationId === "") {
            res.json({
                status: "reg_id_empty"
            });
        }
        else if (registrationId.length > 4096) {
            res.json({
                status: "reg_id_invalid"
            });
        }
        else {
            next();
        }
    });

    // Handler for GCM registration
    app.use("/register", function(req, res) {
        var did = req.query["did"];
        var registrationId = req.query["reg_id"];
        var tag = TAG + " " + did;

        log.info(tag, "received registration request: registration ID " + registrationId);
        connection.query('REPLACE INTO main (RegistrationId, Did) VALUES (' + connection.escape(registrationId) + ',' +
            connection.escape(did) + ');', function(err) {
                if (err) {
                    res.json({
                        status: "database_error"
                    });
{'fraction_non_alphanumeric': 0.1319796954314721, 'fraction_numerical': 0.005801305293691081, 'mean_word_length': 1.8541127780651836, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '541196', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1463, 'n_words': 449}