home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- from __future__ import with_statement
- from logging import getLogger
- from struct import pack
- from util import to_storage, dictargcall, traceguard
- from util.primitives.structures import unpack_named
- from yahooutil import YahooLoginError, format_packet, to_ydict, from_ydict, from_ydict_iter, header_pack, header_size, header_desc
- import common
- import sys
- import traceback
- from import yahoolookup as ylookup
- DEFAULT_YMSG_VERSION = 17
- log = getLogger('yahoo')
- loginlog = getLogger('yahoo.login')
- packets_log = getLogger('yahoo.packets')
-
- class YahooConnectionBase(object):
-
- def __init__(self, yahoo):
- self.yahoo = yahoo
- self.session_id = 0
- packets_log.debug('initializing active_gens, self.id: %d', id(self))
- self.active_gens = { }
-
-
- def handle_packet(self, header, data):
- self.session_id = header.session_id
- packets_log.debug('header %r', (header.command, header.status))
- packets_log.debug('active_gens %r', self.active_gens)
- if (header.command, header.status) in self.active_gens:
- gen = self.active_gens.pop((header.command, header.status))
- self.async_proc(gen, header, data)
- return None
- command_str = ylookup.commands.get(header.command, '')
- status_str = ylookup.statuses.get(header.status, '')
- target = self.yahoo
- fnname = '%s_%s' % (command_str, status_str)
- traceguard.__enter__()
-
- try:
- packet_str = format_packet(data, sensitive = not getattr(sys, 'DEV', True))
- packets_log.debug('~~> %s\n%s', fnname, packet_str)
- finally:
- pass
-
- raw_fn = '%s_raw' % fnname
- if hasattr(target, raw_fn):
-
- try:
- return getattr(target, raw_fn)(self.from_ydict_iter(data))
- except Exception:
- traceguard.__exit__
- traceguard.__exit__
- traceguard
- traceback.print_exc()
- except:
- (header.command, header.status) in self.active_gens
- finally:
- return None
-
-
- if command_str and status_str:
- fn = '%s_%s' % (command_str, status_str)
- if hasattr(target, fn):
- func = getattr(target, fn)
-
- try:
- return dictargcall(func, self.from_ydict(data), ylookup.ykeys)
- except Exception:
- traceback.print_exc()
- print >>sys.stderr, ' File "%s", line %d, in %s' % (func.func_code.co_filename, func.func_code.co_firstlineno, func.func_name)
- return None
-
-
- None<EXCEPTION MATCH>Exception
- log.warning('no fn, %s', fn)
-
- unhandled_text = None([
- None,
- ''.join,
- 'unhandled packet: ' if command_str else str(header.command),
- '_' if status_str else str(header.status),
- '\n',
- '\n'.join((lambda .0: for k, v in .0:
- '%s: %s' % (k, v))(self.from_ydict_iter(data)))])
- log.warning('%s:::%r', unhandled_text, data)
-
-
- def async_proc(self, gen, header = None, data = None):
-
- try:
- args = None if header else None
- (cmd, wait_on, packet) = gen.send(args)
- while cmd == 'send':
- self.push(packet)
- (cmd, wait_on, packet) = gen.next()
- if cmd == 'wait':
- packets_log.debug('queueing: %r %r', wait_on, gen)
- self.active_gens[wait_on] = gen
- packets_log.debug('self.active_gens: %r', self.active_gens)
- except StopIteration:
- pass
- except YahooLoginError:
- self.yahoo.set_disconnected(self.yahoo.Reasons.BAD_PASSWORD)
-
-
-
- def gsend(self, command, status, data = { }, **kwargs):
- if 'v' not in kwargs:
- v = DEFAULT_YMSG_VERSION
- else:
- v = kwargs['v']
- if isinstance(command, str):
- command = ylookup.commands[command]
-
- if isinstance(status, str):
- status = ylookup.statuses[status]
-
- packet = self.make_ypacket(command, status, v, data)
- return ('send', None, packet)
-
-
- def gwait(self, command, status, err_fn):
- command = ylookup.commands[command]
- status = ylookup.statuses[status]
- return ('wait', (command, status), err_fn)
-
-
- def make_ypacket(self, command, status, version = DEFAULT_YMSG_VERSION, data = { }):
- if not isinstance(command, (int, long)):
- raise TypeError('command is', command, 'but should be an int!')
- isinstance(command, (int, long))
- if not isinstance(status, (int, long)):
- raise TypeError('status is', status, 'but should be an int!')
- isinstance(status, (int, long))
- return self.ypacket_pack(command, status, version, data)
-
-
- def ysend(self, command, status, version = DEFAULT_YMSG_VERSION, data = { }):
- packet = self.make_ypacket(command, status, version, data)
- self.push(packet)
-
-
-
- class YahooSocketBase(YahooConnectionBase):
-
- def to_ydict(data):
- return to_ydict(data)
-
- to_ydict = staticmethod(to_ydict)
-
- def from_ydict(data):
- return from_ydict(data)
-
- from_ydict = staticmethod(from_ydict)
-
- def from_ydict_iter(data):
- return from_ydict_iter(data)
-
- from_ydict_iter = staticmethod(from_ydict_iter)
-
- def ypacket_pack(self, command, status, version, data):
- data = self.to_ydict(data)
- vars = (header_pack, 'YMSG', version, 0, len(data), command, status, self.session_id)
- return pack(*vars) + data
-
-
-
- class YahooSocket(YahooSocketBase, common.socket):
-
- def __init__(self, yahoo, server):
- common.socket.__init__(self)
- YahooSocketBase.__init__(self, yahoo)
- self.server = server
- self.set_terminator(header_size)
- self.getting_header = True
- self.data = []
- self.byte_count = 0
-
-
- def __str__(self):
- return 'YahooSocket(%s:%d, %d bytes in)' % (self.server[0], self.server[1], self.byte_count)
-
-
- def __repr__(self):
- return '<%s(%s:%d) - sId: %s, bytes in: %d>' % (self.__class__.__name__, self.server[0], self.server[1], self.session_id, self.byte_count)
-
-
- def handle_connect(self):
- raise NotImplementedError
-
-
- def handle_close(self):
- raise NotImplementedError
-
-
- def collect_incoming_data(self, data):
- self.data.append(data)
- self.byte_count += len(data)
-
-
- def handle_error(self, *a, **k):
- traceback.print_exc()
- raise NotImplementedError
-
-
- def handle_expt(self):
- raise NotImplementedError
-
-
- def found_terminator(self):
- datastr = ''.join(self.data)
- if self.getting_header:
- self.getting_header = False
- self.header = to_storage(unpack_named(*header_desc + tuple([
- datastr])))
- if self.header.ymsg != 'YMSG' or self.header.size < 0:
- return log.warning('invalid packet')
- if self.header.size > 0:
- self.set_terminator(self.header.size)
- else:
- self.getting_header = True
- self.set_terminator(header_size)
- self.handle_packet(self.header, datastr)
- self.data = []
- else:
- self.getting_header = True
- self.set_terminator(header_size)
- self.handle_packet(self.header, datastr[header_size:])
- self.data = []
-
-
- def push(self, pkt):
- super(YahooSocket, self).push(pkt)
-
-
-