-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[FIX] Direct Reply #22588
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
af1b25f
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into develop
ggazzo 7c855ff
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into develop
ggazzo ee38fbb
WIP
ggazzo 25becbb
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into fix/…
ggazzo 32ed1e0
Fix somehow
ggazzo a5ae909
Fix review
ggazzo 11819ef
Merge branch 'develop' into fix/direct-reply
dougfabris 54ec64f
Merge remote-tracking branch 'origin/develop' into fix/direct-reply
ggazzo 567112e
fix CI
ggazzo f8e7231
ts fix
ggazzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
|
@@ -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, | ||
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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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? 👀