-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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
[NEW] Integrated personal email gateway (GSoC'17) #7342
Changes from 1 commit
5e8a23a
a39ca5e
c5e9905
eaac7fb
61aa422
d428eb0
c818311
df8b8da
8883de3
da8e0a4
585623b
75ebf82
5b7c0f6
06ba639
3ea675c
2b64c84
cc32ba4
b4e80b7
1ab89bc
b60b1f1
3219105
41aaae5
8bbe667
923103e
4128ae6
9e7d70f
e4bbcb7
feb8287
6260033
0bbda5e
beff246
e72ba62
d73629d
00d4810
6d08dbc
953d1e2
252279e
b400c39
3048a02
03a7d1c
9a7dfd3
72dc724
fb58ea8
7bc6631
1d7f266
88e0978
3350e43
3e20101
aed2ab8
d2296d6
98a820d
8ef220c
01262cc
c93aecf
aae9e98
3517ae0
747d503
3bd791a
70c5c27
d1573c5
c3a9a5e
af1b739
9909728
7724061
3646237
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import IMAP from 'imap'; | ||
import POP3 from 'poplib'; | ||
import {simpleParser as simpleParser} from 'mailparser-node4'; | ||
|
||
class IMAPIntercepter { | ||
constructor() { | ||
|
@@ -97,6 +99,10 @@ class IMAPIntercepter { | |
email.body = bodyBuffer; | ||
} else { | ||
email.headers = IMAP.parseHeader(headerBuffer); | ||
|
||
email.headers.to = email.headers.to[0]; | ||
email.headers.date = email.headers.date[0]; | ||
email.headers.from = email.headers.from[0]; | ||
} | ||
}); | ||
}); | ||
|
@@ -114,4 +120,130 @@ class IMAPIntercepter { | |
} | ||
} | ||
|
||
class POP3Intercepter { | ||
constructor() { | ||
this.pop3 = new POP3(RocketChat.settings.get('Direct_Reply_Port'), RocketChat.settings.get('Direct_Reply_Host'), { | ||
enabletls: !RocketChat.settings.get('Direct_Reply_IgnoreTLS'), | ||
debug: RocketChat.settings.get('Direct_Reply_Debug') ? console.log : false | ||
}); | ||
|
||
this.totalMsgCount = 0; | ||
this.currentMsgCount = 0; | ||
|
||
this.pop3.on('connect', Meteor.bindEnvironment(() => { | ||
this.pop3.login(RocketChat.settings.get('Direct_Reply_Username'), RocketChat.settings.get('Direct_Reply_Password')); | ||
})); | ||
|
||
this.pop3.on('login', Meteor.bindEnvironment((status, rawData) => { | ||
if (status) { | ||
// run on start | ||
this.pop3.list(); | ||
} else { | ||
console.log('Unable to Log-in ....'); | ||
} | ||
})); | ||
|
||
// on getting list of all emails | ||
this.pop3.on('list', Meteor.bindEnvironment((status, msgcount, msgnumber, data, rawdata) => { | ||
if (status) { | ||
if (msgcount > 0) { | ||
this.totalMsgCount = msgcount; | ||
this.currentMsgCount = 1; | ||
// Retrieve email | ||
this.pop3.retr(this.currentMsgCount); | ||
} else { | ||
this.pop3.quit(); | ||
} | ||
} else { | ||
console.log('Cannot Get Emails ....'); | ||
} | ||
})); | ||
|
||
// on retrieved email | ||
this.pop3.on('retr', Meteor.bindEnvironment((status, msgnumber, data, rawdata) => { | ||
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 { | ||
console.log('Cannot Retrieve Message ....'); | ||
} | ||
})); | ||
|
||
// on email deleted | ||
this.pop3.on('dele', Meteor.bindEnvironment((status, msgnumber, data, rawdata) => { | ||
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 { | ||
console.log('Cannot Delete Message ....'); | ||
} | ||
})); | ||
|
||
// invalid server state | ||
this.pop3.on('invalid-state', function(cmd) { | ||
console.log(`Invalid state. You tried calling ${ cmd }`); | ||
}); | ||
|
||
// locked => command already running, not finished yet | ||
this.pop3.on('locked', function(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 | ||
}; | ||
|
||
RocketChat.processDirectEmail(email); | ||
} | ||
} | ||
|
||
class POP3Helper { | ||
constructor() { | ||
this.running = false; | ||
} | ||
|
||
start() { | ||
// run every x-minutes | ||
if (RocketChat.settings.get('Direct_Reply_Frequency')) { | ||
this.running = Meteor.setInterval(() => { | ||
// get new emails and process | ||
RocketChat.POP3 = new RocketChat.POP3Intercepter(); | ||
}, RocketChat.settings.get('Direct_Reply_Frequency')*60*1000); | ||
} | ||
} | ||
|
||
isActive() { | ||
return this.running; | ||
} | ||
|
||
stop(callback = new Function) { | ||
if (this.isActive()) { | ||
Meteor.clearInterval(this.running); | ||
} | ||
callback(); | ||
} | ||
} | ||
|
||
RocketChat.IMAPIntercepter = IMAPIntercepter; | ||
RocketChat.POP3Intercepter = POP3Intercepter; | ||
RocketChat.POP3Helper = new POP3Helper(); | ||
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. Lets start using export and import instead of global variables? Let me know if you need help with that |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,25 +3,60 @@ const startEmailIntercepter = _.debounce(Meteor.bindEnvironment(function() { | |
|
||
if (RocketChat.settings.get('Direct_Reply_Enable') && RocketChat.settings.get('Direct_Reply_Protocol') && RocketChat.settings.get('Direct_Reply_Host') && RocketChat.settings.get('Direct_Reply_Port') && RocketChat.settings.get('Direct_Reply_Username') && RocketChat.settings.get('Direct_Reply_Password')) { | ||
if (RocketChat.settings.get('Direct_Reply_Protocol') === 'IMAP') { | ||
// stop already running instance | ||
// stop already running IMAP instance | ||
if (RocketChat.IMAP && RocketChat.IMAP.isActive()) { | ||
console.log('Disconnecting already running instance...'); | ||
console.log('Disconnecting already running IMAP instance...'); | ||
RocketChat.IMAP.stop(Meteor.bindEnvironment(function() { | ||
console.log('Starting new instance......'); | ||
console.log('Starting new IMAP instance......'); | ||
RocketChat.IMAP = new RocketChat.IMAPIntercepter(); | ||
RocketChat.IMAP.start(); | ||
return true; | ||
})); | ||
} else if (!RocketChat.IMAP || !RocketChat.IMAP.isActive()) { | ||
console.log('Starting new instance......'); | ||
} else if (RocketChat.POP3 && RocketChat.POP3.isActive()) { | ||
console.log('Disconnecting already running POP instance...'); | ||
RocketChat.POP3Helper.stop(Meteor.bindEnvironment(function() { | ||
console.log('Starting new IMAP instance......'); | ||
RocketChat.IMAP = new RocketChat.IMAPIntercepter(); | ||
RocketChat.IMAP.start(); | ||
return true; | ||
})); | ||
} else { | ||
console.log('Starting new IMAP instance......'); | ||
RocketChat.IMAP = new RocketChat.IMAPIntercepter(); | ||
RocketChat.IMAP.start(); | ||
return true; | ||
} | ||
} else if (RocketChat.settings.get('Direct_Reply_Protocol') === 'POP') { | ||
// stop already running POP instance | ||
if (RocketChat.POP3 && RocketChat.POP3.isActive()) { | ||
console.log('Disconnecting already running POP instance...'); | ||
RocketChat.POP3Helper.stop(Meteor.bindEnvironment(function() { | ||
console.log('Starting new POP instance......'); | ||
RocketChat.POP3 = new RocketChat.POP3Intercepter(); | ||
RocketChat.POP3Helper.start(); | ||
return true; | ||
})); | ||
} else if (RocketChat.IMAP && RocketChat.IMAP.isActive()) { | ||
console.log('Disconnecting already running IMAP instance...'); | ||
RocketChat.IMAP.stop(Meteor.bindEnvironment(function() { | ||
console.log('Starting new POP instance......'); | ||
RocketChat.POP3 = new RocketChat.POP3Intercepter(); | ||
RocketChat.POP3Helper.start(); | ||
return true; | ||
})); | ||
} else { | ||
console.log('Starting new POP instance......'); | ||
RocketChat.POP3 = new RocketChat.POP3Intercepter(); | ||
RocketChat.POP3Helper.start(); | ||
return true; | ||
} | ||
} | ||
} else if (RocketChat.IMAP && RocketChat.IMAP.isActive()) { | ||
// stop IMAP instance | ||
RocketChat.IMAP.stop(); | ||
} else if (RocketChat.POP3 && RocketChat.POP3.isActive()) { | ||
// stop POP3 instance | ||
RocketChat.POP3Helper.stop(); | ||
} | ||
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. On else you can stop the service if it exists, then you don't need the stop button |
||
}), 1000); | ||
|
||
|
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.
Why are you using this syntax, you are not changing the variable name 😄