49 lines
1.3 KiB
Swift
49 lines
1.3 KiB
Swift
import Foundation
|
|
|
|
final class StanzaModule: XmppModule {
|
|
let id = "Stanza module"
|
|
|
|
private weak var storage: XMPPClientStorageHistory?
|
|
|
|
init(_ storage: any XMPPClientStorageHistory) {
|
|
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 {
|
|
print(state, stanza, inbound)
|
|
}
|
|
}
|