Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,6 @@ dist
# End of https://www.toptal.com/developers/gitignore/api/node

.direnv
*.code-workspace
package-lock.json
*.lock
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "node dist/index.js",
"debug": "node --inspect-brk dist/index.js",
"test": "jest --passWithNoTests",
"sendmail": "echo -e \"Subject: subject\n\ntext\" | msmtp -v --host localhost --port 2525 --from from@test.com to@test.com"
"sendmail": "echo -e \"Subject: subject\n\ntext\" | msmtp -v --host localhost --port 25 --from from@test.com to@test.com"
},
"dependencies": {
"mailparser": "3.9.3",
Expand All @@ -23,7 +23,9 @@
"@types/smtp-server": "3.5.12",
"conventional-changelog-conventionalcommits": "5.0.0",
"cz-conventional-changelog": "3.3.0",
"jest": "29.7.0",
"dotenv": "^17.2.3",
"jest": "^29.7.0",
"nodemon": "^3.1.11",
"semantic-release": "19.0.5",
"ts-jest": "29.4.6",
"ts-node": "10.9.2",
Expand Down
58 changes: 41 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
import { SMTPServer } from 'smtp-server';
import { simpleParser } from 'mailparser';
import * as Push from 'pushover-notifications';

//import * as dotenv from 'dotenv';
//dotenv.config({path: '.env'});
const {
PORT = 25,
PUSHOVER_USER,
PUSHOVER_TOKEN,
} = process.env;
const PUSHOVER_DEFAULT_PRIORITY = process.env.PUSHOVER_DEFAULT_PRIORITY ? parseInt(process.env.PUSHOVER_DEFAULT_PRIORITY, 10) : 0;
const PUSHOVER_DEFAULT_RETRY = process.env.PUSHOVER_DEFAULT_RETRY ? parseInt(process.env.PUSHOVER_DEFAULT_RETRY, 10) : 30;
const PUSHOVER_DEFAULT_EXPIRE = process.env.PUSHOVER_DEFAULT_EXPIRE ? parseInt(process.env.PUSHOVER_DEFAULT_EXPIRE, 10) : 3600;

const server = new SMTPServer({
authOptional: true,
async onData(stream, _session, callback) {
const parsed = await simpleParser(stream);

const subject = (parsed.subject ?? "").trim();
const text = (parsed.text ?? "").trim();

console.log(`Subject -> ${subject}`);
console.log(`Text -> ${text}`);
try {
const parsed = await simpleParser(stream);
const subjectSplit = (parsed.subject ?? "").trim().split(':');
const subject = subjectSplit[0].trim();
const text = (parsed.text ?? "").trim();
const rawPriority = (subjectSplit.length > 1) ? subjectSplit[1]?.trim() : undefined;
let priority: number = subjectSplit.length > 1 ? parseInt(rawPriority ?? `${PUSHOVER_DEFAULT_PRIORITY}`, 10) : PUSHOVER_DEFAULT_PRIORITY as number;
priority = Math.max(-2, Math.min(2, priority || 0)); //Clamp priority between -2 and 2
if(subjectSplit.length > 1 && (isNaN(parseInt(rawPriority,10)) || priority < -2 || priority > 2)) {
console.log(`Invalid priority value provided in subject: ${subjectSplit[1]?.trim()} ... using default priority.`);
}
const push = new Push({
user: PUSHOVER_USER,
token: PUSHOVER_TOKEN,
});
let msg = {
title: subject,
message: text,
priority: priority,
retry: PUSHOVER_DEFAULT_RETRY,
expire: PUSHOVER_DEFAULT_EXPIRE
};

const push = new Push({
user: PUSHOVER_USER,
token: PUSHOVER_TOKEN,
});
console.log(`Subject -> ${subject}`);
console.log(`Text -> ${text}`);
console.log(`Priority -> ${priority}`);
push.send(msg, callback);
} catch (error) {
console.error('Error sending push notification:', error);
callback(error);
}

push.send({
title: subject,
message: text,
}, callback);
},
});

server.listen(PORT, () => console.log(`Listening on port ${PORT}`));
server.listen(PORT, () => {
console.log(`Default Notification Priority: ${PUSHOVER_DEFAULT_PRIORITY}`);
console.log(`Default Notification Retry Attempts: ${PUSHOVER_DEFAULT_RETRY}`);
console.log(`Default Notification Expire Time: ${PUSHOVER_DEFAULT_EXPIRE}`);
console.log(`Listening on port ${PORT}`)
});
Loading