Skip to content

Commit

Permalink
Add methods to fetch actual messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravjuvekar committed Jul 31, 2022
1 parent 37cf865 commit 2243e41
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
11 changes: 9 additions & 2 deletions doc/src/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ Welcome to remote-email-filtering's documentation!
:maxdepth: 2
:caption: Contents:

.. automodule:: remote_email_filtering.action
:members:


Remote
------
.. automodule:: remote_email_filtering.remote
:members:

Action
------
.. automodule:: remote_email_filtering.action
:members:


Indices and tables
==================

Expand Down
10 changes: 9 additions & 1 deletion src/remote_email_filtering/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
from pprint import pp


def filter_message(message, filters):
pass


def start_filtering(remote,
filter_map=dict(),
interval=datetime.timedelta(seconds=5)):
while True:
pp(list(remote.list_dirs()))
for dir in remote.list_dirs():
if dir in filter_map:
for message in remote.get_messages(dir):
filter_message(message, filter_map[dir])

time.sleep(interval.seconds)
39 changes: 39 additions & 0 deletions src/remote_email_filtering/message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import email
import email.header


class Message(object):
def __init__(self, rfc822_bytes):
self.raw = rfc822_bytes
self.mail = email.message_from_bytes(self.raw)

@property
def To(self):
ret = self.mail['To']
if ret is None:
ret = ''
return ret

@property
def Recipients(self):
return ', '.join(_ for _ in (self.mail['To'], self.mail['CC'])
if _ is not None)

@property
def Subject(self):
return self.mail['Subject']

@property
def SaneSubject(self):
ret = self.mail['Subject']
ret = email.header.decode_header(ret)[0]
if ret[1] is not None:
ret = ret[0].decode('ascii', errors='replace')
else:
ret = ret[0]
ret = ret.replace('\n', '')
return ret

@property
def From(self):
return self.mail['From']
26 changes: 26 additions & 0 deletions src/remote_email_filtering/remote.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import abc
import imapclient

from . import message


class Remote(abc.ABC):
@abc.abstractmethod
def list_dirs(self):
pass

@abc.abstractmethod
def list_message_ids(self, dir):
pass

@abc.abstractmethod
def fetch_message(self, message_id):
pass

def get_messages(self, dir):
for msg_id in self.list_message_ids(dir):
message = self.fetch_message(msg_id)
yield message


class Imap(Remote):
def __init__(self, host, user, token, **kwargs):
Expand All @@ -18,3 +33,14 @@ def list_dirs(self):
for flags, delim, name in self.connection.list_folders():
name_components = tuple(name.split(delim.decode()))
yield name_components

def list_message_ids(self, dir):
self.connection.select_folder('/'.join(dir))
return self.connection.search()

def fetch_message(self, msg_id):
msg = self.connection.fetch(msg_id, ['FLAGS', 'INTERNALDATE',
'ENVELOPE', 'RFC822'])
msg = msg[msg_id]
msg = message.Message(msg[b'RFC822'])
return msg
5 changes: 4 additions & 1 deletion utils/list_dirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ def main():
remote = remote_email_filtering.remote.Imap(host=args.HOST,
user=args.USER,
token=creds['token'])
filters = {
('INBOX',): [lambda x: x, ],
}

remote_email_filtering.main.start_filtering(remote)
remote_email_filtering.main.start_filtering(remote, filters)


if __name__ == '__main__':
Expand Down

0 comments on commit 2243e41

Please sign in to comment.