diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /comm/mail/services/sync/modules | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'comm/mail/services/sync/modules')
4 files changed, 1515 insertions, 0 deletions
diff --git a/comm/mail/services/sync/modules/engines/accounts.sys.mjs b/comm/mail/services/sync/modules/engines/accounts.sys.mjs new file mode 100644 index 0000000000..d504da0fee --- /dev/null +++ b/comm/mail/services/sync/modules/engines/accounts.sys.mjs @@ -0,0 +1,392 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { CryptoWrapper } from "resource://services-sync/record.sys.mjs"; +import { + Store, + SyncEngine, + Tracker, +} from "resource://services-sync/engines.sys.mjs"; +import { Utils } from "resource://services-sync/util.sys.mjs"; + +const { SCORE_INCREMENT_XLARGE } = ChromeUtils.import( + "resource://services-sync/constants.js" +); +const { MailServices } = ChromeUtils.import( + "resource:///modules/MailServices.jsm" +); + +const SYNCED_SMTP_PROPERTIES = { + authMethod: "authMethod", + port: "port", + description: "name", + socketType: "socketType", +}; + +const SYNCED_SERVER_PROPERTIES = { + authMethod: "authMethod", + biffMinutes: "check_time", + doBiff: "check_new_mail", + downloadOnBiff: "download_on_biff", + emptyTrashOnExit: "empty_trash_on_exit", + incomingDuplicateAction: "dup_action", + limitOfflineMessageSize: "limit_offline_message_size", + loginAtStartUp: "login_at_startup", + maxMessageSize: "max_size", + port: "port", + prettyName: "name", + socketType: "socketType", +}; + +/** + * AccountRecord represents the state of an add-on in an application. + * + * Each add-on has its own record for each application ID it is installed + * on. + * + * The ID of add-on records is a randomly-generated GUID. It is random instead + * of deterministic so the URIs of the records cannot be guessed and so + * compromised server credentials won't result in disclosure of the specific + * add-ons present in a Sync account. + * + * The record contains the following fields: + * + */ +export function AccountRecord(collection, id) { + CryptoWrapper.call(this, collection, id); +} + +AccountRecord.prototype = { + __proto__: CryptoWrapper.prototype, + _logName: "Record.Account", +}; +Utils.deferGetSet(AccountRecord, "cleartext", [ + "username", + "hostname", + "type", + "prefs", + "isDefault", +]); + +export function AccountsEngine(service) { + SyncEngine.call(this, "Accounts", service); +} + +AccountsEngine.prototype = { + __proto__: SyncEngine.prototype, + _storeObj: AccountStore, + _trackerObj: AccountTracker, + _recordObj: AccountRecord, + version: 1, + syncPriority: 3, + + /* + * Returns a changeset for this sync. Engine implementations can override this + * method to bypass the tracker for certain or all changed items. + */ + async getChangedIDs() { + return this._tracker.getChangedIDs(); + }, +}; + +function AccountStore(name, engine) { + Store.call(this, name, engine); +} +AccountStore.prototype = { + __proto__: Store.prototype, + + /** + * Create an item in the store from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The store record to create an item from + */ + async create(record) { + if (record.type == "smtp") { + let smtpServer = MailServices.smtp.createServer(); + smtpServer.UID = record.id; + smtpServer.username = record.username; + smtpServer.hostname = record.hostname; + for (let key of Object.keys(SYNCED_SMTP_PROPERTIES)) { + if (key in record.prefs) { + smtpServer[key] = record.prefs[key]; + } + } + if (record.isDefault) { + MailServices.smtp.defaultServer = smtpServer; + } + return; + } + + try { + // Ensure there is a local mail account... + MailServices.accounts.localFoldersServer; + } catch { + // ... if not, make one. + MailServices.accounts.createLocalMailAccount(); + } + + let server = MailServices.accounts.createIncomingServer( + record.username, + record.hostname, + record.type + ); + server.UID = record.id; + + for (let key of Object.keys(SYNCED_SERVER_PROPERTIES)) { + if (key in record.prefs) { + server[key] = record.prefs[key]; + } + } + + let account = MailServices.accounts.createAccount(); + account.incomingServer = server; + + if (server.loginAtStartUp) { + Services.wm + .getMostRecentWindow("mail:3pane") + ?.GetNewMsgs(server, server.rootFolder); + } + }, + + /** + * Remove an item in the store from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The store record to delete an item from + */ + async remove(record) { + let smtpServer = MailServices.smtp.servers.find(s => s.UID == record.id); + if (smtpServer) { + MailServices.smtp.deleteServer(smtpServer); + return; + } + + let server = MailServices.accounts.allServers.find(s => s.UID == record.id); + if (!server) { + this._log.trace("Asked to remove record that doesn't exist, ignoring"); + return; + } + + let account = MailServices.accounts.FindAccountForServer(server); + if (account) { + MailServices.accounts.removeAccount(account, true); + } else { + // Is this even possible? + MailServices.accounts.removeIncomingServer(account, true); + } + }, + + /** + * Update an item from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The record to use to update an item from + */ + async update(record) { + if (record.type == "smtp") { + await this._updateSMTP(record); + return; + } + + await this._updateIncoming(record); + }, + + async _updateSMTP(record) { + let smtpServer = MailServices.smtp.servers.find(s => s.UID == record.id); + if (!smtpServer) { + this._log.trace("Skipping update for unknown item: " + record.id); + return; + } + smtpServer.username = record.username; + smtpServer.hostname = record.hostname; + for (let key of Object.keys(SYNCED_SMTP_PROPERTIES)) { + if (key in record.prefs) { + smtpServer[key] = record.prefs[key]; + } + } + if (record.isDefault) { + MailServices.smtp.defaultServer = smtpServer; + } + }, + + async _updateIncoming(record) { + let server = MailServices.accounts.allServers.find(s => s.UID == record.id); + if (!server) { + this._log.trace("Skipping update for unknown item: " + record.id); + return; + } + if (server.type != record.type) { + throw new Components.Exception( + `Refusing to change server type from ${server.type} to ${record.type}`, + Cr.NS_ERROR_FAILURE + ); + } + + for (let key of Object.keys(SYNCED_SERVER_PROPERTIES)) { + if (key in record.prefs) { + server[key] = record.prefs[key]; + } + } + }, + + /** + * Determine whether a record with the specified ID exists. + * + * Takes a string record ID and returns a booleans saying whether the record + * exists. + * + * @param id + * string record ID + * @return boolean indicating whether record exists locally + */ + async itemExists(id) { + return id in (await this.getAllIDs()); + }, + + /** + * Obtain the set of all known record IDs. + * + * @return Object with ID strings as keys and values of true. The values + * are ignored. + */ + async getAllIDs() { + let ids = {}; + for (let s of MailServices.smtp.servers) { + ids[s.UID] = true; + } + for (let s of MailServices.accounts.allServers) { + if (["imap", "pop3"].includes(s.type)) { + ids[s.UID] = true; + } + } + return ids; + }, + + /** + * Create a record from the specified ID. + * + * If the ID is known, the record should be populated with metadata from + * the store. If the ID is not known, the record should be created with the + * delete field set to true. + * + * @param id + * string record ID + * @param collection + * Collection to add record to. This is typically passed into the + * constructor for the newly-created record. + * @return record type for this engine + */ + async createRecord(id, collection) { + let record = new AccountRecord(collection, id); + + let server = MailServices.smtp.servers.find(s => s.UID == id); + if (server) { + record.type = "smtp"; + record.username = server.username; + record.hostname = server.hostname; + record.prefs = {}; + for (let key of Object.keys(SYNCED_SMTP_PROPERTIES)) { + record.prefs[key] = server[key]; + } + record.isDefault = MailServices.smtp.defaultServer == server; + return record; + } + + server = MailServices.accounts.allServers.find(s => s.UID == id); + // If we don't know about this ID, mark the record as deleted. + if (!server) { + record.deleted = true; + return record; + } + + record.type = server.type; + record.username = server.username; + record.hostname = server.hostName; + record.prefs = {}; + for (let key of Object.keys(SYNCED_SERVER_PROPERTIES)) { + record.prefs[key] = server[key]; + } + + return record; + }, +}; + +function AccountTracker(name, engine) { + Tracker.call(this, name, engine); +} +AccountTracker.prototype = { + __proto__: Tracker.prototype, + + _changedIDs: new Set(), + _ignoreAll: false, + + async getChangedIDs() { + let changes = {}; + for (let id of this._changedIDs) { + changes[id] = 0; + } + return changes; + }, + + clearChangedIDs() { + this._changedIDs.clear(); + }, + + get ignoreAll() { + return this._ignoreAll; + }, + + set ignoreAll(value) { + this._ignoreAll = value; + }, + + onStart() { + Services.prefs.addObserver("mail.server.", this); + Services.obs.addObserver(this, "message-server-removed"); + }, + + onStop() { + Services.prefs.removeObserver("mail.server.", this); + Services.obs.removeObserver(this, "message-server-removed"); + }, + + observe(subject, topic, data) { + if (this._ignoreAll) { + return; + } + + let server; + if (topic == "message-server-removed") { + server = subject.QueryInterface(Ci.nsIMsgIncomingServer); + } else { + let serverKey = data.split(".")[2]; + let prefName = data.substring(serverKey.length + 13); + if (!Object.values(SYNCED_SERVER_PROPERTIES).includes(prefName)) { + return; + } + + // Don't use getIncomingServer or it'll throw if the server doesn't exist. + server = MailServices.accounts.allServers.find(s => s.key == serverKey); + } + + if ( + server && + ["imap", "pop3"].includes(server.type) && + !this._changedIDs.has(server.UID) + ) { + this._changedIDs.add(server.UID); + this.score += SCORE_INCREMENT_XLARGE; + } + }, +}; diff --git a/comm/mail/services/sync/modules/engines/addressBooks.sys.mjs b/comm/mail/services/sync/modules/engines/addressBooks.sys.mjs new file mode 100644 index 0000000000..9df9f678df --- /dev/null +++ b/comm/mail/services/sync/modules/engines/addressBooks.sys.mjs @@ -0,0 +1,380 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { CryptoWrapper } from "resource://services-sync/record.sys.mjs"; +import { + Store, + SyncEngine, + Tracker, +} from "resource://services-sync/engines.sys.mjs"; +import { Utils } from "resource://services-sync/util.sys.mjs"; + +const { SCORE_INCREMENT_XLARGE } = ChromeUtils.import( + "resource://services-sync/constants.js" +); +const { MailServices } = ChromeUtils.import( + "resource:///modules/MailServices.jsm" +); + +const SYNCED_COMMON_PROPERTIES = { + autocomplete: "enable_autocomplete", + readOnly: "readOnly", +}; + +const SYNCED_CARDDAV_PROPERTIES = { + syncInterval: "carddav.syncinterval", + url: "carddav.url", + username: "carddav.username", +}; + +const SYNCED_LDAP_PROPERTIES = { + protocolVersion: "protocolVersion", + authSASLMechanism: "auth.saslmech", + authDN: "auth.dn", + uri: "uri", + maxHits: "maxHits", +}; + +/** + * AddressBookRecord represents the state of an add-on in an application. + * + * Each add-on has its own record for each application ID it is installed + * on. + * + * The ID of add-on records is a randomly-generated GUID. It is random instead + * of deterministic so the URIs of the records cannot be guessed and so + * compromised server credentials won't result in disclosure of the specific + * add-ons present in a Sync account. + * + * The record contains the following fields: + * + */ +export function AddressBookRecord(collection, id) { + CryptoWrapper.call(this, collection, id); +} + +AddressBookRecord.prototype = { + __proto__: CryptoWrapper.prototype, + _logName: "Record.AddressBook", +}; +Utils.deferGetSet(AddressBookRecord, "cleartext", ["name", "type", "prefs"]); + +export function AddressBooksEngine(service) { + SyncEngine.call(this, "AddressBooks", service); +} + +AddressBooksEngine.prototype = { + __proto__: SyncEngine.prototype, + _storeObj: AddressBookStore, + _trackerObj: AddressBookTracker, + _recordObj: AddressBookRecord, + version: 1, + syncPriority: 6, + + /* + * Returns a changeset for this sync. Engine implementations can override this + * method to bypass the tracker for certain or all changed items. + */ + async getChangedIDs() { + return this._tracker.getChangedIDs(); + }, +}; + +function AddressBookStore(name, engine) { + Store.call(this, name, engine); +} +AddressBookStore.prototype = { + __proto__: Store.prototype, + + _addPrefsToBook(book, record, whichPrefs) { + for (let [key, realKey] of Object.entries(whichPrefs)) { + let value = record.prefs[key]; + let type = typeof value; + if (type == "string") { + book.setStringValue(realKey, value); + } else if (type == "number") { + book.setIntValue(realKey, value); + } else if (type == "boolean") { + book.setBoolValue(realKey, value); + } + } + }, + + /** + * Create an item in the store from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The store record to create an item from + */ + async create(record) { + if ( + ![ + MailServices.ab.LDAP_DIRECTORY_TYPE, + MailServices.ab.CARDDAV_DIRECTORY_TYPE, + ].includes(record.type) + ) { + return; + } + + let dirPrefId = MailServices.ab.newAddressBook( + record.name, + null, + record.type, + record.id + ); + let book = MailServices.ab.getDirectoryFromId(dirPrefId); + + this._addPrefsToBook(book, record, SYNCED_COMMON_PROPERTIES); + if (record.type == MailServices.ab.CARDDAV_DIRECTORY_TYPE) { + this._addPrefsToBook(book, record, SYNCED_CARDDAV_PROPERTIES); + book.wrappedJSObject.fetchAllFromServer(); + } else if (record.type == MailServices.ab.LDAP_DIRECTORY_TYPE) { + this._addPrefsToBook(book, record, SYNCED_LDAP_PROPERTIES); + } + }, + + /** + * Remove an item in the store from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The store record to delete an item from + */ + async remove(record) { + let book = MailServices.ab.getDirectoryFromUID(record.id); + if (!book) { + this._log.trace("Asked to remove record that doesn't exist, ignoring"); + return; + } + + let deletedPromise = new Promise(resolve => { + Services.obs.addObserver( + { + observe() { + Services.obs.removeObserver(this, "addrbook-directory-deleted"); + resolve(); + }, + }, + "addrbook-directory-deleted" + ); + }); + MailServices.ab.deleteAddressBook(book.URI); + await deletedPromise; + }, + + /** + * Update an item from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The record to use to update an item from + */ + async update(record) { + let book = MailServices.ab.getDirectoryFromUID(record.id); + if (!book) { + this._log.trace("Skipping update for unknown item: " + record.id); + return; + } + if (book.dirType != record.type) { + throw new Components.Exception( + `Refusing to change book type from ${book.dirType} to ${record.type}`, + Cr.NS_ERROR_FAILURE + ); + } + + if (book.dirName != record.name) { + book.dirName = record.name; + } + this._addPrefsToBook(book, record, SYNCED_COMMON_PROPERTIES); + if (record.type == MailServices.ab.CARDDAV_DIRECTORY_TYPE) { + this._addPrefsToBook(book, record, SYNCED_CARDDAV_PROPERTIES); + } else if (record.type == MailServices.ab.LDAP_DIRECTORY_TYPE) { + this._addPrefsToBook(book, record, SYNCED_LDAP_PROPERTIES); + } + }, + + /** + * Determine whether a record with the specified ID exists. + * + * Takes a string record ID and returns a booleans saying whether the record + * exists. + * + * @param id + * string record ID + * @return boolean indicating whether record exists locally + */ + async itemExists(id) { + return id in (await this.getAllIDs()); + }, + + /** + * Obtain the set of all known record IDs. + * + * @return Object with ID strings as keys and values of true. The values + * are ignored. + */ + async getAllIDs() { + let ids = {}; + for (let b of MailServices.ab.directories) { + if ( + [ + MailServices.ab.LDAP_DIRECTORY_TYPE, + MailServices.ab.CARDDAV_DIRECTORY_TYPE, + ].includes(b.dirType) + ) { + ids[b.UID] = true; + } + } + return ids; + }, + + /** + * Create a record from the specified ID. + * + * If the ID is known, the record should be populated with metadata from + * the store. If the ID is not known, the record should be created with the + * delete field set to true. + * + * @param id + * string record ID + * @param collection + * Collection to add record to. This is typically passed into the + * constructor for the newly-created record. + * @return record type for this engine + */ + async createRecord(id, collection) { + let record = new AddressBookRecord(collection, id); + + let book = MailServices.ab.getDirectoryFromUID(id); + + // If we don't know about this ID, mark the record as deleted. + if (!book) { + record.deleted = true; + return record; + } + + record.name = book.dirName; + record.type = book.dirType; + record.prefs = {}; + + function collectPrefs(prefData) { + for (let [key, realKey] of Object.entries(prefData)) { + realKey = `${book.dirPrefId}.${realKey}`; + switch (Services.prefs.getPrefType(realKey)) { + case Services.prefs.PREF_STRING: + record.prefs[key] = Services.prefs.getStringPref(realKey); + break; + case Services.prefs.PREF_INT: + record.prefs[key] = Services.prefs.getIntPref(realKey); + break; + case Services.prefs.PREF_BOOL: + record.prefs[key] = Services.prefs.getBoolPref(realKey); + break; + } + } + } + + collectPrefs(SYNCED_COMMON_PROPERTIES); + + if (book.dirType == MailServices.ab.CARDDAV_DIRECTORY_TYPE) { + collectPrefs(SYNCED_CARDDAV_PROPERTIES); + } else if (book.dirType == MailServices.ab.LDAP_DIRECTORY_TYPE) { + collectPrefs(SYNCED_LDAP_PROPERTIES); + } + + return record; + }, +}; + +function AddressBookTracker(name, engine) { + Tracker.call(this, name, engine); +} +AddressBookTracker.prototype = { + __proto__: Tracker.prototype, + + _changedIDs: new Set(), + _ignoreAll: false, + + async getChangedIDs() { + let changes = {}; + for (let id of this._changedIDs) { + changes[id] = 0; + } + return changes; + }, + + clearChangedIDs() { + this._changedIDs.clear(); + }, + + get ignoreAll() { + return this._ignoreAll; + }, + + set ignoreAll(value) { + this._ignoreAll = value; + }, + + onStart() { + Services.prefs.addObserver("ldap_2.servers.", this); + Services.obs.addObserver(this, "addrbook-directory-created"); + Services.obs.addObserver(this, "addrbook-directory-deleted"); + }, + + onStop() { + Services.prefs.removeObserver("ldap_2.servers.", this); + Services.obs.removeObserver(this, "addrbook-directory-created"); + Services.obs.removeObserver(this, "addrbook-directory-deleted"); + }, + + observe(subject, topic, data) { + if (this._ignoreAll) { + return; + } + + let book; + switch (topic) { + case "nsPref:changed": { + let serverKey = data.split(".")[2]; + let prefName = data.substring(serverKey.length + 16); + if ( + prefName != "description" && + !Object.values(SYNCED_COMMON_PROPERTIES).includes(prefName) && + !Object.values(SYNCED_CARDDAV_PROPERTIES).includes(prefName) && + !Object.values(SYNCED_LDAP_PROPERTIES).includes(prefName) + ) { + return; + } + + book = MailServices.ab.getDirectoryFromId( + "ldap_2.servers." + serverKey + ); + break; + } + case "addrbook-directory-created": + case "addrbook-directory-deleted": + book = subject; + break; + } + + if ( + book && + [ + MailServices.ab.LDAP_DIRECTORY_TYPE, + MailServices.ab.CARDDAV_DIRECTORY_TYPE, + ].includes(book.dirType) && + !this._changedIDs.has(book.UID) + ) { + this._changedIDs.add(book.UID); + this.score += SCORE_INCREMENT_XLARGE; + } + }, +}; diff --git a/comm/mail/services/sync/modules/engines/calendars.sys.mjs b/comm/mail/services/sync/modules/engines/calendars.sys.mjs new file mode 100644 index 0000000000..72a3799786 --- /dev/null +++ b/comm/mail/services/sync/modules/engines/calendars.sys.mjs @@ -0,0 +1,349 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { CryptoWrapper } from "resource://services-sync/record.sys.mjs"; +import { + Store, + SyncEngine, + Tracker, +} from "resource://services-sync/engines.sys.mjs"; +import { Utils } from "resource://services-sync/util.sys.mjs"; + +const { SCORE_INCREMENT_XLARGE } = ChromeUtils.import( + "resource://services-sync/constants.js" +); +const { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm"); + +const SYNCED_PROPERTIES = { + cacheEnabled: "cache.enabled", + color: "color", + displayed: "calendar-main-in-composite", + disabled: "disabled", + forceEmailScheduling: "forceEmailScheduling", + // imipIdentityKey: "imip.identity.key", + readOnly: "readOnly", + refreshInterval: "refreshInterval", + sessionId: "sessionId", + suppressAlarms: "suppressAlarms", + username: "username", +}; + +function shouldSyncCalendar(calendar) { + if (calendar.type == "caldav") { + return true; + } + if (calendar.type == "ics") { + return calendar.uri.schemeIs("http") || calendar.uri.schemeIs("https"); + } + return false; +} + +/** + * CalendarRecord represents the state of an add-on in an application. + * + * Each add-on has its own record for each application ID it is installed + * on. + * + * The ID of add-on records is a randomly-generated GUID. It is random instead + * of deterministic so the URIs of the records cannot be guessed and so + * compromised server credentials won't result in disclosure of the specific + * add-ons present in a Sync account. + * + * The record contains the following fields: + * + */ +export function CalendarRecord(collection, id) { + CryptoWrapper.call(this, collection, id); +} + +CalendarRecord.prototype = { + __proto__: CryptoWrapper.prototype, + _logName: "Record.Calendar", +}; +Utils.deferGetSet(CalendarRecord, "cleartext", [ + "name", + "type", + "uri", + "prefs", +]); + +export function CalendarsEngine(service) { + SyncEngine.call(this, "Calendars", service); +} + +CalendarsEngine.prototype = { + __proto__: SyncEngine.prototype, + _storeObj: CalendarStore, + _trackerObj: CalendarTracker, + _recordObj: CalendarRecord, + version: 1, + syncPriority: 6, + + /* + * Returns a changeset for this sync. Engine implementations can override this + * method to bypass the tracker for certain or all changed items. + */ + async getChangedIDs() { + return this._tracker.getChangedIDs(); + }, +}; + +function CalendarStore(name, engine) { + Store.call(this, name, engine); +} +CalendarStore.prototype = { + __proto__: Store.prototype, + + /** + * Create an item in the store from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The store record to create an item from + */ + async create(record) { + if (!["caldav", "ics"].includes(record.type)) { + return; + } + + let calendar = cal.manager.createCalendar( + record.type, + Services.io.newURI(record.uri) + ); + calendar.name = record.name; + + for (let [key, realKey] of Object.entries(SYNCED_PROPERTIES)) { + if (key in record.prefs) { + calendar.setProperty(realKey, record.prefs[key]); + } + } + + // Set this *after* the properties so it can pick up the session ID or username. + calendar.id = record.id; + cal.manager.registerCalendar(calendar); + if (!calendar.getProperty("disabled")) { + calendar.refresh(); + } + }, + + /** + * Remove an item in the store from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The store record to delete an item from + */ + async remove(record) { + let calendar = cal.manager.getCalendarById(record.id); + if (!calendar) { + this._log.trace("Asked to remove record that doesn't exist, ignoring"); + return; + } + cal.manager.removeCalendar(calendar); + }, + + /** + * Update an item from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The record to use to update an item from + */ + async update(record) { + let calendar = cal.manager.getCalendarById(record.id); + if (!calendar) { + this._log.trace("Skipping update for unknown item: " + record.id); + return; + } + if (calendar.type != record.type) { + throw new Components.Exception( + `Refusing to change calendar type from ${calendar.type} to ${record.type}`, + Cr.NS_ERROR_FAILURE + ); + } + if (calendar.getProperty("cache.enabled") != record.prefs.cacheEnabled) { + throw new Components.Exception( + `Refusing to change the cache setting`, + Cr.NS_ERROR_FAILURE + ); + } + + calendar.name = record.name; + if (calendar.uri.spec != record.uri) { + calendar.uri = Services.io.newURI(record.uri); // Should this be allowed? + } + for (let [key, realKey] of Object.entries(SYNCED_PROPERTIES)) { + if (key in record.prefs) { + calendar.setProperty(realKey, record.prefs[key]); + } else if (calendar.getProperty(key)) { + // Only delete properties if they exist. Otherwise bad things happen. + calendar.deleteProperty(realKey); + } + } + }, + + /** + * Determine whether a record with the specified ID exists. + * + * Takes a string record ID and returns a booleans saying whether the record + * exists. + * + * @param id + * string record ID + * @return boolean indicating whether record exists locally + */ + async itemExists(id) { + return id in (await this.getAllIDs()); + }, + + /** + * Obtain the set of all known record IDs. + * + * @return Object with ID strings as keys and values of true. The values + * are ignored. + */ + async getAllIDs() { + let ids = {}; + for (let c of cal.manager.getCalendars()) { + if (shouldSyncCalendar(c)) { + ids[c.id] = true; + } + } + return ids; + }, + + /** + * Create a record from the specified ID. + * + * If the ID is known, the record should be populated with metadata from + * the store. If the ID is not known, the record should be created with the + * delete field set to true. + * + * @param id + * string record ID + * @param collection + * Collection to add record to. This is typically passed into the + * constructor for the newly-created record. + * @return record type for this engine + */ + async createRecord(id, collection) { + let record = new CalendarRecord(collection, id); + + let calendar = cal.manager.getCalendarById(id); + + // If we don't know about this ID, mark the record as deleted. + if (!calendar) { + record.deleted = true; + return record; + } + + record.name = calendar.name; + record.type = calendar.type; + record.uri = calendar.uri.spec; + record.prefs = {}; + + for (let [key, realKey] of Object.entries(SYNCED_PROPERTIES)) { + let value = calendar.getProperty(realKey); + if (value !== null) { + record.prefs[key] = value; + } + } + + return record; + }, +}; + +function CalendarTracker(name, engine) { + Tracker.call(this, name, engine); +} +CalendarTracker.prototype = { + __proto__: Tracker.prototype, + + QueryInterface: cal.generateQI([ + "calICalendarManagerObserver", + "nsIObserver", + ]), + + _changedIDs: new Set(), + _ignoreAll: false, + + async getChangedIDs() { + let changes = {}; + for (let id of this._changedIDs) { + changes[id] = 0; + } + return changes; + }, + + clearChangedIDs() { + this._changedIDs.clear(); + }, + + get ignoreAll() { + return this._ignoreAll; + }, + + set ignoreAll(value) { + this._ignoreAll = value; + }, + + onStart() { + Services.prefs.addObserver("calendar.registry.", this); + cal.manager.addObserver(this); + }, + + onStop() { + Services.prefs.removeObserver("calendar.registry.", this); + cal.manager.removeObserver(this); + }, + + observe(subject, topic, data) { + if (this._ignoreAll) { + return; + } + + let id = data.split(".")[2]; + let prefName = data.substring(id.length + 19); + if ( + prefName != "name" && + !Object.values(SYNCED_PROPERTIES).includes(prefName) + ) { + return; + } + + let calendar = cal.manager.getCalendarById(id); + if (calendar && shouldSyncCalendar(calendar) && !this._changedIDs.has(id)) { + this._changedIDs.add(id); + this.score += SCORE_INCREMENT_XLARGE; + } + }, + + onCalendarRegistered(calendar) { + if (this._ignoreAll) { + return; + } + + if (shouldSyncCalendar(calendar)) { + this._changedIDs.add(calendar.id); + this.score += SCORE_INCREMENT_XLARGE; + } + }, + onCalendarUnregistering(calendar) {}, + onCalendarDeleting(calendar) { + if (this._ignoreAll) { + return; + } + + if (shouldSyncCalendar(calendar)) { + this._changedIDs.add(calendar.id); + this.score += SCORE_INCREMENT_XLARGE; + } + }, +}; diff --git a/comm/mail/services/sync/modules/engines/identities.sys.mjs b/comm/mail/services/sync/modules/engines/identities.sys.mjs new file mode 100644 index 0000000000..07976272eb --- /dev/null +++ b/comm/mail/services/sync/modules/engines/identities.sys.mjs @@ -0,0 +1,394 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { CryptoWrapper } from "resource://services-sync/record.sys.mjs"; +import { + Store, + SyncEngine, + Tracker, +} from "resource://services-sync/engines.sys.mjs"; +import { Utils } from "resource://services-sync/util.sys.mjs"; + +const { SCORE_INCREMENT_XLARGE } = ChromeUtils.import( + "resource://services-sync/constants.js" +); +const { MailServices } = ChromeUtils.import( + "resource:///modules/MailServices.jsm" +); + +const SYNCED_IDENTITY_PROPERTIES = { + attachSignature: "attach_signature", + attachVCard: "attach_vcard", + autoQuote: "auto_quote", + catchAll: "catchAll", + catchAllHint: "catchAllHint", + composeHtml: "compose_html", + email: "useremail", + escapedVCard: "escapedVCard", + fullName: "fullName", + htmlSigFormat: "htmlSigFormat", + htmlSigText: "htmlSigText", + label: "label", + organization: "organization", + replyOnTop: "reply_on_top", + replyTo: "reply_to", + sigBottom: "sig_bottom", + sigOnForward: "sig_on_fwd", + sigOnReply: "sig_on_reply", +}; + +/** + * IdentityRecord represents the state of an add-on in an application. + * + * Each add-on has its own record for each application ID it is installed + * on. + * + * The ID of add-on records is a randomly-generated GUID. It is random instead + * of deterministic so the URIs of the records cannot be guessed and so + * compromised server credentials won't result in disclosure of the specific + * add-ons present in a Sync account. + * + * The record contains the following fields: + * + */ +export function IdentityRecord(collection, id) { + CryptoWrapper.call(this, collection, id); +} + +IdentityRecord.prototype = { + __proto__: CryptoWrapper.prototype, + _logName: "Record.Identity", +}; +Utils.deferGetSet(IdentityRecord, "cleartext", ["accounts", "prefs", "smtpID"]); + +export function IdentitiesEngine(service) { + SyncEngine.call(this, "Identities", service); +} + +IdentitiesEngine.prototype = { + __proto__: SyncEngine.prototype, + _storeObj: IdentityStore, + _trackerObj: IdentityTracker, + _recordObj: IdentityRecord, + version: 1, + syncPriority: 4, + + /* + * Returns a changeset for this sync. Engine implementations can override this + * method to bypass the tracker for certain or all changed items. + */ + async getChangedIDs() { + return this._tracker.getChangedIDs(); + }, +}; + +function IdentityStore(name, engine) { + Store.call(this, name, engine); +} +IdentityStore.prototype = { + __proto__: Store.prototype, + + /** + * Create an item in the store from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The store record to create an item from + */ + async create(record) { + let identity = MailServices.accounts.createIdentity(); + identity.UID = record.id; + + for (let key of Object.keys(SYNCED_IDENTITY_PROPERTIES)) { + if (key in record.prefs) { + identity[key] = record.prefs[key]; + } + } + + if (record.smtpID) { + let smtpServer = MailServices.smtp.servers.find( + s => s.UID == record.smtpID + ); + if (smtpServer) { + identity.smtpServerKey = smtpServer.key; + } else { + this._log.warn( + `Identity uses SMTP server ${record.smtpID}, but it doesn't exist.` + ); + } + } + + for (let { id, isDefault } of record.accounts) { + let account = MailServices.accounts.accounts.find( + a => a.incomingServer?.UID == id + ); + if (account) { + account.addIdentity(identity); + if (isDefault) { + account.defaultIdentity = identity; + } + } else { + this._log.warn(`Identity is for account ${id}, but it doesn't exist.`); + } + } + }, + + /** + * Remove an item in the store from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The store record to delete an item from + */ + async remove(record) { + let identity = MailServices.accounts.allIdentities.find( + i => i.UID == record.id + ); + if (!identity) { + this._log.trace("Asked to remove record that doesn't exist, ignoring"); + return; + } + + for (let server of MailServices.accounts.getServersForIdentity(identity)) { + let account = MailServices.accounts.FindAccountForServer(server); + account.removeIdentity(identity); + // Removing the identity from one account should destroy it. + // No need to continue. + return; + } + }, + + /** + * Update an item from a record. + * + * This is called by the default implementation of applyIncoming(). If using + * applyIncomingBatch(), this won't be called unless your store calls it. + * + * @param record + * The record to use to update an item from + */ + async update(record) { + let identity = MailServices.accounts.allIdentities.find( + i => i.UID == record.id + ); + if (!identity) { + this._log.trace("Skipping update for unknown item: " + record.id); + return; + } + + for (let key of Object.keys(SYNCED_IDENTITY_PROPERTIES)) { + if (key in record.prefs) { + identity[key] = record.prefs[key]; + } + } + + if (record.smtpID) { + let smtpServer = MailServices.smtp.servers.find( + s => s.UID == record.smtpID + ); + if (smtpServer) { + identity.smtpServerKey = smtpServer.key; + } else { + this._log.warn( + `Identity uses SMTP server ${record.smtpID}, but it doesn't exist.` + ); + } + } else { + identity.smtpServerKey = null; + } + + for (let { id, isDefault } of record.accounts) { + let account = MailServices.accounts.accounts.find( + a => a.incomingServer?.UID == id + ); + if (account) { + if (!account.identities.includes(identity)) { + account.addIdentity(identity); + } + if (isDefault && account.defaultIdentity != identity) { + account.defaultIdentity = identity; + } + } else { + this._log.warn(`Identity is for account ${id}, but it doesn't exist.`); + } + } + }, + + /** + * Determine whether a record with the specified ID exists. + * + * Takes a string record ID and returns a booleans saying whether the record + * exists. + * + * @param id + * string record ID + * @return boolean indicating whether record exists locally + */ + async itemExists(id) { + return id in (await this.getAllIDs()); + }, + + /** + * Obtain the set of all known record IDs. + * + * @return Object with ID strings as keys and values of true. The values + * are ignored. + */ + async getAllIDs() { + let ids = {}; + for (let i of MailServices.accounts.allIdentities) { + let servers = MailServices.accounts.getServersForIdentity(i); + if (servers.find(s => ["imap", "pop3"].includes(s.type))) { + ids[i.UID] = true; + } + } + return ids; + }, + + /** + * Create a record from the specified ID. + * + * If the ID is known, the record should be populated with metadata from + * the store. If the ID is not known, the record should be created with the + * delete field set to true. + * + * @param id + * string record ID + * @param collection + * Collection to add record to. This is typically passed into the + * constructor for the newly-created record. + * @return record type for this engine + */ + async createRecord(id, collection) { + let record = new IdentityRecord(collection, id); + + let identity = MailServices.accounts.allIdentities.find(i => i.UID == id); + + // If we don't know about this ID, mark the record as deleted. + if (!identity) { + record.deleted = true; + return record; + } + + record.accounts = []; + for (let server of MailServices.accounts.getServersForIdentity(identity)) { + let account = MailServices.accounts.FindAccountForServer(server); + if (account) { + record.accounts.push({ + id: server.UID, + isDefault: account.defaultIdentity == identity, + }); + } + } + + record.prefs = {}; + for (let key of Object.keys(SYNCED_IDENTITY_PROPERTIES)) { + record.prefs[key] = identity[key]; + } + + if (identity.smtpServerKey) { + let smtpServer = MailServices.smtp.getServerByIdentity(identity); + record.smtpID = smtpServer.UID; + } + + return record; + }, +}; + +function IdentityTracker(name, engine) { + Tracker.call(this, name, engine); +} +IdentityTracker.prototype = { + __proto__: Tracker.prototype, + + _changedIDs: new Set(), + _ignoreAll: false, + + async getChangedIDs() { + let changes = {}; + for (let id of this._changedIDs) { + changes[id] = 0; + } + return changes; + }, + + clearChangedIDs() { + this._changedIDs.clear(); + }, + + get ignoreAll() { + return this._ignoreAll; + }, + + set ignoreAll(value) { + this._ignoreAll = value; + }, + + onStart() { + Services.prefs.addObserver("mail.identity.", this); + Services.obs.addObserver(this, "account-identity-added"); + Services.obs.addObserver(this, "account-identity-removed"); + Services.obs.addObserver(this, "account-default-identity-changed"); + }, + + onStop() { + Services.prefs.removeObserver("mail.account.", this); + Services.obs.removeObserver(this, "account-identity-added"); + Services.obs.removeObserver(this, "account-identity-removed"); + Services.obs.removeObserver(this, "account-default-identity-changed"); + }, + + observe(subject, topic, data) { + if (this._ignoreAll) { + return; + } + + let markAsChanged = identity => { + if (identity && !this._changedIDs.has(identity.UID)) { + this._changedIDs.add(identity.UID); + this.score = SCORE_INCREMENT_XLARGE; + } + }; + + if ( + ["account-identity-added", "account-identity-removed"].includes(topic) + ) { + markAsChanged(subject.QueryInterface(Ci.nsIMsgIdentity)); + return; + } + + if (topic == "account-default-identity-changed") { + // The default identity has changed, update the default identity and + // the previous one, which will now be second on the list. + let [newDefault, oldDefault] = Services.prefs + .getStringPref(`mail.account.${data}.identities`) + .split(","); + if (newDefault) { + markAsChanged(MailServices.accounts.getIdentity(newDefault)); + } + if (oldDefault) { + markAsChanged(MailServices.accounts.getIdentity(oldDefault)); + } + return; + } + + let idKey = data.split(".")[2]; + let prefName = data.substring(idKey.length + 15); + if ( + prefName != "smtpServer" && + !Object.values(SYNCED_IDENTITY_PROPERTIES).includes(prefName) + ) { + return; + } + + // Don't use .getIdentity because it will create one if it doesn't exist. + markAsChanged( + MailServices.accounts.allIdentities.find(i => i.key == idKey) + ); + }, +}; |