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
|
/*
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-2.0-or-later
*/
import Foundation
import NCDesktopClientSocketKit
import NextcloudFileProviderKit
import OSLog
class FileProviderSocketLineProcessor: NSObject, LineProcessor {
var delegate: FileProviderExtension
let logger: FileProviderLogger
required init(delegate: FileProviderExtension, log: any FileProviderLogging) {
self.delegate = delegate
self.logger = FileProviderLogger(category: "FileProviderSocketLineProcessor", log: log)
}
func process(_ line: String) {
if line.contains("~") { // We use this as the separator specifically in ACCOUNT_DETAILS
logger.debug("Processing file provider line with potentially sensitive user data")
} else {
logger.debug("Processing file provider line: \(line)")
}
let splitLine = line.split(separator: ":", maxSplits: 1)
guard let commandSubsequence = splitLine.first else {
logger.error("Input line did not have a first element")
return
}
let command = String(commandSubsequence)
logger.debug("Received command: \(command)")
if command == "SEND_FILE_PROVIDER_DOMAIN_IDENTIFIER" {
delegate.sendFileProviderDomainIdentifier()
} else if command == "ACCOUNT_NOT_AUTHENTICATED" {
delegate.removeAccountConfig()
} else if command == "ACCOUNT_DETAILS" {
guard let accountDetailsSubsequence = splitLine.last else {
logger.error("Account details did not have a first element")
return
}
let splitAccountDetails = accountDetailsSubsequence.split(separator: "~", maxSplits: 4)
let userAgent = String(splitAccountDetails[0])
let user = String(splitAccountDetails[1])
let userId = String(splitAccountDetails[2])
let serverUrl = String(splitAccountDetails[3])
let password = String(splitAccountDetails[4])
delegate.setupDomainAccount(
user: user,
userId: userId,
serverUrl: serverUrl,
password: password,
userAgent: userAgent
)
} else if command == "IGNORE_LIST" {
guard let ignoreListSubsequence = splitLine.last else {
logger.error("Ignore list missing contents!")
return
}
let ignoreList = ignoreListSubsequence.components(separatedBy: "_~IL$~_")
logger.debug("Applying \(ignoreList.count) ignore file patterns")
delegate.ignoredFiles = IgnoredFilesMatcher(ignoreList: ignoreList)
}
}
}
|