File: imaplibutil.py

package info (click to toggle)
offlineimap3 0.0~git20210225.1e7ef9e%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,328 kB
  • sloc: python: 7,974; sh: 548; makefile: 81
file content (263 lines) | stat: -rw-r--r-- 9,562 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# imaplib utilities
# Copyright (C) 2002-2016 John Goerzen & contributors
#    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
import datetime
import os
import fcntl
import time
import subprocess
import threading
import socket
import errno
import zlib
from sys import exc_info
from hashlib import sha512, sha384, sha256, sha224, sha1
from offlineimap import OfflineImapError
from offlineimap.ui import getglobalui
from imaplib2 import IMAP4, IMAP4_SSL, InternalDate


class UsefulIMAPMixIn:
    def __getselectedfolder(self):
        if self.state == 'SELECTED':
            return self.mailbox
        return None

    def select(self, mailbox='INBOX', readonly=False, force=False):
        """Selects a mailbox on the IMAP server

        :returns: 'OK' on success, nothing if the folder was already
        selected or raises an :exc:`OfflineImapError`."""

        if self.__getselectedfolder() == mailbox and \
                self.is_readonly == readonly and \
                not force:
            # No change; return.
            return
        try:
            result = super(UsefulIMAPMixIn, self).select(mailbox, readonly)
        except self.readonly as e:
            # pass self.readonly to our callers
            raise
        except self.abort as 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, exc_info()[2])

        if result[0] != 'OK':
            # 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)
        return result

    # Overrides private function from IMAP4 (@imaplib2)
    def _mesg(self, s, tn=None, secs=None):
        new_mesg(self, s, tn, secs)

    # Overrides private function from IMAP4 (@imaplib2)
    def open_socket(self):
        """open_socket()
        Open socket choosing first address family available."""
        msg = (-1, 'could not open socket')
        for res in socket.getaddrinfo(self.host, self.port, self.af, socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                # use socket of our own, possiblly socksified socket.
                s = self.socket(af, socktype, proto)
            except socket.error as msg:
                continue
            try:
                for i in (0, 1):
                    try:
                        s.connect(sa)
                        break
                    except socket.error as msg:
                        if len(msg.args) < 2 or msg.args[0] != errno.EINTR:
                            raise
                else:
                    raise socket.error(msg)
            except socket.error as msg:
                s.close()
                continue
            break
        else:
            raise socket.error(msg)

        return s


class IMAP4_Tunnel(UsefulIMAPMixIn, IMAP4):
    """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."""

    def __init__(self, tunnelcmd, **kwargs):
        if "use_socket" in kwargs:
            self.socket = kwargs['use_socket']
            del kwargs['use_socket']
        IMAP4.__init__(self, tunnelcmd, **kwargs)

    def open(self, host, port):
        """The tunnelcmd comes in on host!"""

        self.host = host
        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)
        # imaplib2 polls on this fd
        self.read_fd = self.infd.fileno()

        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)

    def read(self, size):
        """data = read(size)
        Read at most 'size' bytes from remote."""

        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)

    def send(self, data):
        if self.compressor is not None:
            data = self.compressor.compress(data)
            data += self.compressor.flush(zlib.Z_SYNC_FLUSH)
        self.outfd.write(data)
        self.outfd.flush()

    def shutdown(self):
        self.infd.close()
        self.outfd.close()
        self.process.wait()


def new_mesg(self, s, tn=None, secs=None):
    if secs is None:
        secs = time.time()
    if tn is None:
        tn = threading.currentThread().getName()
    tm = time.strftime('%M:%S', time.localtime(secs))
    getglobalui().debug('imap', '  %s.%02d %s %s' % (tm, (secs * 100) % 100, tn, s))


class WrappedIMAP4_SSL(UsefulIMAPMixIn, IMAP4_SSL):
    """Improved version of imaplib.IMAP4_SSL overriding select()."""

    def __init__(self, *args, **kwargs):
        if "af" in kwargs:
            self.af = kwargs['af']
            del kwargs['af']
        if "use_socket" in kwargs:
            self.socket = kwargs['use_socket']
            del kwargs['use_socket']
        self._fingerprint = kwargs.get('fingerprint', None)
        if type(self._fingerprint) != type([]):
            self._fingerprint = [self._fingerprint]
        if 'fingerprint' in kwargs:
            del kwargs['fingerprint']
        super(WrappedIMAP4_SSL, self).__init__(*args, **kwargs)

    def open(self, host=None, port=None):
        if not self.ca_certs and not self._fingerprint:
            raise OfflineImapError("No CA certificates "
                                   "and no server fingerprints configured.  "
                                   "You must configure at least something, otherwise "
                                   "having SSL helps nothing.", OfflineImapError.ERROR.REPO)
        super(WrappedIMAP4_SSL, self).open(host, port)
        if self._fingerprint:
            server_cert = self.sock.getpeercert(True)
            hashes = sha512, sha384, sha256, sha224, sha1
            server_fingerprints = [my_hash(server_cert).hexdigest() for my_hash in hashes]
            # compare fingerprints
            matches = [(server_fingerprint in self._fingerprint) for server_fingerprint in server_fingerprints]
            if not any(matches):
                raise OfflineImapError("Server SSL fingerprint(s) '%s' "
                                       "for hostname '%s' "
                                       "does not match configured fingerprint(s) %s.  "
                                       "Please verify and set 'cert_fingerprint' accordingly "
                                       "if not set yet." %
                                       (list(zip([my_hash.__name__ for my_hash in hashes], server_fingerprints)), host,
                                        self._fingerprint),
                                       OfflineImapError.ERROR.REPO)


class WrappedIMAP4(UsefulIMAPMixIn, IMAP4):
    """Improved version of imaplib.IMAP4 overriding select()."""

    def __init__(self, *args, **kwargs):
        if "af" in kwargs:
            self.af = kwargs['af']
            del kwargs['af']
        if "use_socket" in kwargs:
            self.socket = kwargs['use_socket']
            del kwargs['use_socket']
        IMAP4.__init__(self, *args, **kwargs)


def Internaldate2epoch(resp):
    """Convert IMAP4 INTERNALDATE to UT.

    Returns seconds since the epoch."""

    from calendar import timegm

    mo = InternalDate.match(resp)
    if not mo:
        return None

    # Get the month number
    datetime_object = datetime.datetime.strptime(mo.group('mon').decode('utf-8'), "%b")
    mon = datetime_object.month

    zonen = mo.group('zonen')

    day = int(mo.group('day'))
    year = int(mo.group('year'))
    hour = int(mo.group('hour'))
    minu = 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, minu, sec, -1, -1, -1)

    return timegm(tt) - zone