Handle kicks and nick already being in use

This commit is contained in:
Alexei Sorokin 2016-08-11 17:40:15 +03:00
parent b3697b0ba6
commit 641950aaea

View File

@ -21,6 +21,8 @@ class IRCBot:
self.conn = self.client.connection self.conn = self.client.connection
self.opts = opts self.opts = opts
self.nick = opts['nick'] self.nick = opts['nick']
self.pure_nick = self.nick
self.chan = opts['chan']
self.inter = inter self.inter = inter
def register_handlers(self): def register_handlers(self):
@ -30,11 +32,16 @@ class IRCBot:
self.conn.add_global_handler('join', self.on_presence) self.conn.add_global_handler('join', self.on_presence)
self.conn.add_global_handler('part', 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('namreply', self.on_namreply)
self.conn.add_global_handler('kick', self.on_kick)
self.conn.add_global_handler('nicknameinuse', self.on_nicknameinuse) self.conn.add_global_handler('nicknameinuse', self.on_nicknameinuse)
def join_chan(self):
self.conn.part(self.chan, message='Replaced by new connection')
self.conn.join(self.chan)
def on_session_start(self, conn, event): def on_session_start(self, conn, event):
print('Connected to IRC') print('Connected to IRC')
conn.join(self.opts['chan']) self.join_chan()
def on_message(self, conn, event): def on_message(self, conn, event):
nick = event.source.split('!')[0] nick = event.source.split('!')[0]
@ -52,10 +59,12 @@ class IRCBot:
nick = event.source.nick nick = event.source.nick
if typ == 'part': if typ == 'part':
if nick != self.nick:
if nick in self.inter.get_irc_users(): if nick in self.inter.get_irc_users():
self.inter.remove_irc_user(nick) self.inter.remove_irc_user(nick)
else: else:
if nick != self.nick:
if nick not in self.inter.get_irc_users(): if nick not in self.inter.get_irc_users():
self.inter.append_irc_user(nick) self.inter.append_irc_user(nick)
except Exception as e: except Exception as e:
@ -66,15 +75,21 @@ class IRCBot:
if nick != conn.get_nickname(): if nick != conn.get_nickname():
self.inter.append_irc_user(nick) self.inter.append_irc_user(nick)
def on_kick(self, conn, event):
self.nick = self.pure_nick
conn.nick(self.nick)
time.sleep(0.5)
self.join_chan()
def on_nicknameinuse(self, conn, event): def on_nicknameinuse(self, conn, event):
self.nick = conn.get_nickname() + '_' self.nick = self.nick + '_'
conn.nick(self.nick) conn.nick(self.nick)
def send_message(self, msg): def send_message(self, msg):
buffer = 460 buffer = 460
try: try:
for i in range(0, len(msg), buffer): for i in range(0, len(msg), buffer):
self.conn.privmsg(self.opts['chan'], msg[i:i + buffer]) self.conn.privmsg(self.chan, msg[i:i + buffer])
except Exception as e: except Exception as e:
sys.stderr.write(str(e) + '\n') sys.stderr.write(str(e) + '\n')
@ -91,21 +106,28 @@ class XMPPBot:
self.client = sleekxmpp.ClientXMPP(opts['jid'], opts['passwd']) self.client = sleekxmpp.ClientXMPP(opts['jid'], opts['passwd'])
self.opts = opts self.opts = opts
self.nick = opts['nick'] self.nick = opts['nick']
self.pure_nick = self.nick
self.muc = opts['muc']
self.inter = inter self.inter = inter
def register_handlers(self): def register_handlers(self):
self.client.add_event_handler('session_start', self.on_session_start) 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('groupchat_message', self.on_message)
self.client.add_event_handler('muc::%s::presence' % self.opts['muc'], self.client.add_event_handler('muc::%s::presence' % self.muc,
self.on_presence) self.on_presence)
def join_muc(self):
muc_plugin = self.client.plugin['xep_0045']
if self.muc in muc_plugin.getJoinedRooms():
muc_plugin.leaveMUC(self.muc, self.nick, msg='Replaced by new connection')
muc_plugin.joinMUC(self.muc, self.nick, wait=True)
def on_session_start(self, event): def on_session_start(self, event):
print('Connected to XMPP') print('Connected to XMPP')
self.client.get_roster() self.client.get_roster()
self.client.send_presence() self.client.send_presence()
self.client.plugin['xep_0045'].joinMUC(self.opts['muc'], self.join_muc()
self.nick,
wait=True)
def on_message(self, event): def on_message(self, event):
body = event['body'] body = event['body']
@ -115,22 +137,40 @@ class XMPPBot:
def on_presence(self, event): def on_presence(self, event):
try: try:
muc_plugin = self.client.plugin['xep_0045']
typ = event['muc']['type'] typ = event['muc']['type']
nick = event['muc']['nick'] nick = event['muc']['nick']
if typ == 'unavailable': if not typ:
typ = event['type']
if not nick:
nick = muc_plugin.getNick(self.muc, event['from'])
if typ == 'error':
if event['error']['code'] == '409':
self.nick = self.nick + '_'
self.join_muc()
elif typ == 'unavailable':
if nick != self.nick:
if nick in self.inter.get_xmpp_users(): if nick in self.inter.get_xmpp_users():
self.inter.remove_xmpp_user(nick) self.inter.remove_xmpp_user(nick)
else:
self.nick = self.pure_nick
time.sleep(0.5)
self.join_muc()
else: else:
if nick != self.nick:
if nick not in self.inter.get_xmpp_users(): if nick not in self.inter.get_xmpp_users():
self.inter.append_xmpp_user(nick) self.inter.append_xmpp_user(nick)
except Exception as e: except Exception as e:
sys.stderr.write(str(e) + '\n') sys.stderr.write(str(e) + '\n')
def send_message(self, msg): def send_message(self, msg):
try: try:
self.client.send_message(mto=self.opts['muc'], mbody=msg, self.client.send_message(mto=self.muc, mbody=msg,
mtype='groupchat') mtype='groupchat')
except Exception as e: except Exception as e:
sys.stderr.write(str(e) + '\n') sys.stderr.write(str(e) + '\n')
@ -242,7 +282,6 @@ class Intermedia:
return self.irc_users return self.irc_users
def append_irc_user(self, user): def append_irc_user(self, user):
if user != self.ircbot.nick:
self.irc_users.append(user) self.irc_users.append(user)
def remove_irc_user(self, user): def remove_irc_user(self, user):
@ -252,7 +291,6 @@ class Intermedia:
return self.xmpp_users return self.xmpp_users
def append_xmpp_user(self, user): def append_xmpp_user(self, user):
if user != self.xmppbot.nick:
self.xmpp_users.append(user) self.xmpp_users.append(user)
def remove_xmpp_user(self, user): def remove_xmpp_user(self, user):