home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / plugins / msim / MSIMUtil.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  10.9 KB  |  308 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import logging
  5. import path
  6. import lxml.etree as etree
  7. import util
  8. import util.net as net
  9. from util import fmt_to_dict, odict, this_list, try_this
  10. from util.primitives.bits import rol
  11. from hashlib import sha1
  12. from base64 import b64encode, b64decode
  13. log = logging.getLogger('msim.util')
  14. KEYLEN = 128
  15.  
  16. def escape(s):
  17.     if isinstance(s, unicode):
  18.         s = s.encode('utf8')
  19.     
  20.     return s.replace('/', '/1').replace('\\', '/2')
  21.  
  22.  
  23. unescape = lambda s: s.replace('/2', '\\').replace('/1', '/')
  24. from M2Crypto.RC4 import RC4
  25.  
  26. def crypt(nc, password, data = ''):
  27.     (key, salt1) = make_key(nc, password)
  28.     return RC4(key).update(salt1 + data)
  29.  
  30.  
  31. def make_key(nc, password):
  32.     salt = b64decode(nc)
  33.     salt1 = salt[:32]
  34.     salt2 = salt[32:]
  35.     key = sha1(sha1(password.encode('utf-16-le')).digest() + salt2).digest()
  36.     key = key[:KEYLEN / 8]
  37.     rest = key[KEYLEN / 8:]
  38.     return (key, salt1)
  39.  
  40.  
  41. def decrypt(nonce, password, data):
  42.     (key, salt1) = make_key(nonce, password)
  43.     return RC4(key).update(b64decode(data))[len(salt1):]
  44.  
  45.  
  46. def roflcopter(chlnum, sesskey, uid):
  47.     return rol(sesskey, 5) ^ rol(uid, 9) ^ chlnum
  48.  
  49.  
  50. class msmsg(odict):
  51.     
  52.     class CMD(object):
  53.         Get = 1
  54.         Set = 2
  55.         Delete = 3
  56.         Reply = 256
  57.         Action = 512
  58.         Error = 1024
  59.  
  60.     
  61.     def __init__(self, d_or_s = '', **k):
  62.         from_string = False
  63.         if isinstance(d_or_s, basestring):
  64.             self._orig = d_or_s
  65.             d = self.parse(d_or_s)
  66.             from_string = True
  67.         else:
  68.             d = d_or_s
  69.         odict.__init__(self, d, **k)
  70.         if from_string:
  71.             for None in ('body', 'msg'):
  72.                 key = None
  73.                 if key in self:
  74.                     self[key] = unescape(str(self[key]))
  75.                     continue
  76.             
  77.         
  78.         for None in self:
  79.             key = None
  80.             if (None, try_this)((lambda : '\x1c' in self[key]), False) or key == 'body':
  81.                 self[key] = msdict(self[key])
  82.             
  83.             for None in self:
  84.                 key = None
  85.                 if isinstance(self[key], basestring):
  86.                     
  87.                     try:
  88.                         self[key] = eval(self[key], { }, { })
  89.                     except Exception:
  90.                         pass
  91.                     except:
  92.                         None<EXCEPTION MATCH>Exception
  93.                     
  94.  
  95.             
  96.         
  97.  
  98.     
  99.     def mtype(self):
  100.         return self._keys[0]
  101.  
  102.     mtype = property(mtype)
  103.     
  104.     def parse(self, s):
  105.         if not s:
  106.             return { }
  107.         data = s.strip('\\').split('\\')
  108.         return zip(data[::2], data[1::2])
  109.  
  110.     parse = classmethod(parse)
  111.     
  112.     def serialize(self):
  113.         return '\\%s\\final\\' % '\\'.join((lambda .0: for k, v in .0:
  114. '\\'.join((str(k), str(v))))(self.iteritems()))
  115.  
  116.  
  117.  
  118. class msdict(list):
  119.     delim1 = '\x1c'
  120.     delim2 = '='
  121.     
  122.     def __init__(self, _s = '', **k):
  123.         if isinstance(_s, msdict):
  124.             list.__init__(self)
  125.             self[:] = _s[:]
  126.         elif k:
  127.             list.__init__(self, k.items())
  128.         elif _s:
  129.             list.__init__((self,), (lambda .0: for entry in .0:
  130. entry.strip().split(self.delim2, 1))(_s.split(self.delim1)))
  131.         else:
  132.             list.__init__(self)
  133.  
  134.     
  135.     def __getitem__(self, key):
  136.         
  137.         try:
  138.             return list.__getitem__(self, key)[1]
  139.         except Exception:
  140.             r = (filter,)((lambda x: x[0] == key), self)
  141.             if not r:
  142.                 raise KeyError(key)
  143.             r
  144.             if len(r) == 1:
  145.                 return r[0][1]
  146.             return [ x[1] for x in r ]
  147.         except:
  148.             []
  149.  
  150.  
  151.     
  152.     def __setitem__(self, key, val):
  153.         prevs = (filter,)((lambda x: x[0] == key), self)
  154.         if prevs:
  155.             prev = prevs[0]
  156.             prev[1] = val
  157.         else:
  158.             self.append([
  159.                 key,
  160.                 val])
  161.  
  162.     
  163.     def __str__(self):
  164.         
  165.         try:
  166.             return (self.delim1.join,)((lambda .0: for k, v in .0:
  167. '%s%s%s' % (k, self.delim2, escape(v)))(self.items()))
  168.         except Exception:
  169.             print repr(self)
  170.             raise 
  171.  
  172.  
  173.     
  174.     def get(self, key, default = None):
  175.         
  176.         try:
  177.             return self[key]
  178.         except KeyError:
  179.             return default
  180.  
  181.  
  182.     
  183.     def items(self):
  184.         return self[:]
  185.  
  186.     
  187.     def keys(self):
  188.         return _[1]
  189.  
  190.     
  191.     def values(self):
  192.         return [ x[1] for x in self ]
  193.  
  194.  
  195.  
  196. class pipe_list(list):
  197.     delim = '|'
  198.     
  199.     def __init__(self, s = ''):
  200.         if s:
  201.             list.__init__(self, parts)
  202.         else:
  203.             list.__init__(self)
  204.  
  205.     
  206.     def __str__(self):
  207.         return self.delim.join((lambda .0: for x in .0:
  208. escape(x))(self))
  209.  
  210.  
  211.  
  212. class pipe_dict(msdict):
  213.     delim = '|'
  214.     
  215.     def __init__(self, s = ''):
  216.         if s:
  217.             parts = pipe_list(s)
  218.             keys = parts[0::2]
  219.             vals = parts[1::2]
  220.             list.__init__(self, zip(keys, vals))
  221.         else:
  222.             list.__init__(self)
  223.  
  224.     
  225.     def __str__(self):
  226.         
  227.         try:
  228.             return self.delim.join((lambda .0: for x in .0:
  229. escape(str(x)))(util.flatten(self.items())))
  230.         except Exception:
  231.             print 'couldnt str(%r)' % repr(self)
  232.             raise 
  233.  
  234.  
  235.  
  236.  
  237. def obj_list_from_msdict(d):
  238.     if not d:
  239.         return []
  240.     _start_key = d.keys()[0]
  241.     all_objs = []
  242.     for k, v in d.items():
  243.         if k == _start_key:
  244.             this_obj = {
  245.                 k: v }
  246.             all_objs.append(this_obj)
  247.             continue
  248.             continue
  249.         d
  250.         this_obj[k] = v
  251.     
  252.     return all_objs
  253.  
  254.  
  255. def int_to_status(i):
  256.     s = {
  257.         0: 'offline',
  258.         1: 'available',
  259.         2: 'idle',
  260.         5: 'away' }.get(i)
  261.     return s
  262.  
  263.  
  264. def status_to_int(s):
  265.     i = {
  266.         'available': 1,
  267.         'online': 1,
  268.         'idle': 2,
  269.         'away': 5,
  270.         'invisible': 0,
  271.         'offline': 0 }.get(s, 5)
  272.     return i
  273.  
  274.  
  275. def minihtml_to_html(message):
  276.     
  277.     try:
  278.         doc = etree.fromstring(message)
  279.     except Exception:
  280.         e = None
  281.         log.error('Error parsing message as xml. message = %r, error = %r', message, e)
  282.         return message
  283.  
  284.     xslt_doc = etree.parse(_get_mini_xslt_file_path())
  285.     transform = etree.XSLT(xslt_doc)
  286.     return etree.tostring(transform(doc))
  287.  
  288.  
  289. def _get_mini_xslt_file_path():
  290.     return path.path(__file__).parent / 'res' / 'mini_to_html.xsl'
  291.  
  292.  
  293. def html_to_minihtml(message, format):
  294.     E = E
  295.     import lxml.builder
  296.     _message = message
  297.     parts = [
  298.         message]
  299.     bg_color_tuple = format.get('backgroundcolor')
  300.     if bg_color_tuple is None:
  301.         bg_color = 'transparent'
  302.     else:
  303.         bg_color = 'rgba(%s, %s, %s, %s)' % tuple(bg_color_tuple)
  304.     doc = E.p(E.f(E.c(E.b(v = bg_color, *parts), v = 'rgba(%s, %s, %s, %s)' % tuple(format.get('foregroundcolor', (0, 0, 0, 255)))), f = format.get('face', 'Times'), h = str(int(format.get('size', '16')) * 96 / 72), s = str(format.get('bold', False) | format.get('italic', False) << 1 | format.get('underline', False) << 2)))
  305.     log.info('message format: %r', format)
  306.     return etree.tostring(doc)
  307.  
  308.