mirror of
https://github.com/powerline/powerline.git
synced 2025-07-25 14:54:54 +02:00
Merge pull request #1370 from ZyX-I/improve-imap-segment
Add support for non-SSL IMAP4 connections
This commit is contained in:
commit
a09f1e1776
@ -3,36 +3,37 @@ from __future__ import (unicode_literals, division, absolute_import, print_funct
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from imaplib import IMAP4_SSL_PORT, IMAP4_SSL, IMAP4
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from powerline.lib.threaded import KwThreadedSegment
|
from powerline.lib.threaded import KwThreadedSegment
|
||||||
from powerline.segments import with_docstring
|
from powerline.segments import with_docstring
|
||||||
|
|
||||||
|
|
||||||
_IMAPKey = namedtuple('Key', 'username password server port folder')
|
_IMAPKey = namedtuple('Key', 'username password server port folder use_ssl')
|
||||||
|
|
||||||
|
|
||||||
class EmailIMAPSegment(KwThreadedSegment):
|
class EmailIMAPSegment(KwThreadedSegment):
|
||||||
interval = 60
|
interval = 60
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def key(username, password, server='imap.gmail.com', port=993, folder='INBOX', **kwargs):
|
def key(username, password, server='imap.gmail.com', port=IMAP4_SSL_PORT, folder='INBOX', use_ssl=None, **kwargs):
|
||||||
return _IMAPKey(username, password, server, port, folder)
|
if use_ssl is None:
|
||||||
|
use_ssl = (port == IMAP4_SSL_PORT)
|
||||||
|
return _IMAPKey(username, password, server, port, folder, use_ssl)
|
||||||
|
|
||||||
def compute_state(self, key):
|
def compute_state(self, key):
|
||||||
if not key.username or not key.password:
|
if not key.username or not key.password:
|
||||||
self.warn('Username and password are not configured')
|
self.warn('Username and password are not configured')
|
||||||
return None
|
return None
|
||||||
try:
|
if key.use_ssl:
|
||||||
import imaplib
|
mail = IMAP4_SSL(key.server, key.port)
|
||||||
except imaplib.IMAP4.error as e:
|
|
||||||
unread_count = str(e)
|
|
||||||
else:
|
else:
|
||||||
mail = imaplib.IMAP4_SSL(key.server, key.port)
|
mail = IMAP4(key.server, key.port)
|
||||||
mail.login(key.username, key.password)
|
mail.login(key.username, key.password)
|
||||||
rc, message = mail.status(key.folder, '(UNSEEN)')
|
rc, message = mail.status(key.folder, '(UNSEEN)')
|
||||||
unread_str = message[0].decode('utf-8')
|
unread_str = message[0].decode('utf-8')
|
||||||
unread_count = int(re.search('UNSEEN (\d+)', unread_str).group(1))
|
unread_count = int(re.search('UNSEEN (\d+)', unread_str).group(1))
|
||||||
return unread_count
|
return unread_count
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -53,7 +54,7 @@ class EmailIMAPSegment(KwThreadedSegment):
|
|||||||
|
|
||||||
|
|
||||||
email_imap_alert = with_docstring(EmailIMAPSegment(),
|
email_imap_alert = with_docstring(EmailIMAPSegment(),
|
||||||
'''Return unread e-mail count for IMAP servers.
|
('''Return unread e-mail count for IMAP servers.
|
||||||
|
|
||||||
:param str username:
|
:param str username:
|
||||||
login username
|
login username
|
||||||
@ -69,6 +70,9 @@ email_imap_alert = with_docstring(EmailIMAPSegment(),
|
|||||||
Maximum number of messages. If there are more messages then max_msgs then it
|
Maximum number of messages. If there are more messages then max_msgs then it
|
||||||
will use gradient level equal to 100, otherwise gradient level is equal to
|
will use gradient level equal to 100, otherwise gradient level is equal to
|
||||||
``100 * msgs_num / max_msgs``. If not present gradient is not computed.
|
``100 * msgs_num / max_msgs``. If not present gradient is not computed.
|
||||||
|
:param bool use_ssl:
|
||||||
|
If ``True`` then use SSL connection. If ``False`` then do not use it.
|
||||||
|
Default is ``True`` if port is equal to {ssl_port} and ``False`` otherwise.
|
||||||
|
|
||||||
Highlight groups used: ``email_alert_gradient`` (gradient), ``email_alert``.
|
Highlight groups used: ``email_alert_gradient`` (gradient), ``email_alert``.
|
||||||
''')
|
''').format(ssl_port=IMAP4_SSL_PORT))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user