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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import sys
  5. import logging
  6. log = logging.getLogger('msn.slp.message')
  7.  
  8. class ParseError(Exception):
  9.     pass
  10.  
  11.  
  12. class Content(object):
  13.     SESSION_REQ = 'application/x-msnmsgr-sessionreqbody'
  14.     SESSION_CLOSE = 'application/x-msnmsgr-sessionclosebody'
  15.     TRANS_REQ = 'application/x-msnmsgr-transreqbody'
  16.     TRANS_RESP = 'application/x-msnmsgr-transrespbody'
  17.     TRANS_ADDRUP = 'application/x-msnmsgr-transdestaddrupdate'
  18.     TRANS_UDP = 'application/x-msnmsgr-transudpswitch'
  19.  
  20.  
  21. class MIMEMessage(object):
  22.     
  23.     def __init__(self, headers = { }, body = '', **moreheaders):
  24.         moreheaders.update(headers)
  25.         self.headers = moreheaders
  26.         self.body = body
  27.  
  28.     
  29.     def __getitem__(self, key):
  30.         return self.headers.__getitem__(key)
  31.  
  32.     
  33.     def __setitem__(self, key, val):
  34.         return self.headers.__setitem__(key, str(val))
  35.  
  36.     
  37.     def __str__(self):
  38.         res = []
  39.         headers = getattr(self, 'HEADERS', self.headers)
  40.         for key in headers:
  41.             if key in self.headers:
  42.                 res.append('%s: %s' % (key, self.headers[key]))
  43.                 continue
  44.         
  45.         for key in self.headers:
  46.             if key not in headers:
  47.                 res.append('%s: %s' % (key, self.headers[key]))
  48.                 continue
  49.         
  50.         res.append('')
  51.         res.append(self.body)
  52.         return '\r\n'.join(res)
  53.  
  54.     
  55.     def parse(cls, raw):
  56.         headers = { }
  57.         body = ''
  58.         lines = raw.split('\r\n')
  59.         line = lines.pop(0)
  60.         while line:
  61.             (key, val) = line.split(':', 1)
  62.             key = key.strip()
  63.             val = val.strip()
  64.             headers[key] = val
  65.             line = lines.pop(0)
  66.         if lines:
  67.             body = '\r\n'.join(lines)
  68.         
  69.         return cls(headers, body)
  70.  
  71.     parse = classmethod(parse)
  72.  
  73.  
  74. class SLPMessage(MIMEMessage):
  75.     
  76.     class Type(object):
  77.         UNKNOWN = 'unknown'
  78.         REQUEST = 'req'
  79.         RESPONSE = 'resp'
  80.  
  81.     HEADERS = [
  82.         'To',
  83.         'From',
  84.         'Via',
  85.         'CSeq',
  86.         'Call-ID',
  87.         'Max-Forwards']
  88.     STATUS_MESSAGE = {
  89.         200: '200 OK',
  90.         404: '404 Not Found',
  91.         500: '500 Internal Error',
  92.         603: '603 Decline' }
  93.     
  94.     def __init__(self, *a, **k):
  95.         self.method = ''
  96.         self.status = -1
  97.         MIMEMessage.__init__(self, *a, **k)
  98.  
  99.     
  100.     def message_type(self):
  101.         if self.method:
  102.             return self.Type.REQUEST
  103.         if self.status != -1:
  104.             return self.Type.RESPONSE
  105.         return self.Type.UNKNOWN
  106.  
  107.     message_type = property(message_type)
  108.     
  109.     def clear(self):
  110.         MIMEMessage.clear(self)
  111.         self.method = ''
  112.         self.status = -1
  113.  
  114.     
  115.     def request(cls, method, to = '', frm = '', branch = '', command_sequence = 0, call_id = '', max_forwards = 0):
  116.         self = cls()
  117.         self.method = method
  118.         self._SLPMessage__build(to, frm, branch, command_sequence, call_id, max_forwards)
  119.         return self
  120.  
  121.     request = classmethod(request)
  122.     
  123.     def response(cls, status, to = '', frm = '', branch = '', command_sequence = 0, call_id = '', max_forwards = 0):
  124.         self = cls()
  125.         self.status = status
  126.         int(self.status)
  127.         self._SLPMessage__build(to, frm, branch, command_sequence, call_id, max_forwards)
  128.         return self
  129.  
  130.     response = classmethod(response)
  131.     
  132.     def response_to(cls, status, msg):
  133.         self = cls()
  134.         self.status = status
  135.         int(self.status)
  136.         headers = []
  137.         for name in msg.headers:
  138.             if name in self.HEADERS:
  139.                 value = msg.headers[name][:]
  140.                 if name == 'CSeq':
  141.                     value = str(int(value) + 1)
  142.                 
  143.                 headers.append((name, value))
  144.                 continue
  145.         
  146.         self.headers.update(headers)
  147.         self.headers['To'] = msg.headers['From'][:]
  148.         self.headers['From'] = msg.headers['To'][:]
  149.         return self
  150.  
  151.     response_to = classmethod(response_to)
  152.     
  153.     def add_body(self, body = None):
  154.         if body != None:
  155.             content_type = body.content_type
  156.             body_str = str(body)
  157.             body_len = len(body_str)
  158.         else:
  159.             content_type = 'null'
  160.             body_str = ''
  161.             body_len = 0
  162.         self['Content-Type'] = content_type
  163.         self['Content-Length'] = body_len
  164.         self.body = body_str
  165.  
  166.     
  167.     def parse(cls, raw):
  168.         if raw.find('MSNSLP/1.0') < 0:
  169.             raise ParseError("message doesn't seem to be an MSNSLP/1.0 message", raw)
  170.         raw.find('MSNSLP/1.0') < 0
  171.         (start_line, content) = raw.split('\r\n', 1)
  172.         if start_line.startswith('\x00'):
  173.             log.error("Got null bytes in SLP message. Here's raw data: %r", raw)
  174.             print >>sys.stderr, 'MSN null bytes!', repr(raw)
  175.             raise ParseError('message had all those stupid null bytes', raw)
  176.         start_line.startswith('\x00')
  177.         start_line = start_line.split(' ')
  178.         if not start_line:
  179.             return None
  180.         
  181.         try:
  182.             if start_line[0] in ('INVITE', 'BYE', 'ACK', '\x00\x00\x00'):
  183.                 method = start_line[0].strip()
  184.                 status = -1
  185.             else:
  186.                 method = ''
  187.                 status = int(start_line[1])
  188.             self = MIMEMessage.parse(content)
  189.             self.__class__ = cls
  190.             self.method = method
  191.             self.status = status
  192.             log.debug('SLPMessage received: method=%r, status=%r, headers=%r', self.method, self.status, dict(self.headers))
  193.             slpbody = SLPBody.parse(self.body, self['Content-Type'])
  194.             self.add_body(slpbody)
  195.             return self
  196.         except Exception:
  197.             start_line
  198.             _e = start_line
  199.             import traceback
  200.             traceback.print_exc()
  201.             log.info('Bad data in SLPMessage.parse: %r', raw)
  202.             return None
  203.  
  204.  
  205.     parse = classmethod(parse)
  206.     
  207.     def __str__(self):
  208.         result = []
  209.         if self.method:
  210.             result.append('%s MSNMSGR:%s MSNSLP/1.0' % (self.method, self.headers['To'][9:-1]))
  211.         else:
  212.             result.append('MSNSLP/1.0 %s' % self.STATUS_MESSAGE[self.status])
  213.         result.append(MIMEMessage.__str__(self))
  214.         return '\r\n'.join(result)
  215.  
  216.     
  217.     def __build(self, to, frm, branch, command_sequence, call_id, max_forwards):
  218.         if to:
  219.             self['To'] = '<msnmsgr:%s>' % to
  220.         
  221.         if frm:
  222.             self['From'] = '<msnmsgr:%s>' % frm
  223.         
  224.         if branch:
  225.             self['Via'] = 'MSNSLP/1.0/TLP ;branch=%s' % branch
  226.         
  227.         self['CSeq'] = str(command_sequence) + ' '
  228.         if call_id:
  229.             self['Call-ID'] = call_id
  230.         
  231.         self['Max-Forwards'] = max_forwards
  232.  
  233.  
  234.  
  235. class SLPBody(MIMEMessage):
  236.     
  237.     def __init__(self, content_type, *a, **k):
  238.         MIMEMessage.__init__(self, *a, **k)
  239.         self.content_type = content_type
  240.  
  241.     
  242.     def parse(cls, raw, content_type):
  243.         raw = raw[:-1]
  244.         self = MIMEMessage.parse(raw)
  245.         self.__class__ = cls
  246.         self.content_type = content_type
  247.         log.debug('SLPBody received: content_type=%r, headers=%r', self.content_type, dict(self.headers))
  248.         return self
  249.  
  250.     parse = classmethod(parse)
  251.     
  252.     def __str__(self):
  253.         return MIMEMessage.__str__(self) + '\x00'
  254.  
  255.  
  256.  
  257. class SLPSessionInviteBody(SLPBody):
  258.     
  259.     def __init__(self, *a, **k):
  260.         SLPBody.__init__(self, Content.SESSION_REQ, *a, **k)
  261.  
  262.     
  263.     def request(cls, eufguid, sessionid, chan_state, appid, context, body = ''):
  264.         headers = {
  265.             'EUF-GUID': eufguid,
  266.             'SessionID': sessionid,
  267.             'AppID': appid,
  268.             'Context': context }
  269.         return cls(headers, body)
  270.  
  271.     request = classmethod(request)
  272.     
  273.     def response(cls, sessionid, chan_state, body = ''):
  274.         headers = {
  275.             'SessionID': sessionid }
  276.         return cls(headers, body)
  277.  
  278.     response = classmethod(response)
  279.     
  280.     def response_to(cls, msg):
  281.         return cls.response(msg.headers['SessionID'], msg.headers['SChannelState'], '')
  282.  
  283.     response_to = classmethod(response_to)
  284.  
  285.  
  286. class SLPTransferInviteBody(SLPBody):
  287.     
  288.     def __init__(self, bridges, netid, conntype, upnp, icf, hashednonce, sessionid, chanstate, body):
  289.         headers = { }
  290.         headers['Bridges'] = bridges
  291.         headers['NetID'] = netid
  292.         headers['Conn-Type'] = conntype
  293.         headers['UPnPNat'] = upnp
  294.         headers['ICF'] = icf
  295.         headers['Hashed-Nonce'] = hashednonce
  296.         headers['SessionID'] = sessionid
  297.         SLPBody.__init__(self, Content.TRANS_REQ, headers, body)
  298.  
  299.     
  300.     def request(cls, bridges, netid, conntype, upnp, icf, hashednonce, sessionid, chanstate, body):
  301.         return cls(bridges, netid, conntype, upnp, icf, hashednonce, sessionid, chanstate, body)
  302.  
  303.     request = classmethod(request)
  304.     
  305.     def response(cls, *a, **k):
  306.         raise TypeError('Invite only available as request')
  307.  
  308.     response = classmethod(response)
  309.  
  310.  
  311. class SLP_Bye(SLPBody):
  312.     
  313.     def __init__(self):
  314.         SLPBody.__init__(self, Content.SESSION_CLOSE)
  315.  
  316.  
  317.  
  318. class SLP_404(SLPMessage):
  319.     
  320.     def response_to(cls, bad_msg):
  321.         return SLPMessage.response_to(404, bad_msg)
  322.  
  323.     response_to = classmethod(response_to)
  324.  
  325.  
  326. class SLP_500(SLPMessage):
  327.     
  328.     def response_to(cls, bad_msg):
  329.         return SLPMessage.response_to(500, bad_msg)
  330.  
  331.     response_to = classmethod(response_to)
  332.  
  333.  
  334. def main():
  335.     data = 'INVITE MSNMSGR:bob@hotmail.com MSNSLP/1.0\r\nTo: <msnmsgr:bob@hotmail.com>\r\nFrom: <msnmsgr:alice@hotmail.com>\r\nVia: MSNSLP/1.0/TLP ;branch={33517CE4-02FC-4428-B6F4-39927229B722}\r\nCSeq: 0 \r\nCall-ID: {9D79AE57-1BD5-444B-B14E-3FC9BB2B5D58}\r\nMax-Forwards: 0\r\nContent-Type: application/x-msnmsgr-sessionreqbody\r\nContent-Length: 326\r\n\r\nEUF-GUID: {A4268EEC-FEC5-49E5-95C3-F126696BDBF6}\r\nSessionID: 1980589\r\nAppID: 1\r\nContext: PG1zbm9iaiBDcmVhdG9yPSJidWRkeTFAaG90bWFpbC5jb20iIFNpemU9IjI0NTM5IiBUeXBlPSIzIiBMb2NhdGlvbj0iVEZSMkMudG1wIiBGcmllbmRseT0iQUFBPSIgU0hBMUQ9InRyQzhTbEZ4MnNXUXhaTUlCQVdTRW5YYzhvUT0iIFNIQTFDPSJVMzJvNmJvc1p6bHVKcTgyZUF0TXB4NWRJRUk9Ii8+DQoA\r\n\r\n\x00'
  336.  
  337. if __name__ == '__main__':
  338.     main()
  339.  
  340.