Added modules
This commit is contained in:
parent
41fec54c30
commit
0277a779c9
8 changed files with 77 additions and 21 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
*~
|
*~
|
||||||
*.pyo
|
*.pyo
|
||||||
|
config.py
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
config = {
|
|
||||||
'jid': '',
|
|
||||||
'password': '',
|
|
||||||
'cacertfile': ''
|
|
||||||
}
|
|
10
config.py.default
Normal file
10
config.py.default
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
config = {
|
||||||
|
'jid': '',
|
||||||
|
'password': '',
|
||||||
|
'cacertfile': '',
|
||||||
|
|
||||||
|
'rooms': [],
|
||||||
|
'nick': '',
|
||||||
|
|
||||||
|
'modules': []
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ from pyxmpp.client import Client
|
||||||
from pyxmpp.jabber.muc import *
|
from pyxmpp.jabber.muc import *
|
||||||
from pyxmpp.interface import implements
|
from pyxmpp.interface import implements
|
||||||
from pyxmpp.interfaces import *
|
from pyxmpp.interfaces import *
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
class MessageHandler:
|
class MessageHandler:
|
||||||
implements(IMessageHandlersProvider)
|
implements(IMessageHandlersProvider)
|
||||||
|
@ -17,26 +18,24 @@ class MessageHandler:
|
||||||
]
|
]
|
||||||
|
|
||||||
def message(self, stanza):
|
def message(self, stanza):
|
||||||
return Message(
|
pass
|
||||||
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):
|
class MucHandler(MucRoomHandler):
|
||||||
|
def __init__(self, client):
|
||||||
|
MucRoomHandler.__init__(self)
|
||||||
|
self.client = client
|
||||||
|
|
||||||
def message_received(self, user, stanza):
|
def message_received(self, user, stanza):
|
||||||
if(user.same_as(self.room_state.me)):
|
if(user.same_as(self.room_state.me)):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.room_state.send_message(stanza.get_body()[::-1])
|
for mod in self.client.module_manager.modules:
|
||||||
|
mod.groupchat(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_body(), self.room_state.send_message)
|
||||||
|
|
||||||
def user_joined(self, user, stanza):
|
def user_joined(self, user, stanza):
|
||||||
if(not self.room_state.joined or user.same_as(self.room_state.me)):
|
if(not self.room_state.joined or user.same_as(self.room_state.me)):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.room_state.send_message('Willkommen, ' + user.nick)
|
|
||||||
|
|
||||||
class PresenceHandler(object):
|
class PresenceHandler(object):
|
||||||
implements(IPresenceHandlersProvider)
|
implements(IPresenceHandlersProvider)
|
||||||
|
|
||||||
|
@ -112,8 +111,11 @@ class VersionHandler(object):
|
||||||
|
|
||||||
|
|
||||||
class XMPPConnection(Client):
|
class XMPPConnection(Client):
|
||||||
def __init__(self, config):
|
def __init__(self, config, modman):
|
||||||
Client.__init__(self, JID(config['jid']), config['password'], tls_settings = TLSSettings(require = True, cacert_file = config['cacertfile']))
|
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 = [
|
self.interface_providers = [
|
||||||
MessageHandler(self),
|
MessageHandler(self),
|
||||||
|
@ -125,4 +127,6 @@ class XMPPConnection(Client):
|
||||||
Client.authorized(self)
|
Client.authorized(self)
|
||||||
self.room_manager = MucRoomManager(self.stream)
|
self.room_manager = MucRoomManager(self.stream)
|
||||||
self.room_manager.set_handlers()
|
self.room_manager.set_handlers()
|
||||||
self.room_manager.join(JID('c3hl@conference.jabber.ccc.de'), 'curunir', MucHandler(), history_maxstanzas=0)
|
|
||||||
|
for room in self.config['rooms']:
|
||||||
|
self.room_manager.join(JID(room), self.config['nick'], MucHandler(self), history_maxstanzas=0)
|
||||||
|
|
20
curunir.py
20
curunir.py
|
@ -3,6 +3,18 @@ from connection.xmpp import XMPPConnection
|
||||||
import signal, sys
|
import signal, sys
|
||||||
from config import config
|
from config import config
|
||||||
|
|
||||||
|
class ModuleManager:
|
||||||
|
def __init__(self, config):
|
||||||
|
self.config = config
|
||||||
|
self.modules = []
|
||||||
|
|
||||||
|
for mod in config['modules']:
|
||||||
|
self.load(mod)
|
||||||
|
|
||||||
|
def load(self, name):
|
||||||
|
mod = __import__('modules.' + name, globals(), locals(), ['Module'])
|
||||||
|
self.modules.append(mod.Module(self))
|
||||||
|
|
||||||
run = True
|
run = True
|
||||||
|
|
||||||
def exithandler(signum, frame):
|
def exithandler(signum, frame):
|
||||||
|
@ -13,10 +25,14 @@ signal.signal(signal.SIGINT, exithandler)
|
||||||
signal.signal(signal.SIGTERM, exithandler)
|
signal.signal(signal.SIGTERM, exithandler)
|
||||||
signal.signal(signal.SIGQUIT, exithandler)
|
signal.signal(signal.SIGQUIT, exithandler)
|
||||||
|
|
||||||
connection = XMPPConnection(config)
|
modman = ModuleManager(config)
|
||||||
|
|
||||||
|
connection = XMPPConnection(config, modman)
|
||||||
connection.connect()
|
connection.connect()
|
||||||
|
|
||||||
while run:
|
while run:
|
||||||
connection.stream.loop_iter(1)
|
act = connection.stream.loop_iter(1)
|
||||||
|
if not act:
|
||||||
|
connection.idle()
|
||||||
|
|
||||||
connection.disconnect()
|
connection.disconnect()
|
||||||
|
|
0
modules/__init__.py
Normal file
0
modules/__init__.py
Normal file
12
modules/base.py
Normal file
12
modules/base.py
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
class ModuleBase:
|
||||||
|
def __init__(self, manager):
|
||||||
|
self.manager = manager
|
||||||
|
|
||||||
|
def commands(self):
|
||||||
|
return []
|
||||||
|
|
||||||
|
def message(self, message_type, message_from, message_subject, message_body, reply):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def groupchat(self, room, message_from, message_body, reply):
|
||||||
|
pass
|
18
modules/help.py
Normal file
18
modules/help.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
from base import ModuleBase
|
||||||
|
import re
|
||||||
|
|
||||||
|
class Module(ModuleBase):
|
||||||
|
def __init__(self, manager):
|
||||||
|
ModuleBase.__init__(self, manager)
|
||||||
|
|
||||||
|
def commands(self):
|
||||||
|
return [('!help', 'Zeigt diese Hilfe an...')]
|
||||||
|
|
||||||
|
def groupchat(self, room, message_from, message_body, reply):
|
||||||
|
if not re.match(r'!help(?:\W|\Z)', message_body):
|
||||||
|
return
|
||||||
|
|
||||||
|
commands = reduce(lambda l, mod: l + mod.commands(), self.manager.modules, [])
|
||||||
|
helpstring = reduce(lambda s, (c, h): s + c + ': ' + h + '\n', commands, '')
|
||||||
|
|
||||||
|
reply('Befehle:\n\n' + helpstring)
|
Reference in a new issue