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 )
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 )
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 )