Skip to content

Commit

Permalink
basic website
Browse files Browse the repository at this point in the history
  • Loading branch information
Armaldio committed Apr 5, 2017
1 parent e58ebc0 commit b7741a1
Show file tree
Hide file tree
Showing 12 changed files with 485 additions and 560 deletions.
160 changes: 107 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,143 @@
let config = require('./config');
const fs = require("fs");
const low = require("lowdb");
const db = low('db.json');
const bodyParser = require("body-parser");
const moment = require("moment");
const app = require('express')();
const express = require('express');
const server = require('http').Server(app);
const server = require('http')
.Server(app);
const io = require('socket.io')(server);
const firebase = require("firebase");
const base64 = require("base-64");

const server_port = process.env.OPENSHIFT_NODEJS_PORT || 8081;
const server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
console.log("Trying to start server with config:", config.serverip + ":" + config.serverport);

server.listen(server_port, server_ip_address);
console.log("Listening " + server_ip_address + ":" + server_port);

let fbconfig = require("./fbconfig.json");
firebase.initializeApp(fbconfig);
let database = firebase.database();

let receivedMessages = database.ref('serverqueue/');

receivedMessages.on('value', function (snapshot) {
let val = snapshot.val();

for (let property in val) {
if (val.hasOwnProperty(property)) {
server.listen(config.serverport, config.serverip, function () {
console.log("Server running @ http://" + config.serverip + ":" + config.serverport);
});

console.log("Message : ", val[property]);
db.get('messages').push(val[property]).write();
firebase.database().ref("serverqueue/" + property).remove();
let needToConfigure = false;

io.sockets.emit('message', val[property]);
let fbconfig = require("./fbconfig.json");
let database;

try {
firebase.initializeApp(fbconfig);
database = firebase.database();
}
catch (e) {
needToConfigure = true;
console.log("You must configure this app before using it, go to XXX to proceed");
}

let receivedMessages;
let appQueue;

if (!needToConfigure) {

receivedMessages = database.ref('serverqueue/');
appQueue = database.ref('appqueue/');

/**
* On new incomming message from the phone
*
* Also triggers at start
*/
receivedMessages.on('value', function (snapshot) {
let val = snapshot.val();

//For each messages remaining in the queue
for (let property in val) {
if (val.hasOwnProperty(property)) {

console.log("Message received from the queue : ", val[property]);

//Push message to the database
db.get('messages.' + val[property].phone)
.push(val[property])
.write();

//Remove the message from the queue
database.ref("serverqueue/" + property)
.remove();

//Send the message to the browser
io.sockets.emit('message', val[property]);
}
}
}
});
});

//http://stackoverflow.com/questions/8684772/having-a-service-receive-sms-messages
//http://stackoverflow.com/questions/19813707/android-receive-sms-even-if-app-is-closed
}

db.defaults({
contacts: [],
messages: []
}).write();
})
.write();

io.on('connection', function (socket) {
console.log("Client joined");

//When the server receive a new message sent from the browser
socket.on('browserMessage', function (message) {
console.log("Message received from browser : ", message);

message.from = config.number;

//Write message to database
db.get('messages.' + message.to)
.push(message)
.write();

//Send mesage to firebase for the mobile app
appQueue.push(message);
console.log("Message sent : ", message);
//console.log("MESSAGE NOT SENT TO PHONE");

});

});

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(express.static('website'));

app.use("/style", express.static('website/style'));
app.use("/lib", express.static('website/lib'));
app.use("/js", express.static('website/js'));

app.get('/', function (req, res) {
res.sendFile('index.html', {root: __dirname + "/website"});
});
console.log(needToConfigure);
if (needToConfigure)
res.sendFile('index.html', {root: __dirname + "/website"});
else
res.redirect('/chat');

app.get('/contacts', function (req, res) {
res.end(JSON.stringify(db.get('contacts').value()));
});

app.get('/messages', function (req, res) {
res.end(JSON.stringify(db.get('messages').value()));
app.use("/chat", express.static('website/chat'));
app.get('/chat', function (req, res) {
res.sendFile('index.html', {root: __dirname + "/website/chat"});
});

app.post('/newMessage', function (req, res) {
let message = {
phone : req.body.contact,
message : req.body.message,
timestamp: req.body.timestamp
};
app.get('/api/config', function (req, res) {
res.send('var config = ' + JSON.stringify(config));
});

console.log("New message ", message);
app.get('/contacts', function (req, res) {
res.end(JSON.stringify(db.get('contacts')
.value()));
});

db.get('messages').push(message).write();
io.broadcast.emit('message', message);
res.end("ok");
app.get('/messages', function (req, res) {
res.end(JSON.stringify(db.get('messages')
.value()));
});

/*
app.get('/clear', function (req, res) {
fs.unlink("db.json", function (err, res) {
if (err) res.error("Unable to delete file");
else {
res.end("ok");
}
});
});
*/
app.get('/messages/:id', function (req, res) {
let id = req.params.id;
console.log("id = " + id + " = " + base64.decode(id));

res.end(JSON.stringify(db.get('messages.' + base64.decode(id)).value()));
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "An open source project that allow you to send sms from your browser through your phone",
"main": "index.js",
"scripts": {
"test": "no"
"start": "node index.js"
},
"keywords": [
"sms",
Expand All @@ -14,13 +14,15 @@
"author": "Armaldio",
"license": "MIT",
"dependencies": {
"@types/base-64": "^0.1.2",
"@types/body-parser": "^1.16.2",
"@types/express": "^4.0.35",
"@types/lowdb": "^0.15.0",
"@types/moment": "^2.13.0",
"@types/socket.io": "^1.4.29",
"babel-preset-es2015": "^6.24.0",
"babel-preset-stage-2": "^6.22.0",
"base-64": "^0.1.0",
"body-parser": "^1.17.1",
"express": "^4.15.2",
"firebase": "^3.7.4",
Expand Down
46 changes: 46 additions & 0 deletions website/chat/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LiveSMS</title>
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/css/materialize.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="row">
<div class="collection col s2">
<ul id="contacts" class="collection with-header">
<li class="collection-header"><h5>Contacts</h5></li>
</ul>
</div>

<div id="messages" class="col s10">
<nav>
<div class="nav-wrapper">
<img class="responsive-img circle" width="48px" src="http://i.imgur.com/DY6gND0.png"
draggable="false">
<a id="currentContact">{{ contactName }}</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="#">Options</a></li>
</ul>
</div>
</nav>
<div id="message-list" class="row">

</div>
<input id="input" class="textarea" type="text" placeholder="Type here.."/>
</div>
</div>

<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.7.1/firebase.js"></script>
<script src="../api/config"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/js/materialize.min.js"></script>
<script src="index.js"></script>
</body>
</html>
Loading

0 comments on commit b7741a1

Please sign in to comment.