123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- //
- // XMPPController.swift
- // Chat
- //
- // Created by Sergey Tarasov on 25.07.2022.
- //
- import Foundation
- import XMPPFramework
- import XMPPFrameworkSwift
- /*
- final class XMPPController: NSObject {
- static let shared = XMPPController()
- var xmppStream: XMPPStream
- var xmppReconnect: XMPPReconnect
- var xmppRoster: XMPPRoster
- var xmppRosterStorage: XMPPRosterMemoryStorage
- var xmppMUC: XMPPMUCLight
- var xmppMAM: XMPPMessageArchiveManagement
- var xmppCapabilities: XMPPCapabilities
- var xmppCapabilitiesStorage: XMPPCapabilitiesStorage
- var xmppPing: XMPPPing
- var xmppTime: XMPPTime
- var users: [XMPPJID] = []
- var rooms: [XMPPRoom] = []
- var messages: [String] = []
- var password: String?
- override init() {
- self.xmppStream = XMPPStream()
- self.xmppReconnect = XMPPReconnect()
- self.xmppRosterStorage = XMPPRosterMemoryStorage()
- self.xmppRoster = XMPPRoster(rosterStorage: self.xmppRosterStorage, dispatchQueue: DispatchQueue.main)
- self.xmppMUC = XMPPMUCLight(dispatchQueue: DispatchQueue.main)
- self.xmppMAM = XMPPMessageArchiveManagement(dispatchQueue: DispatchQueue.main)
- self.xmppCapabilitiesStorage = XMPPCapabilitiesCoreDataStorage.sharedInstance()
- self.xmppCapabilities = XMPPCapabilities(capabilitiesStorage: self.xmppCapabilitiesStorage)
- self.xmppCapabilities.autoFetchHashedCapabilities = true
- self.xmppCapabilities.autoFetchNonHashedCapabilities = false
- self.xmppPing = XMPPPing()
- self.xmppTime = XMPPTime()
- self.xmppReconnect.activate(self.xmppStream)
- self.xmppRoster.activate(self.xmppStream)
- self.xmppMUC.activate(self.xmppStream)
- self.xmppMAM.activate(self.xmppStream)
- self.xmppCapabilities.activate(self.xmppStream)
- self.xmppPing.activate(self.xmppStream)
- self.xmppTime.activate(self.xmppStream)
- super.init()
- self.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
- self.xmppReconnect.addDelegate(self, delegateQueue: DispatchQueue.main)
- self.xmppMUC.addDelegate(self, delegateQueue: DispatchQueue.main)
- self.xmppMAM.addDelegate(self, delegateQueue: DispatchQueue.main)
- self.xmppCapabilities.addDelegate(self, delegateQueue: DispatchQueue.main)
- }
- func setupStream(with login: String?, password: String?) {
- self.password = password ?? ""
- self.xmppStream.hostName = "msg.sharix-app.org"
- self.xmppStream.hostPort = 5222
- self.xmppStream.myJID = XMPPJID(string: login ?? "test11@msg.sharix-app.org")
- self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
- }
- func connect() {
- if !self.xmppStream.isDisconnected { return }
- try! self.xmppStream.connect(withTimeout: XMPPStreamTimeoutNone)
- }
- func getUserList() {
- self.users = self.xmppRosterStorage.jids(for: self.xmppStream)
- }
- func getRoomList() {
- self.xmppMUC.discoverRooms(forServiceNamed: "chat.msg.sharix-app.org")
- }
- func getMessages(from jid: XMPPJID) {
- self.messages.removeAll()
- let field = DDXMLElement(name: "field")
- field.addAttribute(withName: "var", stringValue: "with")
- let fieldValue = DDXMLElement(name: "value", stringValue: jid.bare)
- field.addChild(fieldValue)
- self.xmppMAM.retrieveMessageArchive(withFields: nil, with: nil)
- }
- }
- extension XMPPController: XMPPMessageArchiveManagementDelegate {
- func xmppMessageArchiveManagement(
- _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
- didReceiveFormFields iq: XMPPIQ
- ) {
- print(iq.debugDescription)
- }
- func xmppMessageArchiveManagement(
- _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
- didReceiveMAMMessage message: XMPPMessage
- ) {
- print(message.debugDescription)
- guard let messageText = message.body else { return }
- self.messages.append(messageText)
- }
- func xmppMessageArchiveManagement(
- _ xmppMessageArchiveManagement: XMPPMessageArchiveManagement,
- didFinishReceivingMessagesWith resultSet: XMPPResultSet
- ) {
- print(resultSet.debugDescription)
- }
- }
- extension XMPPController: XMPPReconnectDelegate {
- func xmppReconnect(
- _ sender: XMPPReconnect,
- shouldAttemptAutoReconnect connectionFlags: SCNetworkConnectionFlags
- ) -> Bool {
- return true
- }
- }
- extension XMPPController: XMPPStreamDelegate {
- func xmppStreamDidConnect(_ stream: XMPPStream) {
- print("Stream: Connected")
- try! stream.authenticate(withPassword: self.password ?? "test11_-")
- }
- func xmppStreamDidAuthenticate(_ sender: XMPPStream) {
- self.xmppStream.send(XMPPPresence())
- print("Stream: Authenticated")
- }
- func xmppStream(_ sender: XMPPStream, willReceive message: XMPPMessage) -> XMPPMessage? {
- guard let body = message.body else {
- return nil
- }
- debugPrint(body)
- return message
- }
- }
- extension XMPPController: XMPPMUCLightDelegate {
- // func xmppMUC(_ sender: XMPPMUC, didDiscoverRooms rooms: [Any], forServiceNamed serviceName: String) {
- // let elements = rooms.map({ try! DDXMLElement(xmlString: String(describing: $0)) })
- // let jids = elements.compactMap({ XMPPJID(string: $0.attributeStringValue(forName: "jid") ?? "") })
- // self.rooms = jids
- // print(self.rooms)
- // }
- func xmppMUCLight(
- _ sender: XMPPMUCLight,
- didDiscoverRooms rooms: [DDXMLElement],
- forServiceNamed serviceName: String
- ) {
- let jids = rooms.compactMap({ XMPPJID(string: $0.attributeStringValue(forName: "jid") ?? "") })
- let rooms = jids.map { XMPPRoom(roomStorage: XMPPRoomMemoryStorage(), jid: $0) }
- self.rooms = rooms
- print(self.rooms)
- }
- }
- */
|