Signal-iOS/SignalUI/ViewControllers/RegistrationValues.swift
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

164 lines
5.3 KiB
Swift

//
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import SignalCoreKit
import SignalServiceKit
@objc
public class RegistrationCountryState: NSObject {
// e.g. France
public let countryName: String
// e.g. +33
public let callingCode: String
// e.g. FR
public let countryCode: String
@objc
public init(countryName: String,
callingCode: String,
countryCode: String) {
self.countryName = countryName
self.callingCode = callingCode
self.countryCode = countryCode
}
public static var defaultValue: RegistrationCountryState {
AssertIsOnMainThread()
var countryCode: String = PhoneNumber.defaultCountryCode()
if let lastRegisteredCountryCode = RegistrationValues.lastRegisteredCountryCode(),
lastRegisteredCountryCode.count > 0 {
countryCode = lastRegisteredCountryCode
}
let callingCodeNumber: NSNumber = phoneNumberUtil.getCountryCode(forRegion: countryCode)
let callingCode = "\(COUNTRY_CODE_PREFIX)\(callingCodeNumber)"
let countryName = PhoneNumberUtil.countryName(fromCountryCode: countryCode)
return RegistrationCountryState(countryName: countryName, callingCode: callingCode, countryCode: countryCode)
}
// MARK: -
public static func countryState(forE164 e164: String) -> RegistrationCountryState? {
for countryState in allCountryStates {
if e164.hasPrefix(countryState.callingCode) {
return countryState
}
}
return nil
}
public static var allCountryStates: [RegistrationCountryState] {
RegistrationCountryState.buildCountryStates(searchText: nil)
}
public static func buildCountryStates(searchText: String?) -> [RegistrationCountryState] {
let searchText = searchText?.strippedOrNil
let countryCodes: [String] = PhoneNumberUtil.countryCodes(forSearchTerm: searchText)
return RegistrationCountryState.buildCountryStates(countryCodes: countryCodes)
}
public static func buildCountryStates(countryCodes: [String]) -> [RegistrationCountryState] {
return countryCodes.compactMap { (countryCode: String) -> RegistrationCountryState? in
guard let countryCode = countryCode.strippedOrNil else {
owsFailDebug("Invalid countryCode.")
return nil
}
guard let callingCode = PhoneNumberUtil.callingCode(fromCountryCode: countryCode)?.strippedOrNil else {
owsFailDebug("Invalid callingCode.")
return nil
}
guard let countryName = PhoneNumberUtil.countryName(fromCountryCode: countryCode).strippedOrNil else {
owsFailDebug("Invalid countryName.")
return nil
}
guard callingCode != "+0" else {
owsFailDebug("Invalid callingCode.")
return nil
}
return RegistrationCountryState(countryName: countryName,
callingCode: callingCode,
countryCode: countryCode)
}
}
}
// MARK: -
@objc
public class RegistrationPhoneNumber: NSObject {
public let e164: String
public let userInput: String
@objc
public init(e164: String, userInput: String) {
self.e164 = e164
self.userInput = userInput
}
public var asPhoneNumber: PhoneNumber? {
PhoneNumber(fromE164: e164)
}
}
// MARK: -
@objc
public class RegistrationValues: NSObject {
private static let kKeychainService_LastRegistered = "kKeychainService_LastRegistered"
private static let kKeychainKey_LastRegisteredCountryCode = "kKeychainKey_LastRegisteredCountryCode"
private static let kKeychainKey_LastRegisteredPhoneNumber = "kKeychainKey_LastRegisteredPhoneNumber"
private class func debugValue(forKey key: String) -> String? {
AssertIsOnMainThread()
guard OWSIsDebugBuild() else {
return nil
}
do {
let value = try CurrentAppContext().keychainStorage().string(forService: kKeychainService_LastRegistered, key: key)
return value
} catch {
// The value may not be present in the keychain.
return nil
}
}
private class func setDebugValue(_ value: String, forKey key: String) {
AssertIsOnMainThread()
guard OWSIsDebugBuild() else {
return
}
do {
try CurrentAppContext().keychainStorage().set(string: value, service: kKeychainService_LastRegistered, key: key)
} catch {
owsFailDebug("Error: \(error)")
}
}
public class func lastRegisteredCountryCode() -> String? {
return debugValue(forKey: kKeychainKey_LastRegisteredCountryCode)
}
public class func setLastRegisteredCountryCode(value: String) {
setDebugValue(value, forKey: kKeychainKey_LastRegisteredCountryCode)
}
public class func lastRegisteredPhoneNumber() -> String? {
return debugValue(forKey: kKeychainKey_LastRegisteredPhoneNumber)
}
public class func setLastRegisteredPhoneNumber(value: String) {
setDebugValue(value, forKey: kKeychainKey_LastRegisteredPhoneNumber)
}
}