/* * This file is part of EAS-4-TbSync. * * 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/. */ "use strict"; var tools = { setCalItemProperty: function (item, prop, value) { if (value == "unset") item.deleteProperty(prop); else item.setProperty(prop, value); }, getCalItemProperty: function (item, prop) { if (item.hasProperty(prop)) return item.getProperty(prop); else return "unset"; }, isString: function (s) { return (typeof s == 'string' || s instanceof String); }, getIdentityKey: function (email) { for (let account of MailServices.accounts.accounts) { if (account.defaultIdentity && account.defaultIdentity.email == email) return account.defaultIdentity.key; } return ""; }, parentIsTrash: function (folderData) { let parentID = folderData.getFolderProperty("parentID"); if (parentID == "0") return false; let parentFolder = folderData.accountData.getFolder("serverID", parentID); if (parentFolder && parentFolder.getFolderProperty("type") == "4") return true; return false; }, getNewDeviceId: function () { //taken from https://jsfiddle.net/briguy37/2MVFd/ let d = new Date().getTime(); let uuid = 'xxxxxxxxxxxxxxxxyxxxxxxxxxxx'.replace(/[xy]/g, function(c) { let r = (d + Math.random()*16)%16 | 0; d = Math.floor(d/16); return (c=='x' ? r : (r&0x3|0x8)).toString(16); }); return "MZTB" + uuid; }, getUriFromDirectoryId: function(ownerId) { let directories = MailServices.ab.directories; for (let directory of directories) { if (directory instanceof Components.interfaces.nsIAbDirectory) { if (ownerId.startsWith(directory.dirPrefId)) return directory.URI; } } return null; }, //function to get correct uri of current card for global book as well for mailLists getSelectedUri : function(aUri, aCard) { if (aUri == "moz-abdirectory://?") { //get parent via card owner return eas.tools.getUriFromDirectoryId(aCard.directoryId); } else if (MailServices.ab.getDirectory(aUri).isMailList) { //MailList suck, we have to cut the url to get the parent return aUri.substring(0, aUri.lastIndexOf("/")) } else { return aUri; } }, //read file from within the XPI package fetchFile: function (aURL, returnType = "Array") { return new Promise((resolve, reject) => { let uri = Services.io.newURI(aURL); let channel = Services.io.newChannelFromURI(uri, null, Services.scriptSecurityManager.getSystemPrincipal(), null, Components.interfaces.nsILoadInfo.SEC_REQUIRE_SAME_ORIGIN_INHERITS_SEC_CONTEXT, Components.interfaces.nsIContentPolicy.TYPE_OTHER); NetUtil.asyncFetch(channel, (inputStream, status) => { if (!Components.isSuccessCode(status)) { reject(status); return; } try { let data = NetUtil.readInputStreamToString(inputStream, inputStream.available()); if (returnType == "Array") { resolve(data.replace("\r","").split("\n")) } else { resolve(data); } } catch (ex) { reject(ex); } }); }); }, // TIMEZONE STUFF TimeZoneDataStructure : class { constructor() { this.buf = new DataView(new ArrayBuffer(172)); } /* Buffer structure: @000 utcOffset (4x8bit as 1xLONG) @004 standardName (64x8bit as 32xWCHAR) @068 standardDate (16x8 as 1xSYSTEMTIME) @084 standardBias (4x8bit as 1xLONG) @088 daylightName (64x8bit as 32xWCHAR) @152 daylightDate (16x8 as 1xSTRUCT) @168 daylightBias (4x8bit as 1xLONG) */ set easTimeZone64 (b64) { //clear buffer for (let i=0; i<172; i++) this.buf.setUint8(i, 0); //load content into buffer let content = (b64 == "") ? "" : atob(b64); for (let i=0; i if found, does the stdOffset match? -> if so, done 2. Try to parse our own format, split name and test each chunk for IANA -> if found, does the stdOffset match? -> if so, done 3. Try if one of the chunks matches international code -> if found, does the stdOffset match? -> if so, done 4. Fallback: Use just the offsets */ //check for windows timezone name if (eas.windowsToIanaTimezoneMap[stdName] && eas.cachedTimezoneData.iana[eas.windowsToIanaTimezoneMap[stdName]] && eas.cachedTimezoneData.iana[eas.windowsToIanaTimezoneMap[stdName]].std.offset == stdOffset ) { //the windows timezone maps multiple IANA zones to one (Berlin*, Rome, Bruessel) //check the windowsZoneName of the default TZ and of the winning, if they match, use default TZ //so Rome could win, even Berlin is the default IANA zone if (eas.defaultTimezoneInfo.std.windowsZoneName && eas.windowsToIanaTimezoneMap[stdName] != eas.defaultTimezoneInfo.std.id && eas.cachedTimezoneData.iana[eas.windowsToIanaTimezoneMap[stdName]].std.offset == eas.defaultTimezoneInfo.std.offset && stdName == eas.defaultTimezoneInfo.std.windowsZoneName) { TbSync.dump("Timezone matched via windows timezone name ("+stdName+") with default TZ overtake", eas.windowsToIanaTimezoneMap[stdName] + " -> " + eas.defaultTimezoneInfo.std.id); return eas.defaultTimezoneInfo.timezone; } TbSync.dump("Timezone matched via windows timezone name ("+stdName+")", eas.windowsToIanaTimezoneMap[stdName]); return eas.cachedTimezoneData.iana[eas.windowsToIanaTimezoneMap[stdName]].timezone; } let parts = stdName.replace(/[;,()\[\]]/g," ").split(" "); for (let i = 0; i < parts.length; i++) { //check for IANA if (eas.cachedTimezoneData.iana[parts[i]] && eas.cachedTimezoneData.iana[parts[i]].std.offset == stdOffset) { TbSync.dump("Timezone matched via IANA", parts[i]); return eas.cachedTimezoneData.iana[parts[i]].timezone; } //check for international abbreviation for standard period (CET, CAT, ...) if (eas.cachedTimezoneData.abbreviations[parts[i]] && eas.cachedTimezoneData.iana[eas.cachedTimezoneData.abbreviations[parts[i]]] && eas.cachedTimezoneData.iana[eas.cachedTimezoneData.abbreviations[parts[i]]].std.offset == stdOffset) { TbSync.dump("Timezone matched via international abbreviation (" + parts[i] +")", eas.cachedTimezoneData.abbreviations[parts[i]]); return eas.cachedTimezoneData.iana[eas.cachedTimezoneData.abbreviations[parts[i]]].timezone; } } //fallback to zone based on stdOffset and dstOffset, if we have that cached if (eas.cachedTimezoneData.bothOffsets[stdOffset+":"+dstOffset]) { TbSync.dump("Timezone matched via both offsets (std:" + stdOffset +", dst:" + dstOffset + ")", eas.cachedTimezoneData.bothOffsets[stdOffset+":"+dstOffset].tzid); return eas.cachedTimezoneData.bothOffsets[stdOffset+":"+dstOffset]; } //fallback to zone based on stdOffset only, if we have that cached if (eas.cachedTimezoneData.stdOffset[stdOffset]) { TbSync.dump("Timezone matched via std offset (" + stdOffset +")", eas.cachedTimezoneData.stdOffset[stdOffset].tzid); return eas.cachedTimezoneData.stdOffset[stdOffset]; } //return default timezone, if everything else fails TbSync.dump("Timezone could not be matched via offsets (std:" + stdOffset +", dst:" + dstOffset + "), using default timezone", eas.defaultTimezoneInfo.std.id); return eas.defaultTimezoneInfo.timezone; }, //extract standard and daylight timezone data getTimezoneInfo: function (timezone) { let tzInfo = {}; tzInfo.std = eas.tools.getTimezoneInfoObject(timezone, "standard"); tzInfo.dst = eas.tools.getTimezoneInfoObject(timezone, "daylight"); if (tzInfo.dst === null) tzInfo.dst = tzInfo.std; tzInfo.timezone = timezone; return tzInfo; }, //get timezone info for standard/daylight getTimezoneInfoObject: function (timezone, standardOrDaylight) { //handle UTC if (timezone.isUTC) { let obj = {} obj.id = "UTC"; obj.offset = 0; obj.abbreviation = "UTC"; obj.displayname = "Coordinated Universal Time (UTC)"; return obj; } //we could parse the icalstring by ourself, but I wanted to use ICAL.parse - TODO try catch let info = TbSync.lightning.ICAL.parse("BEGIN:VCALENDAR\r\n" + timezone.icalComponent.toString() + "\r\nEND:VCALENDAR"); let comp = new TbSync.lightning.ICAL.Component(info); let vtimezone =comp.getFirstSubcomponent("vtimezone"); let id = vtimezone.getFirstPropertyValue("tzid").toString(); let zone = vtimezone.getFirstSubcomponent(standardOrDaylight); if (zone) { let obj = {}; obj.id = id; //get offset let utcOffset = zone.getFirstPropertyValue("tzoffsetto").toString(); let o = parseInt(utcOffset.replace(":","")); //-330 = - 3h 30min let h = Math.floor(o / 100); //-3 -> -180min let m = o - (h*100) //-330 - -300 = -30 obj.offset = -1*((h*60) + m); //get international abbreviation (CEST, CET, CAT ... ) obj.abbreviation = ""; try { obj.abbreviation = zone.getFirstPropertyValue("tzname").toString(); } catch(e) { TbSync.dump("Failed TZ", timezone.icalComponent.toString()); } //get displayname obj.displayname = /*"("+utcOffset+") " +*/ obj.id;// + ", " + obj.abbreviation; //get DST switch date let rrule = zone.getFirstPropertyValue("rrule"); let dtstart = zone.getFirstPropertyValue("dtstart"); if (rrule && dtstart) { /* THE switchdate PART OF THE OBJECT IS MICROSOFT SPECIFIC, EVERYTHING ELSE IS THUNDERBIRD GENERIC, I LET IT SIT HERE ANYHOW https://msdn.microsoft.com/en-us/library/windows/desktop/ms725481(v=vs.85).aspx To select the correct day in the month, set the wYear member to zero, the wHour and wMinute members to the transition time, the wDayOfWeek member to the appropriate weekday, and the wDay member to indicate the occurrence of the day of the week within the month (1 to 5, where 5 indicates the final occurrence during the month if that day of the week does not occur 5 times). Using this notation, specify 02:00 on the first Sunday in April as follows: wHour = 2, wMonth = 4, wDayOfWeek = 0, wDay = 1. Specify 02:00 on the last Thursday in October as follows: wHour = 2, wMonth = 10, wDayOfWeek = 4, wDay = 5. So we have to parse the RRULE to exract wDay RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10 */ let parts =rrule.toString().split(";"); let rules = {}; for (let i = 0; i< parts.length; i++) { let sub = parts[i].split("="); if (sub.length == 2) rules[sub[0]] = sub[1]; } if (rules.FREQ == "YEARLY" && rules.BYDAY && rules.BYMONTH && rules.BYDAY.length > 2) { obj.switchdate = {}; obj.switchdate.month = parseInt(rules.BYMONTH); let days = ["SU","MO","TU","WE","TH","FR","SA"]; obj.switchdate.dayOfWeek = days.indexOf(rules.BYDAY.substring(rules.BYDAY.length-2)); obj.switchdate.weekOfMonth = parseInt(rules.BYDAY.substring(0, rules.BYDAY.length-2)); if (obj.switchdate.weekOfMonth<0 || obj.switchdate.weekOfMonth>5) obj.switchdate.weekOfMonth = 5; //get switch time from dtstart let dttime = eas.tools.createDateTime(dtstart.toString()); obj.switchdate.hour = dttime.hour; obj.switchdate.minute = dttime.minute; obj.switchdate.second = dttime.second; } } return obj; } return null; }, } //TODO: Invites /* cal.itip.checkAndSendOrigial = cal.itip.checkAndSend; cal.itip.checkAndSend = function(aOpType, aItem, aOriginalItem) { //if this item is added_by_user, do not call checkAndSend yet, because the UID is wrong, we need to sync first to get the correct ID - TODO TbSync.dump("cal.checkAndSend", aOpType); cal.itip.checkAndSendOrigial(aOpType, aItem, aOriginalItem); } */