Ejemplo n.º 1
0
    def add_server(self, event):
        alias = event["spec"][0]
        hostname, sep, port = event["spec"][1].partition(":")
        tls = port.startswith("+")
        port = port.lstrip("+")

        if not hostname or not port or not port.isdigit():
            raise utils.EventError("Please provide <hostname>:[+]<port>")
        port = int(port)

        hostmask = IRCLine.parse_hostmask(event["spec"][2])
        nickname = hostmask.nickname
        username = hostmask.username or nickname
        realname = nickname
        bindhost = hostmask.hostname or None

        try:
            server_id = self.bot.database.servers.add(alias, hostname, port,
                                                      "", tls, bindhost,
                                                      nickname, username,
                                                      realname)
        except Exception as e:
            event["stderr"].write("Failed to add server")
            self.log.error("failed to add server \"%s\"", [alias],
                           exc_info=True)
            return
        event["stdout"].write("Added server '%s'" % alias)
Ejemplo n.º 2
0
def handle_353(event):
    channel = event["server"].channels.get(event["line"].args[2])
    nicknames = event["line"].args.get(3).split(" ")

    # there can sometimes be a dangling space at the end of a 353
    if nicknames and not nicknames[-1]:
        nicknames.pop(-1)

    for nickname in nicknames:
        modes = set([])

        while nickname[0] in event["server"].prefix_symbols:
            modes.add(event["server"].prefix_symbols[nickname[0]])
            nickname = nickname[1:]

        if event["server"].has_capability_str("userhost-in-names"):
            hostmask = IRCLine.parse_hostmask(nickname)
            nickname = hostmask.nickname
            user = event["server"].get_user(hostmask.nickname,
                                            username=hostmask.username,
                                            hostname=hostmask.hostname)
        else:
            user = event["server"].get_user(nickname)
        user.join_channel(channel)
        channel.add_user(user)

        for mode in modes:
            channel.add_mode(mode, nickname)
Ejemplo n.º 3
0
    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"])
Ejemplo n.º 4
0
def handle_333(events, event):
    channel = event["server"].channels.get(event["line"].args[1])

    topic_setter = IRCLine.parse_hostmask(event["line"].args[2])
    topic_time = int(event["line"].args[3])

    channel.set_topic_setter(topic_setter)
    channel.set_topic_time(topic_time)
    events.on("received.333").call(channel=channel,
        setter=topic_setter, set_at=topic_time, server=event["server"])
Ejemplo n.º 5
0
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)