Ejemplo n.º 1
0
    def update(self, app):
        any = 0

        if not self.pipe:
            self.pipe = self.pipe_getter()
            any = 1

        if self.pipe.finished() and self.still_searching:
            self.still_searching = 0
            return 1

        list = self.pipe.read_all()

        prev_pair = None
        for pair in list:
            if pair == prev_pair:
                continue
            link, item = prev_pair = pair

            # this is bad, really
            try:
                item = utility.check_and_demangle_item(item)
            except:
                self.empty_text = item
                continue

            keywords = item.get('keywords',[ ])

            if type(keywords) != types.ListType:
                continue

            # Comparing a string to a unicode can throw an error,
            # so force everything to unicode
            for i in range(len(keywords)):
                keywords[i] = utility.force_unicode(keywords[i])

            bad = 0                
            for key in self.keywords:
                if key not in keywords:
                    bad = 1
            for key in self.anti_keywords:
                if key in keywords:
                    bad = 1
            if item.get('type')=='file':
                if self.mime:
                    mime = item.get('mime')
                    if not mime:
                        bad=1
                    else:
                        if mime.find(self.mime) == -1:
                            bad = 1

            if item.get('type') != 'identity' and self.online_only:
                bad = 1                
                            
            if item.get('type') == 'file' and self.remote_only :
                if app.node.data.has_key(item.get('name')+app.node.salt):
                    bad=1
            
            if bad:
                continue

            any = 1
            if item.get('type') == 'directory':
                any=1
                def pipe_getter(path=item['path'],app=app,link=link):
                    pipe = app.node.search_browse_task(link,path)
                    return pipe
                
                new_interior = Search_tree_directory(
                    pipe_getter,self.keywords,self.anti_keywords,\
                    self.what,self.mime,item['filename'],\
                    self.empty_text,self.single_text,0)
                new_interior.item=item
                
                if self.nickname:
                    new_interior.nickname=self.nickname
                    d=self.nickname+':'
                    if item['path']:
                        d=d+utility.force_unicode(string.join(item['path'],'/'))+'/'
                    visited_directories.append(d)

                self.children.append( new_interior )
            else:
                for child in self.children:
                    if child.matches(item,link):
                        child.add_source(item,link)
                        break
                else:
                    try:
                        if search_item_table.has_key(item['type']):
                            self.children.append( search_item_table[item['type']](item,link,self) )
                    except:                     
                        pass
                    
        return any
Ejemplo n.º 2
0
def find_task(daemon, query, input, output, connection):

    import safe_pickle

    for char in "+-_.,?()![]":
        query = query.replace(char, " ")
    query = query.lower()
    list = query.split()
    if list:
        key = list[0]
        pipe = daemon.node.retrieve(hash.hash_of("identity-name " + key), settings.identity_redundancy)
    else:
        pipe = daemon.node.retrieve(hash.hash_of("service identity"), settings.identity_redundancy)

    results = []
    while not pipe.finished():

        list = pipe.read_all()
        prev_pair = None
        for pair in list:
            if pair == prev_pair:
                continue
            link, item = prev_pair = pair

            try:
                item = utility.check_and_demangle_item(item)
            except:
                continue

            if item["key"] not in results:
                results.append(item["key"])
                name = hash.hash_of(safe_pickle.dumps(item["key"]))
                check.check_is_name(name)
                str = hash.hash_to_person(name)
                output.write(str + "   " + item["name"] + " (" + utility.force_string(item["human-name"]) + ")\n")

        time.sleep(0.5)
        try:
            output.flush()
        except:
            return

    if not results:
        try:
            output.write('No user matching "' + key + '"')
        except:
            pass
    else:
        if results.__len__() == 1:
            msg = "1 user found."
        else:
            msg = "%d users found." % results.__len__()
        output.write(msg)

    pipe.stop()
    try:
        input.close()
        output.close()
        connection.close()
    except:
        # connection reset by peer...
        pass