From 37df134314fb2112a088eec6f1ed1dc022d8b8e9 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 2 May 2010 06:20:00 +0200 Subject: Initial commit --- connection/__init__.py | 0 connection/xmpp.py | 132 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 connection/__init__.py create mode 100644 connection/xmpp.py (limited to 'connection') diff --git a/connection/__init__.py b/connection/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/connection/xmpp.py b/connection/xmpp.py new file mode 100644 index 0000000..1b2dcf1 --- /dev/null +++ b/connection/xmpp.py @@ -0,0 +1,132 @@ +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): + return Message( + to_jid=stanza.get_from(), + from_jid=stanza.get_to(), + stanza_type=stanza.get_type(), + subject=stanza.get_subject(), + body=stanza.get_body()) + +class MucHandler(MucRoomHandler): + def message_received(self, user, stanza): + if(user.same_as(self.room_state.me)): + return + + self.room_state.send_message(stanza.get_body()[::-1]) + + def user_joined(self, user, stanza): + if(not self.room_state.joined or user.same_as(self.room_state.me)): + return + + self.room_state.send_message('Willkommen, ' + user.nick) + +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): + _jid = JID('curunir@jabber.ccc.de/Orthanc') + _password = '' + _cert_file = 'cacert.pem' + + def __init__(self): + Client.__init__(self, self._jid, self._password, tls_settings = TLSSettings(require = True, cacert_file = self._cert_file)) + + 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() + self.room_manager.join(JID('c3hl@conference.jabber.ccc.de'), 'curunir', MucHandler(), history_maxstanzas=0) -- cgit v1.2.3