Allow preprocess.send callbacks to request a line not be sent at all

This commit is contained in:
jesopo 2019-06-10 10:11:31 +01:00
parent 512d844a5a
commit 0fc03fd592
2 changed files with 17 additions and 8 deletions

View file

@ -46,12 +46,18 @@ class ParsedLine(object):
self.args = IRCArgs(args) self.args = IRCArgs(args)
self.source = source self.source = source
self.tags = {} if tags == None else tags self.tags = {} if tags == None else tags
self._valid = True
def __repr__(self): def __repr__(self):
return "ParsedLine(%s)" % self.__str__() return "ParsedLine(%s)" % self.__str__()
def __str__(self): def __str__(self):
return self.format() return self.format()
def valid(self) -> bool:
return self._valid
def invalidate(self):
self._valid = False
def _tag_str(self, tags: typing.Dict[str, str]) -> str: def _tag_str(self, tags: typing.Dict[str, str]) -> str:
tag_pieces = [] tag_pieces = []
for tag, value in tags.items(): for tag, value in tags.items():

View file

@ -256,7 +256,8 @@ class Server(IRCObject.Object):
self.events.on("raw.send").call_unsafe(server=self, self.events.on("raw.send").call_unsafe(server=self,
line=line.parsed_line) line=line.parsed_line)
def send(self, line_parsed: IRCLine.ParsedLine, immediate: bool=False): def send(self, line_parsed: IRCLine.ParsedLine, immediate: bool=False
) -> typing.Optional[IRCLine.SentLine]:
if not self.send_enabled: if not self.send_enabled:
return None return None
@ -267,15 +268,17 @@ class Server(IRCObject.Object):
self.events.on("preprocess.send").call_unsafe(server=self, self.events.on("preprocess.send").call_unsafe(server=self,
line=line_parsed, events=line_events) line=line_parsed, events=line_events)
line = line_parsed.format() if line_parsed.valid():
line_obj = IRCLine.SentLine(line_events, datetime.datetime.utcnow(), line = line_parsed.format()
self.hostmask(), line_parsed) line_obj = IRCLine.SentLine(line_events, datetime.datetime.utcnow(),
self.socket.send(line_obj, immediate=immediate) self.hostmask(), line_parsed)
self.socket.send(line_obj, immediate=immediate)
if immediate: if immediate:
self.bot.trigger_write() self.bot.trigger_write()
return line_obj return line_obj
return None
def send_raw(self, line: str): def send_raw(self, line: str):
return self.send(utils.irc.parse_line(line)) return self.send(utils.irc.parse_line(line))