summaryrefslogtreecommitdiff
path: root/protocols
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2023-11-02 18:22:11 +0300
committerGeorge Hazan <george.hazan@gmail.com>2023-11-02 18:22:11 +0300
commitba6089e39ca84e0b17978046565c263303a4802a (patch)
treeef55ecf016c5bfbfa7a4f14e6b5cc2da4f22b2c3 /protocols
parent310576eb7a687c687ecba4e19d11806703e94566 (diff)
Jabber: fix for copying URLs
Diffstat (limited to 'protocols')
-rw-r--r--protocols/JabberG/src/jabber_file.cpp119
1 files changed, 61 insertions, 58 deletions
diff --git a/protocols/JabberG/src/jabber_file.cpp b/protocols/JabberG/src/jabber_file.cpp
index 01f04bb01f..e4fd827a81 100644
--- a/protocols/JabberG/src/jabber_file.cpp
+++ b/protocols/JabberG/src/jabber_file.cpp
@@ -31,71 +31,74 @@ void __cdecl CJabberProto::OfflineFileThread(OFDTHREAD *ofd)
if (m_bJabberOnline && dbei && !strcmp(dbei.szModule, m_szModuleName) && dbei.eventType == EVENTTYPE_FILE) {
DB::FILE_BLOB blob(dbei);
if (const char *url = blob.getUrl()) {
- bool encrypted = false;
- // OMEMO encryped file?
- char protocol[7], hexkey[100], suburl[5001], newurl[5001];
- int ret = sscanf(url, "%6[^:]://%5000[^#]#%88s", protocol, suburl, hexkey);
- protocol[6] = hexkey[88] = 0;
- if (ret == 3 && !strcmp(protocol, "aesgcm") && strlen(hexkey) == 88) {
- mir_snprintf(newurl, "https://%s", suburl);
- url = newurl;
- encrypted = true;
- }
- else if (ret != 2 || (strcmp(protocol, "https") && strcmp(protocol, "http"))) {
- debugLogA("Wrong url");
- delete ofd;
- return;
- }
+ if (!ofd->bCopy) {
+ bool encrypted = false;
+ // OMEMO encryped file?
+ char protocol[7], hexkey[100], suburl[5001], newurl[5001];
+ int ret = sscanf(url, "%6[^:]://%5000[^#]#%88s", protocol, suburl, hexkey);
+ protocol[6] = hexkey[88] = 0;
+ if (ret == 3 && !strcmp(protocol, "aesgcm") && strlen(hexkey) == 88) {
+ mir_snprintf(newurl, "https://%s", suburl);
+ url = newurl;
+ encrypted = true;
+ }
+ else if (ret != 2 || (strcmp(protocol, "https") && strcmp(protocol, "http"))) {
+ debugLogA("Wrong url");
+ delete ofd;
+ return;
+ }
- // initialize the netlib request
- NETLIBHTTPREQUEST nlhr = {};
- nlhr.cbSize = sizeof(nlhr);
- nlhr.requestType = REQUEST_GET;
- nlhr.flags = NLHRF_HTTP11 | NLHRF_DUMPASTEXT | NLHRF_REDIRECT;
- nlhr.szUrl = (char*)url;
-
- // download the page
- NLHR_PTR nlhrReply(Netlib_HttpTransaction(m_hNetlibUser, &nlhr));
- if (nlhrReply && nlhrReply->resultCode == 200) {
- FILE *f = _wfopen(ofd->wszPath, L"wb");
- size_t written = 0;
- if (f) {
- if (encrypted) {
- int payload_len = nlhrReply->dataLength - 16;
- if (payload_len > 0) {
- uint8_t ivkey[44];
- hex2bin(hexkey, ivkey, 44);
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
- EVP_DecryptInit(ctx, EVP_aes_256_gcm(), ivkey + 12, ivkey);
- EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, nlhrReply->pData + payload_len);
-
- int outl = 0, round_len = 0;
- uint8_t *out = (uint8_t *)mir_alloc(payload_len);
- EVP_DecryptUpdate(ctx, out, &outl, (uint8_t *)nlhrReply->pData, (int)payload_len);
- int dec_success = EVP_DecryptFinal(ctx, out + outl, &round_len);
- outl += round_len;
- EVP_CIPHER_CTX_free(ctx);
- if (dec_success && outl == payload_len)
- if (fwrite(out, 1, payload_len, f) == size_t(payload_len))
- written = payload_len;
- mir_free(out);
+ // initialize the netlib request
+ NETLIBHTTPREQUEST nlhr = {};
+ nlhr.cbSize = sizeof(nlhr);
+ nlhr.requestType = REQUEST_GET;
+ nlhr.flags = NLHRF_HTTP11 | NLHRF_DUMPASTEXT | NLHRF_REDIRECT;
+ nlhr.szUrl = (char *)url;
+
+ // download the page
+ NLHR_PTR nlhrReply(Netlib_HttpTransaction(m_hNetlibUser, &nlhr));
+ if (nlhrReply && nlhrReply->resultCode == 200) {
+ FILE *f = _wfopen(ofd->wszPath, L"wb");
+ size_t written = 0;
+ if (f) {
+ if (encrypted) {
+ int payload_len = nlhrReply->dataLength - 16;
+ if (payload_len > 0) {
+ uint8_t ivkey[44];
+ hex2bin(hexkey, ivkey, 44);
+ EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
+ EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 12, NULL);
+ EVP_DecryptInit(ctx, EVP_aes_256_gcm(), ivkey + 12, ivkey);
+ EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, nlhrReply->pData + payload_len);
+
+ int outl = 0, round_len = 0;
+ uint8_t *out = (uint8_t *)mir_alloc(payload_len);
+ EVP_DecryptUpdate(ctx, out, &outl, (uint8_t *)nlhrReply->pData, (int)payload_len);
+ int dec_success = EVP_DecryptFinal(ctx, out + outl, &round_len);
+ outl += round_len;
+ EVP_CIPHER_CTX_free(ctx);
+ if (dec_success && outl == payload_len)
+ if (fwrite(out, 1, payload_len, f) == size_t(payload_len))
+ written = payload_len;
+ mir_free(out);
+ }
}
+ else if (fwrite(nlhrReply->pData, 1, nlhrReply->dataLength, f) == size_t(nlhrReply->dataLength))
+ written = nlhrReply->dataLength;
+ fclose(f);
}
- else if (fwrite(nlhrReply->pData, 1, nlhrReply->dataLength, f) == size_t(nlhrReply->dataLength))
- written = nlhrReply->dataLength;
- fclose(f);
- }
- if (written) {
- DBVARIANT dbv = { DBVT_DWORD };
- dbv.dVal = (DWORD)written;
- db_event_setJson(ofd->hDbEvent, "ft", &dbv);
- db_event_setJson(ofd->hDbEvent, "fs", &dbv);
+ if (written) {
+ DBVARIANT dbv = { DBVT_DWORD };
+ dbv.dVal = (DWORD)written;
+ db_event_setJson(ofd->hDbEvent, "ft", &dbv);
+ db_event_setJson(ofd->hDbEvent, "fs", &dbv);
- ofd->Finish();
+ ofd->Finish();
+ }
}
}
+ else ofd->pCallback->Invoke(*ofd);
}
}