From 4d9fce6f544f0c6782a7ade4900afd3e1c6c9c52 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Sat, 29 Jan 2011 15:17:51 +0300 Subject: Better metacontacts handling\nFixed subkey length in keygen --- utilities.cpp | 388 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 194 insertions(+), 194 deletions(-) (limited to 'utilities.cpp') diff --git a/utilities.cpp b/utilities.cpp index b0f72c0..0b49e61 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -275,37 +275,37 @@ list transfers; int onProtoAck(WPARAM w, LPARAM l) { - ACKDATA *ack=(ACKDATA*)l; - CCSDATA *ccs=(CCSDATA*)ack->lParam; - if (ack->type!=ACKTYPE_FILE) - return 0; - PROTOFILETRANSFERSTATUS *f = (PROTOFILETRANSFERSTATUS*) ack->lParam; -// if(!f) -// f = (PROTOFILETRANSFERSTATUS*) ack->hProcess; - if(!f) - return 0; - - switch(ack->result) - { - case ACKRESULT_DENIED: case ACKRESULT_FAILED: - break; - case ACKRESULT_SUCCESS: - { - TCHAR *filename = NULL; -// if(f->szCurrentFile && f->szCurrentFile[0]) -// filename = mir_utf8decodeW(f->szCurrentFile); - if(f->tszCurrentFile && f->tszCurrentFile[0]) - filename = mir_wstrdup(f->tszCurrentFile); - if(!filename) - break; - TCHAR *temp = _wgetenv(_T("TEMP")); - if(_tcsstr(filename, temp)) - { - mir_free(filename); - break; - } - else if(_tcsstr(filename, _T(".gpg"))) //decrypt it - { //process encrypted file + ACKDATA *ack=(ACKDATA*)l; + CCSDATA *ccs=(CCSDATA*)ack->lParam; + if (ack->type!=ACKTYPE_FILE) + return 0; + PROTOFILETRANSFERSTATUS *f = (PROTOFILETRANSFERSTATUS*) ack->lParam; +// if(!f) +// f = (PROTOFILETRANSFERSTATUS*) ack->hProcess; + if(!f) + return 0; + + switch(ack->result) + { + case ACKRESULT_DENIED: case ACKRESULT_FAILED: + break; + case ACKRESULT_SUCCESS: + { + TCHAR *filename = NULL; +// if(f->szCurrentFile && f->szCurrentFile[0]) +// filename = mir_utf8decodeW(f->szCurrentFile); + if(f->tszCurrentFile && f->tszCurrentFile[0]) + filename = mir_wstrdup(f->tszCurrentFile); + if(!filename) + break; + TCHAR *temp = _wgetenv(_T("TEMP")); + if(_tcsstr(filename, temp)) + { + mir_free(filename); + break; + } + else if(_tcsstr(filename, _T(".gpg"))) //decrypt it + { //process encrypted file if(_waccess(f->tszCurrentFile, 0) == -1) { if(errno == ENOENT) @@ -342,13 +342,13 @@ int onProtoAck(WPARAM w, LPARAM l) delete gpg_thread; debuglog< - -struct TFakeAckParams { - inline TFakeAckParams( HANDLE p1, HANDLE p2, LONG p3, LPCSTR p4 ) : - hEvent( p1 ), - hContact( p2 ), - id( p3 ), - msg( p4 ) - {} - - HANDLE hEvent; - HANDLE hContact; - LONG id; - LPCSTR msg; -}; - -int SendBroadcast( HANDLE hContact, int type, int result, HANDLE hProcess, LPARAM lParam ) { - ACKDATA ack; - memset(&ack,0,sizeof(ack)); - ack.cbSize = sizeof( ACKDATA ); - ack.szModule = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0); - ack.hContact = hContact; - ack.type = type; - ack.result = result; - ack.hProcess = hProcess; - ack.lParam = lParam; - return CallService( MS_PROTO_BROADCASTACK, 0, ( LPARAM )&ack ); -} - - - -unsigned __stdcall sttFakeAck( LPVOID param ) { - - TFakeAckParams* tParam = ( TFakeAckParams* )param; - WaitForSingleObject( tParam->hEvent, INFINITE ); - - Sleep( 100 ); - if ( tParam->msg == NULL ) - SendBroadcast( tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, ( HANDLE )tParam->id, 0 ); - else - SendBroadcast( tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, ( HANDLE )tParam->id, LPARAM( tParam->msg )); - - CloseHandle( tParam->hEvent ); - delete tParam; - - return 0; -} - - -int returnNoError(HANDLE hContact) { - HANDLE hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); - unsigned int tID; - CloseHandle( (HANDLE) _beginthreadex(NULL, 0, sttFakeAck, new TFakeAckParams(hEvent,hContact,777,0), 0, &tID) ); - SetEvent( hEvent ); - return 777; -} - -string toUTF8(wstring str) -{ - string ustr; - wstring tmpstr; - utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr)); - utf8::utf16to8(tmpstr.begin(), tmpstr.end(), back_inserter(ustr)); - return ustr; -} - -string toUTF8(string str) -{ - string ustr; - wstring tmpstr; - utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr)); - utf8::utf16to8(tmpstr.begin(), tmpstr.end(), back_inserter(ustr)); - return ustr; -} - - -wstring toUTF16(string str) //convert as much as possible -{ - wstring ustr; - string tmpstr; - utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr)); - utf8::utf8to16(tmpstr.begin(), tmpstr.end(), back_inserter(ustr)); - return ustr; -} - -string get_random(int length) -{ - string chars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"); - string data; - boost::random_device rng; - boost::variate_generator> gen(rng, boost::uniform_int<>(0, chars.length()-1)); - for(int i = 0; i < length; ++i) - data += chars[gen()]; - return data; -} - -void send_encrypted_msgs_thread(HANDLE hContact) -{ - while(true) - { - char *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", ""); - while(!isContactSecured(hContact)) - boost::this_thread::sleep(boost::posix_time::seconds(1)); - if(!hcontact_data[hContact].msgs_to_send.empty()) - { - boost::this_thread::sleep(boost::posix_time::seconds(1)); - list::iterator end = hcontact_data[hContact].msgs_to_send.end(); - for(list::iterator p = hcontact_data[hContact].msgs_to_send.begin(); p != end; ++p) - { - CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)p->c_str()); - HistoryLog(hContact, (char*)p->c_str(), EVENTTYPE_MESSAGE, DBEF_SENT); - boost::this_thread::sleep(boost::posix_time::seconds(1)); - } - return; - } - else - return; - } -} - +#define NEWTSTR_MALLOC(A) (A==NULL)?NULL:strcpy((char*)mir_alloc(sizeof(char)*(strlen(A)+1)),A) + +const bool StriStr(const char *str, const char *substr) +{ + bool i = false; + char *str_up = NEWTSTR_MALLOC(str); + char *substr_up = NEWTSTR_MALLOC(substr); + + CharUpperBuffA(str_up, strlen(str_up)); + CharUpperBuffA(substr_up, strlen(substr_up)); + + if(strstr (str_up, substr_up)) + i = true; + + mir_free(str_up); + mir_free(substr_up); + + return i; +} + +bool IsOnline(HANDLE hContact) +{ + if(DBGetContactSettingByte(hContact, szGPGModuleName, "Status", 0) == ID_STATUS_OFFLINE) + return false; + return true; +} + +//from secureim +#include + +struct TFakeAckParams { + inline TFakeAckParams( HANDLE p1, HANDLE p2, LONG p3, LPCSTR p4 ) : + hEvent( p1 ), + hContact( p2 ), + id( p3 ), + msg( p4 ) + {} + + HANDLE hEvent; + HANDLE hContact; + LONG id; + LPCSTR msg; +}; + +int SendBroadcast( HANDLE hContact, int type, int result, HANDLE hProcess, LPARAM lParam ) { + ACKDATA ack; + memset(&ack,0,sizeof(ack)); + ack.cbSize = sizeof( ACKDATA ); + ack.szModule = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0); + ack.hContact = hContact; + ack.type = type; + ack.result = result; + ack.hProcess = hProcess; + ack.lParam = lParam; + return CallService( MS_PROTO_BROADCASTACK, 0, ( LPARAM )&ack ); +} + + + +unsigned __stdcall sttFakeAck( LPVOID param ) { + + TFakeAckParams* tParam = ( TFakeAckParams* )param; + WaitForSingleObject( tParam->hEvent, INFINITE ); + + Sleep( 100 ); + if ( tParam->msg == NULL ) + SendBroadcast( tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, ( HANDLE )tParam->id, 0 ); + else + SendBroadcast( tParam->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, ( HANDLE )tParam->id, LPARAM( tParam->msg )); + + CloseHandle( tParam->hEvent ); + delete tParam; + + return 0; +} + + +int returnNoError(HANDLE hContact) { + HANDLE hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); + unsigned int tID; + CloseHandle( (HANDLE) _beginthreadex(NULL, 0, sttFakeAck, new TFakeAckParams(hEvent,hContact,777,0), 0, &tID) ); + SetEvent( hEvent ); + return 777; +} + +string toUTF8(wstring str) +{ + string ustr; + wstring tmpstr; + utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr)); + utf8::utf16to8(tmpstr.begin(), tmpstr.end(), back_inserter(ustr)); + return ustr; +} + +string toUTF8(string str) +{ + string ustr; + wstring tmpstr; + utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr)); + utf8::utf16to8(tmpstr.begin(), tmpstr.end(), back_inserter(ustr)); + return ustr; +} + + +wstring toUTF16(string str) //convert as much as possible +{ + wstring ustr; + string tmpstr; + utf8::replace_invalid(str.begin(), str.end(), back_inserter(tmpstr)); + utf8::utf8to16(tmpstr.begin(), tmpstr.end(), back_inserter(ustr)); + return ustr; +} + +string get_random(int length) +{ + string chars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"); + string data; + boost::random_device rng; + boost::variate_generator> gen(rng, boost::uniform_int<>(0, chars.length()-1)); + for(int i = 0; i < length; ++i) + data += chars[gen()]; + return data; +} + +void send_encrypted_msgs_thread(HANDLE hContact) +{ + while(true) + { + char *key = UniGetContactSettingUtf(hContact, szGPGModuleName, "GPGPubKey", ""); + while(!isContactSecured(hContact)) + boost::this_thread::sleep(boost::posix_time::seconds(1)); + if(!hcontact_data[hContact].msgs_to_send.empty()) + { + boost::this_thread::sleep(boost::posix_time::seconds(1)); + list::iterator end = hcontact_data[hContact].msgs_to_send.end(); + for(list::iterator p = hcontact_data[hContact].msgs_to_send.begin(); p != end; ++p) + { + CallContactService(hContact, PSS_MESSAGE, (WPARAM)PREF_UTF, (LPARAM)p->c_str()); + HistoryLog(hContact, (char*)p->c_str(), EVENTTYPE_MESSAGE, DBEF_SENT); + boost::this_thread::sleep(boost::posix_time::seconds(1)); + } + return; + } + else + return; + } +} + string time_str() -{ - boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); +{ + boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); return (string)boost::posix_time::to_simple_string(now); } -- cgit v1.2.3