aboutsummaryrefslogtreecommitdiff
path: root/modules/ircv3_multiline.py
blob: 18e91d658f5610c7f7cc13cd7355ef940d659878 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from src import ModuleManager, utils

CAP = utils.irc.Capability(None, "bitbot.dev/multiline")
BATCH = utils.irc.BatchType(None, "bitbot.dev/multiline")
TAG = utils.irc.MessageTag(None, "+bitbot.dev/multiline-concat")

class Module(ModuleManager.BaseModule):
    @utils.hook("received.cap.ls")
    @utils.hook("received.cap.new")
    def on_cap(self, event):
        return CAP.copy()

    @utils.hook("preprocess.send.privmsg")
    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 = utils.irc.IRCSendBatch("bitbot.dev/multiline",
                    [target])
                for line in lines:
                    batch.add_line(utils.irc.protocol.privmsg(target, line))
                for line in batch.get_lines():
                    event["server"].send(line)

    @utils.hook("received.batch.end")
    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])]