'Authenticate connnection and bind resource. If resource is not provided
random one or library name used.'

def auth(self, user, password, resource='', sasl=1):

    (self._User, self._Password, self._Resource) = (user, password, resource)
    while ((not self.Dispatcher.Stream._document_attrs) and self.Process(1)):
        pass
    if (self.Dispatcher.Stream._document_attrs.has_key('version') and (self.Dispatcher.Stream._document_attrs['version'] == '1.0')):
        while ((not self.Dispatcher.Stream.features) and self.Process(1)):
            pass
    if sasl:
        auth.SASL(user, password).PlugIn(self)
    if ((not sasl) or (self.SASL.startsasl == 'not-supported')):
        if (not resource):
            resource = 'xmpppy'
        if auth.NonSASL(user, password, resource).PlugIn(self):
            self.connected += '+old_auth'
            return 'old_auth'
        return
    self.SASL.auth()
    while ((self.SASL.startsasl == 'in-process') and self.Process(1)):
        pass
    if (self.SASL.startsasl == 'success'):
        auth.Bind().PlugIn(self)
        while ((self.Bind.bound is None) and self.Process(1)):
            pass
        if self.Bind.Bind(resource):
            self.connected += '+sasl'
            return 'sasl'
    elif self.__dict__.has_key('SASL'):
        self.SASL.PlugOut()
