Stop using global variables
This commit is contained in:
parent
451765625f
commit
454f42dd6c
148
hybridbot.py
148
hybridbot.py
@ -15,11 +15,14 @@ else:
|
||||
|
||||
|
||||
class IRCBot(SingleServerIRCBot):
|
||||
def __init__(self, channel, nickname, server, port, chanmuc):
|
||||
SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
|
||||
self.channel = channel
|
||||
self.nickname = nickname
|
||||
def __init__(self, opts, chanmuc, 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
|
||||
@ -28,8 +31,6 @@ class IRCBot(SingleServerIRCBot):
|
||||
self.chanmuc.chan_add_user(nick)
|
||||
|
||||
def _on_namreply(self, c, e):
|
||||
bot = c.get_nickname()
|
||||
|
||||
for nick in e.arguments[2].split():
|
||||
if nick != c.get_nickname():
|
||||
self.chanmuc.chan_add_user(nick)
|
||||
@ -41,15 +42,15 @@ class IRCBot(SingleServerIRCBot):
|
||||
self.chanmuc.chan_remove_user(nick)
|
||||
|
||||
def on_nicknameinuse(self, c, e):
|
||||
self.nickname = c.get_nickname() + '_'
|
||||
c.nick(self.nickname)
|
||||
self.nick = c.get_nickname() + '_'
|
||||
c.nick(self.nick)
|
||||
|
||||
def on_welcome(self, c, e):
|
||||
print('Connected to IRC')
|
||||
c.join(self.channel)
|
||||
c.join(self.opts['chan'])
|
||||
|
||||
def on_pubmsg(self, c, e):
|
||||
from_nick = e.source.split('!')[0]
|
||||
nick = e.source.split('!')[0]
|
||||
|
||||
if str(e.arguments[0]) == '.users':
|
||||
users = self.chanmuc.muc_users()
|
||||
@ -58,54 +59,56 @@ class IRCBot(SingleServerIRCBot):
|
||||
|
||||
buffer = 460
|
||||
for i in range(0, len(m), buffer):
|
||||
inter.irc(m[i:i + buffer])
|
||||
self.inter.irc(m[i:i + buffer])
|
||||
|
||||
elif str(e.arguments[0]) == '.help':
|
||||
m = 'The only command I know is \'.users\'. Also, my owner is ' \
|
||||
+ owner + '.'
|
||||
+ self.opts['owner'] + '.'
|
||||
|
||||
buffer = 460
|
||||
for i in range(0, len(m), buffer):
|
||||
inter.irc(m[i:i + buffer])
|
||||
self.inter.irc(m[i:i + buffer])
|
||||
|
||||
else:
|
||||
message = '[' + from_nick + '] ' + ''.join(e.arguments)
|
||||
message = '[' + nick + '] ' + ''.join(e.arguments)
|
||||
try:
|
||||
inter.xmpp(message)
|
||||
self.inter.xmpp(message)
|
||||
except:
|
||||
pass
|
||||
|
||||
def on_action(self, c, e):
|
||||
from_nick = e.source.split('!')[0]
|
||||
nick = e.source.split('!')[0]
|
||||
|
||||
if (from_nick != i_nick):
|
||||
message = '***' + from_nick + ' ' + ''.join(e.arguments)
|
||||
if (nick != self.nick):
|
||||
message = '***' + nick + ' ' + ''.join(e.arguments)
|
||||
try:
|
||||
inter.xmpp(message)
|
||||
self.inter.xmpp(message)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
def __init__(self, remotejid, chanmuc):
|
||||
def __init__(self, opts, chanmuc, inter):
|
||||
if sys.version_info < (3, 0):
|
||||
sleekxmpp.util.misc_ops.setdefaultencoding('utf8')
|
||||
|
||||
self.jabber = sleekxmpp.ClientXMPP(jid, password)
|
||||
self.remotejid = remotejid
|
||||
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):
|
||||
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.remotejid,
|
||||
self.jabber.add_event_handler('muc::%s::presence' % self.opts['muc'],
|
||||
self.xmpp_presence)
|
||||
|
||||
def session_start(self, event):
|
||||
self.jabber.get_roster()
|
||||
self.jabber.send_presence()
|
||||
self.jabber.plugin['xep_0045'].joinMUC(self.remotejid,
|
||||
m_nick,
|
||||
self.jabber.plugin['xep_0045'].joinMUC(self.opts['muc'],
|
||||
self.nick,
|
||||
wait=True)
|
||||
|
||||
def xmpp_message(self, event):
|
||||
@ -117,7 +120,7 @@ class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
if text:
|
||||
text = text.replace('\r\n', '\n').replace('\r', '\n').split('\n')
|
||||
|
||||
if text and len(text) > 0 and nick != m_nick:
|
||||
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)
|
||||
@ -125,14 +128,14 @@ class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
if users:
|
||||
users = '[ IRC Users ] ' + users
|
||||
|
||||
self.jabber.send_message(mto=muc, mbody=users,
|
||||
self.jabber.send_message(mto=self.opts['muc'], mbody=users,
|
||||
mtype='groupchat')
|
||||
|
||||
elif len(text) == 1 and text[0] == '.help':
|
||||
message = 'The only command I have is \'.users\''+ \
|
||||
'. Also, my owner is ' + owner + '.'
|
||||
'. Also, my owner is ' + self.opts['owner'] + '.'
|
||||
|
||||
self.jabber.send_message(mto=muc, mbody=message,
|
||||
self.jabber.send_message(mto=self.opts['muc'], mbody=message,
|
||||
mtype='groupchat')
|
||||
|
||||
else:
|
||||
@ -152,7 +155,7 @@ class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
|
||||
buffer = 460
|
||||
for j in range(0, len(m), buffer):
|
||||
inter.irc(m[j:j + buffer])
|
||||
self.inter.irc(m[j:j + buffer])
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
@ -187,19 +190,21 @@ class XMPPBot(sleekxmpp.ClientXMPP):
|
||||
sys.exit(1)
|
||||
|
||||
class ChannelAndMuc:
|
||||
def __init__(self):
|
||||
self.m_users = []
|
||||
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 != m_nick:
|
||||
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 != i_nick:
|
||||
if user != self.c_nick:
|
||||
self.c_users.append(user)
|
||||
|
||||
def chan_remove_user(self, user):
|
||||
@ -213,54 +218,71 @@ class ChannelAndMuc:
|
||||
|
||||
|
||||
class Intermedia:
|
||||
def xmpp(self, m):
|
||||
def __init__(self, irc_chan, xmpp_muc):
|
||||
self.irc_chan = irc_chan
|
||||
self.xmpp_muc = xmpp_muc
|
||||
self.ircbot = None
|
||||
self.xmppbot = None
|
||||
|
||||
def set_bots(self, ircbot, xmppbot):
|
||||
self.ircbot = ircbot
|
||||
self.xmppbot = xmppbot
|
||||
|
||||
def xmpp(self, msg):
|
||||
if self.xmppbot:
|
||||
try:
|
||||
xmppbot.jabber.send_message(mto=muc, mbody=m, mtype='groupchat')
|
||||
self.xmppbot.jabber.send_message(mto=self.xmpp_muc, mbody=msg,
|
||||
mtype='groupchat')
|
||||
except:
|
||||
pass
|
||||
|
||||
def irc(self, m):
|
||||
def irc(self, msg):
|
||||
if self.ircbot:
|
||||
try:
|
||||
ircb0t.connection.privmsg(channel, m)
|
||||
self.ircbot.connection.privmsg(self.irc_chan, msg)
|
||||
except:
|
||||
pass
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
global xmppbot, ircb0t, muc, channel, inter, jid, password, m_nick, owner
|
||||
|
||||
parser = SafeConfigParser()
|
||||
xmpp_opts = {}
|
||||
irc_opts = {}
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
parser.read(sys.argv[1])
|
||||
else:
|
||||
parser.read('config.ini')
|
||||
|
||||
jid = parser.get('XMPP', 'jid')
|
||||
password = parser.get('XMPP', 'password')
|
||||
muc = parser.get('XMPP', 'muc')
|
||||
m_nick = parser.get('XMPP', 'nick')
|
||||
irc_opts['chan'] = parser.get('IRC', 'channel')
|
||||
irc_opts['nick'] = parser.get('IRC', 'nick')
|
||||
irc_opts['server'] = parser.get('IRC', 'server')
|
||||
irc_opts['port'] = int(parser.get('IRC', 'port'))
|
||||
irc_opts['owner'] = parser.get('IRC', 'owner')
|
||||
|
||||
channel = str(parser.get('IRC', 'channel'))
|
||||
i_nick = parser.get('IRC', 'nick')
|
||||
server = parser.get('IRC', 'server')
|
||||
port = int(parser.get('IRC', 'port'))
|
||||
|
||||
owner = parser.get('IRC', 'owner')
|
||||
chanmuc = ChannelAndMuc()
|
||||
xmpp_opts['jid'] = parser.get('XMPP', 'jid')
|
||||
xmpp_opts['passwd'] = parser.get('XMPP', 'password')
|
||||
xmpp_opts['muc'] = parser.get('XMPP', 'muc')
|
||||
xmpp_opts['nick'] = parser.get('XMPP', 'nick')
|
||||
xmpp_opts['owner'] = irc_opts['owner']
|
||||
|
||||
try:
|
||||
ircb0t = IRCBot(channel, i_nick, server, port, chanmuc)
|
||||
xmppbot = XMPPBot(muc, chanmuc)
|
||||
inter = Intermedia()
|
||||
z = Thread(target=ircb0t.start, args=())
|
||||
w = Thread(target=xmppbot.start, args=())
|
||||
z.daemon = True
|
||||
w.daemon = True
|
||||
z.start()
|
||||
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)
|
||||
inter.set_bots(ircbot, xmppbot)
|
||||
|
||||
irc_thread = Thread(target=ircbot.start, args=())
|
||||
xmpp_thread = Thread(target=xmppbot.start, args=())
|
||||
|
||||
irc_thread.daemon = True
|
||||
xmpp_thread.daemon = True
|
||||
|
||||
irc_thread.start()
|
||||
time.sleep(1)
|
||||
w.start()
|
||||
z.join()
|
||||
w.join()
|
||||
xmpp_thread.start()
|
||||
|
||||
irc_thread.join()
|
||||
xmpp_thread.join()
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
print('Exit')
|
||||
|
Loading…
Reference in New Issue
Block a user