rearranged _messageCB and split it into multiple methods

This commit is contained in:
Brendan Taylor 2008-04-06 22:21:46 +00:00
parent c8cae71099
commit d034078c03
1 changed files with 118 additions and 80 deletions

View File

@ -1548,24 +1548,26 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
if msg.getTag('event') is not None: if msg.getTag('event') is not None:
self._pubsubEventCB(con, msg) self._pubsubEventCB(con, msg)
return return
# check if the message is a xep70-confirmation-request # check if the message is a xep70-confirmation-request
if msg.getTag('confirm') and msg.getTag('confirm').namespace == \ if msg.getTag('confirm') and msg.getTag('confirm').namespace == \
common.xmpp.NS_HTTP_AUTH: common.xmpp.NS_HTTP_AUTH:
self._HttpAuthCB(con, msg) self._HttpAuthCB(con, msg)
return return
# check if the message is a XEP 0020 feature negotiation request
if msg.getTag('feature') and msg.getTag('feature').namespace == \ if msg.getTag('feature') and msg.getTag('feature').namespace == \
common.xmpp.NS_FEATURE: common.xmpp.NS_FEATURE:
if gajim.HAVE_PYCRYPTO: if gajim.HAVE_PYCRYPTO:
self._FeatureNegCB(con, msg, session) self._FeatureNegCB(con, msg, session)
return return
# check if the message is initiating an ESession negotiation
if msg.getTag('init') and msg.getTag('init').namespace == \ if msg.getTag('init') and msg.getTag('init').namespace == \
common.xmpp.NS_ESESSION_INIT: common.xmpp.NS_ESESSION_INIT:
self._InitE2ECB(con, msg, session) self._InitE2ECB(con, msg, session)
encrypted = False encrypted = False
tim = msg.getTimestamp()
tim = helpers.datetime_tuple(tim)
tim = localtime(timegm(tim))
e2e_tag = msg.getTag('c', namespace = common.xmpp.NS_STANZA_CRYPTO) e2e_tag = msg.getTag('c', namespace = common.xmpp.NS_STANZA_CRYPTO)
if e2e_tag: if e2e_tag:
@ -1579,35 +1581,36 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
msgtxt = msg.getBody() msgtxt = msg.getBody()
msghtml = msg.getXHTML() msghtml = msg.getXHTML()
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 = helpers.datetime_tuple(tim) 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)
addressTag = msg.getTag('addresses', namespace = common.xmpp.NS_ADDRESS) addressTag = msg.getTag('addresses', namespace = common.xmpp.NS_ADDRESS)
# Be sure it comes from one of our resource, else ignore address element # Be sure it comes from one of our resource, else ignore address element
if addressTag and jid == gajim.get_jid_from_account(self.name): if addressTag and jid == gajim.get_jid_from_account(self.name):
address = addressTag.getTag('address', attrs={'type': 'ofrom'}) address = addressTag.getTag('address', attrs={'type': 'ofrom'})
if address: if address:
frm = address.getAttr('jid') frm = address.getAttr('jid')
jid = gajim.get_jid_without_resource(frm) jid = gajim.get_jid_without_resource(frm)
no_log_for = gajim.config.get_per('accounts', self.name,
'no_log_for')
if not no_log_for:
no_log_for = ''
no_log_for = no_log_for.split()
chatstate = None
encTag = msg.getTag('x', namespace = common.xmpp.NS_ENCRYPTED) encTag = msg.getTag('x', namespace = common.xmpp.NS_ENCRYPTED)
decmsg = ''
# invitations # invitations
invite = None invite = None
if not encTag: if not encTag:
invite = msg.getTag('x', namespace = common.xmpp.NS_MUC_USER) invite = msg.getTag('x', namespace = common.xmpp.NS_MUC_USER)
if invite and not invite.getTag('invite'): if invite and not invite.getTag('invite'):
invite = None invite = None
delayed = msg.getTag('x', namespace = common.xmpp.NS_DELAY) != None delayed = msg.getTag('x', namespace = common.xmpp.NS_DELAY) != None
msg_id = None msg_id = None
composing_xep = None composing_xep = None
# FIXME: Msn transport (CMSN1.2.1 and PyMSN0.10) do NOT RECOMMENDED # FIXME: Msn transport (CMSN1.2.1 and PyMSN0.10) do NOT RECOMMENDED
# invitation # invitation
# stanza (MUC XEP) remove in 2007, as we do not do NOT RECOMMENDED # stanza (MUC XEP) remove in 2007, as we do not do NOT RECOMMENDED
@ -1621,11 +1624,15 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
self.dispatch('GC_INVITATION', (room_jid, frm, '', None, self.dispatch('GC_INVITATION', (room_jid, frm, '', None,
is_continued)) is_continued))
return return
form_node = None form_node = None
for xtag in xtags: for xtag in xtags:
if xtag.getNamespace() == common.xmpp.NS_DATA: if xtag.getNamespace() == common.xmpp.NS_DATA:
form_node = xtag form_node = xtag
break break
chatstate = None
# chatstates - look for chatstate tags in a message if not delayed # chatstates - look for chatstate tags in a message if not delayed
if not delayed: if not delayed:
composing_xep = False composing_xep = False
@ -1643,45 +1650,87 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
composing_xep = 'XEP-0022' composing_xep = 'XEP-0022'
if not msgtxt and chatstate_child.getTag('composing'): if not msgtxt and chatstate_child.getTag('composing'):
chatstate = 'composing' chatstate = 'composing'
# XEP-0172 User Nickname # XEP-0172 User Nickname
user_nick = msg.getTagData('nick') user_nick = msg.getTagData('nick')
if not user_nick: if not user_nick:
user_nick = '' user_nick = ''
if encTag and self.USE_GPG: if encTag and self.USE_GPG:
#decrypt
encmsg = encTag.getData() encmsg = encTag.getData()
keyID = gajim.config.get_per('accounts', self.name, 'keyid') keyID = gajim.config.get_per('accounts', self.name, 'keyid')
if keyID: if keyID:
decmsg = self.gpg.decrypt(encmsg, keyID) decmsg = self.gpg.decrypt(encmsg, keyID)
# \x00 chars are not allowed in C (so in GTK) # \x00 chars are not allowed in C (so in GTK)
decmsg = decmsg.replace('\x00', '') msgtxt = decmsg.replace('\x00', '')
if decmsg:
msgtxt = decmsg
encrypted = True encrypted = True
if mtype == 'error': if mtype == 'error':
self.dispatch_error_message(msg, msgtxt, session, frm, tim, subject)
return
elif mtype == 'groupchat':
self.dispatch_gc_message(msg, subject, frm, msgtxt, jid, tim, msghtml)
return
elif invite is not None:
self.dispatch_invite_message(invite, frm)
return
elif mtype == 'chat':
if not msg.getTag('body') and chatstate is None: # no <body>
return
log_type = 'chat_msg_recv'
else: # it's a single message
log_type = 'single_msg_recv'
mtype = 'normal'
if session.is_loggable() and msgtxt:
try:
msg_id = gajim.logger.write(log_type, frm, msgtxt,
tim = tim, subject = subject)
except exceptions.PysqliteOperationalError, e:
self.dispatch('ERROR', (_('Disk Write Error'), str(e)))
treat_as = gajim.config.get('treat_incoming_messages')
if treat_as:
mtype = treat_as
session.received(frm, msgtxt, tim, encrypted, mtype, subject, chatstate,
msg_id, composing_xep, user_nick, msghtml, form_node)
# END messageCB
# process and dispatch an error message
def dispatch_error_message(self, msg, msgtxt, session, frm, tim, subject)
error_msg = msg.getError() error_msg = msg.getError()
if not error_msg: if not error_msg:
error_msg = msgtxt error_msg = msgtxt
msgtxt = None msgtxt = None
if session.is_loggable(): if session.is_loggable():
try: try:
gajim.logger.write('error', frm, error_msg, tim = tim, gajim.logger.write('error', frm, error_msg, tim = tim,
subject = subject) subject = subject)
except exceptions.PysqliteOperationalError, e: except exceptions.PysqliteOperationalError, e:
self.dispatch('ERROR', (_('Disk Write Error'), str(e))) self.dispatch('ERROR', (_('Disk Write Error'), str(e)))
self.dispatch('MSGERROR', (frm, msg.getErrorCode(), error_msg, msgtxt,
tim)) self.dispatch('MSGERROR', (frm, msg.getErrorCode(), error_msg, msgtxt, tim))
return
elif mtype == 'groupchat': # process and dispatch a groupchat message
has_timestamp = False def dispatch_gc_message(self, msg, subject, frm, msgtxt, jid, tim, msghtml):
if msg.timestamp: has_timestamp = bool(msg.timestamp)
has_timestamp = True
if subject != None: if subject != None:
self.dispatch('GC_SUBJECT', (frm, subject, msgtxt, has_timestamp)) self.dispatch('GC_SUBJECT', (frm, subject, msgtxt, has_timestamp))
else: else:
statusCode = msg.getStatusCode() statusCode = msg.getStatusCode()
if not msg.getTag('body'): #no <body> if not msg.getTag('body'): #no <body>
# It could be a config change. See # It could be a config change. See
# http://www.xmpp.org/extensions/xep-0045.html#roomconfig-notify # http://www.xmpp.org/extensions/xep-0045.html#roomconfig-notify
@ -1689,54 +1738,43 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
if statusCode != []: if statusCode != []:
self.dispatch('GC_CONFIG_CHANGE', (jid, statusCode)) self.dispatch('GC_CONFIG_CHANGE', (jid, statusCode))
return return
# Ignore message from room in which we are not
# Ignore messages from rooms that we're not in
if not self.last_history_line.has_key(jid): if not self.last_history_line.has_key(jid):
return return
self.dispatch('GC_MSG', (frm, msgtxt, tim, has_timestamp, msghtml, self.dispatch('GC_MSG', (frm, msgtxt, tim, has_timestamp, msghtml,
statusCode)) statusCode))
no_log_for = gajim.config.get_per('accounts', self.name,
'no_log_for')
if not no_log_for:
no_log_for = ''
no_log_for = no_log_for.split()
if self.name not in no_log_for and jid not in no_log_for and not \ if self.name not in no_log_for and jid not in no_log_for and not \
int(float(mktime(tim))) <= self.last_history_line[jid] and msgtxt: int(float(mktime(tim))) <= self.last_history_line[jid] and msgtxt:
try: try:
gajim.logger.write('gc_msg', frm, msgtxt, tim = tim) gajim.logger.write('gc_msg', frm, msgtxt, tim = tim)
except exceptions.PysqliteOperationalError, e: except exceptions.PysqliteOperationalError, e:
self.dispatch('ERROR', (_('Disk Write Error'), str(e))) self.dispatch('ERROR', (_('Disk Write Error'), str(e)))
return
elif mtype == 'chat': # it's type 'chat' def dispatch_invite_message(self, invite, frm)
if not msg.getTag('body') and chatstate is None: #no <body>
return
if msg.getTag('body') and session.is_loggable() and msgtxt:
try:
msg_id = gajim.logger.write('chat_msg_recv', frm, msgtxt,
tim = tim, subject = subject)
except exceptions.PysqliteOperationalError, e:
self.dispatch('ERROR', (_('Disk Write Error'), str(e)))
else: # it's single message
if invite is not None:
item = invite.getTag('invite') item = invite.getTag('invite')
jid_from = item.getAttr('from') jid_from = item.getAttr('from')
reason = item.getTagData('reason') reason = item.getTagData('reason')
item = invite.getTag('password') item = invite.getTag('password')
password = invite.getTagData('password') password = invite.getTagData('password')
is_continued = False is_continued = False
if invite.getTag('invite').getTag('continue'): if invite.getTag('invite').getTag('continue'):
is_continued = True is_continued = True
self.dispatch('GC_INVITATION',(frm, jid_from, reason, password, self.dispatch('GC_INVITATION',(frm, jid_from, reason, password,
is_continued)) is_continued))
return
if session.is_loggable()and msgtxt:
try:
gajim.logger.write('single_msg_recv', frm, msgtxt, tim = tim,
subject = subject)
except exceptions.PysqliteOperationalError, e:
self.dispatch('ERROR', (_('Disk Write Error'), str(e)))
mtype = 'normal'
treat_as = gajim.config.get('treat_incoming_messages')
if treat_as:
mtype = treat_as
session.received(frm, msgtxt, tim, encrypted, mtype, subject, chatstate,
msg_id, composing_xep, user_nick, msghtml, form_node)
# END messageCB
def get_session(self, jid, thread_id, type): def get_session(self, jid, thread_id, type):
'''returns an existing session between this connection and 'jid', returns a new one if none exist.''' '''returns an existing session between this connection and 'jid', returns a new one if none exist.'''