2007-07-04 19:53:48 +02:00
|
|
|
# imaplib utilities
|
2010-12-16 13:43:47 +01:00
|
|
|
# Copyright (C) 2002-2007 John Goerzen <jgoerzen@complete.org>
|
|
|
|
# 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>
|
2007-07-04 19:36:33 +02:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
2011-04-13 10:52:18 +02:00
|
|
|
import os
|
|
|
|
import fcntl
|
2011-03-11 22:13:21 +01:00
|
|
|
import re
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import subprocess
|
2011-01-05 17:00:55 +01:00
|
|
|
from offlineimap.ui import getglobalui
|
Update to match semantics of new imaplib2
The biggest change here is that imapobj.untagged_responses is no
longer a dictionary, but a list. To access it, I use the semi-private
_get_untagged_response method.
* offlineimap/folder/IMAP.py (IMAPFolder.quickchanged,
IMAPFolder.cachemessagelist): imaplib2 now explicitly removes its
EXISTS response on select(), so instead we use the return values from
select() to get the number of messages.
* offlineimap/imapserver.py (UsefulIMAPMixIn.select): imaplib2 now
stores untagged_responses for different mailboxes, which confuses us
because it seems like our mailboxes are "still" in read-only mode when
we just re-opened them. Additionally, we have to return the value
from imaplib2's select() so that the above thing works.
* offlineimap/imapserver.py (UsefulIMAPMixIn._mesg): imaplib2 now
calls _mesg with the name of a thread, so we display this
information in debug output. This requires a corresponding change to
imaplibutil.new_mesg.
* offlineimap/imaplibutil.py: We override IMAP4_SSL.open, whose
default arguments have changed, so update the default arguments. We
also subclass imaplib.IMAP4 in a few different places, which now
relies on having a read_fd file descriptor to poll on.
Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-03-10 21:36:20 +01:00
|
|
|
import threading
|
2011-05-07 18:39:13 +02:00
|
|
|
from offlineimap import OfflineImapError
|
2011-05-09 22:43:03 +02:00
|
|
|
from offlineimap.imaplib2 import IMAP4, IMAP4_SSL, zlib, IMAP4_PORT, InternalDate, Mon2num
|
2007-07-04 20:17:27 +02:00
|
|
|
|
2009-08-24 20:11:22 +02:00
|
|
|
try:
|
2009-07-02 03:49:20 +02:00
|
|
|
import ssl
|
2009-08-24 20:11:22 +02:00
|
|
|
except ImportError:
|
2010-12-16 13:43:47 +01:00
|
|
|
#fails on python <2.6
|
|
|
|
pass
|
2007-07-04 20:17:27 +02:00
|
|
|
|
2011-03-22 15:51:43 +01:00
|
|
|
class UsefulIMAPMixIn:
|
|
|
|
def getselectedfolder(self):
|
2011-07-06 23:15:33 +02:00
|
|
|
if self.state == 'SELECTED':
|
|
|
|
return self.mailbox
|
2011-03-22 15:51:43 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
def select(self, mailbox='INBOX', readonly=None, force = 0):
|
2011-05-07 18:39:13 +02:00
|
|
|
"""Selects a mailbox on the IMAP server
|
|
|
|
|
|
|
|
:returns: 'OK' on success, nothing if the folder was already
|
|
|
|
selected or raises an :exc:`OfflineImapError`"""
|
2011-03-22 15:51:43 +01:00
|
|
|
if (not force) and self.getselectedfolder() == mailbox \
|
|
|
|
and self.is_readonly == readonly:
|
|
|
|
# No change; return.
|
|
|
|
return
|
|
|
|
# Wipe out all old responses, to maintain semantics with old imaplib2
|
|
|
|
del self.untagged_responses[:]
|
2011-09-01 11:00:59 +02:00
|
|
|
try:
|
|
|
|
result = self.__class__.__bases__[1].select(self, mailbox, readonly)
|
|
|
|
except self.abort, e:
|
|
|
|
# self.abort is raised when we are supposed to retry
|
|
|
|
errstr = "Server '%s' closed connection, error on SELECT '%s'. Ser"\
|
|
|
|
"ver said: %s" % (self.host, mailbox, e.args[0])
|
|
|
|
severity = OfflineImapError.ERROR.FOLDER_RETRY
|
|
|
|
raise OfflineImapError(errstr, severity)
|
2011-03-22 15:51:43 +01:00
|
|
|
if result[0] != 'OK':
|
2011-05-07 18:39:13 +02:00
|
|
|
#in case of error, bail out with OfflineImapError
|
|
|
|
errstr = "Error SELECTing mailbox '%s', server reply:\n%s" %\
|
|
|
|
(mailbox, result)
|
|
|
|
severity = OfflineImapError.ERROR.FOLDER
|
|
|
|
raise OfflineImapError(errstr, severity)
|
2011-03-22 15:51:43 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
def _mesg(self, s, tn=None, secs=None):
|
|
|
|
new_mesg(self, s, tn, secs)
|
|
|
|
|
2011-03-22 15:51:44 +01:00
|
|
|
class IMAP4_Tunnel(UsefulIMAPMixIn, IMAP4):
|
2007-07-04 19:36:33 +02:00
|
|
|
"""IMAP4 client class over a tunnel
|
|
|
|
|
|
|
|
Instantiate with: IMAP4_Tunnel(tunnelcmd)
|
|
|
|
|
|
|
|
tunnelcmd -- shell command to generate the tunnel.
|
|
|
|
The result will be in PREAUTH stage."""
|
|
|
|
|
2011-04-13 10:52:18 +02:00
|
|
|
def __init__(self, tunnelcmd, **kwargs):
|
|
|
|
IMAP4.__init__(self, tunnelcmd, **kwargs)
|
2007-07-04 19:36:33 +02:00
|
|
|
|
|
|
|
def open(self, host, port):
|
|
|
|
"""The tunnelcmd comes in on host!"""
|
2011-04-13 10:52:18 +02:00
|
|
|
self.host = host
|
2007-07-04 19:36:33 +02:00
|
|
|
self.process = subprocess.Popen(host, shell=True, close_fds=True,
|
|
|
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
|
|
(self.outfd, self.infd) = (self.process.stdin, self.process.stdout)
|
Update to match semantics of new imaplib2
The biggest change here is that imapobj.untagged_responses is no
longer a dictionary, but a list. To access it, I use the semi-private
_get_untagged_response method.
* offlineimap/folder/IMAP.py (IMAPFolder.quickchanged,
IMAPFolder.cachemessagelist): imaplib2 now explicitly removes its
EXISTS response on select(), so instead we use the return values from
select() to get the number of messages.
* offlineimap/imapserver.py (UsefulIMAPMixIn.select): imaplib2 now
stores untagged_responses for different mailboxes, which confuses us
because it seems like our mailboxes are "still" in read-only mode when
we just re-opened them. Additionally, we have to return the value
from imaplib2's select() so that the above thing works.
* offlineimap/imapserver.py (UsefulIMAPMixIn._mesg): imaplib2 now
calls _mesg with the name of a thread, so we display this
information in debug output. This requires a corresponding change to
imaplibutil.new_mesg.
* offlineimap/imaplibutil.py: We override IMAP4_SSL.open, whose
default arguments have changed, so update the default arguments. We
also subclass imaplib.IMAP4 in a few different places, which now
relies on having a read_fd file descriptor to poll on.
Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-03-10 21:36:20 +01:00
|
|
|
# imaplib2 polls on this fd
|
|
|
|
self.read_fd = self.infd.fileno()
|
2007-07-04 19:36:33 +02:00
|
|
|
|
2011-04-13 10:52:18 +02:00
|
|
|
self.set_nonblocking(self.read_fd)
|
|
|
|
|
|
|
|
def set_nonblocking(self, fd):
|
|
|
|
"Mark fd as nonblocking"
|
|
|
|
# get the file's current flag settings
|
|
|
|
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
|
|
|
|
# clear non-blocking mode from flags
|
|
|
|
fl = fl & ~os.O_NONBLOCK
|
|
|
|
fcntl.fcntl(fd, fcntl.F_SETFL, fl)
|
|
|
|
|
2007-07-04 19:36:33 +02:00
|
|
|
def read(self, size):
|
2011-04-13 10:52:18 +02:00
|
|
|
"""data = read(size)
|
|
|
|
Read at most 'size' bytes from remote."""
|
2007-07-04 19:36:33 +02:00
|
|
|
|
2011-04-13 10:52:18 +02:00
|
|
|
if self.decompressor is None:
|
|
|
|
return os.read(self.read_fd, size)
|
|
|
|
|
|
|
|
if self.decompressor.unconsumed_tail:
|
|
|
|
data = self.decompressor.unconsumed_tail
|
|
|
|
else:
|
|
|
|
data = os.read(self.read_fd, 8192)
|
|
|
|
|
|
|
|
return self.decompressor.decompress(data, size)
|
2007-07-04 19:36:33 +02:00
|
|
|
|
|
|
|
def send(self, data):
|
2011-04-13 10:52:18 +02:00
|
|
|
if self.compressor is not None:
|
|
|
|
data = self.compressor.compress(data)
|
|
|
|
data += self.compressor.flush(zlib.Z_SYNC_FLUSH)
|
|
|
|
|
2007-07-04 19:36:33 +02:00
|
|
|
self.outfd.write(data)
|
|
|
|
|
2011-04-13 10:52:18 +02:00
|
|
|
|
2007-07-04 19:36:33 +02:00
|
|
|
def shutdown(self):
|
|
|
|
self.infd.close()
|
|
|
|
self.outfd.close()
|
|
|
|
self.process.wait()
|
|
|
|
|
|
|
|
|
Update to match semantics of new imaplib2
The biggest change here is that imapobj.untagged_responses is no
longer a dictionary, but a list. To access it, I use the semi-private
_get_untagged_response method.
* offlineimap/folder/IMAP.py (IMAPFolder.quickchanged,
IMAPFolder.cachemessagelist): imaplib2 now explicitly removes its
EXISTS response on select(), so instead we use the return values from
select() to get the number of messages.
* offlineimap/imapserver.py (UsefulIMAPMixIn.select): imaplib2 now
stores untagged_responses for different mailboxes, which confuses us
because it seems like our mailboxes are "still" in read-only mode when
we just re-opened them. Additionally, we have to return the value
from imaplib2's select() so that the above thing works.
* offlineimap/imapserver.py (UsefulIMAPMixIn._mesg): imaplib2 now
calls _mesg with the name of a thread, so we display this
information in debug output. This requires a corresponding change to
imaplibutil.new_mesg.
* offlineimap/imaplibutil.py: We override IMAP4_SSL.open, whose
default arguments have changed, so update the default arguments. We
also subclass imaplib.IMAP4 in a few different places, which now
relies on having a read_fd file descriptor to poll on.
Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-03-10 21:36:20 +01:00
|
|
|
def new_mesg(self, s, tn=None, secs=None):
|
2007-07-04 19:36:33 +02:00
|
|
|
if secs is None:
|
|
|
|
secs = time.time()
|
Update to match semantics of new imaplib2
The biggest change here is that imapobj.untagged_responses is no
longer a dictionary, but a list. To access it, I use the semi-private
_get_untagged_response method.
* offlineimap/folder/IMAP.py (IMAPFolder.quickchanged,
IMAPFolder.cachemessagelist): imaplib2 now explicitly removes its
EXISTS response on select(), so instead we use the return values from
select() to get the number of messages.
* offlineimap/imapserver.py (UsefulIMAPMixIn.select): imaplib2 now
stores untagged_responses for different mailboxes, which confuses us
because it seems like our mailboxes are "still" in read-only mode when
we just re-opened them. Additionally, we have to return the value
from imaplib2's select() so that the above thing works.
* offlineimap/imapserver.py (UsefulIMAPMixIn._mesg): imaplib2 now
calls _mesg with the name of a thread, so we display this
information in debug output. This requires a corresponding change to
imaplibutil.new_mesg.
* offlineimap/imaplibutil.py: We override IMAP4_SSL.open, whose
default arguments have changed, so update the default arguments. We
also subclass imaplib.IMAP4 in a few different places, which now
relies on having a read_fd file descriptor to poll on.
Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-03-10 21:36:20 +01:00
|
|
|
if tn is None:
|
|
|
|
tn = threading.currentThread().getName()
|
2007-07-04 19:36:33 +02:00
|
|
|
tm = time.strftime('%M:%S', time.localtime(secs))
|
Update to match semantics of new imaplib2
The biggest change here is that imapobj.untagged_responses is no
longer a dictionary, but a list. To access it, I use the semi-private
_get_untagged_response method.
* offlineimap/folder/IMAP.py (IMAPFolder.quickchanged,
IMAPFolder.cachemessagelist): imaplib2 now explicitly removes its
EXISTS response on select(), so instead we use the return values from
select() to get the number of messages.
* offlineimap/imapserver.py (UsefulIMAPMixIn.select): imaplib2 now
stores untagged_responses for different mailboxes, which confuses us
because it seems like our mailboxes are "still" in read-only mode when
we just re-opened them. Additionally, we have to return the value
from imaplib2's select() so that the above thing works.
* offlineimap/imapserver.py (UsefulIMAPMixIn._mesg): imaplib2 now
calls _mesg with the name of a thread, so we display this
information in debug output. This requires a corresponding change to
imaplibutil.new_mesg.
* offlineimap/imaplibutil.py: We override IMAP4_SSL.open, whose
default arguments have changed, so update the default arguments. We
also subclass imaplib.IMAP4 in a few different places, which now
relies on having a read_fd file descriptor to poll on.
Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-03-10 21:36:20 +01:00
|
|
|
getglobalui().debug('imap', ' %s.%02d %s %s' % (tm, (secs*100)%100, tn, s))
|
2007-07-04 19:36:33 +02:00
|
|
|
|
2011-08-15 11:55:42 +02:00
|
|
|
|
2011-03-22 15:51:44 +01:00
|
|
|
class WrappedIMAP4_SSL(UsefulIMAPMixIn, IMAP4_SSL):
|
2011-08-15 11:55:42 +02:00
|
|
|
"""Improved version of imaplib.IMAP4_SSL overriding select()"""
|
|
|
|
pass
|
2010-12-16 13:43:47 +01:00
|
|
|
|
|
|
|
|
2011-03-22 15:51:44 +01:00
|
|
|
class WrappedIMAP4(UsefulIMAPMixIn, IMAP4):
|
2011-08-15 11:55:42 +02:00
|
|
|
"""Improved version of imaplib.IMAP4 overriding select()"""
|
|
|
|
pass
|
2010-12-16 13:43:46 +01:00
|
|
|
|
Update to match semantics of new imaplib2
The biggest change here is that imapobj.untagged_responses is no
longer a dictionary, but a list. To access it, I use the semi-private
_get_untagged_response method.
* offlineimap/folder/IMAP.py (IMAPFolder.quickchanged,
IMAPFolder.cachemessagelist): imaplib2 now explicitly removes its
EXISTS response on select(), so instead we use the return values from
select() to get the number of messages.
* offlineimap/imapserver.py (UsefulIMAPMixIn.select): imaplib2 now
stores untagged_responses for different mailboxes, which confuses us
because it seems like our mailboxes are "still" in read-only mode when
we just re-opened them. Additionally, we have to return the value
from imaplib2's select() so that the above thing works.
* offlineimap/imapserver.py (UsefulIMAPMixIn._mesg): imaplib2 now
calls _mesg with the name of a thread, so we display this
information in debug output. This requires a corresponding change to
imaplibutil.new_mesg.
* offlineimap/imaplibutil.py: We override IMAP4_SSL.open, whose
default arguments have changed, so update the default arguments. We
also subclass imaplib.IMAP4 in a few different places, which now
relies on having a read_fd file descriptor to poll on.
Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-03-10 21:36:20 +01:00
|
|
|
|
2007-07-04 19:36:33 +02:00
|
|
|
def Internaldate2epoch(resp):
|
|
|
|
"""Convert IMAP4 INTERNALDATE to UT.
|
|
|
|
|
|
|
|
Returns seconds since the epoch.
|
|
|
|
"""
|
|
|
|
|
|
|
|
mo = InternalDate.match(resp)
|
|
|
|
if not mo:
|
|
|
|
return None
|
|
|
|
|
|
|
|
mon = Mon2num[mo.group('mon')]
|
|
|
|
zonen = mo.group('zonen')
|
|
|
|
|
|
|
|
day = int(mo.group('day'))
|
|
|
|
year = int(mo.group('year'))
|
|
|
|
hour = int(mo.group('hour'))
|
|
|
|
min = int(mo.group('min'))
|
|
|
|
sec = int(mo.group('sec'))
|
|
|
|
zoneh = int(mo.group('zoneh'))
|
|
|
|
zonem = int(mo.group('zonem'))
|
|
|
|
|
|
|
|
# INTERNALDATE timezone must be subtracted to get UT
|
|
|
|
|
|
|
|
zone = (zoneh*60 + zonem)*60
|
|
|
|
if zonen == '-':
|
|
|
|
zone = -zone
|
|
|
|
|
|
|
|
tt = (year, mon, day, hour, min, sec, -1, -1, -1)
|
|
|
|
|
|
|
|
return time.mktime(tt)
|