From 5d151da574c4567b1ba1ee6f4e21043866c4ee00 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 10 Jun 2025 14:47:38 +0300 Subject: Jabber: duplicate code removed --- protocols/JabberG/src/jabber_file.cpp | 34 ++++++++++---------------- protocols/JabberG/src/jabber_omemo.cpp | 42 +++++++-------------------------- protocols/JabberG/src/jabber_proto.h | 2 +- protocols/JabberG/src/jabber_thread.cpp | 24 ++++++++++++------- 4 files changed, 38 insertions(+), 64 deletions(-) (limited to 'protocols') diff --git a/protocols/JabberG/src/jabber_file.cpp b/protocols/JabberG/src/jabber_file.cpp index c43f1509d7..317e67ad52 100644 --- a/protocols/JabberG/src/jabber_file.cpp +++ b/protocols/JabberG/src/jabber_file.cpp @@ -148,37 +148,29 @@ void __cdecl CJabberProto::FileReceiveHttpThread(filetransfer *ft) delete ft; } -void CJabberProto::FileProcessHttpDownload(MCONTACT hContact, const char *jid, const char *pszUrl, const char *pszDescr) +void CJabberProto::FileProcessHttpDownload(DB::EventInfo &dbei, const char *pszUrl, const char *pszDescr) { // create incoming file transfer instead of a writing message + dbei.eventType = EVENTTYPE_FILE; + CMStringA szName; const char *b = strrchr(pszUrl, '/') + 1; while (*b != 0 && *b != '#' && *b != '?') szName.AppendChar(*b++); mir_urlDecode(szName.GetBuffer()); - MHttpRequest req(REQUEST_HEAD); - req.m_szUrl = pszUrl; - - filetransfer *ft = new filetransfer(this, 0); - ft->jid = mir_strdup(jid); - ft->std.hContact = hContact; - ft->type = FT_HTTP; - ft->httpPath = mir_strdup(pszUrl); - ft->std.totalFiles = 1; - ft->std.szCurrentFile.w = mir_utf8decodeW(szName); - - NLHR_PTR pResp(Netlib_HttpTransaction(m_hNetlibUser, &req)); - if (pResp && pResp->resultCode == 200) { - auto *p = (*pResp)["Content-Length"]; - if (p) - ft->dwExpectedRecvFileSize = ft->std.currentFileSize = atoi(p); + DB::FILE_BLOB blob(nullptr, szName, pszDescr); + blob.setUrl(pszUrl); + if (File::bOfflineAuto) { + MHttpRequest req(REQUEST_HEAD); + req.m_szUrl = pszUrl; + NLHR_PTR pResp(Netlib_HttpTransaction(m_hNetlibUser, &req)); + if (pResp && pResp->resultCode == 200) + if (auto *p = pResp->FindHeader("Content-Length")) + blob.setSize(_atoi64(p)); } - DB::EventInfo dbei; - dbei.flags = DBEF_TEMPORARY; - dbei.iTimestamp = time(0); - ProtoChainRecvFile(ft->std.hContact, DB::FILE_BLOB(ft, szName, pszDescr), dbei); + blob.write(dbei); } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/JabberG/src/jabber_omemo.cpp b/protocols/JabberG/src/jabber_omemo.cpp index e87414d68f..28026fe7c0 100644 --- a/protocols/JabberG/src/jabber_omemo.cpp +++ b/protocols/JabberG/src/jabber_omemo.cpp @@ -1392,40 +1392,16 @@ bool CJabberProto::OmemoHandleMessage(XmppMsg *msg, const TiXmlElement *node, co char protocol[7], hexkey[89], suburl[5001]; int ret = sscanf(result.GetBuffer(), "%6[^:]://%5000[^#]#%88s", protocol, suburl, hexkey); protocol[6] = hexkey[88] = suburl[5000] = 0; - if (ret == 3 && !strcmp(protocol, "aesgcm") && strlen(hexkey) == 88) { - CMStringA szName; - const char *b = strrchr(suburl, '/') + 1; - while (*b != 0 && *b != '#' && *b != '?') - szName.AppendChar(*b++); - - ptrW pwszName(mir_utf8decodeW(szName.c_str())); - - JSONNode root; - root << WCHAR_PARAM("f", pwszName) << CHAR_PARAM("u", result.GetBuffer()); - - DBEVENTINFO dbei = {}; - dbei.szModule = Proto_GetBaseAccountName(hContact); - dbei.iTimestamp = msgTime; - dbei.eventType = EVENTTYPE_FILE; - if (trusted) - dbei.flags = DBEF_SECURE; - if (isCarbon) - dbei.flags = DBEF_SENT; - - std::string text = root.write(); - dbei.cbBlob = (int)text.size() + 1; - dbei.pBlob = (char *)text.c_str(); - db_event_add(hContact, &dbei); - } - else { - msg->msgTime = msgTime; + + if (trusted == FP_TOFU) + msg->dbei.flags |= DBEF_SECURE; + if (trusted == FP_VERIFIED) + msg->dbei.flags |= DBEF_STRONG; + + if (ret == 3 && !strcmp(protocol, "aesgcm") && strlen(hexkey) == 88) + FileProcessHttpDownload(msg->dbei, result, nullptr); + else msg->szMessage = result; - - if (trusted) - msg->dbei.flags |= DBEF_STRONG; - if (isCarbon) - msg->dbei.flags |= DBEF_SENT; - } return true; } diff --git a/protocols/JabberG/src/jabber_proto.h b/protocols/JabberG/src/jabber_proto.h index d49ac5357b..3c6000f8d9 100644 --- a/protocols/JabberG/src/jabber_proto.h +++ b/protocols/JabberG/src/jabber_proto.h @@ -599,7 +599,7 @@ struct CJabberProto : public PROTO, public IJabberInterface //---- jabber_file.cpp --------------------------------------------------------------- - void FileProcessHttpDownload(MCONTACT, const char *jid, const char *pszUrl, const char *pszDescr); + void FileProcessHttpDownload(DB::EventInfo &dbei, const char *pszUrl, const char *pszDescr); int FileReceiveParse(filetransfer *ft, char* buffer, int datalen); int FileSendParse(HNETLIBCONN s, filetransfer *ft, char* buffer, int datalen); diff --git a/protocols/JabberG/src/jabber_thread.cpp b/protocols/JabberG/src/jabber_thread.cpp index 12106c3552..fa0bd377e7 100644 --- a/protocols/JabberG/src/jabber_thread.cpp +++ b/protocols/JabberG/src/jabber_thread.cpp @@ -1367,12 +1367,10 @@ void CJabberProto::XmppMsg::process() } } else if (!mir_strcmp(pszXmlns, JABBER_FEAT_OOB2)) { - if (auto* url = XmlGetChildText(xNode, "url")) { - m_proto->FileProcessHttpDownload(hContact, from, url, XmlGetChildText(xNode, "desc")); - return; - } - - m_proto->debugLogA("No URL in OOB file transfer, ignoring"); + if (auto* url = XmlGetChildText(xNode, "url")) + m_proto->FileProcessHttpDownload(dbei, url, XmlGetChildText(xNode, "desc")); + else + m_proto->debugLogA("No URL in OOB file transfer, ignoring"); } else if (!mir_strcmp(pszXmlns, JABBER_FEAT_MUC_USER)) { auto* inviteNode = XmlFirstChild(xNode, "invite"); @@ -1420,7 +1418,7 @@ void CJabberProto::XmppMsg::process() szMessage += m_proto->ExtractImage(node); // all service info was already processed - if (szMessage.IsEmpty()) { + if (dbei.eventType == EVENTTYPE_MESSAGE && szMessage.IsEmpty()) { m_proto->debugLogA("empty message, returning"); return; } @@ -1464,10 +1462,18 @@ void CJabberProto::XmppMsg::add_to_db() dbei.flags |= DBEF_SENT; dbei.iTimestamp = (uint32_t)msgTime; - dbei.pBlob = szMessage.GetBuffer(); dbei.szId = szMamMsgId; - MEVENT hDbEVent = (MEVENT)ProtoChainRecvMsg(hContact, dbei); + MEVENT hDbEVent; + if (dbei.eventType == EVENTTYPE_FILE) { + dbei.flags |= DBEF_TEMPORARY; + hDbEVent = (MEVENT)ProtoChainRecvFile(hContact, DB::FILE_BLOB(dbei), dbei); + } + else { + dbei.pBlob = szMessage.GetBuffer(); + hDbEVent = (MEVENT)ProtoChainRecvMsg(hContact, dbei); + } + if (idStr) m_proto->m_arChatMarks.insert(new CChatMark(hDbEVent, idStr, from)); -- cgit v1.2.3