Beispiel #1
0
def manage_unread_users(m, conn):
    #Collect all unread users from all plugins
    cn, c = Globals.master.open_cursor()
    try:
        mess = message.Message('unread_users')
        uu = get_data_from_plugins(mess, c, 'users')
        #Fetch all users
        c2 = Globals.master.add_cursor(cn)
        c.execute('select id, name from users')
        users = []
        for row in c:
            count = 0
            c2.execute('select user, instance_id from users_entries where user_id=?', (row[0], ))
            for row2 in c2:
                if row2[1] in uu:
                    net, arr = uu[row2[1]]
                    for mess in arr:
                        if mess.get('userid') == row2[0]:
                            count = count + int(mess.get('count', 0))
            if count>0:
                u = message.Message('user')
                u.set('userid', row[0])
                u.set('user', row[1])
                u.set('count', count)
                users.append(u)
        resp = message.response_message(m, 'unread_users')
        resp.set('users', users)
        conn.send_message(resp)
        Globals.master.commit(cn)
    except Exception, err:
        Globals.master.rollback(cn)
        logging.exception('Error while manage_unread_users: %s', err)
        report_error(m, conn, 'Error while listing unread users')
Beispiel #2
0
def manage_unread_networks(m, conn):
    cn, c = Globals.master.open_cursor()
    try:
        mess = message.Message('unread_messages')
        um = get_data_from_plugins(mess, c, 'messages', [])
        arr = []
        for id in Globals.plugin_instances:
            pl = Globals.plugin_instances[id]
            #logging.debug('un: %s, %s, %s', id, pl.disabled, um)
            if pl.disabled or pl.instance_id not in um:
                continue
            net, marr = um[pl.instance_id]
            if len(marr)<1:
                continue
            n = message.Message('net')
            n.set('name', id)
            n.set('count', len(marr))
            arr.append(n)
        resp = message.response_message(m, 'unread_networks')
        resp.set('networks', arr)
        conn.send_message(resp)
        Globals.master.commit(cn)
    except Exception, err:
        Globals.master.rollback(cn)
        logging.exception('Error while manage_unread_networks: %s', err)
        report_error(m, conn, 'Error while listing unread networks')
Beispiel #3
0
def manage_rules(m, conn):
    """
    Block messages rule management
    """
#    cn, c = Globals.master.open_cursor()
    try:
#        Globals.master.commit(cn)
        if m.get('add'):
            #add new rule
            rule = Rule()
            rule.xml = m.to_xml()
            rule.type = m.get('add')
            rule.field = m.get('field')
            if not rule.field:
                rule.field = '*'
            if m.get('is'):
                rule.value = m.get('is')
            rule.plugin = m.get('via')
            before = int(m.get('before', len(Globals.rules)))
            if before<len(Globals.rules):
                Globals.rules.insert(before, rule)
            else:
                Globals.rules.append(rule)
            report_ok(m, conn, 'Rule added')
            return
        if m.get('del'):
            if m.get('del') == 'all':
                Globals.rules = []
                report_ok(m, conn, 'Rules cleared')
                return
            index = int(m.get('del', -1))
            if index >= 0 and index < len(Globals.rules):
                Globals.rules.remove(Globals.rules[index])
                report_ok(m, conn, 'Rule removed')
            else:
                report_error(m, conn, 'Invalid rule index')
            return
#Show rules
        resp = message.response_message(m, 'rules')
        arr = []
        for r in Globals.rules:
            rule = message.Message('rule')
            rule.set('type', r.type)
            rule.set('field', r.field)
            rule.set('value', r.value)
            rule.set('op', r.operation)
            rule.set('plugin', r.plugin)
            arr.append(rule)
        resp.set('rules', arr)
        conn.send_message(resp)
    except Exception, err:
#        Globals.master.rollback(cn)
        logging.exception('Error while manage_rules: %s', err)
        report_error(m, conn, 'Error while managing block rules')
Beispiel #4
0
def manage_groups(m, conn):
    '''
    Group management
    Supported operations: add; del
    '''
    cn, c = Globals.master.open_cursor()
    try:
        resp = message.response_message(m, 'groups')
        if m.get('add'):
            #Add new group, check name first
            group_name = m.get('add').strip()
            c.execute('select id from groups where lower(name)=?', (group_name.lower(), ))
            if c.fetchone():
                Globals.master.rollback(cn)
                logging.error('Group %s already exists', group_name)
                report_error(m, conn, 'Group already exists')
                return
            #Add group
            c.execute('insert into groups (name) values (?)', (group_name, ))
            resp.set('group_id', c.lastrowid)
        if m.get('del'):
            group_id = int(m.get('del'))
            c.execute('delete from groups_entries where group_id=?', (group_id, ))
            c.execute('delete from groups_users where group_id=?', (group_id, ))
            c.execute('delete from groups where id=?', (group_id, ))
        #List all groups here
        c.execute('select id, name from groups order by name')
        groups = []
        for r in c:
            group = message.Message('group')
            group.set('groupid', r[0])
            group.set('group', r[1])
            groups.append(group)
        resp.set('groups', groups)
        conn.send_message(resp)
        Globals.master.commit(cn)
    except Exception, err:
        Globals.master.rollback(cn)
        logging.exception('Error while adding metagroup %s', err)
        report_error(m, conn, 'Error adding new group')
Beispiel #5
0
def manage_users(m, conn):
    '''
    User management
    Supported operations: new; list(default); view; to(rename); del; add; rem
    '''
    cn, c = Globals.master.open_cursor()
    try:
        resp = message.response_message(m, 'users')
        if m.get('add'):
            #Add new user, check name first
            user_name = m.get('add').strip()
            c.execute('select id from users where lower(name)=?', (user_name.lower(), ))
            if c.fetchone():
                Globals.master.rollback(cn)
                logging.error('User %s already exists', user_name)
                report_error(m, conn, 'User already exists')
                return
            #Add user
            c.execute('insert into users (name) values (?)', (user_name, ))
            resp.set('user_id', c.lastrowid)
        if m.get('del'):
            user_id = int(m.get('del'))
            c.execute('delete from users_entries where user_id=?', (user_id, ))
            c.execute('delete from groups_users where user_id=?', (user_id, ))
            c.execute('delete from users where id=?', (user_id, ))
        #List all users here
        c.execute('select id, name from users order by name')
        users = []
        for r in c:
            user = message.Message('user')
            user.set('userid', r[0])
            user.set('user', r[1])
            users.append(user)
        resp.set('users', users)
        conn.send_message(resp)
        Globals.master.commit(cn)
    except Exception, err:
        Globals.master.rollback(cn)
        logging.error('Error while adding metauser %s', err)
        report_error(m, conn, 'Error adding new user')
Beispiel #6
0
def manage_profile(m, conn):
    """
    Profile management
    """
    cn, c = Globals.master.open_cursor()
    try:
        if m.get('set'):
            """
            Setting up profile, execute all entries
            """
            Globals.profile = m.get('set')
            c.execute('select command from profiles_entries where profile=? order by id', (Globals.profile, ))
            Globals.rules = []
            lc = LoopbackConnection()
            for r in c:
                command = r[0]
                mess = message.from_xml_string(command)
                process_message(mess, lc)
            report_ok(m, conn, 'Profile set')
            Globals.master.commit(cn)
            return
        if not Globals.profile:
            report_error(m, conn, 'Profile is empty, please activate one')
            Globals.master.rollback(cn)
            return
        if m.get('add') and m.get(m.get('add')):
            cname = m.get('add')
            command = m.get(cname)
            c.execute('insert into profiles_entries (profile, command_name, command) values (?, ?, ?)', (Globals.profile, cname, command.to_xml()))
            report_ok(m, conn, 'Profile entry added')
            Globals.master.commit(cn)
            return
        if m.get('del'):
            if m.get('del') == 'all':
                c.execute('delete from profiles_entries where profile=?', (Globals.profile, ))
            else:
                index = int(m.get('del'))
                c.execute('select id from profiles_entries where profile=? order by id', (Globals.profile, ))
                i = 0
                for r in c:
                    if index == i:
                        c.execute('delete from profiles_entries where id=?', (r[0], ))
                        break;
                    else:
                        i = i + 1
            report_ok(m, conn, 'Profile entry removed')
            Globals.master.commit(cn)
            return
        if m.get('use', '') == 'rules':
#Save all current rules for profile
            c.execute('delete from profiles_entries where profile=? and command_name=?', (Globals.profile, 'rule'))
            for r in Globals.rules:
                c.execute('insert into profiles_entries (profile, command_name, command) values (?, ?, ?)', (Globals.profile, 'rule', r.xml))
            report_ok(m, conn, 'Profile entries updated')
            Globals.master.commit(cn)
            return
#List all profile entries
        resp = message.response_message(m, 'profile')
        c.execute('select command from profiles_entries where profile=? order by id', (Globals.profile, ))
        arr = []
        for r in c:
            logging.debug('XML is: %s', r[0])
            mess = message.from_xml_string(r[0])
            if mess:
                arr.append(mess)
        resp.set('commands', arr)
        conn.send_message(resp)
        Globals.master.commit(cn)
    except Exception, err:
        Globals.master.rollback(cn)
        logging.exception('Error while manage_profile: %s', err)
        report_error(m, conn, 'Error while managing profile')
Beispiel #7
0
def manage_mark_read(m, conn):
    cn, c = Globals.master.open_cursor()
    try:
        user = m.get('user')
        group = m.get('group')
        if user:
            user = user.lower()
        if group:
            group = group.lower()
        count = 0
        messages = []
        c2 = Globals.master.add_cursor(cn)
        c3 = Globals.master.add_cursor(cn)
        if user:
            #Fetch all users
            c.execute('select user, instance_id from users_entries where user_id=?', (user, ))
            for row in c:
                mess = message.Message('mark_read')
                mess.set('user', row[0])
                rep = get_data_from_plugin(row[1], mess, c2)
                if rep:
                    messages.extend(rep.get('messages', []))
                    count = count + len(rep.get('messages', []))
        elif group:
            c2.execute('select user_id from groups_users where group_id=?', (group, ))
            for row2 in c2:
                c.execute('select user, instance_id from users_entries where user_id=?', (row2[0], ))
                for row in c:
                    mess = message.Message('mark_read')
                    mess.set('user', row[0])
                    rep = get_data_from_plugin(row[1], mess, c3)
                    if rep:
                        messages.extend(rep.get('messages', []))
                        count = count + len(rep.get('messages', []))
            c2.execute('select "user", "group", instance_id from groups_entries where group_id=?', (group, ))
            for row2 in c2:
                if row2[0]:
                    mess = message.Message('mark_read')
                    mess.set('user', row2[0])
                    rep = get_data_from_plugin(row2[2], mess, c3)
                    if rep:
                        messages.extend(rep.get('messages', []))
                        count = count + len(rep.get('messages', []))
                if row2[1]:#Group
                    mess = message.Message('mark_read')
                    mess.set('group', row2[1])
                    rep = get_data_from_plugin(row2[2], mess, c3)
                    if rep:
                        messages.extend(rep.get('messages', []))
                        count = count + len(rep.get('messages', []))
        else:
            report_error(m, conn, 'Please specify user or group')
            Globals.master.commit(cn)
            return

        resp = message.response_message(m, 'mark_read')
        resp.set('messages', messages)
        conn.send_message(resp)
        Globals.master.commit(cn)
    except Exception, err:
        Globals.master.rollback(cn)
        logging.exception('Error while mark_read: %s', err)
        report_error(m, conn, 'Error while marking messages as read')
Beispiel #8
0
def manage_unread_messages(m, conn):
    #Collect all unread users from all plugins
    cn, c = Globals.master.open_cursor()
    try:
        mess = message.Message('unread_messages')
        um = get_data_from_plugins(mess, c, 'messages')
        user = m.get('user')
        group = m.get('group')
        if user:
            user = user.lower()
        if group:
            group = group.lower()
        messages = []
        c2 = Globals.master.add_cursor(cn)
        if user:
            #Fetch all users
            c.execute('select user, instance_id from users_entries where user_id=?', (user, ))
            for row in c:
                if row[1] in um:
                    net, arr = um[row[1]]
                    for mess in arr:
                        if mess.get('userid') == row[0]:
                            messages.append(mess)
        elif group:
            c2.execute('select user_id from groups_users where group_id=?', (group, ))
            for row2 in c2:
                c.execute('select user, instance_id from users_entries where user_id=?', (row2[0], ))
                for row in c:
                    if row[1] in um:
                        net, arr = um[row[1]]
                        for mess in arr:
                            if mess.get('userid') == row[0]:
                                messages.append(mess)
            c2.execute('select "user", "group", instance_id from groups_entries where group_id=?', (group, ))
            for row2 in c2:
                if row2[0] and row2[2] in um:
                    net, arr = um[row2[2]]
                    for mess in arr:
                        if mess.get('userid') == row2[0]:
                            messages.append(mess)
                if row2[1]:
                    mess = message.Message('unread_messages')
                    mess.set('group', row2[1])
                    ugm = get_data_from_plugins(mess, c, 'messages')
                    for id in ugm:
                        net, arr = ugm[id]
                        for mess in arr:
                            messages.append(mess)
        else:
            report_error(m, conn, 'Please specify user or group')
            Globals.master.commit(cn)
            return

        resp = message.response_message(m, 'unread_messages')
        resp.set('messages', messages)
        conn.send_message(resp)
        Globals.master.commit(cn)
    except Exception, err:
        Globals.master.rollback(cn)
        logging.exception('Error while manage_unread_users: %s', err)
        report_error(m, conn, 'Error while listing unread users')
Beispiel #9
0
def manage_plugins(m, conn):
    #By default, send back list of loaded plugins
    resp = message.response_message(m, 'plugins')
    for name in Globals.plugins.keys():
        resp.set(name, Globals.plugins[name].get_description())
    conn.send_message(resp)
Beispiel #10
0
def manage_group(m, conn):
    '''
    User group management
    '''
    cn, c = Globals.master.open_cursor()
    try:
        resp = message.response_message(m, 'group')
        group_id = -1

        user = m.get('user')
        group = m.get('group')
        if user:
            user = user.lower()
        if group:
            group = group.lower()

        if m.get('show'):
            group_id = int(m.get('show'))
        if m.get('add'):
            net = m.get('net')
            group_id = int(m.get('add'))
            if not net:
                #Add meta-user
                c.execute('select id from users where id=?', (user, ))
                if not c.fetchone():
                    logging.error('Invalid user provided')
                    report_error(m, conn, 'Invalid user provided')
                    Globals.master.rollback(cn)
                    return
                c.execute('delete from groups_users where group_id=? and user_id=?', (group_id, user))
                c.execute('insert into groups_users (group_id, user_id) values (?, ?)', (group_id, user))
            else:
                c.execute('select id from instances where name=?', (net, ))
                r = c.fetchone()
                if r:
                    net = r[0]
                else:
                    net = None
                if not net or (not user and not group):
                    logging.error('No network or no user provided')
                    report_error(m, conn, 'No network or no user provided')
                    Globals.master.rollback(cn)
                    return

                c.execute('delete from groups_entries where group_id=? and instance_id=? and lower("user")=? and lower("group")=?', (group_id, net, user, group))
                c.execute('insert into groups_entries (group_id, instance_id, "user", "group") values (?, ?, ?, ?)', (group_id, net, user, group))

        if m.get('del'):
            group_id = int(m.get('del'))
            net = m.get('net')
            if not net:
                #Delete meta-user
                c.execute('select id from users where id=?', (user, ))
                if not c.fetchone():
                    logging.error('Invalid user provided')
                    report_error(m, conn, 'Invalid user provided')
                    Globals.master.rollback(cn)
                    return
                c.execute('delete from groups_users where group_id=? and user_id=?', (group_id, user))
            else:
                c.execute('select id from instances where name=?', (net, ))
                r = c.fetchone()
                if r:
                    net = r[0]
                else:
                    net = None
                if not net or (not user and not group):
                    logging.error('No network or no user provided')
                    report_error(m, conn, 'No network or no user provided')
                    Globals.master.rollback(cn)
                    return
                if user:
                    c.execute('delete from groups_entries where group_id=? and instance_id=? and lower("user")=?', (group_id, net, user))
                else:
                    c.execute('delete from groups_entries where group_id=? and instance_id=? and lower("group")=?', (group_id, net, group))

        c.execute('select i.name, "user", "group" from groups_entries ge, instances i where ge.instance_id=i.id and group_id=? order by i.name, "user", "group"', (group_id, ))
        ent = []
        for r in c:
            e = message.Message('entry')
            e.set('net', r[0])
            if r[1]:
                e.set('user', r[1])
            if r[2]:
                e.set('group', r[2])
            ent.append(e)
        c.execute('select u.name from users u, groups_users gu where gu.user_id=u.id and group_id=? order by name', (group_id, ))
        for r in c:
            e = message.Message('entry')
            e.set('user', r[0])
            ent.append(e)

        resp.set('entries', ent)
        conn.send_message(resp)
        Globals.master.commit(cn)
    except Exception, err:
        Globals.master.rollback(cn)
        logging.exception('Error while managing group details: %s', err)
        report_error(m, conn, 'Error managing group details')
Beispiel #11
0
def manage_user(m, conn):
    '''
    User details management
    '''
    cn, c = Globals.master.open_cursor()
    try:
        resp = message.response_message(m, 'user')
        user_id = -1
        if m.get('show'):
            user_id = int(m.get('show'))
        if m.get('add'):
            net = m.get('net')
            user_id = int(m.get('add'))
            user = m.get('user')
            c.execute('select id from instances where name=?', (net, ))
            r = c.fetchone()
            if r:
                net = r[0]
            else:
                net = None
            if not net or not user:
                logging.error('No network or no user provided')
                report_error(m, conn, 'No network or no user provided')
                Globals.master.rollback(cn)
                return

            c.execute('delete from users_entries where user_id=? and instance_id=? and lower(user)=?', (user_id, net, user.lower()))
            c.execute('insert into users_entries (user_id, instance_id, user) values (?, ?, ?)', (user_id, net, user.lower()))

        if m.get('del'):
            net = m.get('net')
            user_id = int(m.get('del'))
            user = m.get('user')
            c.execute('select id from instances where name=?', (net, ))
            r = c.fetchone()
            if r:
                net = r[0]
            else:
                net = None
            if not net or not user:
                logging.error('No network or no user provided')
                report_error(m, conn, 'No network or no user provided')
                Globals.master.rollback(cn)
                return

            c.execute('delete from users_entries where user_id=? and instance_id=? and lower(user)=?', (user_id, net, user.lower()))

        c.execute('select i.name, user from users_entries ue, instances i where ue.instance_id=i.id and user_id=? order by i.name, user', (user_id, ))
        ent = []
        for r in c:
            e = message.Message('entry')
            e.set('net', r[0])
            e.set('user', r[1])
            ent.append(e)
        resp.set('entries', ent)
        conn.send_message(resp)
        Globals.master.commit(cn)
    except Exception, err:
        Globals.master.rollback(cn)
        logging.error('Error while managing user details: %s', err)
        report_error(m, conn, 'Error managing user details')
Beispiel #12
0
def report_ok(m, conn, text = None):
    resp = message.response_message(m, 'ok')
    if text:
        resp.set('text', text)
    conn.send_message(resp)
Beispiel #13
0
def report_error(m, conn, text):
    resp = message.response_message(m, 'error')
    resp.set('text', text)
    conn.send_message(resp)
Beispiel #14
0
             opt_name = m.get('name').lower()
             opt_value = m.get('value')
             c.execute('delete from settings where name=? and instance_id=?', (opt_name, row[0]))
             pl = Globals.plugin_instances[plugin]
             if opt_value:
                 logging.debug('Adding option %i, %s, %s', row[0], opt_name, opt_value)
                 c.execute('insert into settings (instance_id, name, value) values (?, ?, ?)', (row[0], opt_name, opt_value))
                 pl.settings[opt_name] = opt_value
                 if not pl.disabled:
                     pl.setting_changed(opt_name, opt_value)
             else:
                 if opt_name in pl.settings:
                     del pl.settings[opt_name]
                 if not pl.disabled:
                     pl.setting_changed(opt_name, None)
         resp = message.response_message(m, 'options')
         c.execute('select name, value from settings where instance_id=? order by name', (row[0], ))
         for r in c:
             resp.set(r[0], r[1])
         Globals.master.commit(cn)
         conn.send_message(resp)
         return
     except Exception, err:
         Globals.master.rollback(cn)
         logging.error('Error processing entry settings: %s', err)
         report_error(m, conn, 'Error processing entry options')
     return
 resp = message.response_message(m, 'networks')
 arr = []
 for name in Globals.plugin_instances.keys():
     instance = Globals.plugin_instances[name]