<s> """<STR_LIT>""" <EOL> import warnings <EOL> from twisted . internet import defer <EOL> from twisted . words . protocols . jabber import error <EOL> from twisted . words . protocols . jabber . jid import JID <EOL> from twisted . words . xish import domish <EOL> from wokkel . generic import ErrorStanza , Stanza , Request <EOL> from wokkel . subprotocols import IQHandlerMixin <EOL> from wokkel . subprotocols import XMPPHandler <EOL> NS_XML = '<STR_LIT>' <EOL> NS_ROSTER = '<STR_LIT>' <EOL> XPATH_ROSTER_SET = "<STR_LIT>" % NS_ROSTER <EOL> class Presence ( domish . Element ) : <EOL> def __init__ ( self , to = None , type = None ) : <EOL> domish . Element . __init__ ( self , ( None , "<STR_LIT>" ) ) <EOL> if type : <EOL> self [ "<STR_LIT:type>" ] = type <EOL> if to is not None : <EOL> self [ "<STR_LIT:to>" ] = to . full ( ) <EOL> class AvailablePresence ( Presence ) : <EOL> def __init__ ( self , to = None , show = None , statuses = None , priority = <NUM_LIT:0> ) : <EOL> Presence . __init__ ( self , to , type = None ) <EOL> if show in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . addElement ( '<STR_LIT>' , content = show ) <EOL> if statuses is not None : <EOL> for lang , status in statuses . iteritems ( ) : <EOL> s = self . addElement ( '<STR_LIT:status>' , content = status ) <EOL> if lang : <EOL> s [ ( NS_XML , "<STR_LIT>" ) ] = lang <EOL> if priority != <NUM_LIT:0> : <EOL> self . addElement ( '<STR_LIT>' , content = unicode ( int ( priority ) ) ) <EOL> class UnavailablePresence ( Presence ) : <EOL> def __init__ ( self , to = None , statuses = None ) : <EOL> Presence . __init__ ( self , to , type = '<STR_LIT>' ) <EOL> if statuses is not None : <EOL> for lang , status in statuses . iteritems ( ) : <EOL> s = self . addElement ( '<STR_LIT:status>' , content = status ) <EOL> if lang : <EOL> s [ ( NS_XML , "<STR_LIT>" ) ] = lang <EOL> class PresenceClientProtocol ( XMPPHandler ) : <EOL> def connectionInitialized ( self ) : <EOL> self . xmlstream . addObserver ( '<STR_LIT>' , self . _onPresence ) <EOL> def _getStatuses ( self , presence ) : <EOL> statuses = { } <EOL> for element in presence . elements ( ) : <EOL> if element . name == '<STR_LIT:status>' : <EOL> lang = element . getAttribute ( ( NS_XML , '<STR_LIT>' ) ) <EOL> text = unicode ( element ) <EOL> statuses [ lang ] = text <EOL> return statuses <EOL> def _onPresence ( self , presence ) : <EOL> type = presence . getAttribute ( "<STR_LIT:type>" , "<STR_LIT>" ) <EOL> try : <EOL> handler = getattr ( self , '<STR_LIT>' % ( type . capitalize ( ) ) ) <EOL> except AttributeError : <EOL> return <EOL> else : <EOL> handler ( presence ) <EOL> def _onPresenceAvailable ( self , presence ) : <EOL> entity = JID ( presence [ "<STR_LIT>" ] ) <EOL> show = unicode ( presence . show or '<STR_LIT>' ) <EOL> if show not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> show = None <EOL> statuses = self . _getStatuses ( presence ) <EOL> try : <EOL> priority = int ( unicode ( presence . priority or '<STR_LIT>' ) ) or <NUM_LIT:0> <EOL> except ValueError : <EOL> priority = <NUM_LIT:0> <EOL> self . availableReceived ( entity , show , statuses , priority ) <EOL> def _onPresenceUnavailable ( self , presence ) : <EOL> entity = JID ( presence [ "<STR_LIT>" ] ) <EOL> statuses = self . _getStatuses ( presence ) <EOL> self . unavailableReceived ( entity , statuses ) <EOL> def _onPresenceSubscribed ( self , presence ) : <EOL> self . subscribedReceived ( JID ( presence [ "<STR_LIT>" ] ) ) <EOL> def _onPresenceUnsubscribed ( self , presence ) : <EOL> self . unsubscribedReceived ( JID ( presence [ "<STR_LIT>" ] ) ) <EOL> def _onPresenceSubscribe ( self , presence ) : <EOL> self . subscribeReceived ( JID ( presence [ "<STR_LIT>" ] ) ) <EOL> def _onPresenceUnsubscribe ( self , presence ) : <EOL> self . unsubscribeReceived ( JID ( presence [ "<STR_LIT>" ] ) ) <EOL> def availableReceived ( self , entity , show = None , statuses = None , priority = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> def unavailableReceived ( self , entity , statuses = None ) : <EOL> """<STR_LIT>""" <EOL> def subscribedReceived ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> def unsubscribedReceived ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> def subscribeReceived ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> def unsubscribeReceived ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> def available ( self , entity = None , show = None , statuses = None , priority = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . send ( AvailablePresence ( entity , show , statuses , priority ) ) <EOL> def unavailable ( self , entity = None , statuses = None ) : <EOL> """<STR_LIT>""" <EOL> self . send ( UnavailablePresence ( entity , statuses ) ) <EOL> def subscribe ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> self . send ( Presence ( to = entity , type = '<STR_LIT>' ) ) <EOL> def unsubscribe ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> self . send ( Presence ( to = entity , type = '<STR_LIT>' ) ) <EOL> def subscribed ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> self . send ( Presence ( to = entity , type = '<STR_LIT>' ) ) <EOL> def unsubscribed ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> self . send ( Presence ( to = entity , type = '<STR_LIT>' ) ) <EOL> class BasePresence ( Stanza ) : <EOL> """<STR_LIT>""" <EOL> stanzaKind = '<STR_LIT>' <EOL> class AvailabilityPresence ( BasePresence ) : <EOL> """<STR_LIT>""" <EOL> childParsers = { ( None , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( None , '<STR_LIT:status>' ) : '<STR_LIT>' , <EOL> ( None , '<STR_LIT>' ) : '<STR_LIT>' } <EOL> def __init__ ( self , recipient = None , sender = None , available = True , <EOL> show = None , status = None , statuses = None , priority = <NUM_LIT:0> ) : <EOL> BasePresence . __init__ ( self , recipient = recipient , sender = sender ) <EOL> self . available = available <EOL> self . show = show <EOL> self . statuses = statuses or { } <EOL> if status : <EOL> self . statuses [ None ] = status <EOL> self . priority = priority <EOL> def __get_status ( self ) : <EOL> if None in self . statuses : <EOL> return self . statuses [ None ] <EOL> elif self . statuses : <EOL> for status in self . status . itervalues ( ) : <EOL> return status <EOL> else : <EOL> return None <EOL> status = property ( __get_status ) <EOL> def _childParser_show ( self , element ) : <EOL> show = unicode ( element ) <EOL> if show in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . show = show <EOL> def _childParser_status ( self , element ) : <EOL> lang = element . getAttribute ( ( NS_XML , '<STR_LIT>' ) , None ) <EOL> text = unicode ( element ) <EOL> self . statuses [ lang ] = text <EOL> def _childParser_priority ( self , element ) : <EOL> try : <EOL> self . priority = int ( unicode ( element ) ) <EOL> except ValueError : <EOL> pass <EOL> def parseElement ( self , element ) : <EOL> BasePresence . parseElement ( self , element ) <EOL> if self . stanzaType == '<STR_LIT>' : <EOL> self . available = False <EOL> def toElement ( self ) : <EOL> if not self . available : <EOL> self . stanzaType = '<STR_LIT>' <EOL> presence = BasePresence . toElement ( self ) <EOL> if self . available : <EOL> if self . show in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> presence . addElement ( '<STR_LIT>' , content = self . show ) <EOL> if self . priority != <NUM_LIT:0> : <EOL> presence . addElement ( '<STR_LIT>' , content = unicode ( self . priority ) ) <EOL> for lang , text in self . statuses . iteritems ( ) : <EOL> status = presence . addElement ( '<STR_LIT:status>' , content = text ) <EOL> if lang : <EOL> status [ ( NS_XML , '<STR_LIT>' ) ] = lang <EOL> return presence <EOL> class SubscriptionPresence ( BasePresence ) : <EOL> """<STR_LIT>""" <EOL> class ProbePresence ( BasePresence ) : <EOL> """<STR_LIT>""" <EOL> stanzaType = '<STR_LIT>' <EOL> class BasePresenceProtocol ( XMPPHandler ) : <EOL> """<STR_LIT>""" <EOL> presenceTypeParserMap = { } <EOL> def connectionInitialized ( self ) : <EOL> self . xmlstream . addObserver ( "<STR_LIT>" , self . _onPresence ) <EOL> def _onPresence ( self , element ) : <EOL> """<STR_LIT>""" <EOL> stanza = Stanza . fromElement ( element ) <EOL> presenceType = stanza . stanzaType or '<STR_LIT>' <EOL> try : <EOL> parser = self . presenceTypeParserMap [ presenceType ] <EOL> except KeyError : <EOL> return <EOL> presence = parser . fromElement ( element ) <EOL> try : <EOL> handler = getattr ( self , '<STR_LIT>' % presenceType ) <EOL> except AttributeError : <EOL> return <EOL> else : <EOL> handler ( presence ) <EOL> class PresenceProtocol ( BasePresenceProtocol ) : <EOL> presenceTypeParserMap = { <EOL> '<STR_LIT:error>' : ErrorStanza , <EOL> '<STR_LIT>' : AvailabilityPresence , <EOL> '<STR_LIT>' : AvailabilityPresence , <EOL> '<STR_LIT>' : SubscriptionPresence , <EOL> '<STR_LIT>' : SubscriptionPresence , <EOL> '<STR_LIT>' : SubscriptionPresence , <EOL> '<STR_LIT>' : SubscriptionPresence , <EOL> '<STR_LIT>' : ProbePresence , <EOL> } <EOL> def errorReceived ( self , presence ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def availableReceived ( self , presence ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def unavailableReceived ( self , presence ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def subscribedReceived ( self , presence ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def unsubscribedReceived ( self , presence ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def subscribeReceived ( self , presence ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def unsubscribeReceived ( self , presence ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def probeReceived ( self , presence ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def available ( self , recipient = None , show = None , statuses = None , priority = <NUM_LIT:0> , <EOL> status = None , sender = None ) : <EOL> """<STR_LIT>""" <EOL> presence = AvailabilityPresence ( recipient = recipient , sender = sender , <EOL> show = show , statuses = statuses , <EOL> status = status , priority = priority ) <EOL> self . send ( presence . toElement ( ) ) <EOL> def unavailable ( self , recipient = None , statuses = None , sender = None ) : <EOL> """<STR_LIT>""" <EOL> presence = AvailabilityPresence ( recipient = recipient , sender = sender , <EOL> available = False , statuses = statuses ) <EOL> self . send ( presence . toElement ( ) ) <EOL> def subscribe ( self , recipient , sender = None ) : <EOL> """<STR_LIT>""" <EOL> presence = SubscriptionPresence ( recipient = recipient , sender = sender ) <EOL> presence . stanzaType = '<STR_LIT>' <EOL> self . send ( presence . toElement ( ) ) <EOL> def unsubscribe ( self , recipient , sender = None ) : <EOL> """<STR_LIT>""" <EOL> presence = SubscriptionPresence ( recipient = recipient , sender = sender ) <EOL> presence . stanzaType = '<STR_LIT>' <EOL> self . send ( presence . toElement ( ) ) <EOL> def subscribed ( self , recipient , sender = None ) : <EOL> """<STR_LIT>""" <EOL> presence = SubscriptionPresence ( recipient = recipient , sender = sender ) <EOL> presence . stanzaType = '<STR_LIT>' <EOL> self . send ( presence . toElement ( ) ) <EOL> def unsubscribed ( self , recipient , sender = None ) : <EOL> """<STR_LIT>""" <EOL> presence = SubscriptionPresence ( recipient = recipient , sender = sender ) <EOL> presence . stanzaType = '<STR_LIT>' <EOL> self . send ( presence . toElement ( ) ) <EOL> def probe ( self , recipient , sender = None ) : <EOL> """<STR_LIT>""" <EOL> presence = ProbePresence ( recipient = recipient , sender = sender ) <EOL> self . send ( presence . toElement ( ) ) <EOL> class RosterItem ( object ) : <EOL> """<STR_LIT>""" <EOL> __subscriptionStates = { ( False , False ) : None , <EOL> ( True , False ) : '<STR_LIT:to>' , <EOL> ( False , True ) : '<STR_LIT>' , <EOL> ( True , True ) : '<STR_LIT>' } <EOL> def __init__ ( self , entity , subscriptionTo = False , subscriptionFrom = False , <EOL> name = u'<STR_LIT>' , groups = None ) : <EOL> self . entity = entity <EOL> self . subscriptionTo = subscriptionTo <EOL> self . subscriptionFrom = subscriptionFrom <EOL> self . name = name <EOL> self . groups = groups or set ( ) <EOL> self . pendingOut = False <EOL> self . approved = False <EOL> self . remove = False <EOL> def __getJID ( self ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return self . entity <EOL> def __setJID ( self , value ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> self . entity = value <EOL> jid = property ( __getJID , __setJID , <EOL> doc = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __getAsk ( self ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return self . pendingOut <EOL> def __setAsk ( self , value ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> self . pendingOut = value <EOL> ask = property ( __getAsk , __setAsk , <EOL> doc = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def toElement ( self , rosterSet = False ) : <EOL> """<STR_LIT>""" <EOL> element = domish . Element ( ( NS_ROSTER , '<STR_LIT>' ) ) <EOL> element [ '<STR_LIT>' ] = self . entity . full ( ) <EOL> if self . remove : <EOL> subscription = '<STR_LIT>' <EOL> else : <EOL> if self . name : <EOL> element [ '<STR_LIT:name>' ] = self . name <EOL> if self . groups : <EOL> for group in self . groups : <EOL> element . addElement ( '<STR_LIT>' , content = group ) <EOL> if rosterSet : <EOL> subscription = None <EOL> else : <EOL> subscription = self . __subscriptionStates [ self . subscriptionTo , <EOL> self . subscriptionFrom ] <EOL> if self . pendingOut : <EOL> element [ '<STR_LIT>' ] = u'<STR_LIT>' <EOL> if self . approved : <EOL> element [ '<STR_LIT>' ] = u'<STR_LIT:true>' <EOL> if subscription : <EOL> element [ '<STR_LIT>' ] = subscription <EOL> return element <EOL> @ classmethod <EOL> def fromElement ( Class , element ) : <EOL> entity = JID ( element [ '<STR_LIT>' ] ) <EOL> item = Class ( entity ) <EOL> subscription = element . getAttribute ( '<STR_LIT>' ) <EOL> if subscription == '<STR_LIT>' : <EOL> item . remove = True <EOL> else : <EOL> item . name = element . getAttribute ( '<STR_LIT:name>' , u'<STR_LIT>' ) <EOL> item . subscriptionTo = subscription in ( '<STR_LIT:to>' , '<STR_LIT>' ) <EOL> item . subscriptionFrom = subscription in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> item . pendingOut = element . getAttribute ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> item . approved = element . getAttribute ( '<STR_LIT>' ) in ( '<STR_LIT:true>' , '<STR_LIT:1>' ) <EOL> for subElement in domish . generateElementsQNamed ( element . children , <EOL> '<STR_LIT>' , NS_ROSTER ) : <EOL> item . groups . add ( unicode ( subElement ) ) <EOL> return item <EOL> class RosterRequest ( Request ) : <EOL> """<STR_LIT>""" <EOL> item = None <EOL> version = None <EOL> rosterSet = False <EOL> def parseRequest ( self , element ) : <EOL> self . version = element . getAttribute ( '<STR_LIT>' ) <EOL> for child in element . elements ( NS_ROSTER , '<STR_LIT>' ) : <EOL> self . item = RosterItem . fromElement ( child ) <EOL> break <EOL> def toElement ( self ) : <EOL> element = Request . toElement ( self ) <EOL> query = element . addElement ( ( NS_ROSTER , '<STR_LIT>' ) ) <EOL> if self . version is not None : <EOL> query [ '<STR_LIT>' ] = self . version <EOL> if self . item : <EOL> query . addChild ( self . item . toElement ( rosterSet = self . rosterSet ) ) <EOL> return element <EOL> class RosterPushIgnored ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class Roster ( dict ) : <EOL> """<STR_LIT>""" <EOL> version = None <EOL> class RosterClientProtocol ( XMPPHandler , IQHandlerMixin ) : <EOL> """<STR_LIT>""" <EOL> allowAnySender = False <EOL> iqHandlers = { XPATH_ROSTER_SET : "<STR_LIT>" } <EOL> def connectionInitialized ( self ) : <EOL> self . xmlstream . addObserver ( XPATH_ROSTER_SET , self . handleRequest ) <EOL> def getRoster ( self , version = None ) : <EOL> """<STR_LIT>""" <EOL> def processRoster ( result ) : <EOL> if result . query is not None : <EOL> roster = Roster ( ) <EOL> roster . version = result . query . getAttribute ( '<STR_LIT>' ) <EOL> for element in result . query . elements ( NS_ROSTER , '<STR_LIT>' ) : <EOL> item = RosterItem . fromElement ( element ) <EOL> roster [ item . entity ] = item <EOL> return roster <EOL> else : <EOL> return None <EOL> request = RosterRequest ( stanzaType = '<STR_LIT>' ) <EOL> request . version = version <EOL> d = self . request ( request ) <EOL> d . addCallback ( processRoster ) <EOL> return d <EOL> def setItem ( self , item ) : <EOL> """<STR_LIT>""" <EOL> request = RosterRequest ( stanzaType = '<STR_LIT>' ) <EOL> request . rosterSet = True <EOL> request . item = item <EOL> return self . request ( request ) <EOL> def removeItem ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> item = RosterItem ( entity ) <EOL> item . remove = True <EOL> return self . setItem ( item ) <EOL> def _onRosterSet ( self , iq ) : <EOL> def trapIgnored ( failure ) : <EOL> failure . trap ( RosterPushIgnored ) <EOL> raise error . StanzaError ( '<STR_LIT>' ) <EOL> request = RosterRequest . fromElement ( iq ) <EOL> if ( not self . allowAnySender and <EOL> request . sender and <EOL> request . sender . userhostJID ( ) != <EOL> self . parent . jid . userhostJID ( ) ) : <EOL> d = defer . fail ( RosterPushIgnored ( ) ) <EOL> elif request . item . remove : <EOL> d = defer . maybeDeferred ( self . removeReceived , request ) <EOL> else : <EOL> d = defer . maybeDeferred ( self . setReceived , request ) <EOL> d . addErrback ( trapIgnored ) <EOL> return d <EOL> def setReceived ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return defer . maybeDeferred ( self . onRosterSet , request . item ) <EOL> def removeReceived ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning ) <EOL> return defer . maybeDeferred ( self . onRosterRemove , <EOL> request . item . entity ) <EOL> class Message ( Stanza ) : <EOL> """<STR_LIT>""" <EOL> stanzaKind = '<STR_LIT:message>' <EOL> childParsers = { <EOL> ( None , '<STR_LIT:body>' ) : '<STR_LIT>' , <EOL> ( None , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , recipient = None , sender = None , body = None , subject = None ) : <EOL> Stanza . __init__ ( self , recipient , sender ) <EOL> self . body = body <EOL> self . subject = subject <EOL> def _childParser_body ( self , element ) : <EOL> self . body = unicode ( element ) <EOL> def _childParser_subject ( self , element ) : <EOL> self . subject = unicode ( element ) <EOL> def toElement ( self ) : <EOL> element = Stanza . toElement ( self ) <EOL> if self . body : <EOL> element . addElement ( '<STR_LIT:body>' , content = self . body ) <EOL> if self . subject : <EOL> element . addElement ( '<STR_LIT>' , content = self . subject ) <EOL> return element <EOL> class MessageProtocol ( XMPPHandler ) : <EOL> """<STR_LIT>""" <EOL> messageTypes = None , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> def connectionInitialized ( self ) : <EOL> self . xmlstream . addObserver ( "<STR_LIT>" , self . _onMessage ) <EOL> def _onMessage ( self , message ) : <EOL> if message . handled : <EOL> return <EOL> messageType = message . getAttribute ( "<STR_LIT:type>" ) <EOL> if messageType == '<STR_LIT:error>' : <EOL> return <EOL> if messageType not in self . messageTypes : <EOL> message [ "<STR_LIT:type>" ] = '<STR_LIT>' <EOL> self . onMessage ( message ) <EOL> def onMessage ( self , message ) : <EOL> """<STR_LIT>""" </s>
