updated session handlers

This commit is contained in:
Brendan Taylor 2007-06-05 22:17:34 +00:00
parent 17c5bf5e52
commit 370818d982
2 changed files with 38 additions and 41 deletions

View File

@ -37,8 +37,6 @@ from common import atom
from common.commands import ConnectionCommands
from common.pubsub import ConnectionPubSub
from common.stanza_session import StanzaSession
STATUS_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd',
'invisible', 'error']
# kind of events we can wait for an answer
@ -1173,10 +1171,6 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
# keep the latest subscribed event for each jid to prevent loop when we
# acknoledge presences
self.subscribed_events = {}
# keep track of sessions this connection has with other JIDs
self.sessions = {}
try:
idle.init()
except:
@ -1203,13 +1197,13 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
self.dispatch('HTTP_AUTH', (method, url, id, iq_obj, msg));
raise common.xmpp.NodeProcessed
def _FeatureNegCB(self, con, stanza, session):
def _FeatureNegCB(self, con, stanza):
gajim.log.debug('FeatureNegCB')
feature = stanza.getTag('feature')
form = common.xmpp.DataForm(node=feature.getTag('x'))
if form['FORM_TYPE'] == 'urn:xmpp:ssn':
self.dispatch('SESSION_NEG', (stanza.getFrom(), session, form))
self.dispatch('SESSION_NEG', (stanza.getFrom(), stanza.getThread(), form))
else:
reply = stanza.buildReply()
reply.setType('error')
@ -1416,17 +1410,6 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
def _messageCB(self, con, msg):
'''Called when we receive a message'''
frm = helpers.get_full_jid_from_iq(msg)
mtype = msg.getType()
thread_id = msg.getThread()
if not mtype:
mtype = 'normal'
session = self.get_session(frm, thread_id, mtype)
if thread_id and not session.received_thread_id:
session.received_thread_id = True
# check if the message is pubsub#event
if msg.getTag('event') is not None:
self._pubsubEventCB(con, msg)
@ -1438,15 +1421,18 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
return
if msg.getTag('feature') and msg.getTag('feature').namespace == \
common.xmpp.NS_FEATURE:
self._FeatureNegCB(con, msg, session)
self._FeatureNegCB(con, msg)
return
msgtxt = msg.getBody()
msghtml = msg.getXHTML()
mtype = msg.getType()
subject = msg.getSubject() # if not there, it's None
thread = msg.getThread()
tim = msg.getTimestamp()
tim = time.strptime(tim, '%Y%m%dT%H:%M:%S')
tim = time.localtime(timegm(tim))
frm = helpers.get_full_jid_from_iq(msg)
jid = helpers.get_jid_from_iq(msg)
no_log_for = gajim.config.get_per('accounts', self.name,
'no_log_for')
@ -1555,23 +1541,52 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
gajim.logger.write('single_msg_recv', frm, msgtxt, tim = tim,
subject = subject)
mtype = 'normal'
treat_as = gajim.config.get('treat_incoming_messages')
if treat_as:
mtype = treat_as
self.dispatch('MSG', (frm, msgtxt, tim, encrypted, mtype,
subject, chatstate, msg_id, composing_jep, user_nick, msghtml, session))
subject, chatstate, msg_id, composing_jep, user_nick, msghtml, thread))
# END messageCB
def get_session(self, jid, thread_id, type):
'''returns an existing session between this connection and 'jid' or starts a new one.'''
session = self.find_session(jid, thread_id, type)
if session:
return session
else:
# it's possible we initiated a session with a bare JID and this is the
# first time we've seen a resource
bare_jid = gajim.get_jid_without_resource(original_jid)
if bare_jid != jid:
session = self.find_session(bare_jid, thread_id, type)
if session:
self.move_session(bare_jid, thread_id, jid.split("/")[1])
return session
return self.make_new_session(jid, thread_id, type)
def find_session(self, jid, thread_id, type):
try:
if type == 'chat' and not thread_id:
return self.find_null_session(jid)
else:
return self.sessions[jid][thread_id]
except KeyError:
return self.make_new_session(jid, thread_id, type)
return None
def move_session(self, original_jid, thread_id, to_resource):
session = self.sessions[jid][thread_id]
del self.sessions[jid][thread_id]
new_jid = gajim.get_jid_without_resource(original_jid) + '/' + to_resource
session.jid = new_jid
if not new_jid in self.sessions:
self.sessions[new_jid] = {}
self.sessions[new_jid][thread_id] = session
def find_null_session(self, jid):
'''returns the session between this connecting and 'jid' that we last sent a message in.'''

View File

@ -31,21 +31,3 @@ class StanzaSession:
def generate_thread_id(self):
return "".join([random.choice(string.letters) for x in xrange(0,32)])
def get_control(self, advanced_notif_num = None):
account = self.conn.name
highest_contact = gajim.contacts.get_contact_with_highest_priority(account, str(self.jid))
contact = gajim.contacts.get_contact(account, self.jid.getStripped(), self.jid.getResource())
if isinstance(contact, list):
# there was no resource (maybe we're reading unread messages after shutdown). just take the first one for now :/
contact = contact[0]
ctrl = gajim.interface.msg_win_mgr.get_control(str(self.jid), account, self.thread_id)
# if not ctrl:
# if highest_contact and contact.resource == highest_contact.resource and not str(self.jid) == gajim.get_jid_from_account(account):
# ctrl = gajim.interface.msg_win_mgr.get_control(self.jid.getStripped(), account, self.thread_id)
if not ctrl and helpers.allow_popup_window(account, advanced_notif_num):
gajim.new_chat(contact, account, resource = resource_for_chat, session = self)
return ctrl