home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / yahoo / YahooSocket.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  8.2 KB  |  239 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. from logging import getLogger
  6. from struct import pack
  7. from util import to_storage, dictargcall, traceguard
  8. from util.primitives.structures import unpack_named
  9. from yahooutil import YahooLoginError, format_packet, to_ydict, from_ydict, from_ydict_iter, header_pack, header_size, header_desc
  10. import common
  11. import sys
  12. import traceback
  13. from  import yahoolookup as ylookup
  14. DEFAULT_YMSG_VERSION = 17
  15. log = getLogger('yahoo')
  16. loginlog = getLogger('yahoo.login')
  17. packets_log = getLogger('yahoo.packets')
  18.  
  19. class YahooConnectionBase(object):
  20.     
  21.     def __init__(self, yahoo):
  22.         self.yahoo = yahoo
  23.         self.session_id = 0
  24.         packets_log.debug('initializing active_gens, self.id: %d', id(self))
  25.         self.active_gens = { }
  26.  
  27.     
  28.     def handle_packet(self, header, data):
  29.         self.session_id = header.session_id
  30.         packets_log.debug('header %r', (header.command, header.status))
  31.         packets_log.debug('active_gens %r', self.active_gens)
  32.         if (header.command, header.status) in self.active_gens:
  33.             gen = self.active_gens.pop((header.command, header.status))
  34.             self.async_proc(gen, header, data)
  35.             return None
  36.         command_str = ylookup.commands.get(header.command, '')
  37.         status_str = ylookup.statuses.get(header.status, '')
  38.         target = self.yahoo
  39.         fnname = '%s_%s' % (command_str, status_str)
  40.         traceguard.__enter__()
  41.         
  42.         try:
  43.             packet_str = format_packet(data, sensitive = not getattr(sys, 'DEV', True))
  44.             packets_log.debug('~~> %s\n%s', fnname, packet_str)
  45.         finally:
  46.             pass
  47.  
  48.         raw_fn = '%s_raw' % fnname
  49.         if hasattr(target, raw_fn):
  50.             
  51.             try:
  52.                 return getattr(target, raw_fn)(self.from_ydict_iter(data))
  53.             except Exception:
  54.                 traceguard.__exit__
  55.                 traceguard.__exit__
  56.                 traceguard
  57.                 traceback.print_exc()
  58.             except:
  59.                 (header.command, header.status) in self.active_gens
  60.             finally:
  61.                 return None
  62.  
  63.         
  64.         if command_str and status_str:
  65.             fn = '%s_%s' % (command_str, status_str)
  66.             if hasattr(target, fn):
  67.                 func = getattr(target, fn)
  68.                 
  69.                 try:
  70.                     return dictargcall(func, self.from_ydict(data), ylookup.ykeys)
  71.                 except Exception:
  72.                     traceback.print_exc()
  73.                     print >>sys.stderr, '  File "%s", line %d, in %s' % (func.func_code.co_filename, func.func_code.co_firstlineno, func.func_name)
  74.                     return None
  75.                 
  76.  
  77.             None<EXCEPTION MATCH>Exception
  78.             log.warning('no fn, %s', fn)
  79.         
  80.         unhandled_text = None([
  81.             None,
  82.             ''.join,
  83.             'unhandled packet: ' if command_str else str(header.command),
  84.             '_' if status_str else str(header.status),
  85.             '\n',
  86.             '\n'.join((lambda .0: for k, v in .0:
  87. '%s: %s' % (k, v))(self.from_ydict_iter(data)))])
  88.         log.warning('%s:::%r', unhandled_text, data)
  89.  
  90.     
  91.     def async_proc(self, gen, header = None, data = None):
  92.         
  93.         try:
  94.             args = None if header else None
  95.             (cmd, wait_on, packet) = gen.send(args)
  96.             while cmd == 'send':
  97.                 self.push(packet)
  98.                 (cmd, wait_on, packet) = gen.next()
  99.             if cmd == 'wait':
  100.                 packets_log.debug('queueing: %r %r', wait_on, gen)
  101.                 self.active_gens[wait_on] = gen
  102.                 packets_log.debug('self.active_gens: %r', self.active_gens)
  103.         except StopIteration:
  104.             pass
  105.         except YahooLoginError:
  106.             self.yahoo.set_disconnected(self.yahoo.Reasons.BAD_PASSWORD)
  107.  
  108.  
  109.     
  110.     def gsend(self, command, status, data = { }, **kwargs):
  111.         if 'v' not in kwargs:
  112.             v = DEFAULT_YMSG_VERSION
  113.         else:
  114.             v = kwargs['v']
  115.         if isinstance(command, str):
  116.             command = ylookup.commands[command]
  117.         
  118.         if isinstance(status, str):
  119.             status = ylookup.statuses[status]
  120.         
  121.         packet = self.make_ypacket(command, status, v, data)
  122.         return ('send', None, packet)
  123.  
  124.     
  125.     def gwait(self, command, status, err_fn):
  126.         command = ylookup.commands[command]
  127.         status = ylookup.statuses[status]
  128.         return ('wait', (command, status), err_fn)
  129.  
  130.     
  131.     def make_ypacket(self, command, status, version = DEFAULT_YMSG_VERSION, data = { }):
  132.         if not isinstance(command, (int, long)):
  133.             raise TypeError('command is', command, 'but should be an int!')
  134.         isinstance(command, (int, long))
  135.         if not isinstance(status, (int, long)):
  136.             raise TypeError('status is', status, 'but should be an int!')
  137.         isinstance(status, (int, long))
  138.         return self.ypacket_pack(command, status, version, data)
  139.  
  140.     
  141.     def ysend(self, command, status, version = DEFAULT_YMSG_VERSION, data = { }):
  142.         packet = self.make_ypacket(command, status, version, data)
  143.         self.push(packet)
  144.  
  145.  
  146.  
  147. class YahooSocketBase(YahooConnectionBase):
  148.     
  149.     def to_ydict(data):
  150.         return to_ydict(data)
  151.  
  152.     to_ydict = staticmethod(to_ydict)
  153.     
  154.     def from_ydict(data):
  155.         return from_ydict(data)
  156.  
  157.     from_ydict = staticmethod(from_ydict)
  158.     
  159.     def from_ydict_iter(data):
  160.         return from_ydict_iter(data)
  161.  
  162.     from_ydict_iter = staticmethod(from_ydict_iter)
  163.     
  164.     def ypacket_pack(self, command, status, version, data):
  165.         data = self.to_ydict(data)
  166.         vars = (header_pack, 'YMSG', version, 0, len(data), command, status, self.session_id)
  167.         return pack(*vars) + data
  168.  
  169.  
  170.  
  171. class YahooSocket(YahooSocketBase, common.socket):
  172.     
  173.     def __init__(self, yahoo, server):
  174.         common.socket.__init__(self)
  175.         YahooSocketBase.__init__(self, yahoo)
  176.         self.server = server
  177.         self.set_terminator(header_size)
  178.         self.getting_header = True
  179.         self.data = []
  180.         self.byte_count = 0
  181.  
  182.     
  183.     def __str__(self):
  184.         return 'YahooSocket(%s:%d, %d bytes in)' % (self.server[0], self.server[1], self.byte_count)
  185.  
  186.     
  187.     def __repr__(self):
  188.         return '<%s(%s:%d) - sId: %s, bytes in: %d>' % (self.__class__.__name__, self.server[0], self.server[1], self.session_id, self.byte_count)
  189.  
  190.     
  191.     def handle_connect(self):
  192.         raise NotImplementedError
  193.  
  194.     
  195.     def handle_close(self):
  196.         raise NotImplementedError
  197.  
  198.     
  199.     def collect_incoming_data(self, data):
  200.         self.data.append(data)
  201.         self.byte_count += len(data)
  202.  
  203.     
  204.     def handle_error(self, *a, **k):
  205.         traceback.print_exc()
  206.         raise NotImplementedError
  207.  
  208.     
  209.     def handle_expt(self):
  210.         raise NotImplementedError
  211.  
  212.     
  213.     def found_terminator(self):
  214.         datastr = ''.join(self.data)
  215.         if self.getting_header:
  216.             self.getting_header = False
  217.             self.header = to_storage(unpack_named(*header_desc + tuple([
  218.                 datastr])))
  219.             if self.header.ymsg != 'YMSG' or self.header.size < 0:
  220.                 return log.warning('invalid packet')
  221.             if self.header.size > 0:
  222.                 self.set_terminator(self.header.size)
  223.             else:
  224.                 self.getting_header = True
  225.                 self.set_terminator(header_size)
  226.                 self.handle_packet(self.header, datastr)
  227.                 self.data = []
  228.         else:
  229.             self.getting_header = True
  230.             self.set_terminator(header_size)
  231.             self.handle_packet(self.header, datastr[header_size:])
  232.             self.data = []
  233.  
  234.     
  235.     def push(self, pkt):
  236.         super(YahooSocket, self).push(pkt)
  237.  
  238.  
  239.