-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathAcsHelper.swift
215 lines (206 loc) · 6.7 KB
/
AcsHelper.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// AcsHelper.swift
// ios
//
// Copyright © 2019 Tinode. All rights reserved.
//
import Foundation
public class AcsHelper: Codable, CustomStringConvertible, Equatable {
enum AcsError: Error {
case invalidValue
}
// J - join topic.
public static let kModeJoin = 0x01
// R - read broadcasts
public static let kModeRead = 0x02
// W - publish
public static let kModeWrite = 0x04
// P - receive presence notifications
public static let kModePres = 0x08
// A - approve requests
public static let kModeApprove = 0x10
// S - user can invite other people to join (S)
public static let kModeShare = 0x20
// D - user can hard-delete messages (D), only owner can completely delete
public static let kModeDelete = 0x40
// O - user is the owner (O) - full access
public static let kModeOwner = 0x80
// No access, requests to gain access are processed normally (N)
public static let kModeNone = 0
// Invalid mode to indicate an error
public static let kModeInvalid = 0x100000
private static let kModes = ["J", "R", "W", "P", "A", "S", "D", "O"]
private var a: Int?
public var description: String {
guard let b = a else { return "" }
return AcsHelper.encode(mode: b)
}
/// The value is neither nil nor Invalid. None is considered to be defined.
public var isDefined: Bool {
return (a ?? AcsHelper.kModeInvalid) != AcsHelper.kModeInvalid
}
public var isOwner: Bool {
return ((a ?? 0) & AcsHelper.kModeOwner) != 0
}
public var isAdmin: Bool {
return ((a ?? 0) & AcsHelper.kModeApprove) != 0
}
public var isManager: Bool {
return isOwner || isAdmin
}
public var isSharer: Bool {
return ((a ?? 0) & AcsHelper.kModeShare) != 0
}
public var isMuted: Bool {
return ((a ?? 0) & AcsHelper.kModePres) == 0
}
public var isInvalid: Bool {
return (a ?? 0) == AcsHelper.kModeInvalid
}
public var isJoiner: Bool {
return ((a ?? 0) & AcsHelper.kModeJoin) != 0
}
public var isReader: Bool {
return ((a ?? 0) & AcsHelper.kModeRead) != 0
}
public var isWriter: Bool {
return ((a ?? 0) & AcsHelper.kModeWrite) != 0
}
public var isDeleter: Bool {
return ((a ?? 0) & AcsHelper.kModeDelete) != 0
}
public var isNone: Bool {
return (a ?? -1) == AcsHelper.kModeNone
}
public init(str: String?) {
a = AcsHelper.decode(from: str)
}
public init(ah: AcsHelper?) {
if ah != nil {
a = ah!.a
}
}
init(a: Int?) {
self.a = a
}
public func hasPermissions(forMode mode: Int) -> Bool {
guard !isInvalid else { return false }
return (a! & mode) != 0
}
private static func decode(from modeStr: String?) -> Int {
guard let mode = modeStr, mode.count > 0 else {
return kModeInvalid
}
var m0 = kModeNone
let modeUpper = mode.uppercased()
for c in modeUpper {
if let idx = kModes.firstIndex(of: String(c)) {
m0 |= 1 << idx
} else {
return c == "N" ? kModeNone : kModeInvalid
}
}
return m0
}
private static func encode(mode: Int) -> String {
if mode == kModeInvalid {
return ""
}
if mode == kModeNone {
return "N"
}
var result: [String] = []
for i in 0..<kModes.count {
if ((mode >> i) & 1) != 0 {
result.append(kModes[i])
}
}
return result.joined()
}
// Same as split() but retains the separators ["+", "-"]: "+ABC-DEF" -> ["+ABC", "-DEF"]
private static func tokenizeCommand(command str: String) -> [String] {
var result: [String] = []
var temp = ""
for c in str {
if c == "+" || c == "-" {
if !temp.isEmpty {
result.append(temp)
// Clear temp.
temp.removeAll()
}
}
temp.append(c)
}
if !temp.isEmpty {
result.append(temp)
}
return result
}
@discardableResult
public func update(from umode: String) -> Bool {
let olda = a
do {
// Invalid value may be recived from the network. Invalid network data should not crash the client.
try a = AcsHelper.update(original: a, updateWith: umode)
} catch {
Tinode.log.error("AcsHelper.update - failed to parse access mode: '%@' - %@",
umode, error.localizedDescription)
}
return a != olda
}
private static func update(original mode: Int?, updateWith command: String?) throws -> Int? {
guard let command = command, command.count > 0 else {
return mode
}
var result: Int
let action = command.first // [command.startIndex]
if action == "+" || action == "-" {
result = mode ?? 0
let parts = tokenizeCommand(command: command)
for p in parts {
// Skip command character and decode
let m0 = decode(from: String(p.dropFirst(1)))
if m0 == kModeInvalid {
throw AcsError.invalidValue
}
if m0 == kModeNone {
continue
}
if p.first == "+" { // p[p.startIndex]
result |= m0
} else {
result &= ~m0
}
}
} else {
result = AcsHelper.decode(from: command)
if result == kModeInvalid {
throw AcsError.invalidValue
}
}
return result
}
public func merge(with ah: AcsHelper?) -> Bool {
guard ah != nil && self.a != nil && self.a! != AcsHelper.kModeInvalid else {
return false
}
if let aha = ah!.a, aha != self.a {
self.a = aha
return true
}
return false
}
// Bitwise & operator.
public static func and(a1: AcsHelper?, a2: AcsHelper?) -> AcsHelper? {
guard let ah1 = a1, let ah2 = a2, !ah1.isInvalid, !ah2.isInvalid else { return nil }
return AcsHelper(a: ah1.a! & ah2.a!)
}
// Bits present in a1 but missing in a2.
public static func diff(a1: AcsHelper?, a2: AcsHelper?) -> AcsHelper? {
guard let a1a = a1?.a, let a2a = a2?.a, !a1!.isInvalid, !a2!.isInvalid else { return nil }
return AcsHelper(a: a1a & ~a2a)
}
public static func == (lhs: AcsHelper, rhs: AcsHelper) -> Bool {
return lhs.a == rhs.a
}
}