This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
curunir/connection/xmpp.py
2010-05-02 15:03:32 +02:00

128 lines
3.8 KiB
Python

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):
def __init__(self, config):
Client.__init__(self, JID(config['jid']), config['password'], tls_settings = TLSSettings(require = True, cacert_file = config['cacertfile']))
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)