summaryrefslogtreecommitdiff
path: root/protocols/FacebookRM/src/process.cpp
blob: e0608e3555e48cd1e99f8e7914b8f5be6fb2d702 (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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*

Facebook plugin for Miranda Instant Messenger
_____________________________________________

Copyright © 2009-11 Michal Zelinka, 2011-13 Robert Pösel

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 "common.h"

void FacebookProto::ProcessBuddyList(void* data)
{
	if (data == NULL)
		return;

	ScopedLock s(facy.buddies_lock_);

	std::string* resp = (std::string*)data;

	if (isOffline())
		goto exit;

	LOG("***** Starting processing buddy list");

	CODE_BLOCK_TRY

	facebook_json_parser* p = new facebook_json_parser(this);
	p->parse_buddy_list(data, &facy.buddies);
	delete p;

	bool use_mobile_status = db_get_b(NULL,m_szModuleName,FACEBOOK_KEY_LOAD_MOBILE, DEFAULT_LOAD_MOBILE) != 0;

	for (List::Item< facebook_user >* i = facy.buddies.begin(); i != NULL;)
	{		
		facebook_user* fbu = i->data;
		bool on_mobile = false;

		char *status;
		switch (fbu->status_id) {
			case ID_STATUS_OFFLINE:
				status = "offline"; break;				
			case ID_STATUS_ONLINE:
				status = "online"; break;				
			case ID_STATUS_ONTHEPHONE:
				on_mobile = true;				
				status = "onthephone";

				if (!use_mobile_status)
					fbu->status_id = ID_STATUS_OFFLINE;
				break;
		}
		LOG("      Now %s: %s", status, i->data->real_name.c_str());

		if (!fbu->deleted)
		{
			HANDLE hContact = fbu->handle;
			if (!hContact)
				hContact = AddToContactList(fbu, FACEBOOK_CONTACT_FRIEND);
			
			DBVARIANT dbv;
			TCHAR* client = on_mobile ? _T(FACEBOOK_MOBILE) : _T(FACEBOOK_NAME);
			if (!db_get_ts(hContact,m_szModuleName,"MirVer",&dbv)) {
				if (_tcscmp(dbv.ptszVal, client))
					db_set_ts(hContact,m_szModuleName,"MirVer",client);
				db_free(&dbv);
			} else {
				db_set_ts(hContact,m_szModuleName,"MirVer",client);
			}
		}

		if (fbu->status_id == ID_STATUS_OFFLINE || fbu->deleted)
		{
			if (fbu->handle)
				db_set_w(fbu->handle, m_szModuleName, "Status", ID_STATUS_OFFLINE);

			std::string to_delete(i->key);
			i = i->next;
			facy.buddies.erase(to_delete);
		} else {
			i = i->next;

			if (!fbu->handle) { // just been added
				fbu->handle = AddToContactList(fbu, FACEBOOK_CONTACT_FRIEND);
	
				if (!fbu->real_name.empty()) {
					db_set_utf(fbu->handle,m_szModuleName,FACEBOOK_KEY_NAME,fbu->real_name.c_str());
					db_set_utf(fbu->handle,m_szModuleName,FACEBOOK_KEY_NICK,fbu->real_name.c_str());
				}
			}

			if (db_get_w(fbu->handle,m_szModuleName,"Status", 0) != fbu->status_id) {
				db_set_w(fbu->handle,m_szModuleName,"Status", fbu->status_id);
			}

			if (db_get_b(fbu->handle, m_szModuleName, FACEBOOK_KEY_CONTACT_TYPE, 0) != FACEBOOK_CONTACT_FRIEND) {
				db_set_b(fbu->handle, m_szModuleName, FACEBOOK_KEY_CONTACT_TYPE, FACEBOOK_CONTACT_FRIEND);
				// TODO: remove that popup and use "Contact added you" event?
			}

			// Wasn't contact removed from "server-list" someday?
			if (db_get_dw(fbu->handle, m_szModuleName, FACEBOOK_KEY_DELETED, 0)) {
				db_unset(fbu->handle, m_szModuleName, FACEBOOK_KEY_DELETED);

				std::string url = FACEBOOK_URL_PROFILE + fbu->user_id;					

				TCHAR* szTitle = mir_a2t_cp(fbu->real_name.c_str(), CP_UTF8);
				TCHAR* szUrl = mir_a2t_cp(url.c_str(), CP_UTF8);
				NotifyEvent(szTitle, TranslateT("Contact is back on server-list."), fbu->handle, FACEBOOK_EVENT_OTHER, szUrl);
				mir_free(szTitle);
				// mir_free(szUrl); // url is free'd in popup procedure
			}

			// Check avatar change
			CheckAvatarChange(fbu->handle, fbu->image_url);
		}
	}

	LOG("***** Buddy list processed");

	CODE_BLOCK_CATCH

	LOG("***** Error processing buddy list: %s", e.what());

	CODE_BLOCK_END

exit:
	delete resp;
}

void FacebookProto::ProcessFriendList(void* data)
{
	if (data == NULL)
		return;

	std::string* resp = (std::string*)data;

	LOG("***** Starting processing friend list");

	CODE_BLOCK_TRY

	std::map<std::string, facebook_user*> friends;

	facebook_json_parser* p = new facebook_json_parser(this);
	p->parse_friends(data, &friends);
	delete p;


	// Check and update old contacts
	for (HANDLE hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) {
		if (db_get_b(hContact,m_szModuleName,"ChatRoom",0))
			continue;

		DBVARIANT dbv;
		facebook_user *fbu;
		if (!db_get_s(hContact,m_szModuleName,FACEBOOK_KEY_ID,&dbv)) {
			std::string id = dbv.pszVal;
			db_free(&dbv);
			
			std::map< std::string, facebook_user* >::iterator iter;
			
			if ((iter = friends.find(id)) != friends.end()) {
				// Found contact, update it and remove from map
				fbu = iter->second;

				DBVARIANT dbv;
				bool update_required = true;

				// TODO RM: remove, because contacts cant change it, so its only for "first run"
					// - but what with contacts, that was added after logon?
				// Update gender
				if (db_get_b(hContact, m_szModuleName, "Gender", 0) != fbu->gender)
					db_set_b(hContact, m_szModuleName, "Gender", fbu->gender);

				// Update real name
				if (!db_get_utf(hContact, m_szModuleName, FACEBOOK_KEY_NAME, &dbv))
				{
					update_required = strcmp(dbv.pszVal, fbu->real_name.c_str()) != 0;
					db_free(&dbv);
				}
				if (update_required)
				{
					db_set_utf(hContact, m_szModuleName, FACEBOOK_KEY_NAME, fbu->real_name.c_str());
					db_set_utf(hContact, m_szModuleName, FACEBOOK_KEY_NICK, fbu->real_name.c_str());
				}

				if (db_get_b(hContact, m_szModuleName, FACEBOOK_KEY_CONTACT_TYPE, 0) != FACEBOOK_CONTACT_FRIEND) {
					db_set_b(hContact, m_szModuleName, FACEBOOK_KEY_CONTACT_TYPE, FACEBOOK_CONTACT_FRIEND);
					// TODO: remove that popup and use "Contact added you" event?
				}

				// Wasn't contact removed from "server-list" someday?
				if (db_get_dw(hContact, m_szModuleName, FACEBOOK_KEY_DELETED, 0)) {
					db_unset(hContact, m_szModuleName, FACEBOOK_KEY_DELETED);

					std::string url = FACEBOOK_URL_PROFILE + fbu->user_id;					

					TCHAR* szTitle = mir_a2t_cp(fbu->real_name.c_str(), CP_UTF8);
					TCHAR* szUrl = mir_a2t_cp(url.c_str(), CP_UTF8);
					NotifyEvent(szTitle, TranslateT("Contact is back on server-list."), hContact, FACEBOOK_EVENT_OTHER, szUrl);					
					mir_free(szTitle);
					// mir_free(szUrl); // url is free'd in popup procedure
				}

				// Check avatar change
				CheckAvatarChange(hContact, fbu->image_url);
			
				delete fbu;
				friends.erase(iter);
			} else {
				// Contact was removed from "server-list", notify it

				// Wasnt we already been notified about this contact? And was this real friend?
				if (!db_get_dw(hContact, m_szModuleName, FACEBOOK_KEY_DELETED, 0) 
					&& db_get_b(hContact, m_szModuleName, FACEBOOK_KEY_CONTACT_TYPE, 0) == FACEBOOK_CONTACT_FRIEND)
				{

					db_set_dw(hContact, m_szModuleName, FACEBOOK_KEY_DELETED, ::time(NULL));
					db_set_b(hContact, m_szModuleName, FACEBOOK_KEY_CONTACT_TYPE, FACEBOOK_CONTACT_NONE);

					std::string contactname = id;
					if (!db_get_utf(hContact, m_szModuleName, FACEBOOK_KEY_NAME, &dbv)) {
						contactname = dbv.pszVal;
						db_free(&dbv);
					}

					std::string url = FACEBOOK_URL_PROFILE + id;

					TCHAR* szTitle = mir_a2t_cp(contactname.c_str(), CP_UTF8);
					TCHAR* szUrl = mir_a2t_cp(url.c_str(), CP_UTF8);
					NotifyEvent(szTitle, TranslateT("Contact is no longer on server-list."), hContact, FACEBOOK_EVENT_OTHER, szUrl);
					mir_free(szTitle);
					// mir_free(szUrl); // url is free'd in popup procedure
				}
			}
		}
	}

	// Check remain contacts in map and add it to contact list
	for (std::map< std::string, facebook_user* >::iterator iter = friends.begin(); iter != friends.end(); ++iter)
	{
		facebook_user *fbu = iter->second;
		
		HANDLE hContact = AddToContactList(fbu, FACEBOOK_CONTACT_FRIEND, true); // This contact is surely new

		db_set_b(hContact, m_szModuleName, "Gender", fbu->gender);
		db_set_utf(hContact, m_szModuleName, FACEBOOK_KEY_NAME, fbu->real_name.c_str());
		db_set_utf(hContact, m_szModuleName, FACEBOOK_KEY_NICK, fbu->real_name.c_str());
		db_set_s(hContact, m_szModuleName, FACEBOOK_KEY_AV_URL, fbu->image_url.c_str());
//		db_set_w(hContact, m_szModuleName, "Status", ID_STATUS_OFFLINE);
	}

	LOG("***** Friend list processed");

	CODE_BLOCK_CATCH

	LOG("***** Error processing friend list: %s", e.what());

	CODE_BLOCK_END

	delete resp;
}

void FacebookProto::ProcessUnreadMessages(void*)
{
	facy.handle_entry("messages");

	std::string get_data = "sk=inbox&query=is%3Aunread";

	std::string data = "&fb_dtsg=" + facy.dtsg_;
	data += "&lsd=&phstamp=" + utils::time::mili_timestamp();
	data += "&__user=" + facy.self_.user_id;

	// Get unread inbox threads
	http::response resp = facy.flap(FACEBOOK_REQUEST_ASYNC, &data, &get_data);

	// sk=inbox,  sk=other

	// Process result data
	facy.validate_response(&resp);	

	if (resp.code != HTTP_CODE_OK) {
		facy.handle_error("messages");
		return;
	}

	std::string threadlist = utils::text::slashu_to_utf8(resp.data);
	
	std::string::size_type pos = 0;

	while ((pos = threadlist.find("<li class=\\\"threadRow noDraft unread", pos)) != std::string::npos)
	{
		std::string::size_type pos2 = threadlist.find("/li>", pos);
		std::string thread_content = threadlist.substr(pos, pos2 - pos);

		pos = pos2;

		get_data = "sk=inbox&query=is%3Aunread&thread_query=is%3Aunread&action=read&tid=";
		get_data += utils::text::source_get_value(&thread_content, 2, "id=\\\"", "\\\"");
		utils::text::replace_all(&get_data, "+", "%2B");
		
		resp = facy.flap(FACEBOOK_REQUEST_ASYNC, &data, &get_data);
		// TODO: move this to new thread...

		facy.validate_response(&resp);

		if (resp.code != HTTP_CODE_OK) {
			LOG(" !! !! Error when getting messages list");
			continue;
		}
		
		std::string messageslist = utils::text::slashu_to_utf8(resp.data);		
		
		std::string user_id = utils::text::source_get_value(&messageslist, 2, "single_thread_id\":", ",");
		if (user_id.empty()) {
			LOG(" !! !! Thread id is empty - this is groupchat message."); // TODO: remove as this is not such 'error'
			continue;
		}

		facebook_user fbu;
		fbu.user_id = user_id;

		HANDLE hContact = AddToContactList(&fbu, FACEBOOK_CONTACT_NONE);
		// TODO: if contact is newly added, get his user info
		// TODO: maybe create new "receiveMsg" function and use it for offline and channel messages?

		pos2 = 0;
		while ((pos2 = messageslist.find("class=\\\"MessagingMessage ", pos2)) != std::string::npos) {
			pos2 += 8;
			std::string strclass = messageslist.substr(pos2, messageslist.find("\\\"", pos2) - pos2);

			if (strclass.find("MessagingMessageUnread") == std::string::npos)
				continue; // ignoring old messages

			//std::string::size_type pos3 = messageslist.find("/li>", pos2); // TODO: ne proti tomuhle li, protože i přílohy mají li...
			std::string::size_type pos3 = messageslist.find("class=\\\"MessagingMessage ", pos2);
			std::string messagesgroup = messageslist.substr(pos2, pos3 - pos2);

			DWORD timestamp = utils::conversion::to_timestamp(
								utils::text::source_get_value(&messagesgroup, 2, "data-utime=\\\"", "\\\""));

			pos3 = 0;
			while ((pos3 = messagesgroup.find("class=\\\"content noh", pos3)) != std::string::npos)
			{

				std::string message_attachments = "";
				std::string::size_type pos4 = 0;
				if ((pos4 = messagesgroup.find("class=\\\"attachments\\\"", pos4)) != std::string::npos) {
					std::string attachments = messagesgroup.substr(pos4, messagesgroup.find("<\\/ul", pos4) - pos4);

					pos4 = 0;
					while ((pos4 = attachments.find("<li", pos4)) != std::string::npos) {
						std::string attachment = attachments.substr(pos4, attachments.find("<\\/li>", pos4) - pos4);
						std::string link = utils::text::source_get_value(&attachment, 4, "<a class=", "attachment", "href=\\\"", "\\\"");

						link = utils::text::trim(
								utils::text::special_expressions_decode(link));

						// or first: std::string name = utils::text::source_get_value(&attachment, 4, "<a class=", "attachment", ">", "<\\/a>");
						std::string name = utils::text::trim(
								utils::text::special_expressions_decode(
									utils::text::remove_html(attachment)));

						if (link.find("/ajax/messaging/attachments/photo/dialog.php?uri=") != std::string::npos) {
							link = link.substr(49);
							link = utils::url::decode(link);
						}

						message_attachments += "< " + name + " > " + FACEBOOK_URL_HOMEPAGE;
						message_attachments += link + "\r\n";

						pos4++;
					}

				}

				std::string message_text = messagesgroup.substr(pos3, messagesgroup.find("<\\/div", pos3) + 6 - pos3);
				LOG("Got unread message: \"%s\"", message_text.c_str());
				message_text = utils::text::source_get_value(&message_text, 2, "\\\">", "<\\/div");
				message_text = utils::text::trim(
								utils::text::special_expressions_decode(
									utils::text::remove_html(message_text)));

				parseSmileys(message_text, hContact);

				if (!message_attachments.empty()) {
					if (!message_text.empty())
						message_text += "\r\n\r\n";

					message_text += Translate("Attachments:");
					message_text += "\r\n" + message_attachments;
				}

				PROTORECVEVENT recv = {0};
				recv.flags = PREF_UTF;
				recv.szMessage = const_cast<char*>(message_text.c_str());
				recv.timestamp = timestamp;
				ProtoChainRecvMsg(hContact, &recv);

				pos3++;
			}

		}
		
	}

}

void FacebookProto::ProcessMessages(void* data)
{
	if (data == NULL)
		return;

	std::string* resp = (std::string*)data;

	if (isOffline())
		goto exit;

	LOG("***** Starting processing messages");

	CODE_BLOCK_TRY

	std::vector< facebook_message* > messages;
	std::vector< facebook_notification* > notifications;

	facebook_json_parser* p = new facebook_json_parser(this);
	p->parse_messages(data, &messages, &notifications);
	delete p;

	bool local_timestamp = getByte(FACEBOOK_KEY_LOCAL_TIMESTAMP, 0) != 0;

	for(std::vector<facebook_message*>::size_type i=0; i<messages.size(); i++)
	{
		if (messages[i]->user_id != facy.self_.user_id)
		{
			LOG("      Got message: %s", messages[i]->message_text.c_str());
			facebook_user fbu;
			fbu.user_id = messages[i]->user_id;

			HANDLE hContact = AddToContactList(&fbu, FACEBOOK_CONTACT_NONE, false, messages[i]->sender_name.c_str());

			// TODO: if contact is newly added, get his user info
			// TODO: maybe create new "receiveMsg" function and use it for offline and channel messages?

			parseSmileys(messages[i]->message_text, hContact);

			PROTORECVEVENT recv = {0};
			recv.flags = PREF_UTF;
			recv.szMessage = const_cast<char*>(messages[i]->message_text.c_str());
			recv.timestamp = local_timestamp ? ::time(NULL) : messages[i]->time;
			ProtoChainRecvMsg(hContact, &recv);
		}
		delete messages[i];
	}
	messages.clear();

	for(std::vector<facebook_notification*>::size_type i=0; i<notifications.size(); i++)
	{
		LOG("      Got notification: %s", notifications[i]->text.c_str());
		TCHAR* szTitle = mir_a2t_cp(this->m_szModuleName, CP_UTF8);
		TCHAR* szText = mir_a2t_cp(notifications[i]->text.c_str(), CP_UTF8);
		TCHAR* szUrl = mir_a2t_cp(notifications[i]->link.c_str(), CP_UTF8);
		NotifyEvent(szTitle, szText, ContactIDToHContact(notifications[i]->user_id), FACEBOOK_EVENT_NOTIFICATION, szUrl);
		mir_free(szTitle);
		mir_free(szText);
//		mir_free(szUrl); // URL is free'd in popup procedure

		delete notifications[i];
	}
	notifications.clear();

	LOG("***** Messages processed");

	CODE_BLOCK_CATCH

	LOG("***** Error processing messages: %s", e.what());

	CODE_BLOCK_END

exit:
	delete resp;
}

void FacebookProto::ProcessNotifications(void*)
{
	if (isOffline())
		return;

	if (!getByte(FACEBOOK_KEY_EVENT_NOTIFICATIONS_ENABLE, DEFAULT_EVENT_NOTIFICATIONS_ENABLE))
		return;

	facy.handle_entry("notifications");

	// Get notifications
	http::response resp = facy.flap(FACEBOOK_REQUEST_NOTIFICATIONS);

	// Process result data
	facy.validate_response(&resp);
  
	if (resp.code != HTTP_CODE_OK) {
		facy.handle_error("notifications");
		return;
	}


	// Process notifications
	LOG("***** Starting processing notifications");

	CODE_BLOCK_TRY

	std::vector< facebook_notification* > notifications;

	facebook_json_parser* p = new facebook_json_parser(this);
	p->parse_notifications(&(resp.data), &notifications);
	delete p;

	for(std::vector<facebook_notification*>::size_type i=0; i<notifications.size(); i++)
	{
		LOG("      Got notification: %s", notifications[i]->text.c_str());
		TCHAR* szTitle = mir_a2t_cp(this->m_szModuleName, CP_UTF8);
		TCHAR* szText = mir_a2t_cp(notifications[i]->text.c_str(), CP_UTF8);
		TCHAR* szUrl = mir_a2t_cp(notifications[i]->link.c_str(), CP_UTF8);
		NotifyEvent(szTitle, szText, ContactIDToHContact(notifications[i]->user_id), FACEBOOK_EVENT_NOTIFICATION, szUrl);
		mir_free(szTitle);
		mir_free(szText);
//		mir_free(szUrl); // URL is free'd in popup procedure

		delete notifications[i];
	}
	notifications.clear();

	LOG("***** Notifications processed");

	CODE_BLOCK_CATCH

	LOG("***** Error processing notifications: %s", e.what());

	CODE_BLOCK_END
}

void FacebookProto::ProcessFriendRequests(void*)
{
	facy.handle_entry("friendRequests");

	// Get notifications
	http::response resp = facy.flap(FACEBOOK_REQUEST_LOAD_REQUESTS);

	// Process result data
	facy.validate_response(&resp);
  
	if (resp.code != HTTP_CODE_OK) {
		facy.handle_error("friendRequests");
		return;
	}
	
	// Parse it
	std::string reqs = utils::text::source_get_value(&resp.data, 2, "class=\"mRequestItem", "al aps\">");

	std::string::size_type pos = 0;
	std::string::size_type pos2 = 0;
	bool last = false;

	while (!last && !reqs.empty()) {
		std::string req;
		if ((pos2 = reqs.find("class=\"mRequestItem", pos)) != std::string::npos) {
			req = reqs.substr(pos, pos2 - pos);
			pos = pos2 + 19;
		} else {
			req = reqs.substr(pos);
			last = true;
		}
				
		std::string get = utils::text::source_get_value(&req, 3, "<form", "action=\"", "\">");
		std::string time = utils::text::source_get_value2(&get, "seenrequesttime=", "&\"");

		facebook_user *fbu = new facebook_user();
		fbu->real_name = utils::text::source_get_value(&req, 2, "class=\"actor\">", "</");
		fbu->user_id = utils::text::source_get_value(&get, 2, "id=", "&");

		if (fbu->user_id.length() && fbu->real_name.length())
		{
			HANDLE hContact = AddToContactList(fbu, FACEBOOK_CONTACT_APPROVE, false, fbu->real_name.c_str());
			db_set_b(hContact, m_szModuleName, FACEBOOK_KEY_CONTACT_TYPE, FACEBOOK_CONTACT_APPROVE);

			bool seen = false;

			DBVARIANT dbv;
			if (!db_get_s(hContact, m_szModuleName, "RequestTime", &dbv)) {
				seen = !strcmp(dbv.pszVal, time.c_str());
				db_free(&dbv);
			}

			if (!seen) {
				// This is new request
				db_set_s(hContact, m_szModuleName, "RequestTime", time.c_str());

				//blob is: uin(DWORD), hContact(HANDLE), nick(ASCIIZ), first(ASCIIZ), last(ASCIIZ), email(ASCIIZ), reason(ASCIIZ)
				//blob is: 0(DWORD), hContact(HANDLE), nick(ASCIIZ), ""(ASCIIZ), ""(ASCIIZ), ""(ASCIIZ), ""(ASCIIZ)
				DBEVENTINFO dbei = {0};
				dbei.cbSize = sizeof(DBEVENTINFO);
				dbei.szModule = m_szModuleName;
				dbei.timestamp = ::time(NULL);
				dbei.flags = DBEF_UTF;
				dbei.eventType = EVENTTYPE_AUTHREQUEST;
				dbei.cbBlob = (DWORD)(sizeof(DWORD)*2 + fbu->real_name.length() + 5);
					
				PBYTE pCurBlob = dbei.pBlob = (PBYTE) mir_alloc(dbei.cbBlob);					
				*(PDWORD)pCurBlob = 0; pCurBlob += sizeof(DWORD);                    // UID
				*(PDWORD)pCurBlob = (DWORD)hContact; pCurBlob += sizeof(DWORD);      // Contact Handle
				strcpy((char*)pCurBlob, fbu->real_name.data()); pCurBlob += fbu->real_name.length()+1;	// Nickname
				*pCurBlob = '\0'; pCurBlob++;                                        // First Name
				*pCurBlob = '\0'; pCurBlob++;                                        // Last Name
				*pCurBlob = '\0'; pCurBlob++;                                        // E-mail
				*pCurBlob = '\0';                                                    // Reason

				db_event_add(0, &dbei);				

				LOG("      (New) Friendship request from: %s (%s) [%s]", fbu->real_name.c_str(), fbu->user_id.c_str(), time.c_str());
			} else {
				LOG("      (Old) Friendship request from: %s (%s) [%s]", fbu->real_name.c_str(), fbu->user_id.c_str(), time.c_str());
			}
		} else {
			LOG(" !!!  Wrong friendship request");
			LOG(req.c_str());
		}
	}

	facy.handle_success("friendRequests");
}

void FacebookProto::ProcessFeeds(void* data)
{
	if (data == NULL)
		return;

	std::string* resp = (std::string*)data;

	if (!isOnline())
		goto exit;

	CODE_BLOCK_TRY

	LOG("***** Starting processing feeds");

	std::vector< facebook_newsfeed* > news;

	std::string::size_type pos = 0;
	UINT limit = 0;

	*resp = utils::text::slashu_to_utf8(*resp);	
	*resp = utils::text::source_get_value(resp, 2, "\"html\":\"", ">\"");

	while ((pos = resp->find("<div class=\\\"mainWrapper\\\"", pos)) != std::string::npos && limit <= 25)
	{		
		std::string::size_type pos2 = resp->find("<div class=\\\"mainWrapper\\\"", pos+5);
		if (pos2 == std::string::npos)
			pos2 = resp->length();
		
		std::string post = resp->substr(pos, pos2 - pos);
		pos += 5;

		std::string post_header = utils::text::source_get_value(&post, 4, "<h5 class=", "uiStreamHeadline", ">", "<\\/h5>");
		std::string post_message = utils::text::source_get_value(&post, 3, "<h5 class=\"uiStreamMessage userContentWrapper", ">", "<\\/h5>");
		std::string post_link = utils::text::source_get_value(&post, 3, "<span class=\\\"uiStreamSource\\\"", ">", "<\\/span>");
		std::string post_attach = utils::text::source_get_value(&post, 4, "<div class=", "uiStreamAttachments", ">", "<form");

		// in title keep only name, end of events like "X shared link" put into message
		pos2 = post_header.find("<\\/a>") + 5;
		post_message = post_header.substr(pos2, post_header.length() - pos2) + post_message;
		post_header = post_header.substr(0, pos2);

		// append attachement to message (if any)
		post_message += utils::text::trim(post_attach);

		facebook_newsfeed* nf = new facebook_newsfeed;

		nf->title = utils::text::trim(
			utils::text::special_expressions_decode(
				utils::text::remove_html(post_header)));

		nf->user_id = utils::text::source_get_value(&post_header, 2, "user.php?id=", "\\\"");
		
		nf->link = utils::text::special_expressions_decode(
				utils::text::source_get_value(&post_link, 2, "href=\\\"", "\\\">"));

		nf->text = utils::text::trim(
			utils::text::special_expressions_decode(
				utils::text::remove_html(
					utils::text::edit_html(post_message))));

		if (!nf->title.length() || !nf->text.length())
		{
			delete nf;
			continue;
		}

		if (nf->text.length() > 500)
		{
			nf->text = nf->text.substr(0, 500);
			nf->text += "...";
		}

		news.push_back(nf);
		pos++;
		limit++;
	}

	for(std::vector<facebook_newsfeed*>::size_type i=0; i<news.size(); i++)
	{
		LOG("      Got newsfeed: %s %s", news[i]->title.c_str(), news[i]->text.c_str());
		TCHAR* szTitle = mir_a2t_cp(news[i]->title.c_str(), CP_UTF8);
		TCHAR* szText = mir_a2t_cp(news[i]->text.c_str(), CP_UTF8);
		TCHAR* szUrl = mir_a2t_cp(news[i]->link.c_str(), CP_UTF8);
		NotifyEvent(szTitle,szText,this->ContactIDToHContact(news[i]->user_id),FACEBOOK_EVENT_NEWSFEED, szUrl);
		mir_free(szTitle);
		mir_free(szText);
//		mir_free(szUrl); // URL is free'd in popup procedure
		delete news[i];
	}
	news.clear();

	this->facy.last_feeds_update_ = ::time(NULL);

	LOG("***** Feeds processed");

	CODE_BLOCK_CATCH

	LOG("***** Error processing feeds: %s", e.what());

	CODE_BLOCK_END

exit:
	delete resp;
}

void FacebookProto::SearchAckThread(void *targ)
{
	facy.handle_entry("searchAckThread");

	int count = 0;

	char *arg = mir_utf8encodeT((TCHAR*)targ);
	std::string search = utils::url::encode(arg);
	std::string ssid;

	while (count < 50 && !isOffline())
	{
		std::string get_data = search + "&s=" + utils::conversion::to_string(&count, UTILS_CONV_UNSIGNED_NUMBER);
		if (!ssid.empty())
			get_data += "&ssid=" + ssid;

		http::response resp = facy.flap(FACEBOOK_REQUEST_SEARCH, NULL, &get_data);

		// Process result data
		facy.validate_response(&resp);

		if (resp.code == HTTP_CODE_OK)
		{
			std::string items = utils::text::source_get_value(&resp.data, 2, "<body", "</body>");

			std::string::size_type pos = 0;
			std::string::size_type pos2 = 0;

			while ((pos = items.find("<td class=\"pic\">", pos)) != std::string::npos) {
				std::string item = items.substr(pos, items.find("</tr>", pos) - pos);
				pos++; count++;

				std::string id = utils::text::source_get_value2(&item, "?id=", "&\"");
				if (id.empty())
					id = utils::text::source_get_value2(&item, "?slog=", "&\"");

				std::string name = utils::text::source_get_value(&item, 4, "<td class=\"name\">", "<a", ">", "</");
				std::string surname;
				std::string nick;
				std::string common = utils::text::source_get_value(&item, 2, "<span class=\"mfss fcg\">", "</span>");

				if ((pos2 = name.find(" <span class=\"alternate_name\">")) != std::string::npos) {
					nick = name.substr(pos2 + 31, name.length() - pos2 - 32); // also remove brackets around nickname
					name = name.substr(0, pos2);
				}

				if ((pos2 = name.find(" ")) != std::string::npos) {
					surname = name.substr(pos2 + 1, name.length() - pos2 - 1);
					name = name.substr(0, pos2);
				}

				// ignore self contact and empty ids
				if (id.empty() || id == facy.self_.user_id)
					continue;

				TCHAR* tid = mir_a2t_cp(id.c_str(), CP_UTF8);
				TCHAR* tname = mir_a2t_cp(name.c_str(), CP_UTF8);
				TCHAR* tsurname = mir_a2t_cp(surname.c_str(), CP_UTF8);
				TCHAR* tnick = mir_a2t_cp(nick.c_str(), CP_UTF8);
				TCHAR* tcommon = mir_a2t_cp(common.c_str(), CP_UTF8);

				PROTOSEARCHRESULT isr = {0};
				isr.cbSize = sizeof(isr);
				isr.flags = PSR_TCHAR;
				isr.id  = tid;
				isr.nick  = tnick;
				isr.firstName = tname;
				isr.lastName = tsurname;
				isr.email = tcommon;

				ProtoBroadcastAck(m_szModuleName, NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, targ, (LPARAM)&isr);

				mir_free(tid);
				mir_free(tnick);
				mir_free(tname);
				mir_free(tsurname);
				mir_free(tcommon);
			}

			ssid = utils::text::source_get_value(&items, 3, "id=\"more_objects\"", "ssid=", "&");			
			if (ssid.empty())
				break; // No more results
		}
	}

	ProtoBroadcastAck(m_szModuleName, NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, targ, 0);

	facy.handle_success("searchAckThread");

	mir_free(targ);
	mir_free(arg);
}