int CToxProto::OnAccountRenamed(WPARAM, LPARAM) { mir_cslock locker(profileLock); ptrT newPath(GetToxProfilePath()); TCHAR oldPath[MAX_PATH]; mir_sntprintf(oldPath, MAX_PATH, _T("%s\\%s.tox"), VARST(_T("%miranda_userdata%")), accountName); _trename(oldPath, newPath); mir_free(accountName); accountName = mir_tstrdup(m_tszUserName); return 0; }
void CToxProto::SaveToxProfile(CToxThread *toxThread) { mir_cslock locker(profileLock); if (!toxThread) return; size_t size = tox_get_savedata_size(toxThread->Tox()); uint8_t *data = (uint8_t*)mir_calloc(size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH); tox_get_savedata(toxThread->Tox(), data); pass_ptrA password(mir_utf8encodeW(pass_ptrT(getTStringA("Password")))); if (password && mir_strlen(password)) { TOX_ERR_ENCRYPTION coreEncryptError; if (!tox_pass_encrypt(data, size, (uint8_t*)(char*)password, mir_strlen(password), data, &coreEncryptError)) { debugLogA(__FUNCTION__": failed to encrypt tox profile"); mir_free(data); return; } size += TOX_PASS_ENCRYPTION_EXTRA_LENGTH; } ptrT profilePath(GetToxProfilePath()); FILE *profile = _tfopen(profilePath, _T("wb")); if (profile == NULL) { debugLogA(__FUNCTION__": failed to open tox profile"); mir_free(data); return; } size_t written = fwrite(data, sizeof(char), size, profile); if (size != written) { debugLogA(__FUNCTION__": failed to write tox profile"); } fclose(profile); mir_free(data); }
void CToxProto::SaveToxProfile() { mir_cslock locker(profileLock); size_t size = tox_get_savedata_size(tox); uint8_t *data = (uint8_t*)mir_calloc(size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH); tox_get_savedata(tox, data); size_t passwordLen = mir_strlen(password); if (password && passwordLen) { TOX_ERR_ENCRYPTION coreEncryptError; if (!tox_pass_encrypt(data, size, (uint8_t*)password, passwordLen, data, &coreEncryptError)) { debugLogA(__FUNCTION__": failed to encrypt tox profile"); mir_free(data); return; } size += TOX_PASS_ENCRYPTION_EXTRA_LENGTH; } std::tstring profilePath = GetToxProfilePath(); FILE *profile = _tfopen(profilePath.c_str(), _T("wb")); if (profile == NULL) { debugLogA(__FUNCTION__": failed to open tox profile"); mir_free(data); return; } size_t written = fwrite(data, sizeof(char), size, profile); if (size != written) { debugLogA(__FUNCTION__": failed to write tox profile"); } fclose(profile); mir_free(data); }
TCHAR* CToxProto::GetToxProfilePath() { return GetToxProfilePath(m_tszUserName); }
bool CToxProto::LoadToxProfile(Tox_Options *options) { debugLogA(__FUNCTION__": loading tox profile"); mir_cslock locker(profileLock); ptrT profilePath(GetToxProfilePath()); if (!IsFileExists(profilePath)) return false; FILE *profile = _tfopen(profilePath, _T("rb")); if (profile == NULL) { ShowNotification(TranslateT("Unable to open Tox profile"), MB_ICONERROR); debugLogA(__FUNCTION__": failed to open tox profile"); return false; } fseek(profile, 0, SEEK_END); long size = ftell(profile); rewind(profile); if (size < 0) { fclose(profile); return false; } if (size == 0) { fclose(profile); return true; } uint8_t *data = (uint8_t*)mir_calloc(size); if (fread((char*)data, sizeof(char), size, profile) != (size_t)size) { fclose(profile); ShowNotification(TranslateT("Unable to read Tox profile"), MB_ICONERROR); debugLogA(__FUNCTION__": failed to read tox profile"); mir_free(data); return false; } fclose(profile); if (tox_is_data_encrypted(data)) { pass_ptrA password(mir_utf8encodeW(pass_ptrT(getTStringA("Password")))); if (password == NULL || mir_strlen(password) == 0) { CToxPasswordEditor passwordEditor(this); if (!passwordEditor.DoModal()) { mir_free(data); return false; } } uint8_t *encryptedData = (uint8_t*)mir_calloc(size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH); TOX_ERR_DECRYPTION coreDecryptError; if (!tox_pass_decrypt(data, size, (uint8_t*)(char*)password, mir_strlen(password), encryptedData, &coreDecryptError)) { ShowNotification(TranslateT("Unable to decrypt Tox profile"), MB_ICONERROR); debugLogA(__FUNCTION__": failed to decrypt tox profile (%d)", coreDecryptError); mir_free(data); return false; } mir_free(data); data = encryptedData; size -= TOX_PASS_ENCRYPTION_EXTRA_LENGTH; } if (data) { options->savedata_data = data; options->savedata_length = size; options->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; return true; } return false; }
std::tstring CToxProto::GetToxProfilePath() { return GetToxProfilePath(m_tszUserName); }