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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
/*
Jabber Protocol Plugin for Miranda IM
Copyright (C) 2002-04 Santithorn Bunchua
Copyright (C) 2005-12 George Hazan
Copyright (C) 2007 Maxim Mluhov
Copyright (C) 2012-13 Miranda NG Project
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "jabber.h"
#include "jabber_list.h"
void MenuUpdateSrmmIcon(JABBER_LIST_ITEM *item);
/////////////////////////////////////////////////////////////////////////////////////////
// List item freeing
static void JabberListFreeResourceInternal(JABBER_RESOURCE_STATUS *r)
{
if (r->resourceName) mir_free(r->resourceName);
if (r->nick) mir_free(r->nick);
if (r->statusMessage) mir_free(r->statusMessage);
if (r->software) mir_free(r->software);
if (r->version) mir_free(r->version);
if (r->system) mir_free(r->system);
if (r->szCapsNode) mir_free(r->szCapsNode);
if (r->szCapsVer) mir_free(r->szCapsVer);
if (r->szCapsExt) mir_free(r->szCapsExt);
if (r->szRealJid) mir_free(r->szRealJid);
if (r->pSoftwareInfo) delete r->pSoftwareInfo;
}
static void JabberListFreeItemInternal(JABBER_LIST_ITEM *item)
{
if (item == NULL)
return;
JABBER_RESOURCE_STATUS* r = item->pResources;
for (int i=0; i < item->resourceCount; i++, r++)
JabberListFreeResourceInternal(r);
JabberListFreeResourceInternal(&item->itemResource);
if (item->photoFileName) {
if (item->list == LIST_VCARD_TEMP)
DeleteFile(item->photoFileName);
mir_free(item->photoFileName);
}
mir_free(item->jid);
mir_free(item->nick);
mir_free(item->pResources);
mir_free(item->group);
mir_free(item->messageEventIdStr);
mir_free(item->name);
mir_free(item->type);
mir_free(item->service);
mir_free(item->password);
if (item->list == LIST_ROSTER && item->ft)
delete item->ft;
mir_free(item);
}
void CJabberProto::ListWipe(void)
{
mir_cslock lck(m_csLists);
for (int i=0; i < m_lstRoster.getCount(); i++)
JabberListFreeItemInternal(m_lstRoster[i]);
m_lstRoster.destroy();
}
int CJabberProto::ListExist(JABBER_LIST list, const TCHAR *jid)
{
JABBER_LIST_ITEM tmp;
tmp.list = list;
tmp.jid = (TCHAR*)jid;
tmp.bUseResource = FALSE;
mir_cslock lck(m_csLists);
if (list == LIST_ROSTER) {
tmp.list = LIST_CHATROOM;
int id = m_lstRoster.getIndex(&tmp);
if (id != -1)
tmp.bUseResource = TRUE;
tmp.list = list;
}
int idx = m_lstRoster.getIndex(&tmp);
return (idx == -1) ? 0 : idx+1;
}
JABBER_LIST_ITEM *CJabberProto::ListAdd(JABBER_LIST list, const TCHAR *jid)
{
BOOL bUseResource=FALSE;
mir_cslockfull lck(m_csLists);
JABBER_LIST_ITEM *item = ListGetItemPtr(list, jid);
if (item != NULL)
return item;
TCHAR *s = mir_tstrdup(jid);
TCHAR *q = NULL;
// strip resource name if any
//fyr
if ( !((list== LIST_ROSTER) && ListExist(LIST_CHATROOM, jid))) { // but only if it is not chat room contact
if (list != LIST_VCARD_TEMP) {
TCHAR *p;
if ((p = _tcschr(s, '@')) != NULL)
if ((q = _tcschr(p, '/')) != NULL)
*q = '\0';
}
}
else bUseResource = TRUE;
if ( !bUseResource && list == LIST_ROSTER) {
//if it is a chat room keep resource and made it resource sensitive
if (ChatRoomHContactFromJID(s)) {
if (q != NULL)
*q='/';
bUseResource = TRUE;
}
}
item = (JABBER_LIST_ITEM*)mir_calloc(sizeof(JABBER_LIST_ITEM));
item->list = list;
item->jid = s;
item->itemResource.status = ID_STATUS_OFFLINE;
item->resourceMode = RSMODE_LASTSEEN;
item->bUseResource = bUseResource;
m_lstRoster.insert(item);
lck.unlock();
MenuUpdateSrmmIcon(item);
return item;
}
void CJabberProto::ListRemove(JABBER_LIST list, const TCHAR *jid)
{
mir_cslock lck(m_csLists);
int i = ListExist(list, jid);
if (i != 0) {
JabberListFreeItemInternal(m_lstRoster[ --i ]);
m_lstRoster.remove(i);
}
}
void CJabberProto::ListRemoveList(JABBER_LIST list)
{
int i = 0;
while ((i=ListFindNext(list, i)) >= 0)
ListRemoveByIndex(i);
}
void CJabberProto::ListRemoveByIndex(int index)
{
mir_cslock lck(m_csLists);
if (index >= 0 && index < m_lstRoster.getCount()) {
JabberListFreeItemInternal(m_lstRoster[index]);
m_lstRoster.remove(index);
}
}
JABBER_RESOURCE_STATUS *CJabberProto::ListFindResource(JABBER_LIST list, const TCHAR *jid)
{
mir_cslock lck(m_csLists);
int i = ListExist(list, jid);
if (i == 0)
return NULL;
JABBER_LIST_ITEM* LI = m_lstRoster[i-1];
const TCHAR *p = _tcschr(jid, '@');
const TCHAR *q = _tcschr((p == NULL) ? jid : p, '/');
if (q == NULL)
return NULL;
const TCHAR *resource = q+1;
if (*resource)
for (int j=0; j < LI->resourceCount; j++)
if ( !_tcscmp(LI->pResources[j].resourceName, resource))
return LI->pResources + j;
return NULL;
}
int CJabberProto::ListAddResource(JABBER_LIST list, const TCHAR *jid, int status, const TCHAR *statusMessage, char priority, const TCHAR *nick)
{
mir_cslockfull lck(m_csLists);
int i = ListExist(list, jid);
if (!i)
return 0;
JABBER_LIST_ITEM* LI = m_lstRoster[i-1];
int bIsNewResource = false, j;
const TCHAR *p = _tcschr(jid, '@');
const TCHAR *q = _tcschr((p == NULL) ? jid : p, '/');
if (q) {
const TCHAR *resource = q+1;
if (resource[0]) {
JABBER_RESOURCE_STATUS* r = LI->pResources;
for (j=0; j < LI->resourceCount; j++, r++) {
if ( !_tcscmp(r->resourceName, resource)) {
// Already exist, update status and statusMessage
r->status = status;
replaceStrT(r->statusMessage, statusMessage);
r->priority = priority;
break;
} }
if (j >= LI->resourceCount) {
// Not already exist, add new resource
LI->pResources = (JABBER_RESOURCE_STATUS *) mir_realloc(LI->pResources, (LI->resourceCount+1)*sizeof(JABBER_RESOURCE_STATUS));
bIsNewResource = true;
r = LI->pResources + LI->resourceCount++;
memset(r, 0, sizeof(JABBER_RESOURCE_STATUS));
r->status = status;
r->affiliation = AFFILIATION_NONE;
r->role = ROLE_NONE;
r->resourceName = mir_tstrdup(resource);
r->nick = mir_tstrdup(nick);
if (statusMessage)
r->statusMessage = mir_tstrdup(statusMessage);
r->priority = priority;
} }
}
// No resource, update the main statusMessage
else {
LI->itemResource.status = status;
replaceStrT(LI->itemResource.statusMessage, statusMessage);
}
lck.unlock();
MenuUpdateSrmmIcon(LI);
return bIsNewResource;
}
void CJabberProto::ListRemoveResource(JABBER_LIST list, const TCHAR *jid)
{
mir_cslockfull lck(m_csLists);
int i = ListExist(list, jid);
JABBER_LIST_ITEM *LI = m_lstRoster[i-1];
if (i == 0 || LI == NULL)
return;
const TCHAR *p = _tcschr(jid, '@');
const TCHAR *q = _tcschr((p == NULL) ? jid : p, '/');
if (q == NULL)
return;
const TCHAR *resource = q+1;
if (resource[0] == 0)
return;
JABBER_RESOURCE_STATUS *r = LI->pResources;
for (int j=0; j < LI->resourceCount; j++, r++)
if ( !_tcsicmp(r->resourceName, resource))
break;
if (r >= LI->pResources + LI->resourceCount)
return;
// Found last seen resource ID to be removed
if (LI->pLastSeenResource == r)
LI->pLastSeenResource = NULL;
// update manually selected resource ID
if (LI->resourceMode == RSMODE_MANUAL) {
if (LI->pManualResource == r) {
LI->resourceMode = RSMODE_LASTSEEN;
LI->pManualResource = NULL;
}
}
// Update MirVer due to possible resource changes
UpdateMirVer(LI);
JabberListFreeResourceInternal(r);
if (LI->resourceCount-- == 1) {
mir_free(r);
LI->pResources = NULL;
}
else {
memmove(r, r+1, (LI->resourceCount - (r-LI->pResources))*sizeof(JABBER_RESOURCE_STATUS));
LI->pResources = (JABBER_RESOURCE_STATUS*)mir_realloc(LI->pResources, LI->resourceCount*sizeof(JABBER_RESOURCE_STATUS));
}
lck.unlock();
MenuUpdateSrmmIcon(LI);
}
TCHAR* CJabberProto::ListGetBestResourceNamePtr(const TCHAR *jid)
{
mir_cslock lck(m_csLists);
int i = ListExist(LIST_ROSTER, jid);
if (i == 0)
return NULL;
TCHAR* res = NULL;
JABBER_LIST_ITEM* LI = m_lstRoster[i-1];
if (LI->resourceCount > 1) {
if (LI->resourceMode == RSMODE_LASTSEEN && LI->pLastSeenResource)
res = LI->pLastSeenResource->resourceName;
else if (LI->resourceMode == RSMODE_MANUAL && LI->pManualResource)
res = LI->pManualResource->resourceName;
else {
int nBestPos = -1, nBestPri = -200, j;
for (j = 0; j < LI->resourceCount; j++) {
if (LI->pResources[ j ].priority > nBestPri) {
nBestPri = LI->pResources[ j ].priority;
nBestPos = j;
}
}
if (nBestPos != -1)
res = LI->pResources[nBestPos].resourceName;
}
}
if (!res && LI->pResources)
res = LI->pResources[0].resourceName;
return res;
}
TCHAR* CJabberProto::ListGetBestClientResourceNamePtr(const TCHAR *jid)
{
mir_cslock lck(m_csLists);
int i = ListExist(LIST_ROSTER, jid);
if (i == 0)
return NULL;
JABBER_LIST_ITEM* LI = m_lstRoster[i-1];
TCHAR* res = ListGetBestResourceNamePtr(jid);
if (res == NULL) {
JABBER_RESOURCE_STATUS* r = LI->pResources;
int status = ID_STATUS_OFFLINE;
res = NULL;
for (i=0; i < LI->resourceCount; i++) {
int s = r[i].status;
BOOL foundBetter = FALSE;
switch (s) {
case ID_STATUS_FREECHAT:
foundBetter = TRUE;
break;
case ID_STATUS_ONLINE:
if (status != ID_STATUS_FREECHAT)
foundBetter = TRUE;
break;
case ID_STATUS_DND:
if (status != ID_STATUS_FREECHAT && status != ID_STATUS_ONLINE)
foundBetter = TRUE;
break;
case ID_STATUS_AWAY:
if (status != ID_STATUS_FREECHAT && status != ID_STATUS_ONLINE && status != ID_STATUS_DND)
foundBetter = TRUE;
break;
case ID_STATUS_NA:
if (status != ID_STATUS_FREECHAT && status != ID_STATUS_ONLINE && status != ID_STATUS_DND && status != ID_STATUS_AWAY)
foundBetter = TRUE;
break;
}
if (foundBetter) {
res = r[i].resourceName;
status = s;
} } }
return res;
}
int CJabberProto::ListFindNext(JABBER_LIST list, int fromOffset)
{
mir_cslock lck(m_csLists);
int i = (fromOffset >= 0) ? fromOffset : 0;
for (; i<m_lstRoster.getCount(); i++)
if (m_lstRoster[i]->list == list)
return i;
return -1;
}
JABBER_LIST_ITEM *CJabberProto::ListGetItemPtr(JABBER_LIST list, const TCHAR *jid)
{
mir_cslock lck(m_csLists);
int i = ListExist(list, jid);
return (i == 0) ? NULL : m_lstRoster[i-1];
}
JABBER_LIST_ITEM *CJabberProto::ListGetItemPtrFromIndex(int index)
{
mir_cslock lck(m_csLists);
if (index >= 0 && index < m_lstRoster.getCount())
return m_lstRoster[index];
return NULL;
}
BOOL CJabberProto::ListLock()
{
EnterCriticalSection(&m_csLists);
return TRUE;
}
BOOL CJabberProto::ListUnlock()
{
LeaveCriticalSection(&m_csLists);
return TRUE;
}
|