chatsecureios / ChatSecure / Classes / Model / ServerCapabilityInfo.swift @ edeae62c
History | View | Annotate | Download (5.04 KB)
1 |
// |
---|---|
2 |
// ServerCapabilityInfo.swift |
3 |
// ChatSecure |
4 |
// |
5 |
// Created by Chris Ballinger on 2/10/17. |
6 |
// Copyright © 2017 Chris Ballinger. All rights reserved. |
7 |
// |
8 |
|
9 |
import UIKit |
10 |
|
11 |
@objc(OTRCapabilityStatus) |
12 |
public enum CapabilityStatus: UInt { |
13 |
case Unknown |
14 |
case Available |
15 |
case Unavailable |
16 |
case Warning |
17 |
} |
18 |
|
19 |
public enum CapabilityCode: String { |
20 |
case Unknown = "Unknown" |
21 |
/// XEP-0198: Stream Management |
22 |
case XEP0198 = "XEP-0198" |
23 |
/// XEP-0357: Push |
24 |
case XEP0357 = "XEP-0357" |
25 |
/// XEP-0363: HTTP File Upload https://xmpp.org/extensions/xep-0363.html |
26 |
case XEP0363 = "XEP-0363" |
27 |
/// XEP-0352: Client State Indication https://xmpp.org/extensions/xep-0352.html |
28 |
case XEP0352 = "XEP-0352" |
29 |
/// XEP-0359: Unique and Stable Stanza IDs https://xmpp.org/extensions/xep-0359.html |
30 |
case XEP0359 = "XEP-0359" |
31 |
/// XEP-0313: Message Archive Management https://xmpp.org/extensions/xep-0313.html |
32 |
case XEP0313 = "XEP-0313" |
33 |
} |
34 |
|
35 |
@objc(OTRServerCapabilityInfo) |
36 |
public class ServerCapabilityInfo: NSObject, NSCopying { |
37 |
public var status: CapabilityStatus = .Unknown |
38 |
public let code: CapabilityCode |
39 |
public let title: String |
40 |
public let subtitle: String |
41 |
/// used to match against caps xml |
42 |
public let xmlns: String |
43 |
public let url: NSURL |
44 |
|
45 |
public init(code: CapabilityCode, title: String, subtitle: String, xmlns: String, url: NSURL) { |
46 |
self.code = code |
47 |
self.title = title |
48 |
self.subtitle = subtitle |
49 |
self.xmlns = xmlns |
50 |
self.url = url |
51 |
} |
52 |
|
53 |
public func copy(with zone: NSZone? = nil) -> Any { |
54 |
return ServerCapabilityInfo(code: self.code, title: self.title, subtitle: self.subtitle, xmlns: self.xmlns, url: url) |
55 |
} |
56 |
|
57 |
|
58 |
public class func allCapabilities() -> [CapabilityCode: ServerCapabilityInfo] { |
59 |
var caps: [CapabilityCode: ServerCapabilityInfo] = [:] |
60 |
caps[.XEP0198] = ServerCapabilityInfo( |
61 |
code: .XEP0198, |
62 |
title: "Stream Management", |
63 |
subtitle: "\(CapabilityCode.XEP0198.rawValue): Provides better experience during temporary disconnections.", |
64 |
xmlns: "urn:xmpp:sm", |
65 |
url: NSURL(string: "https://xmpp.org/extensions/xep-0198.html")!) |
66 |
caps[.XEP0357] = ServerCapabilityInfo( |
67 |
code: .XEP0357, |
68 |
title: "Push", |
69 |
subtitle: "\(CapabilityCode.XEP0357.rawValue): Provides push messaging support.", |
70 |
xmlns: "urn:xmpp:push", |
71 |
url: NSURL(string: "https://xmpp.org/extensions/xep-0357.html")!) |
72 |
caps[.XEP0363] = ServerCapabilityInfo( |
73 |
code: .XEP0363, |
74 |
title: "HTTP File Upload", |
75 |
subtitle: "\(CapabilityCode.XEP0363.rawValue): Provides file transfer for media messaging.", |
76 |
xmlns: XMPPHTTPFileUploadNamespace, |
77 |
url: NSURL(string: "https://xmpp.org/extensions/xep-0363.html")!) |
78 |
caps[.XEP0352] = ServerCapabilityInfo( |
79 |
code: .XEP0352, |
80 |
title: "Client State Indication", |
81 |
subtitle: "\(CapabilityCode.XEP0352.rawValue): Helps reduce network usage when running in the background.", |
82 |
xmlns: "urn:xmpp:csi", |
83 |
url: NSURL(string: "https://xmpp.org/extensions/xep-0352.html")!) |
84 |
caps[.XEP0359] = ServerCapabilityInfo( |
85 |
code: .XEP0359, |
86 |
title: "Unique and Stable Stanza IDs", |
87 |
subtitle: "\(CapabilityCode.XEP0359.rawValue): Improves message deduplication accuracy.", |
88 |
xmlns: "urn:xmpp:sid", |
89 |
url: NSURL(string: "https://xmpp.org/extensions/xep-0359.html")!) |
90 |
caps[.XEP0313] = ServerCapabilityInfo( |
91 |
code: .XEP0313, |
92 |
title: "Message Archive Management", |
93 |
subtitle: "\(CapabilityCode.XEP0313.rawValue): History synchronization across your devices.", |
94 |
xmlns: "urn:xmpp:mam", |
95 |
url: NSURL(string: "https://xmpp.org/extensions/xep-0313.html")!) |
96 |
return caps |
97 |
} |
98 |
|
99 |
|
100 |
|
101 |
} |
102 |
|
103 |
public extension OTRServerCapabilities { |
104 |
// MARK: Utility |
105 |
|
106 |
/** |
107 |
* This will determine which features are available. |
108 |
* Will return nil if the module hasn't finished processing. |
109 |
*/ |
110 |
public func markAvailable(capabilities: [CapabilityCode : ServerCapabilityInfo]) -> [CapabilityCode :ServerCapabilityInfo]? { |
111 |
guard let allCaps = self.allCapabilities, let features = self.streamFeatures else { |
112 |
return nil |
113 |
} |
114 |
let allFeatures = OTRServerCapabilities.allFeatures(forCapabilities: allCaps, streamFeatures: features) |
115 |
var newCaps: [CapabilityCode : ServerCapabilityInfo] = [:] |
116 |
for (_, var capInfo) in capabilities { |
117 |
capInfo = capInfo.copy() as! ServerCapabilityInfo |
118 |
for feature in allFeatures { |
119 |
if feature.contains(capInfo.xmlns) { |
120 |
capInfo.status = .Available |
121 |
break |
122 |
} |
123 |
} |
124 |
// if its not found, mark it unavailable |
125 |
if capInfo.status != .Available { |
126 |
capInfo.status = .Unavailable |
127 |
} |
128 |
newCaps[capInfo.code] = capInfo |
129 |
} |
130 |
return newCaps |
131 |
} |
132 |
} |