Restructure callbacks, IRCBot to not inherit irc.bot.SingleServerIRCBot

This commit is contained in:
Alexei Sorokin 2016-08-11 00:02:04 +03:00
parent 838676ee93
commit 9e5356590e

View File

@ -6,98 +6,120 @@ import time
import re import re
import sleekxmpp import sleekxmpp
from irc.bot import SingleServerIRCBot from irc.bot import SingleServerIRCBot
from irc.bot import Channel
from threading import Thread from threading import Thread
if sys.version_info >= (3, 0): if sys.version_info.major >= 3:
from configparser import SafeConfigParser from configparser import SafeConfigParser
else: else:
from ConfigParser import SafeConfigParser from ConfigParser import SafeConfigParser
class IRCBot(SingleServerIRCBot): class IRCBot:
def __init__(self, opts, inter): def __init__(self, opts, inter):
SingleServerIRCBot.__init__(self, [(opts['server'], opts['port'])], self.client = SingleServerIRCBot([(opts['server'], opts['port'])],
opts['nick'], opts['nick']) opts['nick'], opts['nick'])
self.conn = self.client.connection
self.opts = opts
self.nick = opts['nick']
self.inter = inter
def _on_join(self, c, e):
nick = e.source.nick
if nick not in self.inter.get_irc_users():
self.inter.append_irc_user(nick)
def _on_namreply(self, c, e):
for nick in e.arguments[2].split():
if nick != c.get_nickname():
self.inter.append_irc_user(nick)
def _on_part(self, c, e):
nick = e.source.nick
if nick in self.inter.get_irc_users():
self.inter.remove_irc_user(nick)
def on_nicknameinuse(self, c, e):
self.nick = c.get_nickname() + '_'
c.nick(self.nick)
def on_welcome(self, c, e):
print('Connected to IRC')
c.join(self.opts['chan'])
def on_pubmsg(self, c, e):
nick = e.source.split('!')[0]
body = ''.join(e.arguments)
self.inter.relay_message('irc', nick, body)
def on_action(self, c, e):
nick = e.source.split('!')[0]
body = '/me ' + ''.join(e.arguments)
self.inter.relay_message('irc', nick, body)
class XMPPBot(sleekxmpp.ClientXMPP):
def __init__(self, opts, inter):
if sys.version_info < (3, 0):
sleekxmpp.util.misc_ops.setdefaultencoding('utf8')
self.jabber = sleekxmpp.ClientXMPP(opts['jid'], opts['passwd'])
self.opts = opts self.opts = opts
self.nick = opts['nick'] self.nick = opts['nick']
self.inter = inter self.inter = inter
def register_handlers(self): def register_handlers(self):
self.jabber.add_event_handler('session_start', self.session_start) self.conn.add_global_handler('welcome', self.on_session_start)
self.jabber.add_event_handler('groupchat_message', self.xmpp_message) self.conn.add_global_handler('pubmsg', self.on_message)
self.jabber.add_event_handler('muc::%s::presence' % self.opts['muc'], self.conn.add_global_handler('action', self.on_message)
self.xmpp_presence) self.conn.add_global_handler('join', self.on_presence)
self.conn.add_global_handler('part', self.on_presence)
self.conn.add_global_handler('namreply', self.on_namreply)
self.conn.add_global_handler('nicknameinuse', self.on_nicknameinuse)
def session_start(self, event): def on_session_start(self, conn, event):
self.jabber.get_roster() print('Connected to IRC')
self.jabber.send_presence() conn.join(self.opts['chan'])
self.jabber.plugin['xep_0045'].joinMUC(self.opts['muc'],
def on_message(self, conn, event):
nick = event.source.split('!')[0]
body = ''.join(event.arguments)
typ = event.type
if typ == 'action':
body = '/me ' + body
self.inter.relay_message('irc', nick, body)
def on_presence(self, conn, event):
try:
typ = event.type
nick = event.source.nick
if typ == 'part':
if nick in self.inter.get_irc_users():
self.inter.remove_irc_user(nick)
else:
if nick not in self.inter.get_irc_users():
self.inter.append_irc_user(nick)
except Exception as e:
print(e)
def on_namreply(self, conn, event):
for nick in event.arguments[2].split():
if nick != conn.get_nickname():
self.inter.append_irc_user(nick)
def on_nicknameinuse(self, conn, event):
self.nick = conn.get_nickname() + '_'
conn.nick(self.nick)
def send_message(self, msg):
buffer = 460
try:
for i in range(0, len(msg), buffer):
self.conn.privmsg(self.opts['chan'], msg[i:i + buffer])
except Exception as e:
print(e)
def start(self):
self.register_handlers()
self.client.start()
class XMPPBot:
def __init__(self, opts, inter):
if sys.version_info.major < 3:
sleekxmpp.util.misc_ops.setdefaultencoding('utf-8')
self.client = sleekxmpp.ClientXMPP(opts['jid'], opts['passwd'])
self.opts = opts
self.nick = opts['nick']
self.inter = inter
def register_handlers(self):
self.client.add_event_handler('session_start', self.on_session_start)
self.client.add_event_handler('groupchat_message', self.on_message)
self.client.add_event_handler('muc::%s::presence' % self.opts['muc'],
self.on_presence)
def on_session_start(self, event):
print('Connected to XMPP')
self.client.get_roster()
self.client.send_presence()
self.client.plugin['xep_0045'].joinMUC(self.opts['muc'],
self.nick, self.nick,
wait=True) wait=True)
def xmpp_message(self, event): def on_message(self, event):
body = event['body'] body = event['body']
nick = event['mucnick'] nick = event['mucnick']
self.inter.relay_message('xmpp', nick, body) self.inter.relay_message('xmpp', nick, body)
def xmpp_presence(self, event): def on_presence(self, event):
try: try:
type = event['muc']['type'] typ = event['muc']['type']
nick = event['muc']['nick'] nick = event['muc']['nick']
if type == 'unavailable': if typ == 'unavailable':
if nick in self.inter.get_xmpp_users(): if nick in self.inter.get_xmpp_users():
self.inter.remove_xmpp_user(n) self.inter.remove_xmpp_user(nick)
else: else:
if nick not in self.inter.get_xmpp_users(): if nick not in self.inter.get_xmpp_users():
@ -105,14 +127,20 @@ class XMPPBot(sleekxmpp.ClientXMPP):
except Exception as e: except Exception as e:
print(e) print(e)
def send_message(self, msg):
try:
self.client.send_message(mto=self.opts['muc'], mbody=msg,
mtype='groupchat')
except Exception as e:
print(e)
def start(self): def start(self):
self.jabber.register_plugin('xep_0045') # XMPP MUC. self.client.register_plugin('xep_0045') # XMPP MUC.
self.jabber.register_plugin('xep_0199') # XMPP Ping. self.client.register_plugin('xep_0199') # XMPP Ping.
if self.jabber.connect(): if self.client.connect():
# sys.stderr.write('connected with %s\n'%con) # sys.stderr.write('connected with %s\n'%con)
print('Connected to XMPP')
self.register_handlers() self.register_handlers()
self.jabber.process(block=True) self.client.process(block=True)
else: else:
# sys.stderr.write('could not connect!\n') # sys.stderr.write('could not connect!\n')
sys.stderr.write('Could not connect to server, or password ' + sys.stderr.write('Could not connect to server, or password ' +
@ -137,22 +165,12 @@ class Intermedia:
self.xmppbot = xmppbot self.xmppbot = xmppbot
def to_irc(self, msg): def to_irc(self, msg):
buffer = 460
if self.ircbot: if self.ircbot:
try: self.ircbot.send_message(msg)
for i in range(0, len(msg), buffer):
self.ircbot.connection.privmsg(self.irc_chan,
msg[i:i + buffer])
except:
pass
def to_xmpp(self, msg): def to_xmpp(self, msg):
if self.xmppbot: if self.xmppbot:
try: self.xmppbot.send_message(msg)
self.xmppbot.jabber.send_message(mto=self.xmpp_muc, mbody=msg,
mtype='groupchat')
except:
pass
def relay_message(self, from_net, nick, body): def relay_message(self, from_net, nick, body):
if not self.ircbot or not self.xmppbot: if not self.ircbot or not self.xmppbot: