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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from pyxmpp import sasl
  5. from pyxmpp.exceptions import SASLNotAvailable, SASLMechanismNotAvailable, SASLAuthenticationFailed, FatalStreamError
  6. from jabber.threadstream import ThreadStream
  7. from pyxmpp.streamsasl import SASL_NS
  8. from pyxmpp.streambase import STREAM_NS
  9. from pyxmpp.jid import JID
  10. import libxml2
  11. from logging import getLogger
  12. log = getLogger('gtalkStream')
  13. GTALK_AUTH_NS = 'http://www.google.com/talk/protocol/auth'
  14.  
  15. class GoogleTalkStream(ThreadStream):
  16.     
  17.     def stream_start(self, doc):
  18.         self.doc_in = doc
  19.         log.debug('input document: %r' % (self.doc_in.serialize(),))
  20.         
  21.         try:
  22.             r = self.doc_in.getRootElement()
  23.             if r.ns().getContent() != STREAM_NS:
  24.                 self._send_stream_error('invalid-namespace')
  25.                 raise FatalStreamError, 'Invalid namespace.'
  26.             r.ns().getContent() != STREAM_NS
  27.         except libxml2.treeError:
  28.             self._send_stream_error('invalid-namespace')
  29.             raise FatalStreamError, "Couldn't get the namespace."
  30.  
  31.         self.version = r.prop('version')
  32.         if self.version and self.version != '1.0':
  33.             self._send_stream_error('unsupported-version')
  34.             raise FatalStreamError, 'Unsupported protocol version.'
  35.         self.version != '1.0'
  36.         if self.initiator:
  37.             self.stream_id = r.prop('id')
  38.             peer = r.prop('from')
  39.             if peer:
  40.                 peer = JID(peer)
  41.                 self.peer = peer
  42.             
  43.         else:
  44.             to = r.prop('to')
  45.             if to:
  46.                 to = self.check_to(to)
  47.                 if not to:
  48.                     self._send_stream_error('host-unknown')
  49.                     raise FatalStreamError, 'Bad "to"'
  50.                 to
  51.                 self.me = JID(to)
  52.             
  53.             self._send_stream_start(self.generate_id())
  54.             self._send_stream_features()
  55.             self.state_change('fully connected', self.peer)
  56.             self._post_connect()
  57.         if not self.version:
  58.             self.state_change('fully connected', self.peer)
  59.             self._post_connect()
  60.         
  61.  
  62.     
  63.     def _sasl_authenticate(self, username, authzid, mechanism = None):
  64.         if not self.initiator:
  65.             raise SASLAuthenticationFailed, 'Only initiating entity start SASL authentication'
  66.         self.initiator
  67.         while not self.features:
  68.             self._GoogleTalkStream__logger.debug('Waiting for features')
  69.             self._read()
  70.         if not self.peer_sasl_mechanisms:
  71.             raise SASLNotAvailable, "Peer doesn't support SASL"
  72.         self.peer_sasl_mechanisms
  73.         if not mechanism:
  74.             mechanism = None
  75.             for m in self.sasl_mechanisms:
  76.                 if m in self.peer_sasl_mechanisms:
  77.                     mechanism = m
  78.                     break
  79.                     continue
  80.             
  81.             if not mechanism:
  82.                 raise SASLMechanismNotAvailable, "Peer doesn't support any of our SASL mechanisms"
  83.             mechanism
  84.             self._GoogleTalkStream__logger.debug('Our mechanism: %r' % (mechanism,))
  85.         elif mechanism not in self.peer_sasl_mechanisms:
  86.             raise SASLMechanismNotAvailable, '%s is not available' % (mechanism,)
  87.         
  88.         self.auth_method_used = 'sasl:' + mechanism
  89.         self.authenticator = sasl.client_authenticator_factory(mechanism, self)
  90.         initial_response = self.authenticator.start(username, authzid)
  91.         if not isinstance(initial_response, sasl.Response):
  92.             raise SASLAuthenticationFailed, 'SASL initiation failed'
  93.         isinstance(initial_response, sasl.Response)
  94.         root = self.doc_out.getRootElement()
  95.         xmlnode = root.newChild(None, 'auth', None)
  96.         ns = xmlnode.newNs(SASL_NS, None)
  97.         xmlnode.setNs(ns)
  98.         xmlnode.setProp('mechanism', mechanism)
  99.         ga = xmlnode.newNs(GTALK_AUTH_NS, 'ga')
  100.         xmlnode.newNsProp(ga, 'client-uses-full-bind-result', 'true')
  101.         if initial_response.data:
  102.             xmlnode.setContent(initial_response.base64())
  103.         
  104.         self._write_raw(xmlnode.serialize(encoding = 'UTF-8'))
  105.         self.freeOutNode(xmlnode)
  106.  
  107.  
  108.