diff options
author | Gluzskiy Alexandr <sss123next@list.ru> | 2011-04-01 02:24:07 +0300 |
---|---|---|
committer | Gluzskiy Alexandr <sss123next@list.ru> | 2011-04-01 02:24:07 +0300 |
commit | a7823b56a5b08f81248e83804349bc8e9fc61206 (patch) | |
tree | 9d2e3a7a558498be03513c96c35796b948418115 /messages.cpp | |
parent | b0e1bb306eec7f2d13ff385d4c1191646cc3e7f9 (diff) | |
parent | 8cdda41b119526a2741938d57b7e066e1697315b (diff) |
Merge branch 'new_gpg' into new_gpg_autoexchange
Conflicts:
messages.cpp
Diffstat (limited to 'messages.cpp')
-rw-r--r-- | messages.cpp | 1061 |
1 files changed, 547 insertions, 514 deletions
diff --git a/messages.cpp b/messages.cpp index 40db55c..ee80e2f 100644 --- a/messages.cpp +++ b/messages.cpp @@ -1,4 +1,4 @@ -// Copyright © 2010-2011 sss +// Copyright � 2010-2011 sss // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -23,236 +23,12 @@ boost::mutex new_key_hcnt_mutex; bool _terminate = false; int returnNoError(HANDLE hContact); -int RecvMsgSvc(WPARAM w, LPARAM l) -{ - CCSDATA *ccs = (CCSDATA*)l; - if (!ccs) - return CallService(MS_PROTO_CHAINRECV, w, l); - PROTORECVEVENT *pre = (PROTORECVEVENT*)(ccs->lParam); - if (!pre) - return CallService(MS_PROTO_CHAINRECV, w, l); - char *msg = pre->szMessage; - if (!msg) - return CallService(MS_PROTO_CHAINRECV, w, l); - HANDLE hContact = ccs->hContact; - +int RecvMsgSvc_func(HANDLE hContact, std::wstring str, char *msg, DWORD flags) +{ + DWORD dbflags = DBEF_UTF; { //check for gpg related data - wstring str = toUTF16(msg); wstring::size_type s1 = wstring::npos, s2 = wstring::npos; - if((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-----")); - if(s1 != wstring::npos && s2 != wstring::npos) - { - s2 += _tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")); - DBWriteContactSettingTString(hContact, szGPGModuleName, "GPGPubKey", str.substr(s1,s2-s1).c_str()); - DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 1); - { //gpg execute block - wstring cmd; - TCHAR tmp2[MAX_PATH] = {0}; - TCHAR *ptmp; - string output; - DWORD exitcode; - { - ptmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); - _tcscpy(tmp2, ptmp); - mir_free(ptmp); - _tcscat(tmp2, _T("\\")); - _tcscat(tmp2, _T("temporary_exported.asc")); - DeleteFile(tmp2); - wfstream f(tmp2, std::ios::out); - ptmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T("")); - wstring new_key = ptmp; - mir_free(ptmp); - f<<new_key.c_str(); - f.close(); - cmd += _T(" --batch "); - cmd += _T(" --import \""); - cmd += tmp2; - cmd += _T("\""); - } - gpg_execution_params params; - pxResult result; - params.cmd = &cmd; - params.useless = ""; - params.out = &output; - params.code = &exitcode; - params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) - { - gpg_thread.~thread(); - debuglog<<time_str()<<": GPG execution timed out, aborted\n"; - return 1; - } - if(result == pxNotFound) - return 1; - { - char *tmp = NULL; - string::size_type s = output.find("gpg: key ") + strlen("gpg: key "); - string::size_type s2 = output.find(":", s); - DBWriteContactSettingString(hContact, szGPGModuleName, "KeyID", output.substr(s,s2-s).c_str()); - s2+=2; - s = output.find("“", s2); - if(s == string::npos) - { - s = output.find("\"", s2); - s += 1; - } - else - s += 3; - if((s2 = output.find("(", s)) == string::npos) - 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]; - strcpy(tmp, output.substr(s,s2-s-1).c_str()); - mir_utf8decode(tmp, 0); - DBWriteContactSettingString(hContact, szGPGModuleName, "KeyMainName", tmp); - mir_free(tmp); - if((s = output.find(")", s2)) == string::npos) - s = output.find(">", s2); - else if(s > output.find(">", s2)) - s = output.find(">", s2); - s2++; - if(output[s] == ')') - { - tmp = new char [output.substr(s2,s-s2).length()+1]; - strcpy(tmp, output.substr(s2,s-s2).c_str()); - mir_utf8decode(tmp, 0); - DBWriteContactSettingString(hContact, szGPGModuleName, "KeyComment", tmp); - mir_free(tmp); - s+=3; - s2 = output.find(">", s); - tmp = new char [output.substr(s,s2-s).length()+1]; - strcpy(tmp, output.substr(s,s2-s).c_str()); - mir_utf8decode(tmp, 0); - DBWriteContactSettingString(hContact, szGPGModuleName, "KeyMainEmail", tmp); - mir_free(tmp); - } - else - { - tmp = new char [output.substr(s2,s-s2).length()+1]; - strcpy(tmp, output.substr(s2,s-s2).c_str()); - mir_utf8decode(tmp, 0); - DBWriteContactSettingString(hContact, szGPGModuleName, "KeyMainEmail", output.substr(s2,s-s2).c_str()); - mir_free(tmp); - } - DBWriteContactSettingByte(hContact, szGPGModuleName, "bAlwatsTrust", 1); - void setSrmmIcon(HANDLE); - void setClistIcon(HANDLE); - setSrmmIcon(hContact); - setClistIcon(hContact); - if(metaIsSubcontact(hContact)) - { - setSrmmIcon(metaGetContact(hContact)); - setClistIcon(metaGetContact(hContact)); - HistoryLog(metaGetContact(hContact), "PGP Encryption turned on by key autoexchange feature", EVENTTYPE_MESSAGE, 0); - } - HistoryLog(hContact, "PGP Encryption turned on by key autoexchange feature", EVENTTYPE_MESSAGE, 0); - } - } - hcontact_data[hContact].msgs_to_ignore.push_back(msg); - return 1; - } - } - if((str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")) != wstring::npos)) - { - s2 = str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")); - s1 = str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")); - } - else if((str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----END PGP PRIVATE KEY BLOCK-----")) != wstring::npos)) - { - s2 = str.find(_T("-----END PGP PRIVATE KEY BLOCK-----")); - s1 = str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")); - } - if((s2 != wstring::npos) && (s1 != wstring::npos)) - { //this is public key - if(metaIsSubcontact(hContact)) - return CallService(MS_PROTO_CHAINRECV, w, l); //yet another metacontacts problem - debuglog<<time_str()<<": info: "<<"received key from: "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)<<"\n"; - s1 = 0; - while((s1 = str.find(_T("\r"), s1)) != wstring::npos) - { - str.erase(s1, 1); - } - void ShowNewKeyDialog(); - if((str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")) != wstring::npos)) - { - s2 = str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")); - s1 = str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")); - s2 += _tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")); - } - else if((str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----END PGP PRIVATE KEY BLOCK-----")) != wstring::npos)) - { - s2 = str.find(_T("-----END PGP PRIVATE KEY BLOCK-----")); - s1 = str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")); - s2 += _tcslen(_T("-----END PGP PRIVATE KEY BLOCK-----")); - } - new_key.append(str.substr(s1,s2-s1)); - new_key_hcnt_mutex.lock(); - new_key_hcnt = hContact; - ShowNewKeyDialog(); - return CallService(MS_PROTO_CHAINRECV, w, l); - } - if(bAutoExchange && strstr(msg, "-----PGP KEY REQUEST-----") && isGPGConfigured()) - { - char *tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); - if(tmp[0]) - { - DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0); - string str = "-----PGP KEY RESPONSE-----"; - str.append(tmp); - CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)str.c_str()); - DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 1); - } - mir_free(tmp); - if(!isContactHaveKey(hContact) && bAutoExchange && isGPGConfigured()) - { - LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0); - DWORD uin = DBGetContactSettingDword(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)hContact, (LPARAM)&cap)) - CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)"-----PGP KEY REQUEST-----"); - } - } - else - { - TCHAR *jid = UniGetContactSettingUtf(hContact, proto, "jid", _T("")); - if(jid[0]) - { - extern list <JabberAccount*> Accounts; - list<JabberAccount*>::iterator end = Accounts.end(); - for(list<JabberAccount*>::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)0, (LPARAM)"-----PGP KEY REQUEST-----"); - } - } - } - } - } - return 1; - } s1 = str.find(_T("-----BEGIN PGP MESSAGE-----")); s2 = str.find(_T("-----END PGP MESSAGE-----")); if((s2 != wstring::npos) && (s1 != wstring::npos)) @@ -260,7 +36,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) void setSrmmIcon(HANDLE); void setClistIcon(HANDLE); bool isContactHaveKey(HANDLE hContact); - if(!DBGetContactSettingByte(metaGetCurrent(hContact), szGPGModuleName, "GPGEncryption", 0)) + if(!DBGetContactSettingByte(metaGetMostOnline(hContact), szGPGModuleName, "GPGEncryption", 0)) { debuglog<<time_str()<<": info: "<<"received message from: "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)<<" whith tyrned off encryption\n"; if(bAutoExchange) @@ -300,19 +76,22 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } else { - DBWriteContactSettingByte(metaGetCurrent(hContact), szGPGModuleName, "GPGEncryption", 1); + DBWriteContactSettingByte(metaGetMostOnline(hContact), szGPGModuleName, "GPGEncryption", 1); setSrmmIcon(hContact); setClistIcon(hContact); } if(isContactHaveKey(hContact)) { - DBWriteContactSettingByte(metaGetCurrent(hContact), szGPGModuleName, "GPGEncryption", 1); + DBWriteContactSettingByte(metaGetMostOnline(hContact), szGPGModuleName, "GPGEncryption", 1); setSrmmIcon(hContact); setClistIcon(hContact); } } else if(MessageBox(0, _T("Do you want try to decrypt encrypted message ?"), _T("Warning"), MB_YESNO) == IDNO) - return CallService(MS_PROTO_CHAINRECV, w, l); + { + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); + return 0; + } } { wstring::size_type p = 0; @@ -323,9 +102,14 @@ int RecvMsgSvc(WPARAM w, LPARAM l) char *tmp = mir_t2a(str.substr(s1,s2-s1).c_str()); TCHAR *tmp2 = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); wstring path = tmp2; - path.append(_T("\\encrypted_data.asc")); + wstring encfile = toUTF16(get_random(10)); + wstring decfile = toUTF16(get_random(10)); + path.append(_T("\\tmp\\")); + path.append(encfile); DeleteFile(path.c_str()); fstream f(path.c_str(), std::ios::out); + while(!f.is_open()) + f.open(path.c_str(), std::ios::out); f<<tmp; mir_free(tmp); f.close(); @@ -336,7 +120,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) wstring cmd; cmd += _T("--batch "); { - char *inkeyid = UniGetContactSettingUtf(metaGetCurrent(hContact), szGPGModuleName, "InKeyID", ""); + char *inkeyid = UniGetContactSettingUtf(metaGetMostOnline(hContact), szGPGModuleName, "InKeyID", ""); TCHAR *pass = NULL; if(strlen(inkeyid) > 0) { @@ -373,12 +157,15 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } { wstring path = tmp2; - path += _T("\\decrypted_data"); + path += _T("\\tmp\\"); + path += decfile; DeleteFile(path.c_str()); } cmd += _T("--output \""); cmd += tmp2; - cmd += _T("\\decrypted_data\""); + cmd += _T("\\tmp\\"); + cmd += decfile; + cmd += _T("\""); cmd += _T(" -d -a \""); cmd += path; cmd += _T("\""); @@ -393,14 +180,17 @@ int RecvMsgSvc(WPARAM w, LPARAM l) if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) { delete gpg_thread; + TerminateProcess(params.hProcess, 1); debuglog<<time_str()<<": GPG execution timed out, aborted\n"; DeleteFile(path.c_str()); - return CallService(MS_PROTO_CHAINRECV, w, l); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); + return 0; } if(result == pxNotFound) { DeleteFile(path.c_str()); - return CallService(MS_PROTO_CHAINRECV, w, l); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); + return 0; } _terminate = false; while(out.find("public key decryption failed: bad passphrase") != string::npos) @@ -413,7 +203,7 @@ int RecvMsgSvc(WPARAM w, LPARAM l) s = out.find(" ID ", s); s += strlen(" ID "); string::size_type s2 = out.find(",",s); - DBWriteContactSettingString(metaGetCurrent(hContact), szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str()); + DBWriteContactSettingString(metaGetMostOnline(hContact), szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str()); } void ShowLoadKeyPasswordWindow(); new_key_hcnt_mutex.lock(); @@ -440,14 +230,17 @@ int RecvMsgSvc(WPARAM w, LPARAM l) if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) { delete gpg_thread; + TerminateProcess(params.hProcess, 1); debuglog<<time_str()<<": GPG execution timed out, aborted\n"; DeleteFile(path.c_str()); - return CallService(MS_PROTO_CHAINRECV, w, l); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); + return 0; } if(result == pxNotFound) { DeleteFile(path.c_str()); - return CallService(MS_PROTO_CHAINRECV, w, l); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); + return 0; } } out.clear(); @@ -455,45 +248,48 @@ int RecvMsgSvc(WPARAM w, LPARAM l) if(!gpg_thread->timed_join(boost::posix_time::seconds(10))) { delete gpg_thread; + TerminateProcess(params.hProcess, 1); debuglog<<time_str()<<": GPG execution timed out, aborted\n"; DeleteFile(path.c_str()); - return CallService(MS_PROTO_CHAINRECV, w, l); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); + return 0; } if(result == pxNotFound) { DeleteFile(path.c_str()); - return CallService(MS_PROTO_CHAINRECV, w, l); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); } { wstring tmp = tmp2; - tmp += _T("\\encrypted_data.asc"); + tmp += _T("\\tmp\\"); + tmp += encfile; DeleteFile(tmp.c_str()); } { wstring tmp = tmp2; - tmp += _T("\\decrypted_data"); + tmp += _T("\\tmp\\"); + tmp += decfile; if(_waccess(tmp.c_str(), 0) == -1) { if(errno == ENOENT) { - string str = pre->szMessage; - mir_free((void**)pre->szMessage); + string str = msg; str.insert(0, "Received unencrypted message:\n"); debuglog<<time_str()<<": info: Failed to decrypt GPG encrypted message.\n"; char *tmp = new char [str.length()+1]; strcpy(tmp, str.c_str()); - pre->szMessage = tmp; - return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs); + HistoryLog(hContact, tmp, EVENTTYPE_MESSAGE, dbflags); + mir_free(tmp); + return 0; } } } - - str.clear(); { wstring path = tmp2; mir_free(tmp2); - path += _T("\\decrypted_data"); + path += _T("\\tmp\\"); + path += decfile; fstream f(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary); if(f.is_open()) { @@ -510,12 +306,13 @@ int RecvMsgSvc(WPARAM w, LPARAM l) } if(str.empty()) { - string str = pre->szMessage; - mir_free((void**)pre->szMessage); + string str = msg; str.insert(0, "Failed to decrypt GPG encrypted message.\nMessage body for manual decryption:\n"); debuglog<<time_str()<<": info: Failed to decrypt GPG encrypted message.\n"; - pre->szMessage = mir_strdup(str.c_str()); - return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs); + char *tmp = mir_strdup(str.c_str()); + HistoryLog(hContact, tmp, EVENTTYPE_MESSAGE, dbflags); + mir_free(tmp); + return 0; } else { @@ -527,207 +324,362 @@ int RecvMsgSvc(WPARAM w, LPARAM l) if(metaIsSubcontact(hContact)) { char *msg = mir_strdup(toUTF8(str).c_str()); - HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, DBEF_UTF|DBEF_READ); - HistoryLog(metaGetContact(hContact), msg, EVENTTYPE_MESSAGE, DBEF_UTF); - mir_free(msg); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags|DBEF_READ); + HistoryLog(metaGetContact(hContact), msg, EVENTTYPE_MESSAGE, dbflags); return 1; } - mir_free((void**)pre->szMessage); - pre->szMessage = mir_strdup(toUTF8(str).c_str()); - return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs); + char *tmp = mir_strdup(toUTF8(str).c_str()); + HistoryLog(hContact, tmp, EVENTTYPE_MESSAGE, dbflags); + mir_free(tmp); + return 0; } } } } } - if(DBGetContactSettingByte(metaGetCurrent(hContact), szGPGModuleName, "GPGEncryption", 0)) + if(DBGetContactSettingByte(metaGetMostOnline(hContact), szGPGModuleName, "GPGEncryption", 0)) { if(metaIsSubcontact(hContact)) { - HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, DBEF_UTF| DBEF_READ); - HistoryLog(metaGetContact(hContact), msg, EVENTTYPE_MESSAGE, DBEF_UTF); - mir_free(msg); - return 1; + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags| DBEF_READ); + HistoryLog(metaGetContact(hContact), msg, EVENTTYPE_MESSAGE, dbflags); + return 0; } - string str = msg; - mir_free((void**)pre->szMessage); - str.insert(0, "Received unencrypted message:\n"); - pre->szMessage = mir_strdup(str.c_str()); - return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags|DBEF_READ); + return 0; } - return CallService(MS_PROTO_CHAINRECV, w, l); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); + return 0; } -int SendMsgSvc(WPARAM w, LPARAM l) +int RecvMsgSvc(WPARAM w, LPARAM l) { CCSDATA *ccs = (CCSDATA*)l; if (!ccs) - return CallService(MS_PROTO_CHAINSEND, w, l); - - char *msg = (char*)(ccs->lParam); + return CallService(MS_PROTO_CHAINRECV, w, l); + PROTORECVEVENT *pre = (PROTORECVEVENT*)(ccs->lParam); + if (!pre) + return CallService(MS_PROTO_CHAINRECV, w, l); + char *msg = pre->szMessage; if (!msg) - return CallService(MS_PROTO_CHAINSEND, w, l); - if(strstr(msg,"-----BEGIN PGP MESSAGE-----")) //metecontacts .... %) - return CallService(MS_PROTO_CHAINSEND, w, l); - HANDLE hContact = ccs->hContact; - if(metaIsProtoMetaContacts(hContact)) - { - hcontact_data[ccs->hContact].msgs_to_ignore.push_back((char*)ccs->lParam); - return CallService(MS_PROTO_CHAINSEND, w, l); - } - if(!isContactHaveKey(hContact) && bAutoExchange && !strstr(msg, "-----PGP KEY REQUEST-----") && !strstr(msg, "-----BEGIN PGP PUBLIC KEY BLOCK-----") && isGPGConfigured()) + return CallService(MS_PROTO_CHAINRECV, w, 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)) { - void send_encrypted_msgs_thread(HANDLE hContact); - LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0); - DWORD uin = DBGetContactSettingDword(hContact, proto, "UIN", 0); - if(uin) + s2 = str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")); + s1 = str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")); + if(s1 != wstring::npos && s2 != wstring::npos) { - if(ServiceExists("ICQ"PS_ICQ_CHECKCAPABILITY)) - { - ICQ_CUSTOMCAP cap = {0}; - strcpy(cap.caps, "GPG AutoExchange"); - if(CallService("ICQ"PS_ICQ_CHECKCAPABILITY, (WPARAM)hContact, (LPARAM)&cap)) + 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}; + TCHAR *ptmp; + string output; + DWORD exitcode; { - CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (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); + ptmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T("")); + _tcscpy(tmp2, ptmp); + mir_free(ptmp); + _tcscat(tmp2, _T("\\")); + _tcscat(tmp2, _T("temporary_exported.asc")); + DeleteFile(tmp2); + wfstream f(tmp2, std::ios::out); + while(!f.is_open()) + f.open(tmp2, std::ios::out); + ptmp = UniGetContactSettingUtf(ccs->hContact, szGPGModuleName, "GPGPubKey", _T("")); + wstring new_key = ptmp; + mir_free(ptmp); + f<<new_key.c_str(); + f.close(); + cmd += _T(" --batch "); + cmd += _T(" --import \""); + cmd += tmp2; + cmd += _T("\""); } - } - } - else - { - TCHAR *jid = UniGetContactSettingUtf(hContact, proto, "jid", _T("")); - if(jid[0]) - { - extern list <JabberAccount*> Accounts; - list<JabberAccount*>::iterator end = Accounts.end(); - for(list<JabberAccount*>::iterator p = Accounts.begin(); p != end; p++) + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &output; + params.code = &exitcode; + params.result = &result; + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) { - TCHAR *caps = (*p)->getJabberInterface()->Net()->GetResourceFeatures(jid); - if(caps) + gpg_thread.~thread(); + TerminateProcess(params.hProcess, 1); + debuglog<<time_str()<<": GPG execution timed out, aborted\n"; + return 1; + } + if(result == pxNotFound) + return 1; + { + char *tmp = NULL; + string::size_type s = output.find("gpg: key ") + strlen("gpg: key "); + string::size_type s2 = output.find(":", s); + DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyID", output.substr(s,s2-s).c_str()); + s2+=2; + s = output.find("“", s2); + if(s == 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(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (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); - } + s = output.find("\"", s2); + s += 1; } + else + s += 3; + if((s2 = output.find("(", s)) == string::npos) + 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]; + strcpy(tmp, output.substr(s,s2-s-1).c_str()); + mir_utf8decode(tmp, 0); + DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyMainName", tmp); + mir_free(tmp); + if((s = output.find(")", s2)) == string::npos) + s = output.find(">", s2); + else if(s > output.find(">", s2)) + s = output.find(">", s2); + s2++; + if(output[s] == ')') + { + tmp = new char [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]; + strcpy(tmp, output.substr(s,s2-s).c_str()); + mir_utf8decode(tmp, 0); + DBWriteContactSettingString(ccs->hContact, szGPGModuleName, "KeyMainEmail", tmp); + mir_free(tmp); + } + else + { + tmp = new char [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, "bAlwatsTrust", 1); + void setSrmmIcon(HANDLE); + void setClistIcon(HANDLE); + setSrmmIcon(ccs->hContact); + setClistIcon(ccs->hContact); + if(metaIsSubcontact(ccs->hContact)) + { + setSrmmIcon(metaGetContact(ccs->hContact)); + setClistIcon(metaGetContact(ccs->hContact)); + HistoryLog(metaGetContact(ccs->hContact), "PGP Encryption turned on by key autoexchange feature", EVENTTYPE_MESSAGE, 0); + } + HistoryLog(ccs->hContact, "PGP Encryption turned on by key autoexchange feature", EVENTTYPE_MESSAGE, 0); + } } + return 1; } } - } - if(!isContactSecured(hContact)) - return CallService(MS_PROTO_CHAINSEND, w, l); - - { //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((str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")) != wstring::npos)) + { + s2 = str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")); + s1 = str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")); } + else if((str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----END PGP PRIVATE KEY BLOCK-----")) != wstring::npos)) { - string out; - DWORD code; - wstring cmd; - wstring path; - extern bool bJabberAPI, bIsMiranda09; - char *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID", ""); - if(!tmp[0]) + s2 = str.find(_T("-----END PGP PRIVATE KEY BLOCK-----")); + s1 = str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")); + } + if((s2 != wstring::npos) && (s1 != wstring::npos)) + { //this is public key + if(metaIsSubcontact(ccs->hContact)) { - mir_free(tmp); - HistoryLog(hContact, "Failed to encrypt message with GPG", EVENTTYPE_MESSAGE, DBEF_SENT); - return CallService(MS_PROTO_CHAINSEND, w, l); + HistoryLog(ccs->hContact, msg, EVENTTYPE_MESSAGE, dbflags); + return 0; } - 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("\""); + debuglog<<time_str()<<": info: "<<"received key from: "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)ccs->hContact, GCDNF_TCHAR)<<"\n"; + s1 = 0; + while((s1 = str.find(_T("\r"), s1)) != wstring::npos) { - char *tmp; - tmp = mir_strdup(toUTF8(str).c_str()); - fstream f(path.c_str(), std::ios::out); - f<<tmp; - mir_free(tmp); - f.close(); + str.erase(s1, 1); } - gpg_execution_params params; - pxResult result; - params.cmd = &cmd; - params.useless = ""; - params.out = &out; - params.code = &code; - params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + void ShowNewKeyDialog(); + if((str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")) != wstring::npos)) { - gpg_thread.~thread(); - debuglog<<time_str()<<": GPG execution timed out, aborted\n"; - return CallService(MS_PROTO_CHAINSEND, w, l); + s2 = str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")); + s1 = str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")); + s2 += _tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----")); } - if(result == pxNotFound) - return CallService(MS_PROTO_CHAINSEND, w, l); - if(out.find("There is no assurance this key belongs to the named user") != string::npos) + else if((str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----END PGP PRIVATE KEY BLOCK-----")) != wstring::npos)) { - out.clear(); - if(bAutoExchange) + s2 = str.find(_T("-----END PGP PRIVATE KEY BLOCK-----")); + s1 = str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")); + s2 += _tcslen(_T("-----END PGP PRIVATE KEY BLOCK-----")); + } + new_key.append(str.substr(s1,s2-s1)); + new_key_hcnt_mutex.lock(); + new_key_hcnt = ccs->hContact; + ShowNewKeyDialog(); + HistoryLog(ccs->hContact, msg, EVENTTYPE_MESSAGE, dbflags); + return 0; + } + if(bAutoExchange && strstr(msg, "-----PGP KEY REQUEST-----") && isGPGConfigured()) + { + char *tmp = UniGetContactSettingUtf(NULL, szGPGModuleName, "GPGPubKey", ""); + if(tmp[0]) + { + 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); + } + mir_free(tmp); + if(!isContactHaveKey(ccs->hContact) && bAutoExchange && isGPGConfigured()) + { + LPSTR proto = (LPSTR)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ccs->hContact, 0); + DWORD uin = DBGetContactSettingDword(ccs->hContact, proto, "UIN", 0); + if(uin) { - DBWriteContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 1); - cmd.insert(0, _T("--trust-model always ")); - gpg_execution_params params; - pxResult result; - params.cmd = &cmd; - params.useless = ""; - params.out = &out; - params.code = &code; - params.result = &result; - boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + if(ServiceExists("ICQ"PS_ICQ_CHECKCAPABILITY)) { - gpg_thread.~thread(); - debuglog<<time_str()<<": GPG execution timed out, aborted\n"; - return CallService(MS_PROTO_CHAINSEND, w, l); + 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-----"); } - if(result == pxNotFound) - return CallService(MS_PROTO_CHAINSEND, w, l); } - else if(MessageBox(0, _T("We trying to encrypt with untrusted key, do you want to trust this key permanently ?"), _T("Warning"), MB_YESNO) == IDYES) + else + { + TCHAR *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", _T("")); + if(jid[0]) + { + extern list <JabberAccount*> Accounts; + list<JabberAccount*>::iterator end = Accounts.end(); + for(list<JabberAccount*>::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)0, (LPARAM)"-----PGP KEY REQUEST-----"); + } + } + } + } + } + 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)); + return returnNoError(ccs->hContact); +} + +int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) +{ + wstring str; + bool isansi = false; + DWORD dbflags = 0; + if(flags & PREF_UTF) + dbflags |= DBEF_UTF; + 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); + } + string out; + DWORD code; + wstring cmd; + wstring file = toUTF16(get_random(10)); + 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); + } + 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("\\tmp\\"); + cmd += file; + path.append(_T("\\tmp\\")); + path += file; + cmd += _T("\""); + { + char *tmp; + tmp = mir_strdup(toUTF8(str).c_str()); + fstream f(path.c_str(), std::ios::out); + while(!f.is_open()) + f.open(path.c_str(), std::ios::out); + f<<tmp; + mir_free(tmp); + f.close(); + } + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) + { + gpg_thread.~thread(); + TerminateProcess(params.hProcess, 1); + debuglog<<time_str()<<": GPG execution timed out, aborted\n"; + mir_free(msg); + return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); + } + if(result == pxNotFound) + { + mir_free(msg); + return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); + } + if(out.find("There is no assurance this key belongs to the named user") != string::npos) + { + out.clear(); + if(bAutoExchange) { DBWriteContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 1); cmd.insert(0, _T("--trust-model always ")); @@ -748,127 +700,213 @@ int SendMsgSvc(WPARAM w, LPARAM l) if(result == pxNotFound) return CallService(MS_PROTO_CHAINSEND, w, l); } - else - return 0; - } - if(out.find("usage: ") != string::npos) + else if(MessageBox(0, _T("We trying to encrypt with untrusted key, do you want to trust this key permanently ?"), _T("Warning"), MB_YESNO) == IDYES) + { + DBWriteContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 1); + cmd.insert(0, _T("--trust-model always ")); + gpg_execution_params params; + pxResult result; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + boost::thread gpg_thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread.timed_join(boost::posix_time::seconds(10))) { - MessageBox(0, _T("Something wrong, gpg does not understand us, aborting encryption."), _T("Warning"), MB_OK); - DeleteFile(path.c_str()); - return CallService(MS_PROTO_CHAINSEND, w, l); + gpg_thread.~thread(); + TerminateProcess(params.hProcess, 1); + debuglog<<time_str()<<": GPG execution timed out, aborted\n"; + mir_free(msg); + return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } - DeleteFile(path.c_str()); - path.append(_T(".asc")); - wfstream f(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary); - str.clear(); - if(f.is_open()) + if(result == pxNotFound) { - std::wifstream::pos_type size = f.tellg(); - TCHAR *tmp = new TCHAR [(std::ifstream::pos_type)size+(std::ifstream::pos_type)1]; - f.seekg(0, std::ios::beg); - f.read(tmp, size); - tmp[size]= '\0'; - str.append(tmp); - delete [] tmp; - f.close(); - DeleteFile(path.c_str()); + mir_free(msg); + return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } - if(str.empty()) + } + else + { + mir_free(msg); + return 0; + } + } + if(out.find("usage: ") != string::npos) + { + MessageBox(0, _T("Something wrong, gpg does not understand us, aborting encryption."), _T("Warning"), MB_OK); + DeleteFile(path.c_str()); + mir_free(msg); + return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); + } + DeleteFile(path.c_str()); + path.append(_T(".asc")); + wfstream f(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary); + while(!f.is_open()) + f.open(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary); + str.clear(); + if(f.is_open()) + { + std::wifstream::pos_type size = f.tellg(); + TCHAR *tmp = new TCHAR [(std::ifstream::pos_type)size+(std::ifstream::pos_type)1]; + f.seekg(0, std::ios::beg); + f.read(tmp, size); + tmp[size]= '\0'; + str.append(tmp); + delete [] tmp; + f.close(); + DeleteFile(path.c_str()); + } + if(str.empty()) + { + 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"); + debuglog<<time_str()<<": info: Failed to encrypt message with GPG\n"; + mir_free(msg); + return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); + } + string str_event = msg; + if(bAppendTags) + { + str_event.insert(0, toUTF8(outopentag)); + str_event.append(toUTF8(outclosetag)); + } + if(metaIsSubcontact(hContact)) + { + hcontact_data[metaGetContact(hContact)].msgs_to_pass.push_back(str_event); + HistoryLog(metaGetContact(hContact), (char*)str_event.c_str(), EVENTTYPE_MESSAGE, DBEF_SENT|dbflags); + } + hcontact_data[hContact].msgs_to_pass.push_back(str_event); + HistoryLog(hContact, (char*)str_event.c_str(), EVENTTYPE_MESSAGE, dbflags|DBEF_SENT); + if(!(flags & PREF_UTF)) + flags |= PREF_UTF; + CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)toUTF8(str).c_str()); + mir_free(msg); + return 0; +} + +int SendMsgSvc(WPARAM w, LPARAM l) +{ + CCSDATA *ccs = (CCSDATA*)l; + if (!ccs) + return CallService(MS_PROTO_CHAINSEND, w, l); + char *msg = mir_strdup((char*)(ccs->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) { - HistoryLog(hContact, "Failed to encrypt message with GPG", EVENTTYPE_MESSAGE, DBEF_SENT); - debuglog<<time_str()<<": info: Failed to encrypt message with GPG"; - return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs); + 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); + } + } } - if(metaIsSubcontact(hContact)) - { //dirty hack to avoid metacontacts problem ..., this also broke filters chain - string str_event = (char*)ccs->lParam; - if(bAppendTags) - { //utf8 tag will cause problems here - str_event.insert(0, toUTF8(outopentag)); - str_event.append(toUTF8(outclosetag)); + else + { + TCHAR *jid = UniGetContactSettingUtf(ccs->hContact, proto, "jid", _T("")); + if(jid[0]) + { + extern list <JabberAccount*> Accounts; + list<JabberAccount*>::iterator end = Accounts.end(); + for(list<JabberAccount*>::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); + } + } + } } - DWORD flags = 0, flags2 = 0; - if(!isansi) //message from metacontact itself, yet another metacontacts workaround... - flags |= DBEF_UTF; - else - flags2 |= DBEF_READ; - HistoryLog(hContact, (char*)str_event.c_str(), EVENTTYPE_MESSAGE, DBEF_SENT | flags2 | flags); - HistoryLog(metaGetContact(hContact), (char*)str_event.c_str(), EVENTTYPE_MESSAGE, DBEF_SENT | flags); - hcontact_data[hContact].msgs_to_ignore.push_back(msg); - hcontact_data[metaGetContact(hContact)].msgs_to_ignore.push_back(msg); - CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)toUTF8(str).c_str()); - return returnNoError(hContact); } - if(bAppendTags) - hcontact_data[hContact].msgs_to_tag.push_back((char*)ccs->lParam); - ccs->lParam = (LPARAM)mir_strdup(toUTF8(str).c_str()); - } + } + else + { + mir_free(msg); + return CallService(MS_PROTO_CHAINSEND, w, l); + } } - return CallService(MS_PROTO_CHAINSEND, w, l); + if(metaIsProtoMetaContacts(ccs->hContact) || !isContactSecured(ccs->hContact)) + { + 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<string>::iterator end = hcontact_data[hContact].msgs_to_ignore.end(); - for(list<string>::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<string>::iterator end = hcontact_data[hContact].msgs_to_pass.end(); + for(std::list<string>::iterator i = hcontact_data[hContact].msgs_to_pass.begin(); i != end; ++i) { - std::list<string>::iterator end = hcontact_data[hContact].msgs_to_tag.end(); - for(std::list<string>::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(!isContactSecured(hContact)) + return 0; + if(!(dbei->flags & DBEF_SENT) && metaIsProtoMetaContacts((HANDLE)w)) { char tmp[29]; strncpy(tmp, (char*)dbei->pBlob, 27); @@ -879,11 +917,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) { |