def clear(self, uri): """ Clear password @param uri as str """ try: self.__wait_for_secret(self.clear, uri) parsed = urlparse(uri) SecretSchema = { "type": Secret.SchemaAttributeType.STRING, "formSubmitURL": Secret.SchemaAttributeType.STRING, } SecretAttributes = { "type": "eolie web login", "formSubmitURL": "%s://%s%s" % (parsed.scheme, parsed.netloc, parsed.path) } schema = Secret.Schema.new("org.gnome.Eolie", Secret.SchemaFlags.NONE, SecretSchema) self.__secret.search(schema, SecretAttributes, Secret.SearchFlags.ALL, None, self.__on_clear_search) except Exception as e: debug("PasswordsHelper::clear(): %s" % e)
def store_sync(self, login, password, uid, token, keyB, callback): """ Store Mozilla Sync password @param login as str @param password as str @param uid as str @param token as str @param keyB as str @param callback as function """ try: self.__wait_for_secret(self.store_sync, login, password, uid, token, keyB, callback) schema_string = "org.gnome.Eolie.sync" SecretSchema = { "sync": Secret.SchemaAttributeType.STRING, "login": Secret.SchemaAttributeType.STRING, "uid": Secret.SchemaAttributeType.STRING, "token": Secret.SchemaAttributeType.STRING, "keyB": Secret.SchemaAttributeType.STRING } schema = Secret.Schema.new("org.gnome.Eolie", Secret.SchemaFlags.NONE, SecretSchema) SecretAttributes = { "sync": "mozilla", "login": login, "uid": uid, "token": token, "keyB": keyB } Secret.password_store(schema, SecretAttributes, Secret.COLLECTION_DEFAULT, schema_string, password, None, callback) except Exception as e: debug("PasswordsHelper::store_sync(): %s" % e)
def __push_password(self, user_form_name, user_form_value, pass_form_name, pass_form_value, uri, form_uri, uuid): """ Push password @param user_form_name as str @param user_form_value as str @param pass_form_name as str @param pass_form_value as str @param uri as str @param uuid as str """ try: self.__check_worker() bulk_keys = self.__get_session_bulk_keys() record = {} record["id"] = "{%s}" % uuid record["hostname"] = uri record["formSubmitURL"] = form_uri record["httpRealm"] = None record["username"] = user_form_value record["password"] = pass_form_value record["usernameField"] = user_form_name record["passwordField"] = pass_form_name mtime = int(time()*1000) record["timeCreated"] = mtime record["timePasswordChanged"] = mtime debug("pushing %s" % record) self.__mozilla_sync.add(record, "passwords", bulk_keys) self.__update_state() except Exception as e: print("SyncWorker::__push_password():", e)
def get_all(self, callback, *args): """ Call function @param callback as function @param args """ try: self.__wait_for_secret(self.get_all, callback, *args) SecretSchema = { "type": Secret.SchemaAttributeType.STRING, } SecretAttributes = { "type": "eolie web login", } schema = Secret.Schema.new("org.gnome.Eolie", Secret.SchemaFlags.NONE, SecretSchema) self.__secret.search(schema, SecretAttributes, Secret.SearchFlags.ALL, None, self.__on_secret_search, None, callback, *args) except Exception as e: debug("PasswordsHelper::get_all(): %s" % e)
def get(self, uri, callback, *args): """ Call function @param uri as str @param callback as function @param args """ try: self.__wait_for_secret(self.get, uri, callback, *args) parsed = urlparse(uri) SecretSchema = { "type": Secret.SchemaAttributeType.STRING, "formSubmitURL": Secret.SchemaAttributeType.STRING } SecretAttributes = { "type": "eolie web login", "formSubmitURL": "%s://%s" % (parsed.scheme, parsed.netloc) } schema = Secret.Schema.new("org.gnome.Eolie", Secret.SchemaFlags.NONE, SecretSchema) self.__secret.search(schema, SecretAttributes, Secret.SearchFlags.ALL, None, self.__on_secret_search, uri, callback, *args) except Exception as e: debug("PasswordsHelper::get(): %s" % e)
def __on_secret_search(self, source, result, uri, callback, *args): """ Set username/password input @param source as GObject.Object @param result as Gio.AsyncResult @param uri as str/None @param callback as function @param args """ try: if result is not None: items = self.__secret.search_finish(result) count = len(items) index = 0 for item in items: item.load_secret(None, self.__on_load_secret, uri, index, count, callback, *args) index += 1 if not items: callback(None, None, uri, 0, 0, *args) else: callback(None, None, uri, 0, 0, *args) except Exception as e: debug("PasswordsHelper::__on_secret_search(): %s" % e) callback(None, None, uri, 0, 0, *args)
def __push_password(self, username, userform, password, passform, uri, uuid): """ Push password @param username as str @param userform as str @param password as str @param passform as str @param uri as str @param uuid as str """ if not self.__username or not self.__password: return try: bulk_keys = self.__get_session_bulk_keys() record = {} record["id"] = "{%s}" % uuid record["hostname"] = uri record["formSubmitURL"] = uri record["httpRealm"] = None record["username"] = username record["password"] = password record["usernameField"] = userform record["passwordField"] = passform mtime = int(time()*1000) record["timeCreated"] = mtime record["timePasswordChanged"] = mtime debug("pushing %s" % record) self.__mozilla_sync.add(record, "passwords", bulk_keys) except Exception as e: print("SyncWorker::__push_password():", e)
def __push_history(self, history_ids): """ Push history ids if atime is available, else, ask to remove @param history ids as [int] """ if not self.__username or not self.__password: return try: bulk_keys = self.__get_session_bulk_keys() for history_id in history_ids: sleep(0.01) record = {} atimes = El().history.get_atimes(history_id) guid = El().history.get_guid(history_id) if atimes: record["histUri"] = El().history.get_uri(history_id) record["id"] = guid record["title"] = El().history.get_title(history_id) record["visits"] = [] for atime in atimes: record["visits"].append({"date": atime*1000000, "type": 1}) debug("pushing %s" % record) self.__mozilla_sync.add(record, "history", bulk_keys) else: record["id"] = guid record["type"] = "item" record["deleted"] = True debug("deleting %s" % record) self.__mozilla_sync.add(record, "history", bulk_keys) self.__mtimes = self.__mozilla_sync.client.info_collections() dump(self.__mtimes, open(EOLIE_LOCAL_PATH + "/mozilla_sync.bin", "wb")) except Exception as e: print("SyncWorker::__push_history():", e)
def get_sync(self, callback, *args): """ Get sync password @param callback as function """ try: self.__wait_for_secret(self.get_sync, callback, *args) SecretSchema = { "sync": Secret.SchemaAttributeType.STRING } SecretAttributes = { "sync": "mozilla" } schema = Secret.Schema.new("org.gnome.Eolie", Secret.SchemaFlags.NONE, SecretSchema) self.__secret.search(schema, SecretAttributes, Secret.SearchFlags.NONE, None, self.__on_secret_search, None, callback, *args) except Exception as e: debug("PasswordsHelper::get_sync(): %s" % e)
def clear(self, uuid, callback=None, *args): """ Clear password @param uuid as str @param callback as function """ try: self.__wait_for_secret(self.clear, uuid) SecretSchema = { "type": Secret.SchemaAttributeType.STRING, "uuid": Secret.SchemaAttributeType.STRING } SecretAttributes = { "type": "eolie web login", "uuid": uuid } schema = Secret.Schema.new("org.gnome.Eolie", Secret.SchemaFlags.NONE, SecretSchema) self.__secret.search(schema, SecretAttributes, Secret.SearchFlags.ALL, None, self.__on_clear_search, callback, *args) except Exception as e: debug("PasswordsHelper::clear(): %s" % e)
def __pull_passwords(self, bulk_keys): """ Pull from passwords @param bulk_keys as KeyBundle @raise StopIteration """ debug("pull passwords") records = self.__mozilla_sync.get_records("passwords", bulk_keys) for record in records: self.__check_worker() if record["modified"] < self.__mtimes["passwords"]: continue sleep(0.01) debug("pulling %s" % record) password = record["payload"] password_id = password["id"].strip("{}") if "formSubmitURL" in password.keys(): self.__helper.clear(password_id, self.__helper.store, password["usernameField"], password["username"], password["passwordField"], password["password"], password["hostname"], password["formSubmitURL"], password_id, None) elif "deleted" in password.keys(): # We assume True self.__helper.clear(password_id)
def store(self, user_form_name, user_form_value, pass_form_name, pass_form_value, uri, form_uri, uuid, callback, *args): """ Store password @param user_form_name as str @param user_form_value as str @param pass_form_name as str @param pass_form_value as str @param uri as str @param form_uri as str @param uuid as str @param callback as function """ try: self.__wait_for_secret(self.store, user_form_name, user_form_value, pass_form_name, pass_form_value, uri, form_uri, uuid, callback, *args) parsed = urlparse(uri) parsed_form_uri = urlparse(form_uri) schema_string = "org.gnome.Eolie: %s@%s" % (user_form_value, parsed.netloc) SecretSchema = { "type": Secret.SchemaAttributeType.STRING, "uuid": Secret.SchemaAttributeType.STRING, "login": Secret.SchemaAttributeType.STRING, "hostname": Secret.SchemaAttributeType.STRING, "formSubmitURL": Secret.SchemaAttributeType.STRING, "userform": Secret.SchemaAttributeType.STRING, "passform": Secret.SchemaAttributeType.STRING, } SecretAttributes = { "type": "eolie web login", "uuid": uuid, "login": user_form_value, "hostname": "%s://%s" % (parsed.scheme, parsed.netloc), "formSubmitURL": "%s://%s" % (parsed_form_uri.scheme, parsed_form_uri.netloc), "userform": user_form_name, "passform": pass_form_name } schema = Secret.Schema.new("org.gnome.Eolie", Secret.SchemaFlags.NONE, SecretSchema) Secret.password_store(schema, SecretAttributes, Secret.COLLECTION_DEFAULT, schema_string, pass_form_value, None, callback) except Exception as e: debug("PasswordsHelper::store(): %s" % e)
def __on_get_secret(self, source, result): """ Store secret proxy @param source as GObject.Object @param result as Gio.AsyncResult """ try: self.__secret = Secret.Service.get_finish(result) except Exception as e: self.__secret = -1 debug("PasswordsHelper::__on_get_secret(): %s" % e)
def __save_css_default_rule(self, line): """ Save default (without blacklist, whitelist) rule to db @param line as str """ name = line[2:] # Update entry if exists, create else with SqlCursor(self) as sql: debug("Add filter: %s" % name) sql.execute( "INSERT INTO adblock_css\ (name, mtime) VALUES (?, ?)", (name, self.__adblock_mtime))
def __on_clear_search(self, source, result): """ Clear passwords @param source as GObject.Object @param result as Gio.AsyncResult """ try: if result is not None: items = source.search_finish(result) for item in items: item.delete(None, None) except Exception as e: debug("SettingsDialog::__on_clear_search(): %s" % e)
def clear_sync(self): """ Clear sync secrets """ try: SecretSchema = {"sync": Secret.SchemaAttributeType.STRING} SecretAttributes = {"sync": "mozilla"} schema = Secret.Schema.new("org.gnome.Eolie", Secret.SchemaFlags.NONE, SecretSchema) self.__secret.search(schema, SecretAttributes, Secret.SearchFlags.ALL, None, self.__on_clear_search) except Exception as e: debug("PasswordsHelper::clear_sync(): %s" % e)
def __on_clear_search(self, source, result, callback=None, *args): """ Clear passwords @param source as GObject.Object @param result as Gio.AsyncResult """ try: if result is not None: items = source.search_finish(result) for item in items: item.delete(None, None) if callback is not None: callback(*args) except Exception as e: debug("PasswordsHelper::__on_clear_search(): %s" % e)
def clear_all(self): """ Clear passwords """ try: self.__wait_for_secret(self.clear_all) SecretSchema = {"type": Secret.SchemaAttributeType.STRING} SecretAttributes = {"type": "eolie web login"} schema = Secret.Schema.new("org.gnome.Eolie", Secret.SchemaFlags.NONE, SecretSchema) self.__secret.search(schema, SecretAttributes, Secret.SearchFlags.ALL, None, self.__on_clear_search) except Exception as e: debug("PasswordsHelper::clear_all(): %s" % e)
def __update_state(self): """ Update state file """ try: # If syncing, state will be written by self.__sync() if not self.syncing: f = open(EOLIE_DATA_PATH + "/mozilla_sync.bin", "wb") # Lock file flock(f, LOCK_EX | LOCK_NB) self.__mtimes = self.__mozilla_sync.client.info_collections() dump(self.__mtimes, f) # Unlock file flock(f, LOCK_UN) except Exception as e: debug("SyncWorker::__update_state(): %s" % e)
def __remove_from_passwords(self, uuid): """ Remove password from passwords collection @param uuid as str """ try: self.__check_worker() bulk_keys = self.__get_session_bulk_keys() record = {} record["id"] = uuid record["deleted"] = True debug("deleting %s" % record) self.__mozilla_sync.add(record, "passwords", bulk_keys) self.__update_state() except Exception as e: debug("SyncWorker::__remove_from_passwords(): %s" % e)
def __remove_from_history(self, guid): """ Remove from history @param guid as str """ if not self.__username or not self.__password: return try: bulk_keys = self.__get_session_bulk_keys() record = {} record["id"] = guid record["type"] = "item" record["deleted"] = True debug("deleting %s" % record) self.__mozilla_sync.add(record, "history", bulk_keys) except Exception as e: print("SyncWorker::__remove_from_history():", e)
def update_zoom_level(self): """ Update zoom level """ try: zoom_level = El().websettings.get_zoom(self.get_uri()) if zoom_level is None: zoom_level = 100 if self.__related_view is None: zoom_level *= self.get_ancestor(Gtk.Window).zoom_level else: zoom_level *= self.__related_view.get_ancestor( Gtk.Window).zoom_level except Exception as e: print("WebView::update_zoom_level()", e) debug("Update zoom level: %s" % zoom_level) self.set_zoom_level(zoom_level / 100)
def __remove_from_bookmarks(self, guid): """ Remove from history @param guid as str """ try: self.__check_worker() bulk_keys = self.__get_session_bulk_keys() record = {} record["id"] = guid record["type"] = "bookmark" record["deleted"] = True debug("deleting %s" % record) self.__mozilla_sync.add(record, "bookmark", bulk_keys) self.__update_state() except Exception as e: debug("SyncWorker::__remove_from_bookmarks(): %s" % e)
def update_zoom_level(self): """ Update zoom level """ try: zoom_level = El().websettings.get_zoom(self.uri) if zoom_level is None: zoom_level = 100 if self.__related_view is None: zoom_level *= self._window.zoom_level else: window = self.__related_view.get_ancestor(Gtk.Window) if window is not None and hasattr(window, "zoom-level"): zoom_level *= window.zoom_level except Exception as e: print("WebView::update_zoom_level()", e) debug("Update zoom level: %s" % zoom_level) self.set_zoom_level(zoom_level / 100)
def update_zoom_level(self): """ Update zoom level """ from eolie.database_settings import DatabaseSettings try: settings_db = DatabaseSettings() zoom_level = settings_db.get_zoom(self.get_uri()) if zoom_level is None: zoom_level = 100 if self.__related_view is None: zoom_level *= self.get_ancestor(Gtk.Window).zoom_level else: zoom_level *= self.__related_view.get_ancestor( Gtk.Window).zoom_level except Exception as e: print("WebView::update_zoom_level()", e) debug("Update zoom level: %s" % zoom_level) self.set_zoom_level(zoom_level / 100)
def __pull_history(self, bulk_keys): """ Pull from history @param bulk_keys as KeyBundle @raise StopIteration """ debug("pull history") records = self.__mozilla_sync.get_records("history", bulk_keys) for record in records: if self.__stop: raise StopIteration("Cancelled") if record["modified"] < self.__mtimes["history"]: continue sleep(0.01) El().history.thread_lock.acquire() history = record["payload"] keys = history.keys() history_id = El().history.get_id_by_guid(history["id"]) # Check we have a valid history item if "histUri" in keys and\ "title" in keys and\ history["title"] and\ El().history.get_mtime(history_id) < record["modified"]: # Try to get visit date atimes = [] try: for visit in history["visits"]: atimes.append(round(int(visit["date"]) / 1000000, 2)) except: El().history.thread_lock.release() continue debug("pulling %s" % record) title = history["title"].rstrip().lstrip() history_id = El().history.add(title, history["histUri"], record["modified"], history["id"], atimes, True) elif "deleted" in keys: history_id = El().history.get_id_by_guid(history_id) El().history.remove(history_id) El().history.thread_lock.release()
def update_zoom_level(self): """ Update zoom level """ try: parsed = urlparse(self.get_uri()) if parsed.netloc in El().zoom_levels.keys(): zoom_level = El().zoom_levels[parsed.netloc] else: zoom_level = 100 if self.__related_view is None: zoom_level *= self.get_ancestor(Gtk.Window).zoom_level else: zoom_level *= self.__related_view.get_ancestor( Gtk.Window).zoom_level except Exception as e: print("WebView::update_zoom_level()", e) debug("Update zoom level: %s" % zoom_level) self.set_zoom_level(zoom_level / 100)
def __remove_from_passwords(self, attributes, password, uri): """ Remove password from passwords collection @param attributes as {} @param password as str @param uri as str """ if not self.__username or not self.__password: return try: bulk_keys = self.__get_session_bulk_keys() record = {} record["id"] = attributes["uuid"] record["deleted"] = True debug("deleting %s" % record) self.__mozilla_sync.add(record, "passwords", bulk_keys) self.__helper.clear(uri) except Exception as e: print("SyncWorker::__remove_from_passwords():", e)
def __pull_history(self, bulk_keys): """ Pull from history @param bulk_keys as KeyBundle @raise StopIteration """ debug("pull history") records = self.__mozilla_sync.get_records("history", bulk_keys) for record in records: if self.__stop: raise StopIteration("Cancelled") sleep(0.01) El().history.thread_lock.acquire() history = record["payload"] keys = history.keys() # Ignore pages without a title if "title" not in keys or not history["title"]: El().history.thread_lock.release() continue # Ignore pages without an uri (deleted) if "histUri" not in keys: El().history.thread_lock.release() continue history_id = El().history.get_id_by_guid(history["id"]) # Nothing to apply, continue if El().history.get_mtime(history_id) >= record["modified"]: El().history.thread_lock.release() continue # Try to get visit date atimes = [] try: for visit in history["visits"]: atimes.append(round(int(visit["date"]) / 1000000, 2)) except: El().history.thread_lock.release() continue debug("pulling %s" % record) title = history["title"].rstrip().lstrip() history_id = El().history.add(title, history["histUri"], record["modified"], history["id"], atimes, True) El().history.thread_lock.release()
def __save_css_domain_rule(self, line): """ Save domain rule to db @param line as str """ whitelist = "" blacklist = "" (domains, name) = line.split("##") for domain in domains.split(","): if domain.startswith("~"): blacklist += "@%s@" % domain[1:] else: whitelist += domain with SqlCursor(self) as sql: debug("Add filter: %s" % name) sql.execute( "INSERT INTO adblock_css\ (name, whitelist, blacklist, mtime)\ VALUES (?, ?, ?, ?)", (name, whitelist, blacklist, self.__adblock_mtime))