<s> import time <EOL> from twisted . python . failure import Failure <EOL> from twisted . internet . defer import Deferred <EOL> from twisted . words . xish . domish import escapeToXml <EOL> from droned . entity import Entity <EOL> from droned . context import EntityContext <EOL> from droned . errors import ServiceNotAvailable <EOL> import services <EOL> import config <EOL> from kitt . interfaces import IConversationContext , IDroneModelConversation , IDroneModelChatRoom , implements <EOL> class Conversation ( Entity ) : <EOL> """<STR_LIT>""" <EOL> implements ( IDroneModelConversation ) <EOL> jabber = property ( lambda s : services . getService ( '<STR_LIT>' ) ) <EOL> jabber_config = property ( lambda s : s . jabber . SERVICECONFIG ) <EOL> deferredAnswer = None <EOL> expectedAnswers = ( ) <EOL> askingQuestion = property ( lambda s : s . deferredAnswer is not None ) <EOL> groupChat = False <EOL> authorized = property ( lambda s : s . _authorized or s . buddy == s . jabber_config . DEPUTY ) <EOL> _authorized = False <EOL> idleTime = property ( lambda s : time . time ( ) - s . lastMessageReceived ) <EOL> responsive = property ( lambda s : s . idleTime < s . jabber_config . CONVERSATION_RESPONSE_PERIOD ) <EOL> online = True <EOL> buddy = property ( lambda s : s . _buddy ) <EOL> buddyName = property ( lambda s : s . _buddyName ) <EOL> serializable = True <EOL> def __init__ ( self , buddy ) : <EOL> self . _buddy = buddy <EOL> self . _buddyName = buddy . split ( '<STR_LIT:@>' ) [ <NUM_LIT:0> ] <EOL> self . notificationQueue = [ ] <EOL> self . lastMessageReceived = <NUM_LIT:0.0> <EOL> self . context = ConversationContext ( self ) <EOL> def __getstate__ ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . buddy , <EOL> '<STR_LIT>' : self . authorized , <EOL> '<STR_LIT>' : list ( self . context . get ( '<STR_LIT>' , [ ] ) ) <EOL> } <EOL> @ staticmethod <EOL> def construct ( state ) : <EOL> conversation = Conversation ( state [ '<STR_LIT>' ] ) <EOL> conversation . context [ '<STR_LIT>' ] = set ( state [ '<STR_LIT>' ] ) <EOL> if state . get ( '<STR_LIT>' ) : <EOL> conversation . grantAuthorization ( notify = False ) <EOL> for name in state [ '<STR_LIT>' ] : <EOL> Event ( name ) . subscribe ( conversation . notify ) <EOL> return conversation <EOL> @ staticmethod <EOL> def byName ( name ) : <EOL> """<STR_LIT>""" <EOL> for conversation in Conversation . objects : <EOL> if conversation . buddy == name : <EOL> return conversation <EOL> elif conversation . buddyName == name : <EOL> return conversation <EOL> def say ( self , message , ** options ) : <EOL> "<STR_LIT>" <EOL> options [ '<STR_LIT>' ] = self . groupChat <EOL> self . jabber . sendMessage ( self . buddy , message , ** options ) <EOL> def hear ( self , message ) : <EOL> "<STR_LIT>" <EOL> self . lastMessageReceived = time . time ( ) <EOL> for answer in self . expectedAnswers : <EOL> if message == answer : <EOL> d = self . deferredAnswer <EOL> del self . expectedAnswers <EOL> del self . deferredAnswer <EOL> d . callback ( answer ) <EOL> return <EOL> try : <EOL> dispatch ( self , message ) <EOL> except NoMatch : <EOL> self . say ( "<STR_LIT>" ) <EOL> except : <EOL> self . say ( Failure ( ) . getTraceback ( ) , useHTML = False ) <EOL> def ask ( self , question , answers ) : <EOL> """<STR_LIT>""" <EOL> if self . askingQuestion : <EOL> raise AlreadyAskingQuestion ( <EOL> self . context [ '<STR_LIT>' ] , <EOL> self . expectedAnswers <EOL> ) <EOL> self . say ( question ) <EOL> self . context [ '<STR_LIT>' ] = question <EOL> self . context [ '<STR_LIT>' ] = answers <EOL> self . deferredAnswer = Deferred ( ) <EOL> self . expectedAnswers = answers <EOL> return self . deferredAnswer <EOL> def nevermind ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . askingQuestion : <EOL> if '<STR_LIT>' in self . context : <EOL> del self . context [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in self . context : <EOL> del self . context [ '<STR_LIT>' ] <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> del self . deferredAnswer <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> del self . expectedAnswers <EOL> def grantAuthorization ( self , notify = True ) : <EOL> """<STR_LIT>""" <EOL> self . _authorized = True <EOL> self . context [ '<STR_LIT>' ] = True <EOL> if not notify : return <EOL> self . say ( "<STR_LIT>" ) <EOL> def revokeAuthorization ( self , notify = True ) : <EOL> """<STR_LIT>""" <EOL> self . _authorized = False <EOL> self . context [ '<STR_LIT>' ] = False <EOL> self . nevermind ( ) <EOL> if not notify : return <EOL> self . say ( "<STR_LIT>" ) <EOL> def notify ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . notificationQueue . append ( event ) <EOL> if len ( self . notificationQueue ) == <NUM_LIT:1> : <EOL> config . reactor . callLater ( <NUM_LIT:1> , self . sendNotifications ) <EOL> def sendNotifications ( self ) : <EOL> """<STR_LIT>""" <EOL> message = "<STR_LIT>" <EOL> for event in self . notificationQueue : <EOL> params = '<STR_LIT:U+002CU+0020>' . join ( "<STR_LIT>" % item for item in event . params . items ( ) ) <EOL> params = escapeToXml ( params ) <EOL> message += "<STR_LIT>" % ( event . name , params ) <EOL> self . notificationQueue = [ ] <EOL> preStyle = '<STR_LIT>' <EOL> postStyle = '<STR_LIT>' <EOL> self . say ( preStyle + message + postStyle ) <EOL> class ConversationContext ( EntityContext ) : <EOL> implements ( IConversationContext ) <EOL> entityAttr = '<STR_LIT>' <EOL> specialKeys = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_conversation ( self ) : <EOL> return self . conversation <EOL> def get_buddy ( self ) : <EOL> return self . conversation . buddy <EOL> def get_agent ( self ) : <EOL> if SupportAgent . exists ( self . conversation . buddy ) : <EOL> return SupportAgent ( self . conversation . buddy ) <EOL> def get_issue ( self ) : <EOL> agent = self [ '<STR_LIT>' ] <EOL> if agent : <EOL> return agent . currentIssue <EOL> def get_sop ( self ) : <EOL> agent = self [ '<STR_LIT>' ] <EOL> if agent : <EOL> return agent . sop <EOL> class ChatRoom ( Entity ) : <EOL> """<STR_LIT>""" <EOL> implements ( IDroneModelChatRoom ) <EOL> jabber = property ( lambda s : services . getService ( '<STR_LIT>' ) ) <EOL> jabber_config = property ( lambda s : s . jabber . SERVICECONFIG ) <EOL> room = property ( lambda s : s . _room ) <EOL> jid = property ( lambda s : '<STR_LIT>' % ( s . room , s . jabber_config . JABBER_CHAT_SERVICE ) ) <EOL> nick = property ( lambda s : s . jabber_config . JABBER_CHAT_NICK ) <EOL> def __init__ ( self , room ) : <EOL> self . _room = room <EOL> self . conversation = Conversation ( self . jid ) <EOL> self . conversation . groupChat = True <EOL> def join ( self ) : <EOL> """<STR_LIT>""" <EOL> self . jabber . joinChatRoom ( self . room ) <EOL> def leave ( self ) : <EOL> """<STR_LIT>""" <EOL> self . jabber . leaveChatRoom ( self . jid ) <EOL> def hear ( self , message ) : <EOL> """<STR_LIT>""" <EOL> if message . lower ( ) . split ( ) [ <NUM_LIT:0> ] == self . nick : <EOL> message = message [ len ( self . nick ) : ] . strip ( ) <EOL> self . conversation . hear ( message ) <EOL> class AlreadyAskingQuestion ( Exception ) : <EOL> def __init__ ( self , question , answers ) : <EOL> self . question = question <EOL> self . answers = answers <EOL> from droned . models . event import Event <EOL> from droned . models . team import SupportAgent <EOL> from droned . responders import dispatch , NoMatch </s>
