another.im-ios/AnotherXMPP/modules/roster/RosterModule.swift

263 lines
9.5 KiB
Swift
Raw Normal View History

2025-01-09 15:17:02 +00:00
// RFC 6121
// XEP-0237
2024-12-16 12:51:12 +00:00
import Foundation
final class RosterModule: XmppModule {
let id = "Roseter module"
2024-12-17 10:00:51 +00:00
private weak var storage: (any XMPPStorage)?
2025-01-09 15:17:02 +00:00
private var isVerSupported = false
2025-01-13 03:11:24 +00:00
private let registry = StanzaRegistry()
2024-12-17 10:00:51 +00:00
init(_ storage: any XMPPStorage) {
self.storage = storage
}
2024-12-16 12:51:12 +00:00
func reduce(oldState: ClientState, with _: Event) -> ClientState {
oldState
}
func process(state: ClientState, with event: Event) async -> Event? {
switch event {
case .streamReady:
return .requestRoster
2025-01-09 15:17:02 +00:00
case .xmlInbound(let xml):
if xml.name == "stream:features" {
if let ver = xml.nodes.first(where: { $0.name == "ver" }), ver.xmlns == "urn:xmpp:features:rosterver" {
isVerSupported = true
}
}
return nil
2024-12-16 12:51:12 +00:00
case .requestRoster:
2025-01-09 15:17:02 +00:00
var attributes: [String: String] = [:]
if isVerSupported {
let ver = await storage?.getRosterVer(jid: state.jid)
attributes["ver"] = ver ?? ""
}
2024-12-17 10:00:51 +00:00
let req = Stanza.iqGet(
from: state.jid.full,
2025-01-09 15:17:02 +00:00
payload: XMLElement(
name: "query",
xmlns: "jabber:iq:roster",
attributes: attributes,
content: nil,
nodes: []
)
2024-12-17 10:00:51 +00:00
)
2024-12-16 12:51:12 +00:00
if let req {
2025-01-13 03:11:24 +00:00
await registry.enqueue(req)
2024-12-16 12:51:12 +00:00
return .stanzaOutbound(req)
} else {
return nil
}
2025-01-09 16:04:09 +00:00
case .addRosterItem(let jidStr, let args), .updateRosterItem(let jidStr, let args):
var attr = ["jid": jidStr]
if let name = args["name"] {
attr["name"] = name
}
let req = Stanza.iqSet(
from: state.jid.full,
payload: XMLElement(
name: "query",
xmlns: "jabber:iq:roster",
attributes: [:],
content: nil,
nodes: [
XMLElement(
name: "item",
xmlns: nil,
attributes: attr,
content: nil,
nodes: []
)
]
)
)
if let req {
2025-01-13 03:11:24 +00:00
await registry.enqueue(req)
2025-01-09 16:04:09 +00:00
return .stanzaOutbound(req)
} else {
return nil
}
2024-12-24 10:43:15 +00:00
case .deleteRosterItem(let jidStr):
2025-01-09 17:07:39 +00:00
let req = Stanza.iqSet(
from: state.jid.full,
payload: XMLElement(
name: "query",
xmlns: "jabber:iq:roster",
attributes: [:],
content: nil,
nodes: [
XMLElement(
name: "item",
xmlns: nil,
attributes: ["jid": jidStr, "subscription": "remove"],
content: nil,
nodes: []
)
]
)
)
if let req {
2025-01-13 03:11:24 +00:00
await registry.enqueue(req)
2025-01-09 17:07:39 +00:00
return .stanzaOutbound(req)
} else {
return nil
}
2024-12-24 10:43:15 +00:00
2024-12-17 10:00:51 +00:00
case .stanzaInbound(let stanza):
2024-12-17 15:28:30 +00:00
if let query = stanza.wrapped.nodes.first(where: { $0.name == "query" }), query.xmlns == "jabber:iq:roster" {
2025-01-09 17:47:42 +00:00
// update version if needed
if let ver = stanza.wrapped.nodes.first(where: { $0.name == "query" })?.attributes["ver"] {
await storage?.setRosterVer(jid: state.jid, version: ver)
}
2025-01-13 03:11:24 +00:00
// process stanza
2025-01-13 03:42:08 +00:00
return await processInbound(state: state, stanza: stanza)
2025-01-13 03:11:24 +00:00
} else {
return nil
}
2024-12-31 12:28:03 +00:00
2025-01-13 14:14:04 +00:00
// if we signaling "result" to server it means roster was updated on this resource
case .stanzaOutbound(let stanza):
if let query = stanza.wrapped.nodes.first(where: { $0.name == "query" }), query.xmlns == "jabber:iq:roster", stanza.type == .iq(.result) {
return .rosterUpdated
} else {
return nil
}
2025-01-13 03:11:24 +00:00
default:
return nil
}
}
}
2024-12-31 12:28:03 +00:00
2025-01-13 03:11:24 +00:00
private extension RosterModule {
2025-01-13 03:42:08 +00:00
func processInbound(state: ClientState, stanza: Stanza) async -> Event? {
2025-01-13 03:11:24 +00:00
switch stanza.type {
2025-01-13 03:42:08 +00:00
// "set" type stanza from server is just "push", so change roster accordingly
2025-01-13 03:11:24 +00:00
case .iq(.set):
2025-01-13 14:14:04 +00:00
// sanity check (according RFC6121 skip this push if its not for current resource)
2025-01-13 03:42:08 +00:00
if stanza.wrapped.attributes["to"] != state.jid.bare {
return nil
}
2025-01-09 17:47:42 +00:00
2025-01-13 03:42:08 +00:00
// get exists roster items
var existItems: [RosterItem] = []
if let data = await storage?.getRoster(jid: state.jid), let decoded = try? JSONDecoder().decode([XMLElement].self, from: data) {
existItems = decoded.compactMap { RosterItem(wrap: $0, owner: state.jid) }
}
// get item from stanza
let xmlItem = stanza.wrapped
.nodes
.first(where: { $0.name == "query" })?
.nodes
.first(where: { $0.name == "item" })
guard let xmlItem, let item = RosterItem(wrap: xmlItem, owner: state.jid) else { return nil }
// filter out exists items
existItems = existItems.filter { $0.id != item.id }
// append updated/new item (if it not "removed")
if item.subsription != .remove {
existItems.append(item)
}
2025-01-09 17:47:42 +00:00
2025-01-13 03:42:08 +00:00
// save roster
guard let data = try? JSONEncoder().encode(existItems.map { $0.wrapped }) else { return nil }
await storage?.setRoster(jid: state.jid, roster: data)
2025-01-13 14:14:04 +00:00
// according RFC6120 it is not necessary to answer server with "result" on push, but why not?
let result = Stanza.iqResult(
from: state.jid.full,
payload: XMLElement(
name: "query",
xmlns: "jabber:iq:roster",
attributes: [:],
content: nil,
nodes: []
)
)
if let result {
return .stanzaOutbound(result)
} else {
return nil
}
// the "result" from server is an answer from one of our "set", or initial roster request
2025-01-13 03:11:24 +00:00
case .iq(.result):
2025-01-13 03:42:08 +00:00
// get request stanza from registry
guard let respId = stanza.id, let req = await registry.deuque(for: respId) else { return nil }
// get exists roster items
var existItems: [RosterItem] = []
if let data = await storage?.getRoster(jid: state.jid), let decoded = try? JSONDecoder().decode([XMLElement].self, from: data) {
existItems = decoded.compactMap { RosterItem(wrap: $0, owner: state.jid) }
}
// perform changes based on stanza request type
switch req.type {
// for simple roster request
case .iq(.get):
// get items from response
let stanzaItems = stanza.wrapped
.nodes
.first(where: { $0.name == "query" })?
.nodes
.filter { $0.name == "item" }
.compactMap { RosterItem(wrap: $0, owner: state.jid) } ?? []
let stanzaItemsIds = stanzaItems.map { $0.id }
// filter out exists items and append updated
existItems = existItems.filter { !stanzaItemsIds.contains($0.id) }
existItems.append(contentsOf: stanzaItems)
// save roster
guard let data = try? JSONEncoder().encode(existItems.map { $0.wrapped }) else { return nil }
await storage?.setRoster(jid: state.jid, roster: data)
return .rosterUpdated
// for result of one of our request
case .iq(.set):
2025-01-13 14:14:04 +00:00
// get item from request stanza
let xmlItem = req.wrapped
.nodes
.first(where: { $0.name == "query" })?
.nodes
.first(where: { $0.name == "item" })
guard let xmlItem, let item = RosterItem(wrap: xmlItem, owner: state.jid) else { return nil }
// filter out exists items
existItems = existItems.filter { $0.id != item.id }
// append updated/new item (if it not "removed")
if item.subsription != .remove {
existItems.append(item)
}
// save roster
guard let data = try? JSONEncoder().encode(existItems.map { $0.wrapped }) else { return nil }
await storage?.setRoster(jid: state.jid, roster: data)
return .rosterUpdated
2025-01-13 03:42:08 +00:00
default:
2025-01-13 14:14:04 +00:00
return nil
2025-01-13 03:42:08 +00:00
}
// TODO: add error handling here
case .iq(.error):
2025-01-13 14:14:04 +00:00
guard let respId = stanza.id, let req = await registry.deuque(for: respId) else { return nil }
print("Error on request: \(req.wrapped)\n with: \(stanza.wrapped)")
2025-01-13 03:11:24 +00:00
return nil
2024-12-17 10:00:51 +00:00
2024-12-16 12:51:12 +00:00
default:
return nil
}
}
}