Merge ChannelAndMuc functionality into Intermedia
This commit is contained in:
parent
454f42dd6c
commit
c108f2ded9
98
hybridbot.py
98
hybridbot.py
@ -15,31 +15,30 @@ else:
|
||||
|
||||
|
||||
class IRCBot(SingleServerIRCBot):
|
||||
def __init__(self, opts, chanmuc, inter):
|
||||
def __init__(self, opts, inter):
|
||||
SingleServerIRCBot.__init__(self, [(opts['server'], opts['port'])],
|
||||
opts['nick'], opts['nick'])
|
||||
|
||||
self.opts = opts
|
||||
self.nick = opts['nick']
|
||||
self.chanmuc = chanmuc
|
||||
self.inter = inter
|
||||
|
||||
def _on_join(self, c, e):
|
||||
nick = e.source.nick
|
||||
|
||||
if nick not in self.chanmuc.chan_users():
|
||||
self.chanmuc.chan_add_user(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.chanmuc.chan_add_user(nick)
|
||||
self.inter.append_irc_user(nick)
|
||||
|
||||
def _on_part(self, c, e):
|
||||
nick = e.source.nick
|
||||
|
||||
if nick in self.chanmuc.chan_users():
|
||||
self.chanmuc.chan_remove_user(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() + '_'
|
||||
@ -53,13 +52,12 @@ class IRCBot(SingleServerIRCBot):
|
||||
nick = e.source.split('!')[0]
|
||||
|
||||
if str(e.arguments[0]) == '.users':
|
||||
users = self.chanmuc.muc_users()
|
||||
users = ', '.join(users)
|
||||
users = ', '.join(self.inter.get_xmpp_users())
|
||||
m = '[ XMPP Users ] ' + users
|
||||
|
||||
buffer = 460
|
||||
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':
|
||||
m = 'The only command I know is \'.users\'. Also, my owner is ' \
|
||||
@ -67,12 +65,12 @@ class IRCBot(SingleServerIRCBot):
|
||||
|
||||
buffer = 460
|
||||
for i in range(0, len(m), buffer):
|
||||
self.inter.irc(m[i:i + buffer])
|
||||
self.inter.to_irc(m[i:i + buffer])
|
||||
|
||||
else:
|
||||
message = '[' + nick + '] ' + ''.join(e.arguments)
|
||||
try:
|
||||
self.inter.xmpp(message)
|
||||
self.inter.to_xmpp(message)
|
||||
except:
|
||||
pass
|
||||
|
||||
@ -82,20 +80,19 @@ class IRCBot(SingleServerIRCBot):
|
||||
if (nick != self.nick):
|
||||
message = '***' + nick + ' ' + ''.join(e.arguments)
|
||||
try:
|
||||
self.inter.xmpp(message)
|
||||
self.inter.to_xmpp(message)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
def __init__(self, opts, chanmuc, inter):
|
||||
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.nick = opts['nick']
|
||||
self.chanmuc = chanmuc
|
||||
self.inter = inter
|
||||
|
||||
def register_handlers(self):
|
||||
@ -122,8 +119,7 @@ class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
|
||||
if text and len(text) > 0 and nick != self.nick:
|
||||
if len(text) == 1 and text[0] == '.users':
|
||||
users = self.chanmuc.chan_users()
|
||||
users = ', '.join(users)
|
||||
users = ', '.join(self.inter.get_irc_users())
|
||||
|
||||
if users:
|
||||
users = '[ IRC Users ] ' + users
|
||||
@ -155,7 +151,7 @@ class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
|
||||
buffer = 460
|
||||
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:
|
||||
print(e)
|
||||
@ -166,12 +162,12 @@ class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
nick = event['muc']['nick']
|
||||
|
||||
if type == 'unavailable':
|
||||
if nick in self.chanmuc.muc_users():
|
||||
self.chanmuc.muc_remove_user(n)
|
||||
if nick in self.inter.get_xmpp_users():
|
||||
self.inter.remove_xmpp_user(n)
|
||||
|
||||
else:
|
||||
if nick not in self.chanmuc.muc_users():
|
||||
self.chanmuc.muc_add_user(nick)
|
||||
if nick not in self.inter.get_xmpp_users():
|
||||
self.inter.append_xmpp_user(nick)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
@ -189,33 +185,6 @@ class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
'mismatch!\n')
|
||||
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:
|
||||
def __init__(self, irc_chan, xmpp_muc):
|
||||
@ -223,12 +192,14 @@ class Intermedia:
|
||||
self.xmpp_muc = xmpp_muc
|
||||
self.ircbot = None
|
||||
self.xmppbot = None
|
||||
self.irc_users = []
|
||||
self.xmpp_users = []
|
||||
|
||||
def set_bots(self, ircbot, xmppbot):
|
||||
self.ircbot = ircbot
|
||||
self.xmppbot = xmppbot
|
||||
|
||||
def xmpp(self, msg):
|
||||
def to_xmpp(self, msg):
|
||||
if self.xmppbot:
|
||||
try:
|
||||
self.xmppbot.jabber.send_message(mto=self.xmpp_muc, mbody=msg,
|
||||
@ -236,13 +207,33 @@ class Intermedia:
|
||||
except:
|
||||
pass
|
||||
|
||||
def irc(self, msg):
|
||||
def to_irc(self, msg):
|
||||
if self.ircbot:
|
||||
try:
|
||||
self.ircbot.connection.privmsg(self.irc_chan, msg)
|
||||
except:
|
||||
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__':
|
||||
parser = SafeConfigParser()
|
||||
xmpp_opts = {}
|
||||
@ -266,10 +257,9 @@ if __name__ == '__main__':
|
||||
xmpp_opts['owner'] = irc_opts['owner']
|
||||
|
||||
try:
|
||||
chanmuc = ChannelAndMuc(irc_opts['nick'], xmpp_opts['nick'])
|
||||
inter = Intermedia(irc_opts['chan'], xmpp_opts['muc'])
|
||||
ircbot = IRCBot(irc_opts, chanmuc, inter)
|
||||
xmppbot = XMPPBot(xmpp_opts, chanmuc, inter)
|
||||
ircbot = IRCBot(irc_opts, inter)
|
||||
xmppbot = XMPPBot(xmpp_opts, inter)
|
||||
inter.set_bots(ircbot, xmppbot)
|
||||
|
||||
irc_thread = Thread(target=ircbot.start, args=())
|
||||
|
Loading…
Reference in New Issue
Block a user