WebClientInfoResponse.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. class WebClientInfoResponse: WebAbstractMessage {
  22. let device: String
  23. let os: String
  24. let osVersion: String
  25. let appVersion: String
  26. let isWork: Bool
  27. var pushToken: String?
  28. let configuration: WebClientInfoConfiguration
  29. let capabilities: WebClientInfoCapabilities
  30. init(requestId: String?) {
  31. device = UIDevice.current.name
  32. os = "ios"
  33. osVersion = UIDevice.current.systemVersion
  34. appVersion = BundleUtil.mainBundle()?.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
  35. isWork = LicenseStore.requiresLicenseKey()
  36. let tmpPushToken = AppGroup.userDefaults().object(forKey: kVoIPPushNotificationDeviceToken) as? Data
  37. if tmpPushToken != nil {
  38. #if DEBUG
  39. let server = "s"
  40. #else
  41. let server = "p"
  42. #endif
  43. pushToken = tmpPushToken!.hexEncodedString() + ";" + server + ";" + Bundle.main.bundleIdentifier! + ".voip"
  44. }
  45. configuration = WebClientInfoConfiguration.init()
  46. capabilities = WebClientInfoCapabilities.init()
  47. var tmpData:[AnyHashable:Any?] = ["device": device, "os": os, "osVersion": osVersion, "appVersion": appVersion, "isWork": isWork, "configuration": configuration.objectDict(), "capabilities": capabilities.objectDict()]
  48. if pushToken != nil {
  49. tmpData.updateValue(pushToken, forKey: "pushToken")
  50. }
  51. let tmpAck = requestId != nil ? WebAbstractMessageAcknowledgement.init(requestId, true, nil) : nil
  52. super.init(messageType: "response", messageSubType: "clientInfo", requestId: nil, ack: tmpAck, args: nil, data: tmpData)
  53. }
  54. }
  55. struct WebClientInfoConfiguration {
  56. var voipEnabled: Bool
  57. var voipForceTurn: Bool
  58. var largeSingleEmoji: Bool
  59. var showInactiveIDs: Bool
  60. init() {
  61. voipEnabled = false
  62. if UserSettings.shared().enableThreemaCall == true {
  63. voipEnabled = true
  64. }
  65. voipForceTurn = UserSettings.shared().alwaysRelayCalls
  66. largeSingleEmoji = !UserSettings.shared().disableBigEmojis
  67. showInactiveIDs = !UserSettings.shared().hideStaleContacts
  68. }
  69. func objectDict() -> [String: Any] {
  70. return ["voipEnabled": voipEnabled, "voipForceTurn": voipForceTurn, "largeSingleEmoji": largeSingleEmoji, "showInactiveIDs": showInactiveIDs]
  71. }
  72. }
  73. struct WebClientInfoCapabilities {
  74. var maxGroupSize: Int
  75. var distributionLists: Bool
  76. var maxFileSize: Int
  77. var mdm: WebClientInfoMdmRestrictions?
  78. var recurrentPushes: Bool
  79. var imageFormat: WebClientInfoImageFormat
  80. init() {
  81. maxGroupSize = BundleUtil.object(forInfoDictionaryKey: "ThreemaMaxGroupMembers") as! Int
  82. distributionLists = false
  83. mdm = WebClientInfoMdmRestrictions.init()
  84. maxFileSize = kMaxFileSize
  85. recurrentPushes = true
  86. imageFormat = WebClientInfoImageFormat.init()
  87. }
  88. func objectDict() -> [String: Any] {
  89. var objectDict:[String: Any] = ["maxGroupSize": maxGroupSize, "maxFileSize": maxFileSize, "distributionLists": distributionLists, "recurrentPushes": recurrentPushes, "imageFormat": imageFormat.objectDict()]
  90. if mdm != nil && MDMSetup(setup: false).isManaged() {
  91. objectDict.updateValue(mdm!.objectDict(), forKey: "mdm")
  92. }
  93. return objectDict
  94. }
  95. }
  96. struct WebClientInfoImageFormat {
  97. var avatar: String
  98. var thumbnail: String
  99. init() {
  100. avatar = "image/jpeg"
  101. thumbnail = "image/jpeg"
  102. }
  103. func objectDict() -> [String: Any] {
  104. return ["avatar": avatar, "thumbnail": thumbnail]
  105. }
  106. }
  107. struct WebClientInfoMdmRestrictions {
  108. var disableAddContact: Bool
  109. var disableCreateGroup: Bool
  110. var disableSaveToGallery: Bool
  111. var disableExport: Bool
  112. var disableMessagePreview: Bool
  113. var disableCalls: Bool
  114. var readonlyProfile: Bool
  115. init() {
  116. let mdmSetup = MDMSetup(setup: false)!
  117. disableAddContact = mdmSetup.disableAddContact()
  118. disableCreateGroup = mdmSetup.disableCreateGroup()
  119. disableSaveToGallery = mdmSetup.disableSaveToGallery()
  120. disableExport = mdmSetup.disableExport()
  121. disableMessagePreview = mdmSetup.disableMessagePreview()
  122. disableCalls = mdmSetup.disableCalls()
  123. readonlyProfile = mdmSetup.readonlyProfile()
  124. }
  125. func objectDict() -> [String: Any] {
  126. return ["disableAddContact": disableAddContact, "disableCreateGroup": disableCreateGroup, "disableSaveToGallery": disableSaveToGallery, "disableExport": disableExport, "disableMessagePreview": disableMessagePreview, "disableCalls": disableCalls, "readonlyProfile": readonlyProfile]
  127. }
  128. }
  129. extension Data {
  130. func hexEncodedString() -> String {
  131. return map { String(format: "%02hhx", $0) }.joined()
  132. }
  133. }