Commit 1c726c2e authored by valtron's avatar valtron

organizing

parent 5c2106a1
......@@ -8,11 +8,6 @@ class Session:
self.client = None
self.state = state
def data_received(self, data: bytes) -> None:
state = self.state
for incoming_event in state.reader.data_received(data):
state.apply_incoming_event(incoming_event, self)
def send_event(self, outgoing_event):
raise NotImplementedError('Session.send_event')
......@@ -75,11 +70,8 @@ class PollingSession(Session):
return data
class SessionState:
def __init__(self, reader):
self.reader = reader
def apply_incoming_event(self, incoming_event, sess: Session) -> None:
raise NotImplementedError('SessionState.apply_incoming_event')
def __init__(self):
self.front_specific = {}
def on_connection_lost(self, sess: Session) -> None:
raise NotImplementedError('SessionState.on_connection_lost')
......@@ -50,4 +50,4 @@ class ListenerMSNP(asyncio.Protocol):
self.transport = None
def data_received(self, data):
self.sess.data_received(data)
self.sess.state.data_received(data, self.sess)
......@@ -116,7 +116,7 @@ async def handle_http_gateway(req):
sess.on_connect(req.transport)
# Read incoming messages
sess.data_received(await req.read())
sess.state.data_received(await req.read(), sess)
# Write outgoing messages
body = sess.on_disconnect()
......
......@@ -42,9 +42,9 @@ def build_msnp_presence_notif(trid, ctc, dialect, backend):
rst = []
ctc_sess = first_in_iterable(backend.util_get_sessions_by_user(head))
if dialect >= 8:
rst.append(ctc_sess.state.capabilities)
rst.append(ctc_sess.state.front_specific.get('msn_capabilities') or 0)
if dialect >= 9:
rst.append(encode_msnobj(ctc_sess.state.msnobj or '<msnobj/>'))
rst.append(encode_msnobj(ctc_sess.state.front_specific.get('msn_msnobj') or '<msnobj/>'))
if dialect >= 18:
yield (*frst, status.substatus.name, encode_email_networkid(head.email, networkid), status.name, *rst)
......
......@@ -379,8 +379,8 @@ def _m_chg(sess, trid, sts_name, capabilities = None, msnobj = None):
sess.state.backend.me_update(sess, {
'substatus': getattr(Substatus, sts_name),
})
sess.state.capabilities = capabilities
sess.state.msnobj = msnobj
sess.state.front_specific['msn_capabilities'] = capabilities
sess.state.front_specific['msn_msnobj'] = msnobj
sess.send_reply('CHG', trid, sts_name, capabilities, encode_msnobj(msnobj))
# Send ILNs
......
......@@ -36,7 +36,7 @@ def _m_ans(sess, trid, arg, token, sessid):
dialect = extra_data['dialect']
state.dialect = dialect
state.chat = chat
state.capabilities = extra_data['capabilities']
state.front_specific['msn_capabilities'] = extra_data['msn_capabilities']
roster = [
(sc, su) for (sc, su) in chat.get_roster(sess)
if su != sess.user
......@@ -45,7 +45,7 @@ def _m_ans(sess, trid, arg, token, sessid):
for i, (sc, su) in enumerate(roster):
extra = ()
if dialect >= 13:
extra = (sc.state.capabilities,)
extra = (sc.state.front_specific.get('msn_capabilities') or 0,)
sess.send_reply('IRO', trid, i + 1, l, su.email, su.status.name, *extra)
sess.send_reply('ANS', trid, 'OK')
......
......@@ -55,7 +55,7 @@ class MSNPWriter:
extra = ()
dialect = self._sess_state.dialect
if dialect >= 13:
extra = (self._sess_state.capabilities,)
extra = (self._sess_state.front_specific.get('msn_capabilities') or 0,)
self._write(['JOI', user.email, user.status.name, *extra])
return
if isinstance(outgoing_event, event.ChatMessage):
......@@ -156,19 +156,29 @@ def _msnp_encode(m: List[object], buf, logger) -> None:
if data is not None:
w(data)
class MSNP_NS_SessState(SessionState):
class MSNP_SessState(SessionState):
def __init__(self, reader, backend):
super().__init__(reader)
super().__init__()
self.reader = reader
self.backend = backend
self.dialect = None
def data_received(self, data: bytes, sess: Session) -> None:
for incoming_event in self.reader.data_received(data):
self.apply_incoming_event(incoming_event, sess)
def apply_incoming_event(self, incoming_event, sess: Session) -> None:
raise NotImplementedError('MSNP_SessState.apply_incoming_event')
class MSNP_NS_SessState(MSNP_SessState):
def __init__(self, reader, backend):
super().__init__(reader, backend)
self.usr_email = None
self.syn_ser = None
self.iln_sent = False
self.capabilities = 0
self.msnobj = None
def get_sb_extra_data(self):
return { 'dialect': self.dialect, 'capabilities': self.capabilities }
return { 'dialect': self.dialect, 'msn_capabilities': self.front_specific.get('msn_capabilities') or 0 }
def apply_incoming_event(self, incoming_event, sess) -> None:
msg_ns.apply(incoming_event, sess)
......@@ -176,13 +186,10 @@ class MSNP_NS_SessState(SessionState):
def on_connection_lost(self, sess: Session) -> None:
self.backend.on_leave(sess)
class MSNP_SB_SessState(SessionState):
class MSNP_SB_SessState(MSNP_SessState):
def __init__(self, reader, backend):
super().__init__(reader)
self.backend = backend
self.dialect = None
super().__init__(reader, backend)
self.chat = None
self.capabilities = 0
def apply_incoming_event(self, incoming_event, sess) -> None:
msg_sb.apply(incoming_event, sess)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment