aboutsummaryrefslogtreecommitdiffstats
path: root/archivemail.py
diff options
context:
space:
mode:
authorNikolaus Schulz <microschulz@web.de>2008-03-15 15:59:05 +0000
committerNikolaus Schulz <microschulz@web.de>2008-03-15 15:59:05 +0000
commit22e23d9d389298df4252816c1c470836e407be25 (patch)
tree6b524a5bad2f5649ac44b7fe25c7d998b034449e /archivemail.py
parentf6f7903f5872eb05353020a36ed360a76ef282fb (diff)
downloadarchivemail-22e23d9d389298df4252816c1c470836e407be25.tar.gz
archivemail-22e23d9d389298df4252816c1c470836e407be25.tar.bz2
archivemail-22e23d9d389298df4252816c1c470836e407be25.zip
Sanitized parsing some IMAP server responses by switching to regular
expressions.
Diffstat (limited to 'archivemail.py')
-rwxr-xr-xarchivemail.py16
1 files changed, 8 insertions, 8 deletions
diff --git a/archivemail.py b/archivemail.py
index b3f0fc8..b01ea35 100755
--- a/archivemail.py
+++ b/archivemail.py
@@ -71,7 +71,7 @@ import errno
# From_ mangling regex.
from_re = re.compile(r'^From ', re.MULTILINE)
-
+imapsize_re = re.compile(r'^(?P<msn>[0-9]+) \(RFC822\.SIZE (?P<size>[0-9]+)\)')
############## class definitions ###############
@@ -1432,8 +1432,8 @@ def _archive_imap(mailbox_name, final_archive_name):
# where the first number is the message sequence number, the second is
# the size.
for x in response:
- msn, blurb, msg_size = x.split()
- msg_size = int(msg_size.rstrip(')'))
+ m = imapsize_re.match(x)
+ msn, msg_size = m.group('msn'), int(m.group('size'))
stats.another_message(msg_size)
if msn in message_list:
stats.another_archived(msg_size)
@@ -1590,14 +1590,14 @@ def imap_getdelim(imap_server):
result, response = imap_server.list(pattern='%')
if result != 'OK': unexpected_error("Error listing directory; "
"server says '%s'" % response[0])
+
# Response should be a list of strings like
# '(\\Noselect \\HasChildren) "." "boxname"'
# We parse only the first list item and just grab the delimiter.
- try:
- i = response[0].index(')')
- except (ValueError, AttributeError):
- unexpected_error("get_delim(): cannot parse '%s'" % response[0])
- delim = response[0][i+2:i+5].strip('"')
+ m = re.match(r'\([^\)]*\) (?P<delim>"."|NIL)', response[0])
+ if not m:
+ unexpected_error("imap_getdelim(): cannot parse '%s'" % response[0])
+ delim = m.group('delim').strip('"')
vprint("Found mailbox hierarchy delimiter: '%s'" % delim)
if delim == "NIL":
return None