dtchat: download accepted Telegram documents (T8.1)
An incoming messageDocument is remembered with its TDLib file id; file-accept sends downloadFile (priority 1, whole file) and updateFile is followed until local.is_downloading_completed, then the local file is read in 4 KiB blocks and sent as file-data with a file-end carrying the SHA-256 computed with dc_file_sha256. Unknown offers answer file-error. The fake mode can offer a real local file through DTCHAT_TELEGRAM_FAKE_FILE and exercises the same read/hash/chunk/event path; tests/telegram_fake.py now uses a 9000-byte file, checks the offer size and SHA-256, reassembles the chunks byte-for-byte and verifies the file-end hash (25 checks, green stand-alone). The adapter links CRYPTOLIB for EVP/dc_file_sha256. The TDLib downloadFile/updateFile branch is compiled but not exercised without an account. Full build and harness follow.
This commit is contained in:
1 parent
9e8c7cd00c
commit
790669d989
3 files changed
+255
-34
No files matched your search
@@ -45,7 +45,8 @@ dtchat_telegram_CPPFLAGS = -I$(top_srcdir)/include \
|
||||
-I$(top_srcdir)/lib/libDtChat -I$(top_builddir)/lib/libDtChat \
|
||||
$(TDLIB_CPPFLAGS)
|
||||
dtchat_telegram_LDADD = $(top_builddir)/lib/libDtChat/libDtChat.la \
|
||||
$(top_builddir)/lib/libDtChat/libDtChatIpc.la $(TDLIBLIB)
|
||||
$(top_builddir)/lib/libDtChat/libDtChatIpc.la $(TDLIBLIB) \
|
||||
$(CRYPTOLIB)
|
||||
|
||||
if MCDE_GO
|
||||
# whatsmeow is a Go library, so the WhatsApp adapter is a Go program built
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
#include <td/telegram/td_json_client.h>
|
||||
#endif
|
||||
|
||||
#include <openssl/evp.h>
|
||||
|
||||
static char api_id[64] = "";
|
||||
static char api_hash[256] = "";
|
||||
static char phone[64] = "";
|
||||
@@ -40,6 +42,128 @@ 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
|
||||
@@ -218,8 +342,32 @@ send_file_offer(const char *conv, const char *id, const char *name,
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -365,6 +513,9 @@ td_event(const char *json)
|
||||
|
||||
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);
|
||||
@@ -408,6 +559,19 @@ td_event(const char *json)
|
||||
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"));
|
||||
@@ -629,31 +793,72 @@ handle_command(DcJson *cmd)
|
||||
|
||||
if (!online || id == NULL)
|
||||
return;
|
||||
if (fake && strcmp(id, "f1") == 0) {
|
||||
DcJson *data = dc_json_new_object();
|
||||
DcJson *end = dc_json_new_object();
|
||||
const char *sha =
|
||||
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
|
||||
if (fake) {
|
||||
const char *path = getenv("DTCHAT_TELEGRAM_FAKE_FILE");
|
||||
|
||||
(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);
|
||||
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
|
||||
|
||||
+25
-10
@@ -7,7 +7,10 @@ DTCHAT_TELEGRAM_FAKE=1 replaces TDLib with the authorization request and an
|
||||
echo, so the IPC path (hello, configure, connect, request/answer, send,
|
||||
disconnect) is tested without credentials or network.
|
||||
"""
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import queue
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -25,11 +28,16 @@ def check(cond, what):
|
||||
failures.append(what)
|
||||
|
||||
|
||||
PAYLOAD = bytes((i * 37 + 11) & 0xff for i in range(9000))
|
||||
FILE = "/tmp/dtchat-telegram-file.bin"
|
||||
with open(FILE, "wb") as f:
|
||||
f.write(PAYLOAD)
|
||||
proc = subprocess.Popen([ADAPTER, "--stdio"], stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
env={"PATH": "/usr/bin:/bin",
|
||||
"DTCHAT_TELEGRAM_FAKE": "1"})
|
||||
"DTCHAT_TELEGRAM_FAKE": "1",
|
||||
"DTCHAT_TELEGRAM_FAKE_FILE": FILE})
|
||||
q = queue.Queue()
|
||||
|
||||
|
||||
@@ -110,25 +118,31 @@ while time.time() < deadline and (len(roster) < 2 or offer is None):
|
||||
check(roster.get("1", {}).get("name") == "Alice", "roster has Alice")
|
||||
check(roster.get("2", {}).get("name") == "Bob", "roster has Bob")
|
||||
check(roster.get("1", {}).get("online") is True, "Alice is online")
|
||||
check(offer is not None and offer.get("name") == "note.txt",
|
||||
check(offer is not None and offer.get("name") == "dtchat-telegram-file.bin",
|
||||
"a file offer arrives")
|
||||
if offer is not None:
|
||||
check(offer.get("size") == 11, "the offer has the size")
|
||||
check(offer.get("size") == len(PAYLOAD), "the offer has the size")
|
||||
check(offer.get("sha256") == hashlib.sha256(PAYLOAD).hexdigest(),
|
||||
"the offer has the SHA-256")
|
||||
send({"type": "file-accept", "id": offer.get("id")})
|
||||
data = end = None
|
||||
chunks = []
|
||||
end = None
|
||||
deadline = time.time() + 5
|
||||
while time.time() < deadline and (data is None or end is None):
|
||||
while time.time() < deadline:
|
||||
e = ev(1.0)
|
||||
if e is None:
|
||||
continue
|
||||
if e.get("type") == "file-data":
|
||||
data = e
|
||||
chunks.append(base64.b64decode(e.get("data", "")))
|
||||
elif e.get("type") == "file-end":
|
||||
end = e
|
||||
check(data is not None and
|
||||
__import__("base64").b64decode(data.get("data", "")) == b"hello world",
|
||||
"accepted file data")
|
||||
check(end is not None and end.get("size") == 11, "file end")
|
||||
break
|
||||
received = b"".join(chunks)
|
||||
check(received == PAYLOAD, "the downloaded bytes arrive unchanged")
|
||||
check(end is not None and end.get("size") == len(PAYLOAD), "file end size")
|
||||
check(end is not None and
|
||||
end.get("sha256") == hashlib.sha256(PAYLOAD).hexdigest(),
|
||||
"file end SHA-256")
|
||||
|
||||
send({"type": "typing", "conv": "1", "on": True})
|
||||
typing = None
|
||||
@@ -184,6 +198,7 @@ except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
check(False, "exit 0")
|
||||
check(proc.stderr.read().strip() == "", "no stderr output")
|
||||
os.unlink(FILE)
|
||||
|
||||
if failures:
|
||||
print("test_telegram_fake: FAIL (%d)" % len(failures))
|
||||
|
||||
Reference in new issue
Block a user