Stop using global variables

This commit is contained in:
Alexei Sorokin 2016-08-01 15:44:08 +03:00
parent 451765625f
commit 454f42dd6c

View File

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