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

161 lines
5 KiB
Python
Raw Normal View History

2010-05-02 06:20:00 +02:00
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 *
2010-05-02 20:28:17 +02:00
from functools import partial
2010-05-02 06:20:00 +02:00
class MessageHandler:
implements(IMessageHandlersProvider)
def __init__(self, client):
self.client = client
def get_message_handlers(self):
return [
("normal", self.message),
]
def message(self, stanza):
2010-05-02 20:28:17 +02:00
pass
2010-05-02 06:20:00 +02:00
class MucHandler(MucRoomHandler):
2010-05-02 20:28:17 +02:00
def __init__(self, client):
MucRoomHandler.__init__(self)
self.client = client
2010-05-02 23:40:21 +02:00
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)
2010-05-02 06:20:00 +02:00
def message_received(self, user, stanza):
2010-05-02 23:40:21 +02:00
if(user == None or user.same_as(self.room_state.me) or stanza.get_body() == None):
2010-05-02 06:20:00 +02:00
return
2010-05-02 21:18:09 +02:00
for mod in self.client.module_manager.modules.itervalues():
2010-05-02 23:40:21 +02:00
mod.groupchat(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_body(), self)
2010-05-02 06:20:00 +02:00
def user_joined(self, user, stanza):
if(not self.room_state.joined or user.same_as(self.room_state.me)):
return
2010-05-02 23:40:21 +02:00
for mod in self.client.module_manager.modules.itervalues():
mod.join(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), stanza.get_status(), self)
def user_left(self, user, stanza):
if(not self.room_state.joined or user.same_as(self.room_state.me)):
return
for mod in self.client.module_manager.modules.itervalues():
mod.leave(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), stanza.get_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)
2010-05-02 06:20:00 +02:00
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):
2010-05-02 20:28:17 +02:00
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)
2010-05-02 06:20:00 +02:00
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()
2010-05-02 20:28:17 +02:00
for room in self.config['rooms']:
self.room_manager.join(JID(room), self.config['nick'], MucHandler(self), history_maxstanzas=0)