]> code.delx.au - offlineimap/blob - offlineimap/imaplibutil.py
Update changelog
[offlineimap] / offlineimap / imaplibutil.py
1 # imaplib utilities
2 # Copyright (C) 2002-2007 John Goerzen
3 # <jgoerzen@complete.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 import os, re, socket, time, subprocess, sys, threading
20 from offlineimap.ui import UIBase
21 from offlineimap.imaplib2 import *
22
23 # Import the symbols we need that aren't exported by default
24 from offlineimap.imaplib2 import IMAP4_PORT, IMAP4_SSL_PORT, InternalDate, Mon2num
25
26
27 class IMAP4_Tunnel(IMAP4):
28 """IMAP4 client class over a tunnel
29
30 Instantiate with: IMAP4_Tunnel(tunnelcmd)
31
32 tunnelcmd -- shell command to generate the tunnel.
33 The result will be in PREAUTH stage."""
34
35 def __init__(self, tunnelcmd):
36 IMAP4.__init__(self, tunnelcmd)
37
38 def open(self, host, port):
39 """The tunnelcmd comes in on host!"""
40 self.process = subprocess.Popen(host, shell=True, close_fds=True,
41 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
42 (self.outfd, self.infd) = (self.process.stdin, self.process.stdout)
43 self.read_fd = self.infd.fileno()
44
45 def read(self, size):
46 return os.read(self.read_fd, size)
47
48 def readline(self):
49 return self.infd.readline()
50
51 def send(self, data):
52 self.outfd.write(data)
53
54 def shutdown(self):
55 self.infd.close()
56 self.outfd.close()
57 self.process.wait()
58
59 class sslwrapper:
60 def __init__(self, sslsock):
61 self.sslsock = sslsock
62 self.readbuf = ''
63
64 def write(self, s):
65 return self.sslsock.write(s)
66
67 def _read(self, n):
68 return self.sslsock.read(n)
69
70 def read(self, n):
71 if len(self.readbuf):
72 # Return the stuff in readbuf, even if less than n.
73 # It might contain the rest of the line, and if we try to
74 # read more, might block waiting for data that is not
75 # coming to arrive.
76 bytesfrombuf = min(n, len(self.readbuf))
77 retval = self.readbuf[:bytesfrombuf]
78 self.readbuf = self.readbuf[bytesfrombuf:]
79 return retval
80 retval = self._read(n)
81 if len(retval) > n:
82 self.readbuf = retval[n:]
83 return retval[:n]
84 return retval
85
86 def readline(self):
87 retval = ''
88 while 1:
89 linebuf = self.read(1024)
90 nlindex = linebuf.find("\n")
91 if nlindex != -1:
92 retval += linebuf[:nlindex + 1]
93 self.readbuf = linebuf[nlindex + 1:] + self.readbuf
94 return retval
95 else:
96 retval += linebuf
97
98 def new_mesg(self, s, tn=None, secs=None):
99 if secs is None:
100 secs = time.time()
101 if tn is None:
102 tn = threading.currentThread().getName()
103 tm = time.strftime('%M:%S', time.localtime(secs))
104 UIBase.getglobalui().debug('imap', ' %s.%02d %s %s' % (tm, (secs*100)%100, tn, s))
105
106 class WrappedIMAP4_SSL(IMAP4_SSL):
107 def open(self, host=None, port=None):
108 IMAP4_SSL.open(self, host, port)
109 self.sslobj = sslwrapper(self.sslobj)
110
111 def readline(self):
112 return self.sslobj.readline()
113
114 mustquote = re.compile(r"[^\w!#$%&'+,.:;<=>?^`|~-]")
115
116 def Internaldate2epoch(resp):
117 """Convert IMAP4 INTERNALDATE to UT.
118
119 Returns seconds since the epoch.
120 """
121
122 mo = InternalDate.match(resp)
123 if not mo:
124 return None
125
126 mon = Mon2num[mo.group('mon')]
127 zonen = mo.group('zonen')
128
129 day = int(mo.group('day'))
130 year = int(mo.group('year'))
131 hour = int(mo.group('hour'))
132 min = int(mo.group('min'))
133 sec = int(mo.group('sec'))
134 zoneh = int(mo.group('zoneh'))
135 zonem = int(mo.group('zonem'))
136
137 # INTERNALDATE timezone must be subtracted to get UT
138
139 zone = (zoneh*60 + zonem)*60
140 if zonen == '-':
141 zone = -zone
142
143 tt = (year, mon, day, hour, min, sec, -1, -1, -1)
144
145 return time.mktime(tt)