XMPPController.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // XMPPController.swift
  3. // Chat
  4. //
  5. // Created by Sergey Tarasov on 25.07.2022.
  6. //
  7. import Foundation
  8. import XMPPFramework
  9. import XMPPFrameworkSwift
  10. /*
  11. final class XMPPController: NSObject {
  12. static let shared = XMPPController()
  13. var xmppStream: XMPPStream
  14. var xmppReconnect: XMPPReconnect
  15. var xmppRoster: XMPPRoster
  16. var xmppRosterStorage: XMPPRosterMemoryStorage
  17. var xmppMUC: XMPPMUCLight
  18. var xmppMAM: XMPPMessageArchiveManagement
  19. var xmppCapabilities: XMPPCapabilities
  20. var xmppCapabilitiesStorage: XMPPCapabilitiesStorage
  21. var xmppPing: XMPPPing
  22. var xmppTime: XMPPTime
  23. var users: [XMPPJID] = []
  24. var rooms: [XMPPRoom] = []
  25. var messages: [String] = []
  26. var password: String?
  27. override init() {
  28. self.xmppStream = XMPPStream()
  29. self.xmppReconnect = XMPPReconnect()
  30. self.xmppRosterStorage = XMPPRosterMemoryStorage()
  31. self.xmppRoster = XMPPRoster(rosterStorage: self.xmppRosterStorage, dispatchQueue: DispatchQueue.main)
  32. self.xmppMUC = XMPPMUCLight(dispatchQueue: DispatchQueue.main)
  33. self.xmppMAM = XMPPMessageArchiveManagement(dispatchQueue: DispatchQueue.main)
  34. self.xmppCapabilitiesStorage = XMPPCapabilitiesCoreDataStorage.sharedInstance()
  35. self.xmppCapabilities = XMPPCapabilities(capabilitiesStorage: self.xmppCapabilitiesStorage)
  36. self.xmppCapabilities.autoFetchHashedCapabilities = true
  37. self.xmppCapabilities.autoFetchNonHashedCapabilities = false
  38. self.xmppPing = XMPPPing()
  39. self.xmppTime = XMPPTime()
  40. self.xmppReconnect.activate(self.xmppStream)
  41. self.xmppRoster.activate(self.xmppStream)
  42. self.xmppMUC.activate(self.xmppStream)
  43. self.xmppMAM.activate(self.xmppStream)
  44. self.xmppCapabilities.activate(self.xmppStream)
  45. self.xmppPing.activate(self.xmppStream)
  46. self.xmppTime.activate(self.xmppStream)
  47. super.init()
  48. self.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
  49. self.xmppReconnect.addDelegate(self, delegateQueue: DispatchQueue.main)
  50. self.xmppMUC.addDelegate(self, delegateQueue: DispatchQueue.main)
  51. self.xmppMAM.addDelegate(self, delegateQueue: DispatchQueue.main)
  52. self.xmppCapabilities.addDelegate(self, delegateQueue: DispatchQueue.main)
  53. }
  54. func setupStream(with login: String?, password: String?) {
  55. self.password = password ?? ""
  56. self.xmppStream.hostName = "msg.sharix-app.org"
  57. self.xmppStream.hostPort = 5222
  58. self.xmppStream.myJID = XMPPJID(string: login ?? "test11@msg.sharix-app.org")
  59. self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
  60. }
  61. func connect() {
  62. if !self.xmppStream.isDisconnected { return }
  63. try! self.xmppStream.connect(withTimeout: XMPPStreamTimeoutNone)
  64. }
  65. func getUserList() {
  66. self.users = self.xmppRosterStorage.jids(for: self.xmppStream)
  67. }
  68. func getRoomList() {
  69. self.xmppMUC.discoverRooms(forServiceNamed: "chat.msg.sharix-app.org")
  70. }
  71. func getMessages(from jid: XMPPJID) {
  72. self.messages.removeAll()
  73. let field = DDXMLElement(name: "field")
  74. field.addAttribute(withName: "var", stringValue: "with")
  75. let fieldValue = DDXMLElement(name: "value", stringValue: jid.bare)
  76. field.addChild(fieldValue)
  77. self.xmppMAM.retrieveMessageArchive(withFields: nil, with: nil)
  78. }
  79. }
  80. extension XMPPController: XMPPMessageArchiveManagementDelegate {
  81. func xmppMessageArchiveManagement(
  82. _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
  83. didReceiveFormFields iq: XMPPIQ
  84. ) {
  85. print(iq.debugDescription)
  86. }
  87. func xmppMessageArchiveManagement(
  88. _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
  89. didReceiveMAMMessage message: XMPPMessage
  90. ) {
  91. print(message.debugDescription)
  92. guard let messageText = message.body else { return }
  93. self.messages.append(messageText)
  94. }
  95. func xmppMessageArchiveManagement(
  96. _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
  97. didFinishReceivingMessagesWith resultSet: XMPPResultSet
  98. ) {
  99. print(resultSet.debugDescription)
  100. }
  101. }
  102. extension XMPPController: XMPPReconnectDelegate {
  103. func xmppReconnect(
  104. _ sender: XMPPReconnect,
  105. shouldAttemptAutoReconnect connectionFlags: SCNetworkConnectionFlags
  106. ) -> Bool {
  107. return true
  108. }
  109. }
  110. extension XMPPController: XMPPStreamDelegate {
  111. func xmppStreamDidConnect(_ stream: XMPPStream) {
  112. print("Stream: Connected")
  113. try! stream.authenticate(withPassword: self.password ?? "test11_-")
  114. }
  115. func xmppStreamDidAuthenticate(_ sender: XMPPStream) {
  116. self.xmppStream.send(XMPPPresence())
  117. print("Stream: Authenticated")
  118. }
  119. func xmppStream(_ sender: XMPPStream, willReceive message: XMPPMessage) -> XMPPMessage? {
  120. guard let body = message.body else {
  121. return nil
  122. }
  123. debugPrint(body)
  124. return message
  125. }
  126. }
  127. extension XMPPController: XMPPMUCLightDelegate {
  128. // func xmppMUC(_ sender: XMPPMUC, didDiscoverRooms rooms: [Any], forServiceNamed serviceName: String) {
  129. // let elements = rooms.map({ try! DDXMLElement(xmlString: String(describing: $0)) })
  130. // let jids = elements.compactMap({ XMPPJID(string: $0.attributeStringValue(forName: "jid") ?? "") })
  131. // self.rooms = jids
  132. // print(self.rooms)
  133. // }
  134. func xmppMUCLight(
  135. _ sender: XMPPMUCLight,
  136. didDiscoverRooms rooms: [DDXMLElement],
  137. forServiceNamed serviceName: String
  138. ) {
  139. let jids = rooms.compactMap({ XMPPJID(string: $0.attributeStringValue(forName: "jid") ?? "") })
  140. let rooms = jids.map { XMPPRoom(roomStorage: XMPPRoomMemoryStorage(), jid: $0) }
  141. self.rooms = rooms
  142. print(self.rooms)
  143. }
  144. }
  145. */