Ejemplo n.º 1
0
 def shh(self, server, line) -> "privmsg":
     if re.match("shh+", Message(line).text) and time.time() - self.lasttime < 30:
         msg = ""
         for i in self.lastmsg:
             if i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
                 msg += "ᴀʙᴄᴅᴇꜰɢʜɪᴊᴋʟᴍɴᴏᴘǫʀꜱᴛᴜᴠᴡxʏᴢ"[ord(i) - 65]
             else:
                 msg += i                    
         server.message(msg, Message(line).context)
Ejemplo n.º 2
0
    def adjust_weights(self, server, line) -> "privmsg":
        msg = Message(line)
        score = self.score(msg.text)
        now = time.time()
        adjust = [i for i in self.lastlines if now - 60 >= i[0]]
        for t, channel, weights, inputs, adjustments in adjust:
            # Commit changes
            for k, v in adjustments.items():
                pass
                # self.settings[k] += sigmoid(v) * self.learningrate

        self.lastlines = [i for i in self.lastlines if now - 60 < i[0]]
        for t, channel, weights, inputs, adjustments in self.lastlines:
            if server.eq(channel, msg.context):
                c = score * (1 - (now - t) / 60)
                for k, v in weights.items():
                    adjustments.setdefault(k, 0)
                    adjustments[k] += c * v
                for i in inputs:
                    self.istats.setdefault(i, 1)
                    self.istats[i] += c * self.learningrate
        self.settings = {k: min(1, max(0, v)) for k, v in self.settings.items()}

        with open(self.configdir + "/settings.json", "w") as f:
            json.dump(self.settings, f)
        with open(self.configdir + "/inputs.json", "w") as f:
            json.dump(self.istats, f)
Ejemplo n.º 3
0
 def trigger(self, words, line):
     message = Message(line)
     videos = re.findall(
         r"(?:youtube\.com/watch\?(?:.+&)?v=|youtu\.be/)([a-zA-Z0-9-_]+)",
         message.message)
     if videos:
         playlist = self.get_playlist_id(
             message.context) or self.create_playlist(message.context)
         for video in videos:
             self.playlist_insert(playlist, video)
Ejemplo n.º 4
0
        def passiveCorrector(self, server, line) -> "privmsg":
            msg = Message(line)
            nick = msg.address.nick
            if not self.dictionary.check(nick):
                self.dictionary.add(nick)
            nick = server.lower(nick)
            if msg.text and msg.text[0] in "@!.:`~/":
                return
            if msg.text.startswith("\x01ACTION") and msg.text.endswith("\x01"):
                data = self.spellcheck(msg.text[8:-1])
            else:
                data = self.spellcheck(msg.text)

            user = self.users.setdefault(nick, [0, 0])
            user[0] += len(data) if data else 0
            user[1] += len(line.split(" ")) - 3
            if user[1] > self.reset_at:
                user[0] /= self.reset_to
                user[1] /= self.reset_to

            if data:
                with sqlite3.connect(self.db) as typos:
                    for i in data:
                        typos.execute(
                            "INSERT INTO typos VALUES (?, ?, ?, ?, ?)",
                            (time.time(), nick, msg.context, self.name, i))

                threshhold_context = self.getSettings(msg.context)
                threshhold_user = self.getSettings(nick)
                if threshhold_user == threshhold_context == None:
                    return

                threshhold = min(threshhold_context,
                                 threshhold_user,
                                 key=lambda x: float("inf")
                                 if x is None else x)

                if user[1] and 1000 * user[0] / user[1] > threshhold:
                    sentence_substitute = ircstrip(msg.text)
                    if sentence_substitute.startswith(
                            "\x01ACTION") and sentence_substitute.endswith(
                                "\x01"):
                        sentence_substitute = "%s %s" % (
                            msg.address.nick, sentence_substitute[8:-1])
                    for word, sub in data.items():
                        sentence_substitute = sentence_substitute.replace(
                            word, "\x02%s\x02" %
                            sub[0] if sub else strikethrough(word))
                    server.message(
                        ("%s: " % msg.address.nick) + sentence_substitute,
                        msg.context)
                    if len(data) == 1:
                        self.last = list(data.keys())[0]
                    else:
                        self.last = None
Ejemplo n.º 5
0
 def authenticate(server, line):
     """ Sends nickserv credentials after the server preamble. """
     msg = Message(line)
     if msg.address.nick == "NickServ":
         if "is a registered nick." in msg.text:
             cmd = "nickserv AUTH %s"
         elif msg.text.startswith("This nickname is registered."):
             cmd = "nickserv IDENTIFY %s"
         else:
             return
         server.sendline(cmd % args["--identify"])
Ejemplo n.º 6
0
        def ctcpreply(server, line):
            """ Reply to ctcp requests """
            msg = Message(line)
            text = msg.text

            if len(text) > 1 and text[0] == text[-1] == "\x01":
                text = text[1:-1].split(" ")
                command = text[0].upper()

                if command in replies:
                    server.printer.raw_message(
                        "NOTICE %s :\x01%s %s\x01" % (
                            msg.address.nick,
                            command,
                            replies[command].format(*text)
                        )
                    )
Ejemplo n.º 7
0
 def custom_cards(self, server, line):
     printer = server.printer
     msg = Message(line)
     if len(msg.words) < 2 or not server.is_admin(msg.address.hostmask):
         return
     if msg.words[0] == "!Q.":
         data = re.sub("_+", "_______", msg.text.split(" ", 1)[-1])
         with self.lock:
             CardsAgainstHumanity.expansionqs.append(data)
             CardsAgainstHumanity.savecards(self.expansiondir)
         printer.message(CAHPREFIX + "Added: 00,01 %s " % (data),
                         msg.context)
     elif msg.words[0] == "!A.":
         data = msg.text.split(" ", 1)[-1]
         data = data.strip()
         if re.search("[^.?!]$", data): data += "."
         with self.lock:
             CardsAgainstHumanity.expansionas.append(data)
             CardsAgainstHumanity.savecards(self.expansiondir)
         printer.message(CAHPREFIX + "Added: 01,00 %s " % (data),
                         msg.context)
Ejemplo n.º 8
0
 def _(*argv):
     try:
         bot = argv[-2]
         msg = Message(argv[-1])
     except IndexError:
         return
     else:
         # Check if we're a bound method.
         if len(argv) == 3:
             fargs = [argv[0], bot, msg]
         else:
             fargs = [bot, msg]
         output = bot.printer.buffer(msg.context, "PRIVMSG")
         if inspect.isgeneratorfunction(funct):
             with output as out:
                 for line in funct(*fargs):
                     out += line
         else:
             rval = funct(*fargs)
             if rval is not None:
                 with output as out:
                     out += rval
Ejemplo n.º 9
0
    def trigger(self, server, line):
        message = Message(line)
        user, target, text = message.address, message.context, message.text
        if server.is_admin(user.hostmask) and message.text.split()[0] == "$":
            args = text.split(" ", 1)[-1]

            if not self.active_shell:
                try:
                    shell = subprocess.Popen(args,
                                             stdin=subprocess.PIPE,
                                             stdout=subprocess.PIPE,
                                             stderr=subprocess.STDOUT,
                                             shell=True,
                                             preexec_fn=os.setsid)
                except OSError:
                    server.printer.message("05bash│ Command failed.", target)
                    return
                self.active_shell = True
                self.shell_thread = Process(shell, target, self)
                self.shell_thread.start()
            else:
                self.shell_thread.stdin.write((args + "\n").encode("utf-8"))
                self.shell_thread.stdin.flush()
Ejemplo n.º 10
0
 def privmsg_check(self, server, line) -> "privmsg":
     msg = Message(line)
     address = msg.address
     if server.lower(address.nick) in self.reminders:
         self.send_messages(address.nick, msg.context)
Ejemplo n.º 11
0
    def trigger(self, server, line):
        msg = Message(line)
        args = msg.text.split(" ", 1)
        if server.is_admin(msg.address.hostmask):
            self.namespace.globals["print"] = self.get_stdout(
                server, msg.context)
            self.namespace.globals["_"] = self.last
            self.namespace.globals["namespace"] = self.namespace
            self.namespace.globals["pop"] = self.namespace
            evaluate = False
            if msg.text == ("%s, undo" % server.nick):
                # Delete the last command off the buffer
                self.curcmd.pop()
                server.printer.message("oh, sure", Message(line).context)
            elif self.building:
                # Code is being added to the buffer.
                # Keep building
                if '"""' in msg.text:
                    # Is the end of the input somewhere in the text?
                    self.building = 0
                    evaluate = True
                self.curcmd.append(msg.text.split('"""', 1)[0])

            elif '"""' in msg.text:
                # Enable code building.
                #TODO: Generalise
                self.building = 1
                act = msg.text.split('"""', 1)[-1]
                if act:
                    self.curcmd = [act]

            elif args[0].endswith(">>>") and args[0][:-3] in ["", server.nick]:
                try:
                    act = args[1]
                except IndexError:
                    act = ""
                if act and (act[-1] in "\\:" or act[0] in " \t@"):
                    self.curcmd += [act[:-1]] if act[-1] == "\\" else [act]
                else:
                    self.curcmd.append(act)
                    evaluate = True
            if evaluate:
                code = "\n".join(self.curcmd)
                print("-------- Executing code --------")
                print(code)
                print("--------------------------------")
                try:
                    assert "\n" not in code
                    output = eval(code, self.namespace.globals,
                                  self.namespace.locals)
                    if type(output) in [map, filter]:
                        output = list(output)
                    self.last = output
                    if output != None:
                        server.printer.message(str(output), msg.context)
                except BaseException:
                    try:
                        exec(code, self.namespace.globals,
                             self.namespace.locals)
                    except BaseException as err:
                        # TODO: Clean this.
                        server.printer.message(
                            "\x02「\x02\x0305 "
                            "oh wow"
                            "\x0307 \x0315%s \x03\x02」\x02 " %
                            (repr(err)[:repr(err).find("(")]) + str(err),
                            msg.context)
                self.curcmd = []
Ejemplo n.º 12
0
def smallvars(server, line):
    msg = Message(line)
    text = re.sub(r"\\[a-z]+", subs, msg.text, flags=re.IGNORECASE)
    if text != msg.text and msg.text[0] not in ".!@:":
        server.message("│ " + text, msg.context)