INT_PTR AddContactDialog(WPARAM wParam, LPARAM lParam) { if (lParam == 0) return 1; ADDCONTACTSTRUCT *acs = (ADDCONTACTSTRUCT*)mir_alloc(sizeof(ADDCONTACTSTRUCT)); memcpy(acs, (ADDCONTACTSTRUCT*)lParam, sizeof(ADDCONTACTSTRUCT)); if (acs->psr) { // bad! structures that are bigger than psr will cause crashes if they define pointers within unreachable structural space PROTOSEARCHRESULT *psr = (PROTOSEARCHRESULT*)mir_alloc(acs->psr->cbSize); memcpy(psr, acs->psr, acs->psr->cbSize); psr->nick = psr->flags & PSR_UNICODE ? mir_u2t((wchar_t*)psr->nick) : mir_a2t((char*)psr->nick); psr->firstName = psr->flags & PSR_UNICODE ? mir_u2t((wchar_t*)psr->firstName) : mir_a2t((char*)psr->firstName); psr->lastName = psr->flags & PSR_UNICODE ? mir_u2t((wchar_t*)psr->lastName) : mir_a2t((char*)psr->lastName); psr->email = psr->flags & PSR_UNICODE ? mir_u2t((wchar_t*)psr->email) : mir_a2t((char*)psr->email); psr->flags = psr->flags & ~PSR_UNICODE | PSR_TCHAR; acs->psr = psr; /* copied the passed acs structure, the psr structure with, the pointers within that */ } if (wParam) DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_ADDCONTACT), (HWND)wParam, AddContactDlgProc, (LPARAM)acs); else CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_ADDCONTACT), (HWND)wParam, AddContactDlgProc, (LPARAM)acs); return 0; }
void PopupHistoryList::Add(wchar_t *title, wchar_t *message, time_t timestamp) { PopupHistoryData item = {0}; //create an unicode history item item.flags = PHDF_UNICODE; //mark it as unicode item.timestamp = timestamp; item.titleT = mir_u2t(title); item.messageT = mir_u2t(message); AddItem(item); //add it }
void RecalculateTime(void) { GetTimeZoneInformation(&myInfo.myTZ.tzi); myInfo.timestamp = time(NULL); myInfo.myTZ.offset = INT_MIN; bool found = false; DYNAMIC_TIME_ZONE_INFORMATION dtzi; if (pfnGetDynamicTimeZoneInformation && pfnGetDynamicTimeZoneInformation(&dtzi) != TIME_ZONE_ID_INVALID) { TCHAR *myTzKey = mir_u2t(dtzi.TimeZoneKeyName); _tcsncpy_s(myInfo.myTZ.tszName, myTzKey, _TRUNCATE); mir_free(myTzKey); found = true; } for (int i = 0; i < g_timezones.getCount(); i++) { MIM_TIMEZONE &tz = g_timezones[i]; if (tz.offset != INT_MIN) tz.offset = INT_MIN; if (!found) { if (!mir_wstrcmp(tz.tzi.StandardName, myInfo.myTZ.tzi.StandardName) || !mir_wstrcmp(tz.tzi.DaylightName, myInfo.myTZ.tzi.DaylightName)) { _tcsncpy_s(myInfo.myTZ.tszName, tz.tszName, _TRUNCATE); found = true; } } } }
INT_PTR __cdecl CJabberProto::JabberSetNickname(WPARAM wParam, LPARAM lParam) { TCHAR *nickname = (wParam & SMNN_UNICODE) ? mir_u2t((WCHAR*)lParam) : mir_a2t((char*)lParam); setTString("Nick", nickname); SetServerVcard(FALSE, _T("")); return 0; }
static TCHAR* sttDecodeString(DWORD dwFlags, MAllStrings &src) { if (dwFlags & PSR_UNICODE) return mir_u2t(src.w); if (dwFlags & PSR_UTF8) return mir_utf8decodeT(src.a); return mir_a2t(src.a); }
static TCHAR* sttSettingToTchar(DBCONTACTWRITESETTING *cws) { switch (cws->value.type) { case DBVT_ASCIIZ: return mir_a2t(cws->value.pszVal); case DBVT_UTF8: return mir_utf8decodeT(cws->value.pszVal); case DBVT_WCHAR: return mir_u2t(cws->value.pwszVal); } return NULL; }
INT_PTR CToxProto::SetMyNickname(WPARAM wParam, LPARAM lParam) { ptrT nickname((wParam & SMNN_UNICODE) ? mir_u2t((TCHAR*)lParam) : mir_a2t((char*)lParam)); setTString("Nick", nickname); if (IsOnline()) { T2Utf szNick8(nickname); TOX_ERR_SET_INFO error; if (!tox_self_set_name(toxThread->tox, szNick8, mir_strlen(szNick8), &error)) logger->Log(__FUNCTION__": failed to set nick name"); } return 0; }
static INT_PTR srvRegister(WPARAM wParam, LPARAM lParam) { CRYPTO_PROVIDER *p = (CRYPTO_PROVIDER*)lParam; if (p == NULL || p->dwSize != sizeof(CRYPTO_PROVIDER)) return 1; CRYPTO_PROVIDER *pNew = new CRYPTO_PROVIDER(*p); pNew->pszName = mir_strdup(p->pszName); if (pNew->dwFlags & CPF_UNICODE) pNew->ptszDescr = mir_u2t(TranslateW_LP(p->pwszDescr, wParam)); else pNew->ptszDescr = mir_a2t(TranslateA_LP(p->pszDescr, wParam)); arProviders.insert(pNew); return 0; }
TCHAR *RichEdit::GetText(int start, int end) const { if (end <= start) end = GetTextLength(); if (textDocument != NULL) { ITextRange *range; if (textDocument->Range(start, end, &range) != S_OK) return mir_tstrdup(_T("")); BSTR text = NULL; if (range->GetText(&text) != S_OK || text == NULL) { range->Release(); return mir_tstrdup(_T("")); } TCHAR *ret = mir_u2t(text); SysFreeString(text); range->Release(); return ret; } else { int len = GetTextLength(); TCHAR *tmp = (TCHAR *) mir_alloc(len * sizeof(TCHAR)); GetWindowText(hwnd, tmp, len); tmp[len] = 0; TCHAR *ret = (TCHAR *) mir_alloc((end - start + 1) * sizeof(TCHAR)); memmove(ret, &tmp[start], (end - start) * sizeof(TCHAR)); ret[end - start] = 0; mir_free(tmp); return ret; } }
void __cdecl FindSettings(LPVOID param) { FindInfo* fi = (FindInfo*)param; HWND hwndParent = GetParent(fi->hwnd); ModuleSettingLL ModuleList, SettingList; ModSetLinkLinkItem *module, *setting; MCONTACT hContact; DBVARIANT dbv = { 0 }; int foundCount = 0, replaceCount = 0, deleteCount = 0; DWORD numsearch = 0, numreplace = 0; int NULLContactDone = 0; if (!fi->search || !EnumModules(&ModuleList)) { fi_free(fi); return; } _T2A search(fi->search); _T2A replace(fi->replace); // skip modules and setting names on unicode search or replace if (IsRealUnicode(fi->search) || IsRealUnicode(fi->replace)) { fi->options &= ~(F_SETNAME | F_MODNAME); fi->options |= F_UNICODE; } if (!(fi->options & F_UNICODE) && (fi->options & F_SETVAL)) { char val[16]; numsearch = strtoul(search, NULL, 10); _ultoa(numsearch, val, 10); if (!mir_strcmp(search, val)) { fi->options |= F_NUMSRCH; // replace numeric values only entirely if (replace && (fi->options & F_ENTIRE)) { numreplace = strtoul(replace, NULL, 10); _ultoa(numreplace, val, 10); if (!replace[0] || !mir_strcmp(replace, val)) fi->options |= F_NUMREPL; } } } SendDlgItemMessage(hwndParent, IDC_SBAR, SB_SETTEXT, 0, (LPARAM)TranslateT("Searching...")); hContact = 0; while (GetWindowLongPtr(GetDlgItem(hwndParent, IDC_SEARCH), GWLP_USERDATA)) { if (!hContact) { if (NULLContactDone) break; else { NULLContactDone = 1; hContact = db_find_first(); } } else hContact = db_find_next(hContact); for (module = ModuleList.first; module; module = module->next) { if (IsModuleEmpty(hContact, module->name)) continue; if (fi->options & (F_SETVAL | F_SETNAME)) { if (!EnumSettings(hContact, module->name, &SettingList)) { fi_free(fi); FreeModuleSettingLL(&ModuleList); return; } for (setting = SettingList.first; setting; setting = setting->next) { dbv.type = 0; if (db_get_s(hContact, module->name, setting->name, &dbv, 0)) continue; // check in settings value if (fi->options & F_SETVAL) { TCHAR *value = NULL; switch(dbv.type) { case DBVT_BYTE: case DBVT_WORD: case DBVT_DWORD: if ((fi->options & F_NUMSRCH) && numsearch == getNumericValue(&dbv)) { TCHAR *val = fi->search; int flag = F_SETVAL; if (fi->options & F_NUMREPL) { if (replace[0]) { db_unset(hContact, module->name, setting->name); flag |= F_DELETED; deleteCount++; } else if (setNumericValue(hContact, module->name, setting->name, numreplace, dbv.type)) { val = fi->replace; flag |= F_REPLACED; replaceCount++; } } ItemFound(fi->hwnd, hContact, module->name, setting->name, val, flag); } break; case DBVT_WCHAR: if (!value) value = mir_u2t(dbv.pwszVal); case DBVT_UTF8: if (!value) value = mir_utf8decodeT(dbv.pszVal); case DBVT_ASCIIZ: if (!value) value = mir_a2t(dbv.pszVal); if (FindMatchT(value, fi->search, fi->options)) { foundCount++; ptrT ptr; TCHAR *newValue = value; int flag = F_SETVAL; if (fi->replace) { newValue = (fi->options & F_ENTIRE) ? fi->replace : ptr = multiReplaceT(value, fi->search, fi->replace, fi->options & F_CASE); // !!!! delete or make empty ? if (!newValue[0]) { db_unset(hContact, module->name, setting->name); flag |= F_DELETED; newValue = value; deleteCount++; } else { #ifdef _UNICODE // save as unicode if needed if (dbv.type != DBVT_ASCIIZ || IsRealUnicode(newValue)) db_set_ws(hContact, module->name, setting->name, newValue); else #endif db_set_s(hContact, module->name, setting->name, _T2A(newValue)); flag |= F_REPLACED; replaceCount++; } } ItemFound(fi->hwnd, hContact, module->name, setting->name, newValue, flag); } mir_free(value); break; } // switch } // check in setting name if ((fi->options & F_SETNAME) && FindMatchA(setting->name, search, fi->options)) { foundCount++; ptrA ptr; char *newSetting = setting->name; int flag = F_SETNAME; if (replace) { newSetting = (fi->options & F_ENTIRE) ? replace : ptr = multiReplaceA(setting->name, search, replace, fi->options & F_CASE); if (!newSetting[0]) { db_unset(hContact, module->name, setting->name); flag |= F_DELETED; newSetting = setting->name; deleteCount++; } else { DBVARIANT dbv2; // skip if exist if (!db_get_s(hContact, module->name, newSetting, &dbv2, 0)) db_free(&dbv2); else if (!db_set(hContact, module->name, newSetting, &dbv)) { db_unset(hContact, module->name, setting->name); flag |= F_REPLACED; replaceCount++; } } } ItemFound(fi->hwnd, hContact, module->name, newSetting, NULL, flag); } db_free(&dbv); } // for(setting) FreeModuleSettingLL(&SettingList); } // check in module name if ((fi->options & F_MODNAME) && FindMatchA(module->name, search, fi->options)) { foundCount++; char *newModule = module->name; int flag = F_MODNAME; ptrA ptr; if (replace) { newModule = (fi->options & F_ENTIRE) ? replace : ptr = multiReplaceA(module->name, search, replace, fi->options & F_CASE); if (!newModule[0]) { deleteModule(hContact, module->name, 0); replaceTreeItem(hContact, module->name, NULL); flag |= F_DELETED; newModule = module->name; deleteCount++; } else if (renameModule(hContact, module->name, newModule)) { replaceTreeItem(hContact, module->name, NULL); flag |= F_REPLACED; replaceCount++; } } ItemFound(fi->hwnd, hContact, newModule, 0, 0, flag); } } // for(module) } TCHAR msg[MSG_SIZE]; mir_sntprintf(msg, TranslateT("Finished. Items found: %d / replaced: %d / deleted: %d"), foundCount, replaceCount, deleteCount); SendDlgItemMessage(hwndParent, IDC_SBAR, SB_SETTEXT, 0, (LPARAM)msg); if (fi->replace) { EnableWindow(GetDlgItem(hwndParent, IDC_SEARCH), 1); SetDlgItemText(hwndParent, IDOK, TranslateT("&Replace")); } else { SetDlgItemText(hwndParent, IDC_SEARCH, TranslateT("&Search")); EnableWindow(GetDlgItem(hwndParent, IDOK), 1); } fi_free(fi); FreeModuleSettingLL(&ModuleList); SetWindowLongPtr(GetDlgItem(hwndParent, IDC_SEARCH), GWLP_USERDATA, 0); EnableWindow(GetDlgItem(hwndParent, IDCANCEL), 1); }
static INT_PTR svcModernOpt_AddObject(WPARAM wParam, LPARAM lParam) { struct ModernOptionsData *dat = (struct ModernOptionsData *)wParam; MODERNOPTOBJECT *obj = (MODERNOPTOBJECT *)lParam; struct ModernOptionsObject *objCopy = (struct ModernOptionsObject *)mir_calloc(sizeof(struct ModernOptionsObject)); objCopy->dwIdx = ++g_dwIdx; objCopy->optObject.cbSize = sizeof(MODERNOPTOBJECT); objCopy->optObject.dwFlags = obj->dwFlags; objCopy->optObject.hIcon = obj->hIcon; objCopy->optObject.iSection = obj->iSection; objCopy->optObject.iType = obj->iType; objCopy->optObject.hInstance = obj->hInstance; objCopy->optObject.lpzTemplate = obj->lpzTemplate; objCopy->optObject.pfnDlgProc = obj->pfnDlgProc; if (obj->lpzClassicGroup) objCopy->optObject.lpzClassicGroup = mir_strdup(obj->lpzClassicGroup); if (obj->lpzClassicPage) objCopy->optObject.lpzClassicPage = mir_strdup(obj->lpzClassicPage); if (obj->lpzClassicTab) objCopy->optObject.lpzClassicTab = mir_strdup(obj->lpzClassicTab); if (obj->lpzHelpUrl) objCopy->optObject.lpzHelpUrl = mir_strdup(obj->lpzHelpUrl); if (obj->iBoldControls) { int count = 0; while (obj->iBoldControls[count++]) ; objCopy->optObject.iBoldControls = (int *)mir_alloc(sizeof(int) * count); memcpy(objCopy->optObject.iBoldControls, obj->iBoldControls, sizeof(int) * count); } switch (obj->iType) { case MODERNOPT_TYPE_SECTIONPAGE: objCopy->optObject.lptzSubsection = NULL; break; case MODERNOPT_TYPE_SUBSECTIONPAGE: objCopy->optObject.lptzSubsection = (objCopy->optObject.dwFlags & MODEROPT_FLG_UNICODE) ? mir_u2t(obj->lpwzSubsection) : mir_a2t(obj->lpzSubsection); break; case MODERNOPT_TYPE_IGNOREOBJECT: objCopy->optObject.lptzSubsection = (objCopy->optObject.dwFlags & MODEROPT_FLG_UNICODE) ? mir_u2t(obj->lpwzSubsection) : mir_a2t(obj->lpzSubsection); objCopy->optObject.lpzIgnoreModule = mir_strdup(obj->lpzIgnoreModule); objCopy->optObject.lpzIgnoreSetting = mir_strdup(obj->lpzIgnoreSetting); objCopy->optObject.dwIgnoreBit = obj->dwIgnoreBit; break; case MODERNOPT_TYPE_SELECTORPAGE: objCopy->optObject.iType = MODERNOPT_TYPE_SUBSECTIONPAGE; objCopy->optObject.hInstance = hInst; objCopy->optObject.lpzTemplate = MAKEINTRESOURCEA(IDD_MODERNOPT_SKINS); objCopy->optObject.pfnDlgProc = ModernOptSelector_DlgProc; objCopy->optObject.lptzSubsection = (objCopy->optObject.dwFlags & MODEROPT_FLG_UNICODE) ? mir_u2t(obj->lpwzSubsection) : mir_a2t(obj->lpzSubsection); objCopy->optObject.lpzThemeExtension = mir_strdup(obj->lpzThemeExtension); objCopy->optObject.lpzThemeModuleName = mir_strdup(obj->lpzThemeModuleName); break; } dat->pObjectList.insert(objCopy); return 0; }
static LRESULT CALLBACK MDescButtonWndProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { MDescButtonCtrl *dat = (MDescButtonCtrl *)GetWindowLongPtr(hwndDlg, 0); switch (msg) { case WM_NCCREATE: dat = (MDescButtonCtrl*)mir_alloc(sizeof(MDescButtonCtrl)); if (dat == NULL) return FALSE; memset(dat, 0, sizeof(MDescButtonCtrl)); SetWindowLongPtr(hwndDlg, 0, (LONG_PTR)dat); MDescButton_SetupColors(dat); return TRUE; case WM_SETFONT: dat->hFont = (HFONT)wParam; break; case WM_SIZE: GetClientRect(hwndDlg, &dat->rc); dat->width = dat->rc.right - dat->rc.left; dat->height = dat->rc.bottom - dat->rc.top; return TRUE; case WM_THEMECHANGED: case WM_STYLECHANGED: MDescButton_SetupColors(dat); return TRUE; case WM_MOUSEMOVE: if (!dat->bMouseInside) { TRACKMOUSEEVENT tme = { 0 }; tme.cbSize = sizeof(tme); tme.dwFlags = TME_LEAVE; tme.hwndTrack = hwndDlg; _TrackMouseEvent(&tme); dat->bMouseInside = TRUE; RedrawWindow(hwndDlg, NULL, NULL, RDW_INVALIDATE); } return 0; case WM_MOUSELEAVE: dat->bMouseInside = FALSE; RedrawWindow(hwndDlg, NULL, NULL, RDW_INVALIDATE); return 0; case WM_LBUTTONUP: SendMessage(GetParent(hwndDlg), WM_COMMAND, MAKEWPARAM(GetWindowLongPtr(hwndDlg, GWL_ID), 0), 0); return 0; case WM_ERASEBKGND: return 1; case WM_NCPAINT: InvalidateRect(hwndDlg, NULL, FALSE); break; case WM_PAINT: MDescButton_OnPaint(hwndDlg, dat); break; case DBCM_SETTITLE: if (dat->lpzTitle) mir_free(dat->lpzTitle); if (wParam & MDBCF_UNICODE) dat->lpzTitle = mir_u2t((WCHAR *)lParam); else dat->lpzTitle = mir_a2t((char *)lParam); RedrawWindow(hwndDlg, NULL, NULL, RDW_INVALIDATE); return TRUE; case DBCM_SETDESCRIPTION: if (dat->lpzDescription) mir_free(dat->lpzDescription); if (wParam & MDBCF_UNICODE) dat->lpzDescription = mir_u2t((WCHAR *)lParam); else dat->lpzDescription = mir_a2t((char *)lParam); RedrawWindow(hwndDlg, NULL, NULL, RDW_INVALIDATE); return TRUE; case DBCM_SETICON: if (dat->hIcon && !dat->bSharedIcon) DestroyIcon(dat->hIcon); if (wParam & MDBCF_SHAREDICON) { dat->bSharedIcon = TRUE; dat->hIcon = (HICON)lParam; } else { dat->bSharedIcon = FALSE; dat->hIcon = CopyIcon((HICON)lParam); } RedrawWindow(hwndDlg, NULL, NULL, RDW_INVALIDATE); return TRUE; case WM_DESTROY: if (dat->lpzTitle) mir_free(dat->lpzTitle); if (dat->lpzDescription) mir_free(dat->lpzDescription); if (dat->hIcon && !dat->bSharedIcon) DestroyIcon(dat->hIcon); mir_free(dat); return TRUE; } return DefWindowProc(hwndDlg, msg, wParam, lParam); }
BOOL ITunes::FillCache() { HRESULT hr; long lret; CALL( track->get_Album(&ret)); listening_info.ptszAlbum = U2T(ret); CALL( track->get_Artist(&ret)); listening_info.ptszArtist = U2T(ret); CALL( track->get_Name(&ret)); listening_info.ptszTitle = U2T(ret); CALL( track->get_Year(&lret)); if (lret > 0) { listening_info.ptszYear = (TCHAR*) mir_alloc(10 * sizeof(TCHAR)); _itot(lret, listening_info.ptszYear, 10); } CALL( track->get_TrackNumber(&lret)); if (lret > 0) { listening_info.ptszTrack = (TCHAR*) mir_alloc(10 * sizeof(TCHAR)); _itot(lret, listening_info.ptszTrack, 10); } CALL( track->get_Genre(&ret)); listening_info.ptszGenre = U2T(ret); CALL( track->get_Duration(&lret)); if (lret > 0) { listening_info.ptszLength = (TCHAR*) mir_alloc(10 * sizeof(TCHAR)); int s = lret % 60; int m = (lret / 60) % 60; int h = (lret / 60) / 60; if (h > 0) mir_sntprintf(listening_info.ptszLength, 9, _T("%d:%02d:%02d"), h, m, s); else mir_sntprintf(listening_info.ptszLength, 9, _T("%d:%02d"), m, s); } listening_info.ptszType = mir_tstrdup(_T("Music")); if (listening_info.ptszTitle == NULL) { // Get from filename WCHAR *p = wcsrchr(filename, '\\'); if (p != NULL) p++; else p = filename; listening_info.ptszTitle = mir_u2t(p); TCHAR *pt = _tcsrchr(listening_info.ptszTitle, '.'); if (pt != NULL) *p = _T('\0'); } listening_info.ptszPlayer = mir_tstrdup(name); listening_info.cbSize = sizeof(listening_info); listening_info.dwFlags = LTI_TCHAR; return TRUE; }
void GenericPlayer::ProcessReceived() { mir_cslockfull lck(cs); // Do the processing // L"<Status 0-stoped 1-playing>\\0<Player>\\0<Type>\\0<Title>\\0<Artist>\\0<Album>\\0<Track>\\0<Year>\\0<Genre>\\0<Length (secs)>\\0\\0" WCHAR *p1 = wcsstr(received, L"\\0"); if (IsEmpty(received) || p1 == NULL) return; // Process string WCHAR *parts[11] = { 0 }; int pCount = 0; WCHAR *p = received; do { *p1 = _T('\0'); parts[pCount] = p; pCount++; p = p1 + 2; p1 = wcsstr(p, _T("\\0")); } while (p1 != NULL && pCount < 10); if (p1 != NULL) *p1 = _T('\0'); parts[pCount] = p; if (pCount < 5) return; // See if player is enabled Player *player = this; for (int i = FIRST_PLAYER; i < NUM_PLAYERS; i++) { WCHAR *player_name = players[i]->name; if (_wcsicmp(parts[1], player_name) == 0) { player = players[i]; break; } } player->FreeData(); if (wcscmp(L"1", parts[0]) != 0 || IsEmpty(parts[1]) || (IsEmpty(parts[3]) && IsEmpty(parts[4]))) { // Stoped playing or not enought info } else { mir_cslock plck(player->GetLock()); LISTENINGTOINFO *li = player->GetInfo(); li->cbSize = sizeof(listening_info); li->dwFlags = LTI_TCHAR; li->ptszType = U2TD(parts[2], _T("Music")); li->ptszTitle = U2T(parts[3]); li->ptszArtist = U2T(parts[4]); li->ptszAlbum = U2T(parts[5]); li->ptszTrack = U2T(parts[6]); li->ptszYear = U2T(parts[7]); li->ptszGenre = U2T(parts[8]); if (player == this) li->ptszPlayer = mir_u2t(parts[1]); else li->ptszPlayer = mir_tstrdup(player->name); if (parts[9] != NULL) { long length = _wtoi(parts[9]); if (length > 0) { li->ptszLength = (TCHAR*)mir_alloc(10 * sizeof(TCHAR)); int s = length % 60; int m = (length / 60) % 60; int h = (length / 60) / 60; if (h > 0) mir_sntprintf(li->ptszLength, 9, _T("%d:%02d:%02d"), h, m, s); else mir_sntprintf(li->ptszLength, 9, _T("%d:%02d"), m, s); } } } // Put back the '\\'s for (int i = 1; i <= pCount; i++) *(parts[i] - 2) = L'\\'; if (p1 != NULL) *p1 = L'\\'; wcscpy(last_received, received); lck.unlock(); NotifyInfoChanged(); }
int CAimProto::receiving_file(file_transfer *ft, HANDLE hServerPacketRecver, NETLIBPACKETRECVER &packetRecv) { debugLogA("P2P: Entered file receiving thread."); bool failed = true; bool failed_conn = false; bool accepted_file = false; int fid = -1; oft2 *oft = NULL; ft->pfts.tszWorkingDir = mir_utf8decodeT(ft->file); //start listen for packets stuff for (;;) { int recvResult = packetRecv.bytesAvailable - packetRecv.bytesUsed; if (recvResult <= 0) recvResult = CallService(MS_NETLIB_GETMOREPACKETS, (WPARAM)hServerPacketRecver, (LPARAM)&packetRecv); if (recvResult == 0) { debugLogA("P2P: File transfer connection Error: 0"); break; } if (recvResult == SOCKET_ERROR) { failed_conn = true; debugLogA("P2P: File transfer connection Error: -1"); break; } if (recvResult > 0) { if (!accepted_file) { if (recvResult < 0x100) continue; oft2* recv_ft = (oft2*)&packetRecv.buffer[packetRecv.bytesUsed]; unsigned short pkt_len = _htons(recv_ft->length); if (recvResult < pkt_len) continue; packetRecv.bytesUsed += pkt_len; unsigned short type = _htons(recv_ft->type); if (type == 0x0101) { debugLogA("P2P: Buddy Ready to begin transfer."); oft = (oft2*)mir_realloc(oft, pkt_len); memcpy(oft, recv_ft, pkt_len); memcpy(oft->icbm_cookie, ft->icbm_cookie, 8); int buflen = pkt_len - 0x100 + 64; char *buf = (char*)mir_calloc(buflen + 2); unsigned short enc; ft->pfts.currentFileSize = _htonl(recv_ft->size); ft->pfts.totalBytes = _htonl(recv_ft->total_size); ft->pfts.currentFileTime = _htonl(recv_ft->mod_time); memcpy(buf, recv_ft->filename, buflen); enc = _htons(recv_ft->encoding); TCHAR *name; if (enc == 2) { wchar_t* wbuf = (wchar_t*)buf; wcs_htons(wbuf); for (wchar_t *p = wbuf; *p; ++p) { if (*p == 1) *p = '\\'; } name = mir_u2t(wbuf); } else { for (char *p = buf; *p; ++p) { if (*p == 1) *p = '\\'; } name = mir_a2t(buf); } mir_free(buf); TCHAR fname[256]; mir_sntprintf(fname, _T("%s%s"), ft->pfts.tszWorkingDir, name); mir_free(name); mir_free(ft->pfts.tszCurrentFile); ft->pfts.tszCurrentFile = mir_tstrdup(fname); ResetEvent(ft->hResumeEvent); if (ProtoBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_FILERESUME, ft, (LPARAM)&ft->pfts)) WaitForSingleObject(ft->hResumeEvent, INFINITE); if (ft->pfts.tszCurrentFile) { TCHAR* dir = get_dir(ft->pfts.tszCurrentFile); CreateDirectoryTreeT(dir); mir_free(dir); oft->type = _htons(ft->pfts.currentFileProgress ? 0x0205 : 0x0202); const int flag = ft->pfts.currentFileProgress ? 0 : _O_TRUNC; fid = _topen(ft->pfts.tszCurrentFile, _O_CREAT | _O_WRONLY | _O_BINARY | flag, _S_IREAD | _S_IWRITE); if (fid < 0) { report_file_error(fname); break; } accepted_file = ft->pfts.currentFileProgress == 0; if (ft->pfts.currentFileProgress) { bool the_same; oft->recv_bytes = _htonl(ft->pfts.currentFileProgress); oft->recv_checksum = _htonl(aim_oft_checksum_file(ft->pfts.tszCurrentFile)); the_same = oft->size == oft->recv_bytes && oft->checksum == oft->recv_checksum; if (the_same) { ft->pfts.totalProgress += ft->pfts.currentFileProgress; oft->type = _htons(0x0204); _close(fid); ProtoBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ft, 0); ++ft->pfts.currentFileNumber; ft->pfts.currentFileProgress = 0; } } } else { oft->type = _htons(0x0204); ProtoBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ft, 0); ++ft->pfts.currentFileNumber; ft->pfts.currentFileProgress = 0; } if (Netlib_Send(ft->hConn, (char*)oft, pkt_len, 0) == SOCKET_ERROR) break; if (ft->pfts.currentFileNumber >= ft->pfts.totalFiles && _htons(oft->type) == 0x0204) { failed = false; break; } } else if (type == 0x0106) { oft = (oft2*)mir_realloc(oft, pkt_len); memcpy(oft, recv_ft, pkt_len); ft->pfts.currentFileProgress = _htonl(oft->recv_bytes); ft->pfts.totalProgress += ft->pfts.currentFileProgress; _lseeki64(fid, ft->pfts.currentFileProgress, SEEK_SET); accepted_file = true; oft->type = _htons(0x0207); if (Netlib_Send(ft->hConn, (char*)oft, pkt_len, 0) == SOCKET_ERROR) break; } else break; } else { packetRecv.bytesUsed = packetRecv.bytesAvailable; _write(fid, packetRecv.buffer, packetRecv.bytesAvailable); ft->pfts.currentFileProgress += packetRecv.bytesAvailable; ft->pfts.totalProgress += packetRecv.bytesAvailable; ProtoBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_DATA, ft, (LPARAM)&ft->pfts); if (ft->pfts.currentFileSize == ft->pfts.currentFileProgress) { oft->type = _htons(0x0204); oft->recv_bytes = _htonl(ft->pfts.currentFileProgress); oft->recv_checksum = _htonl(aim_oft_checksum_file(ft->pfts.tszCurrentFile)); debugLogA("P2P: We got the file successfully"); Netlib_Send(ft->hConn, (char*)oft, _htons(oft->length), 0); if (_htons(oft->num_files_left) == 1) { failed = false; break; } else { accepted_file = false; _close(fid); ProtoBroadcastAck(ft->hContact, ACKTYPE_FILE, ACKRESULT_NEXTFILE, ft, 0); ++ft->pfts.currentFileNumber; ft->pfts.currentFileProgress = 0; } } } } } if (accepted_file) _close(fid); mir_free(oft); ft->success = !failed; return failed ? (failed_conn ? 1 : 2) : 0; }
static INT_PTR NetlibRegisterUser(WPARAM,LPARAM lParam) { NETLIBUSER *nlu=(NETLIBUSER*)lParam; struct NetlibUser *thisUser; if(nlu==NULL || nlu->cbSize!=sizeof(NETLIBUSER) || nlu->szSettingsModule==NULL || (!(nlu->flags&NUF_NOOPTIONS) && nlu->szDescriptiveName==NULL) || (nlu->flags&NUF_HTTPGATEWAY && (nlu->pfnHttpGatewayInit==NULL))) { SetLastError(ERROR_INVALID_PARAMETER); return 0; } thisUser = (struct NetlibUser*)mir_calloc(sizeof(struct NetlibUser)); thisUser->handleType = NLH_USER; thisUser->user = *nlu; EnterCriticalSection(&csNetlibUser); if (netlibUser.getIndex(thisUser) >= 0) { LeaveCriticalSection(&csNetlibUser); mir_free(thisUser); SetLastError(ERROR_DUP_NAME); return 0; } LeaveCriticalSection(&csNetlibUser); if (nlu->szDescriptiveName) { thisUser->user.ptszDescriptiveName = (thisUser->user.flags&NUF_UNICODE ? mir_u2t((WCHAR*)nlu->ptszDescriptiveName) : mir_a2t(nlu->szDescriptiveName)); } if((thisUser->user.szSettingsModule=mir_strdup(nlu->szSettingsModule))==NULL || (nlu->szDescriptiveName && thisUser->user.ptszDescriptiveName ==NULL) || (nlu->szHttpGatewayUserAgent && (thisUser->user.szHttpGatewayUserAgent=mir_strdup(nlu->szHttpGatewayUserAgent))==NULL)) { mir_free(thisUser); SetLastError(ERROR_OUTOFMEMORY); return 0; } if (nlu->szHttpGatewayHello) thisUser->user.szHttpGatewayHello=mir_strdup(nlu->szHttpGatewayHello); else thisUser->user.szHttpGatewayHello=NULL; thisUser->settings.cbSize=sizeof(NETLIBUSERSETTINGS); thisUser->settings.useProxy=GetNetlibUserSettingInt(thisUser->user.szSettingsModule,"NLUseProxy",0); thisUser->settings.proxyType=GetNetlibUserSettingInt(thisUser->user.szSettingsModule,"NLProxyType",PROXYTYPE_SOCKS5); if(thisUser->user.flags&NUF_NOHTTPSOPTION && thisUser->settings.proxyType==PROXYTYPE_HTTPS) thisUser->settings.proxyType=PROXYTYPE_HTTP; if(!(thisUser->user.flags&(NUF_HTTPCONNS|NUF_HTTPGATEWAY)) && thisUser->settings.proxyType==PROXYTYPE_HTTP) { thisUser->settings.useProxy=0; thisUser->settings.proxyType=PROXYTYPE_SOCKS5; } thisUser->settings.szProxyServer=GetNetlibUserSettingString(thisUser->user.szSettingsModule,"NLProxyServer",0); thisUser->settings.wProxyPort=GetNetlibUserSettingInt(thisUser->user.szSettingsModule,"NLProxyPort",1080); thisUser->settings.useProxyAuth=GetNetlibUserSettingInt(thisUser->user.szSettingsModule,"NLUseProxyAuth",0); thisUser->settings.szProxyAuthUser=GetNetlibUserSettingString(thisUser->user.szSettingsModule,"NLProxyAuthUser",0); thisUser->settings.szProxyAuthPassword=GetNetlibUserSettingString(thisUser->user.szSettingsModule,"NLProxyAuthPassword",1); thisUser->settings.dnsThroughProxy=GetNetlibUserSettingInt(thisUser->user.szSettingsModule,"NLDnsThroughProxy",1); thisUser->settings.specifyIncomingPorts=GetNetlibUserSettingInt(thisUser->user.szSettingsModule,"NLSpecifyIncomingPorts",0); thisUser->settings.szIncomingPorts=GetNetlibUserSettingString(thisUser->user.szSettingsModule,"NLIncomingPorts",0); thisUser->settings.specifyOutgoingPorts=GetNetlibUserSettingInt(thisUser->user.szSettingsModule,"NLSpecifyOutgoingPorts",0); thisUser->settings.szOutgoingPorts=GetNetlibUserSettingString(thisUser->user.szSettingsModule,"NLOutgoingPorts",0); thisUser->settings.enableUPnP=GetNetlibUserSettingInt(thisUser->user.szSettingsModule,"NLEnableUPnP",1); //default to on thisUser->settings.validateSSL=GetNetlibUserSettingInt(thisUser->user.szSettingsModule,"NLValidateSSL",0); thisUser->toLog=GetNetlibUserSettingInt(thisUser->user.szSettingsModule,"NLlog",1); EnterCriticalSection(&csNetlibUser); netlibUser.insert(thisUser); LeaveCriticalSection(&csNetlibUser); return (INT_PTR)thisUser; }
void GenericPlayer::ProcessReceived() { EnterCriticalSection(&cs); // Do the processing // L"<Status 0-stoped 1-playing>\\0<Player>\\0<Type>\\0<Title>\\0<Artist>\\0<Album>\\0<Track>\\0<Year>\\0<Genre>\\0<Length (secs)>\\0\\0" WCHAR *p1 = wcsstr(received, L"\\0"); if (IsEmpty(received) || p1 == NULL) { // if (received[0] == L'\0') // m_log(_T("ProcessReceived"), _T("ERROR: Empty text")); // else // m_log(_T("ProcessReceived"), _T("ERROR: No \\0 found")); // Ignore LeaveCriticalSection(&cs); return; } // Process string int i; WCHAR *parts[11] = {0}; int pCount = 0; WCHAR *p = received; do { *p1 = L'\0'; parts[pCount] = p; pCount ++; p = p1 + 2; p1 = wcsstr(p, L"\\0"); } while( p1 != NULL && pCount < 10 ); if (p1 != NULL) *p1 = L'\0'; parts[pCount] = p; // select known player (default is generic = this) Player *player = m_enabled ? this : NULL; for (i = FIRST_PLAYER; i < NUM_PLAYERS; i++) { #ifdef UNICODE WCHAR *player_name = players[i]->m_name; #else WCHAR player_name[128]; MultiByteToWideChar(CP_ACP, 0, players[i]->m_name, -1, player_name, MAX_REGS(player_name)); #endif if (_wcsicmp(parts[1], player_name) == 0) { player = players[i]; break; } } //is player enabled if(!player || !player->m_enabled) { LeaveCriticalSection(&cs); return; } //set player status SetActivePlayer(player->m_index, player->m_index); int status = IsEmpty(parts[0]) ? 0 : _wtoi(parts[0]); switch(status){ case 0: player->m_state = player->GetStatus() ? PL_STOPPED : PL_OFFLINE; break; case 1: player->m_state = PL_PLAYING; break; } player->FreeData(); if (pCount < 5 || wcscmp(L"1", parts[0]) != 0 || IsEmpty(parts[1]) || (IsEmpty(parts[3]) && IsEmpty(parts[4]))) { // Stoped playing or not enought info player->m_state = PL_OFFLINE; // SetActivePlayer(player->m_index, -1); // if (wcscmp(L"1", parts[0]) != 0) // m_log(_T("ProcessReceived"), _T("END: Stoped playing")); // else // m_log(_T("ProcessReceived"), _T("ERROR: not enought info")); } else { SetActivePlayer(player->m_index, player->m_index); LISTENINGTOINFO *li = player->LockListeningInfo(); li->cbSize = sizeof(m_listening_info); li->dwFlags = LTI_TCHAR; li->ptszType = U2TD(parts[2], L"Music"); li->ptszTitle = U2T(parts[3]); li->ptszArtist = U2T(parts[4]); li->ptszAlbum = U2T(parts[5]); li->ptszTrack = U2T(parts[6]); li->ptszYear = U2T(parts[7]); li->ptszGenre = U2T(parts[8]); if (player == this) li->ptszPlayer = mir_u2t(parts[1]); else li->ptszPlayer = mir_tstrdup(player->m_name); if (parts[9] != NULL) { long length = _wtoi(parts[9]); if (length > 0) { li->ptszLength = (TCHAR*) mir_alloc(10 * sizeof(TCHAR)); int s = length % 60; int m = (length / 60) % 60; int h = (length / 60) / 60; if (h > 0) mir_sntprintf(li->ptszLength, 9, _T("%d:%02d:%02d"), h, m, s); else mir_sntprintf(li->ptszLength, 9, _T("%d:%02d"), m, s); } } player->ReleaseListeningInfo(); } // Put back the '\\'s for(i = 1; i <= pCount; i++) *(parts[i] - 2) = L'\\'; if (p1 != NULL) *p1 = L'\\'; wcscpy(last_received, received); LeaveCriticalSection(&cs); NotifyInfoChanged(player->m_index); // m_log(_T("ProcessReceived"), _T("END: Success")); }