2018-03-11 01:43:55 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-03-11 01:43:55 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
import Foundation
|
|
|
|
|
2018-02-12 17:28:26 +01:00
|
|
|
// TDLib Client Swift binding
|
2018-02-12 14:10:53 +01:00
|
|
|
class TdClient {
|
2020-11-18 15:38:19 +01:00
|
|
|
var client_id = td_create_client_id()
|
2018-02-12 17:28:26 +01:00
|
|
|
let tdlibMainLoop = DispatchQueue(label: "TDLib")
|
|
|
|
let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery")
|
2018-02-12 14:10:53 +01:00
|
|
|
var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>()
|
2018-02-12 18:21:29 +01:00
|
|
|
var updateF: ((Dictionary<String,Any>)->())?
|
|
|
|
var queryId: Int64 = 0
|
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
func queryAsync(query: [String: Any], f: ((Dictionary<String,Any>)->())? = nil) {
|
|
|
|
tdlibQueryQueue.async {
|
|
|
|
var newQuery = query
|
2018-02-12 18:21:29 +01:00
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
if f != nil {
|
|
|
|
let nextQueryId = self.queryId + 1
|
2018-02-12 17:28:26 +01:00
|
|
|
newQuery["@extra"] = nextQueryId
|
|
|
|
self.queryF[nextQueryId] = f
|
|
|
|
self.queryId = nextQueryId
|
2018-02-12 14:10:53 +01:00
|
|
|
}
|
2020-11-14 20:45:18 +01:00
|
|
|
td_send(self.client_id, to_json(newQuery))
|
2018-02-12 14:10:53 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-12 18:21:29 +01:00
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
func querySync(query: [String: Any]) -> Dictionary<String,Any> {
|
|
|
|
let semaphore = DispatchSemaphore(value:0)
|
|
|
|
var result = Dictionary<String,Any>()
|
|
|
|
queryAsync(query: query) {
|
|
|
|
result = $0
|
|
|
|
semaphore.signal()
|
|
|
|
}
|
|
|
|
semaphore.wait()
|
|
|
|
return result
|
|
|
|
}
|
2018-02-12 18:21:29 +01:00
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
init() {
|
|
|
|
}
|
2018-02-12 17:28:26 +01:00
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
deinit {
|
|
|
|
}
|
2018-02-12 17:28:26 +01:00
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
func run(updateHandler: @escaping (Dictionary<String,Any>)->()) {
|
|
|
|
updateF = updateHandler
|
|
|
|
tdlibMainLoop.async { [weak self] in
|
|
|
|
while (true) {
|
|
|
|
if let s = self {
|
2020-11-14 20:45:18 +01:00
|
|
|
if let res = td_receive(10) {
|
2018-02-12 14:10:53 +01:00
|
|
|
let event = String(cString: res)
|
|
|
|
s.queryResultAsync(event)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 18:21:29 +01:00
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
private func queryResultAsync(_ result: String) {
|
|
|
|
tdlibQueryQueue.async {
|
|
|
|
let json = try? JSONSerialization.jsonObject(with: result.data(using: .utf8)!, options:[])
|
2018-02-12 18:21:29 +01:00
|
|
|
if let dictionary = json as? [String:Any] {
|
2018-02-12 14:10:53 +01:00
|
|
|
if let extra = dictionary["@extra"] as? Int64 {
|
|
|
|
let index = self.queryF.index(forKey: extra)!
|
|
|
|
self.queryF[index].value(dictionary)
|
|
|
|
self.queryF.remove(at: index)
|
|
|
|
} else {
|
|
|
|
self.updateF!(dictionary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 18:21:29 +01:00
|
|
|
func to_json(_ obj: Any) -> String {
|
2018-02-12 14:10:53 +01:00
|
|
|
do {
|
|
|
|
let obj = try JSONSerialization.data(withJSONObject: obj)
|
|
|
|
return String(data: obj, encoding: .utf8)!
|
|
|
|
} catch {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 17:28:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
var client = TdClient()
|
|
|
|
|
2020-11-14 20:45:18 +01:00
|
|
|
// start the client by sending request to it
|
|
|
|
client.queryAsync(query: ["@type":"getOption", "name":"version"])
|
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
func myReadLine() -> String {
|
|
|
|
while (true) {
|
|
|
|
if let line = readLine() {
|
|
|
|
return line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 11:25:07 +01:00
|
|
|
func updateAuthorizationState(authorizationState: Dictionary<String, Any>) {
|
|
|
|
switch(authorizationState["@type"] as! String) {
|
2018-02-12 14:10:53 +01:00
|
|
|
case "authorizationStateWaitTdlibParameters":
|
|
|
|
client.queryAsync(query:[
|
|
|
|
"@type":"setTdlibParameters",
|
2022-09-07 21:19:08 +02:00
|
|
|
"database_directory":"tdlib",
|
|
|
|
"use_message_database":true,
|
|
|
|
"use_secret_chats":true,
|
|
|
|
"api_id":94575,
|
|
|
|
"api_hash":"a3406de8d171bb422bb6ddf3bbd800e2",
|
|
|
|
"system_language_code":"en",
|
|
|
|
"device_model":"Desktop",
|
|
|
|
"application_version":"1.0",
|
|
|
|
"enable_storage_optimizer":true
|
2018-02-12 17:28:26 +01:00
|
|
|
]);
|
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
case "authorizationStateWaitPhoneNumber":
|
2019-12-18 17:23:14 +01:00
|
|
|
print("Enter your phone number: ")
|
|
|
|
let phone_number = myReadLine()
|
|
|
|
client.queryAsync(query:["@type":"setAuthenticationPhoneNumber", "phone_number":phone_number], f:checkAuthenticationError)
|
2018-02-12 18:21:29 +01:00
|
|
|
|
2022-09-08 12:11:35 +02:00
|
|
|
case "authorizationStateWaitEmailAddress":
|
|
|
|
print("Enter your email address: ")
|
|
|
|
let email_address = myReadLine()
|
|
|
|
client.queryAsync(query:["@type":"setAuthenticationEmailAddress", "email_address":email_address], f:checkAuthenticationError)
|
|
|
|
|
|
|
|
case "authorizationStateWaitEmailCode":
|
|
|
|
var code: String = ""
|
|
|
|
print("Enter email code: ")
|
|
|
|
code = myReadLine()
|
|
|
|
client.queryAsync(query:["@type":"checkAuthenticationEmailCode", "code":["@type":"emailAddressAuthenticationCode", "code":code]], f:checkAuthenticationError)
|
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
case "authorizationStateWaitCode":
|
2018-02-12 18:21:29 +01:00
|
|
|
var code: String = ""
|
2018-02-12 17:28:26 +01:00
|
|
|
print("Enter (SMS) code: ")
|
2018-02-12 14:10:53 +01:00
|
|
|
code = myReadLine()
|
2019-07-16 21:08:34 +02:00
|
|
|
client.queryAsync(query:["@type":"checkAuthenticationCode", "code":code], f:checkAuthenticationError)
|
|
|
|
|
|
|
|
case "authorizationStateWaitRegistration":
|
|
|
|
var first_name: String = ""
|
|
|
|
var last_name: String = ""
|
|
|
|
print("Enter your first name: ")
|
|
|
|
first_name = myReadLine()
|
|
|
|
print("Enter your last name: ")
|
|
|
|
last_name = myReadLine()
|
|
|
|
client.queryAsync(query:["@type":"registerUser", "first_name":first_name, "last_name":last_name], f:checkAuthenticationError)
|
2018-02-12 17:28:26 +01:00
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
case "authorizationStateWaitPassword":
|
|
|
|
print("Enter password: ")
|
|
|
|
let password = myReadLine()
|
2018-02-28 11:25:07 +01:00
|
|
|
client.queryAsync(query:["@type":"checkAuthenticationPassword", "password":password], f:checkAuthenticationError)
|
2018-02-12 17:28:26 +01:00
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
case "authorizationStateReady":
|
|
|
|
()
|
2018-02-12 17:28:26 +01:00
|
|
|
|
2019-12-18 17:23:14 +01:00
|
|
|
case "authorizationStateLoggingOut":
|
|
|
|
print("Logging out...")
|
|
|
|
|
|
|
|
case "authorizationStateClosing":
|
|
|
|
print("Closing...")
|
|
|
|
|
2020-11-14 20:45:18 +01:00
|
|
|
case "authorizationStateClosed":
|
2019-12-18 17:23:14 +01:00
|
|
|
print("Closed.")
|
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
default:
|
2019-12-18 17:23:14 +01:00
|
|
|
assert(false, "TODO: Unexpected authorization state");
|
2018-02-12 17:28:26 +01:00
|
|
|
}
|
2018-02-12 14:10:53 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 11:25:07 +01:00
|
|
|
func checkAuthenticationError(error: Dictionary<String, Any>) {
|
2018-02-12 14:10:53 +01:00
|
|
|
if (error["@type"] as! String == "error") {
|
|
|
|
client.queryAsync(query:["@type":"getAuthorizationState"], f:updateAuthorizationState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client.run {
|
|
|
|
let update = $0
|
|
|
|
print(update)
|
|
|
|
if update["@type"] as! String == "updateAuthorizationState" {
|
2018-02-28 11:25:07 +01:00
|
|
|
updateAuthorizationState(authorizationState: update["authorization_state"] as! Dictionary<String, Any>)
|
2018-02-12 14:10:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while true {
|
|
|
|
sleep(1)
|
|
|
|
}
|