summaryrefslogtreecommitdiff
path: root/protocols/Xfire/src/buddylist.cpp
blob: e3a5dd7dff19e7c6ede456b47a44446288c3801a (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
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
/*
 *  xfirelib - C++ Library for the xfire protocol.
 *  Copyright (C) 2006 by
 *          Beat Wolf <asraniel@fryx.ch> / http://gfire.sf.net
 *          Herbert Poul <herbert.poul@gmail.com> / http://goim.us
 *    http://xfirelib.sphene.net
 *
 *  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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "stdafx.h"

#include <iostream>
#include <vector>
#include "buddylist.h"
#include "buddylistonlinepacket.h"
#include "buddylistgamespacket.h"
#include "buddylistgames2packet.h"
#include "recvremovebuddypacket.h"
#include "recvstatusmessagepacket.h"
#include "recvbuddychangednick.h"
#include "xdebug.h"

//buddylist verarbeitung von der main in buddylist verschoben, um doppelte schleifenverarbeitung zuunterbinden
#include "baseProtocol.h"

extern MCONTACT handlingBuddys(xfirelib::BuddyListEntry *entry, int clan=0,char* group=NULL,BOOL dontscan=FALSE);
extern void setBuddyStatusMsg(xfirelib::BuddyListEntry *entry);

namespace xfirelib {

  using namespace std;

  BuddyList::BuddyList(Client *client) {
    entries = new vector<BuddyListEntry *>;

	//clan entries initialisieren - dufte
	entriesClan = new vector<BuddyListEntry *>;

    this->client = client;
    this->client->addPacketListener( this );
  }
  BuddyList::~BuddyList() {
    for(vector<BuddyListEntry *>::iterator it = entries->begin();
	it != entries->end(); it++) {
      delete *it;
    }
    delete entries;
	
	//clan entries entfernen - dufte
	for(vector<BuddyListEntry *>::iterator it = entriesClan->begin();
	it != entriesClan->end(); it++) {
      delete *it;
    }
    delete entriesClan;
  }

  BuddyListEntry *BuddyList::getBuddyById(long userid) {
    for(uint i = 0 ; i < entries->size() ; i++) {
      BuddyListEntry *entry = entries->at(i);
      if (entry->userid == userid)
	return entry;
    }

	//clan entries durchsuchen - dufte
	for(uint i = 0 ; i < entriesClan->size() ; i++) {
      BuddyListEntry *entry = entriesClan->at(i);
      if (entry->userid == userid)
	return entry;
    }

    return 0;
  }

  BuddyListEntry *BuddyList::getBuddyByName(string username) {
    for(uint i = 0 ; i < entries->size() ; i++) {
      BuddyListEntry *entry = entries->at(i);
      if (entry->username == username)
	return entry;
    }

	//clan entries durchsuchen - dufte
    for(uint i = 0 ; i < entriesClan->size() ; i++) {
      BuddyListEntry *entry = entriesClan->at(i);
      if (entry->username == username)
	return entry;
    }

    return 0;
  }

  BuddyListEntry *BuddyList::getBuddyBySid(const char *sid) {
    for(uint i = 0 ; i < entries->size() ; i++) {
      BuddyListEntry *entry = entries->at(i);

      if (memcmp((void *)sid,(void *)entry->sid,16) == 0) 
		return entry;

    }

	//clan entries durchsuchen - dufte
    for(uint i = 0 ; i < entriesClan->size() ; i++) {
      BuddyListEntry *entry = entriesClan->at(i);

      if (memcmp((void *)sid,(void *)entry->sid,16) == 0)
	return entry;

    }

    return 0;
  }


  void BuddyList::initEntries(BuddyListNamesPacket *buddyNames) {
    for(uint i = 0 ; i < buddyNames->usernames->size() ; i++) {
      BuddyListEntry *entry = new BuddyListEntry;
      entry->username = buddyNames->usernames->at(i);
      entry->userid = buddyNames->userids->at(i);
      entry->nick = buddyNames->nicks->at(i);
	  //buddies in miranda verarbeiten
	  handlingBuddys(entry,0,NULL);
      entries->push_back(entry);
    }
  }

  // funktion zum initialisieren der clanliste - dufte
  void BuddyList::initEntriesClan(ClanBuddyListNamesPacket *buddyNames) {
    for(uint i = 0 ; i < buddyNames->usernames->size() ; i++) {
      BuddyListEntry *entry = new BuddyListEntry;
      entry->username = buddyNames->usernames->at(i);
      entry->userid = buddyNames->userids->at(i);
      entry->nick = buddyNames->nicks->at(i);
	  entry->clanid = buddyNames->clanid;
	  /* ## buddies im miranda verarbietn */
	  char temp[255];
	  char * dummy;
	  mir_snprintf(temp,_countof(temp),"Clan_%d",entry->clanid);

	  DBVARIANT dbv;
	  if (!db_get(NULL,protocolname,temp,&dbv))
	  {
	  	dummy=dbv.pszVal;
	  }
	  else
		dummy=NULL;

   	  handlingBuddys(entry,entry->clanid,dummy);
	  /* ## ende                           */
      entriesClan->push_back(entry);
    }
  }

  void BuddyList::updateFriendsofFriend(FriendsBuddyListNamesPacket* friends)
  {
    for(uint i = 0 ; i < friends->sids->size() ; i++) {
      BuddyListEntry *entry = getBuddyBySid( friends->sids->at(i) );
        if (entry){
			XDEBUG2("Friends of Friend %s!\n",friends->usernames->at(i).c_str());
			 entry->nick=friends->nicks->at(i);
			 entry->username=friends->usernames->at(i);
			 entry->userid=friends->userids->at(i);
        }else{
			XERROR(("updateFriendsofFriendBuddies: sid not found!\n"));
        }
    }
  }

  void BuddyList::updateOnlineBuddies(BuddyListOnlinePacket* buddiesOnline) {
    for(uint i = 0 ; i < buddiesOnline->userids->size() ; i++) {
      BuddyListEntry *entry = getBuddyById( buddiesOnline->userids->at(i) );
        if (entry){
         entry->setSid( buddiesOnline->sids->at(i) );
		 //buddies in miranda verarbeiten
		 handlingBuddys(entry,0,NULL);
        }else{
			XERROR(("updateOnlineBuddies: Could not find buddy with this sid!\n"));
        }
    }
  }

  void BuddyList::updateBuddiesGame(BuddyListGamesPacket* buddiesGames) {
    bool isFirst = buddiesGames->getPacketId() == XFIRE_BUDDYS_GAMES_ID;
    for(uint i = 0 ; i < buddiesGames->sids->size() ; i++) {
      BuddyListEntry *entry = getBuddyBySid( buddiesGames->sids->at(i) );
	  if (!entry) {
		  //nicht zuordbare sids zuordnen
		  XERROR("Add dummy Contact in buddylist for friends of friends!\n");
		  BuddyListEntry *newentry = new BuddyListEntry;
		  newentry->username = "";
		  newentry->userid = 0;
		  newentry->nick = "";
		  newentry->setSid(buddiesGames->sids->at(i));
		  entries->push_back(newentry);
		  //nochmal entry suchen
		  entry = newentry; //getBuddyBySid( buddiesGames->sids->at(i) );
	  }
      if (entry){
	if (isFirst) {
	  entry->game = buddiesGames->gameids->at(i);
	  delete entry->gameObj; entry->gameObj = NULL;
	} else {
	  entry->game2 = buddiesGames->gameids->at(i);
	  delete entry->game2Obj; entry->game2Obj = NULL;
	}
	XDEBUG(( "Resolving Game... \n" ));
	XFireGameResolver *resolver = client->getGameResolver();
	if (resolver) {
	  XDEBUG(( "Resolving Game... \n" ));
	  if (isFirst)
	    entry->gameObj = resolver->resolveGame( entry->game, i, buddiesGames );
	  else
	    entry->game2Obj = resolver->resolveGame( entry->game2, i, buddiesGames );
	} else {
	  XDEBUG(( "No GameResolver ? :(\n" ));
	}
	XDEBUG(( "%s: Game (%ld): %s / Game2 (%ld): %s\n",
		 entry->username.c_str(),
		 entry->game,
		 (entry->gameObj == NULL ? "UNKNOWN" : entry->gameObj->getGameName().c_str()),
		 entry->game2,
		 (entry->game2Obj== NULL ? "UNKNOWN" :entry->game2Obj->getGameName().c_str())
		 ));
      }else{
		  XERROR("updateBuddiesGame: Could not find buddy with this sid!\n");
      }
    }
  }

  void BuddyList::receivedPacket(XFirePacket *packet) {
    XFirePacketContent *content = packet->getContent();
    if (content == 0) return;
    XDEBUG2( "hmm... %d\n", content->getPacketId() );
    switch(content->getPacketId()) {
    case XFIRE_BUDDYS_NAMES_ID: {
      XINFO(( "Received Buddy List..\n" ));
      this->initEntries( (BuddyListNamesPacket*)content );
      break;
    }
	case XFIRE_CLAN_BUDDYS_NAMES_ID: {
      XINFO(( "Received Clan Buddy List..\n" ));
      this->initEntriesClan( (ClanBuddyListNamesPacket*)content );
      break;
    }
	//neue nicks updaten, dufte
	case XFIRE_RECVBUDDYCHANGEDNICK: {
	  RecvBuddyChangedNick* recvchangednick=(RecvBuddyChangedNick*)content;
      XINFO(( "Received new nick of a buddy..\n" ));
      BuddyListEntry* entry=NULL;
	  entry=this->getBuddyById(recvchangednick->userid);
	  if (entry) {
		  entry->nick=recvchangednick->newnick;
		  recvchangednick->entry=(void*)entry;
		  handlingBuddys(entry,0,NULL);
	  }
      break;
    }
    case XFIRE_BUDDYS_ONLINE_ID: {
      XINFO(( "Received Buddy Online Packet..\n" ));
      this->updateOnlineBuddies( (BuddyListOnlinePacket *)content );
      break;
    }
    case XFIRE_FRIENDS_BUDDYS_NAMES_ID: {
      XINFO(( "Received Friends of Friend..\n" ));
      this->updateFriendsofFriend( (FriendsBuddyListNamesPacket *)content );
      break;
    }
    case XFIRE_BUDDYS_GAMES2_ID:
    case XFIRE_BUDDYS_GAMES_ID: {
      XINFO(( "Recieved the game a buddy is playing..\n" ));
      this->updateBuddiesGame( (BuddyListGamesPacket *)content );
	  break;
    }
    case XFIRE_RECVREMOVEBUDDYPACKET: {
      RecvRemoveBuddyPacket *p = (RecvRemoveBuddyPacket*)content;
      XDEBUG2( "Buddy was removed from contact list (userid: %ld)\n", p->userid );
      std::vector<BuddyListEntry *>::iterator i = entries->begin();
      while( i != entries->end() ) {
	if ((*i)->userid == p->userid) {
	  BuddyListEntry *buddy = *i;
	  XINFO(( "%s (%s) was removed from BuddyList.\n", buddy->username.c_str(), buddy->nick.c_str() ));
	  p->username = buddy->username;
	  p->handle = buddy->hcontact; // handle übergeben - dufte
	  entries->erase(i);
	  // i.erase();
	  break; // we are done.
	}
	++i;
      }
      break;
    }
    case XFIRE_RECV_STATUSMESSAGE_PACKET_ID: {
      RecvStatusMessagePacket *status = (RecvStatusMessagePacket*) content;

	  //status->entries=new PBuddyListEntry[status->sids->size];

     for(uint i = 0 ; i < status->sids->size() ; i++) {
        BuddyListEntry *entry = getBuddyBySid( status->sids->at(i) );
		//status->entries[i]=entry;

        if (entry == NULL) {
            XERROR(( "No such Entry - Got StatusMessage from someone who is not in the buddylist ??\n" ));
            return;
        }
		else
		{
			entry->statusmsg = status->msgs->at(i);
			setBuddyStatusMsg(entry); //auf eine funktion reduziert, verringert cpuauslastung und beseitigt das
													 //das problem der fehlenden statusmsg
		}
    }

      break;
    }
    }
  }

  BuddyListEntry::~BuddyListEntry() {
	  if (lastpopup) {
		  delete[] lastpopup;
		  lastpopup=NULL;
	  }
  }

  BuddyListEntry::BuddyListEntry() {
    memset(sid,0,16);
    statusmsg = std::string();
    game = 0;
    game2 = 0;
    gameObj = NULL;
    game2Obj = NULL;
	hcontact = NULL;
	clanid = 0;
	lastpopup=NULL;
  }
  bool BuddyListEntry::isOnline() {
    for(int i = 0 ; i < 16 ; i++) {
      if (sid[i]) return true;
    }
    return false;
  }
  void BuddyListEntry::setSid(const char *sid) {
    int s=0;
	for(int i = 0 ; i < 16 ; i++) { //wenn buddy offline geht, seine gameinfos zurücksetzen - dufte
      if (sid[i])
	  {
		  s=1;
		  break;
	  }
    }
	if (s)
	{
		this->statusmsg = std::string();
		this->game = 0;
		this->game2 = 0;
		this->gameObj = NULL;
		this->game2Obj = NULL;
	}
    memcpy(this->sid,sid,16);
  }
};