Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / lib / appenders / smtp.js
1 "use strict";
2
3 var layouts = require("../layouts");
4 var mailer = require("nodemailer");
5 var os = require('os');
6
7 var logEventBuffer = [];
8 var subjectLayout;
9 var layout;
10
11 var unsentCount = 0;
12 var shutdownTimeout;
13
14 var sendInterval;
15 var sendTimer;
16
17 var config;
18
19 function sendBuffer() {
20     if (logEventBuffer.length > 0) {
21
22         var transportOpts = getTransportOptions(config);
23         var transport = mailer.createTransport(transportOpts);
24         var firstEvent = logEventBuffer[0];
25         var body = "";
26         var count = logEventBuffer.length;
27         while (logEventBuffer.length > 0) {
28             body += layout(logEventBuffer.shift(), config.timezoneOffset) + "\n";
29         }
30
31         var msg = {
32             to: config.recipients,
33             subject: config.subject || subjectLayout(firstEvent),
34             headers: {"Hostname": os.hostname()}
35         };
36
37         if (true === config.attachment.enable) {
38             msg[config.html ? "html" : "text"] = config.attachment.message;
39             msg.attachments = [
40                 {
41                     filename: config.attachment.filename,
42                     contentType: 'text/x-log',
43                     content: body
44                 }
45             ];
46         } else {
47             msg[config.html ? "html" : "text"] = body;
48         }
49
50         if (config.sender) {
51             msg.from = config.sender;
52         }
53         transport.sendMail(msg, function (error) {
54             if (error) {
55                 console.error("log4js.smtpAppender - Error happened", error);
56             }
57             transport.close();
58             unsentCount -= count;
59         });
60     }
61 }
62
63 function getTransportOptions() {
64     var transportOpts = null;
65     if (config.SMTP) {
66         transportOpts = config.SMTP;
67     } else if (config.transport) {
68         var plugin = config.transport.plugin || 'smtp';
69         var transportModule = 'nodemailer-' + plugin + '-transport';
70         var transporter = require(transportModule);
71         transportOpts = transporter(config.transport.options);
72     }
73
74     return transportOpts;
75 }
76
77 function scheduleSend() {
78     if (!sendTimer) {
79         sendTimer = setTimeout(function () {
80             sendTimer = null;
81             sendBuffer();
82         }, sendInterval);
83     }
84 }
85
86 /**
87  * SMTP Appender. Sends logging events using SMTP protocol. 
88  * It can either send an email on each event or group several 
89  * logging events gathered during specified interval.
90  *
91  * @param _config appender configuration data
92  *    config.sendInterval time between log emails (in seconds), if 0
93  *    then every event sends an email
94  *    config.shutdownTimeout time to give up remaining emails (in seconds; defaults to 5).
95  * @param _layout a function that takes a logevent and returns a string (defaults to basicLayout).
96  */
97 function smtpAppender(_config, _layout) {
98     config = _config;
99
100     if (!config.attachment) {
101         config.attachment = {};
102     }
103
104     config.attachment.enable = !!config.attachment.enable;
105     config.attachment.message = config.attachment.message || "See logs as attachment";
106     config.attachment.filename = config.attachment.filename || "default.log";
107     layout = _layout || layouts.basicLayout;
108     subjectLayout = layouts.messagePassThroughLayout;
109     sendInterval = config.sendInterval * 1000 || 0;
110
111     shutdownTimeout = ('shutdownTimeout' in config ? config.shutdownTimeout : 5) * 1000;
112
113     return function (loggingEvent) {
114         unsentCount++;
115         logEventBuffer.push(loggingEvent);
116         if (sendInterval > 0) {
117             scheduleSend();
118         } else {
119             sendBuffer();
120         }
121     };
122 }
123
124 function configure(_config) {
125     config = _config;
126     if (_config.layout) {
127         layout = layouts.layout(_config.layout.type, _config.layout);
128     }
129     return smtpAppender(_config, layout);
130 }
131
132 function shutdown(cb) {
133     if (shutdownTimeout > 0) {
134         setTimeout(function () {
135             if (sendTimer)
136                 clearTimeout(sendTimer);
137             sendBuffer();
138         }, shutdownTimeout);
139     }
140     (function checkDone() {
141       if (unsentCount > 0) {
142         setTimeout(checkDone, 100);
143       } else {
144         cb();
145       }
146     })();
147 }
148
149 exports.name = "smtp";
150 exports.appender = smtpAppender;
151 exports.configure = configure;
152 exports.shutdown = shutdown;