Crashing on invalid dates

Bug #822204 reported by arild
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Backup Gmail
New
Undecided
Unassigned

Bug Description

Excellent backup solution, works very well. One issue I found is that in the processMail function, if the date is wrongly formatted, it fails (for some reason some of my messages had -1:-1:-1 +0000 as the time, however the date part was fine).

However, as it only requires the date part to generate the folder name I did a quick work around where the time is replaced with a valid time if it fails, code attached with change start/end marked with comments:

 def processMail(self, rfc, seen, label):
  mail = email.message_from_string(rfc)
  #CHANGE START---
  datestring = mail.get('date')
  date = email.utils.parsedate(datestring)

  try:
   fold = time.strftime("%Y-%m", date)
  except:
   datestring = datestring[:16] + ' 01:01:01 +0000'
   date = email.utils.parsedate(datestring)
   fold = time.strftime("%Y-%m", date)
  #CHANGE END---

  h = hashlib.sha256(rfc).hexdigest()
  mid = mail.get('message-id')
  if mid == None:
   mid = "<%<email address hidden>>" % (h, )
   mail.add_header('message-id', mid)
   rfc = mail.as_string()
   mail = email.message_from_string(rfc)
  try:
   os.mkdir("%s/%s" % (self.dest, fold))
  except OSError as e:
   pass
  with open("%s/%s/%s" % (self.dest, fold, h), 'w') as f:
   f.write(rfc)

  if mid not in self.mails:
   self.mails[mid] = MailMetaData(mid, h, fold)
   self.mails[mid].seen = seen

  self.mails[mid].labels.add(label)
  self.addToMBox(label, mail)

Revision history for this message
C Fraire (cfraire) wrote :

As you say, given that only the month and year are needed for the folder, an even simpler workaround is to avoid strftime and use the email date directly.

Replace this line:

fold = time.strftime("%Y-%m", date)

with this:

fold = "%04d-%02d" % (date[0], date[1])

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.