This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 264
/
restmail.js
47 lines (40 loc) · 1.5 KB
/
restmail.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
const utils = require("./utils.js"),
request = require('request');
const DEFAULT_POLL = 1000;
const DEFAULT_TIMEOUT = 20000;
// get a randomly generated restmail email
exports.randomEmail = function(chars, domain) {
var str = "";
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i=0; i < chars; i++) {
str += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
}
// Until GH-2551 is fixed, use lowercase alphanum for email
str = str.toLowerCase();
return str + '@' + (domain ? domain : 'restmail.net');
};
// poll restmail to attempt to fetch a verification link,
// args include .timeout (optional), .poll (optional), .index (optional)
// and .email (required)
// callback is cb(error, verificationURL, fullEmail)
exports.getVerificationLink = function(args, cb) {
var poll = args.poll || DEFAULT_POLL;
var timeout = args.timeout || DEFAULT_TIMEOUT;
var email = args.email;
var index = args.index || 0;
var url = 'http://restmail.net/mail/' + email.split('@')[0];
utils.waitFor(poll, timeout, function(doneCB) {
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var b = JSON.parse(body);
var message = b[index];
if (message && message.headers['x-browserid-verificationurl']) {
doneCB(true, error, message.headers['x-browserid-verificationurl'], message);
}
else doneCB(false);
} else {
doneCB(false);
}
})
}, cb);
};