summaryrefslogtreecommitdiff
path: root/protocols/MSN/src/msn_p2ps.cpp
blob: f7840374c82ebaa79e8d89c3a9237c34cb498555 (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
/*
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"

/////////////////////////////////////////////////////////////////////////////////////////
// add file session to a list

void CMsnProto::p2p_registerSession(filetransfer* ft)
{
	mir_cslock lck(m_csSessions);
	m_arSessions.insert(ft);
}

/////////////////////////////////////////////////////////////////////////////////////////
// remove file session from a list

void CMsnProto::p2p_unregisterSession(filetransfer* ft)
{
	mir_cslock lck(m_csSessions);
	m_arSessions.remove(ft);
}

/////////////////////////////////////////////////////////////////////////////////////////
// get session by some parameter

filetransfer* CMsnProto::p2p_getSessionByID(unsigned id)
{
	if (id == 0)
		return NULL;

	mir_cslock lck(m_csSessions);

	for (int i = 0; i < m_arSessions.getCount(); i++) {
		filetransfer* FT = &m_arSessions[i];
		if (FT->p2p_sessionid == id)
			return FT;
	}

	return NULL;
}

filetransfer* CMsnProto::p2p_getSessionByUniqueID(unsigned id)
{
	if (id == 0)
		return NULL;

	mir_cslock lck(m_csSessions);

	for (int i = 0; i < m_arSessions.getCount(); i++) {
		filetransfer* FT = &m_arSessions[i];
		if (FT->p2p_acksessid == id)
			return FT;
	}

	return NULL;
}


bool CMsnProto::p2p_sessionRegistered(filetransfer* ft)
{
	if (ft != NULL && ft->p2p_appID == 0)
		return true;

	mir_cslock lck(m_csSessions);
	return m_arSessions.getIndex(ft) > -1;
}

filetransfer* CMsnProto::p2p_getThreadSession(MCONTACT hContact, TInfoType mType)
{
	mir_cslock lck(m_csSessions);

	for (int i = 0; i < m_arSessions.getCount(); i++) {
		filetransfer* FT = &m_arSessions[i];
		if (FT->std.hContact == hContact && FT->tType == mType)
			return FT;
	}

	return NULL;
}

void CMsnProto::p2p_clearThreadSessions(MCONTACT hContact, TInfoType mType)
{
	mir_cslock lck(m_csSessions);

	for (int i = 0; i < m_arSessions.getCount(); i++) {
		filetransfer* ft = &m_arSessions[i];
		if (ft->std.hContact == hContact && ft->tType == mType) {
			ft->bCanceled = true;
			ft->tType = SERVER_NOTIFICATION;
			p2p_sendCancel(ft);
		}
	}
}

filetransfer* CMsnProto::p2p_getAvatarSession(MCONTACT hContact)
{
	mir_cslock lck(m_csSessions);

	for (int i = 0; i < m_arSessions.getCount(); i++) {
		filetransfer* FT = &m_arSessions[i];
		if (FT->std.hContact == hContact && !(FT->std.flags & PFTS_SENDING) && FT->p2p_type == MSN_APPID_AVATAR)
			return FT;
	}

	return NULL;
}

bool CMsnProto::p2p_isAvatarOnly(MCONTACT hContact)
{
	mir_cslock lck(m_csSessions);

	bool result = true;
	for (int i = 0; i < m_arSessions.getCount(); i++) {
		filetransfer* FT = &m_arSessions[i];
		result &= FT->std.hContact != hContact || FT->p2p_type != MSN_APPID_FILE;
	}

	return result;
}

void CMsnProto::p2p_clearDormantSessions(void)
{
	mir_cslockfull lck(m_csSessions);

	time_t ts = time(NULL);
	for (int i = 0; i < m_arSessions.getCount(); i++) {
		filetransfer* FT = &m_arSessions[i];
		if (!FT->p2p_sessionid && !MSN_GetUnconnectedThread(FT->p2p_dest, SERVER_P2P_DIRECT))
			p2p_invite(FT->p2p_type, FT, NULL);
		else if (FT->p2p_waitack && (ts - FT->ts) > 120) {
			FT->bCanceled = true;
			p2p_sendCancel(FT);
			lck.unlock();
			p2p_unregisterSession(FT);
			lck.lock();
			i = 0;
		}
	}
}

void CMsnProto::p2p_redirectSessions(const char *wlid)
{
	mir_cslock lck(m_csSessions);

	ThreadData* T = MSN_GetP2PThreadByContact(wlid);
	for (int i = 0; i < m_arSessions.getCount(); i++) {
		filetransfer* FT = &m_arSessions[i];
		if (_stricmp(FT->p2p_dest, wlid) == 0 &&
			FT->std.currentFileProgress < FT->std.currentFileSize &&
			(T == NULL || (FT->tType != T->mType && FT->tType != 0))) {
			if (FT->p2p_isV2) {
				if ((FT->std.flags & PFTS_SENDING) && T)
					FT->tType = T->mType;
			}
			else {
				if (!(FT->std.flags & PFTS_SENDING))
					p2p_sendRedirect(FT);
			}
		}
	}
}

void CMsnProto::p2p_startSessions(const char* wlid)
{
	mir_cslock lck(m_csSessions);

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

	for (int i = 0; i < m_arSessions.getCount(); i++) {
		filetransfer* FT = &m_arSessions[i];
		if (!FT->bAccepted  && !_stricmp(FT->p2p_dest, szEmail)) {
			if (FT->p2p_appID == MSN_APPID_FILE && (FT->std.flags & PFTS_SENDING))
				p2p_invite(FT->p2p_type, FT, wlid);
			else if (FT->p2p_appID != MSN_APPID_FILE && !(FT->std.flags & PFTS_SENDING))
				p2p_invite(FT->p2p_type, FT, wlid);
		}
	}
}

void CMsnProto::p2p_cancelAllSessions(void)
{
	mir_cslock lck(m_csSessions);

	for (int i = 0; i < m_arSessions.getCount(); i++) {
		m_arSessions[i].bCanceled = true;
		p2p_sendCancel(&m_arSessions[i]);
	}
}

filetransfer* CMsnProto::p2p_getSessionByCallID(const char* CallID, const char* wlid)
{
	if (CallID == NULL)
		return NULL;

	mir_cslock lck(m_csSessions);

	char* szEmail = NULL;
	for (int i = 0; i < m_arSessions.getCount(); i++) {
		filetransfer* FT = &m_arSessions[i];
		if (FT->p2p_callID && !_stricmp(FT->p2p_callID, CallID)) {
			if (_stricmp(FT->p2p_dest, wlid)) {
				if (!szEmail)
					parseWLID(NEWSTR_ALLOCA(wlid), NULL, &szEmail, NULL);
				if (_stricmp(FT->p2p_dest, szEmail))
					continue;
			}
			return FT;
		}
	}

	return NULL;
}


void CMsnProto::p2p_registerDC(directconnection* dc)
{
	mir_cslock lck(m_csSessions);
	m_arDirect.insert(dc);
}

void CMsnProto::p2p_unregisterDC(directconnection* dc)
{
	mir_cslock lck(m_csSessions);
	m_arDirect.remove(dc);
}

directconnection* CMsnProto::p2p_getDCByCallID(const char* CallID, const char* wlid)
{
	if (CallID == NULL)
		return NULL;

	mir_cslock lck(m_csSessions);

	for (int i = 0; i < m_arDirect.getCount(); i++) {
		directconnection* DC = &m_arDirect[i];
		if (DC->callId != NULL && !strcmp(DC->callId, CallID) && !strcmp(DC->wlid, wlid))
			return DC;
	}

	return NULL;
}

/////////////////////////////////////////////////////////////////////////////////////////
// external functions

void CMsnProto::P2pSessions_Uninit(void)
{
	mir_cslock lck(m_csSessions);
	m_arSessions.destroy();
	m_arDirect.destroy();
}