summaryrefslogtreecommitdiff
path: root/protocols/MSN/src/msn_contact.cpp
blob: ea27761a17538dcc0b66b1ec47bafe73f7959d49 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
Plugin of Miranda IM for communicating with users of the MSN Messenger protocol.

Copyright (c) 2012-2014 Miranda NG Team
Copyright (c) 2006-2012 Boris Krasnovskiy.
Copyright (c) 2003-2005 George Hazan.
Copyright (c) 2002-2003 Richard Hughes (original version).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "msn_global.h"
#include "msn_proto.h"

MCONTACT CMsnProto::MSN_HContactFromEmail(const char* wlid, const char* msnNick, bool addIfNeeded, bool temporary)
{
	MCONTACT hContact = NULL;

	char *szEmail, *szNet = NULL;
	parseWLID(NEWSTR_ALLOCA(wlid), &szNet, &szEmail, NULL);

	MsnContact *msc = Lists_Get(szEmail);
	if (msc && msc->hContact)
		hContact = msc->hContact;

	if (hContact == NULL && addIfNeeded) {
		int netId = (msc && msc->netId)?msc->netId:(szNet?atoi(szNet):NETID_MSN);
		hContact = (MCONTACT)CallService(MS_DB_CONTACT_ADD, 0, 0);
		CallService(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)m_szModuleName);
		if (netId != NETID_SKYPE) setString(hContact, "e-mail", szEmail);
		setStringUtf(hContact, "Nick", msnNick ? msnNick : wlid);
		setWord(hContact, "netId", netId);
		setString(hContact, "wlid", szEmail);
		if (temporary)
			db_set_b(hContact, "CList", "NotOnList", 1);

		Lists_Add(0, szNet?atoi(szNet):NETID_MSN, szEmail, hContact);
	}

	return hContact;
}

MCONTACT CMsnProto::MSN_HContactFromChatID(const char* wlid)
{
	MCONTACT hContact = NULL;

	for (hContact = db_find_first(m_szModuleName); hContact; 
			hContact = db_find_next(hContact, m_szModuleName)) 
	{
		if (isChatRoom(hContact) != 0) {
			DBVARIANT dbv;
			if (getString(hContact, "ChatRoomID", &dbv) == 0) {
				if (mir_strcmp(dbv.pszVal, wlid) == 0) {
					db_free(&dbv);
					return hContact;
				}
				db_free(&dbv);
			}
		}
	}
	return NULL;
}


void CMsnProto::MSN_SetContactDb(MCONTACT hContact, const char *szEmail)
{
	MsnContact *cont = Lists_Get(szEmail);
	const int listId = cont->list;

	if (listId & LIST_FL) {
		if (db_get_b(hContact, "CList", "NotOnList", 0) == 1) {
			db_unset(hContact, "CList", "NotOnList");
			db_unset(hContact, "CList", "Hidden");
		}

		if (listId & (LIST_BL | LIST_AL)) {
			WORD tApparentMode = getWord(hContact, "ApparentMode", 0);
			if ((listId & LIST_BL) && tApparentMode == 0)
				setWord(hContact, "ApparentMode", ID_STATUS_OFFLINE);
			else if ((listId & LIST_AL) && tApparentMode != 0)
				delSetting(hContact, "ApparentMode");
		}

		if (cont->netId == NETID_MOB) {
			setWord(hContact, "Status", ID_STATUS_ONTHEPHONE);
			setString(hContact, "MirVer", "SMS");
		}
	}
	if (listId & LIST_LL)
		setByte(hContact, "LocalList", 1);
	else
		delSetting(hContact, "LocalList");
}


void CMsnProto::AddDelUserContList(const char* email, const int list, const int netId, const bool del)
{
#ifdef OBSOLETE
	char buf[512];
	size_t sz;

	if (list < LIST_RL) {
		const char* dom = strchr(email, '@');
		if (dom == NULL) {
			sz = mir_snprintf(buf, SIZEOF(buf),
				"<ml><t><c n=\"%s\" l=\"%d\"/></t></ml>",
				email, list);
		}
		else {
			*(char*)dom = 0;
			sz = mir_snprintf(buf, SIZEOF(buf),
				"<ml><d n=\"%s\"><c n=\"%s\" l=\"%d\" t=\"%d\"/></d></ml>",
				dom + 1, email, list, netId);
			*(char*)dom = '@';
		}
		msnNsThread->sendPacket(del ? "RML" : "ADL", "%d\r\n%s", sz, buf);
	}
#endif

	if (del)
		Lists_Remove(list, email);
	else
		Lists_Add(list, netId, email);
}


/////////////////////////////////////////////////////////////////////////////////////////
// MSN_AddUser - adds a e-mail address to one of the MSN server lists

bool CMsnProto::MSN_AddUser(MCONTACT hContact, const char* email, int netId, int flags, const char *msg)
{
	bool needRemove = (flags & LIST_REMOVE) != 0;
	bool leaveHotmail = (flags & LIST_REMOVENH) == LIST_REMOVENH;
	flags &= 0xFF;

	if (needRemove != Lists_IsInList(flags, email))
		return true;

	bool res = false;
	if (flags == LIST_FL) {
		if (needRemove) {
			if (hContact == NULL) {
				hContact = MSN_HContactFromEmail(email);
				if (hContact == NULL)
					return false;
			}

			char id[MSN_GUID_LEN];
			if (!db_get_static(hContact, m_szModuleName, "ID", id, sizeof(id))) {
				int netId = Lists_GetNetId(email);
				if (leaveHotmail)
					res = MSN_ABAddRemoveContact(id, netId, false);
				else
					res = MSN_ABAddDelContactGroup(id, NULL, "ABContactDelete");
				if (res) AddDelUserContList(email, flags, netId, true);

				delSetting(hContact, "GroupID");
				delSetting(hContact, "ID");
				MSN_RemoveEmptyGroups();
			}
		}
		else {
			DBVARIANT dbv = { 0 };
			if (!mir_strcmp(email, MyOptions.szEmail))
				getStringUtf("Nick", &dbv);

			unsigned res1 = MSN_ABContactAdd(email, dbv.pszVal, netId, msg, false);
			if (netId == NETID_MSN && res1 == 2) {
				netId = NETID_LCS;
				res = MSN_ABContactAdd(email, dbv.pszVal, netId, msg, false) == 0;
			}
			else if (netId == NETID_MSN && res1 == 3) {
				char szContactID[100];
				hContact = MSN_HContactFromEmail(email);
				if (db_get_static(hContact, m_szModuleName, "ID", szContactID, sizeof(szContactID)) == 0) {
					MSN_ABAddRemoveContact(szContactID, netId, true);
					res = true;
				}
			}
			else res = (res1 == 0);

			if (res) {
				DBVARIANT dbv;
				if (!db_get_utf(hContact, "CList", "Group", &dbv)) {
					MSN_MoveContactToGroup(hContact, dbv.pszVal);
					db_free(&dbv);
				}

				char szContactID[100];
				if (db_get_static(hContact, m_szModuleName, "ID", szContactID, sizeof(szContactID)) == 0)
					MSN_ABFind("ABFindByContacts", szContactID);

				MSN_SharingFindMembership(true);
				AddDelUserContList(email, flags, netId, false);
			}
#ifdef OBSOLETE
			else if (netId == 1 && strstr(email, "@yahoo.com") != 0)
				MSN_FindYahooUser(email);
#endif

			db_free(&dbv);
		}
	}
	else if (flags == LIST_LL) {
		if (needRemove)
			Lists_Remove(LIST_LL, email);
		else
			Lists_Add(LIST_LL, NETID_MSN, email);
	}
	else {
		if (netId == 0)
			netId = Lists_GetNetId(email);
		res = MSN_SharingAddDelMember(email, flags, netId, needRemove ? "DeleteMember" : "AddMember");
		AddDelUserContList(email, flags, netId, needRemove);
		if ((flags & LIST_BL) && !needRemove) {
			ThreadData* thread = MSN_GetThreadByContact(email, SERVER_SWITCHBOARD);
			if (thread) thread->sendTerminate();
		}

		if ((flags & LIST_PL) && needRemove)
			MSN_AddUser(hContact, email, netId, LIST_RL);
	}
	return res;
}

#ifdef OBSOLETE
void CMsnProto::MSN_FindYahooUser(const char* email)
{
	const char *dom = strchr(email, '@');
	if (dom) {
		char buf[512];
		size_t sz;

		*(char*)dom = '\0';
		sz = mir_snprintf(buf, SIZEOF(buf), "<ml><d n=\"%s\"><c n=\"%s\"/></d></ml>", dom + 1, email);
		*(char*)dom = '@';
		msnNsThread->sendPacket("FQY", "%d\r\n%s", sz, buf);
	}
}
#endif

bool CMsnProto::MSN_RefreshContactList(void)
{
	Lists_Wipe();
	Lists_Populate();

	if (MyOptions.netId != NETID_SKYPE)
	{
		// Get your own profile info
		if (!MSN_SharingFindMembership()) return false;

		if (m_iDesiredStatus == ID_STATUS_OFFLINE) return false;

		// Get "classic" MSN contacts
		if (!MSN_ABFind("ABFindContactsPaged", NULL)) return false;

		// Get Skype contacts on linked profiles
		if (!MSN_ABRefreshClist()) return false;

		if (m_iDesiredStatus == ID_STATUS_OFFLINE) return false;

		// Remove contacts not on server etc.
		MSN_CleanupLists();

		if (m_iDesiredStatus == ID_STATUS_OFFLINE) return false;

		msnLoggedIn = true;

		// Populate Contact list on MSN network to get status updates of contacts
		MSN_CreateContList();

		// Read my own profile from SOAP AB
		MSN_StoreGetProfile();
	}
	else
	{
		/* TODO: Add pulling Skype contacts from event server or skypeweb or other unknown method.. */
		MSN_CreateContList();
	}

	// Refresh Threads which are also part of the contact list
	if (msnP24Ver>1) MSN_GCRefreshThreadsInfo();

	return true;
}