From c83fb4b4bfd6b29e0264d5215ce43f6afe10ce51 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Wed, 22 Aug 2012 06:32:09 +0300 Subject: autoexchange fixes db event filter fixes backported fixes from miranda_ng port --- messages.cpp | 172 +++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 103 insertions(+), 69 deletions(-) (limited to 'messages.cpp') diff --git a/messages.cpp b/messages.cpp index 18b2378..5dc673c 100755 --- a/messages.cpp +++ b/messages.cpp @@ -25,7 +25,7 @@ int returnNoError(HANDLE hContact); std::list sent_msgs; -int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, DWORD timestamp) +void RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, DWORD timestamp) { DWORD dbflags = DBEF_UTF; { //check for gpg related data @@ -69,7 +69,7 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, D { HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); - return 0; + return; } } { @@ -169,14 +169,15 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, D CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)"Unable to decrypt PGP encrypted message"); HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc); - return 0; + return; } if(result == pxNotFound) { DeleteFile(path.c_str()); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); - return 0; + return; } + //TODO: check gpg output for errors _terminate = false; while(out.find("public key decryption failed: bad passphrase") != string::npos) { @@ -232,14 +233,15 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, D CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)"Unable to decrypt PGP encrypted message"); HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc); - return 0; + return; } if(result == pxNotFound) { DeleteFile(path.c_str()); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); - return 0; + return; } + //TODO: check gpg output for errors } out.clear(); gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); @@ -256,13 +258,14 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, D CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)"Unable to decrypt PGP encrypted message"); HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc); - return 0; + return; } if(result == pxNotFound) { DeleteFile(path.c_str()); HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); } + //TODO: check gpg output for errors { wstring tmp = tmp2; tmp += _T("\\tmp\\"); @@ -289,7 +292,7 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, D HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc); mir_free(tmp); - return 0; + return; } } } @@ -326,7 +329,7 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, D HistoryLog(hContact, db_event("Error message sent", 0, 0, DBEF_SENT)); DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", enc); mir_free(tmp); - return 0; + return; } else { @@ -341,12 +344,12 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, D HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags|DBEF_READ)); HistoryLog(metaGetContact(hContact), db_event(msg, timestamp, 0, dbflags)); mir_free(msg); - return 0; + return; } char *tmp = mir_strdup(toUTF8(str).c_str()); HistoryLog(hContact, db_event(tmp, timestamp, 0, dbflags)); mir_free(tmp); - return 0; + return; } } } @@ -358,13 +361,13 @@ int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags, D { HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags|DBEF_READ)); HistoryLog(metaGetContact(hContact), db_event(msg, timestamp, 0, dbflags)); - return 0; + return; } HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags|DBEF_READ)); - return 0; + return; } HistoryLog(hContact, db_event(msg, timestamp, 0, dbflags)); - return 0; + return; } int RecvMsgSvc(WPARAM w, LPARAM l) @@ -381,7 +384,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) wstring str = toUTF16(msg); wstring::size_type s1 = wstring::npos, s2 = wstring::npos; DWORD dbflags = DBEF_UTF; - if((str.find(_T("-----PGP KEY RESPONSE-----")) != wstring::npos) && !metaIsProtoMetaContacts(ccs->hContact)) + if(bAutoExchange && (str.find(_T("-----PGP KEY RESPONSE-----")) != wstring::npos) && !metaIsProtoMetaContacts(ccs->hContact)) { s2 = str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")); s1 = str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")); @@ -389,7 +392,6 @@ int RecvMsgSvc(WPARAM w, LPARAM l) { s2 += _tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")); DBWriteContactSettingTString(ccs->hContact, szGPGModuleName, "GPGPubKey", str.substr(s1,s2-s1).c_str()); - DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 1); { //gpg execute block wstring cmd; TCHAR tmp2[MAX_PATH] = {0}; @@ -434,6 +436,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } if(result == pxNotFound) return 1; + //TODO: check gpg output for errors { char *tmp = NULL; string::size_type s = output.find("gpg: key ") + strlen("gpg: key "); @@ -452,7 +455,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) s2 = output.find("<", s); else if(s2 > output.find("<", s)) s2 = output.find("<", s); - tmp = new char [output.substr(s,s2-s-1).length()+1]; + tmp = (char*)mir_alloc(output.substr(s,s2-s-1).length()+1); strcpy(tmp, output.substr(s,s2-s-1).c_str()); mir_utf8decode(tmp, 0); DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyMainName", tmp); @@ -464,14 +467,14 @@ int RecvMsgSvc(WPARAM w, LPARAM l) s2++; if(output[s] == ')') { - tmp = new char [output.substr(s2,s-s2).length()+1]; + tmp = (char*)mir_alloc(output.substr(s2,s-s2).length()+1); strcpy(tmp, output.substr(s2,s-s2).c_str()); mir_utf8decode(tmp, 0); DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyComment", tmp); mir_free(tmp); s+=3; s2 = output.find(">", s); - tmp = new char [output.substr(s,s2-s).length()+1]; + tmp = (char*)mir_alloc(output.substr(s,s2-s).length()+1); strcpy(tmp, output.substr(s,s2-s).c_str()); mir_utf8decode(tmp, 0); DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyMainEmail", tmp); @@ -479,12 +482,13 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } else { - tmp = new char [output.substr(s2,s-s2).length()+1]; + tmp = (char*)mir_alloc(output.substr(s2,s-s2).length()+1); strcpy(tmp, output.substr(s2,s-s2).c_str()); mir_utf8decode(tmp, 0); DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyMainEmail", output.substr(s2,s-s2).c_str()); mir_free(tmp); } + DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 1); DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "bAlwatsTrust", 1); void setSrmmIcon(HANDLE); void setClistIcon(HANDLE); @@ -550,64 +554,77 @@ int RecvMsgSvc(WPARAM w, LPARAM l) char *tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); if(tmp[0]) { - DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 0); + int enc_state = DBGetContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 0); + if(enc_state) + DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 0); string str = "-----PGP KEY RESPONSE-----"; str.append(tmp); CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)str.c_str()); - DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 1); + if(enc_state) + DBWriteContactSettingByte(ccs->hContact, szGPGModuleName, "GPGEncryption", 1); } mir_free(tmp); - if(!isContactHaveKey(ccs->hContact) && bAutoExchange && gpg_valid && gpg_keyexist) + return returnNoError(ccs->hContact); + } + else if(!isContactHaveKey(ccs->hContact) && bAutoExchange && gpg_valid && gpg_keyexist) + { + LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); + DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0); + if(uin) { - LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); - DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0); - if(uin) + char *proto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); + char svc[64]; + strcpy(svc, proto); + strcat(svc, PS_ICQ_CHECKCAPABILITY); + if(ServiceExists(svc)) { - if(ServiceExists("ICQ"PS_ICQ_CHECKCAPABILITY)) + ICQ_CUSTOMCAP cap = {0}; + strcpy(cap.caps, "GPG AutoExchange"); + if(CallService(svc, (WPARAM)ccs->hContact, (LPARAM)&cap)) { - 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)PREF_UTF, (LPARAM)"-----PGP KEY REQUEST-----"); + CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)"-----PGP KEY REQUEST-----"); + return returnNoError(ccs->hContact); } } - else + } + else + { + TCHAR *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", _T("")); + if(jid[0]) { - 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++) { - 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) { - 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) { - 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)0, (LPARAM)"-----PGP KEY REQUEST-----"); + CallContactService(ccs->hContact, PSS_MESSAGE, (WPARAM)0, (LPARAM)"-----PGP KEY REQUEST-----"); + return returnNoError(ccs->hContact); } } } } } - return 1; } if(!(strstr(msg, "-----BEGIN PGP MESSAGE-----") && strstr(msg, "-----END PGP MESSAGE-----"))) return CallService(MS_PROTO_CHAINRECV, w, l); - boost::thread *thr = new boost::thread(boost::bind(RecvMsgSvc_func, ccs->hContact, str, msg, ccs->wParam, pre->timestamp)); - return returnNoError(ccs->hContact); + boost::thread *thr = new boost::thread(boost::bind(RecvMsgSvc_func, ccs->hContact, str, msg, ccs->wParam, pre->timestamp)); + return returnNoError(ccs->hContact); } -int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) +void SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) { wstring str; bool isansi = false; @@ -654,7 +671,8 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) HistoryLog(hContact, db_event("Failed to encrypt message with GPG", 0,0, 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); + CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); + return; } if(!bJabberAPI || !bIsMiranda09) //force jabber to handle encrypted message by itself cmd += _T("--comment \"\" --no-version "); @@ -700,13 +718,16 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) params.hProcess = NULL; debuglog<hContact, proto, "UIN", 0); if(uin) { - if(ServiceExists("ICQ"PS_ICQ_CHECKCAPABILITY)) + char *proto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); + char svc[64]; + strcpy(svc, proto); + strcat(svc, PS_ICQ_CHECKCAPABILITY); + + if(ServiceExists(svc)) { ICQ_CUSTOMCAP cap = {0}; strcpy(cap.caps, "GPG AutoExchange"); - if(CallService("ICQ"PS_ICQ_CHECKCAPABILITY, (WPARAM)ccs->hContact, (LPARAM)&cap)) + if(CallService(svc, (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); @@ -848,7 +880,7 @@ int SendMsgSvc(WPARAM w, LPARAM l) if(caps) { wstring str; - for(int i =0;;i++) + for(int i=0;;i++) { str.push_back(caps[i]); if(caps[i] == '\0') @@ -893,12 +925,14 @@ int HookSendMsg(WPARAM w, LPARAM l) DBEVENTINFO * dbei = (DBEVENTINFO*)l; if(dbei->eventType != EVENTTYPE_MESSAGE) return 0; + HANDLE hContact = (HANDLE)w; 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. + if(isContactSecured(hContact) && strstr((char*)dbei->pBlob, "-----BEGIN PGP MESSAGE-----")) //our service data, can be double added by metacontacts e.t.c. + return 1; + if(bAutoExchange && (strstr((char*)dbei->pBlob, "-----PGP KEY RESPONSE-----") || strstr((char*)dbei->pBlob, "-----PGP KEY REQUEST-----"))) ///do not show service data in history return 1; } - HANDLE hContact = (HANDLE)w; if(isContactSecured(hContact) && (dbei->flags & DBEF_SENT)) //aggressive outgoing events filtering { if(!hcontact_data[hContact].msgs_to_pass.empty()) -- cgit v1.2.3