-
Notifications
You must be signed in to change notification settings - Fork 12
/
mail.js
167 lines (152 loc) · 4.61 KB
/
mail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* vim:set sw=2 ts=2 sts=2 ft=javascript expandtab:
*
* # Permission Module
*
* ## License
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
* ## Description
*
* This module contains all functions about permission for groups and pads,
* according to authenticated user.
*/
// External dependencies
var ld = require('lodash');
var cuid = require('cuid');
var emailjs = require('emailjs');
// Local dependencies
require('./helpers.js'); // Helpers auto-init
var conf = require('./configuration.js');
module.exports = (function () {
'use strict';
var mail = {
tokens: {},
ends: {},
connection: undefined
};
/**
* ## genToken
*
* `genToken` is a function that creates a token using *cuid* and populates
* local in memory store with necessary information. It also fixes maximum
* time validity for the current token, according to configuration.
*
* It takes a mandatory `value` and returns the generated token.
*/
mail.genToken = function (value) {
if (ld.isUndefined(value)) {
throw new TypeError('BACKEND.ERROR.TYPE.PARAMS_REQUIRED');
}
var duration = parseFloat(conf.get('tokenDuration')) || 60;
var ts = Date.now();
var end = ts + duration * 60 * 1000;
var token = cuid();
mail.tokens[token] = value;
mail.ends[token] = end;
return token;
};
/**
* ## isValidToken
*
* This function checks if, for a given token, expiration has not been
* reached.
*/
mail.isValidToken = function (token) {
return (mail.tokens[token] && (Date.now() < mail.ends[token]));
};
/**
* ## connect
*
* `connect` uses SMTP configuration options to connect to the remote SMTP
* server. It takes a `callback` function on which it returns *true* for
* success or *error* on failure. If a connection has already been setup, it
* quits before recreating one. If user and pass are provided, it uses them to
* login after connection.
*/
mail.connect = function () {
var opts = {
port: parseInt(conf.get('SMTPPort'), 10),
host: conf.get('SMTPHost'),
ssl: conf.get('SMTPSSL'),
tls: conf.get('SMTPTLS'),
user: conf.get('SMTPUser'),
password: conf.get('SMTPPass')
};
if (!ld.isNumber(opts.port) || !ld.isString(opts.host) ||
!ld.isBoolean(opts.ssl) || !ld.isBoolean(opts.tls)) {
throw new TypeError('BACKEND.ERROR.TYPE.SMTP_CONFIG');
}
try {
mail.server = emailjs.server.connect(opts);
}
catch (e) { console.error(e); }
};
/**
* ## send
*
* `send` is an asynchronous function that sends an email. It takes mandatory
*
* - `to`, an email string
* - `message`, a string containing the text message
* - a `callback` function
*
* It returns to the callback function *error* if there is, *null* otherwise
* and an information object. See smtp-connection for more details.
*/
mail.send = function (to, subject, message, callback) {
var err;
if (!ld.isEmail(to)) {
throw new TypeError('BACKEND.ERROR.TYPE.TO_MAIL');
}
if (!ld.isString(subject)) {
throw new TypeError('BACKEND.ERROR.TYPE.SUBJECT_STR');
}
if (!ld.isString(message)) {
throw new TypeError('BACKEND.ERROR.TYPE.MSG_STR');
}
if (!ld.isFunction(callback)) {
throw new TypeError('BACKEND.ERROR.TYPE.CALLBACK_FN');
}
var emailFrom = conf.get('SMTPEmailFrom');
if (!mail.server || !emailFrom) {
err = 'BACKEND.ERROR.CONFIGURATION.MAIL_NOT_CONFIGURED';
return callback(err);
}
var envelope = {
from: emailFrom,
to: to,
subject: subject,
text: message
};
mail.server.send(envelope, callback);
};
/**
* ## init
*
* This function gets configuration option to initialize SMTP connection if
* `SMTPHost` is defined.
*/
mail.init = function () {
if (conf.get('SMTPHost')) {
mail.connect();
}
};
return mail;
}).call(this);