Handle kicks and nick already being in use
This commit is contained in:
parent
b3697b0ba6
commit
641950aaea
80
hybridbot.py
80
hybridbot.py
@ -21,6 +21,8 @@ class IRCBot:
|
||||
self.conn = self.client.connection
|
||||
self.opts = opts
|
||||
self.nick = opts['nick']
|
||||
self.pure_nick = self.nick
|
||||
self.chan = opts['chan']
|
||||
self.inter = inter
|
||||
|
||||
def register_handlers(self):
|
||||
@ -30,11 +32,16 @@ class IRCBot:
|
||||
self.conn.add_global_handler('join', 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('kick', self.on_kick)
|
||||
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):
|
||||
print('Connected to IRC')
|
||||
conn.join(self.opts['chan'])
|
||||
self.join_chan()
|
||||
|
||||
def on_message(self, conn, event):
|
||||
nick = event.source.split('!')[0]
|
||||
@ -52,12 +59,14 @@ class IRCBot:
|
||||
nick = event.source.nick
|
||||
|
||||
if typ == 'part':
|
||||
if nick in self.inter.get_irc_users():
|
||||
self.inter.remove_irc_user(nick)
|
||||
if nick != self.nick:
|
||||
if nick in self.inter.get_irc_users():
|
||||
self.inter.remove_irc_user(nick)
|
||||
|
||||
else:
|
||||
if nick not in self.inter.get_irc_users():
|
||||
self.inter.append_irc_user(nick)
|
||||
if nick != self.nick:
|
||||
if nick not in self.inter.get_irc_users():
|
||||
self.inter.append_irc_user(nick)
|
||||
except Exception as e:
|
||||
sys.stderr.write(str(e) + '\n')
|
||||
|
||||
@ -66,15 +75,21 @@ class IRCBot:
|
||||
if nick != conn.get_nickname():
|
||||
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):
|
||||
self.nick = conn.get_nickname() + '_'
|
||||
self.nick = self.nick + '_'
|
||||
conn.nick(self.nick)
|
||||
|
||||
def send_message(self, msg):
|
||||
buffer = 460
|
||||
try:
|
||||
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:
|
||||
sys.stderr.write(str(e) + '\n')
|
||||
|
||||
@ -91,21 +106,28 @@ class XMPPBot:
|
||||
self.client = sleekxmpp.ClientXMPP(opts['jid'], opts['passwd'])
|
||||
self.opts = opts
|
||||
self.nick = opts['nick']
|
||||
self.pure_nick = self.nick
|
||||
self.muc = opts['muc']
|
||||
self.inter = inter
|
||||
|
||||
def register_handlers(self):
|
||||
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('muc::%s::presence' % self.opts['muc'],
|
||||
self.client.add_event_handler('muc::%s::presence' % self.muc,
|
||||
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):
|
||||
print('Connected to XMPP')
|
||||
self.client.get_roster()
|
||||
self.client.send_presence()
|
||||
self.client.plugin['xep_0045'].joinMUC(self.opts['muc'],
|
||||
self.nick,
|
||||
wait=True)
|
||||
self.join_muc()
|
||||
|
||||
def on_message(self, event):
|
||||
body = event['body']
|
||||
@ -115,22 +137,40 @@ class XMPPBot:
|
||||
|
||||
def on_presence(self, event):
|
||||
try:
|
||||
muc_plugin = self.client.plugin['xep_0045']
|
||||
typ = event['muc']['type']
|
||||
nick = event['muc']['nick']
|
||||
|
||||
if typ == 'unavailable':
|
||||
if nick in self.inter.get_xmpp_users():
|
||||
self.inter.remove_xmpp_user(nick)
|
||||
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():
|
||||
self.inter.remove_xmpp_user(nick)
|
||||
else:
|
||||
self.nick = self.pure_nick
|
||||
time.sleep(0.5)
|
||||
self.join_muc()
|
||||
|
||||
else:
|
||||
if nick not in self.inter.get_xmpp_users():
|
||||
self.inter.append_xmpp_user(nick)
|
||||
if nick != self.nick:
|
||||
if nick not in self.inter.get_xmpp_users():
|
||||
self.inter.append_xmpp_user(nick)
|
||||
|
||||
except Exception as e:
|
||||
sys.stderr.write(str(e) + '\n')
|
||||
|
||||
def send_message(self, msg):
|
||||
try:
|
||||
self.client.send_message(mto=self.opts['muc'], mbody=msg,
|
||||
self.client.send_message(mto=self.muc, mbody=msg,
|
||||
mtype='groupchat')
|
||||
except Exception as e:
|
||||
sys.stderr.write(str(e) + '\n')
|
||||
@ -242,8 +282,7 @@ class Intermedia:
|
||||
return self.irc_users
|
||||
|
||||
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):
|
||||
self.irc_users.remove(user)
|
||||
@ -252,8 +291,7 @@ class Intermedia:
|
||||
return self.xmpp_users
|
||||
|
||||
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):
|
||||
self.xmpp_users.remove(user)
|
||||
|
Loading…
Reference in New Issue
Block a user