made the args for IRCLineHandler functions global and added a timer-based channel join re-try.

This commit is contained in:
jesopo 2016-03-31 13:20:03 +01:00
parent 6dff99490c
commit acc38b7460
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3
3 changed files with 55 additions and 33 deletions

View file

@ -1,4 +1,4 @@
import re import re, threading
import Utils import Utils
RE_PREFIXES = re.compile(r"\bPREFIX=\((\w+)\)(\W+)(?:\b|$)") RE_PREFIXES = re.compile(r"\bPREFIX=\((\w+)\)(\W+)(?:\b|$)")
@ -9,6 +9,9 @@ RE_CHANTYPES = re.compile(r"\bCHANTYPES=(\W+)(?:\b|$)")
handlers = {} handlers = {}
descriptions = {} descriptions = {}
current_description = None current_description = None
handle_lock = threading.Lock()
line, line_split, bot, server = None, None, None, None
def handler(f=None, description=None): def handler(f=None, description=None):
global current_description global current_description
if description: if description:
@ -19,35 +22,40 @@ def handler(f=None, description=None):
if current_description: if current_description:
descriptions[name] = current_description descriptions[name] = current_description
current_description = None current_description = None
def handle(line, line_split, bot, server): def handle(_line, _line_split, _bot, _server):
global line, line_split, bot, server
handler_function = None handler_function = None
if len(line_split) > 1: if len(_line_split) > 1:
if line_split[0][0] == ":": if _line_split[0][0] == ":":
if line_split[1] in handlers: if _line_split[1] in handlers:
handler_function = handlers[line_split[1]] handler_function = handlers[_line_split[1]]
elif line_split[1].isdigit(): elif _line_split[1].isdigit():
bot.events.on("received").on("numeric").on( _bot.events.on("received").on("numeric").on(
line_split[1]).call(line=line, _line_split[1]).call(line=_line,
line_split=line_split, server=server, line_split=_line_split, server=_server,
number=line_split[1]) number=_line_split[1])
elif line_split[0] in handlers: elif _line_split[0] in handlers:
handler_function = handlers[line_split[0]] handler_function = handlers[_line_split[0]]
if handler_function: if handler_function:
handler_function(line, line_split, bot, server) with handle_lock:
line, line_split, bot, server = (_line, _line_split,
_bot, _server)
handler_function()
line, line_split, bot, server = None, None, None, None
@handler(description="reply to a ping") @handler(description="reply to a ping")
def handle_PING(line, line_split, bot, server): def handle_PING():
nonce = Utils.remove_colon(line_split[1]) nonce = Utils.remove_colon(line_split[1])
server.send_pong(Utils.remove_colon(line_split[1])) server.send_pong(Utils.remove_colon(line_split[1]))
bot.events.on("received").on("ping").call(line=line, bot.events.on("received").on("ping").call(line=line,
line_split=line_split, server=server, nonce=nonce) line_split=line_split, server=server, nonce=nonce)
@handler(description="the first line sent to a registered client") @handler(description="the first line sent to a registered client")
def handle_001(line, line_split, bot, server): def handle_001():
server.set_own_nickname(line_split[2]) server.set_own_nickname(line_split[2])
server.send_whois(server.nickname) server.send_whois(server.nickname)
bot.events.on("received").on("numeric").on("001").call( bot.events.on("received").on("numeric").on("001").call(
line=line, line_split=line_split, server=server) line=line, line_split=line_split, server=server)
@handler(description="the extra supported things line") @handler(description="the extra supported things line")
def handle_005(line, line_split, bot, server): def handle_005():
isupport_line = Utils.arbitrary(line_split, 3) isupport_line = Utils.arbitrary(line_split, 3)
if "NAMESX" in line: if "NAMESX" in line:
server.send("PROTOCTL NAMESX") server.send("PROTOCTL NAMESX")
@ -68,7 +76,7 @@ def handle_005(line, line_split, bot, server):
line=line, line_split=line_split, server=server, line=line, line_split=line_split, server=server,
isupport=isupport_line) isupport=isupport_line)
@handler(description="whois respose (nickname, username, realname, hostname)") @handler(description="whois respose (nickname, username, realname, hostname)")
def handle_311(line, line_split, bot, server): def handle_311():
nickname = line_split[2] nickname = line_split[2]
if server.is_own_nickname(nickname): if server.is_own_nickname(nickname):
target = server target = server
@ -78,12 +86,12 @@ def handle_311(line, line_split, bot, server):
target.realname = Utils.arbitrary(line_split, 7) target.realname = Utils.arbitrary(line_split, 7)
target.hostname = line_split[5] target.hostname = line_split[5]
@handler(description="on-join channel topic line") @handler(description="on-join channel topic line")
def handle_332(line, line_split, bot, server): def handle_332():
channel = server.get_channel(line_split[3]) channel = server.get_channel(line_split[3])
topic = Utils.arbitrary(line_split, 4) topic = Utils.arbitrary(line_split, 4)
channel.set_topic(topic) channel.set_topic(topic)
@handler(description="on-join channel topic set by/at") @handler(description="on-join channel topic set by/at")
def handle_333(line, line_split, bot, server): def handle_333():
channel = server.get_channel(line_split[3]) channel = server.get_channel(line_split[3])
topic_setter_hostmask = line_split[4] topic_setter_hostmask = line_split[4]
nickname, username, hostname = Utils.seperate_hostmask( nickname, username, hostname = Utils.seperate_hostmask(
@ -93,7 +101,7 @@ def handle_333(line, line_split, bot, server):
channel.set_topic_setter(nickname, username, hostname) channel.set_topic_setter(nickname, username, hostname)
channel.set_topic_time(topic_time) channel.set_topic_time(topic_time)
@handler(description="on-join user list with status symbols") @handler(description="on-join user list with status symbols")
def handle_353(line, line_split, bot, server): def handle_353():
channel = server.get_channel(line_split[4]) channel = server.get_channel(line_split[4])
nicknames = line_split[5:] nicknames = line_split[5:]
nicknames[0] = Utils.remove_colon(nicknames[0]) nicknames[0] = Utils.remove_colon(nicknames[0])
@ -109,7 +117,7 @@ def handle_353(line, line_split, bot, server):
for mode in modes: for mode in modes:
channel.add_mode(mode, nickname) channel.add_mode(mode, nickname)
@handler(description="on user joining channel") @handler(description="on user joining channel")
def handle_JOIN(line, line_split, bot, server): def handle_JOIN():
nickname, username, realname = Utils.seperate_hostmask(line_split[0]) nickname, username, realname = Utils.seperate_hostmask(line_split[0])
channel = server.get_channel(Utils.remove_colon(line_split[2])) channel = server.get_channel(Utils.remove_colon(line_split[2]))
if not server.is_own_nickname(nickname): if not server.is_own_nickname(nickname):
@ -120,12 +128,14 @@ def handle_JOIN(line, line_split, bot, server):
line_split=line_split, server=server, channel=channel, line_split=line_split, server=server, channel=channel,
user=user) user=user)
else: else:
if channel.name in server.attempted_join:
del server.attempted_join[channel.name]
bot.events.on("self").on("join").call(line=line, bot.events.on("self").on("join").call(line=line,
line_split=line_split, server=server, channel=channel) line_split=line_split, server=server, channel=channel)
server.send_who(channel.name) server.send_who(channel.name)
channel.send_mode() channel.send_mode()
@handler(description="on user parting channel") @handler(description="on user parting channel")
def handle_PART(line, line_split, bot, server): def handle_PART():
nickname, username, hostname = Utils.seperate_hostmask(line_split[0]) nickname, username, hostname = Utils.seperate_hostmask(line_split[0])
channel = server.get_channel(line_split[2]) channel = server.get_channel(line_split[2])
reason = Utils.arbitrary(line_split, 3) reason = Utils.arbitrary(line_split, 3)
@ -144,10 +154,10 @@ def handle_PART(line, line_split, bot, server):
line_split=line_split, server=server, channel=channel, line_split=line_split, server=server, channel=channel,
reason=reason) reason=reason)
@handler(description="unknown command sent by us, oops!") @handler(description="unknown command sent by us, oops!")
def handle_421(line, line_split, bot, server): def handle_421():
print("warning: unknown command '%s'." % line_split[3]) print("warning: unknown command '%s'." % line_split[3])
@handler(description="a user has disconnected!") @handler(description="a user has disconnected!")
def handle_QUIT(line, line_split, bot, server): def handle_QUIT():
nickname, username, hostname = Utils.seperate_hostmask(line_split[0]) nickname, username, hostname = Utils.seperate_hostmask(line_split[0])
reason = Utils.arbitrary(line_split, 2) reason = Utils.arbitrary(line_split, 2)
if not server.is_own_nickname(nickname): if not server.is_own_nickname(nickname):
@ -159,7 +169,7 @@ def handle_QUIT(line, line_split, bot, server):
else: else:
server.disconnect() server.disconnect()
@handler(description="someone has changed their nickname") @handler(description="someone has changed their nickname")
def handle_NICK(line, line_split, bot, server): def handle_NICK():
nickname, username, hostname = Utils.seperate_hostmask(line_split[0]) nickname, username, hostname = Utils.seperate_hostmask(line_split[0])
new_nickname = Utils.remove_colon(line_split[2]) new_nickname = Utils.remove_colon(line_split[2])
if not server.is_own_nickname(nickname): if not server.is_own_nickname(nickname):
@ -178,7 +188,7 @@ def handle_NICK(line, line_split, bot, server):
line_split=line_split, server=server, line_split=line_split, server=server,
new_nickname=new_nickname, old_nickname=old_nickname) new_nickname=new_nickname, old_nickname=old_nickname)
@handler(description="something's mode has changed") @handler(description="something's mode has changed")
def handle_MODE(line, line_split, bot, server): def handle_MODE():
nickname, username, hostname = Utils.seperate_hostmask(line_split[0]) nickname, username, hostname = Utils.seperate_hostmask(line_split[0])
target = line_split[2] target = line_split[2]
is_channel = target[0] in server.channel_types is_channel = target[0] in server.channel_types
@ -219,7 +229,7 @@ def handle_MODE(line, line_split, bot, server):
else: else:
server.add_own_mode(char) server.add_own_mode(char)
@handler(description="I've been invited somewhere") @handler(description="I've been invited somewhere")
def handle_INVITE(line, line_split, bot, server): def handle_INVITE():
nickname, username, hostname = Utils.seperate_hostmask(line_split[0]) nickname, username, hostname = Utils.seperate_hostmask(line_split[0])
target_channel = Utils.remove_colon(line_split[3]) target_channel = Utils.remove_colon(line_split[3])
user = server.get_user(nickname) user = server.get_user(nickname)
@ -227,7 +237,7 @@ def handle_INVITE(line, line_split, bot, server):
line=line, line_split=line_split, server=server, line=line, line_split=line_split, server=server,
user=user, target_channel=target_channel) user=user, target_channel=target_channel)
@handler(description="we've received a message") @handler(description="we've received a message")
def handle_PRIVMSG(line, line_split, bot, server): def handle_PRIVMSG():
nickname, username, hostname = Utils.seperate_hostmask(line_split[0]) nickname, username, hostname = Utils.seperate_hostmask(line_split[0])
user = server.get_user(nickname) user = server.get_user(nickname)
message = Utils.arbitrary(line_split, 3) message = Utils.arbitrary(line_split, 3)
@ -250,13 +260,13 @@ def handle_PRIVMSG(line, line_split, bot, server):
action=action) action=action)
user.log.add_line(user.nickname, message, action) user.log.add_line(user.nickname, message, action)
@handler(description="response to a WHO command for user information") @handler(description="response to a WHO command for user information")
def handle_352(line, line_split, bot, server): def handle_352():
user = server.get_user(line_split[7]) user = server.get_user(line_split[7])
user.username = line_split[4] user.username = line_split[4]
user.realname = Utils.arbitrary(line_split, 10) user.realname = Utils.arbitrary(line_split, 10)
user.hostname = line_split[5] user.hostname = line_split[5]
@handler(description="response to an empty mode command") @handler(description="response to an empty mode command")
def handle_324(line, line_split, bot, server): def handle_324():
channel = server.get_channel(line_split[3]) channel = server.get_channel(line_split[3])
modes = line_split[4] modes = line_split[4]
if modes[0] == "+" and modes[1:]: if modes[0] == "+" and modes[1:]:
@ -264,9 +274,14 @@ def handle_324(line, line_split, bot, server):
if mode in server.channel_modes: if mode in server.channel_modes:
channel.add_mode(mode) channel.add_mode(mode)
@handler(description="channel creation unix timestamp") @handler(description="channel creation unix timestamp")
def handle_329(line, line_split, bot, server): def handle_329():
channel = server.get_channel(line_split[3]) channel = server.get_channel(line_split[3])
channel.creation_timestamp = int(line_split[4]) channel.creation_timestamp = int(line_split[4])
@handler(description="nickname already in use") @handler(description="nickname already in use")
def handle_433(line, line_split, bot, server): def handle_433():
pass pass
@handler(description="we need a registered nickname for this channel")
def handle_477():
bot.add_timer(server.try_rejoin, 5, line_split[3],
server.attempted_join[line_split[3].lower()])
#:newirc.tripsit.me 477 BitBot ##nope :Cannot join channel (+r) - you need to be identified with services

View file

@ -26,6 +26,7 @@ class Server(object):
self.channel_modes = [] self.channel_modes = []
self.channel_types = [] self.channel_types = []
self.last_read = None self.last_read = None
self.attempted_join = {}
if ipv4: if ipv4:
self.socket = socket.socket(socket.AF_INET, self.socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM) socket.SOCK_STREAM)
@ -173,7 +174,11 @@ class Server(object):
self.send("PING :%s" % nonce) self.send("PING :%s" % nonce)
def send_pong(self, nonce="hello"): def send_pong(self, nonce="hello"):
self.send("PONG :%s" % nonce) self.send("PONG :%s" % nonce)
def try_rejoin(self, timer, channel_name, key):
if channel_name in self.attempted_join:
self.send_join(channel_name, key)
def send_join(self, channel_name, key=None): def send_join(self, channel_name, key=None):
self.attempted_join[channel_name.lower()] = None
self.send("JOIN %s%s" % (channel_name, self.send("JOIN %s%s" % (channel_name,
"" if key == None else " %s" % key)) "" if key == None else " %s" % key))
def send_part(self, channel_name, reason=None): def send_part(self, channel_name, reason=None):

View file

@ -7,6 +7,7 @@ class Timer(object):
self.kwargs = kwargs self.kwargs = kwargs
self.args = args self.args = args
self._done = False self._done = False
self.call_count = 0
def set_started_time(self): def set_started_time(self):
self.started_time = time.time() self.started_time = time.time()
@ -19,6 +20,7 @@ class Timer(object):
def call(self): def call(self):
self._done = True self._done = True
self.call_count +=1
self.function(self, *self.args, **self.kwargs) self.function(self, *self.args, **self.kwargs)
def redo(self): def redo(self):