Skip to content

Commit

Permalink
Complete overhaul of emails
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Gressmann <mail@explodingcamera.com>
  • Loading branch information
explodingcamera committed Jul 13, 2016
1 parent 51c8e69 commit 5cae0b4
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 28 deletions.
46 changes: 40 additions & 6 deletions config.example.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
greet: "Welcome to musiqpad!"
bg: "" // Background image file path. Accepts external images. If this is undefined the default background will be used.
maxCon: 0
ownerEmail: "mail@explodingcamera.com" // This needs to be set then the server restarted to take effect.
ownerEmail: "user@domain.tld" // This needs to be set then the server restarted to take effect.
guestCanSeeChat: true
bannedCanSeeChat: false
lastmsglimit: 6// How many messages a user can see after joining.
Expand All @@ -45,15 +45,49 @@
limit_save: 0
limit_send: 50
}
email: {
mail: {
confirmation: false// Whether to force user to confirm his email address before he is able to do anything
sender: "your@email.tld"
sender: "user@domain.tld" // Domain should point to this box when using direct mode

// DIRECT PROBABLY WON'T WORK BECAUSE YOUR IP DOESN'T HAVE A GOOD REPUTATION!

transport: "direct" // 'smtp', 'direct' or 'xoauth'
options: {}
/*
description: Email server setup please refer to https://github.com/nodemailer/nodemailer documention on what the options are supports xOAuth 2.0
default: {}
*/
EXAMPLES:

-- SMTP --
transport: "smtp"
options: {
service: "gmail"
auth: {
user: "mail@somewebsite.com",
pass: "pass",
}
}
----------

-- XOAUTH2 --
transport: "xoauth"
options: {
service: "gmail"
auth: {
xoauth: {
user: '{username}'
clientId: '{Client ID}'
clientSecret: '{Client Secret}'
refreshToken: '{refresh-token}'
accessToken: '{cached access token}
}
}
}
-------------

-- DIRECT --
transport: "direct"
options: {}
-------------
*/
}
description:
'''
Expand Down
2 changes: 1 addition & 1 deletion socketserver/database_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ DBUtils.prototype.validateUsername = function (un) {
return /^[a-z0-9_-]{3,20}$/i.test(un);
};

module.exports = new DBUtils();
module.exports = new DBUtils();
6 changes: 3 additions & 3 deletions socketserver/db_level.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const log = new(require('basic-logger'))({
const nconf = require('nconf');

// Files
const Mailer = require('./mailer');
var Mailer = require('./mail/Mailer');
const DBUtils = require('./database_util');

// Variables
Expand Down Expand Up @@ -387,7 +387,7 @@ LevelDB.prototype.createUser = function (obj, callback) {
user.data.salt = DBUtils.makePass(Date.now()).slice(0, 10);
user.data.pw = DBUtils.makePass(inData.pw, user.data.salt);
user.data.created = Date.now();
if (nconf.get('room:email:confirmation')) user.data.confirmation = DBUtils.makePass(Date.now());
if (nconf.get('room:mail:confirmation')) user.data.confirmation = DBUtils.makePass(Date.now());
var updatedUserObj = user.makeDbObj();

var tok = that.createToken(inData.email);
Expand All @@ -399,7 +399,7 @@ LevelDB.prototype.createUser = function (obj, callback) {
}

// Send confirmation email
if (nconf.get('room:email:confirmation')) {
if (nconf.get('room:mail:confirmation')) {
Mailer.sendEmail('signup', {
code: user.data.confirmation,
user: inData.un,
Expand Down
6 changes: 3 additions & 3 deletions socketserver/db_mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const log = new(require('basic-logger'))({

// Files
const nconf = require('nconf');
const Mailer = require('./mailer');
const Mailer = require('./mail/Mailer');
const DBUtils = require('./database_util');

// Variables
Expand Down Expand Up @@ -506,7 +506,7 @@ MongoDB.prototype.createUser = function (obj, callback) {
user.data.salt = DBUtils.makePass(Date.now()).slice(0, 10);
user.data.pw = DBUtils.makePass(inData.pw, user.data.salt);
user.data.created = Date.now();
if (nconf.get('room:email:confirmation')) {
if (nconf.get('room:mail:confirmation')) {
user.data.confirmation = DBUtils.makePass(Date.now());
}
var updatedUserObj = user.makeDbObj();
Expand All @@ -522,7 +522,7 @@ MongoDB.prototype.createUser = function (obj, callback) {
}

// Send confirmation email
if (nconf.get('room:email:confirmation')) {
if (nconf.get('room:mail:confirmation')) {
Mailer.sendEmail('signup', {
code: user.data.confirmation,
user: inData.un,
Expand Down
6 changes: 3 additions & 3 deletions socketserver/db_mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const nconf = require('nconf');

//Files
var Hash = require('./hash');
var Mailer = require('./mailer');
const Mailer = require('./mail/Mailer');
var DBUtils = require('./database_util');
var Roles = require('./role.js');

Expand Down Expand Up @@ -550,7 +550,7 @@ MysqlDB.prototype.createUser = function(obj, callback) {
user.data.salt = DBUtils.makePass(Date.now()).slice(0, 10);
user.data.pw = DBUtils.makePass(inData.pw, user.data.salt);
user.data.created = Date.now();
if (nconf.get('room:email:confirmation')) user.data.confirmation = DBUtils.makePass(Date.now());
if (nconf.get('room:mail:confirmation')) user.data.confirmation = DBUtils.makePass(Date.now());
var updatedUserObj = user.makeDbObj();

var tok = that.createToken(inData.email);
Expand All @@ -564,7 +564,7 @@ MysqlDB.prototype.createUser = function(obj, callback) {
}

//Send confirmation email
if (nconf.get('room:email:confirmation')) {
if (nconf.get('room:mail:confirmation')) {
Mailer.sendEmail('signup', {
code: user.data.confirmation,
user: inData.un,
Expand Down
58 changes: 58 additions & 0 deletions socketserver/mail/Mailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const nodemailer = require('nodemailer');
const xoauth2 = require('xoauth2');
const fs = require('fs-extra');
const nconf = require('nconf');
const ejs = require('ejs');

var Mailer = function () {
const options = nconf.get('room:mail:options');
var _this = this;
_this.test = 1;
if (nconf.get('room:allowrecovery') || nconf.get('room:mail:confirmation')) {
switch (nconf.get('room:mail:transport')) {
case 'smtp': {
_this.transporter = nodemailer.createTransport(options);
break;
}
case 'xoauth': {
const xoauth = xoauth2.createXOAuth2Generator({
user: options.auth.xoauth.user,
clientId: options.auth.xoauth.clientId,
clientSecret: options.auth.xoauth.clientSecret,
refreshToken: options.auth.xoauth.refreshToken,
accessToken: options.auth.xoauth.accessToken
});
options.auth.xoauth = xoauth;
_this.transporter = nodemailer.createTransport(options);
break;
}
case 'direct': {
const domain = nconf.get('room:mail:sender').split('@')[1];
options.name = domain;
_this.transporter = nodemailer.createTransport(options);
break;
}
}
}
}

Mailer.prototype.sendEmail = function (type, opts, receiver, callback) {
const html = ejs.render(fs.readFileSync(`socketserver/mail/templates/${type}.html`, 'utf8'), {
opts,
room: nconf.get('room'),
});

const emailObj = {
from: nconf.get('room:mail:sender'),
to: receiver,
subject: type.subject,
html,
};

this.transporter.sendMail(emailObj, (error, response) => {
if (error) callback(error);
else callback(null, response);
});
};

module.exports = new Mailer();
Loading

0 comments on commit 5cae0b4

Please sign in to comment.