Handle malformed timestamps (which would cause an unhandled exception and hork the connection)

and XEP-82 (dashes in timestamps).
This commit is contained in:
Travis Shirk 2007-06-29 21:21:08 +00:00
parent 6ca2d76cba
commit 270c956db3
3 changed files with 16 additions and 4 deletions

View File

@ -24,7 +24,7 @@ import socket
import sys import sys
from time import (altzone, daylight, gmtime, localtime, mktime, strftime, from time import (altzone, daylight, gmtime, localtime, mktime, strftime,
strptime, time as time_time, timezone, tzname) time as time_time, timezone, tzname)
from calendar import timegm from calendar import timegm
import socks5 import socks5
@ -1424,7 +1424,7 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
mtype = msg.getType() mtype = msg.getType()
subject = msg.getSubject() # if not there, it's None subject = msg.getSubject() # if not there, it's None
tim = msg.getTimestamp() tim = msg.getTimestamp()
tim = strptime(tim, '%Y%m%dT%H:%M:%S') tim = helpers.datetime_tuple(tim)
tim = localtime(timegm(tim)) tim = localtime(timegm(tim))
frm = helpers.get_full_jid_from_iq(msg) frm = helpers.get_full_jid_from_iq(msg)
jid = helpers.get_jid_from_iq(msg) jid = helpers.get_jid_from_iq(msg)
@ -1614,7 +1614,7 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
elif namespace == common.xmpp.NS_DELAY: elif namespace == common.xmpp.NS_DELAY:
# XEP-0091 # XEP-0091
tim = prs.getTimestamp() tim = prs.getTimestamp()
tim = strptime(tim, '%Y%m%dT%H:%M:%S') tim = helpers.datetime_tuple(tim)
timestamp = localtime(timegm(tim)) timestamp = localtime(timegm(tim))
elif namespace == 'http://delx.cjb.net/protocol/roster-subsync': elif namespace == 'http://delx.cjb.net/protocol/roster-subsync':
# see http://trac.gajim.org/ticket/326 # see http://trac.gajim.org/ticket/326

View File

@ -970,3 +970,15 @@ def get_avatar_path(prefix):
if os.path.exists(file_): if os.path.exists(file_):
return file_ return file_
return None return None
def datetime_tuple(timestamp):
'''Converts timestamp using strptime and the format: %Y%m%dT%H:%M:%S
Because of various datetime formats are used the following exceptions
are handled:
- Optional milliseconds appened to the string are removed
- XEP-082 datetime strings have all '-' cahrs removed to meet
the above format.'''
timestamp = timestamp.split('.')[0]
timestamp = timestamp.replace('-', '')
from time import strptime
return strptime(timestamp, '%Y%m%dT%H:%M:%S')

View File

@ -640,7 +640,7 @@ class ConnectionHandlersZeroconf(ConnectionVcard, ConnectionBytestream):
mtype = msg.getType() mtype = msg.getType()
subject = msg.getSubject() # if not there, it's None subject = msg.getSubject() # if not there, it's None
tim = msg.getTimestamp() tim = msg.getTimestamp()
tim = time.strptime(tim, '%Y%m%dT%H:%M:%S') tim = helpers.datetime_tuple(tim)
tim = time.localtime(timegm(tim)) tim = time.localtime(timegm(tim))
frm = msg.getFrom() frm = msg.getFrom()
if frm == None: if frm == None: