-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
128 lines (121 loc) · 3.8 KB
/
app.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
'use strict';
const SMTPServer = require('smtp-server').SMTPServer;
const mailParser = require('mailparser').simpleParser;
const nodemailer = require('nodemailer');
const https = require('https');
const fs = require('fs');
var config;
var transporter;
const server = new SMTPServer({
secure: false,
name: "Johnny Bravo",
authOptional: false,
allowInsecureAuth: true,
onConnect(session, callback){
if(session.remoteAddress !== config.nvr.ip){
console.log("onConnect error");
return callback(new Error('Unexpected error 0x0001'));
}
return callback(); // Accept the connection
},
onAuth(auth, session, callback){
if(auth.username !== config.nvr.user || auth.password !== config.nvr.password){
console.log("onAuth error");
return callback(new Error('Invalid username or password'));
}
callback(null, {user: 1}); // where 123 is the user id or similar property
},
onRcptTo(address, session, callback) {
if (address.address === config.nvr.sender) {
console.log("onRcptTo error");
err = new Error('Unexpected error 0x0002');
err.responseCode = 452;
return callback(err);
}
callback();
},
onData(stream, session, callback){
var buffers = [];
stream.on('data', (data) => {
buffers.push(data);
});
stream.on('end', () => {
mailParser(Buffer.concat(buffers), (err, mail) => {
var eventType = 'unknown';
var eventTime = 0;
var cameraName = '';
var channelName = '';
var lines = mail.text.split('\n');
for(var i in lines) {
var parts = lines[i].split(':');
if (parts.length >= 2) {
var item = parts[0];
var data = parts.slice(1).join(':').trim();
switch (item) {
case 'EVENT TYPE': eventType = data; break;
case 'EVENT TIME': eventTime = new Date(data); break;
case 'CAMERA NAME(NUM)':
var j = data.lastIndexOf('(');
if (j && data.endsWith(')')) {
cameraName = data.substring(0, j).trim();
channelName = data.substring(j + 1, data.length - 1).trim();
} else {
cameraName = data;
}
break;
}
if (!!eventType && !!cameraName && !!channelName) {
var body = JSON.stringify({
eventType: eventType,
eventTime: eventTime.getTime(),
cameraName: cameraName,
channelName: channelName
});
var headers = {
'Content-Type': 'application/json',
'Content-Length': body.length
};
var options = {
host: config.st.host,
path: '/api/token/' + config.st.accessToken + '/smartapps/installations/' + config.st.appId + '/event',
port: 443,
method: 'PUT',
headers: headers
};
https.request(options).write(body);
}
}
}
console.log("Event " + eventType + " happened at " + eventTime + ' for camera ' + cameraName + ' on channel ' + channelName);
sendEmail(mail);
});
callback(null, 'Message queued, maybe...');
});
}
});
function sendEmail(mail) {
// setup email data with unicode symbols
let mailOptions = {
from: mail.from.text,
to: mail.to.text,
subject: mail.subject,
text: mail.text,
html: mail.textAsHtml,
attachments: mail.attachments
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
}
function loadConfig() {
fs.readFile(__dirname + '/config.json', function read(err, data) {
config = JSON.parse(data);
transporter = nodemailer.createTransport(config.mail);
server.listen(config.nvr.port, '0.0.0.0');
console.log("Started the NVR SMTP service on port " + config.nvr.port);
});
}
loadConfig();