From 59822f2c393d553d43adb4a5eb8daf77609063f2 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Tue, 22 Mar 2011 14:09:37 +0200 Subject: new threading code (part 2, usable) --- messages.cpp | 490 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 237 insertions(+), 253 deletions(-) (limited to 'messages.cpp') diff --git a/messages.cpp b/messages.cpp index b37bbd0..05d138a 100644 --- a/messages.cpp +++ b/messages.cpp @@ -28,6 +28,9 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) { //check for gpg related data wstring str = toUTF16(msg); wstring::size_type s1 = wstring::npos, s2 = wstring::npos; + DWORD dbflags = 0; + if(flags & PREF_UTF) + flags |= DBEF_UTF; if((str.find(_T("-----PGP KEY RESPONSE-----")) != wstring::npos) && !metaIsProtoMetaContacts(hContact)) { @@ -147,7 +150,6 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) HistoryLog(hContact, "PGP Encryption turned on by key autoexchange feature", EVENTTYPE_MESSAGE, 0); } } - hcontact_data[hContact].msgs_to_ignore.push_back(msg); mir_free(msg); return 1; } @@ -166,7 +168,7 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) { //this is public key if(metaIsSubcontact(hContact)) { - HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, flags); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); mir_free(msg); return 0; } @@ -193,7 +195,7 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) new_key_hcnt_mutex.lock(); new_key_hcnt = hContact; ShowNewKeyDialog(); - HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, flags); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); mir_free(msg); return 0; } @@ -290,7 +292,7 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) } else if(MessageBox(0, _T("Do you want try to decrypt encrypted message ?"), _T("Warning"), MB_YESNO) == IDNO) { - HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, flags); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); mir_free(msg); return 0; } @@ -376,14 +378,14 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) delete gpg_thread; debuglog< Accounts; - list::iterator end = Accounts.end(); - for(list::iterator p = Accounts.begin(); p != end; p++) - { - TCHAR *caps = (*p)->getJabberInterface()->Net()->GetResourceFeatures(jid); - if(caps) - { - wstring str; - for(int i =0;;i++) - { - str.push_back(caps[i]); - if(caps[i] == '\0') - if(caps[i+1] == '\0') - break; - } - mir_free(caps); - if(str.find(_T("GPG_Key_Auto_Exchange:0")) != string::npos) - { - CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)"-----PGP KEY REQUEST-----"); - hcontact_data[hContact].msgs_to_ignore.push_back(msg); - hcontact_data[hContact].msgs_to_send.push_back(msg); - boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, hContact)); - return returnNoError(hContact); - } - } - } - } + tmp = mir_a2t(msg); + isansi = true; } + str.append(tmp); + mir_free(tmp); } - if(!isContactSecured(hContact)) + string out; + DWORD code; + wstring cmd; + wstring path; + extern bool bJabberAPI, bIsMiranda09; + char *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID", ""); + if(!tmp[0]) + { + mir_free(tmp); + HistoryLog(hContact, "Failed to encrypt message with GPG", EVENTTYPE_MESSAGE, DBEF_SENT); + hcontact_data[hContact].msgs_to_pass.push_back("Failed to encrypt message with GPG"); + mir_free(msg); return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); - - { //encrypt data here - wstring str; - bool isansi = false; - if(!metaIsSubcontact(hContact)) - str = toUTF16(msg); - else - {//workaround ... - wchar_t *tmp = mir_utf8decodeW(msg); - if(!tmp) - { - tmp = mir_a2t(msg); - isansi = true; - } - str.append(tmp); - mir_free(tmp); - } + } + if(!bJabberAPI || !bIsMiranda09) //force jabber to handle encrypted message by itself + cmd += _T("--comment \"\" --no-version "); + if(DBGetContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 0)) + cmd += _T("--trust-model always "); + cmd += _T("--batch --yes -e -a -r "); + TCHAR *tmp2 = mir_a2t(tmp); + mir_free(tmp); + cmd += tmp2; + mir_free(tmp2); + cmd += _T(" \""); + tmp2 = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); + path.append(tmp2); + cmd += tmp2; + mir_free(tmp2); + cmd += _T("\\exported_data"); + path.append(_T("\\exported_data")); + cmd += _T("\""); + { + char *tmp; + tmp = mir_strdup(toUTF8(str).c_str()); + fstream f(path.c_str(), std::ios::out); + f<lParam)); if (!msg) + { + mir_free(msg); return CallService(MS_PROTO_CHAINSEND, w, l); + } if(strstr(msg,"-----BEGIN PGP MESSAGE-----")) return CallService(MS_PROTO_CHAINSEND, w, l); + if(!isContactHaveKey(ccs->hContact)) + { + if(bAutoExchange && !strstr(msg, "-----PGP KEY REQUEST-----") && !strstr(msg, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && isGPGConfigured()) + { + void send_encrypted_msgs_thread(HANDLE hContact); + LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); + DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0); + if(uin) + { + if(ServiceExists("ICQ"PS_ICQ_CHECKCAPABILITY)) + { + ICQ_CUSTOMCAP cap = {0}; + strcpy(cap.caps, "GPG AutoExchange"); + if(CallService("ICQ"PS_ICQ_CHECKCAPABILITY, (WPARAM)ccs->hContact, (LPARAM)&cap)) + { + CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)ccs->wParam, (LPARAM)"-----PGP KEY REQUEST-----"); + hcontact_data[ccs->hContact].msgs_to_send.push_back(msg); + boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, ccs->hContact)); + mir_free(msg); + return returnNoError(ccs->hContact); + } + } + } + else + { + TCHAR *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", _T("")); + if(jid[0]) + { + extern list Accounts; + list::iterator end = Accounts.end(); + for(list::iterator p = Accounts.begin(); p != end; p++) + { + TCHAR *caps = (*p)->getJabberInterface()->Net()->GetResourceFeatures(jid); + if(caps) + { + wstring str; + for(int i =0;;i++) + { + str.push_back(caps[i]); + if(caps[i] == '\0') + if(caps[i+1] == '\0') + break; + } + mir_free(caps); + if(str.find(_T("GPG_Key_Auto_Exchange:0")) != string::npos) + { + CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)ccs->wParam, (LPARAM)"-----PGP KEY REQUEST-----"); + hcontact_data[ccs->hContact].msgs_to_send.push_back(msg); + boost::thread *thr = new boost::thread(boost::bind(send_encrypted_msgs_thread, ccs->hContact)); + mir_free(msg); + return returnNoError(ccs->hContact); + } + } + } + } + } + } + else + { + mir_free(msg); + return CallService(MS_PROTO_CHAINSEND, w, l); + } + } if(metaIsProtoMetaContacts(ccs->hContact)) { - hcontact_data[ccs->hContact].msgs_to_ignore.push_back((char*)msg); + mir_free(msg); return CallService(MS_PROTO_CHAINSEND, w, l); } boost::thread *thr = new boost::thread(boost::bind(SendMsgSvc_func, ccs->hContact, msg, (DWORD)ccs->wParam)); return returnNoError(ccs->hContact); } +boost::mutex event_processing_mutex; + int HookSendMsg(WPARAM w, LPARAM l) -{ //TODO: implement additional filtering for metacontacts data... +{ if(!l) return 0; DBEVENTINFO * dbei = (DBEVENTINFO*)l; - if((dbei->eventType == EVENTTYPE_MESSAGE) && (dbei->flags & DBEF_SENT)) + if(dbei->eventType != EVENTTYPE_MESSAGE) + return 0; + if(dbei->flags & DBEF_SENT) { if(strstr((char*)dbei->pBlob, "-----BEGIN PGP MESSAGE-----") || strstr((char*)dbei->pBlob, "-----PGP KEY RESPONSE-----") || strstr((char*)dbei->pBlob, "-----PGP KEY REQUEST-----") || strstr((char*)dbei->pBlob, "-----PGP KEY RESPONSE-----")) //our service data, can be double added by metacontacts e.t.c. return 1; - HANDLE hContact = NULL; - if(!hcontact_data[(HANDLE)w].msgs_to_ignore.empty()) - hContact = (HANDLE)w; - else if(!hcontact_data[metaGetContact((HANDLE)w)].msgs_to_ignore.empty()) - hContact = metaGetContact((HANDLE)w); - if(hContact) - { - list::iterator end = hcontact_data[hContact].msgs_to_ignore.end(); - for(list::iterator p = hcontact_data[hContact].msgs_to_ignore.begin(); p != end; p++) - { - if(*p == (char*)dbei->pBlob) - { - hcontact_data[hContact].msgs_to_ignore.erase(p); - return 1; - } - } - } } HANDLE hContact = (HANDLE)w; - - - if(metaIsProtoMetaContacts(hContact)) - hContact = metaGetCurrent(hContact); - if(!DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0)) - return 0; - - if(bAppendTags) + if(isContactSecured(hContact) && (dbei->flags & DBEF_SENT)) //aggressive outgoing events filtering { - if((dbei->eventType == EVENTTYPE_MESSAGE) && (dbei->flags & DBEF_SENT)) + if(!hcontact_data[hContact].msgs_to_pass.empty()) { - if(!hcontact_data[hContact].msgs_to_tag.empty()) + event_processing_mutex.lock(); + std::list::iterator end = hcontact_data[hContact].msgs_to_pass.end(); + for(std::list::iterator i = hcontact_data[hContact].msgs_to_pass.begin(); i != end; ++i) { - std::list::iterator end = hcontact_data[hContact].msgs_to_tag.end(); - for(std::list::iterator i = hcontact_data[hContact].msgs_to_tag.begin(); i != end; ++i) + if(!strcmp((*i).c_str(), (char*)dbei->pBlob)) { - if(*i == (char*)dbei->pBlob) - { - char *msg = (char*)dbei->pBlob; - wstring str = toUTF16(msg); - str.insert(0, outopentag); - str.append(outclosetag); - char *msg2 = mir_strdup(toUTF8(str).c_str()); - mir_free(dbei->pBlob); - dbei->pBlob = (PBYTE)msg2; - dbei->cbBlob = strlen(msg2)+1; - hcontact_data[hContact].msgs_to_tag.erase(i); - break; - } + hcontact_data[hContact].msgs_to_pass.erase(i); + event_processing_mutex.unlock(); + return 0; } } + event_processing_mutex.unlock(); } + return 1; } - if((dbei->eventType == EVENTTYPE_MESSAGE) && !(dbei->flags & DBEF_SENT) && metaIsProtoMetaContacts((HANDLE)w)) + if(!DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0)) + return 0; + if(!(dbei->flags & DBEF_SENT) && metaIsProtoMetaContacts((HANDLE)w)) { char tmp[29]; strncpy(tmp, (char*)dbei->pBlob, 27); @@ -883,11 +872,6 @@ int HookSendMsg(WPARAM w, LPARAM l) return 0; } -int TestHook(WPARAM w, LPARAM l) -{ - return 0; -} - static BOOL CALLBACK DlgProcKeyPassword(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { -- cgit v1.2.3