]>
code.delx.au - offlineimap/blob - offlineimap/imaplibutil.py
2 # Copyright (C) 2002-2007 John Goerzen
3 # <jgoerzen@complete.org>
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.
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.
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
19 import re
, string
, types
, binascii
, socket
, time
, random
, subprocess
, sys
, os
20 from offlineimap
.ui
import UIBase
23 # Import the symbols we need that aren't exported by default
24 from imaplib
import IMAP4_PORT
, IMAP4_SSL_PORT
, InternalDate
, Mon2num
27 class IMAP4_Tunnel(IMAP4
):
28 """IMAP4 client class over a tunnel
30 Instantiate with: IMAP4_Tunnel(tunnelcmd)
32 tunnelcmd -- shell command to generate the tunnel.
33 The result will be in PREAUTH stage."""
35 def __init__(self
, tunnelcmd
):
36 IMAP4
.__init__(self
, tunnelcmd
)
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
)
46 while len(retval
) < size
:
47 retval
+= self
.infd
.read(size
- len(retval
))
51 return self
.infd
.readline()
54 self
.outfd
.write(data
)
62 def __init__(self
, sslsock
):
63 self
.sslsock
= sslsock
67 return self
.sslsock
.write(s
)
70 return self
.sslsock
.read(n
)
74 # Return the stuff in readbuf, even if less than n.
75 # It might contain the rest of the line, and if we try to
76 # read more, might block waiting for data that is not
78 bytesfrombuf
= min(n
, len(self
.readbuf
))
79 retval
= self
.readbuf
[:bytesfrombuf
]
80 self
.readbuf
= self
.readbuf
[bytesfrombuf
:]
82 retval
= self
._read
(n
)
84 self
.readbuf
= retval
[n
:]
91 linebuf
= self
.read(1024)
92 nlindex
= linebuf
.find("\n")
94 retval
+= linebuf
[:nlindex
+ 1]
95 self
.readbuf
= linebuf
[nlindex
+ 1:] + self
.readbuf
100 def new_mesg(self
, s
, secs
=None):
103 tm
= time
.strftime('%M:%S', time
.localtime(secs
))
104 UIBase
.getglobalui().debug('imap', ' %s.%02d %s' % (tm
, (secs
*100)%100, s
))
106 def new_open(self
, host
= '', port
= IMAP4_PORT
):
107 """Setup connection to remote server on "host:port"
108 (default: localhost:standard IMAP4 port).
109 This connection will be used by the routines:
110 read, readline, send, shutdown.
114 res
= socket
.getaddrinfo(host
, port
, socket
.AF_UNSPEC
,
116 self
.sock
= socket
.socket(af
, socktype
, proto
)
118 # Try each address returned by getaddrinfo in turn until we
119 # manage to connect to one.
120 # Try all the addresses in turn until we connect()
123 af
, socktype
, proto
, canonname
, sa
= remote
124 self
.sock
= socket
.socket(af
, socktype
, proto
)
125 last_error
= self
.sock
.connect_ex(sa
)
132 raise socket
.error(last_error
)
133 self
.file = self
.sock
.makefile('rb')
135 def new_open_ssl(self
, host
= '', port
= IMAP4_SSL_PORT
):
136 """Setup connection to remote server on "host:port".
137 (default: localhost:standard IMAP4 SSL port).
138 This connection will be used by the routines:
139 read, readline, send, shutdown.
143 #This connects to the first ip found ipv4/ipv6
144 #Added by Adriaan Peeters <apeeters@lashout.net> based on a socket
145 #example from the python documentation:
146 #http://www.python.org/doc/lib/socket-example.html
147 res
= socket
.getaddrinfo(host
, port
, socket
.AF_UNSPEC
,
149 # Try all the addresses in turn until we connect()
152 af
, socktype
, proto
, canonname
, sa
= remote
153 self
.sock
= socket
.socket(af
, socktype
, proto
)
154 last_error
= self
.sock
.connect_ex(sa
)
161 raise socket
.error(last_error
)
162 if sys
.version_info
[0] <= 2 and sys
.version_info
[1] <= 2:
163 self
.sslobj
= socket
.ssl(self
.sock
, self
.keyfile
, self
.certfile
)
165 self
.sslobj
= socket
.ssl(self
.sock
._sock
, self
.keyfile
, self
.certfile
)
166 self
.sslobj
= sslwrapper(self
.sslobj
)
168 mustquote
= re
.compile(r
"[^\w!#$%&'+,.:;<=>?^`|~-]")
170 def Internaldate2epoch(resp
):
171 """Convert IMAP4 INTERNALDATE to UT.
173 Returns seconds since the epoch.
176 mo
= InternalDate
.match(resp
)
180 mon
= Mon2num
[mo
.group('mon')]
181 zonen
= mo
.group('zonen')
183 day
= int(mo
.group('day'))
184 year
= int(mo
.group('year'))
185 hour
= int(mo
.group('hour'))
186 min = int(mo
.group('min'))
187 sec
= int(mo
.group('sec'))
188 zoneh
= int(mo
.group('zoneh'))
189 zonem
= int(mo
.group('zonem'))
191 # INTERNALDATE timezone must be subtracted to get UT
193 zone
= (zoneh
*60 + zonem
)*60
197 tt
= (year
, mon
, day
, hour
, min, sec
, -1, -1, -1)
199 return time
.mktime(tt
)