'Search functionality'

def func(self):

    caller = self.caller
    switches = self.switches
    if (not self.args):
        caller.msg('Usage:    @find    <string>    [=    low    [-high]]')
        return
    searchstring = self.lhs
    (low, high) = (1, ObjectDB.objects.all().order_by('-id')[0].id)
    if self.rhs:
        if ('-' in self.rhs):
            limlist = [part.lstrip('#').strip() for part in self.rhs.split('-', 1)]
        else:
            limlist = [part.lstrip('#') for part in self.rhs.split(None, 1)]
        if (limlist and limlist[0].isdigit()):
            low = max(low, int(limlist[0]))
        if ((len(limlist) > 1) and limlist[1].isdigit()):
            high = min(high, int(limlist[1]))
    low = min(low, high)
    high = max(low, high)
    is_dbref = utils.dbref(searchstring)
    is_player = searchstring.startswith('*')
    restrictions = ''
    if self.switches:
        restrictions = (',    %s' % ','.join(self.switches))
    if (is_dbref or is_player):
        if is_dbref:
            result = caller.search(searchstring, global_search=True, quiet=True)
            string = ('|wExact    dbref    match|n(#%i-#%i%s):' % (low, high, restrictions))
        else:
            searchstring = searchstring.lstrip('*')
            result = caller.search_player(searchstring, quiet=True)
            string = ('|wMatch|n(#%i-#%i%s):' % (low, high, restrictions))
        if ('room' in switches):
            result = (result if inherits_from(result, ROOM_TYPECLASS) else None)
        if ('exit' in switches):
            result = (result if inherits_from(result, EXIT_TYPECLASS) else None)
        if ('char' in switches):
            result = (result if inherits_from(result, CHAR_TYPECLASS) else None)
        if (not result):
            string += '\n            |RNo    match    found.|n'
        elif (not (low <= int(result[0].id) <= high)):
            string += ("\n            |RNo    match    found    for    '%s'    in    #dbref    interval.|n" % searchstring)
        else:
            result = result[0]
            string += ('\n|g            %s    -    %s|n' % (result.get_display_name(caller), result.path))
    else:
        if ('exact' in switches):
            keyquery = Q(db_key__iexact=searchstring, id__gte=low, id__lte=high)
            aliasquery = Q(db_tags__db_key__iexact=searchstring, db_tags__db_tagtype__iexact='alias', id__gte=low, id__lte=high)
        else:
            keyquery = Q(db_key__istartswith=searchstring, id__gte=low, id__lte=high)
            aliasquery = Q(db_tags__db_key__istartswith=searchstring, db_tags__db_tagtype__iexact='alias', id__gte=low, id__lte=high)
        results = ObjectDB.objects.filter((keyquery | aliasquery)).distinct()
        nresults = results.count()
        if nresults:
            results = [result for result in results]
            if ('room' in switches):
                results = [obj for obj in results if inherits_from(obj, ROOM_TYPECLASS)]
            if ('exit' in switches):
                results = [obj for obj in results if inherits_from(obj, EXIT_TYPECLASS)]
            if ('char' in switches):
                results = [obj for obj in results if inherits_from(obj, CHAR_TYPECLASS)]
            nresults = len(results)
        if nresults:
            if (nresults > 1):
                string = ('|w%i    Matches|n(#%i-#%i%s):' % (nresults, low, high, restrictions))
                for res in results:
                    string += ('\n            |g%s    -    %s|n' % (res.get_display_name(caller), res.path))
            else:
                string = ('|wOne    Match|n(#%i-#%i%s):' % (low, high, restrictions))
                string += ('\n            |g%s    -    %s|n' % (results[0].get_display_name(caller), results[0].path))
        else:
            string = ('|wMatch|n(#%i-#%i%s):' % (low, high, restrictions))
            string += ("\n            |RNo    matches    found    for    '%s'|n" % searchstring)
    caller.msg(string.strip())
