# Path: xclib/ejabberdctl.py
# class ejabberdctl:
#     def __init__(self, ctx):
#         self.ctx = ctx
# 
#     def execute(self, args):
#         logging.debug(self.ctx.ejabberdctl_path + str(args))
#         try:
#             return unutf8(subprocess.check_output([self.ctx.ejabberdctl_path] + args))
#         except subprocess.CalledProcessError as err:
#             return None
#             logging.warn('ejabberdctl failed with %s'
#                 % str(err))
#             return None
# 
#     def members(self, group, domain):
#         membership = self.execute(['srg_get_members', group, domain])
#         if membership is None:
#             membership = ()
#         else:
#             membership = membership.split('\n')
#         # Delete empty values (e.g. from empty output)
#         mem = []
#         for m in membership:
#             if m != '':
#                 mem.append(m)
#         logging.debug('%s@%s members: %s' % (group, domain, mem))
#         return mem
# 
# Path: xclib/utf8.py
# def utf8(u, opts='strict'):
#     return u.encode('utf-8', opts)
# 
# def unutf8(u, opts='strict'):
#     if opts == 'illegal':
#         try:
#             return u.decode('utf-8', 'strict')
#         except UnicodeError:
#             dec = u.decode('utf-8', 'ignore')
#             logging.error('Illegal UTF-8 sequence: %r' % dec)
#             sys.stderr.write('Illegal UTF-8 sequence: %r\n' % dec)
#             traceback.print_exc()
#             return 'illegal-utf8-sequence-' + dec
#     else:
#         return u.decode('utf-8', opts)
# 
# def utf8l(l):
#     '''Encode a copy of the list, converted to UTF-8'''
#     return list(map(utf8, l))


import logging
import traceback
import unicodedata
import sys
import time
from xclib.ejabberdctl import ejabberdctl
from xclib.utf8 import utf8, unutf8, utf8l

def sanitize(name):
    name = str(name)
    printable = {'Lu', 'Ll', 'Lm', 'Lo', 'Nd', 'Nl', 'No', 'Pc', 'Pd', 'Ps', 'Pe', 'Pi', 'Pf', 'Po', 'Sm', 'Sc', 'Sk', 'So', 'Zs'}
    return ''.join(c for c in name if unicodedata.category(c) in printable and c != '@')

class roster_thread:
    def roster_background_thread(self, sr):
        '''Entry for background roster update thread'''
        start = time.time()
        ucommands = gcommands = ()
        try:
            logging.debug('roster_thread for ' + str(sr))
            # Allow test hooks with static ejabberd_controller
            if hasattr(self.ctx, 'ejabberd_controller') and self.ctx.ejabberd_controller is not None:
                e = self.ctx.ejabberd_controller
            else:

# Path: xclib/ejabberdctl.py
# class ejabberdctl:
#     def __init__(self, ctx):
#         self.ctx = ctx
# 
#     def execute(self, args):
#         logging.debug(self.ctx.ejabberdctl_path + str(args))
#         try:
#             return unutf8(subprocess.check_output([self.ctx.ejabberdctl_path] + args))
#         except subprocess.CalledProcessError as err:
#             return None
#             logging.warn('ejabberdctl failed with %s'
#                 % str(err))
#             return None
# 
#     def members(self, group, domain):
#         membership = self.execute(['srg_get_members', group, domain])
#         if membership is None:
#             membership = ()
#         else:
#             membership = membership.split('\n')
#         # Delete empty values (e.g. from empty output)
#         mem = []
#         for m in membership:
#             if m != '':
#                 mem.append(m)
#         logging.debug('%s@%s members: %s' % (group, domain, mem))
#         return mem
# 
# Path: xclib/utf8.py
# def utf8(u, opts='strict'):
#     return u.encode('utf-8', opts)
# 
# def unutf8(u, opts='strict'):
#     if opts == 'illegal':
#         try:
#             return u.decode('utf-8', 'strict')
#         except UnicodeError:
#             dec = u.decode('utf-8', 'ignore')
#             logging.error('Illegal UTF-8 sequence: %r' % dec)
#             sys.stderr.write('Illegal UTF-8 sequence: %r\n' % dec)
#             traceback.print_exc()
#             return 'illegal-utf8-sequence-' + dec
#     else:
#         return u.decode('utf-8', opts)
# 
# def utf8l(l):
#     '''Encode a copy of the list, converted to UTF-8'''
#     return list(map(utf8, l))


# Path: xclib/roster_thread.py
import logging
import traceback
import unicodedata
import sys
import time
from xclib.ejabberdctl import ejabberdctl
from xclib.utf8 import utf8, unutf8, utf8l


def sanitize(name):
    name = str(name)
    printable = {'Lu', 'Ll', 'Lm', 'Lo', 'Nd', 'Nl', 'No', 'Pc', 'Pd', 'Ps', 'Pe', 'Pi', 'Pf', 'Po', 'Sm', 'Sc', 'Sk', 'So', 'Zs'}
    return ''.join(c for c in name if unicodedata.category(c) in printable and c != '@')

class roster_thread:
    def roster_background_thread(self, sr):
        '''Entry for background roster update thread'''
        start = time.time()
        ucommands = gcommands = ()
        try:
            logging.debug('roster_thread for ' + str(sr))
            # Allow test hooks with static ejabberd_controller
            if hasattr(self.ctx, 'ejabberd_controller') and self.ctx.ejabberd_controller is not None:
                e = self.ctx.ejabberd_controller
            else:

                e = ejabberdctl(self.ctx)