def receivedMessage(self, e): # Extract the body of the message try: message = str([c for c in e.children if c.name == 'body'][0]) except: log('discarding invalid message (has no body!): %s' % e.toXml()) return # Discard delayed messages delays = [x for x in e.children if x.name == 'delay'] stamps = [ x for x in e.children \ if x.name == 'x' and \ x.compareAttribute('xmlns','jabber:x:delay') and \ x.hasAttribute('stamp') ] #stampstring = str( stamps[0].getAttribute('stamp') ) #timestamp = time.mktime( time.strptime(stampstring, "%Y%m%dT%H:%M:%S") ) if delays or stamps: log('discarding delayed message: %s' % e.toXml()) return # Route message to the right Conversation or ChatRoom entity if e.getAttribute('type') == 'chat': buddy = str(e['from'].split('/')[0]) if not Conversation.exists(buddy): self.requestAuthorization(buddy) log('received message from %s: %s' % (buddy.split('@')[0], message)) Conversation(buddy).hear(message) elif e.getAttribute('type') == 'groupchat': room = e['from'].split('@')[0] log('received message [chatroom=%s]: %s' % (room, message)) ChatRoom(room).hear(message) else: log('received message of unknown type: %s' % e.toXml(), error=True)
def joinEnvironmentalChatRoom(event): """determine if we should join a chatroom""" chat = ChatRoom(config.ROMEO_ENV_NAME) #make sure the drone can be managed by the room username = config.ROMEO_ENV_NAME jbserver = jconfig.JABBER_CHAT_SERVICE jid = "%(username)s@%(jbserver)s" % locals() Team('support').addMember(jid) #get the conversation context set to some sane defaults conversation = Conversation(jid) #grant the room access to the server if jconfig.JABBER_TRUST_ROOM: conversation.grantAuthorization(notify=False) #be vain assume all conversations revolve around ourself context = { 'server': Server(config.HOSTNAME), 'subject': Server(config.HOSTNAME), } conversation.context.update(context) #finally join the room chat.join()
def auth_request(conversation): result = None try: if conversation.authorized: conversation.say("You are already authorized to use this droned.") raise AssertionError('Already authorized to use this droned') conversation.say("Ok, I will pass your request along to the environment administrator. I will let you know what they decide.") deputy = Conversation(jabber_config.DEPUTY) try: question = "%s has requested authorization, do you wish to <b>grant</b> or <b>deny</b> this request?" % conversation.buddy answers = ('grant','deny') d = deputy.ask(question, answers) wfd = defer.waitForDeferred(d) yield wfd answer = wfd.getResult() except AlreadyAskingQuestion: err = "Sorry, the environment administrator is busy dealing with something else right now. " err += "You should wait and try again later, or if it is urgent contact them directly. " err += "The environment administrator is %s" % jabber_config.DEPUTY conversation.say(err) raise AssertionError('Administrator is busy') if answer == 'grant': conversation.grantAuthorization() deputy.say("%s has been granted authorization." % conversation.buddy) else: err = "Your request for authorization has been denied. If you wish to discuss this further you " err += "should contact the environment administrator directly. " err += "The environment administrator is %s" % jabber_config.DEPUTY conversation.say(err) deputy.say("%s has been denied authorization." % conversation.buddy) except AssertionError: pass except: result = Failure() yield result
def __init__(self, jid): self.jid = jid self.conversation = Conversation(jid)