Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12a8cd9a41 | |||
| 70b0552df4 | |||
| b2f82b6fb7 | |||
| 76fc7bfe8c | |||
| 0f8abf3e4a | |||
| fee17fbc76 | |||
| 27d19a209a | |||
| 0a94f2e4a4 | |||
| 810e6a0cd8 | |||
|
|
782d3977f9 | ||
|
|
c21f2ec9f7 | ||
|
|
1f0b952fbc | ||
|
|
d0b3fa495e | ||
|
|
038094e16d | ||
|
|
e1eb91b689 | ||
|
|
dbdc96f91e | ||
|
|
4dc43d2ba2 | ||
|
|
fd35764586 | ||
|
|
acfd162c8a | ||
|
|
9dbf9a9071 | ||
|
|
0249d59a73 | ||
|
|
1ad18477f1 | ||
|
|
079569dac9 | ||
|
|
7ea534fbda | ||
|
|
db30a8044f | ||
|
|
d5a4e47904 | ||
|
|
330a989452 | ||
|
|
aa6739d91f | ||
| f82ce52220 | |||
| 87e42a9fdd | |||
| 86cf1fbf9b | |||
| 6f3cc1ab92 | |||
| 1fbb8511b9 | |||
|
|
adf70cbed5 | ||
|
|
c04c340bb7 | ||
|
|
b52d43c496 | ||
|
|
a8440f60ca | ||
|
|
c86cbe766d | ||
|
|
16a05e15e5 | ||
| 7d11b590d8 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -34,3 +34,6 @@ example.py
|
|||||||
|
|
||||||
# PyCharm
|
# PyCharm
|
||||||
.idea/
|
.idea/
|
||||||
|
|
||||||
|
# Mac
|
||||||
|
.DS_Store
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
language: python
|
language: python
|
||||||
python:
|
python:
|
||||||
- "3.2"
|
|
||||||
- "3.3"
|
- "3.3"
|
||||||
- "3.4"
|
- "3.4"
|
||||||
- "3.5"
|
- "3.5"
|
||||||
|
|||||||
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"python.pythonPath": "/usr/local/bin/python3"
|
||||||
|
}
|
||||||
32
CHANGELOG.md
32
CHANGELOG.md
@@ -1,22 +1,32 @@
|
|||||||
|
## 0.9.6 (14 August 2018)
|
||||||
|
|
||||||
|
IMPROVEMENTS:
|
||||||
|
|
||||||
|
* Vendors package, adding provider specific functionality ([#139](https://github.com/martinrusev/imbox/pull/139)) - Contributed by @zevaverbach
|
||||||
|
* Type hints for every method and function ([#136](https://github.com/martinrusev/imbox/pull/136)) - Contributed by @zevaverbach
|
||||||
|
* Move all code out of __init__.py and into a separate module ([#130](https://github.com/martinrusev/imbox/pull/130)) - Contributed by @zevaverbach
|
||||||
|
* Enhance `messages' generator: ([#129](https://github.com/martinrusev/imbox/pull/129)) - Contributed by @zevaverbach
|
||||||
|
|
||||||
|
|
||||||
## 0.9.5 (5 December 2017)
|
## 0.9.5 (5 December 2017)
|
||||||
|
|
||||||
IMPROVEMENTS:
|
IMPROVEMENTS:
|
||||||
|
|
||||||
* `date__on` support: ([#109](https://github.com/martinrusev/imbox/pull/109))
|
* `date__on` support: ([#109](https://github.com/martinrusev/imbox/pull/109)) - Contributed by @balsagoth
|
||||||
* Starttls support: ([#108](https://github.com/martinrusev/imbox/pull/108))
|
* Starttls support: ([#108](https://github.com/martinrusev/imbox/pull/108)) - Contributed by @balsagoth
|
||||||
* Mark emails as flagged/starred: ([#107](https://github.com/martinrusev/imbox/pull/107))
|
* Mark emails as flagged/starred: ([#107](https://github.com/martinrusev/imbox/pull/107)) - Contributed by @memanikantan
|
||||||
* Messages filter can use date objects instead of stringified dates: ([#104](https://github.com/martinrusev/imbox/pull/104))
|
* Messages filter can use date objects instead of stringified dates: ([#104](https://github.com/martinrusev/imbox/pull/104)) - Contributed by @sblondon
|
||||||
* Fix attachment parsing when a semicolon character ends the Content-Disposition line: ([#100](https://github.com/martinrusev/imbox/pull/100))
|
* Fix attachment parsing when a semicolon character ends the Content-Disposition line: ([#100](https://github.com/martinrusev/imbox/pull/100)) - Contributed by @sblondon
|
||||||
* Parsing - UnicecodeDecodeError() fixes: ([#96](https://github.com/martinrusev/imbox/pull/96))
|
* Parsing - UnicecodeDecodeError() fixes: ([#96](https://github.com/martinrusev/imbox/pull/96)) - Contributed by @am0z
|
||||||
* Imbox() `with` support: ([#92](https://github.com/martinrusev/imbox/pull/92))
|
* Imbox() `with` support: ([#92](https://github.com/martinrusev/imbox/pull/92)) - Contributed by @sblondon
|
||||||
|
|
||||||
|
|
||||||
## 0.9 (18 September 2017)
|
## 0.9 (18 September 2017)
|
||||||
|
|
||||||
IMPROVEMENTS:
|
IMPROVEMENTS:
|
||||||
|
|
||||||
* Permissively Decode Emails: ([#78](https://github.com/martinrusev/imbox/pull/78))
|
* Permissively Decode Emails: ([#78](https://github.com/martinrusev/imbox/pull/78)) - Contributed by @AdamNiederer
|
||||||
* "With" statement for automatic cleanup/logout ([#92](https://github.com/martinrusev/imbox/pull/92))
|
* "With" statement for automatic cleanup/logout ([#92](https://github.com/martinrusev/imbox/pull/92)) - Contributed by @sblondon
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -24,7 +34,7 @@ IMPROVEMENTS:
|
|||||||
|
|
||||||
IMPROVEMENTS:
|
IMPROVEMENTS:
|
||||||
|
|
||||||
* Add support for Python 3.3+ Parsing policies: ([#75](https://github.com/martinrusev/imbox/pull/75))
|
* Add support for Python 3.3+ Parsing policies: ([#75](https://github.com/martinrusev/imbox/pull/75)) - Contributed by @bhtucker
|
||||||
|
|
||||||
BACKWARDS INCOMPATIBILITIES / NOTES:
|
BACKWARDS INCOMPATIBILITIES / NOTES:
|
||||||
|
|
||||||
@@ -35,4 +45,4 @@ BACKWARDS INCOMPATIBILITIES / NOTES:
|
|||||||
|
|
||||||
IMPROVEMENTS:
|
IMPROVEMENTS:
|
||||||
|
|
||||||
* ssl_context: Check SSLContext for IMAP4_SSL connections ([#69](https://github.com/martinrusev/imbox/pull/69))
|
* ssl_context: Check SSLContext for IMAP4_SSL connections ([#69](https://github.com/martinrusev/imbox/pull/69)) - Contributed by @dmth
|
||||||
|
|||||||
10
README.rst
10
README.rst
@@ -73,15 +73,19 @@ Usage
|
|||||||
# Messages received on a specific date
|
# Messages received on a specific date
|
||||||
inbox_messages_received_on_date = imbox.messages(date__on=datetime.date(2018, 7, 30))
|
inbox_messages_received_on_date = imbox.messages(date__on=datetime.date(2018, 7, 30))
|
||||||
|
|
||||||
# Messages from a specific folder
|
|
||||||
messages_from_folder = imbox.messages(folder='Social')
|
|
||||||
|
|
||||||
# Messages whose subjects contain a string
|
# Messages whose subjects contain a string
|
||||||
inbox_messages_subject_christmas = imbox.messages(subject='Christmas')
|
inbox_messages_subject_christmas = imbox.messages(subject='Christmas')
|
||||||
|
|
||||||
|
# Messages whose UID is greater than 1050
|
||||||
|
inbox_messages_uids_greater_than_1050 = imbox.messages(uid__range='1050:*')
|
||||||
|
|
||||||
# Messages from a specific folder
|
# Messages from a specific folder
|
||||||
messages_in_folder_social = imbox.messages(folder='Social')
|
messages_in_folder_social = imbox.messages(folder='Social')
|
||||||
|
|
||||||
|
# Some of Gmail's IMAP Extensions are supported (label and raw):
|
||||||
|
all_messages_with_an_attachment_from_martin = imbox.messages(folder='all', raw='from:martin@amon.cx has:attachment')
|
||||||
|
all_messages_labeled_finance = imbox.messages(folder='all', label='finance')
|
||||||
|
|
||||||
for uid, message in all_inbox_messages:
|
for uid, message in all_inbox_messages:
|
||||||
# Every message is an object with the following keys
|
# Every message is an object with the following keys
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
from imbox.imbox import Imbox
|
from imbox.imbox import Imbox
|
||||||
|
|
||||||
__version_info__ = (0, 9, 5)
|
__version_info__ = (0, 9, 6)
|
||||||
__version__ = '.'.join([str(x) for x in __version_info__])
|
__version__ = '.'.join([str(x) for x in __version_info__])
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['Imbox']
|
__all__ = ['Imbox']
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,44 @@
|
|||||||
|
import imaplib
|
||||||
|
|
||||||
from imbox.imap import ImapTransport
|
from imbox.imap import ImapTransport
|
||||||
from imbox.messages import Messages
|
from imbox.messages import Messages
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from imbox.vendors import GmailMessages, hostname_vendorname_dict, name_authentication_string_dict
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Imbox:
|
class Imbox:
|
||||||
|
|
||||||
|
authentication_error_message = None
|
||||||
|
|
||||||
def __init__(self, hostname, username=None, password=None, ssl=True,
|
def __init__(self, hostname, username=None, password=None, ssl=True,
|
||||||
port=None, ssl_context=None, policy=None, starttls=False):
|
port=None, ssl_context=None, policy=None, starttls=False,
|
||||||
|
vendor=None):
|
||||||
|
|
||||||
self.server = ImapTransport(hostname, ssl=ssl, port=port,
|
self.server = ImapTransport(hostname, ssl=ssl, port=port,
|
||||||
ssl_context=ssl_context, starttls=starttls)
|
ssl_context=ssl_context, starttls=starttls)
|
||||||
|
|
||||||
self.hostname = hostname
|
self.hostname = hostname
|
||||||
self.username = username
|
self.username = username
|
||||||
self.password = password
|
self.password = password
|
||||||
self.parser_policy = policy
|
self.parser_policy = policy
|
||||||
self.connection = self.server.connect(username, password)
|
self.vendor = vendor or hostname_vendorname_dict.get(self.hostname)
|
||||||
|
|
||||||
|
if self.vendor is not None:
|
||||||
|
self.authentication_error_message = name_authentication_string_dict.get(
|
||||||
|
self.vendor)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.connection = self.server.connect(username, password)
|
||||||
|
except imaplib.IMAP4.error as e:
|
||||||
|
if self.authentication_error_message is None:
|
||||||
|
raise
|
||||||
|
raise imaplib.IMAP4.error(
|
||||||
|
self.authentication_error_message + '\n' + str(e))
|
||||||
|
|
||||||
logger.info("Connected to IMAP Server with user {username} on {hostname}{ssl}".format(
|
logger.info("Connected to IMAP Server with user {username} on {hostname}{ssl}".format(
|
||||||
hostname=hostname, username=username, ssl=(" over SSL" if ssl or starttls else "")))
|
hostname=hostname, username=username, ssl=(" over SSL" if ssl or starttls else "")))
|
||||||
|
|
||||||
@@ -42,31 +63,42 @@ class Imbox:
|
|||||||
self.connection.uid('STORE', uid, '+FLAGS', '(\\Flagged)')
|
self.connection.uid('STORE', uid, '+FLAGS', '(\\Flagged)')
|
||||||
|
|
||||||
def delete(self, uid):
|
def delete(self, uid):
|
||||||
logger.info("Mark UID {} with \\Deleted FLAG and expunge.".format(int(uid)))
|
logger.info(
|
||||||
|
"Mark UID {} with \\Deleted FLAG and expunge.".format(int(uid)))
|
||||||
self.connection.expunge()
|
self.connection.expunge()
|
||||||
|
|
||||||
def copy(self, uid, destination_folder):
|
def copy(self, uid, destination_folder):
|
||||||
logger.info("Copy UID {} to {} folder".format(int(uid), str(destination_folder)))
|
logger.info("Copy UID {} to {} folder".format(
|
||||||
|
int(uid), str(destination_folder)))
|
||||||
return self.connection.uid('COPY', uid, destination_folder)
|
return self.connection.uid('COPY', uid, destination_folder)
|
||||||
|
|
||||||
def move(self, uid, destination_folder):
|
def move(self, uid, destination_folder):
|
||||||
logger.info("Move UID {} to {} folder".format(int(uid), str(destination_folder)))
|
logger.info("Move UID {} to {} folder".format(
|
||||||
|
int(uid), str(destination_folder)))
|
||||||
if self.copy(uid, destination_folder):
|
if self.copy(uid, destination_folder):
|
||||||
self.delete(uid)
|
self.delete(uid)
|
||||||
|
|
||||||
def messages(self, **kwargs):
|
def messages(self, **kwargs):
|
||||||
folder = kwargs.get('folder', False)
|
folder = kwargs.get('folder', False)
|
||||||
|
|
||||||
|
messages_class = Messages
|
||||||
|
|
||||||
|
if self.vendor == 'gmail':
|
||||||
|
messages_class = GmailMessages
|
||||||
|
|
||||||
if folder:
|
if folder:
|
||||||
self.connection.select(folder)
|
self.connection.select(
|
||||||
|
messages_class.FOLDER_LOOKUP.get((folder.lower())) or folder)
|
||||||
msg = " from folder '{}'".format(folder)
|
msg = " from folder '{}'".format(folder)
|
||||||
|
del kwargs['folder']
|
||||||
else:
|
else:
|
||||||
msg = " from inbox"
|
msg = " from inbox"
|
||||||
|
|
||||||
logger.info("Fetch list of messages{}".format(msg))
|
logger.info("Fetch list of messages{}".format(msg))
|
||||||
return Messages(connection=self.connection,
|
|
||||||
parser_policy=self.parser_policy,
|
return messages_class(connection=self.connection,
|
||||||
**kwargs)
|
parser_policy=self.parser_policy,
|
||||||
|
**kwargs)
|
||||||
|
|
||||||
def folders(self):
|
def folders(self):
|
||||||
return self.connection.list()
|
return self.connection.list()
|
||||||
|
|||||||
@@ -1,13 +1,30 @@
|
|||||||
from imbox.parser import fetch_email_by_uid
|
import datetime
|
||||||
from imbox.query import build_search_query
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from imbox.query import build_search_query
|
||||||
|
from imbox.parser import fetch_email_by_uid
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Messages:
|
class Messages:
|
||||||
|
|
||||||
|
IMAP_ATTRIBUTE_LOOKUP = {
|
||||||
|
'unread': '(UNSEEN)',
|
||||||
|
'flagged': '(FLAGGED)',
|
||||||
|
'unflagged': '(UNFLAGGED)',
|
||||||
|
'sent_from': '(FROM "{}")',
|
||||||
|
'sent_to': '(TO "{}")',
|
||||||
|
'date__gt': '(SINCE "{}")',
|
||||||
|
'date__lt': '(BEFORE "{}")',
|
||||||
|
'date__on': '(ON "{}")',
|
||||||
|
'subject': '(SUBJECT "{}")',
|
||||||
|
'uid__range': '(UID {})',
|
||||||
|
}
|
||||||
|
|
||||||
|
FOLDER_LOOKUP = {}
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
connection,
|
connection,
|
||||||
parser_policy,
|
parser_policy,
|
||||||
@@ -26,8 +43,8 @@ class Messages:
|
|||||||
parser_policy=self.parser_policy)
|
parser_policy=self.parser_policy)
|
||||||
|
|
||||||
def _query_uids(self, **kwargs):
|
def _query_uids(self, **kwargs):
|
||||||
query_ = build_search_query(**kwargs)
|
query_ = build_search_query(self.IMAP_ATTRIBUTE_LOOKUP, **kwargs)
|
||||||
message, data = self.connection.uid('search', None, query_)
|
_, data = self.connection.uid('search', None, query_)
|
||||||
if data[0] is None:
|
if data[0] is None:
|
||||||
return []
|
return []
|
||||||
return data[0].split()
|
return data[0].split()
|
||||||
|
|||||||
@@ -60,7 +60,8 @@ def get_mail_addresses(message, header_name):
|
|||||||
for index, (address_name, address_email) in enumerate(addresses):
|
for index, (address_name, address_email) in enumerate(addresses):
|
||||||
addresses[index] = {'name': decode_mail_header(address_name),
|
addresses[index] = {'name': decode_mail_header(address_name),
|
||||||
'email': address_email}
|
'email': address_email}
|
||||||
logger.debug("{} Mail address in message: <{}> {}".format(header_name.upper(), address_name, address_email))
|
logger.debug("{} Mail address in message: <{}> {}".format(
|
||||||
|
header_name.upper(), address_name, address_email))
|
||||||
return addresses
|
return addresses
|
||||||
|
|
||||||
|
|
||||||
@@ -111,7 +112,8 @@ def parse_attachment(message_part):
|
|||||||
name, value = decode_param(param)
|
name, value = decode_param(param)
|
||||||
|
|
||||||
if 'file' in name:
|
if 'file' in name:
|
||||||
attachment['filename'] = value[1:-1] if value.startswith('"') else value
|
attachment['filename'] = value[1:-
|
||||||
|
1] if value.startswith('"') else value
|
||||||
|
|
||||||
if 'create-date' in name:
|
if 'create-date' in name:
|
||||||
attachment['create-date'] = value
|
attachment['create-date'] = value
|
||||||
@@ -126,6 +128,8 @@ def decode_content(message):
|
|||||||
charset = message.get_content_charset('utf-8')
|
charset = message.get_content_charset('utf-8')
|
||||||
try:
|
try:
|
||||||
return content.decode(charset, 'ignore')
|
return content.decode(charset, 'ignore')
|
||||||
|
except LookupError:
|
||||||
|
return content.decode(charset.replace("-", ""), 'ignore')
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return content
|
return content
|
||||||
|
|
||||||
@@ -161,9 +165,11 @@ def parse_email(raw_email, policy=None):
|
|||||||
email_parse_kwargs = {}
|
email_parse_kwargs = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
email_message = email.message_from_string(raw_email, **email_parse_kwargs)
|
email_message = email.message_from_string(
|
||||||
|
raw_email, **email_parse_kwargs)
|
||||||
except UnicodeEncodeError:
|
except UnicodeEncodeError:
|
||||||
email_message = email.message_from_string(raw_email.encode('utf-8'), **email_parse_kwargs)
|
email_message = email.message_from_string(
|
||||||
|
raw_email.encode('utf-8'), **email_parse_kwargs)
|
||||||
maintype = email_message.get_content_maintype()
|
maintype = email_message.get_content_maintype()
|
||||||
parsed_email = {'raw_email': raw_email}
|
parsed_email = {'raw_email': raw_email}
|
||||||
|
|
||||||
@@ -185,7 +191,7 @@ def parse_email(raw_email, policy=None):
|
|||||||
content = decode_content(part)
|
content = decode_content(part)
|
||||||
|
|
||||||
is_inline = content_disposition is None \
|
is_inline = content_disposition is None \
|
||||||
or content_disposition.startswith("inline")
|
or content_disposition.startswith("inline")
|
||||||
if content_type == "text/plain" and is_inline:
|
if content_type == "text/plain" and is_inline:
|
||||||
body['plain'].append(content)
|
body['plain'].append(content)
|
||||||
elif content_type == "text/html" and is_inline:
|
elif content_type == "text/html" and is_inline:
|
||||||
|
|||||||
@@ -26,5 +26,9 @@ def parse_attachment(message_part: Message) -> Optional[Dict[str, Union[int, str
|
|||||||
def decode_content(message: Message) -> str: ...
|
def decode_content(message: Message) -> str: ...
|
||||||
|
|
||||||
def fetch_email_by_uid(uid: bytes, connection: IMAP4_SSL, parser_policy: Optional[Policy]) -> Struct: ...
|
def fetch_email_by_uid(uid: bytes, connection: IMAP4_SSL, parser_policy: Optional[Policy]) -> Struct: ...
|
||||||
|
raw_headers: bytes
|
||||||
|
raw_email: bytes
|
||||||
|
|
||||||
|
def parse_flags(headers: str) -> Union[list, List[bytes]]: ...
|
||||||
|
|
||||||
def parse_email(raw_email: bytes, policy: Optional[Policy]) -> Struct: ...
|
def parse_email(raw_email: bytes, policy: Optional[Policy]) -> Struct: ...
|
||||||
|
|||||||
@@ -1,61 +1,17 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import logging
|
|
||||||
# TODO - Validate query arguments
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def format_date(date):
|
def build_search_query(imap_attribute_lookup, **kwargs):
|
||||||
if isinstance(date, datetime.date):
|
|
||||||
return date.strftime('%d-%b-%Y')
|
|
||||||
return date
|
|
||||||
|
|
||||||
|
|
||||||
def build_search_query(**kwargs):
|
|
||||||
|
|
||||||
# Parse keyword arguments
|
|
||||||
unread = kwargs.get('unread', False)
|
|
||||||
unflagged = kwargs.get('unflagged', False)
|
|
||||||
flagged = kwargs.get('flagged', False)
|
|
||||||
sent_from = kwargs.get('sent_from', False)
|
|
||||||
sent_to = kwargs.get('sent_to', False)
|
|
||||||
date__gt = kwargs.get('date__gt', False)
|
|
||||||
date__lt = kwargs.get('date__lt', False)
|
|
||||||
date__on = kwargs.get('date__on', False)
|
|
||||||
subject = kwargs.get('subject')
|
|
||||||
|
|
||||||
query = []
|
query = []
|
||||||
|
for name, value in kwargs.items():
|
||||||
if unread:
|
if value is not None:
|
||||||
query.append("(UNSEEN)")
|
if isinstance(value, datetime.date):
|
||||||
|
value = value.strftime('%d-%b-%Y')
|
||||||
if unflagged:
|
if isinstance(value, str) and '"' in value:
|
||||||
query.append("(UNFLAGGED)")
|
value = value.replace('"', "'")
|
||||||
|
query.append(imap_attribute_lookup[name].format(value))
|
||||||
if flagged:
|
|
||||||
query.append("(FLAGGED)")
|
|
||||||
|
|
||||||
if sent_from:
|
|
||||||
query.append('(FROM "%s")' % sent_from)
|
|
||||||
|
|
||||||
if sent_to:
|
|
||||||
query.append('(TO "%s")' % sent_to)
|
|
||||||
|
|
||||||
if date__gt:
|
|
||||||
query.append('(SINCE "%s")' % format_date(date__gt))
|
|
||||||
|
|
||||||
if date__lt:
|
|
||||||
query.append('(BEFORE "%s")' % format_date(date__lt))
|
|
||||||
|
|
||||||
if date__on:
|
|
||||||
query.append('(ON "%s")' % format_date(date__on))
|
|
||||||
|
|
||||||
if subject is not None:
|
|
||||||
query.append('(SUBJECT "%s")' % subject)
|
|
||||||
|
|
||||||
if query:
|
if query:
|
||||||
logger.debug("IMAP query: {}".format(" ".join(query)))
|
|
||||||
return " ".join(query)
|
return " ".join(query)
|
||||||
|
|
||||||
logger.debug("IMAP query: {}".format("(ALL)"))
|
|
||||||
return "(ALL)"
|
return "(ALL)"
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
import datetime
|
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
|
|
||||||
def format_date(date: Union[str, datetime.date]) -> str: ...
|
|
||||||
|
|
||||||
def build_search_query(**kwargs: Union[bool, str, datetime.date]) -> str: ...
|
|
||||||
11
imbox/vendors/__init__.py
vendored
Normal file
11
imbox/vendors/__init__.py
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from imbox.vendors.gmail import GmailMessages
|
||||||
|
|
||||||
|
vendors = [GmailMessages]
|
||||||
|
|
||||||
|
hostname_vendorname_dict = {vendor.hostname: vendor.name for vendor in vendors}
|
||||||
|
name_authentication_string_dict = {vendor.name: vendor.authentication_error_message for vendor in vendors}
|
||||||
|
|
||||||
|
__all__ = [v.__name__ for v in vendors]
|
||||||
|
|
||||||
|
__all__ += ['hostname_vendorname_dict',
|
||||||
|
'name_authentication_string_dict']
|
||||||
9
imbox/vendors/__init__.pyi
vendored
Normal file
9
imbox/vendors/__init__.pyi
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from typing import List, Dict, Optional
|
||||||
|
|
||||||
|
from imbox.messages import Messages
|
||||||
|
|
||||||
|
vendors: List[Messages]
|
||||||
|
|
||||||
|
hostname_vendorname_dict: Dict[str, str]
|
||||||
|
name_authentication_string_dict: Dict[str, Optional[str]]
|
||||||
|
|
||||||
39
imbox/vendors/gmail.py
vendored
Normal file
39
imbox/vendors/gmail.py
vendored
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
from imbox.messages import Messages
|
||||||
|
from imbox.vendors.helpers import merge_two_dicts
|
||||||
|
|
||||||
|
|
||||||
|
class GmailMessages(Messages):
|
||||||
|
authentication_error_message = ('If you\'re not using an app-specific password, grab one here: '
|
||||||
|
'https://myaccount.google.com/apppasswords')
|
||||||
|
hostname = 'imap.gmail.com'
|
||||||
|
name = 'gmail'
|
||||||
|
FOLDER_LOOKUP = {
|
||||||
|
|
||||||
|
'all_mail': '"[Gmail]/All Mail"',
|
||||||
|
'all': '"[Gmail]/All Mail"',
|
||||||
|
'all mail': '"[Gmail]/All Mail"',
|
||||||
|
'sent': '"[Gmail]/Sent Mail"',
|
||||||
|
'sent mail': '"[Gmail]/Sent Mail"',
|
||||||
|
'sent_mail': '"[Gmail]/Sent Mail"',
|
||||||
|
'drafts': '"[Gmail]/Drafts"',
|
||||||
|
'important': '"[Gmail]/Important"',
|
||||||
|
'spam': '"[Gmail]/Spam"',
|
||||||
|
'starred': '"[Gmail]/Starred"',
|
||||||
|
'trash': '"[Gmail]/Trash"',
|
||||||
|
}
|
||||||
|
|
||||||
|
GMAIL_IMAP_ATTRIBUTE_LOOKUP_DIFF = {
|
||||||
|
'subject': '(X-GM-RAW "subject:\'{}\'")',
|
||||||
|
'label': '(X-GM-LABELS "{}")',
|
||||||
|
'raw': '(X-GM-RAW "{}")'
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
connection,
|
||||||
|
parser_policy,
|
||||||
|
**kwargs):
|
||||||
|
|
||||||
|
self.IMAP_ATTRIBUTE_LOOKUP = merge_two_dicts(self.IMAP_ATTRIBUTE_LOOKUP,
|
||||||
|
self.GMAIL_IMAP_ATTRIBUTE_LOOKUP_DIFF)
|
||||||
|
|
||||||
|
super().__init__(connection, parser_policy, **kwargs)
|
||||||
14
imbox/vendors/gmail.pyi
vendored
Normal file
14
imbox/vendors/gmail.pyi
vendored
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import datetime
|
||||||
|
from email._policybase import Policy
|
||||||
|
from imaplib import IMAP4, IMAP4_SSL
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
from imbox.messages import Messages
|
||||||
|
|
||||||
|
|
||||||
|
class GmailMessages(Messages):
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
connection: Union[IMAP4, IMAP4_SSL],
|
||||||
|
parser_policy: Policy,
|
||||||
|
**kwargs: Union[bool, str, datetime.date]) -> None: ...
|
||||||
6
imbox/vendors/helpers.py
vendored
Normal file
6
imbox/vendors/helpers.py
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
def merge_two_dicts(x, y):
|
||||||
|
"""from https://stackoverflow.com/a/26853961/4386191"""
|
||||||
|
z = x.copy() # start with x's keys and values
|
||||||
|
z.update(y) # modifies z with y's keys and values & returns None
|
||||||
|
return z
|
||||||
5
setup.py
5
setup.py
@@ -19,7 +19,7 @@ setup(
|
|||||||
author_email='martin@amon.cx',
|
author_email='martin@amon.cx',
|
||||||
url='https://github.com/martinrusev/imbox',
|
url='https://github.com/martinrusev/imbox',
|
||||||
license='MIT',
|
license='MIT',
|
||||||
packages=['imbox'],
|
packages=['imbox', 'imbox.vendors'],
|
||||||
package_dir={'imbox': 'imbox'},
|
package_dir={'imbox': 'imbox'},
|
||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
classifiers=(
|
classifiers=(
|
||||||
@@ -27,7 +27,8 @@ setup(
|
|||||||
'Programming Language :: Python :: 3.3',
|
'Programming Language :: Python :: 3.3',
|
||||||
'Programming Language :: Python :: 3.4',
|
'Programming Language :: Python :: 3.4',
|
||||||
'Programming Language :: Python :: 3.5',
|
'Programming Language :: Python :: 3.5',
|
||||||
'Programming Language :: Python :: 3.6'
|
'Programming Language :: Python :: 3.6',
|
||||||
|
'Programming Language :: Python :: 3.7',
|
||||||
),
|
),
|
||||||
test_suite='tests',
|
test_suite='tests',
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -274,6 +274,44 @@ R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==
|
|||||||
--____NOIBTUQXSYRVOOAFLCHY____--
|
--____NOIBTUQXSYRVOOAFLCHY____--
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
raw_email_encoded_encoding_charset_contains_a_minus = b"""Delivered-To: <receiver@example.org>
|
||||||
|
Return-Path: <sender@example.org>
|
||||||
|
Message-ID: <74836CF6FF9B1965927DE7EE8A087483@NXOFGRQFQW2>
|
||||||
|
From: <sender@example.org>
|
||||||
|
To: <sender@example.org>
|
||||||
|
Subject: Salut, mon cher.
|
||||||
|
Date: 30 May 2018 22:47:37 +0200
|
||||||
|
MIME-Version: 1.0
|
||||||
|
Content-Type: multipart/alternative;
|
||||||
|
boundary="----=_NextPart_000_0038_01D3F85C.02934C4A"
|
||||||
|
|
||||||
|
------=_NextPart_000_0038_01D3F85C.02934C4A
|
||||||
|
Content-Type: text/plain;
|
||||||
|
charset="cp-850"
|
||||||
|
Content-Transfer-Encoding: quoted-printable
|
||||||
|
|
||||||
|
spam here
|
||||||
|
|
||||||
|
|
||||||
|
cliquez ici
|
||||||
|
------=_NextPart_000_0038_01D3F85C.02934C4A
|
||||||
|
Content-Type: text/html;
|
||||||
|
charset="cp-850"
|
||||||
|
Content-Transfer-Encoding: quoted-printable
|
||||||
|
|
||||||
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||||||
|
<HTML><HEAD>
|
||||||
|
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dcp-850">
|
||||||
|
<META content=3D"MSHTML 6.00.2900.2456" name=3DGENERATOR>
|
||||||
|
<STYLE></STYLE>
|
||||||
|
</HEAD>
|
||||||
|
<BODY bgColor=3D#ffffff>
|
||||||
|
spam here<br>
|
||||||
|
<br>
|
||||||
|
<a href=3D"http://spammer-url"><b>cliquez =
|
||||||
|
ici</b></a></br></BODY></HTML>
|
||||||
|
------=_NextPart_000_0038_01D3F85C.02934C4A--
|
||||||
|
"""
|
||||||
|
|
||||||
class TestParser(unittest.TestCase):
|
class TestParser(unittest.TestCase):
|
||||||
|
|
||||||
@@ -324,6 +362,12 @@ class TestParser(unittest.TestCase):
|
|||||||
self.assertEqual('abc.xyz', attachment['filename'])
|
self.assertEqual('abc.xyz', attachment['filename'])
|
||||||
self.assertTrue(attachment['content'])
|
self.assertTrue(attachment['content'])
|
||||||
|
|
||||||
|
def test_parse_email_accept_if_declared_charset_contains_a_minus_character(self):
|
||||||
|
parsed_email = parse_email(raw_email_encoded_encoding_charset_contains_a_minus)
|
||||||
|
self.assertEqual("Salut, mon cher.", parsed_email.subject)
|
||||||
|
self.assertTrue(parsed_email.body['plain'])
|
||||||
|
self.assertTrue(parsed_email.body['html'])
|
||||||
|
|
||||||
# TODO - Complete the test suite
|
# TODO - Complete the test suite
|
||||||
def test_decode_mail_header(self):
|
def test_decode_mail_header(self):
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -1,50 +1,83 @@
|
|||||||
import unittest
|
|
||||||
from imbox.query import build_search_query
|
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from imbox.query import build_search_query
|
||||||
|
from imbox.messages import Messages
|
||||||
|
from imbox.vendors.helpers import merge_two_dicts
|
||||||
|
from imbox.vendors.gmail import GmailMessages
|
||||||
|
|
||||||
|
IMAP_ATTRIBUTE_LOOKUP = Messages.IMAP_ATTRIBUTE_LOOKUP
|
||||||
|
GMAIL_ATTRIBUTE_LOOKUP = merge_two_dicts(IMAP_ATTRIBUTE_LOOKUP,
|
||||||
|
GmailMessages.GMAIL_IMAP_ATTRIBUTE_LOOKUP_DIFF)
|
||||||
|
|
||||||
|
|
||||||
class TestQuery(unittest.TestCase):
|
class TestQuery(unittest.TestCase):
|
||||||
|
|
||||||
def test_all(self):
|
def test_all(self):
|
||||||
|
|
||||||
res = build_search_query()
|
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP)
|
||||||
self.assertEqual(res, "(ALL)")
|
self.assertEqual(res, "(ALL)")
|
||||||
|
|
||||||
|
def test_subject(self):
|
||||||
|
|
||||||
|
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP, subject='hi')
|
||||||
|
self.assertEqual(res, '(SUBJECT "hi")')
|
||||||
|
|
||||||
|
res = build_search_query(GMAIL_ATTRIBUTE_LOOKUP, subject='hi')
|
||||||
|
self.assertEqual(res, '(X-GM-RAW "subject:\'hi\'")')
|
||||||
|
|
||||||
def test_unread(self):
|
def test_unread(self):
|
||||||
|
|
||||||
res = build_search_query(unread=True)
|
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP, unread=True)
|
||||||
self.assertEqual(res, "(UNSEEN)")
|
self.assertEqual(res, "(UNSEEN)")
|
||||||
|
|
||||||
def test_unflagged(self):
|
def test_unflagged(self):
|
||||||
|
|
||||||
res = build_search_query(unflagged=True)
|
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP, unflagged=True)
|
||||||
self.assertEqual(res, "(UNFLAGGED)")
|
self.assertEqual(res, "(UNFLAGGED)")
|
||||||
|
|
||||||
def test_flagged(self):
|
def test_flagged(self):
|
||||||
|
|
||||||
res = build_search_query(flagged=True)
|
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP, flagged=True)
|
||||||
self.assertEqual(res, "(FLAGGED)")
|
self.assertEqual(res, "(FLAGGED)")
|
||||||
|
|
||||||
def test_sent_from(self):
|
def test_sent_from(self):
|
||||||
|
|
||||||
res = build_search_query(sent_from='test@example.com')
|
res = build_search_query(
|
||||||
|
IMAP_ATTRIBUTE_LOOKUP, sent_from='test@example.com')
|
||||||
self.assertEqual(res, '(FROM "test@example.com")')
|
self.assertEqual(res, '(FROM "test@example.com")')
|
||||||
|
|
||||||
def test_sent_to(self):
|
def test_sent_to(self):
|
||||||
|
|
||||||
res = build_search_query(sent_to='test@example.com')
|
res = build_search_query(
|
||||||
|
IMAP_ATTRIBUTE_LOOKUP, sent_to='test@example.com')
|
||||||
self.assertEqual(res, '(TO "test@example.com")')
|
self.assertEqual(res, '(TO "test@example.com")')
|
||||||
|
|
||||||
def test_date__gt(self):
|
def test_date__gt(self):
|
||||||
|
|
||||||
res = build_search_query(date__gt=date(2014, 12, 31))
|
res = build_search_query(
|
||||||
|
IMAP_ATTRIBUTE_LOOKUP, date__gt=date(2014, 12, 31))
|
||||||
self.assertEqual(res, '(SINCE "31-Dec-2014")')
|
self.assertEqual(res, '(SINCE "31-Dec-2014")')
|
||||||
|
|
||||||
def test_date__lt(self):
|
def test_date__lt(self):
|
||||||
|
|
||||||
res = build_search_query(date__lt=date(2014, 1, 1))
|
res = build_search_query(
|
||||||
|
IMAP_ATTRIBUTE_LOOKUP, date__lt=date(2014, 1, 1))
|
||||||
self.assertEqual(res, '(BEFORE "01-Jan-2014")')
|
self.assertEqual(res, '(BEFORE "01-Jan-2014")')
|
||||||
|
|
||||||
def test_date__on(self):
|
def test_date__on(self):
|
||||||
res = build_search_query(date__on=date(2014, 1, 1))
|
res = build_search_query(
|
||||||
|
IMAP_ATTRIBUTE_LOOKUP, date__on=date(2014, 1, 1))
|
||||||
self.assertEqual(res, '(ON "01-Jan-2014")')
|
self.assertEqual(res, '(ON "01-Jan-2014")')
|
||||||
|
|
||||||
|
def test_uid__range(self):
|
||||||
|
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP, uid__range='1000:*')
|
||||||
|
self.assertEqual(res, '(UID 1000:*)')
|
||||||
|
|
||||||
|
def test_gmail_raw(self):
|
||||||
|
res = build_search_query(GMAIL_ATTRIBUTE_LOOKUP, raw='has:attachment subject:"hey"')
|
||||||
|
self.assertEqual(res, '(X-GM-RAW "has:attachment subject:\'hey\'")')
|
||||||
|
|
||||||
|
def test_gmail_label(self):
|
||||||
|
res = build_search_query(GMAIL_ATTRIBUTE_LOOKUP, label='finance')
|
||||||
|
self.assertEqual(res, '(X-GM-LABELS "finance")')
|
||||||
|
|||||||
Reference in New Issue
Block a user