diff options
Diffstat (limited to 'searx/preferences.py')
-rw-r--r-- | searx/preferences.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/searx/preferences.py b/searx/preferences.py index 30a4252b0..6befdd6e1 100644 --- a/searx/preferences.py +++ b/searx/preferences.py @@ -104,6 +104,31 @@ class MultipleChoiceSetting(EnumStringSetting): resp.set_cookie(name, ','.join(self.value), max_age=COOKIE_MAX_AGE) +class SetSetting(Setting): + def _post_init(self): + if not hasattr(self, 'values'): + self.values = set() + + def get_value(self): + return ','.join(self.values) + + def parse(self, data): + if data == '': + self.values = set() + return + + elements = data.split(',') + for element in elements: + self.values.add(element) + + def parse_form(self, data): + elements = data.split(',') + self.values = set(elements) + + def save(self, name, resp): + resp.set_cookie(name, ','.join(self.values), max_age=COOKIE_MAX_AGE) + + class SearchLanguageSetting(EnumStringSetting): """Available choices may change, so user's value may not be in choices anymore""" @@ -272,6 +297,7 @@ class Preferences(object): self.engines = EnginesSetting('engines', choices=engines) self.plugins = PluginsSetting('plugins', choices=plugins) + self.tokens = SetSetting('tokens') self.unknown_params = {} def get_as_url_params(self): @@ -288,6 +314,8 @@ class Preferences(object): settings_kv['disabled_plugins'] = ','.join(self.plugins.disabled) settings_kv['enabled_plugins'] = ','.join(self.plugins.enabled) + settings_kv['tokens'] = ','.join(self.tokens.values) + return urlsafe_b64encode(compress(urlencode(settings_kv).encode('utf-8'))).decode('utf-8') def parse_encoded_data(self, input_data): @@ -307,6 +335,8 @@ class Preferences(object): elif user_setting_name == 'disabled_plugins': self.plugins.parse_cookie((input_data.get('disabled_plugins', ''), input_data.get('enabled_plugins', ''))) + elif user_setting_name == 'tokens': + self.tokens.parse(user_setting) elif not any(user_setting_name.startswith(x) for x in [ 'enabled_', 'disabled_', @@ -328,6 +358,8 @@ class Preferences(object): enabled_categories.append(user_setting_name[len('category_'):]) elif user_setting_name.startswith('plugin_'): disabled_plugins.append(user_setting_name) + elif user_setting_name == 'tokens': + self.tokens.parse_form(user_setting) else: self.unknown_params[user_setting_name] = user_setting self.key_value_settings['categories'].parse_form(enabled_categories) @@ -346,6 +378,18 @@ class Preferences(object): user_setting.save(user_setting_name, resp) self.engines.save(resp) self.plugins.save(resp) + self.tokens.save('tokens', resp) for k, v in self.unknown_params.items(): resp.set_cookie(k, v, max_age=COOKIE_MAX_AGE) return resp + + def validate_token(self, engine): + valid = True + if hasattr(engine, 'tokens') and engine.tokens: + valid = False + for token in self.tokens.values: + if token in engine.tokens: + valid = True + break + + return valid |