Restructure callbacks, IRCBot to not inherit irc.bot.SingleServerIRCBot
This commit is contained in:
parent
838676ee93
commit
9e5356590e
186
hybridbot.py
186
hybridbot.py
@ -6,98 +6,120 @@ import time
|
||||
import re
|
||||
import sleekxmpp
|
||||
from irc.bot import SingleServerIRCBot
|
||||
from irc.bot import Channel
|
||||
from threading import Thread
|
||||
if sys.version_info >= (3, 0):
|
||||
if sys.version_info.major >= 3:
|
||||
from configparser import SafeConfigParser
|
||||
else:
|
||||
from ConfigParser import SafeConfigParser
|
||||
|
||||
|
||||
class IRCBot(SingleServerIRCBot):
|
||||
class IRCBot:
|
||||
def __init__(self, opts, inter):
|
||||
SingleServerIRCBot.__init__(self, [(opts['server'], opts['port'])],
|
||||
opts['nick'], opts['nick'])
|
||||
|
||||
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.client = SingleServerIRCBot([(opts['server'], opts['port'])],
|
||||
opts['nick'], opts['nick'])
|
||||
self.conn = self.client.connection
|
||||
self.opts = opts
|
||||
self.nick = opts['nick']
|
||||
self.inter = inter
|
||||
|
||||
def register_handlers(self):
|
||||
self.jabber.add_event_handler('session_start', self.session_start)
|
||||
self.jabber.add_event_handler('groupchat_message', self.xmpp_message)
|
||||
self.jabber.add_event_handler('muc::%s::presence' % self.opts['muc'],
|
||||
self.xmpp_presence)
|
||||
self.conn.add_global_handler('welcome', self.on_session_start)
|
||||
self.conn.add_global_handler('pubmsg', self.on_message)
|
||||
self.conn.add_global_handler('action', self.on_message)
|
||||
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):
|
||||
self.jabber.get_roster()
|
||||
self.jabber.send_presence()
|
||||
self.jabber.plugin['xep_0045'].joinMUC(self.opts['muc'],
|
||||
def on_session_start(self, conn, event):
|
||||
print('Connected to IRC')
|
||||
conn.join(self.opts['chan'])
|
||||
|
||||
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,
|
||||
wait=True)
|
||||
|
||||
def xmpp_message(self, event):
|
||||
def on_message(self, event):
|
||||
body = event['body']
|
||||
nick = event['mucnick']
|
||||
|
||||
self.inter.relay_message('xmpp', nick, body)
|
||||
|
||||
def xmpp_presence(self, event):
|
||||
def on_presence(self, event):
|
||||
try:
|
||||
type = event['muc']['type']
|
||||
typ = event['muc']['type']
|
||||
nick = event['muc']['nick']
|
||||
|
||||
if type == 'unavailable':
|
||||
if typ == 'unavailable':
|
||||
if nick in self.inter.get_xmpp_users():
|
||||
self.inter.remove_xmpp_user(n)
|
||||
self.inter.remove_xmpp_user(nick)
|
||||
|
||||
else:
|
||||
if nick not in self.inter.get_xmpp_users():
|
||||
@ -105,14 +127,20 @@ class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
except Exception as 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):
|
||||
self.jabber.register_plugin('xep_0045') # XMPP MUC.
|
||||
self.jabber.register_plugin('xep_0199') # XMPP Ping.
|
||||
if self.jabber.connect():
|
||||
self.client.register_plugin('xep_0045') # XMPP MUC.
|
||||
self.client.register_plugin('xep_0199') # XMPP Ping.
|
||||
if self.client.connect():
|
||||
# sys.stderr.write('connected with %s\n'%con)
|
||||
print('Connected to XMPP')
|
||||
self.register_handlers()
|
||||
self.jabber.process(block=True)
|
||||
self.client.process(block=True)
|
||||
else:
|
||||
# sys.stderr.write('could not connect!\n')
|
||||
sys.stderr.write('Could not connect to server, or password ' +
|
||||
@ -137,22 +165,12 @@ class Intermedia:
|
||||
self.xmppbot = xmppbot
|
||||
|
||||
def to_irc(self, msg):
|
||||
buffer = 460
|
||||
if self.ircbot:
|
||||
try:
|
||||
for i in range(0, len(msg), buffer):
|
||||
self.ircbot.connection.privmsg(self.irc_chan,
|
||||
msg[i:i + buffer])
|
||||
except:
|
||||
pass
|
||||
self.ircbot.send_message(msg)
|
||||
|
||||
def to_xmpp(self, msg):
|
||||
if self.xmppbot:
|
||||
try:
|
||||
self.xmppbot.jabber.send_message(mto=self.xmpp_muc, mbody=msg,
|
||||
mtype='groupchat')
|
||||
except:
|
||||
pass
|
||||
self.xmppbot.send_message(msg)
|
||||
|
||||
def relay_message(self, from_net, nick, body):
|
||||
if not self.ircbot or not self.xmppbot:
|
||||
|
Loading…
Reference in New Issue
Block a user