improved logging, verbose/silent opts, and colorized
This commit is contained in:
parent
415ef98ee5
commit
ceec701c35
@ -9,7 +9,8 @@
|
|||||||
"smtp-protocol": "^2",
|
"smtp-protocol": "^2",
|
||||||
"squabble": "^1",
|
"squabble": "^1",
|
||||||
"request": "^2",
|
"request": "^2",
|
||||||
"mailparser": "^0.5"
|
"mailparser": "^0.5",
|
||||||
|
"colors": "^1"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
36
smtp2http.js
36
smtp2http.js
@ -7,14 +7,28 @@ var squabble = require("squabble").createParser(),
|
|||||||
tlsTokens,
|
tlsTokens,
|
||||||
serverOpts = {};
|
serverOpts = {};
|
||||||
|
|
||||||
|
// enable color support
|
||||||
|
require("colors");
|
||||||
|
|
||||||
// setup CLI argument parsing
|
// setup CLI argument parsing
|
||||||
squabble.shortOpts().longOpts().stopper()
|
squabble.shortOpts().longOpts().stopper()
|
||||||
.option("-T", "--tls")
|
.option("-T", "--tls")
|
||||||
|
.flag("-s", "-q", "--silent", "--quiet")
|
||||||
|
.flag("-v", "--verbose")
|
||||||
.required("ENDPOINT");
|
.required("ENDPOINT");
|
||||||
|
|
||||||
// parse arguments
|
// parse arguments
|
||||||
args = squabble.parse();
|
args = squabble.parse();
|
||||||
serverOpts.endpoint = args.named.ENDPOINT;
|
serverOpts.endpoint = args.named.ENDPOINT;
|
||||||
|
|
||||||
|
// configure console output
|
||||||
|
if (args.named["--quiet"]) {
|
||||||
|
console.log = function() {};
|
||||||
|
console.error = function() {};
|
||||||
|
} else if (!args.named["--verbose"]) {
|
||||||
|
console.log = function() {};
|
||||||
|
}
|
||||||
|
|
||||||
if (args.named["--tls"]) {
|
if (args.named["--tls"]) {
|
||||||
tlsTokens = args.named["--tls"].split(":");
|
tlsTokens = args.named["--tls"].split(":");
|
||||||
switch (tlsTokens.length) {
|
switch (tlsTokens.length) {
|
||||||
@ -39,18 +53,34 @@ if (args.named["--tls"]) {
|
|||||||
smtp.createServer(serverOpts, function(req) {
|
smtp.createServer(serverOpts, function(req) {
|
||||||
// accept all incoming messages
|
// accept all incoming messages
|
||||||
req.on("to", function(to, ack) {
|
req.on("to", function(to, ack) {
|
||||||
console.log(to);
|
console.log("incoming message to " + to);
|
||||||
ack.accept();
|
ack.accept();
|
||||||
});
|
});
|
||||||
|
|
||||||
// send message to web endpoint
|
// send message to web endpoint
|
||||||
req.on("message", function(stream, ack) {
|
req.on("message", function(stream, ack) {
|
||||||
stream.pipe(new MailParser().on("end", function(email) {
|
stream.pipe(new MailParser().on("end", function(email) {
|
||||||
|
var msg;
|
||||||
|
|
||||||
http.post({
|
http.post({
|
||||||
url: serverOpts.endpoint,
|
url: serverOpts.endpoint,
|
||||||
json: email
|
json: email
|
||||||
}, function(err) {
|
}, function(err, res, body) {
|
||||||
if (err) console.error(err);
|
if (err) {
|
||||||
|
msg = "error".red + " " + err.message;
|
||||||
|
console.error(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (res.statusCode < 200 || res.statusCode > 299) {
|
||||||
|
msg = res.statusCode + " -----------";
|
||||||
|
console.error(msg.magenta);
|
||||||
|
console.error(body);
|
||||||
|
console.error("---------------".magenta);
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
console.log("passed message successfully");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}).on("error", function(err) {
|
}).on("error", function(err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user