Skip to content

Commit

Permalink
Fix SMTP authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunoo committed Jul 31, 2020
1 parent 864d92b commit 7fd090e
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:

steps:
- uses: actions/checkout@v2.3.1
with:
ref: beta
- uses: actions/setup-node@v2.1.1
with:
node-version: 10
Expand Down
119 changes: 98 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"displayName": "Homebridge SMTP Motion",
"name": "homebridge-smtp-motion",
"version": "1.1.0",
"version": "1.1.1",
"description": "Converts SMTP messages into HTTP motion alerts that homebridge-camera-ffmpeg understands",
"main": "dist/index.js",
"repository": {
Expand All @@ -25,8 +25,8 @@
"clean": "rimraf ./dist ./coverage",
"build": "rimraf ./dist ./coverage && tsc",
"format": "markdownlint --fix *.md",
"lint": "eslint **/*.ts --fix",
"lint-check": "eslint **/*.ts",
"lint": "eslint src/*.ts --fix",
"lint-check": "eslint src/*.ts",
"prepare": "npm run build",
"prepublishOnly": "npm run lint-check",
"postpublish": "npm run clean",
Expand All @@ -35,7 +35,8 @@
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
"@types/node": "14.0.27",
"@types/bunyan": "^1.8.6",
"@types/node": "14.0.26",
"@types/smtp-server": "3.5.4",
"eslint": "^7.5.0",
"homebridge": "^1.1.1",
Expand All @@ -55,6 +56,7 @@
"homebridge": ">=1.0.0"
},
"dependencies": {
"bunyan": ">=1.8.14",
"smtp-server": ">=3.7.0"
}
}
28 changes: 24 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {
PlatformAccessory,
PlatformConfig
} from 'homebridge';
import { SMTPServer, SMTPServerDataStream, SMTPServerSession } from 'smtp-server';
import Bunyan from 'bunyan';
import http from 'http';
import { SMTPServer } from 'smtp-server';
import Stream from 'stream';
import { SmtpMotionPlatformConfig } from './configTypes';

const PLUGIN_NAME = 'homebridge-smtp-motion';
Expand All @@ -32,17 +34,35 @@ class SmtpMotionPlatform implements DynamicPlatformPlugin {
const smtpPort = this.config.smtp_port || 2525;
const httpPort = this.config.http_port || 8080;
const log = this.log;
const logStream = new Stream.Writable({
write: (chunk: string, encoding: BufferEncoding, callback): void => {
const data = JSON.parse(chunk);
this.log.debug('[SMTP Server] ' + data.msg);
callback();
}
});
const bunyanLog = Bunyan.createLogger({
name: 'ftp',
streams: [{
stream: logStream
}]
});
const server = new SMTPServer({authOptional: true,
onData(stream: SMTPServerDataStream, session: SMTPServerSession, callback: (err?: Error | null) => void): void {
disabledCommands: ['STARTTLS'],
logger: bunyanLog,
onAuth(auth, session, callback): void {
callback(null, { user: true });
},
onData(stream, session, callback): void {
stream.on('data', () => {}); // eslint-disable-line @typescript-eslint/no-empty-function
stream.on('end', callback);
session.envelope.rcptTo.forEach((rcptTo) => {
const name = rcptTo.address.split('@')[0].replace(/\+/g, ' ');
log.debug(name + ' Motion Detected!');
log('[' + name + '] Email received.');
try {
http.get('http://127.0.0.1:' + httpPort + '/motion?' + name);
} catch (ex) {
log.error(name + ': Error making HTTP call: ' + ex);
log.error('[' + name + '] Error making HTTP call: ' + ex);
}
});
}
Expand Down

0 comments on commit 7fd090e

Please sign in to comment.