summaryrefslogtreecommitdiff
path: root/protocols/Yahoo/src/search.cpp
blob: d809bf496f526e700d30dbc15046947d00ff1287 (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
/*
 * $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 "yahoo.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)
{
	TCHAR *id = (TCHAR *) snsearch;

	if (lstrlen(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;
	}

	TCHAR *c = _tcschr(id, '@');
	if (c) *c = 0;

	PROTOSEARCHRESULT psr = { 0 };
	psr.cbSize = sizeof(psr);
	psr.flags = PSR_TCHAR;
	psr.id = (TCHAR*)_tcslwr(id);
	psr.reserved[0] = 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 TCHAR* nick )
{
	LOG(("[YahooBasicSearch] Searching for: %S", nick));
	
	if ( !m_bLoggedIn )
		return 0;

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

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

void CYahooProto::ext_got_search_result(int found, int start, int total, YList *contacts)
{
	struct 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));
		
	PROTOSEARCHRESULT psr = { 0 };
	psr.cbSize = sizeof(psr);
	psr.flags = PSR_TCHAR;
	psr.reserved[0] = 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 = mir_utf8decodeT( yct->id );
			
			if (yct->gender[0] != 5)
				psr.firstName = mir_utf8decodeT( yct->gender );
			else
				psr.firstName = NULL;
			
			TCHAR c[10];
			if (yct->age > 0) {
				_itot(yct->age, c,10);
				psr.lastName = ( TCHAR* )c;
			}
			else
				psr.lastName = NULL;
			
			if (yct->location[0] != 5)
				psr.email = mir_utf8decodeT( yct->location );
			else
				psr.email = 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);

			mir_free(psr.id);
			mir_free(psr.firstName);
			mir_free(psr.email);
		}
		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 lParam)
{
	switch ( msg ) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hwndDlg);
			
		SendDlgItemMessageA(hwndDlg, IDC_SEARCH_PROTOCOL, CB_ADDSTRING, 0, (LPARAM)(LPCSTR)"Yahoo! Messenger");
		SendDlgItemMessageA(hwndDlg, IDC_SEARCH_PROTOCOL, CB_ADDSTRING, 0, (LPARAM)(LPCSTR)"Lotus Sametime");
		SendDlgItemMessageA(hwndDlg, IDC_SEARCH_PROTOCOL, CB_ADDSTRING, 0, (LPARAM)(LPCSTR)"LCS");
		SendDlgItemMessageA(hwndDlg, IDC_SEARCH_PROTOCOL, CB_ADDSTRING, 0, (LPARAM)(LPCSTR)"Windows Live (MSN)");
		
		// select the first one
		SendDlgItemMessage(hwndDlg, IDC_SEARCH_PROTOCOL, CB_SETCURSEL, 0, 0);
		return TRUE;
	}
	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;

	TCHAR searchid[128];
	GetDlgItemText(hwndDlg, IDC_SEARCH_ID, searchid, 128);

	if (lstrlen(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;
	} 

	PROTOSEARCHRESULT psr = { 0 };
	psr.cbSize = sizeof(psr);
	psr.flags = PSR_TCHAR;
	psr.id = _tcslwr(searchid);

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

	psr.reserved[0] = pid;

	/*
	* 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;
}