def get_dir_stream(folder): """ Pack all files and metadata in directory """ message = slskmessages.FileSearchResult() stream = bytearray() stream.extend(message.pack_object(len(folder))) for fileinfo in folder: stream.extend(message.pack_file_info(fileinfo)) return stream
def processSearchRequest(self, searchterm, user, searchid, direct=0): if not self.config.sections["searches"]["search_results"]: # Don't return _any_ results when this option is disabled return if searchterm is None: return if user == self.config.sections["server"]["login"]: # We shouldn't send a search response if we initiated the search request return maxresults = self.config.sections["searches"]["maxresults"] if maxresults == 0: return checkuser, reason = self.np.CheckUser(user, None) if not checkuser: return if checkuser == 2: wordindex = self.config.sections["transfers"]["bwordindex"] else: wordindex = self.config.sections["transfers"]["wordindex"] terms = searchterm.translate(self.translatepunctuation).lower().split() length = 0 for i in terms: if i in wordindex: length += 1 if length == 0 or length != len(terms): return try: list = [wordindex[i] for i in terms if i in wordindex] except ValueError: # Shelf is probably closed, perhaps when rescanning share return shortest = min(list, key=len) list.remove(shortest) for i in shortest[:]: for j in list: if i not in j: shortest.remove(i) break results = shortest[:maxresults] if len(results) > 0 and self.np.transfers is not None: queuesizes = self.np.transfers.getUploadQueueSizes() slotsavail = self.np.transfers.allowNewUploads() if reason == "geoip": geoip = 1 else: geoip = 0 if checkuser == 2: fileindex = self.config.sections["transfers"]["bfileindex"] else: fileindex = self.config.sections["transfers"]["fileindex"] fifoqueue = self.config.sections["transfers"]["fifoqueue"] message = slskmessages.FileSearchResult( None, self.config.sections["server"]["login"], geoip, searchid, results, fileindex, slotsavail, self.np.speed, queuesizes, fifoqueue) self.np.ProcessRequestToPeer(user, message) if direct: self.logMessage( _("User %(user)s is directly searching for %(query)s, returning %(num)i results" ) % { 'user': user, 'query': searchterm, 'num': len(results) }, 2) else: self.logMessage( _("User %(user)s is searching for %(query)s, returning %(num)i results" ) % { 'user': user, 'query': searchterm, 'num': len(results) }, 2)
def processSearchRequest(self, searchterm, user, searchid, direct=0): if not self.config.sections["searches"]["search_results"]: # Don't return _any_ results when this option is disabled return if searchterm is None: return checkuser, reason = self.np.CheckUser(user, None) if not checkuser: return if reason == "geoip": geoip = 1 else: geoip = 0 maxresults = self.config.sections["searches"]["maxresults"] if checkuser == 2: wordindex = self.config.sections["transfers"]["bwordindex"] fileindex = self.config.sections["transfers"]["bfileindex"] else: wordindex = self.config.sections["transfers"]["wordindex"] fileindex = self.config.sections["transfers"]["fileindex"] fifoqueue = self.config.sections["transfers"]["fifoqueue"] if maxresults == 0: return terms = searchterm.translate(self.translatepunctuation).lower().split() list = [wordindex[i][:] for i in terms if i in wordindex] if len(list) != len(terms) or len(list) == 0: return min = list[0] for i in list[1:]: if len(i) < len(min): min = i list.remove(min) for i in min[:]: for j in list: if i not in j: min.remove(i) break results = min[:maxresults] if len(results) > 0 and self.np.transfers is not None: queuesizes = self.np.transfers.getUploadQueueSizes() slotsavail = self.np.transfers.allowNewUploads() if len(results) > 0: message = slskmessages.FileSearchResult( None, self.config.sections["server"]["login"], geoip, searchid, results, fileindex, slotsavail, self.np.speed, queuesizes, fifoqueue) self.np.ProcessRequestToPeer(user, message) if direct: self.logMessage( _("User %(user)s is directly searching for %(query)s, returning %(num)i results" ) % { 'user': user, 'query': searchterm, 'num': len(results) }, 2) else: self.logMessage( _("User %(user)s is searching for %(query)s, returning %(num)i results" ) % { 'user': user, 'query': searchterm, 'num': len(results) }, 2)
def process_search_request(self, searchterm, user, token, direct=False): """ Note: since this section is accessed every time a search request arrives several times per second, please keep it as optimized and memory sparse as possible! """ if not searchterm: return if not self.config.sections["searches"]["search_results"]: # Don't return _any_ results when this option is disabled return if not direct and user == self.core.login_username: # We shouldn't send a search response if we initiated the search request, # unless we're specifically searching our own username return maxresults = self.config.sections["searches"]["maxresults"] if maxresults == 0: return # Remember excluded/partial words for later excluded_words = [] partial_words = [] if '-' in searchterm or '*' in searchterm: for word in searchterm.split(): if len(word) < 1: continue if word.startswith('-'): for subword in word.translate( self.translatepunctuation).split(): excluded_words.append(subword) elif word.startswith('*'): for subword in word.translate( self.translatepunctuation).split(): partial_words.append(subword) # Strip punctuation searchterm_old = searchterm searchterm = searchterm.lower().translate( self.translatepunctuation).strip() if len(searchterm ) < self.config.sections["searches"]["min_search_chars"]: # Don't send search response if search term contains too few characters return checkuser, _reason = self.core.network_filter.check_user(user, None) if not checkuser: return if checkuser == 2: wordindex = self.share_dbs.get("buddywordindex") else: wordindex = self.share_dbs.get("wordindex") if wordindex is None: return # Find common file matches for each word in search term resultlist = self.create_search_result_list(searchterm, wordindex, excluded_words, partial_words) if not resultlist: return if checkuser == 2: fileindex = self.share_dbs.get("buddyfileindex") else: fileindex = self.share_dbs.get("fileindex") if fileindex is None: return fileinfos = [] numresults = min(len(resultlist), maxresults) for index in islice(resultlist, numresults): fileinfo = fileindex.get(repr(index)) if fileinfo is not None: fileinfos.append(fileinfo) if numresults != len(fileinfos): log.add_debug( ("Error: File index inconsistency while responding to search request \"%(query)s\". " "Expected %(expected_num)i results, but found %(total_num)i results in database." ), { "query": searchterm_old, "expected_num": numresults, "total_num": len(fileinfos) }) numresults = len(fileinfos) if not numresults: return uploadspeed = self.core.transfers.upload_speed queuesize = self.core.transfers.get_upload_queue_size() slotsavail = self.core.transfers.allow_new_uploads() fifoqueue = self.config.sections["transfers"]["fifoqueue"] message = slskmessages.FileSearchResult(None, self.core.login_username, token, fileinfos, slotsavail, uploadspeed, queuesize, fifoqueue) self.core.send_message_to_peer(user, message) log.add_search( _("User %(user)s is searching for \"%(query)s\", found %(num)i results" ), { 'user': user, 'query': searchterm_old, 'num': numresults })
def processSearchRequest(self, searchterm, user, searchid, direct=0): """ Note: since this section is accessed every time a search request arrives, several times a second, please keep it as optimized and memory sparse as possible! """ if not self.config.sections["searches"]["search_results"]: # Don't return _any_ results when this option is disabled return if searchterm is None: return if user == self.config.sections["server"]["login"]: # We shouldn't send a search response if we initiated the search request return maxresults = self.config.sections["searches"]["maxresults"] if maxresults == 0: return # Don't count excluded words as matches (words starting with -) # Strip punctuation searchterm = re.sub(r'(\s)-\w+', r'\1', searchterm).lower().translate( self.translatepunctuation).strip() if len(searchterm ) < self.config.sections["searches"]["min_search_chars"]: # Don't send search response if search term contains too few characters return checkuser, reason = self.np.CheckUser(user, None) if not checkuser: return if checkuser == 2: wordindex = self.config.sections["transfers"]["bwordindex"] else: wordindex = self.config.sections["transfers"]["wordindex"] # Find common file matches for each word in search term resultlist = self.create_search_result_list(searchterm, wordindex, maxresults) if not resultlist: return if self.np.transfers is not None: numresults = min(len(resultlist), maxresults) queuesizes = self.np.transfers.getUploadQueueSizes() slotsavail = self.np.transfers.allowNewUploads() if reason == "geoip": geoip = 1 else: geoip = 0 if checkuser == 2: fileindex = self.config.sections["transfers"]["bfileindex"] else: fileindex = self.config.sections["transfers"]["fileindex"] fifoqueue = self.config.sections["transfers"]["fifoqueue"] message = slskmessages.FileSearchResult( None, self.config.sections["server"]["login"], geoip, searchid, resultlist, fileindex, slotsavail, self.np.speed, queuesizes, fifoqueue, numresults) self.np.ProcessRequestToPeer(user, message) if direct: self.logMessage( _("User %(user)s is directly searching for \"%(query)s\", returning %(num)i results" ) % { 'user': user, 'query': searchterm, 'num': numresults }, 2) else: self.logMessage( _("User %(user)s is searching for \"%(query)s\", returning %(num)i results" ) % { 'user': user, 'query': searchterm, 'num': numresults }, 2)
def process_search_request(self, searchterm, user, searchid, direct=False): """ Note: since this section is accessed every time a search request arrives, several times a second, please keep it as optimized and memory sparse as possible! """ if not self.config.sections["searches"]["search_results"]: # Don't return _any_ results when this option is disabled return if searchterm is None: return if not direct and user == self.config.sections["server"]["login"]: # We shouldn't send a search response if we initiated the search request, # unless we're specifically searching our own username return maxresults = self.config.sections["searches"]["maxresults"] if maxresults == 0: return # Don't count excluded words as matches (words starting with -) # Strip punctuation searchterm = re.sub(r'(\s)-\w+', r'\1', searchterm).lower().translate( self.translatepunctuation).strip() if len(searchterm ) < self.config.sections["searches"]["min_search_chars"]: # Don't send search response if search term contains too few characters return checkuser, reason = self.np.check_user(user, None) if not checkuser: return if checkuser == 2: wordindex = self.share_dbs["buddywordindex"] else: wordindex = self.share_dbs["wordindex"] # Find common file matches for each word in search term resultlist = self.create_search_result_list(searchterm, wordindex, maxresults) if not resultlist: return if self.np.transfers is not None: numresults = min(len(resultlist), maxresults) queuesizes = self.np.transfers.get_upload_queue_sizes() slotsavail = self.np.transfers.allow_new_uploads() if checkuser == 2: fileindex = self.share_dbs["buddyfileindex"] else: fileindex = self.share_dbs["fileindex"] fifoqueue = self.config.sections["transfers"]["fifoqueue"] message = slskmessages.FileSearchResult( None, self.config.sections["server"]["login"], searchid, resultlist, fileindex, slotsavail, self.np.speed, queuesizes, fifoqueue, numresults) self.np.send_message_to_peer(user, message) if direct: log.add_search( _("User %(user)s is directly searching for \"%(query)s\", returning %(num)i results" ), { 'user': user, 'query': searchterm, 'num': numresults }) else: log.add_search( _("User %(user)s is searching for \"%(query)s\", returning %(num)i results" ), { 'user': user, 'query': searchterm, 'num': numresults })