summaryrefslogtreecommitdiff
path: root/protocols/Yahoo/src/search.cpp
blob: 5b0427d2a4602f9d2fb279d49159e461c5ceedf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * $Id: search.cpp 11632 2010-04-24 12:32:03Z borkra $
 *
 * myYahoo Miranda Plugin 
 *
 * Authors: Gennady Feldman (aka Gena01) 
 *          Laurent Marechal (aka Peorth)
 *
 * This code is under GPL and is based on AIM, MSN and Miranda source code.
 * I want to thank Robert Rainwater and George Hazan for their code and support
 * and for answering some of my questions during development of this plugin.
 */

#include "stdafx.h"
#include <m_protosvc.h>
#include <m_langpack.h>

#include "resource.h"

////////////////////////////////////////////////////////////////////////////////////////
// SearchBasic - searches the contact by UIN

void __cdecl CYahooProto::search_simplethread(void *snsearch)
{
	wchar_t *id = (wchar_t *)snsearch;

	if (mir_wstrlen(id) < 4) {
		ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)1, 0);
		MessageBoxA(NULL, "Please enter a valid ID to search for.", "Search", MB_OK);
		return;
	}

	wchar_t *c = wcschr(id, '@');
	if (c) *c = 0;

	YAHOO_SEARCH_RESULT psr;
	memset(&psr, 0, sizeof(psr));
	psr.cbSize = sizeof(psr);
	psr.flags = PSR_TCHAR;
	psr.id.w = (wchar_t*)wcslwr(id);
	psr.protocol = YAHOO_IM_YAHOO;

	ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)1, (LPARAM)& psr);

	//yahoo_search(m_id, YAHOO_SEARCH_YID, m, YAHOO_GENDER_NONE, YAHOO_AGERANGE_NONE, 0, 1);

	ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)1, 0);
}

HANDLE __cdecl CYahooProto::SearchBasic(const wchar_t* nick)
{
	LOG(("[YahooBasicSearch] Searching for: %S", nick));

	if (!m_bLoggedIn)
		return 0;

	ForkThread(&CYahooProto::search_simplethread, wcsdup(nick));
	return (HANDLE)1;
}

////////////////////////////////////////////////////////////////////////////////////////
// SearchAdvanced - searches the contact by UIN

void CYahooProto::ext_got_search_result(int found, int start, int total, YList *contacts)
{
	yahoo_found_contact *yct = NULL;
	int i = start;
	YList *en = contacts;

	LOG(("got search result: "));

	LOG(("Found: %d", found));
	LOG(("Start: %d", start));
	LOG(("Total: %d", total));

	YAHOO_SEARCH_RESULT psr;
	memset(&psr, 0, sizeof(psr));
	psr.cbSize = sizeof(psr);
	psr.flags = PSR_UTF8;
	psr.protocol = YAHOO_IM_YAHOO;

	while (en) {
		yct = (yahoo_found_contact*)en->data;
		if (yct == NULL) {
			LOG(("[%d] Empty record?", i++));
		}
		else {
			LOG(("[%d] id: '%s', online: %d, age: %d, sex: '%s', location: '%s'", i++, yct->id, yct->online, yct->age, yct->gender, yct->location));
			psr.id.a = yct->id;

			if (yct->gender[0] != 5) {
				psr.firstName.a = yct->gender;
				psr.email.a = yct->location;
			}
			else psr.firstName.a = psr.email.a = NULL;

			char c[10];
			if (yct->age > 0) {
				_itoa(yct->age, c, 10);
				psr.lastName.a = c;
			}
			else psr.lastName.a = NULL;

			// void yahoo_search(int id, enum yahoo_search_type t, const char *text, enum yahoo_search_gender g, enum yahoo_search_agerange ar, 
			//	int photo, int yahoo_only)

			ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)1, (LPARAM)& psr);
		}
		en = y_list_next(en);
	}
	ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)1, 0);
}

/*
 * This service function creates an advanced search dialog in Find/Add contacts Custom area.
 *
 * Returns: 0 on failure or HWND on success
 */

static INT_PTR CALLBACK YahooSearchAdvancedDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM)
{
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwndDlg);

		SendDlgItemMessageA(hwndDlg, IDC_SEARCH_PROTOCOL, CB_ADDSTRING, 0, (LPARAM)"Yahoo! Messenger");
		SendDlgItemMessageA(hwndDlg, IDC_SEARCH_PROTOCOL, CB_ADDSTRING, 0, (LPARAM)"Lotus Sametime");
		SendDlgItemMessageA(hwndDlg, IDC_SEARCH_PROTOCOL, CB_ADDSTRING, 0, (LPARAM)"LCS");
		SendDlgItemMessageA(hwndDlg, IDC_SEARCH_PROTOCOL, CB_ADDSTRING, 0, (LPARAM)"Windows Live (MSN)");

		// select the first one
		SendDlgItemMessage(hwndDlg, IDC_SEARCH_PROTOCOL, CB_SETCURSEL, 0, 0);
		return TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDC_SEARCH_ID && HIWORD(wParam) == EN_CHANGE) {
			PostMessage(GetParent(hwndDlg), WM_COMMAND, MAKEWPARAM(0, EN_SETFOCUS), (LPARAM)hwndDlg);
			return TRUE;
		}
		if (LOWORD(wParam) == IDC_SEARCH_PROTOCOL && HIWORD(wParam) == CBN_SELCHANGE) {
			PostMessage(GetParent(hwndDlg), WM_COMMAND, MAKEWPARAM(0, EN_SETFOCUS), (LPARAM)hwndDlg);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

HWND __cdecl CYahooProto::CreateExtendedSearchUI(HWND parent)
{
	if (parent && hInstance)
		return CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_SEARCHUSER), parent, YahooSearchAdvancedDlgProc, (LPARAM)this);

	return 0;
}

void __cdecl CYahooProto::searchadv_thread(void *pHWND)
{
	HWND hwndDlg = (HWND)pHWND;

	wchar_t searchid[128];
	GetDlgItemText(hwndDlg, IDC_SEARCH_ID, searchid, _countof(searchid));

	if (mir_wstrlen(searchid) == 0) {
		ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)1, 0);
		MessageBoxA(NULL, "Please enter a valid ID to search for.", "Search", MB_OK);
		return;
	}

	YAHOO_SEARCH_RESULT psr;
	memset(&psr, 0, sizeof(psr));
	psr.cbSize = sizeof(psr);
	psr.flags = PSR_UNICODE;
	psr.id.w = wcslwr(searchid);

	switch (SendDlgItemMessage(hwndDlg, IDC_SEARCH_PROTOCOL, CB_GETCURSEL, 0, 0)) {
		case 0: psr.firstName.w = L"<Yahoo>";  psr.protocol = YAHOO_IM_YAHOO; break;
		case 1: psr.firstName.w = L"<Lotus Sametime>"; psr.protocol = YAHOO_IM_SAMETIME; break;
		case 2: psr.firstName.w = L"<LCS>"; psr.protocol = YAHOO_IM_LCS; break;
		case 3: psr.firstName.w = L"<Windows Live (MSN)>"; psr.protocol = YAHOO_IM_MSN; break;
	}

	/*
	* Show this in results
	*/
	ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)1, (LPARAM)& psr);

	/*
	* Done searching.
	*/
	ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)1, 0);
}

/*
 * This service function does the advanced search
 *
 * Returns: 0 on failure or HWND on success
 */

HWND __cdecl CYahooProto::SearchAdvanced(HWND owner)
{
	LOG(("[YahooAdvancedSearch]"));

	if (!m_bLoggedIn)
		return 0;

	ForkThread(&CYahooProto::searchadv_thread, owner);
	return (HWND)1;
}