def get_lines(self) -> typing.List[IRCLine.ParsedLine]: lines = [] for line in self._lines: line.add_tag("batch", self.identifier) lines.append(line) lines.insert(0, IRCLine.ParsedLine("BATCH", ["+%s" % self.identifier, self.type])) lines.append(IRCLine.ParsedLine("BATCH", ["-%s" % self.identifier])) return lines
def parse_line(line: str) -> IRCLine.ParsedLine: tags = {} # type: typing.Dict[str, typing.Any] source = None # type: typing.Optional[IRCLine.Hostmask] command = None if line[0] == "@": tags_prefix, line = line[1:].split(" ", 1) for tag in filter(None, tags_prefix.split(";")): tag, sep, value = tag.partition("=") if value: tags[tag] = message_tag_unescape(value) else: tags[tag] = None line, trailing_separator, trailing_split = line.partition(" :") trailing = None # type: typing.Optional[str] if trailing_separator: trailing = trailing_split if line[0] == ":": source_str, line = line[1:].split(" ", 1) source = seperate_hostmask(source_str) command, sep, line = line.partition(" ") args = [] # type: typing.List[str] if line: # this is so that `args` is empty if `line` is empty args = line.split(" ") if not trailing == None: args.append(typing.cast(str, trailing)) return IRCLine.ParsedLine(command, args, source, tags)
def _out(self, server, target, target_str, is_channel, obj, type, tags): if type == OutType.OUT: color = utils.consts.GREEN else: color = utils.consts.RED line_str = obj.pop() if obj.prefix: line_str = "[%s] %s" % (utils.irc.color(obj.prefix, color), line_str) method = self._command_method(server, target, is_channel) if not method in ["PRIVMSG", "NOTICE"]: raise ValueError("Unknown command-method '%s'" % method) line = IRCLine.ParsedLine(method, [target_str, line_str], tags=tags) valid, trunc = line.truncate(server.hostmask(), margin=STR_MORE_LEN) if trunc: if not trunc[0] in WORD_BOUNDARIES: for boundary in WORD_BOUNDARIES: left, *right = valid.rsplit(boundary, 1) if right: valid = left trunc = right[0] + trunc obj.insert("%s %s" % (STR_CONTINUED, trunc)) valid = valid + STR_MORE line = IRCLine.parse_line(valid) server.send(line)
def whowas(target: str, amount: int=None, server: str=None ) -> IRCLine.ParsedLine: command_args = [target] if amount: command_args.append(str(amount)) if server: command_args.append(server) return IRCLine.ParsedLine("WHOWAS", command_args)
def mode(target: str, mode: str=None, args: typing.List[str]=None ) -> IRCLine.ParsedLine: command_args = [target] if mode: command_args.append(mode) if args: command_args = command_args+args return IRCLine.ParsedLine("MODE", command_args)
def send_message(self, event): our_hostmask = IRCLine.parse_hostmask(event["server"].hostmask()) echo = IRCLine.ParsedLine(event["line"].command, event["line"].args, source=our_hostmask, tags=event["line"].tags) echo.id = event["line"].id self.events.on("raw.received").call(line=echo, server=event["server"])
def preprocess_send_privmsg(self, event): if len(event["line"].args) > 1: if ("\n" in event["line"].args[1] and event["server"].has_capability(CAP)): event["line"].invalidate() target = event["line"].args[0] lines = event["line"].args[1].split("\n") batch = IRCLine.IRCSendBatch("draft/multiline", [target]) for line in lines: line = IRCLine.ParsedLine("PRIVMSG", [target, line]) batch.add_line(line) for line in batch.get_lines(): event["server"].send(line)
def batch_end(self, event): if BATCH.match(event["batch"].type): messages = [] lines = event["batch"].get_lines() for line in lines: message = line.args[1] if TAG.present(line.tags): last_message = "" if messages: last_message = messages.pop(-1) message = last_message + message messages.append(message) target = event["batch"].args[0] message = "\n".join(messages) return [ IRCLine.ParsedLine("PRIVMSG", [target, message], source=event["batch"].source) ]
def _kill(self, signum): if self._exited: return self._exited = True self.events.on("signal.interrupt").call(signum=signum) written = False for server in list(self.bot.servers.values()): if server.connected: server.socket.clear_send_buffer() line = IRCLine.ParsedLine("QUIT", ["Shutting down"]) sent_line = server.send(line, immediate=True) sent_line.events.on("send").hook(self._make_hook(server)) server.send_enabled = False written = True if not written: sys.exit()
def batch_end(identifier: str, tags: typing.Dict[str, str]={}): return IRCLine.ParsedLine("BATCH", ["-%s" % identifier], tags=tags)
def batch_start(identifier: str, batch_type: str, tags: typing.Dict[str, str]={} ) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("BATCH", ["+%s" % identifier, batch_type], tags=tags)
def capability_ls() -> IRCLine.ParsedLine: return IRCLine.ParsedLine("CAP", ["LS", "302"])
def whox(mask: str, filter: str, fields: str, label: str=None ) -> IRCLine.ParsedLine: flags = "%s%%%s%s" % (filter, fields, ","+label if label else "") return IRCLine.ParsedLine("WHO", [mask, flags])
def who(filter: str=None) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("WHO", [filter] if filter else [])
def privmsg(target: str, message: str, tags: typing.Dict[str, str]={} ) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("PRIVMSG", [target, message], tags=tags)
def tagmsg(target, tags: dict) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("TAGMSG", [target], tags=tags)
def _line(self, command: str, unfiltered_args: typing.Sequence[typing.Optional[str]], tags={}): args: typing.List[str] = [a for a in unfiltered_args if not a is None] return IRCLine.ParsedLine(command, args, tags=tags)
def kick(channel_name: str, target: str, reason: str=None ) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("KICK", [channel_name, target]+( [reason] if reason else []))
def names(channel_name: str) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("NAMES", [channel_name])
def topic(channel_name: str, topic: str) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("TOPIC", [channel_name, topic])
def nick(nickname: str) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("NICK", [nickname])
def user(username: str, realname: str) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("USER", [username, "0", "*", realname])
def _(target, message, tags): return IRCLine.ParsedLine(command, [target, message], tags=tags)
def _tagmsg(self, target, state): return IRCLine.ParsedLine("TAGMSG", [target], tags={ "+typing": state, "+draft/typing": state })
def invite(target: str, channel_name: str) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("INVITE", [target, channel_name])
def notice(target: str, message: str, tags: typing.Dict[str, str]={} ) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("NOTICE", [target, message], tags=tags)
def whois(target: str) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("WHOIS", [target])
def part(channel_name: str, reason: str=None) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("PART", [channel_name]+( [reason] if reason else []))
def list(search_for: str=None) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("LIST", [search_for] if search_for else [])
def quit(reason: str=None) -> IRCLine.ParsedLine: return IRCLine.ParsedLine("QUIT", [reason] if reason else [])