import Foundation final class StanzaModule: XmppModule { let id = "Stanza module" private weak var storage: XMPPStorage? init(_ storage: any XMPPStorage) { self.storage = storage } func reduce(oldState: ClientState, with _: Event) -> ClientState { oldState } func process(state: ClientState, with event: Event) async -> Event? { // try to send/receive stanzas only on ready, secured, and authorized state guard state.isSocketSecured && state.authorizationStep == .authorized else { return nil } // receive/send stanzas from/to xml switch event { case .stanzaOutbound(let stanza): await saveStanza(state, stanza, inbound: false) return .xmlOutbound(stanza.wrapped) case .xmlInbound(let xml): if let stanza = Stanza(wrap: xml) { await saveStanza(state, stanza, inbound: true) return .stanzaInbound(stanza) } else { return nil } default: return nil } } } // MARK: Save history private extension StanzaModule { func saveStanza(_ state: ClientState, _ stanza: Stanza, inbound: Bool) async { guard let storage else { return } print(state, stanza, inbound) } }