Permalink
Browse files

Merge branch 'master' of github.com:junkafarian/watchdog

  • Loading branch information...
2 parents 157f3e8 + fa2db99 commit c23c398d4c432e5553bf2d69e204cca20aff3054 @joelanman joelanman committed Feb 18, 2012
View
6 app.js
@@ -33,7 +33,11 @@ app.configure('production', function(){
app.get('/', routes.index);
app.get('/signedin', routes.signedin);
-app.get('/alert/:key', routes.alert);
+app.get('/alert/:username', routes.alert);
+app.get('/status/', routes.status_listings);
+app.get('/status/:username', routes.status);
+app.get('/register', routes.register);
+app.post('/newuser', routes.adduser);
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
View
@@ -5,5 +5,6 @@
, "dependencies": {
"express": "2.5.5"
, "stache": ">= 0.0.1"
+ , "mailer": ">= 0.0.1"
}
}
View
@@ -1,6 +1,12 @@
var models = require('./models');
var users = new models.UserList();
+function bootstrap() {
+ // bootstrap data
+ var dummy = new models.User('dummy', 'password', 'dummy123', 'I might be in trouble, please eat all the vegetables.', 'dummy@example.com');
+ users.add_user(dummy);
+}
+
exports.index = function(req, res){
res.render('index', {
locals: {
@@ -18,14 +24,79 @@ exports.signedin = function(req, res){
};
exports.alert = function(req, res){
- var key = req.params.key;
+ var username = req.params.username;
+ var user = users.get_user(username);
+
+ if (user != undefined) {
+ user.alert();
+
+ console.log("Alert received and processed for %s", username);
+ res.render('alert', {
+ locals: {'username': username}
+ });
+ } else {
+ console.log("Alert received for invalid username: %s", username);
+ res.render('failed_alert', {
+ locals: {'username': username,
+ 'reason': 'User could not be found'}
+ });
+ }
+};
- // TODO: - lookup user based on `key`
- // - send alerts
- // - update user status
+exports.status = function(req, res) {
+ var username = req.params.username;
+ var user = users.get_user(username);
+
+ if (user != undefined) {
+ console.log("Requested status for %s: %s", username, user.status);
+ res.render('status', {
+ locals: {'username': username,
+ 'status': user.status}
+ });
+ } else {
+ console.log("Requested status for invalid username: %s", username);
+ res.render('404', {
+ locals: {'reason': 'User could not be found'}
+ });
+ }
+};
- res.render('alert', {
- locals: {'key': key}
+exports.status_listings = function(req, res) {
+ var statuses = [];
+ for (var username in users.users) {
+ user = users.get_user(username)
+ if (user != undefined) {
+ statuses.push({'username': user.username,
+ 'status': user.status});
+ }
+ }
+ res.render('status_listings', {
+ locals: {'statuses': statuses}
});
-};
+}
+
+exports.testmail = function(req, res){
+ var usr = new models.User('gerard', 'gerard123', 'QWERTY', 'I might be in trouble, please eat all the vegetables.', 'gerard@ideesabsurdes.net');
+ // users.add_user(usr);
+ usr.sendEmail();
+}
+
+exports.adduser = function(req, res){
+
+ var user_data = req.body.user;
+
+ // I use Date().getTime() to simulate somehow a keygenerator to acces the "API"
+
+ var key = new Date().getTime();
+ var usr = new models.User(user_data.username, user_data.password, key, user_data.email, [user_data.contact01, user_data.contact02, user_data.contact03, user_data.contact04, user_data.contact05]);
+ users.add_user(usr);
+
+ res.render('generic_message', {
+ locals:{'message': 'registered! This is your secret key to update your status from anywhere else:<br><h2>'+key+'</h2><br>'}
+ });
+}
+
+exports.register = function(req, res){
+ res.render('registration');
+}
View
@@ -1,45 +1,88 @@
+// UserList
+//
+//
+//
+
+var email = require('mailer');
+
function UserList(){
- this.users = [];
+ this.users = {};
}
-UserList.prototype.add_user = function(new_user) { this.users[new_user.get_username()] = new_user; return unsers.length };
-UserList.prototype.get_user = function(username) { return this.users[username];};
-
+UserList.prototype.add_user = function(new_user) { this.users[new_user.username] = new_user; };
+UserList.prototype.get_user = function(username) { return this.users[username];};
+// User
+//
+//
+//
function User(username, password, key, email, email_to) {
- this.username = username;
- this.password = password;
- this.key = key;
- this.email = email;
- this.email_to = email_to;
- this.panic_status = false;
- this.checkins = [ new Checkin(new Date().getTime(), null, username, 'created account') ];
+ this.username = username;
+ this.password = password;
+ this.key = key;
+ this.email = email;
+ this.email_to = email_to;
+ this.status = 'ok';
+ this.checkins = [ new Checkin(new Date().getTime(), null, username, 'created account') ];
}
-User.prototype.get_username = function() { return this.username; };
-User.prototype.get_password = function() { return this.password; };
-User.prototype.get_key = function() { return this.key; };
-User.prototype.get_email = function() { return this.email; };
-User.prototype.get_emailto = function() { return this.email_to; };
-User.prototype.get_panic_status = function() { return this.panic_status; };
-User.prototype.get_checkins = function() { return this.checkins; };
+User.prototype.get_username = function() { return this.username; };
+User.prototype.get_password = function() { return this.password; };
+User.prototype.get_key = function() { return this.key; };
+User.prototype.get_email = function() { return this.email; };
+User.prototype.get_emailto = function() { return this.email_to; };
+User.prototype.get_status = function() { return this.status; };
+User.prototype.get_checkins = function() { return this.checkins; };
User.prototype.get_last_checkin = function() { return this.checkins[checkins.length - 1]; };
-User.prototype.checkin = function(checkin) { this.checkins.push(checkin); }
+User.prototype.checkin = function(checkin) { this.checkins.push(checkin); }
+User.prototype.alert = function() {
+ var checkin = new Checkin(new Date().getTime(), null, this.username, 'panic alert')
+ this.checkins.push(checkin);
+ this.status = 'offline'
+ // send alerts & notifications
+}
+User.prototype.sendEmail = function(){
+
+ email.send({
+
+ host : "localhost", // smtp server hostname
+ port : "25", // smtp server port
+ ssl: false, // for SSL support - REQUIRES NODE v0.3.x OR HIGHER
+ domain : "localhost", // domain used by client to identify itself to server
+ to : this.email_to.toString(),
+ from : "alert@amnestywatchdog.im",
+ subject : "Automatic Alert Message",
+ body: this.email,
+ authentication : null, // auth login is supported; anything else is no auth
+ //username : "my_username", // username
+ //password : "my_password" // password
+
+ },
+ function(err, result){
+ if(err){ console.log(err); }
+ });
+
+}
+
+// Checkin
+//
+//
+//
function Checkin(date_time, location, source, action) {
- this.date_time = date_time;
- this.location = location;
- this.source = source;
- this.action = action;
+ this.date_time = date_time;
+ this.location = location;
+ this.source = source;
+ this.action = action;
}
-User.prototype.get_date_time = function() { return this.date_time; };
-User.prototype.get_location = function() { return this.location; };
-User.prototype.get_source = function() { return this.source; };
-User.prototype.get_action = function() { return this.action; };
+User.prototype.get_date_time = function() { return this.date_time; };
+User.prototype.get_location = function() { return this.location; };
+User.prototype.get_source = function() { return this.source; };
+User.prototype.get_action = function() { return this.action; };
exports.User = User;
View
@@ -0,0 +1,2 @@
+<h1>404 Not Found</h1>
+<p>{{reason}}</p>
View
@@ -1,2 +1,2 @@
-<h1>Alert recieved for {{key}}</h1>
+<h1>Alert recieved for {{username}}</h1>
<p>We have notified your contacts and are sending help</p>
@@ -0,0 +1,2 @@
+<h1>Alert could not be processed {{username}}</h1>
+<p>{{reason}}</p>
@@ -0,0 +1 @@
+{{message}}
@@ -0,0 +1,19 @@
+<form action="/newuser" method="post">
+
+ <span>username</span>
+ <br><input type="text" name="user[username]">
+ <br><span>password</span>
+ <br><input type="password" name="user[password]">
+ <br><span>email to contacts</span>
+ <br><textarea name="user[email]"></textarea>
+
+ <br><span>contacts emails</span>
+ <br><input type="text" name="user[contact01]"/>
+ <br><input type="text" name="user[contact02]"/>
+ <br><input type="text" name="user[contact03]"/>
+ <br><input type="text" name="user[contact04]"/>
+ <br><input type="text" name="user[contact05]"/>
+
+ <br><input type="submit" value="Submit">
+
+</form>
View
@@ -0,0 +1,2 @@
+<h1>Current status of {{username}}</h1>
+<p>{{status}}</p>
@@ -0,0 +1,4 @@
+<h1>Current status of all system users</h1>
+{{#statuses}}
+ <p>{{username}}: {{status}}</p>
+{{/statuses}}

0 comments on commit c23c398

Please sign in to comment.