2018-03-11 01:43:55 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// 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 {
|
|
|
|
typealias Client = UnsafeMutableRawPointer
|
|
|
|
var client = td_json_client_create()!
|
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
|
|
|
}
|
|
|
|
td_json_client_send(self.client, to_json(newQuery))
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
td_json_client_destroy(client)
|
|
|
|
}
|
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 {
|
2018-02-12 18:21:29 +01:00
|
|
|
if let res = td_json_client_receive(s.client, 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
|
|
|
|
|
|
|
|
|
|
|
// An example of usage
|
|
|
|
td_set_log_verbosity_level(1);
|
|
|
|
|
|
|
|
var client = TdClient()
|
|
|
|
|
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",
|
|
|
|
"parameters":[
|
2018-02-26 10:41:12 +01:00
|
|
|
"database_directory":"tdlib",
|
2018-02-12 14:10:53 +01:00
|
|
|
"use_message_database":true,
|
|
|
|
"use_secret_chats":true,
|
|
|
|
"api_id":94575,
|
|
|
|
"api_hash":"a3406de8d171bb422bb6ddf3bbd800e2",
|
|
|
|
"system_language_code":"en",
|
|
|
|
"device_model":"Desktop",
|
|
|
|
"system_version":"Unknown",
|
|
|
|
"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 "authorizationStateWaitEncryptionKey":
|
|
|
|
client.queryAsync(query: ["@type":"checkDatabaseEncryptionKey", "key":"cucumber"])
|
2018-02-12 18:21:29 +01:00
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
case "authorizationStateWaitPhoneNumber":
|
|
|
|
print("Enter your phone: ")
|
|
|
|
let phone = myReadLine()
|
2018-02-28 11:25:07 +01:00
|
|
|
client.queryAsync(query:["@type":"setAuthenticationPhoneNumber", "phone_number":phone], f:checkAuthenticationError)
|
2018-02-12 18:21:29 +01:00
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
case "authorizationStateWaitCode":
|
2018-02-12 18:21:29 +01:00
|
|
|
var first_name: String = ""
|
2018-02-12 14:10:53 +01:00
|
|
|
var last_name: String = ""
|
2018-02-12 18:21:29 +01:00
|
|
|
var code: String = ""
|
2018-02-28 11:25:07 +01:00
|
|
|
if let is_registered = authorizationState["is_registered"] as? Bool, is_registered {
|
2018-02-12 14:10:53 +01:00
|
|
|
} else {
|
2018-02-12 18:21:29 +01:00
|
|
|
print("Enter your first name: ")
|
2018-02-12 14:10:53 +01:00
|
|
|
first_name = myReadLine()
|
|
|
|
print("Enter your last name: ")
|
|
|
|
last_name = myReadLine()
|
|
|
|
}
|
2018-02-12 17:28:26 +01:00
|
|
|
print("Enter (SMS) code: ")
|
2018-02-12 14:10:53 +01:00
|
|
|
code = myReadLine()
|
2018-02-28 11:25:07 +01:00
|
|
|
client.queryAsync(query:["@type":"checkAuthenticationCode", "code":code, "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
|
|
|
|
2018-02-12 14:10:53 +01:00
|
|
|
default:
|
2018-02-12 17:28:26 +01:00
|
|
|
assert(false, "TODO: Unknown authorization state");
|
|
|
|
}
|
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)
|
|
|
|
}
|