home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / msn / MSNUtil.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  6.9 KB  |  195 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. from struct import pack, unpack
  6. from urllib2 import quote, unquote
  7. from base64 import b64encode, b64decode
  8. from string import zfill
  9. from util import get_func_name, get_func, pythonize, to_storage, Timer, default_timer, fmt_to_dict
  10. from util.auxencodings import fuzzydecode
  11. from logging import getLogger
  12. log = getLogger('msn.util')
  13. from mail.passport import make_auth_envelope
  14. msgtypes = {
  15.     'text/x-msmsgsprofile': 'profile',
  16.     'text/x-msmsgsinitialmdatanotification': 'notification',
  17.     'text/x-msmsgscontrol': 'control',
  18.     'text/plain': 'plain',
  19.     'text/x-msmsgsinitialemailnotification': 'init_email',
  20.     'text/x-msmsgsemailnotification': 'new_email',
  21.     'text/x-msmsgsinvite': 'invite',
  22.     'text/x-msnmsgr-datacast': 'datacast',
  23.     'application/x-msnmsgrp2p': 'p2p',
  24.     'text/x-clientcaps': 'caps',
  25.     'text/x-msmsgsoimnotification': 'oims' }
  26.  
  27. def utf8_encode(str):
  28.     return unicode(str, 'utf-8')
  29.  
  30.  
  31. def utf8_decode(str):
  32.     return str.encode('utf-8')
  33.  
  34.  
  35. def url_encode(str):
  36.     return quote(str)
  37.  
  38.  
  39. def url_decode(str):
  40.     return unquote(str)
  41.  
  42.  
  43. def base64_encode(s):
  44.     return s.encode('base64').replace('\n', '')
  45.  
  46.  
  47. def base64_decode(s):
  48.     return s.decode('base64')
  49.  
  50.  
  51. def utf16_encode(str):
  52.     
  53.     try:
  54.         return unicode(str, 'utf-16')
  55.     except TypeError:
  56.         if isinstance(str, unicode):
  57.             return str.encode('utf-16')
  58.         return fuzzydecode(s, 'utf-8').encode('utf-16')
  59.     except:
  60.         isinstance(str, unicode)
  61.  
  62.  
  63.  
  64. def utf16_decode(str):
  65.     return str.decode('utf-16')
  66.  
  67. mime_to_dict = fmt_to_dict(';', '=')
  68. csd_to_dict = fmt_to_dict(',', '=')
  69.  
  70. def mime_to_storage(str):
  71.     info = mime_to_dict(str)
  72.     for k in info.keys():
  73.         info[pythonize(k)] = info[k]
  74.     
  75.     return to_storage(info)
  76.  
  77.  
  78. def csd_to_storage(str):
  79.     info = csd_to_dict(str)
  80.     for k in info.keys():
  81.         info[pythonize(k)] = info.pop(k)
  82.     
  83.     return to_storage(info)
  84.  
  85.  
  86. def gen_msg_payload(obj, socket, trid, msg, src_account, src_display, *params):
  87.     type = msg.get('Content-Type', None)
  88.     if type:
  89.         type = type.split(';')[0]
  90.     
  91.     if type not in msgtypes:
  92.         log.critical("Can't handle type %s", type)
  93.         return None
  94.     func = get_func(obj, get_func_name(2) + '_%s' % msgtypes[type])
  95.     if func:
  96.         func(socket, msg, src_account, src_display, *params)
  97.     
  98.  
  99.  
  100. def dict_to_mime_header(d):
  101.     hdr = [
  102.         'MIME-Version: 1.0']
  103.     ctype = 'Content-Type'
  104.     ctype_val = d.pop(ctype, 'text/plain; charset=UTF-8')
  105.     hdr.append('%s: %s' % (ctype, ctype_val))
  106.     for k, v in d.items():
  107.         if isinstance(v, dict):
  108.             v = dict_to_mime_val(v)
  109.         
  110.         hdr.append('%s: %s' % (k, v))
  111.     
  112.     return '\r\n'.join(hdr) + '\r\n'
  113.  
  114.  
  115. def dict_to_mime_val(d):
  116.     s = []
  117.     for k, v in d.items():
  118.         None(s.append if k else '' + '%s' % v)
  119.     
  120.     return '; '.join(s)
  121.  
  122.  
  123. def bgr_to_rgb(c):
  124.     s = '000000' + c[-6:]
  125.     (b, g, r) = [ a + b for a, b in zip(s[::2], s[1::2]) ]
  126.     return r + g + b
  127.  
  128.  
  129. def rgb_to_bgr(s):
  130.     (r, g, b) = [ a + b for a, b in zip(s[::2], s[1::2]) ]
  131.     s = b + g + r
  132.     while s.startswith('0'):
  133.         s = s[1:]
  134.         continue
  135.         []
  136.     return s
  137.  
  138.  
  139. class FuncProducer(object):
  140.     
  141.     def __init__(self, f):
  142.         object.__init__(self)
  143.         self.f = f
  144.  
  145.     
  146.     def more(self):
  147.         
  148.         try:
  149.             v = self.f()
  150.         except:
  151.             v = None
  152.         finally:
  153.             return v
  154.  
  155.  
  156.  
  157.  
  158. def q_untilready(func):
  159.     
  160.     def wrapper(self, *a, **k):
  161.         if self.state == 'ready' and self.session_id == None and self.type == 'sb':
  162.             print 'ERROR STATE DETECTED: CALLING DISCONNECT', self
  163.             self.disconnect()
  164.         
  165.         print 'in quntilready -- %r' % self
  166.         if self.state != 'ready':
  167.             self._q.append((func, (self,) + a, k))
  168.             if self.state == 'disconnected':
  169.                 self.connect()
  170.             elif self.state != 'calling':
  171.                 self.invite(self.buddy)
  172.             
  173.         else:
  174.             return func(self, *a, **k)
  175.         return self.state != 'ready'
  176.  
  177.     return wrapper
  178.  
  179. import functools
  180.  
  181. def dispatch(f):
  182.     
  183.     def wrapper(self, *a, **k):
  184.         print 'DISPATCH type:', self.type
  185.         fname = '_%s_%s' % (f.func_name.lstrip('_'), self.type)
  186.         if f(self, *a, **k):
  187.             print 'dispatch: calling %s' % fname
  188.             return getattr(self, fname)(*a, **k)
  189.         print 'dispatch: not calling %s' % fname
  190.         return False
  191.  
  192.     wrapper = (functools.wraps(f),)(wrapper)
  193.     return wrapper
  194.  
  195.