This commit is contained in:
fmodf 2024-12-16 14:11:46 +01:00
parent ab01430aae
commit b9bf1ebfa5
3 changed files with 55 additions and 21 deletions

View file

@ -0,0 +1,29 @@
import Foundation
enum RosterSubsriptionType: String {
case both
case from
case none
case remove
case to
var isFrom: Bool {
switch self {
case .from, .both:
return true
case .none, .to, .remove:
return false
}
}
var isTo: Bool {
switch self {
case .to, .both:
return true
case .none, .from, .remove:
return false
}
}
}

View file

@ -73,12 +73,32 @@ struct Stanza {
} }
} }
// MARK: Init // MARK: Init IQ Stanza
extension Stanza { extension Stanza {
static func iqGet(jid: String? = nil, payload: XMLElement) -> Stanza? { static func iqGet(payload: XMLElement) -> Stanza? {
var attributes = ["id": XMLElement.randomId, "type": "get"] buildIq(direction: "get", from: nil, to: nil, payload: payload)
if let jid { }
attributes["from"] = jid
static func iqGet(from: String, payload: XMLElement) -> Stanza? {
buildIq(direction: "get", from: from, to: nil, payload: payload)
}
static func iqSet(payload: XMLElement) -> Stanza? {
buildIq(direction: "set", from: nil, to: nil, payload: payload)
}
static func iqSet(from: String, payload: XMLElement) -> Stanza? {
buildIq(direction: "set", from: from, to: nil, payload: payload)
}
// build iq stanza
private static func buildIq(direction: String, from: String?, to: String?, payload: XMLElement) -> Stanza? {
var attributes = ["id": XMLElement.randomId, "type": direction]
if let from {
attributes["from"] = from
}
if let to {
attributes["to"] = to
} }
let req = XMLElement( let req = XMLElement(
name: "iq", name: "iq",
@ -89,21 +109,6 @@ extension Stanza {
) )
return Stanza(wrap: req) return Stanza(wrap: req)
} }
static func iqSet(jid: String? = nil, payload: XMLElement) -> Stanza? {
var attributes = ["id": XMLElement.randomId, "type": "get"]
if let jid {
attributes["from"] = jid
}
let req = XMLElement(
name: "iq",
xmlns: nil,
attributes: ["type": "set", "id": XMLElement.randomId],
content: nil,
nodes: [payload]
)
return Stanza(wrap: req)
}
} }
private extension Stanza { private extension Stanza {

View file

@ -14,7 +14,7 @@ final class RosterModule: XmppModule {
case .requestRoster: case .requestRoster:
// TODO: check version! // TODO: check version!
let req = Stanza.iqGet(jid: state.jid.full, payload: XMLElement(name: "query", xmlns: "jabber:iq:roster", attributes: [:], content: nil, nodes: [])) let req = Stanza.iqGet(from: state.jid.full, payload: XMLElement(name: "query", xmlns: "jabber:iq:roster", attributes: [:], content: nil, nodes: []))
if let req { if let req {
return .stanzaOutbound(req) return .stanzaOutbound(req)
} else { } else {