diff options
-rw-r--r-- | utilities.cpp | 172 |
1 files changed, 101 insertions, 71 deletions
diff --git a/utilities.cpp b/utilities.cpp index a2ca82b..34307cc 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -440,6 +440,76 @@ int onProtoAck(WPARAM w, LPARAM l) return 0; } +std::wstring encrypt_file(HANDLE hContact, TCHAR *filename) +{ + string out; + DWORD code; + pxResult result; + wstring cmd = _T("--batch --yes -r "); + char *keyid = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID", ""); + if(DBGetContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 0)) + cmd += _T("--trust-model always "); + TCHAR *szKeyid = mir_a2t(keyid); + TCHAR *name = _tcsrchr(filename,_T('\\')); + if( !name ) + name = filename; + else + name++; + TCHAR *file_out = new TCHAR [_tcslen(filename)+4]; + mir_sntprintf(file_out, _tcslen(name)+7, _T("%s.gpg"), name); + cmd += szKeyid; + mir_free(szKeyid); + mir_free(keyid); + cmd += _T(" -o \""); + TCHAR *temp = _tgetenv(_T("TEMP")); + cmd += temp; + cmd += _T("\\"); + cmd += file_out; + wstring path_out = temp; + path_out += _T("\\"); + path_out += file_out; + DeleteFile(path_out.c_str()); + cmd += _T("\" "); + mir_free(temp); + cmd += _T(" -e \""); + cmd += filename; + cmd += _T("\" "); + gpg_execution_params params; + params.cmd = &cmd; + params.useless = ""; + params.out = &out; + params.code = &code; + params.result = &result; + mir_free(keyid); + delete [] file_out; + boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread->timed_join(boost::posix_time::seconds(180))) + { + delete gpg_thread; + debuglog<<time_str()<<": GPG execution timed out, aborted\n"; + return 0; + } + if(out.find("There is no assurance this key belongs to the named user") != string::npos) + { + out.clear(); + 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_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); + if(!gpg_thread->timed_join(boost::posix_time::seconds(180))) + { + delete gpg_thread; + debuglog<<time_str()<<": GPG execution timed out, aborted\n"; + return 0; + } + } + else + return 0; + } + return path_out; +} + //from secureim partially int onSendFile(WPARAM w, LPARAM l) { @@ -448,84 +518,44 @@ int onSendFile(WPARAM w, LPARAM l) CCSDATA *ccs=(CCSDATA*)l; if(isContactSecured(ccs->hContact)) { - TCHAR **file=(TCHAR **)ccs->lParam; + DWORD flags = (DWORD)ccs->wParam; //check for PFTS_UNICODE here int i; - for(i = 0; file[i]; i++) + if(flags & PFTS_UNICODE) { - if (_tcsstr(file[i],_T(".gpg"))) - continue; - if(_waccess(file[i], 0) == -1) - if(errno == ENOENT) - return 0; //we do not want to send file unencrypted (sometimes ack have wrong info) - TCHAR *name = _tcsrchr(file[i],_T('\\')); - if( !name ) - name = file[i]; - else - name++; - TCHAR *file_out = new TCHAR [_tcslen(file[i])+4]; - mir_sntprintf(file_out, _tcslen(name)+7, _T("%s.gpg"), name); - string out; - DWORD code; - pxResult result; - wstring cmd = _T("--batch --yes -r "); - char *keyid = UniGetContactSettingUtf(ccs->hContact, szGPGModuleName, "KeyID", ""); - if(DBGetContactSettingByte(ccs->hContact, szGPGModuleName, "bAlwaysTrust", 0)) - cmd += _T("--trust-model always "); - TCHAR *szKeyid = mir_a2t(keyid); - cmd += szKeyid; - mir_free(szKeyid); - mir_free(keyid); - cmd += _T(" -o \""); - TCHAR *temp = _tgetenv(_T("TEMP")); - cmd += temp; - cmd += _T("\\"); - cmd += file_out; - wstring path_out = temp; - path_out += _T("\\"); - path_out += file_out; - DeleteFile(path_out.c_str()); - cmd += _T("\" "); - mir_free(temp); - cmd += _T(" -e \""); - cmd += file[i]; - cmd += _T("\" "); - gpg_execution_params params; - params.cmd = &cmd; - params.useless = ""; - params.out = &out; - params.code = &code; - params.result = &result; - mir_free(keyid); - boost::thread *gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(180))) + TCHAR **file=(TCHAR **)ccs->lParam; + for(i = 0; file[i]; i++) { - delete gpg_thread; - debuglog<<time_str()<<": GPG execution timed out, aborted\n"; - return 0; + if(_waccess(file[i], 0) == -1) + if(errno == ENOENT) + return 0; //we do not want to send file unencrypted (sometimes ack have wrong info) + if (_tcsstr(file[i],_T(".gpg"))) + continue; + std::wstring path_out = encrypt_file(ccs->hContact, file[i]); + mir_free(file[i]); + file[i] = mir_tstrdup(path_out.c_str()); + transfers.push_back(path_out); } - if(out.find("There is no assurance this key belongs to the named user") != string::npos) + } + else + { + char **file = (char**) ccs->lParam; + for(i = 0; file[i]; i++) { - out.clear(); - 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(ccs->hContact, szGPGModuleName, "bAlwaysTrust", 1); - cmd.insert(0, _T("--trust-model always ")); - gpg_thread = new boost::thread(boost::bind(&pxEexcute_thread, ¶ms)); - if(!gpg_thread->timed_join(boost::posix_time::seconds(180))) - { - delete gpg_thread; - debuglog<<time_str()<<": GPG execution timed out, aborted\n"; - return 0; - } - } - else - return 0; + if(_access(file[i], 0) == -1) + if(errno == ENOENT) + return 0; //we do not want to send file unencrypted (sometimes ack have wrong info) + if (strstr(file[i],".gpg")) + continue; + TCHAR *tmp = mir_utf8decodeT(file[i]); + std::wstring path_out = encrypt_file(ccs->hContact, tmp); + mir_free(tmp); + char* tmp2 = mir_utf8encodeW(path_out.c_str()); + mir_free(file[i]); + file[i] = tmp2; + transfers.push_back(path_out); + } - mir_free(file[i]); - file[i] = mir_tstrdup(path_out.c_str()); - delete [] file_out; - transfers.push_back(path_out); } } return CallService(MS_PROTO_CHAINSEND, w, l); |