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