Merge ChannelAndMuc functionality into Intermedia

This commit is contained in:
Alexei Sorokin 2016-08-01 15:55:21 +03:00
parent 454f42dd6c
commit c108f2ded9

View File

@ -15,31 +15,30 @@ else:
class IRCBot(SingleServerIRCBot): class IRCBot(SingleServerIRCBot):
def __init__(self, opts, chanmuc, inter): def __init__(self, opts, inter):
SingleServerIRCBot.__init__(self, [(opts['server'], opts['port'])], SingleServerIRCBot.__init__(self, [(opts['server'], opts['port'])],
opts['nick'], opts['nick']) opts['nick'], opts['nick'])
self.opts = opts self.opts = opts
self.nick = opts['nick'] self.nick = opts['nick']
self.chanmuc = chanmuc
self.inter = inter self.inter = inter
def _on_join(self, c, e): def _on_join(self, c, e):
nick = e.source.nick nick = e.source.nick
if nick not in self.chanmuc.chan_users(): if nick not in self.inter.get_irc_users():
self.chanmuc.chan_add_user(nick) self.inter.append_irc_user(nick)
def _on_namreply(self, c, e): def _on_namreply(self, c, e):
for nick in e.arguments[2].split(): for nick in e.arguments[2].split():
if nick != c.get_nickname(): if nick != c.get_nickname():
self.chanmuc.chan_add_user(nick) self.inter.append_irc_user(nick)
def _on_part(self, c, e): def _on_part(self, c, e):
nick = e.source.nick nick = e.source.nick
if nick in self.chanmuc.chan_users(): if nick in self.inter.get_irc_users():
self.chanmuc.chan_remove_user(nick) self.inter.remove_irc_user(nick)
def on_nicknameinuse(self, c, e): def on_nicknameinuse(self, c, e):
self.nick = c.get_nickname() + '_' self.nick = c.get_nickname() + '_'
@ -53,13 +52,12 @@ class IRCBot(SingleServerIRCBot):
nick = e.source.split('!')[0] nick = e.source.split('!')[0]
if str(e.arguments[0]) == '.users': if str(e.arguments[0]) == '.users':
users = self.chanmuc.muc_users() users = ', '.join(self.inter.get_xmpp_users())
users = ', '.join(users)
m = '[ XMPP Users ] ' + users m = '[ XMPP Users ] ' + users
buffer = 460 buffer = 460
for i in range(0, len(m), buffer): for i in range(0, len(m), buffer):
self.inter.irc(m[i:i + buffer]) self.inter.to_irc(m[i:i + buffer])
elif str(e.arguments[0]) == '.help': elif str(e.arguments[0]) == '.help':
m = 'The only command I know is \'.users\'. Also, my owner is ' \ m = 'The only command I know is \'.users\'. Also, my owner is ' \
@ -67,12 +65,12 @@ class IRCBot(SingleServerIRCBot):
buffer = 460 buffer = 460
for i in range(0, len(m), buffer): for i in range(0, len(m), buffer):
self.inter.irc(m[i:i + buffer]) self.inter.to_irc(m[i:i + buffer])
else: else:
message = '[' + nick + '] ' + ''.join(e.arguments) message = '[' + nick + '] ' + ''.join(e.arguments)
try: try:
self.inter.xmpp(message) self.inter.to_xmpp(message)
except: except:
pass pass
@ -82,20 +80,19 @@ class IRCBot(SingleServerIRCBot):
if (nick != self.nick): if (nick != self.nick):
message = '***' + nick + ' ' + ''.join(e.arguments) message = '***' + nick + ' ' + ''.join(e.arguments)
try: try:
self.inter.xmpp(message) self.inter.to_xmpp(message)
except: except:
pass pass
class XMPPBot(sleekxmpp.ClientXMPP): class XMPPBot(sleekxmpp.ClientXMPP):
def __init__(self, opts, chanmuc, inter): def __init__(self, opts, inter):
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
sleekxmpp.util.misc_ops.setdefaultencoding('utf8') sleekxmpp.util.misc_ops.setdefaultencoding('utf8')
self.jabber = sleekxmpp.ClientXMPP(opts['jid'], opts['passwd']) self.jabber = sleekxmpp.ClientXMPP(opts['jid'], opts['passwd'])
self.opts = opts self.opts = opts
self.nick = opts['nick'] self.nick = opts['nick']
self.chanmuc = chanmuc
self.inter = inter self.inter = inter
def register_handlers(self): def register_handlers(self):
@ -122,8 +119,7 @@ class XMPPBot(sleekxmpp.ClientXMPP):
if text and len(text) > 0 and nick != self.nick: if text and len(text) > 0 and nick != self.nick:
if len(text) == 1 and text[0] == '.users': if len(text) == 1 and text[0] == '.users':
users = self.chanmuc.chan_users() users = ', '.join(self.inter.get_irc_users())
users = ', '.join(users)
if users: if users:
users = '[ IRC Users ] ' + users users = '[ IRC Users ] ' + users
@ -155,7 +151,7 @@ class XMPPBot(sleekxmpp.ClientXMPP):
buffer = 460 buffer = 460
for j in range(0, len(m), buffer): for j in range(0, len(m), buffer):
self.inter.irc(m[j:j + buffer]) self.inter.to_irc(m[j:j + buffer])
except Exception as e: except Exception as e:
print(e) print(e)
@ -166,12 +162,12 @@ class XMPPBot(sleekxmpp.ClientXMPP):
nick = event['muc']['nick'] nick = event['muc']['nick']
if type == 'unavailable': if type == 'unavailable':
if nick in self.chanmuc.muc_users(): if nick in self.inter.get_xmpp_users():
self.chanmuc.muc_remove_user(n) self.inter.remove_xmpp_user(n)
else: else:
if nick not in self.chanmuc.muc_users(): if nick not in self.inter.get_xmpp_users():
self.chanmuc.muc_add_user(nick) self.inter.append_xmpp_user(nick)
except Exception as e: except Exception as e:
print(e) print(e)
@ -189,33 +185,6 @@ class XMPPBot(sleekxmpp.ClientXMPP):
'mismatch!\n') 'mismatch!\n')
sys.exit(1) sys.exit(1)
class ChannelAndMuc:
def __init__(self, irc_nick, xmpp_nick):
self.c_nick = irc_nick
self.m_nick = xmpp_nick
self.c_users = []
self.m_users = []
def muc_add_user(self, user):
if user != self.m_nick:
self.m_users.append(user)
def muc_remove_user(self, user):
self.m_users.remove(user)
def chan_add_user(self, user):
if user != self.c_nick:
self.c_users.append(user)
def chan_remove_user(self, user):
self.c_users.remove(user)
def muc_users(self):
return self.m_users
def chan_users(self):
return self.c_users
class Intermedia: class Intermedia:
def __init__(self, irc_chan, xmpp_muc): def __init__(self, irc_chan, xmpp_muc):
@ -223,12 +192,14 @@ class Intermedia:
self.xmpp_muc = xmpp_muc self.xmpp_muc = xmpp_muc
self.ircbot = None self.ircbot = None
self.xmppbot = None self.xmppbot = None
self.irc_users = []
self.xmpp_users = []
def set_bots(self, ircbot, xmppbot): def set_bots(self, ircbot, xmppbot):
self.ircbot = ircbot self.ircbot = ircbot
self.xmppbot = xmppbot self.xmppbot = xmppbot
def xmpp(self, msg): def to_xmpp(self, msg):
if self.xmppbot: if self.xmppbot:
try: try:
self.xmppbot.jabber.send_message(mto=self.xmpp_muc, mbody=msg, self.xmppbot.jabber.send_message(mto=self.xmpp_muc, mbody=msg,
@ -236,13 +207,33 @@ class Intermedia:
except: except:
pass pass
def irc(self, msg): def to_irc(self, msg):
if self.ircbot: if self.ircbot:
try: try:
self.ircbot.connection.privmsg(self.irc_chan, msg) self.ircbot.connection.privmsg(self.irc_chan, msg)
except: except:
pass pass
def get_irc_users(self):
return self.irc_users
def append_irc_user(self, user):
if user != self.ircbot.nick:
self.irc_users.append(user)
def remove_irc_user(self, user):
self.irc_users.remove(user)
def get_xmpp_users(self):
return self.xmpp_users
def append_xmpp_user(self, user):
if user != self.xmppbot.nick:
self.xmpp_users.append(user)
def remove_xmpp_user(self, user):
self.xmpp_users.remove(user)
if __name__ == '__main__': if __name__ == '__main__':
parser = SafeConfigParser() parser = SafeConfigParser()
xmpp_opts = {} xmpp_opts = {}
@ -266,10 +257,9 @@ if __name__ == '__main__':
xmpp_opts['owner'] = irc_opts['owner'] xmpp_opts['owner'] = irc_opts['owner']
try: try:
chanmuc = ChannelAndMuc(irc_opts['nick'], xmpp_opts['nick'])
inter = Intermedia(irc_opts['chan'], xmpp_opts['muc']) inter = Intermedia(irc_opts['chan'], xmpp_opts['muc'])
ircbot = IRCBot(irc_opts, chanmuc, inter) ircbot = IRCBot(irc_opts, inter)
xmppbot = XMPPBot(xmpp_opts, chanmuc, inter) xmppbot = XMPPBot(xmpp_opts, inter)
inter.set_bots(ircbot, xmppbot) inter.set_bots(ircbot, xmppbot)
irc_thread = Thread(target=ircbot.start, args=()) irc_thread = Thread(target=ircbot.start, args=())