Пример #1
0
    def func(self):
        """
        Create a new message and send it to channel, using
        the already formatted input.
        """
        channelkey, msg = self.args
        caller = self.caller
        channel = ChannelDB.objects.get_channel(channelkey)
        admin_switches = ("destroy", "emit", "lock", "locks", "desc", "kick")

        # Check that the channel exist
        if not channel:
            self.msg(_("Channel '%s' not found.") % channelkey)
            return

        # Check that the caller is connected
        if not channel.has_connection(caller):
            string = _("You are not connected to channel '%s'.")
            self.msg(string % channelkey)
            return

        # Check that the caller has send access
        if not channel.access(caller, 'send'):
            string = _("You are not permitted to send to channel '%s'.")
            self.msg(string % channelkey)
            return

        # Get the list of connected to this channel
        puppets = [session.puppet for session in SESSION_HANDLER.values() \
                if session.puppet]
        connected = [
            obj for obj in channel.subscriptions.all() if obj in puppets
        ]

        # Handle the various switches
        if self.switch == "me":
            if not msg:
                self.msg("What do you want to do on this channel?")
            else:
                msg = "{} {}".format(caller.key, msg)
                channel.msg(msg, online=True)
        elif self.switch == "who":
            keys = [obj.key for obj in connected]
            keys.sort()
            string = "Connected to the {} channel:".format(channel.key)
            string += ", ".join(keys) if keys else "(no one)"
            string += "."
            self.msg(string)
        elif channel.access(caller,
                            'control') and self.switch in admin_switches:
            if self.switch == "destroy":
                confirm = yield (
                    "Are you sure you want to delete the channel {}? (Y?N)".
                    format(channel.key))
                if confirm.lower() in ("y", "yes"):
                    channel.msg("Destroying the channel.")
                    channel.delete()
                    CHANNELHANDLER.update()
                    self.msg("The channel was destroyed.")
                else:
                    self.msg("Operation cancelled, do not destroy.")
            elif self.switch == "emit":
                if not msg:
                    self.msg("What do you want to say on this channel?")
                else:
                    channel.msg(msg, online=True)
            elif self.switch in ("lock", "locks"):
                if msg:
                    try:
                        channel.locks.add(msg)
                    except LockException as err:
                        self.msg(err)
                        return
                    else:
                        self.msg("Channel permissions were edited.")

                string = "Current locks on {}:\n  {}".format(
                    channel.key, channel.locks)
                self.msg(string)
            elif self.switch == "desc":
                if msg:
                    channel.db.desc = msg
                    self.msg("Channel description was updated.")

                self.msg("Description of the {} channel: {}".format(
                    channel.key, channel.db.desc))
            elif self.switch == "kick":
                if not msg:
                    self.msg("Who do you want to kick from this channel?")
                else:
                    to_kick = caller.search(msg, candidates=connected)
                    if to_kick is None:
                        return

                    channel.disconnect(to_kick)
                    channel.msg("{} has been kicked from the channel.".format(
                        to_kick.key))
                    to_kick.msg(
                        "You have been kicked from the {} channel.".format(
                            channel.key))
        elif self.history_start is not None:
            # Try to view history
            log_file = channel.attributes.get("log_file",
                                              default="channel_%s.log" %
                                              channel.key)
            send_msg = lambda lines: self.msg("".join(
                line.split("[-]", 1)[1] if "[-]" in line else line
                for line in lines))
            tail_log_file(log_file, self.history_start, 20, callback=send_msg)
        elif self.switch:
            self.msg("{}: Invalid switch {}.".format(channel.key, self.switch))
        elif not msg:
            self.msg(_("Say what?"))
            return
        else:
            if caller in channel.mutelist:
                self.msg("You currently have %s muted." % channel)
                return
            channel.msg(msg, senders=self.caller, online=True)
Пример #2
0
    def func(self):
        """Body of the command."""
        caller = self.caller

        # If there's an argument, it's a channel name
        channel = None
        if self.args.strip():
            channel = search_channel(self.args)
            if not channel:
                return

            # Check permissions
            if not channel.access(caller, 'listen'):
                self.msg("%s: You are not allowed to listen to this channel." %
                         channel.key)
                return

        if "join" in self.switches:
            if not channel:
                self.msg("Which channel do you want to join?")
                return

            # If not connected to the channel, try to connect
            if not channel.has_connection(caller):
                if not channel.connect(caller):
                    self.msg("%s: You are not allowed to join this channel." %
                             channel.key)
                    return
                else:
                    self.msg("You now are connected to the %s channel. " %
                             channel.key.lower())
            else:
                self.msg("You already are connected to the %s channel. " %
                         channel.key.lower())
        elif "leave" in self.switches:
            if not channel:
                self.msg("Which channel do you want to leave?")
                return

            # If connected to the channel, try to disconnect
            if channel.has_connection(caller):
                if not channel.disconnect(caller):
                    self.msg(
                        "%s: You are not allowed to disconnect from this channel."
                        % channel.key)
                    return
                else:
                    self.msg("You stop listening to the %s channel. " %
                             channel.key.lower())
            else:
                self.msg("You are not connected to the %s channel. " %
                         channel.key.lower())
        else:
            channels = sorted(ChannelDB.objects.all(),
                              key=lambda chan: chan.key)
            channels = [
                chan for chan in channels if chan.access(caller, 'listen')
            ]
            if channels:
                puppets = [session.puppet for session in SESSION_HANDLER.values() \
                        if session.puppet]
                table = evtable.EvTable("Channel", "Descriptions", "Nb")
                for channel in channels:
                    nb = len([obj for obj in channel.subscriptions.all() \
                            if obj in puppets])
                    table.add_row(channel.key, channel.db.desc, nb)
                self.msg(unicode(table))
            else:
                self.msg("There's currently no channel here... odd...")