from pyxmpp.all import JID, Message from pyxmpp.streamtls import TLSSettings from pyxmpp.client import Client from pyxmpp.jabber.muc import * from pyxmpp.interface import implements from pyxmpp.interfaces import * class MessageHandler: implements(IMessageHandlersProvider) def __init__(self, client): self.client = client def get_message_handlers(self): return [ ("normal", self.message), ] def message(self, stanza): pass class MucHandler(MucRoomHandler): def __init__(self, client): MucRoomHandler.__init__(self) self.client = client def reply(self, text): self.room_state.send_message(text) def get_topic(self): return self.room_state.subject def set_topic(self, topic): self.room_state.set_subject(topic) def message_received(self, user, stanza): if(user == None or user.same_as(self.room_state.me) or stanza.get_body() == None): return for mod in self.client.module_manager.modules.itervalues(): mod.groupchat(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_body(), self) def user_joined(self, user, stanza): if(not self.room_state.joined or user.same_as(self.room_state.me)): return status = '' if stanza.get_status() != None: status = stanza.get_status() for mod in self.client.module_manager.modules.itervalues(): mod.join(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self) def user_left(self, user, stanza): if(not self.room_state.joined or user.same_as(self.room_state.me)): return status = '' if stanza.get_status() != None: status = stanza.get_status() for mod in self.client.module_manager.modules.itervalues(): mod.leave(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self) def subject_changed(self, user, stanza): topic = '' if stanza.get_subject() != None: topic = stanza.get_subject() for mod in self.client.module_manager.modules.itervalues(): mod.topic(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, topic, self) class PresenceHandler(object): implements(IPresenceHandlersProvider) def __init__(self, client): self.client = client def get_presence_handlers(self): return [ (None, self.presence), ("unavailable", self.presence), ("subscribe", self.presence_control), ("subscribed", self.presence_control), ("unsubscribe", self.presence_control), ("unsubscribed", self.presence_control), ] def presence(self,stanza): msg=u"%s has become " % (stanza.get_from()) t=stanza.get_type() if t=="unavailable": msg+=u"unavailable" else: msg+=u"available" show=stanza.get_show() if show: msg+=u"(%s)" % (show,) status=stanza.get_status() if status: msg+=u": "+status print msg def presence_control(self,stanza): msg=unicode(stanza.get_from()) t=stanza.get_type() if t=="subscribe": msg+=u" has requested presence subscription." elif t=="subscribed": msg+=u" has accepted our presence subscription request." elif t=="unsubscribe": msg+=u" has canceled his subscription of our." elif t=="unsubscribed": msg+=u" has canceled our subscription of his presence." print msg return stanza.make_accept_response() class VersionHandler(object): implements(IIqHandlersProvider, IFeaturesProvider) def __init__(self, client): self.client = client def get_features(self): return ["jabber:iq:version"] def get_iq_get_handlers(self): return [ ("query", "jabber:iq:version", self.get_version), ] def get_iq_set_handlers(self): return [] def get_version(self,iq): iq=iq.make_result_response() q=iq.new_query("jabber:iq:version") q.newTextChild(q.ns(),"name","Curunir XMPP-Bot") q.newTextChild(q.ns(),"version","0.1") return iq class XMPPConnection(Client): def __init__(self, config, modman): self.config = config self.module_manager = modman Client.__init__(self, JID(config['jid']), config['password'], tls_settings = TLSSettings(require = True, cacert_file = config['cacertfile']), keepalive = 60) self.interface_providers = [ MessageHandler(self), PresenceHandler(self), VersionHandler(self) ] def authorized(self): Client.authorized(self) self.room_manager = MucRoomManager(self.stream) self.room_manager.set_handlers() for room in self.config['rooms']: self.room_manager.join(JID(room), self.config['nick'], MucHandler(self), history_maxstanzas=0)