/* * telegram_adapter.c -- dtchat's Telegram adapter, protocol v1 on stdio * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * The skeleton for T8.1: hello with the settings schema (api_id, api_hash, * phone, database), configure, connect through TDLib's JSON interface, * the authorization requests (code, password), state online/offline, text * messages in both directions and a clean disconnect. Without TDLib the * adapter still answers hello and says in a fatal error that it was built * without it. DTCHAT_TELEGRAM_FAKE=1 replaces TDLib with a tiny flow * (code request, then online, sends echo) so the IPC path can be tested on * a machine without credentials or network. */ #include "dc_internal.h" #include #include #include #include #include #include #include #include #ifdef HAVE_TDLIB #include #endif #include static char api_id[64] = ""; static char api_hash[256] = ""; static char phone[64] = ""; static char database[512] = ""; static int configured; static int online; static int quitting; static int fake; static unsigned int request_seq; static int pending_request; /* id of the open request */ static char pending_kind[32]; static unsigned int client_seq; static void send_json(DcJson *json); #ifdef HAVE_TDLIB /* an incoming document: the offer id shown to the user and the TDLib file * id that downloadFile takes */ #define MAX_PENDING 8 typedef struct { int used; char offer[32]; int file_id; char name[256]; long long size; } PendingDoc; static PendingDoc pending_docs[MAX_PENDING]; static PendingDoc * pending_by_offer(const char *offer) { int i; for (i = 0; i < MAX_PENDING; i++) if (pending_docs[i].used && strcmp(pending_docs[i].offer, offer) == 0) return &pending_docs[i]; return NULL; } static PendingDoc * pending_by_file(int file_id) { int i; for (i = 0; i < MAX_PENDING; i++) if (pending_docs[i].used && pending_docs[i].file_id == file_id) return &pending_docs[i]; return NULL; } static PendingDoc * pending_add(const char *offer, int file_id, const char *name, long long size) { int i; PendingDoc *p = pending_by_offer(offer); if (p == NULL) { for (i = 0; i < MAX_PENDING; i++) if (!pending_docs[i].used) { p = &pending_docs[i]; break; } } if (p == NULL) return NULL; memset(p, 0, sizeof(*p)); p->used = 1; snprintf(p->offer, sizeof(p->offer), "%s", offer); p->file_id = file_id; snprintf(p->name, sizeof(p->name), "%s", name != NULL ? name : "file"); p->size = size; return p; } #endif /* HAVE_TDLIB */ /* read PATH in 4 KiB blocks and emit file-data/file-end (with its hash) */ static void emit_file_from_path(const char *offer, const char *path) { unsigned char data[4096]; char b64[4 * ((4096 + 2) / 3) + 1]; FILE *fp; size_t n; long long total = 0; char sha[65]; fp = fopen(path, "rb"); if (fp == NULL) { DcJson *json = dc_json_new_object(); (void)dc_json_object_set(json, "type", dc_json_new_string("file-error")); (void)dc_json_object_set(json, "id", dc_json_new_string(offer)); (void)dc_json_object_set(json, "text", dc_json_new_string("cannot read the downloaded file")); send_json(json); return; } while ((n = fread(data, 1, sizeof(data), fp)) > 0) { DcJson *json = dc_json_new_object(); EVP_EncodeBlock((unsigned char *)b64, data, (int)n); (void)dc_json_object_set(json, "type", dc_json_new_string("file-data")); (void)dc_json_object_set(json, "id", dc_json_new_string(offer)); (void)dc_json_object_set(json, "data", dc_json_new_string(b64)); (void)dc_json_object_set(json, "size", dc_json_new_int((long long)n)); send_json(json); total += (long long)n; } fclose(fp); if (dc_file_sha256(path, sha, NULL) != 0) sha[0] = '\0'; { DcJson *json = dc_json_new_object(); (void)dc_json_object_set(json, "type", dc_json_new_string("file-end")); (void)dc_json_object_set(json, "id", dc_json_new_string(offer)); (void)dc_json_object_set(json, "size", dc_json_new_int(total)); if (sha[0] != '\0') (void)dc_json_object_set(json, "sha256", dc_json_new_string(sha)); send_json(json); } } #ifdef HAVE_TDLIB static void *td; #endif static void send_json(DcJson *json) { size_t len = 0; char *text; if (json == NULL) return; text = dc_json_write(json, &len); if (text != NULL) { (void)dc_frame_send(STDOUT_FILENO, text, len); free(text); } dc_json_free(json); } static void send_state(const char *state, const char *detail) { DcJson *json = dc_json_new_object(); (void)dc_json_object_set(json, "type", dc_json_new_string("state")); (void)dc_json_object_set(json, "state", dc_json_new_string(state)); if (detail != NULL && *detail != '\0') (void)dc_json_object_set(json, "detail", dc_json_new_string(detail)); send_json(json); } static void send_error(const char *code, const char *text, int fatal) { DcJson *json = dc_json_new_object(); (void)dc_json_object_set(json, "type", dc_json_new_string("error")); (void)dc_json_object_set(json, "code", dc_json_new_string(code)); (void)dc_json_object_set(json, "text", dc_json_new_string(text)); (void)dc_json_object_set(json, "fatal", dc_json_new_bool(fatal)); send_json(json); } static void send_bye(const char *reason) { DcJson *json = dc_json_new_object(); (void)dc_json_object_set(json, "type", dc_json_new_string("bye")); (void)dc_json_object_set(json, "reason", dc_json_new_string(reason)); send_json(json); } static void send_hello(void) { DcJson *hello = dc_json_new_object(); DcJson *caps = dc_json_new_array(); DcJson *settings = dc_json_new_array(); static const char *const capnames[] = { "text", "files", "qr" }; for (size_t i = 0; i < sizeof(capnames) / sizeof(capnames[0]); i++) (void)dc_json_array_append(caps, dc_json_new_string(capnames[i])); #define FIELD(key, label, type) do { \ DcJson *f = dc_json_new_object(); \ (void)dc_json_object_set(f, "key", dc_json_new_string(key)); \ (void)dc_json_object_set(f, "label", dc_json_new_string(label)); \ (void)dc_json_object_set(f, "type", dc_json_new_string(type)); \ (void)dc_json_array_append(settings, f); \ } while (0) FIELD("api_id", "API ID", "int"); FIELD("api_hash", "API hash", "secret"); FIELD("phone", "Phone number", "text"); FIELD("database", "State directory", "text"); (void)dc_json_object_set(hello, "type", dc_json_new_string("hello")); (void)dc_json_object_set(hello, "ipc", dc_json_new_int(DC_IPC_VERSION)); (void)dc_json_object_set(hello, "protocol", dc_json_new_string("telegram")); (void)dc_json_object_set(hello, "version", dc_json_new_string("0.1")); (void)dc_json_object_set(hello, "caps", caps); (void)dc_json_object_set(hello, "settings", settings); send_json(hello); #undef FIELD } static void ask(const char *kind, const char *title, const char *prompt, int secret) { DcJson *json = dc_json_new_object(); pending_request = (int)++request_seq; snprintf(pending_kind, sizeof(pending_kind), "%s", kind); (void)dc_json_object_set(json, "type", dc_json_new_string("request")); (void)dc_json_object_set(json, "id", dc_json_new_int(pending_request)); (void)dc_json_object_set(json, "kind", dc_json_new_string(kind)); (void)dc_json_object_set(json, "title", dc_json_new_string(title)); (void)dc_json_object_set(json, "prompt", dc_json_new_string(prompt)); (void)dc_json_object_set(json, "secret", dc_json_new_bool(secret)); send_json(json); } static void send_text(const char *conv, const char *text, const char *client_id) { DcJson *sent = dc_json_new_object(); DcJson *message = dc_json_new_object(); (void)dc_json_object_set(sent, "type", dc_json_new_string("sent")); (void)dc_json_object_set(sent, "conv", dc_json_new_string(conv)); if (client_id != NULL) (void)dc_json_object_set(sent, "client_id", dc_json_new_string(client_id)); (void)dc_json_object_set(sent, "ts", dc_json_new_int(time(NULL))); send_json(sent); (void)dc_json_object_set(message, "type", dc_json_new_string("message")); (void)dc_json_object_set(message, "conv", dc_json_new_string(conv)); (void)dc_json_object_set(message, "from", dc_json_new_string("telegram")); (void)dc_json_object_set(message, "kind", dc_json_new_string("text")); (void)dc_json_object_set(message, "text", dc_json_new_string(text)); (void)dc_json_object_set(message, "ts", dc_json_new_int(time(NULL))); send_json(message); } static void send_roster(const char *id, const char *name, int online) { DcJson *json = dc_json_new_object(); DcJson *contact = dc_json_new_object(); (void)dc_json_object_set(contact, "id", dc_json_new_string(id)); (void)dc_json_object_set(contact, "name", dc_json_new_string(name)); (void)dc_json_object_set(contact, "online", dc_json_new_bool(online)); (void)dc_json_object_set(contact, "subscription", dc_json_new_string("both")); (void)dc_json_object_set(json, "type", dc_json_new_string("roster")); (void)dc_json_object_set(json, "contact", contact); send_json(json); } static void send_typing(const char *conv, int on, const char *who) { DcJson *json = dc_json_new_object(); (void)dc_json_object_set(json, "type", dc_json_new_string("typing")); (void)dc_json_object_set(json, "conv", dc_json_new_string(conv)); (void)dc_json_object_set(json, "on", dc_json_new_bool(on)); (void)dc_json_object_set(json, "who", dc_json_new_string(who != NULL ? who : "telegram")); send_json(json); } static void send_file_offer(const char *conv, const char *id, const char *name, long long size) { DcJson *json = dc_json_new_object(); (void)dc_json_object_set(json, "type", dc_json_new_string("file-offer")); (void)dc_json_object_set(json, "conv", dc_json_new_string(conv)); (void)dc_json_object_set(json, "id", dc_json_new_string(id)); (void)dc_json_object_set(json, "name", dc_json_new_string(name)); (void)dc_json_object_set(json, "size", dc_json_new_int(size)); send_json(json); } static void fake_files(void) { const char *path = getenv("DTCHAT_TELEGRAM_FAKE_FILE"); char sha[65]; long long size = 0; send_roster("1", "Alice", 1); send_roster("2", "Bob", 0); if (path != NULL && dc_file_sha256(path, sha, &size) == 0) { const char *name = strrchr(path, '/') != NULL ? strrchr(path, '/') + 1 : path; DcJson *json = dc_json_new_object(); (void)dc_json_object_set(json, "type", dc_json_new_string("file-offer")); (void)dc_json_object_set(json, "conv", dc_json_new_string("1")); (void)dc_json_object_set(json, "id", dc_json_new_string("f1")); (void)dc_json_object_set(json, "name", dc_json_new_string(name)); (void)dc_json_object_set(json, "size", dc_json_new_int(size)); (void)dc_json_object_set(json, "sha256", dc_json_new_string(sha)); send_json(json); return; } send_file_offer("1", "f1", "note.txt", 11); } /* ------------------------------- TDLib ---------------------------------- */ #ifdef HAVE_TDLIB static void td_request(DcJson *request) { size_t len = 0; char *text = dc_json_write(request, &len); dc_json_free(request); if (text == NULL) return; td_json_client_send(td, text); free(text); } static void td_start(void) { DcJson *request; DcJson *params; if (api_id[0] == '\0' || api_hash[0] == '\0') { send_error("config", "api_id and api_hash are required", 0); send_state("offline", NULL); return; } td = td_json_client_create(); if (td == NULL) { send_error("config", "cannot create the TDLib client", 1); return; } request = dc_json_new_object(); params = dc_json_new_object(); (void)dc_json_object_set(request, "@type", dc_json_new_string("setTdlibParameters")); (void)dc_json_object_set(params, "database_directory", dc_json_new_string(database[0] != '\0' ? database : ".")); (void)dc_json_object_set(params, "files_directory", dc_json_new_string(database[0] != '\0' ? database : ".")); (void)dc_json_object_set(params, "use_message_database", dc_json_new_bool(1)); (void)dc_json_object_set(params, "use_secret_chats", dc_json_new_bool(0)); (void)dc_json_object_set(params, "api_id", dc_json_new_int(strtol(api_id, NULL, 10))); (void)dc_json_object_set(params, "api_hash", dc_json_new_string(api_hash)); (void)dc_json_object_set(params, "system_language_code", dc_json_new_string("en")); (void)dc_json_object_set(params, "device_model", dc_json_new_string("dtchat")); (void)dc_json_object_set(params, "application_version", dc_json_new_string("1.0")); (void)dc_json_object_set(request, "parameters", params); td_request(request); send_state("connecting", "Telegram"); } static void td_authorization(const char *state) { DcJson *request; if (strcmp(state, "authorizationStateReady") == 0) { online = 1; send_state("online", "Telegram"); return; } if (strcmp(state, "authorizationStateWaitPhoneNumber") == 0) { if (phone[0] == '\0') { ask("text", "Telegram", "Phone number", 0); snprintf(pending_kind, sizeof(pending_kind), "phone"); return; } request = dc_json_new_object(); (void)dc_json_object_set(request, "@type", dc_json_new_string("setAuthenticationPhoneNumber")); (void)dc_json_object_set(request, "phone_number", dc_json_new_string(phone)); td_request(request); return; } if (strcmp(state, "authorizationStateWaitCode") == 0) { ask("code", "Telegram", "Code from the Telegram app", 1); return; } if (strcmp(state, "authorizationStateWaitPassword") == 0) { ask("password", "Telegram", "Two-step verification password", 1); return; } if (strcmp(state, "authorizationStateClosed") == 0) { online = 0; send_state("offline", NULL); send_bye("closed"); _exit(0); } } static void td_event(const char *json) { DcJson *event = dc_json_parse(json, strlen(json), NULL, 0); const char *type; if (event == NULL) return; type = dc_json_string(dc_json_get(event, "@type")); if (type == NULL) { dc_json_free(event); return; } if (strcmp(type, "updateAuthorizationState") == 0) { const DcJson *auth = dc_json_get(event, "authorization_state"); const char *state = dc_json_string(dc_json_get(auth, "@type")); if (state != NULL) td_authorization(state); } else if (strcmp(type, "updateNewMessage") == 0) { const DcJson *message = dc_json_get(event, "message"); const DcJson *content = dc_json_get(message, "content"); const DcJson *textobj = dc_json_get(content, "text"); const char *text = dc_json_string(dc_json_get(textobj, "text")); long long chat = dc_json_int(dc_json_get(message, "chat_id")); const char *ctype = dc_json_string(dc_json_get(content, "@type")); char conv[32]; if (ctype != NULL && strcmp(ctype, "messageDocument") == 0) { const DcJson *doc = dc_json_get(content, "document"); const char *fname = dc_json_string(dc_json_get(doc, "file_name")); const DcJson *file = dc_json_get(doc, "document"); long long fsize = dc_json_int(dc_json_get(file, "size")); long long mid = dc_json_int(dc_json_get(message, "id")); char id[32]; snprintf(conv, sizeof(conv), "%lld", chat); snprintf(id, sizeof(id), "%lld", mid); (void)pending_add(id, (int)dc_json_int(dc_json_get(file, "id")), fname != NULL ? fname : "document", fsize); send_file_offer(conv, id, fname != NULL ? fname : "document", fsize); dc_json_free(event); return; } if (text != NULL) { DcJson *out = dc_json_new_object(); snprintf(conv, sizeof(conv), "%lld", chat); (void)dc_json_object_set(out, "type", dc_json_new_string("message")); (void)dc_json_object_set(out, "conv", dc_json_new_string(conv)); (void)dc_json_object_set(out, "from", dc_json_new_string("telegram")); (void)dc_json_object_set(out, "kind", dc_json_new_string("text")); (void)dc_json_object_set(out, "text", dc_json_new_string(text)); (void)dc_json_object_set(out, "ts", dc_json_new_int(dc_json_int(dc_json_get( message, "date")))); send_json(out); } } else if (strcmp(type, "updateUser") == 0) { const DcJson *user = dc_json_get(event, "user"); long long id = dc_json_int(dc_json_get(user, "id")); const char *first = dc_json_string(dc_json_get(user, "first_name")); const char *last = dc_json_string(dc_json_get(user, "last_name")); const DcJson *status = dc_json_get(user, "status"); const char *stype = dc_json_string(dc_json_get(status, "@type")); char who[32]; char name[256]; snprintf(who, sizeof(who), "%lld", id); snprintf(name, sizeof(name), "%s%s%s", first != NULL ? first : "", last != NULL ? " " : "", last != NULL ? last : ""); send_roster(who, name, stype != NULL && strcmp(stype, "userStatusOnline") == 0); } else if (strcmp(type, "updateFile") == 0) { const DcJson *file = dc_json_get(event, "file"); const DcJson *local = dc_json_get(file, "local"); int fid = (int)dc_json_int(dc_json_get(file, "id")); int complete = dc_json_bool(dc_json_get(local, "is_downloading_completed")); const char *path = dc_json_string(dc_json_get(local, "path")); PendingDoc *p = pending_by_file(fid); if (p != NULL && complete && path != NULL && *path != '\0') { emit_file_from_path(p->offer, path); p->used = 0; } } else if (strcmp(type, "error") == 0) { const char *text = dc_json_string(dc_json_get(event, "message")); send_error("telegram", text != NULL ? text : "TDLib reported an error", 0); } dc_json_free(event); } static void td_pump(void) { const char *json; int i; for (i = 0; i < 8; i++) { json = td_json_client_receive(td, 0.02); if (json == NULL) break; td_event(json); } } #else static void td_start(void) { send_error("build", "this build has no TDLib; install libtd-dev and " "rebuild dtchat-telegram", 1); send_state("offline", NULL); } #endif /* HAVE_TDLIB */ /* ------------------------------ commands -------------------------------- */ static void handle_command(DcJson *cmd) { const char *type = dc_json_string(dc_json_get(cmd, "type")); if (type == NULL) return; if (strcmp(type, "configure") == 0) { const DcJson *settings = dc_json_get(cmd, "settings"); const DcJson *secrets = dc_json_get(cmd, "secrets"); const char *v; if (dc_json_get(settings, "api_id") != NULL) snprintf(api_id, sizeof(api_id), "%lld", dc_json_int(dc_json_get(settings, "api_id"))); v = dc_json_string(dc_json_get(settings, "api_hash")); if (v == NULL) v = dc_json_string(dc_json_get(secrets, "api_hash")); if (v != NULL) snprintf(api_hash, sizeof(api_hash), "%s", v); v = dc_json_string(dc_json_get(settings, "phone")); if (v != NULL) snprintf(phone, sizeof(phone), "%s", v); v = dc_json_string(dc_json_get(settings, "database")); if (v != NULL) snprintf(database, sizeof(database), "%s", v); configured = 1; } else if (strcmp(type, "connect") == 0) { if (!configured) { send_error("config", "configure first", 0); send_state("offline", NULL); return; } if (fake) { send_state("connecting", "Telegram (fake)"); ask("code", "Telegram", "Code (fake: anything)", 1); } else { td_start(); } } else if (strcmp(type, "answer") == 0) { const char *value = dc_json_string(dc_json_get(cmd, "value")); int id = (int)dc_json_int(dc_json_get(cmd, "id")); if (id != pending_request || value == NULL) return; pending_request = 0; if (fake) { if (strcmp(pending_kind, "code") == 0) { online = 1; send_state("online", "Telegram (fake)"); fake_files(); } return; } #ifdef HAVE_TDLIB if (td != NULL) { DcJson *request = dc_json_new_object(); if (strcmp(pending_kind, "code") == 0) (void)dc_json_object_set(request, "@type", dc_json_new_string( "checkAuthenticationCode")); else if (strcmp(pending_kind, "password") == 0) (void)dc_json_object_set(request, "@type", dc_json_new_string( "checkAuthenticationPassword")); else if (strcmp(pending_kind, "phone") == 0) (void)dc_json_object_set(request, "@type", dc_json_new_string( "setAuthenticationPhoneNumber")); else (void)dc_json_object_set(request, "@type", dc_json_new_string("checkAuthenticationCode")); (void)dc_json_object_set(request, "code", dc_json_new_string(value)); (void)dc_json_object_set(request, "password", dc_json_new_string(value)); (void)dc_json_object_set(request, "phone_number", dc_json_new_string(value)); td_request(request); } #endif } else if (strcmp(type, "send") == 0) { const char *conv = dc_json_string(dc_json_get(cmd, "conv")); const char *text = dc_json_string(dc_json_get(cmd, "text")); const char *client_id = dc_json_string(dc_json_get(cmd, "client_id")); if (!online || conv == NULL || text == NULL) return; if (fake) { send_text(conv, text, client_id); return; } #ifdef HAVE_TDLIB if (td != NULL) { DcJson *request = dc_json_new_object(); DcJson *content = dc_json_new_object(); DcJson *formatted = dc_json_new_object(); (void)dc_json_object_set(request, "@type", dc_json_new_string("sendMessage")); (void)dc_json_object_set(request, "chat_id", dc_json_new_int(strtoll(conv, NULL, 10))); (void)dc_json_object_set(content, "@type", dc_json_new_string("inputMessageText")); (void)dc_json_object_set(formatted, "text", dc_json_new_string(text)); (void)dc_json_object_set(content, "text", formatted); (void)dc_json_object_set(request, "input_message_content", content); td_request(request); } #endif (void)client_seq; } else if (strcmp(type, "typing") == 0) { const char *conv = dc_json_string(dc_json_get(cmd, "conv")); int on = dc_json_bool(dc_json_get(cmd, "on")); if (!online || conv == NULL) return; if (fake) { send_typing(conv, on, "alice"); return; } #ifdef HAVE_TDLIB if (td != NULL) { DcJson *request = dc_json_new_object(); DcJson *action = dc_json_new_object(); (void)dc_json_object_set(request, "@type", dc_json_new_string("sendChatAction")); (void)dc_json_object_set(request, "chat_id", dc_json_new_int(strtoll(conv, NULL, 10))); (void)dc_json_object_set(action, "@type", dc_json_new_string(on ? "chatActionTyping" : "chatActionCancel")); (void)dc_json_object_set(request, "action", action); td_request(request); } #endif } else if (strcmp(type, "file-send") == 0) { const char *conv = dc_json_string(dc_json_get(cmd, "conv")); const char *path = dc_json_string(dc_json_get(cmd, "path")); const char *id = dc_json_string(dc_json_get(cmd, "id")); if (!online || conv == NULL || path == NULL || id == NULL) return; if (fake) { DcJson *json = dc_json_new_object(); (void)dc_json_object_set(json, "type", dc_json_new_string("file-sent")); (void)dc_json_object_set(json, "id", dc_json_new_string(id)); (void)dc_json_object_set(json, "size", dc_json_new_int(0)); send_json(json); return; } #ifdef HAVE_TDLIB if (td != NULL) { DcJson *request = dc_json_new_object(); DcJson *doc = dc_json_new_object(); DcJson *file = dc_json_new_object(); (void)dc_json_object_set(request, "@type", dc_json_new_string("sendDocument")); (void)dc_json_object_set(request, "chat_id", dc_json_new_int(strtoll(conv, NULL, 10))); (void)dc_json_object_set(doc, "@type", dc_json_new_string("inputMessageDocument")); (void)dc_json_object_set(file, "@type", dc_json_new_string("inputFileLocal")); (void)dc_json_object_set(file, "path", dc_json_new_string(path)); (void)dc_json_object_set(doc, "document", file); (void)dc_json_object_set(request, "document", doc); td_request(request); } #endif } else if (strcmp(type, "file-accept") == 0) { const char *id = dc_json_string(dc_json_get(cmd, "id")); if (!online || id == NULL) return; if (fake) { const char *path = getenv("DTCHAT_TELEGRAM_FAKE_FILE"); if (path != NULL) { emit_file_from_path(id, path); } else if (strcmp(id, "f1") == 0) { DcJson *data = dc_json_new_object(); DcJson *end = dc_json_new_object(); const char *sha = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"; (void)dc_json_object_set(data, "type", dc_json_new_string("file-data")); (void)dc_json_object_set(data, "id", dc_json_new_string(id)); (void)dc_json_object_set(data, "data", dc_json_new_string("aGVsbG8gd29ybGQ=")); (void)dc_json_object_set(data, "size", dc_json_new_int(11)); send_json(data); (void)dc_json_object_set(end, "type", dc_json_new_string("file-end")); (void)dc_json_object_set(end, "id", dc_json_new_string(id)); (void)dc_json_object_set(end, "size", dc_json_new_int(11)); (void)dc_json_object_set(end, "sha256", dc_json_new_string(sha)); send_json(end); } return; } #ifdef HAVE_TDLIB { PendingDoc *p = pending_by_offer(id); if (p != NULL && td != NULL) { DcJson *request = dc_json_new_object(); (void)dc_json_object_set(request, "@type", dc_json_new_string("downloadFile")); (void)dc_json_object_set(request, "file_id", dc_json_new_int(p->file_id)); (void)dc_json_object_set(request, "priority", dc_json_new_int(1)); (void)dc_json_object_set(request, "offset", dc_json_new_int(0)); (void)dc_json_object_set(request, "limit", dc_json_new_int(0)); (void)dc_json_object_set(request, "synchronous", dc_json_new_bool(0)); td_request(request); } else { DcJson *json = dc_json_new_object(); (void)dc_json_object_set(json, "type", dc_json_new_string("file-error")); (void)dc_json_object_set(json, "id", dc_json_new_string(id)); (void)dc_json_object_set(json, "text", dc_json_new_string( "no such document")); send_json(json); } } #endif } else if (strcmp(type, "disconnect") == 0) { quitting = 1; #ifdef HAVE_TDLIB if (td != NULL) td_json_client_destroy(td); #endif send_bye("requested"); _exit(0); } } int main(int argc, char **argv) { DcFrameBuf frame; char buf[4096]; int fl; (void)argc; (void)argv; signal(SIGPIPE, SIG_IGN); fake = getenv("DTCHAT_TELEGRAM_FAKE") != NULL; dc_frame_init(&frame); send_hello(); fl = fcntl(STDIN_FILENO, F_GETFL, 0); if (fl >= 0) (void)fcntl(STDIN_FILENO, F_SETFL, fl | O_NONBLOCK); for (;;) { ssize_t n; #ifdef HAVE_TDLIB if (td != NULL) td_pump(); else #endif { struct timespec pause; pause.tv_sec = 0; pause.tv_nsec = 20 * 1000 * 1000; (void)nanosleep(&pause, NULL); } for (;;) { n = read(STDIN_FILENO, buf, sizeof(buf)); if (n < 0) { if (errno == EINTR) continue; break; } if (n == 0) goto out; if (dc_frame_feed(&frame, buf, (size_t)n) != 0) goto out; for (;;) { size_t len = 0; char *line = dc_frame_next(&frame, &len); DcJson *cmd; if (line == NULL) break; cmd = dc_json_parse(line, len, NULL, 0); free(line); if (cmd != NULL) { handle_command(cmd); dc_json_free(cmd); } } } } out: #ifdef HAVE_TDLIB if (td != NULL) td_json_client_destroy(td); #endif dc_frame_free(&frame); return 0; }