Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Direct Reply #22588

Merged
merged 10 commits into from
Jun 29, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export function getEmailData({ message, receiver, sender, subscription, room, em
// Reply-To header with format "username+messageId@domain"
email.headers['Reply-To'] = `${replyto.split('@')[0].split(settings.get('Direct_Reply_Separator'))[0]}${settings.get(
'Direct_Reply_Separator',
)}${message._id}@${replyto.split('@')[1]}`;
)}${message.tmid || message._id}@${replyto.split('@')[1]}`;
}

metrics.notificationsSent.inc({ notification_type: 'email' });
Expand Down
194 changes: 83 additions & 111 deletions apps/meteor/app/lib/server/lib/interceptDirectReplyEmails.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Meteor } from 'meteor/meteor';
import POP3Lib from '@rocket.chat/poplib';
import { simpleParser } from 'mailparser';

import { settings } from '../../../settings/server';
import { SystemLogger } from '../../../../server/lib/logger/system';
import { settings } from '../../../settings';
import { IMAPInterceptor } from '../../../../server/email/IMAPInterceptor';
import { processDirectEmail } from '.';

export class IMAPIntercepter extends IMAPInterceptor {
export class DirectReplyIMAPInterceptor extends IMAPInterceptor {
constructor(imapConfig, options = {}) {
imapConfig = {
user: settings.get('Direct_Reply_Username'),
Expand All @@ -23,154 +21,128 @@ export class IMAPIntercepter extends IMAPInterceptor {

super(imapConfig, options);

this.on(
'email',
Meteor.bindEnvironment((email) => processDirectEmail(email)),
);
this.on('email', (email) => processDirectEmail(email));
}
}

export 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: settings.get('Direct_Reply_Debug') ? console.log : false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to keep the comment? 👀

debug: console.log,
});

this.totalMsgCount = 0;
this.currentMsgCount = 0;

this.pop3.on(
'connect',
Meteor.bindEnvironment(() => {
this.pop3.login(settings.get('Direct_Reply_Username'), settings.get('Direct_Reply_Password'));
}),
);

this.pop3.on(
'login',
Meteor.bindEnvironment((status) => {
if (status) {
// run on start
this.pop3.list();
} else {
SystemLogger.info('Unable to Log-in ....');
}
}),
);
this.pop3.on('connect', () => {
console.log('Pop connect');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 could be changed by an actual logger instead of console, same for debug

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',
Meteor.bindEnvironment((status, msgcount) => {
if (status) {
if (msgcount > 0) {
this.totalMsgCount = msgcount;
this.currentMsgCount = 1;
// Retrieve email
this.pop3.retr(this.currentMsgCount);
} else {
this.pop3.quit();
}
} else {
SystemLogger.info('Cannot Get 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',
Meteor.bindEnvironment((status, msgnumber, data) => {
if (status) {
// parse raw email data to JSON object
simpleParser(
data,
Meteor.bindEnvironment((err, mail) => {
this.initialProcess(mail);
}),
);

this.currentMsgCount += 1;

// delete email
this.pop3.dele(msgnumber);
} else {
SystemLogger.info('Cannot Retrieve Message ....');
}
}),
);
this.pop3.on('retr', (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',
Meteor.bindEnvironment((status) => {
if (status) {
// get next email
if (this.currentMsgCount <= this.totalMsgCount) {
this.pop3.retr(this.currentMsgCount);
} else {
// parsed all messages.. so quitting
this.pop3.quit();
}
} else {
SystemLogger.info('Cannot Delete Message....');
}
}),
);
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', function (cmd) {
SystemLogger.info(`Invalid state. You tried calling ${cmd}`);
console.log(`Invalid state. You tried calling ${cmd}`);
});

this.pop3.on('error', function (cmd) {
console.log(`error state. You tried calling ${cmd}`);
});

// locked => command already running, not finished yet
this.pop3.on('locked', function (cmd) {
SystemLogger.info(`Current command has not finished yet. You tried calling ${cmd}`);
console.log(`Current command has not finished yet. You tried calling ${cmd}`);
});
}

initialProcess(mail) {
const email = {
headers: {
'from': mail.from.text,
'to': mail.to.text,
'date': mail.date,
'message-id': mail.messageId,
},
body: mail.text,
};

processDirectEmail(email);
}
}
export let POP3;

export class POP3Helper {
constructor() {
constructor(frequency) {
this.frequency = frequency;
this.running = false;
}

start() {
// run every x-minutes
if (settings.get('Direct_Reply_Frequency')) {
POP3 = new POP3Intercepter();

this.running = Meteor.setInterval(() => {
// get new emails and process
POP3 = new POP3Intercepter();
}, Math.max(settings.get('Direct_Reply_Frequency') * 60 * 1000, 2 * 60 * 1000));
}
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()) {
Meteor.clearInterval(this.running);
clearInterval(this.running);
}
callback();
this.log('POP3 stopped');
}
}
128 changes: 0 additions & 128 deletions apps/meteor/app/lib/server/lib/processDirectEmail.js

This file was deleted.

Loading