Beispiel #1
0
 def __init__(self, bot):
     self.bot = bot
     self.authlib = AuthLib('%s/data/db-%s-passwd' % (bot.dir, bot.ns))
     self.users = {}
Beispiel #2
0
class Admin(Base):
    """Class to handle admin interface"""

    _reAdminCommand = re.compile(r'^\s*admin\s+(.+?)\s*$', re.I)
    _reRegister = re.compile('^\s*register\s+(\S+)\s*$', re.I)
    _reAuth = re.compile('^\s*(?:log[io]n|auth)\s+(\S+)\s*$', re.I)
    _reFist = re.compile('^\s*fist\s+(\S+)\s+(.+)$', re.I)
    _reHelp = re.compile('^\s*help\s*$', re.I)
    _reLogout = re.compile('^\s*log(?:out|off)\s*$', re.I)
    _reDelUser = re.compile(r'\s*del(?:ete)?\s+(\S+)\s*$', re.I)
    _reListUsers = re.compile(r'\s*list\s+users\s*$', re.I)
    _reChFlag = re.compile(r'\s*chflag\s+(\S+)\s+(\S+)\s*$', re.I)
    _reAddUser = re.compile(r'^\s*add\s+(\S+)\s+(\S+)(?:\s+(\S+))?\s*$', re.I)

    _basic_usage = [
        'help - this screen',
        'register <pass> - register with bot',
        'login <pass> - login to bot',
    ]

    _loggedin_usage = [
        'logout - log out of bot',
    ]

    _admin_usage = [
        'fist <chan> <msg> - make bot say something in channel',
        'add <user> <flags> [pass] - add a user (no pass = no login)',
        'del <user> - delete a user',
        'list users - list users :P',
        'chflag <user> <[+-][aor]> - update user flags',
    ]

    def __init__(self, bot):
        self.bot = bot
        self.authlib = AuthLib('%s/data/db-%s-passwd' % (bot.dir, bot.ns))
        self.users = {}

    def parse(self, req):
        """Parse request for admin commands and execute, returns output"""
        if not self.bot.config.admin.enabled:
            return
        try:
            command = self._reAdminCommand.search(req.message).group(1)
        except:
            return
        nick = req.nick.lower()

        # register
        try:
            passwd = self._reRegister.search(command).group(1)
            return self.registerUser(nick, passwd)
        except:
            pass

        # log in
        try:
            passwd = self._reAuth.search(command).group(1)
            return self.authenticateUser(nick, passwd)
        except:
            pass

        # help
        help = []
        help += self._basic_usage
        if nick in self.users:
            help += self._loggedin_usage
            if self.users[nick].isAdmin():
                help += self._admin_usage
        if self._reHelp.search(command):
            return '\n'.join(help)

        # don't pass this point unless we are logged in
        try:
            user = self.users[nick]
        except:
            return

        # logout
        if Admin._reLogout.search(command):
            del self.users[nick]
            return 'You are now logged out.'

        # functions past here require admin
        if not user.isAdmin():
            return

        try:
            adduser = self._reAddUser.search(command).groups()
            return self.addUser(*adduser)
        except:
            pass

        # be the puppetmaster
        try:
            channel, message = Admin._reFist.search(command).groups()
            req.sendTo = channel
            return message
        except:
            pass

        # delete a user
        try:
            deluser = self._reDelUser.search(command).group(1)
            self.authlib.delete_user(deluser)
            if self.users.has_key(deluser):
                del self.users[deluser]
            return 'User deleted: %s' % deluser
        except:
            pass

        # list users
        try:
            if self._reListUsers.search(command):
                output = []
                passwd = self.authlib.get_passwd()
                for luser, data in passwd.items():
                    flags = []
                    if 'a' in data['flags']:
                        flags.append('admin')
                    if 'r' in data['flags']:
                        flags.append('registered')
                    if 'o' in data['flags']:
                        flags.append('autoop')
                    if self.users.has_key(luser):
                        flags.append('loggedin')
                    flags = ' '.join(flags)
                    output.append('%s: %s' % (luser, flags))
                return '\n'.join(output)
        except:
            pass

        # update user flags
        try:
            chuser, newflags = self._reChFlag.search(command).groups()
            return self.changeFlags(chuser, newflags)
        except:
            pass

    def changeFlags(self, user, chflags):
        """Change flags for a user"""
        curflags = self.authlib.get_flags(user)
        curflags = set(curflags)
        args = re.split(r'([+-])', chflags)[1:]
        for i in range(0, len(args), 2):
            action, flags = args[i], args[i+1]
            flags = set(flags)
            if action == '-':
                for flag in flags:
                    curflags.discard(flag)
            elif action == '+':
                for flag in flags:
                    curflags.add(flag)
        curflags = ''.join(curflags)
        self.authlib.change_flags(user, curflags)
        if self.users.has_key(user):
            self.users[user].flags = curflags
        return 'flags for %s changed to %s' % (user, curflags)

    def addUser(self, user, flags, password):
        """Add a new user"""
        if self.authlib.user_exists(user):
            return "User already registered."
        flags = ''.join(set(flags))
        self.authlib.add_user(user, password, flags)
        return 'user added: %s' % user

    def registerUser(self, user, passwd):
        """Register with the bot"""
        if not self.bot.config.admin.allowRegistration:
            return "Registration is disabled."
        if self.authlib.user_exists(user):
            return "User already registered."
        flags = self.bot.config.admin.defaultFlags
        if not flags:
            flags = 'r'
        flags = set(flags)
        if user.lower() == self.bot.config.main.owner.lower():
            flags.add('a')
        flags = ''.join(flags)
        self.authlib.add_user(user, passwd, flags)
        return "You are now registered, try logging in: login <pass>"

    def authenticateUser(self, user, passwd):
        """Attempt to log in"""
        if not self.authlib.user_exists(user):
            return "You are not registered: try register <password>."
        if not self.authlib.check_user(user, passwd):
            return 'Nice try.. notifying FBI'
        self.users[user] = User(user, self.authlib.get_flags(user))
        return 'You are now logged in. Message me "admin help" for help'
Beispiel #3
0
 def __init__(self, bot):
     self.bot = bot
     self.authlib = AuthLib('%s/data/db-%s-passwd' % (bot.dir, bot.ns))
     self.users = {}
Beispiel #4
0
class Admin(Base):
    """Class to handle admin interface"""

    _reAdminCommand = re.compile(r'^\s*admin\s+(.+?)\s*$', re.I)
    _reRegister = re.compile('^\s*register\s+(\S+)\s*$', re.I)
    _reAuth = re.compile('^\s*(?:log[io]n|auth)\s+(\S+)\s*$', re.I)
    _reFist = re.compile('^\s*fist\s+(\S+)\s+(.+)$', re.I)
    _reHelp = re.compile('^\s*help\s*$', re.I)
    _reLogout = re.compile('^\s*log(?:out|off)\s*$', re.I)
    _reDelUser = re.compile(r'\s*del(?:ete)?\s+(\S+)\s*$', re.I)
    _reListUsers = re.compile(r'\s*list\s+users\s*$', re.I)
    _reChFlag = re.compile(r'\s*chflag\s+(\S+)\s+(\S+)\s*$', re.I)
    _reAddUser = re.compile(r'^\s*add\s+(\S+)\s+(\S+)(?:\s+(\S+))?\s*$', re.I)

    _basic_usage = [
        'help - this screen',
        'register <pass> - register with bot',
        'login <pass> - login to bot',
    ]

    _loggedin_usage = [
        'logout - log out of bot',
    ]

    _admin_usage = [
        'fist <chan> <msg> - make bot say something in channel',
        'add <user> <flags> [pass] - add a user (no pass = no login)',
        'del <user> - delete a user',
        'list users - list users :P',
        'chflag <user> <[+-][aor]> - update user flags',
    ]

    def __init__(self, bot):
        self.bot = bot
        self.authlib = AuthLib('%s/data/db-%s-passwd' % (bot.dir, bot.ns))
        self.users = {}

    def parse(self, req):
        """Parse request for admin commands and execute, returns output"""
        if not self.bot.config.admin.enabled:
            return
        try:
            command = self._reAdminCommand.search(req.message).group(1)
        except:
            return
        nick = req.nick.lower()

        # register
        try:
            passwd = self._reRegister.search(command).group(1)
            return self.registerUser(nick, passwd)
        except:
            pass

        # log in
        try:
            passwd = self._reAuth.search(command).group(1)
            return self.authenticateUser(nick, passwd)
        except:
            pass

        # help
        help = []
        help += self._basic_usage
        if nick in self.users:
            help += self._loggedin_usage
            if self.users[nick].isAdmin():
                help += self._admin_usage
        if self._reHelp.search(command):
            return '\n'.join(help)

        # don't pass this point unless we are logged in
        try:
            user = self.users[nick]
        except:
            return

        # logout
        if Admin._reLogout.search(command):
            del self.users[nick]
            return 'You are now logged out.'

        # functions past here require admin
        if not user.isAdmin():
            return

        try:
            adduser = self._reAddUser.search(command).groups()
            return self.addUser(*adduser)
        except:
            pass

        # be the puppetmaster
        try:
            channel, message = Admin._reFist.search(command).groups()
            req.sendTo = channel
            return message
        except:
            pass

        # delete a user
        try:
            deluser = self._reDelUser.search(command).group(1)
            self.authlib.delete_user(deluser)
            if self.users.has_key(deluser):
                del self.users[deluser]
            return 'User deleted: %s' % deluser
        except:
            pass

        # list users
        try:
            if self._reListUsers.search(command):
                output = []
                passwd = self.authlib.get_passwd()
                for luser, data in passwd.items():
                    flags = []
                    if 'a' in data['flags']:
                        flags.append('admin')
                    if 'r' in data['flags']:
                        flags.append('registered')
                    if 'o' in data['flags']:
                        flags.append('autoop')
                    if self.users.has_key(luser):
                        flags.append('loggedin')
                    flags = ' '.join(flags)
                    output.append('%s: %s' % (luser, flags))
                return '\n'.join(output)
        except:
            pass

        # update user flags
        try:
            chuser, newflags = self._reChFlag.search(command).groups()
            return self.changeFlags(chuser, newflags)
        except:
            pass

    def changeFlags(self, user, chflags):
        """Change flags for a user"""
        curflags = self.authlib.get_flags(user)
        curflags = set(curflags)
        args = re.split(r'([+-])', chflags)[1:]
        for i in range(0, len(args), 2):
            action, flags = args[i], args[i + 1]
            flags = set(flags)
            if action == '-':
                for flag in flags:
                    curflags.discard(flag)
            elif action == '+':
                for flag in flags:
                    curflags.add(flag)
        curflags = ''.join(curflags)
        self.authlib.change_flags(user, curflags)
        if self.users.has_key(user):
            self.users[user].flags = curflags
        return 'flags for %s changed to %s' % (user, curflags)

    def addUser(self, user, flags, password):
        """Add a new user"""
        if self.authlib.user_exists(user):
            return "User already registered."
        flags = ''.join(set(flags))
        self.authlib.add_user(user, password, flags)
        return 'user added: %s' % user

    def registerUser(self, user, passwd):
        """Register with the bot"""
        if not self.bot.config.admin.allowRegistration:
            return "Registration is disabled."
        if self.authlib.user_exists(user):
            return "User already registered."
        flags = self.bot.config.admin.defaultFlags
        if not flags:
            flags = 'r'
        flags = set(flags)
        if user.lower() == self.bot.config.main.owner.lower():
            flags.add('a')
        flags = ''.join(flags)
        self.authlib.add_user(user, passwd, flags)
        return "You are now registered, try logging in: login <pass>"

    def authenticateUser(self, user, passwd):
        """Attempt to log in"""
        if not self.authlib.user_exists(user):
            return "You are not registered: try register <password>."
        if not self.authlib.check_user(user, passwd):
            return 'Nice try.. notifying FBI'
        self.users[user] = User(user, self.authlib.get_flags(user))
        return 'You are now logged in. Message me "admin help" for help'
Beispiel #5
0
 def __init__(self, bot):
     self.bot = bot
     self.users = {}
     self.authlib = AuthLib(u'%s/data/db-%s-passwd' % (bot.prefix,
                                                       bot.namespace),
                            bot.charset)
Beispiel #6
0
class Admin(object):

    """Class to handle admin interface"""

    _admin_cmd_re = re.compile(r'^\s*admin\s+(.+?)\s*$', re.I)
    _register_re = re.compile(u'^\s*register\s+(\S+)\s*$', re.I)
    _auth_re = re.compile(u'^\s*(?:log[io]n|auth)\s+(\S+)\s*$', re.I)
    _fist_re = re.compile(u'^\s*fist\s+(\S+)\s+(.+)$', re.I)
    _help_re = re.compile(u'^\s*help\s*$', re.I)
    _reload_re = re.compile(u'^\s*reload\s*$', re.I)
    _logout_re = re.compile(u'^\s*log(?:out|off)\s*$', re.I)
    _deluser_re = re.compile(r'\s*del(?:ete)?\s+(\S+)\s*$', re.I)
    _list_users_re = re.compile(r'\s*list\s+users\s*$', re.I)
    _chflag_re = re.compile(r'\s*chflag\s+(\S+)\s+(\S+)\s*$', re.I)
    _adduser_re = re.compile(r'^\s*add\s+(\S+)\s+(\S+)(?:\s+(\S+))?\s*$', re.I)
    _basic_usage = [u'help - this screen',
                    u'register <pass> - register with bot',
                    u'login <pass> - login to bot']
    _loggedin_usage = [u'logout - log out of bot']
    _admin_usage = [u'fist <chan> <msg> - make bot say something in channel',
                    u'add <user> <flags> [pass] - add a user',
                    u'del <user> - delete a user',
                    u'list users - list users :P',
                    u'chflag <user> <[+-][aor]> - update user flags']

    def __init__(self, bot):
        self.bot = bot
        self.users = {}
        self.authlib = AuthLib(u'%s/data/db-%s-passwd' % (bot.prefix,
                                                          bot.namespace),
                               bot.charset)

    def parse(self, req):
        """Parse request for admin commands and execute, returns output"""
        if not self.bot.config.admin.enabled:
            return
        try:
            command = self._admin_cmd_re.search(req.message).group(1)
        except AttributeError:
            return
        nick = req.nick.lower()

        # register
        try:
            passwd = self._register_re.search(command).group(1)
            return self.register_user(nick, passwd)
        except AttributeError:
            pass

        # log in
        try:
            passwd = self._auth_re.search(command).group(1)
            return self.authuser(nick, passwd)
        except AttributeError:
            pass

        # reload
        if self._reload_re.search(command):
            self.bot.reload_modules()
            return u'Reloading modules and configuration.'

        # help
        usage = []
        usage += self._basic_usage
        if nick in self.users:
            usage += self._loggedin_usage
            if self.users[nick].is_asmin():
                usage += self._admin_usage
        if self._help_re.search(command):
            return u'\n'.join(usage)

        # don't pass this point unless we are logged in
        if nick not in self.users:
            return
        user = self.users[nick]

        # logout
        if Admin._logout_re.search(command):
            del self.users[nick]
            return u'You are now logged out.'

        # functions past here require admin
        if not user.is_asmin():
            return

        try:
            adduser, flags, password = self._adduser_re.search(command).groups()
            return self.adduser(adduser, flags, password)
        except AttributeError:
            pass

        # be the puppetmaster
        try:
            channel, message = Admin._fist_re.search(command).groups()
            req.sendto = channel
            return message
        except AttributeError:
            pass

        # delete a user
        try:
            deluser = self._deluser_re.search(command).group(1)
            self.authlib.delete_user(deluser)
            if deluser in self.users:
                del self.users[deluser]
            return u'User deleted: %s' % deluser
        except AttributeError:
            pass

        # list users
        if self._list_users_re.search(command):
            output = []
            passwd = self.authlib.get_passwd()
            for luser, data in passwd.items():
                flags = []
                if u'a' in data[u'flags']:
                    flags.append(u'admin')
                if u'r' in data[u'flags']:
                    flags.append(u'registered')
                if u'o' in data[u'flags']:
                    flags.append(u'autoop')
                if luser in self.users:
                    flags.append(u'loggedin')
                flags = u' '.join(flags)
                output.append(u'%s: %s' % (luser, flags))
            return u'\n'.join(output)

        # update user flags
        try:
            chuser, newflags = self._chflag_re.search(command).groups()
            return self.change_flags(chuser, newflags)
        except AttributeError:
            pass

    def change_flags(self, user, chflags):
        """Change flags for a user"""
        curflags = self.authlib.get_flags(user)
        curflags = set(curflags)
        args = re.split(r'([+-])', chflags)[1:]
        for i in range(0, len(args), 2):
            action, flags = args[i], args[i+1]
            flags = set(flags)
            if action == u'-':
                for flag in flags:
                    curflags.discard(flag)
            elif action == u'+':
                for flag in flags:
                    curflags.add(flag)
        curflags = u''.join(curflags)
        self.authlib.change_flags(user, curflags)
        if user in self.users:
            self.users[user].flags = curflags
        return u'flags for %s changed to %s' % (user, curflags)

    def adduser(self, user, flags, password):
        """Add a new user"""
        if self.authlib.user_exists(user):
            return u"User already registered."
        flags = u''.join(set(flags))
        self.authlib.add_user(user, password, flags)
        return u'user added: %s' % user

    def register_user(self, user, passwd):
        """Register with the bot"""
        if not self.bot.config.admin.allowRegistration:
            return u"Registration is disabled."
        if self.authlib.user_exists(user):
            return u"User already registered."
        flags = self.bot.config.admin.defaultFlags
        if not flags:
            flags = u'r'
        flags = set(flags)
        if user.lower() == self.bot.config.main.owner.lower():
            flags.add(u'a')
        flags = u''.join(flags)
        self.authlib.add_user(user, passwd, flags)
        return u"You are now registered, try logging in: login <pass>"

    def authuser(self, user, passwd):
        """Attempt to log in"""
        if not self.authlib.user_exists(user):
            return u"You are not registered: try register <password>."
        if not self.authlib.check_user(user, passwd):
            return u'Nice try.. notifying FBI'
        self.users[user] = User(user, self.authlib.get_flags(user))
        return u'You are now logged in. Message me "admin help" for help'