diff options
Diffstat (limited to 'messages.cpp')
-rw-r--r-- | messages.cpp | 62 |
1 files changed, 43 insertions, 19 deletions
diff --git a/messages.cpp b/messages.cpp index 05d138a..6875ece 100644 --- a/messages.cpp +++ b/messages.cpp @@ -19,18 +19,16 @@ wstring new_key; HANDLE new_key_hcnt = NULL; -boost::mutex new_key_hcnt_mutex; +boost::mutex new_key_hcnt_mutex, gpg_file_mutex; bool _terminate = false; int returnNoError(HANDLE hContact); int RecvMsgSvc_func(HANDLE hContact, 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; - DWORD dbflags = 0; - if(flags & PREF_UTF) - flags |= DBEF_UTF; if((str.find(_T("-----PGP KEY RESPONSE-----")) != wstring::npos) && !metaIsProtoMetaContacts(hContact)) { @@ -54,7 +52,10 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) _tcscat(tmp2, _T("\\")); _tcscat(tmp2, _T("temporary_exported.asc")); DeleteFile(tmp2); + gpg_file_mutex.timed_lock(boost::posix_time::minutes(1)); wfstream f(tmp2, std::ios::out); + while(!f.is_open()) + f.open(tmp2, std::ios::out); ptmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", _T("")); wstring new_key = ptmp; mir_free(ptmp); @@ -78,13 +79,16 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) gpg_thread.~thread(); debuglog<<time_str()<<": GPG execution timed out, aborted\n"; mir_free(msg); + gpg_file_mutex.unlock(); return 1; } if(result == pxNotFound) { mir_free(msg); + gpg_file_mutex.unlock(); return 1; } + gpg_file_mutex.unlock(); { char *tmp = NULL; string::size_type s = output.find("gpg: key ") + strlen("gpg: key "); @@ -308,7 +312,10 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) wstring path = tmp2; path.append(_T("\\encrypted_data.asc")); DeleteFile(path.c_str()); + gpg_file_mutex.timed_lock(boost::posix_time::minutes(1)); 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(); @@ -380,6 +387,7 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) DeleteFile(path.c_str()); HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); mir_free(msg); + gpg_file_mutex.unlock(); return 0; } if(result == pxNotFound) @@ -387,6 +395,7 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) DeleteFile(path.c_str()); HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); mir_free(msg); + gpg_file_mutex.unlock(); return 0; } _terminate = false; @@ -431,6 +440,7 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) DeleteFile(path.c_str()); HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); mir_free(msg); + gpg_file_mutex.unlock(); return 0; } if(result == pxNotFound) @@ -438,6 +448,7 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) DeleteFile(path.c_str()); HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); mir_free(msg); + gpg_file_mutex.unlock(); return 0; } } @@ -450,6 +461,7 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) DeleteFile(path.c_str()); HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); mir_free(msg); + gpg_file_mutex.unlock(); return 0; } if(result == pxNotFound) @@ -477,17 +489,18 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) HistoryLog(hContact, tmp, EVENTTYPE_MESSAGE, dbflags); mir_free(tmp); mir_free(msg); + gpg_file_mutex.unlock(); return 0; } } } - - + gpg_file_mutex.unlock(); str.clear(); { wstring path = tmp2; mir_free(tmp2); path += _T("\\decrypted_data"); + gpg_file_mutex.timed_lock(boost::posix_time::minutes(1)); fstream f(path.c_str(), std::ios::in | std::ios::ate | std::ios::binary); if(f.is_open()) { @@ -501,7 +514,10 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) delete [] tmp; f.close(); DeleteFile(path.c_str()); + gpg_file_mutex.unlock(); } + else + gpg_file_mutex.unlock(); if(str.empty()) { string str = msg; @@ -529,11 +545,10 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) return 1; } char *tmp = mir_strdup(toUTF8(str).c_str()); - if(!(flags & DBEF_UTF)) - flags |= DBEF_UTF; HistoryLog(hContact, tmp, EVENTTYPE_MESSAGE, dbflags); mir_free(tmp); mir_free(msg); + gpg_file_mutex.unlock(); return 0; } } @@ -544,19 +559,16 @@ int RecvMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) { if(metaIsSubcontact(hContact)) { - HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, DBEF_UTF| DBEF_READ); - HistoryLog(metaGetContact(hContact), msg, EVENTTYPE_MESSAGE, DBEF_UTF); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags| DBEF_READ); + HistoryLog(metaGetContact(hContact), msg, EVENTTYPE_MESSAGE, dbflags); mir_free(msg); return 0; } - string str = msg; - char *tmp = mir_strdup(str.c_str()); - HistoryLog(hContact, tmp, EVENTTYPE_MESSAGE, flags); - mir_free(tmp); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags|DBEF_READ); mir_free(msg); return 0; } - HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, flags); + HistoryLog(hContact, msg, EVENTTYPE_MESSAGE, dbflags); mir_free(msg); return 0; } @@ -630,7 +642,10 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) { char *tmp; tmp = mir_strdup(toUTF8(str).c_str()); + gpg_file_mutex.timed_lock(boost::posix_time::minutes(1)); 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(); @@ -648,11 +663,13 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) gpg_thread.~thread(); debuglog<<time_str()<<": GPG execution timed out, aborted\n"; mir_free(msg); + gpg_file_mutex.unlock(); return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } if(result == pxNotFound) { mir_free(msg); + gpg_file_mutex.unlock(); 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) @@ -675,17 +692,20 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) gpg_thread.~thread(); debuglog<<time_str()<<": GPG execution timed out, aborted\n"; mir_free(msg); + gpg_file_mutex.unlock(); return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } if(result == pxNotFound) { mir_free(msg); + gpg_file_mutex.unlock(); return CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)msg); } } else { mir_free(msg); + gpg_file_mutex.unlock(); return 0; } } @@ -694,11 +714,14 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) MessageBox(0, _T("Something wrong, gpg does not understand us, aborting encryption."), _T("Warning"), MB_OK); DeleteFile(path.c_str()); mir_free(msg); + gpg_file_mutex.unlock(); 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()) { @@ -712,6 +735,7 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) f.close(); DeleteFile(path.c_str()); } + gpg_file_mutex.unlock(); if(str.empty()) { HistoryLog(hContact, "Failed to encrypt message with GPG", EVENTTYPE_MESSAGE, DBEF_SENT); @@ -729,13 +753,13 @@ int SendMsgSvc_func(HANDLE hContact, char *msg, DWORD flags) 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); + 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)PREF_UTF, (LPARAM)toUTF8(str).c_str()); + flags |= PREF_UTF; + CallContactService(hContact, PSS_MESSAGE, (WPARAM)flags, (LPARAM)toUTF8(str).c_str()); mir_free(msg); return 0; } |