CryptoPassphrase  Diff

Differences From Artifact [e4b689e0a1]:

To Artifact [9f3b8af52a]:


1
2

3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9

-
+







/*
 * Copyright (c) 2016 - 2021 Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2016 - 2023 Jonathan Schleifer <js@nil.im>
 *
 * https://fossil.nil.im/cryptopassphrase
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
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
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







-
+





-
+

-
-
+
+


-
+




-
+







-
+







import ObjFWBridge

class SiteStorage: OFObject {
    private static let lengthField = NSNumber(value: 0)
    private static let legacyField = NSNumber(value: 1)
    private static let keyFileField = NSNumber(value: 2)

    private var path: OFString
    private var IRI: OFIRI
    private var storage: [String: [NSNumber: AnyObject]]
    private var sites: [String]

    override init() {
        let fileManager = OFFileManager.default
        let userDataURL = OFSystemInfo.userDataURL!
        let userDataIRI = OFSystemInfo.userDataIRI!

        if !fileManager.directoryExists(at: userDataURL) {
            fileManager.createDirectory(at: userDataURL)
        if !fileManager.directoryExists(at: userDataIRI) {
            fileManager.createDirectory(at: userDataIRI)
        }

        let URL = userDataURL.appendingPathComponent(
        let IRI = userDataIRI.appendingPathComponent(
            OFString(utf8String: "sites.msgpack"))

        var storage: [String: [NSNumber: AnyObject]]? = nil
        OFException.try({
            let decoded = (OFData(contentsOf: URL).objectByParsingMessagePack)
            let decoded = (OFData(contentsOf: IRI).objectByParsingMessagePack)
                as? OFDictionary<OFString, OFDictionary<OFNumber, AnyObject>>
            storage =
                (decoded?.nsObject as? [String: [NSNumber: AnyObject]]) ?? [:]
        }, catch: { (OFException) in
            storage = [:]
        })

        self.path = URL.fileSystemRepresentation!
        self.IRI = IRI
        self.storage = storage!
        self.sites = self.storage.keys.sorted()
    }

    func sites(withFilter filter: String?) -> [String] {
        return storage.keys.sorted().filter({ (name) in
            if filter == nil || filter!.isEmpty {
111
112
113
114
115
116
117
118

119
120
121
111
112
113
114
115
116
117

118
119
120
121







-
+



        storage[name] = nil
        self.update()
    }

    private func update() {
        let ofStorage = (storage as NSDictionary).ofObject

        ofStorage.messagePackRepresentation.write(toFile: path)
        ofStorage.messagePackRepresentation.write(to: IRI)
        sites = storage.keys.sorted()
    }
}