home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / msn / MSNUtil.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  6.9 KB  |  197 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  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.         else:
  59.             return fuzzydecode(s, 'utf-8').encode('utf-16')
  60.     except:
  61.         isinstance(str, unicode)
  62.  
  63.  
  64.  
  65. def utf16_decode(str):
  66.     return str.decode('utf-16')
  67.  
  68. mime_to_dict = fmt_to_dict(';', '=')
  69. csd_to_dict = fmt_to_dict(',', '=')
  70.  
  71. def mime_to_storage(str):
  72.     info = mime_to_dict(str)
  73.     for k in info.keys():
  74.         info[pythonize(k)] = info[k]
  75.     
  76.     return to_storage(info)
  77.  
  78.  
  79. def csd_to_storage(str):
  80.     info = csd_to_dict(str)
  81.     for k in info.keys():
  82.         info[pythonize(k)] = info.pop(k)
  83.     
  84.     return to_storage(info)
  85.  
  86.  
  87. def gen_msg_payload(obj, socket, trid, msg, src_account, src_display, *params):
  88.     type = msg.get('Content-Type', None)
  89.     if type:
  90.         type = type.split(';')[0]
  91.     
  92.     if type not in msgtypes:
  93.         log.critical("Can't handle type %s", type)
  94.         return None
  95.     
  96.     func = get_func(obj, get_func_name(2) + '_%s' % msgtypes[type])
  97.     if func:
  98.         func(socket, msg, src_account, src_display, *params)
  99.     
  100.  
  101.  
  102. def dict_to_mime_header(d):
  103.     hdr = [
  104.         'MIME-Version: 1.0']
  105.     ctype = 'Content-Type'
  106.     ctype_val = d.pop(ctype, 'text/plain; charset=UTF-8')
  107.     hdr.append('%s: %s' % (ctype, ctype_val))
  108.     for k, v in d.items():
  109.         if isinstance(v, dict):
  110.             v = dict_to_mime_val(v)
  111.         
  112.         hdr.append('%s: %s' % (k, v))
  113.     
  114.     return '\r\n'.join(hdr) + '\r\n'
  115.  
  116.  
  117. def dict_to_mime_val(d):
  118.     s = []
  119.     for k, v in d.items():
  120.         None(s.append if k else '' + '%s' % v)
  121.     
  122.     return '; '.join(s)
  123.  
  124.  
  125. def bgr_to_rgb(c):
  126.     s = '000000' + c[-6:]
  127.     (b, g, r) = [ a + b for a, b in zip(s[::2], s[1::2]) ]
  128.     return r + g + b
  129.  
  130.  
  131. def rgb_to_bgr(s):
  132.     (r, g, b) = [ a + b for a, b in zip(s[::2], s[1::2]) ]
  133.     s = b + g + r
  134.     while s.startswith('0'):
  135.         s = s[1:]
  136.         continue
  137.         []
  138.     return s
  139.  
  140.  
  141. class FuncProducer(object):
  142.     
  143.     def __init__(self, f):
  144.         object.__init__(self)
  145.         self.f = f
  146.  
  147.     
  148.     def more(self):
  149.         
  150.         try:
  151.             v = self.f()
  152.         except:
  153.             v = None
  154.         finally:
  155.             return v
  156.  
  157.  
  158.  
  159.  
  160. def q_untilready(func):
  161.     
  162.     def wrapper(self, *a, **k):
  163.         if self.state == 'ready' and self.session_id == None and self.type == 'sb':
  164.             print 'ERROR STATE DETECTED: CALLING DISCONNECT', self
  165.             self.disconnect()
  166.         
  167.         print 'in quntilready -- %r' % self
  168.         if self.state != 'ready':
  169.             self._q.append((func, (self,) + a, k))
  170.             if self.state == 'disconnected':
  171.                 self.connect()
  172.             elif self.state != 'calling':
  173.                 self.invite(self.buddy)
  174.             
  175.         else:
  176.             return func(self, *a, **k)
  177.  
  178.     return wrapper
  179.  
  180. import functools
  181.  
  182. def dispatch(f):
  183.     
  184.     def wrapper(self, *a, **k):
  185.         print 'DISPATCH type:', self.type
  186.         fname = '_%s_%s' % (f.func_name.lstrip('_'), self.type)
  187.         if f(self, *a, **k):
  188.             print 'dispatch: calling %s' % fname
  189.             return getattr(self, fname)(*a, **k)
  190.         else:
  191.             print 'dispatch: not calling %s' % fname
  192.             return False
  193.  
  194.     wrapper = (functools.wraps(f),)(wrapper)
  195.     return wrapper
  196.  
  197.