Package pyxmpp :: Module stream |
[hide private]

# Source Code for Module pyxmpp.stream

      1  #
      2  # (C) Copyright 2003-2010 Jacek Konieczny <jajcus@jajcus.net>
      3  #
      4  # This program is free software; you can redistribute it and/or modify
      5  # it under the terms of the GNU Lesser General Public License Version
      6  # 2.1 as published by the Free Software Foundation.
      7  #
      8  # This program is distributed in the hope that it will be useful,
      9  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  # GNU Lesser General Public License for more details.
     12  #
     13  # You should have received a copy of the GNU Lesser General Public
     14  # License along with this program; if not, write to the Free Software
     15  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     16  #
     17
     18  """Generic XMPP stream implementation.
     19
     20  Normative reference:
     21    - `RFC 3920 <http://www.ietf.org/rfc/rfc3920.txt>`__
     22  """
     23
     24  __docformat__="restructuredtext en"
     25
     26  import logging
     27
     28  from pyxmpp.streambase import StreamBase
     29  from pyxmpp.streamtls import StreamTLSMixIn
     30  from pyxmpp.streamsasl import StreamSASLMixIn
     31
     32 -class Stream(StreamTLSMixIn,StreamSASLMixIn,StreamBase):
     33      """Generic XMPP stream class.
     34
     35      Responsible for establishing connection, parsing the stream,
     36      StartTLS encryption and SASL authentication negotiation
     37      and usage, dispatching received stanzas to apopriate handlers
     38      and sending application's stanzas.
     39
     40      Whenever we say "stream" here we actually mean two streams
     41      (incoming and outgoing) of one connections, as defined by the XMPP
     42      specification.
     43
     44      :Ivariables:
     45          - `lock`: RLock object used to synchronize access to Stream object.
     46          - `features`: stream features as annouced by the initiator.
     47          - `me`: local stream endpoint JID.
     48          - `peer`: remote stream endpoint JID.
     49          - `process_all_stanzas`: when `True` then all stanzas received are
     50            considered local.
     51          - `tls`: TLS connection object.
     52          - `initiator`: `True` if local stream endpoint is the initiating entity.
     53          - `_reader`: the stream reader object (push parser) for the stream.
     54      """
     55 -    def __init__(self, default_ns, extra_ns = (), sasl_mechanisms = (),
     56                      tls_settings = None, keepalive = 0, owner = None):
     57          """Initialize Stream object
     58
     59          :Parameters:
     60            - `default_ns`: stream's default namespace ("jabber:client" for
     61              client, "jabber:server" for server, etc.)
     62            - `extra_ns`: sequence of extra namespace URIs to be defined for
     63              the stream.
     64            - `sasl_mechanisms`: sequence of SASL mechanisms allowed for
     65              authentication. Currently "PLAIN", "DIGEST-MD5" and "GSSAPI" are supported.
     66            - `tls_settings`: settings for StartTLS -- `TLSSettings` instance.
     67            - `keepalive`: keepalive output interval. 0 to disable.
     68            - `owner`: `Client`, `Component` or similar object "owning" this stream.
     69          """
     70          StreamBase.__init__(self, default_ns, extra_ns, keepalive, owner)
     71          StreamTLSMixIn.__init__(self, tls_settings)
     72          StreamSASLMixIn.__init__(self, sasl_mechanisms)
     73          self.__logger = logging.getLogger("pyxmpp.Stream")
     74
     75 -    def _reset(self):
     76          """Reset `Stream` object state making it ready to handle new
     77          connections."""
     78          StreamBase._reset(self)
     79          self._reset_tls()
     80          self._reset_sasl()
     81
     82 -    def _make_stream_features(self):
     83          """Create the <features/> element for the stream.
     84
     85          [receving entity only]
     86
     87          :returns: new <features/> element node."""
     88          features=StreamBase._make_stream_features(self)
     89          self._make_stream_tls_features(features)
     90          self._make_stream_sasl_features(features)
     91          return features
     92
     93 -    def _process_node(self,xmlnode):
     94          """Process first level element of the stream.
     95
     96          The element may be stream error or features, StartTLS
     97          request/response, SASL request/response or a stanza.
     98
     99          :Parameters:
    100              - `xmlnode`: XML node describing the element
    101          """
    102          if self._process_node_tls(xmlnode):
    103              return
    104          if self._process_node_sasl(xmlnode):
    105              return
    106          StreamBase._process_node(self,xmlnode)
    107
    108 -    def _got_features(self):
    109          """Process incoming <stream:features/> element.
    110
    111          [initiating entity only]
    112
    113          The received features node is available in `self.features`."""
    114          self._handle_tls_features()
    115          self._handle_sasl_features()
    116          StreamBase._got_features(self)
    117          if not self.tls_requested and not self.authenticated:
    118              self.state_change("fully connected",self.peer)
    119              self._post_connect()
    120
    121  # vi: sts=4 et sw=4
    122