def subscribeReceived(self, subscriber): rtr_client = CommonClientManager.getHandler( 'roster', self.my_client ) def rosterAddResponse( response ): log.msg( 'rosterAddResponse', level = logging.DEBUG ) self.subscribed( subscriber ) # subscribe to subscriber user_status = rtr_client.getUserStatus( subscriber ) if not user_status or ( not user_status == 'both' and not user_status == 'to' ): rtr_client.addItem( subscriber.userhostJID() ).addCallback( addSubscriberToRosterResponse ).addErrback( log.err ) def addSubscriberToRosterResponse( response ): log.msg( 'addSubscriberToRosterResponse', level = logging.DEBUG ) if response.attributes['type'] == 'result': self.subscribe( subscriber.userhostJID() ) rtr_client.addItem( subscriber ).addCallback( rosterAddResponse )
def run(self, *args, **kwargs): log.msg( 'MessageAction: run', level = logging.DEBUG ) if self.recipient == "##event_source##": print kwargs.keys() if not 'triggering_element' in kwargs: raise MessageActionExceptionNoTriggeringElement() triggering_element = kwargs[ 'triggering_element' ] log.msg( 'Triggering Element: %s' % triggering_element, level = logging.DEBUG ) my_recipient_jid = jid.JID( triggering_element[ 'from' ] ) else: my_recipient_jid = jid.JID( self.recipient ) if self.message_type == "groupchat": log.msg( 'MessageAction Type: groupchat') muc_client = CommonClientManager.getHandler( 'muc', self.trigger.handler.my_client ) if not muc_client._getRoom( my_recipient_jid ): log.msg( 'not in room', level = logging.DEBUG ) return False def getContentResponse( response ): log.msg( 'MessageAction: getContentResponse', level = logging.DEBUG ) msg = domish.Element( (None, 'message') ) msg['type'] = self.message_type msg['to'] = my_recipient_jid.full() processed_response = response if 'check_response' in kwargs: print kwargs['check_response'] processed_response = response.format( **kwargs[ 'check_response' ].groupdict() ) log.msg( 'processed_response: %s' % processed_response ) msg.addElement('body', None, processed_response ) self.trigger.handler.xmlstream.send( msg ) self.content_handler.getContent().addCallback( getContentResponse )
rtr_client = CommonClientManager.getHandler( 'roster', self.my_client ) def rosterAddResponse( response ): log.msg( 'rosterAddResponse', level = logging.DEBUG ) self.subscribed( subscriber ) # subscribe to subscriber user_status = rtr_client.getUserStatus( subscriber ) if not user_status or ( not user_status == 'both' and not user_status == 'to' ): rtr_client.addItem( subscriber.userhostJID() ).addCallback( addSubscriberToRosterResponse ).addErrback( log.err ) def addSubscriberToRosterResponse( response ): log.msg( 'addSubscriberToRosterResponse', level = logging.DEBUG ) if response.attributes['type'] == 'result': self.subscribe( subscriber.userhostJID() ) rtr_client.addItem( subscriber ).addCallback( rosterAddResponse ) def availableReceived(self, entity, show=None, statuses=None, priority=0): log.msg( 'availableReceived', level = logging.DEBUG ) # ignore if self if entity.full() == self.my_client.jid.full(): return self.received_statuses[ entity.full() ] = 'available' CommonClientManager.addHandler( 'presence', CommonPresenceHandler )
if isinstance( item, type( () ) ) or isinstance( item, type( [] ) ) and len( item ): new_ele = domish.Element( (None, 'array') ) d = new_ele.addElement( (None, 'data') ) for x in item: d.addElement( (None, 'value') ).addChild( objectToElement( x ) ) elif isinstance( item, type( {} ) ): new_ele = domish.Element( (None, 'struct') ) for name, value in item.items(): if not isinstance( name, type( str() ) ): continue m = new_ele.addElement( (None, 'member') ) m.addElement( (None, 'name'), content = str( name ) ) m.addElement( (None, 'value') ).addChild( objectToElement( value ) ) elif item is True or item is False: new_ele = domish.Element( (None, 'boolean') ) new_ele.addContent( ( item and '1' ) or '0' ) elif isinstance( item, type( int() ) ): new_ele = domish.Element( (None, 'int') ) new_ele.addContent( str( item ) ) elif isinstance( item, type( float() ) ): new_ele = domish.Element( (None, 'double') ) new_ele.addContent( str( item ) ) else: new_ele = domish.Element( (None, 'string') ) new_ele.addContent( str( item ) ) return new_ele CommonClientManager.addHandler( 'rpc', RPCProtocolHandler )
from wokkel import xmppim from twisted.words.xish import domish from twisted.python import log import logging from common import CommonClientManager class CommonMessageHandler( xmppim.MessageProtocol ): def __init__( self, client ): super( CommonMessageHandler, self ).__init__() self.my_client = client def sendMessage(self, recipient, message): msg = domish.Element( (None, 'message') ) msg['type'] = 'normal' msg['to'] = recipient.userhost() msg['from'] = self.my_client.jid.full() msg.addElement('body', None, message) self.send( msg ) #self.addElement('subject', None, subject) def onMessage( self, message ): log.msg( 'onMessage', level = logging.DEBUG ) CommonClientManager.addHandler( 'message', CommonMessageHandler )
class ExampleHandler( XMPPHandler ): subscribed_methods = {} def __init__( self, client ): super( ExampleHandler, self ).__init__() self.my_client = client def connectionInitialized(self): self.xmlstream.addObserver( "/message[@type='groupchat']" , self.onGroupchatMessage ) def onGroupchatMessage(self, message): log.msg( 'onGroupchatMessage', level = logging.DEBUG ) from_jid = jid.JID( message.getAttribute( 'from' ) ) if from_jid.resource == self.my_client.user: return if not re.search( '(^(?=(thanks|thank you))|(thanks|thank you)(?=$))', str( message.body ), flags = re.IGNORECASE ): return msg = domish.Element( ( None, 'message' ) ) msg['type'] = 'groupchat' msg['to'] = from_jid.userhost() msg.addElement( 'body', None, 'no, thank you, %s' % from_jid.resource ) print msg self.xmlstream.send( msg ) CommonClientManager.addHandler( 'example', ExampleHandler )
if iq.handled or iq.hasAttribute('from') and iq['from'] != self.my_parent.jid.full(): return iq.handled = True itemElement = iq.query.item if unicode(itemElement['subscription']) == 'remove': self.onRosterRemove(jid.JID(itemElement['jid'])) else: item = self._parseRosterItem(iq.query.item) self.onRosterSet(item) """ def getUserStatus(self, jid): if jid.userhost() in self.my_roster: ritem = self.my_roster[jid.userhost()] if ritem.subscriptionTo and ritem.subscriptionFrom: return "both" elif ritem.subscriptionTo: return "to" elif ritem.subscriptionFrom: return "from" else: return "none" else: return None CommonClientManager.addHandler("roster", CommonRosterHandler)
log.msg(body, level=logging.DEBUG) def inviteEntity(self, entity, room): log.msg("inviteEntity", level=logging.DEBUG) self.invite(room.userhost(), None, entity.full()) def receivedRoomInviteMessage(self, message): log.msg("receivedRoomInviteMessage", level=logging.DEBUG) def response_roomJoined(room): log.msg("response_roomJoined", level=logging.DEBUG) if message["from"].lower() in self._rooms: return room, server = message["from"].split("@") self.join(server, room, self.my_client.jid.user, self.history_options).addCallback( response_roomJoined ).addErrback(log.err) def receivedRoomKickMessage(self, message): log.msg("receivedRoomKickMessage", level=logging.DEBUG) room_jid = jid.JID(message["from"].lower()).userhostJID() self._removeRoom(room_jid) def roomJoined(self, message): log.msg("roomJoined", level=logging.DEBUG) CommonClientManager.addHandler("muc", CommonMucHandler)
from_jid = jid.JID( element['from'] ) if not self.source_entity == from_jid and not self.source_entity == from_jid.userhostJID(): return defer.succeed( False ) # validate presece message type if 'type' in element.attributes: presence_type = element['type'] else: presence_type = 'available' def getContentResponse( response ): log.msg( 'getContentResponse', level = logging.DEBUG ) log.msg( 'response: %s' % response, level = logging.DEBUG ) log.msg( 'type: %s' % presence_type, level = logging.DEBUG ) response_match = re.match( response, presence_type ) if response_match: log.msg( 'matched', level = logging.DEBUG ) return response_match return False if self.content_handler: return self.content_handler.getContent().addCallback( getContentResponse ) return defer.succeed( True ) EVENT_TYPES = { 'message': MessageEventType, 'presence': PresenceEventType } CommonClientManager.addHandler( 'trigger', TriggerHandler )