Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
152 lines
3.6 KiB
JavaScript
152 lines
3.6 KiB
JavaScript
import POP3Lib from '@rocket.chat/poplib';
|
|
import { simpleParser } from 'mailparser';
|
|
|
|
import { processDirectEmail } from './processDirectEmail';
|
|
import { IMAPInterceptor } from '../../email/IMAPInterceptor';
|
|
import { settings } from '../../settings';
|
|
|
|
export class DirectReplyIMAPInterceptor extends IMAPInterceptor {
|
|
constructor(imapConfig, options = {}) {
|
|
imapConfig = {
|
|
user: settings.get('Direct_Reply_Username'),
|
|
password: settings.get('Direct_Reply_Password'),
|
|
host: settings.get('Direct_Reply_Host'),
|
|
port: settings.get('Direct_Reply_Port'),
|
|
debug: settings.get('Direct_Reply_Debug') ? console.log : false,
|
|
tls: !settings.get('Direct_Reply_IgnoreTLS'),
|
|
...imapConfig,
|
|
};
|
|
|
|
options.deleteAfterRead = settings.get('Direct_Reply_Delete');
|
|
|
|
super(imapConfig, options);
|
|
|
|
this.on('email', (email) => processDirectEmail(email));
|
|
}
|
|
}
|
|
|
|
class POP3Intercepter {
|
|
constructor() {
|
|
this.pop3 = new POP3Lib(settings.get('Direct_Reply_Port'), settings.get('Direct_Reply_Host'), {
|
|
enabletls: !settings.get('Direct_Reply_IgnoreTLS'),
|
|
// debug: settings.get('Direct_Reply_Debug') ? console.log : false,
|
|
debug: console.log,
|
|
});
|
|
|
|
this.totalMsgCount = 0;
|
|
this.currentMsgCount = 0;
|
|
|
|
this.pop3.on('connect', () => {
|
|
console.log('Pop connect');
|
|
this.pop3.login(settings.get('Direct_Reply_Username'), settings.get('Direct_Reply_Password'));
|
|
});
|
|
|
|
this.pop3.on('login', (status) => {
|
|
if (!status) {
|
|
return console.log('Unable to Log-in ....');
|
|
}
|
|
console.log('Pop logged');
|
|
// run on start
|
|
this.pop3.list();
|
|
});
|
|
|
|
// on getting list of all emails
|
|
this.pop3.on('list', (status, msgcount) => {
|
|
if (!status) {
|
|
console.log('Cannot Get Emails ....');
|
|
}
|
|
if (msgcount === 0) {
|
|
return this.pop3.quit();
|
|
}
|
|
|
|
this.totalMsgCount = msgcount;
|
|
this.currentMsgCount = 1;
|
|
// Retrieve email
|
|
this.pop3.retr(this.currentMsgCount);
|
|
});
|
|
|
|
// on retrieved email
|
|
this.pop3.on('retr', async (status, msgnumber, data) => {
|
|
if (!status) {
|
|
return console.log('Cannot Retrieve Message ....');
|
|
}
|
|
|
|
// parse raw email data to JSON object
|
|
simpleParser(data, (err, mail) => {
|
|
processDirectEmail(mail);
|
|
});
|
|
|
|
this.currentMsgCount += 1;
|
|
|
|
// delete email
|
|
this.pop3.dele(msgnumber);
|
|
});
|
|
|
|
// on email deleted
|
|
this.pop3.on('dele', (status) => {
|
|
if (!status) {
|
|
return console.log('Cannot Delete Message....');
|
|
}
|
|
|
|
// get next email
|
|
if (this.currentMsgCount <= this.totalMsgCount) {
|
|
return this.pop3.retr(this.currentMsgCount);
|
|
}
|
|
|
|
// parsed all messages.. so quitting
|
|
this.pop3.quit();
|
|
});
|
|
|
|
// invalid server state
|
|
this.pop3.on('invalid-state', (cmd) => {
|
|
console.log(`Invalid state. You tried calling ${cmd}`);
|
|
});
|
|
|
|
this.pop3.on('error', (cmd) => {
|
|
console.log(`error state. You tried calling ${cmd}`);
|
|
});
|
|
|
|
// locked => command already running, not finished yet
|
|
this.pop3.on('locked', (cmd) => {
|
|
console.log(`Current command has not finished yet. You tried calling ${cmd}`);
|
|
});
|
|
}
|
|
}
|
|
|
|
export class POP3Helper {
|
|
constructor(frequency) {
|
|
this.frequency = frequency;
|
|
this.running = false;
|
|
|
|
this.POP3 = new POP3Intercepter();
|
|
}
|
|
|
|
isActive() {
|
|
return this.running;
|
|
}
|
|
|
|
start() {
|
|
this.log('POP3 started');
|
|
this.running = setInterval(
|
|
() => {
|
|
// get new emails and process
|
|
this.POP3 = new POP3Intercepter();
|
|
},
|
|
Math.max(this.frequency * 60 * 1000, 2 * 60 * 1000),
|
|
);
|
|
}
|
|
|
|
log(...args) {
|
|
console.log(...args);
|
|
}
|
|
|
|
stop(callback = new Function()) {
|
|
this.log('POP3 stop called');
|
|
if (this.isActive()) {
|
|
clearInterval(this.running);
|
|
}
|
|
callback();
|
|
this.log('POP3 stopped');
|
|
}
|
|
}
|