aboutsummaryrefslogtreecommitdiff
path: root/src/core_modules/line_handler/message.py
blob: 87b91c9ebbd955ee4a39a1ed6401dc3059f3f4d9 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import uuid
from src import IRCBuffer, IRCLine, utils

def _from_self(server, source):
    if source:
        return server.is_own_nickname(source.nickname)
    else:
        return False

def message(events, event):
    from_self = _from_self(event["server"], event["line"].source)
    if from_self == None:
        return

    direction = "send" if from_self else "received"

    target_str = event["line"].args[0]

    message = None
    if len(event["line"].args) > 1:
        message = event["line"].args[1]

    source = event["line"].source
    if (not event["server"].nickname
            or not source
            or source.hostmask == event["server"].name):
        if source:
            event["server"].name = event["line"].source.hostmask
        else:
            source = IRCLine.parse_hostmask(event["server"].name)
        target_str = event["server"].nickname or "*"

    if from_self:
        user = event["server"].get_user(event["server"].nickname)
    else:
        user = event["server"].get_user(source.nickname,
            username=source.username,
            hostname=source.hostname)

    # strip prefix_symbols from the start of target, for when people use
    # e.g. 'PRIVMSG +#channel :hi' which would send a message to only
    # voiced-or-above users
    statusmsg = ""
    for char in target_str:
        if char in event["server"].statusmsg:
            statusmsg += char
        else:
            break
    target = target_str.replace(statusmsg, "", 1)

    is_channel = event["server"].is_channel(target)

    if is_channel:
        if not target in event["server"].channels:
            return
        target_obj = event["server"].channels.get(target)
    else:
        target_obj = event["server"].get_user(target)

    kwargs = {"server": event["server"], "target": target_obj,
        "target_str": target_str, "user": user, "tags": event["line"].tags,
        "is_channel": is_channel, "from_self": from_self, "line": event["line"],
        "statusmsg": statusmsg}

    action = False

    if message:
        ctcp_message = utils.irc.parse_ctcp(message)

        if ctcp_message:
            if (not ctcp_message.command == "ACTION" or not
                    event["line"].command == "PRIVMSG"):
                if event["line"].command == "PRIVMSG":
                    ctcp_action = "request"
                else:
                    ctcp_action = "response"
                events.on(direction).on("ctcp").on(ctcp_action).call(
                    message=ctcp_message.message, **kwargs)
                events.on(direction).on("ctcp").on(ctcp_action).on(
                    ctcp_message.command).call(message=ctcp_message.message,
                    **kwargs)
                return
            else:
                message = ctcp_message.message
                action = True

    if not message == None:
        kwargs["message"] = message
        kwargs["message_split"] = message.split(" ")
        kwargs["action"] = action

    event_type = event["line"].command.lower()
    if event_type == "privmsg":
        event_type = "message"

    context = "channel" if is_channel else "private"
    hook = events.on(direction).on(event_type).on(context)

    buffer_line = None
    if message:
        buffer_line = IRCBuffer.BufferLine(user.nickname, message, action,
            event["line"].tags, from_self, event["line"].command)

    buffer_obj = target_obj
    if is_channel:
        hook.call(channel=target_obj, buffer_line=buffer_line, **kwargs)
    else:
        buffer_obj = target_obj
        if not from_self:
            buffer_obj = user

        hook.call(buffer_line=buffer_line, **kwargs)

    if buffer_line:
        buffer_obj.buffer.add(buffer_line)