summaryrefslogtreecommitdiff
path: root/protocols/AimOscar/src/client.cpp
blob: 5c4421ac117e918f089943a2c5b90b94a6ba0515 (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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/*
Plugin of Miranda IM for communicating with users of the AIM protocol.
Copyright (c) 2008-2012 Boris Krasnovskiy
Copyright (C) 2005-2006 Aaron Myles Landwehr

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 "aim.h"
#include "version.h"

int CAimProto::aim_send_connection_packet(HANDLE hServerConn,unsigned short &seqno,char *buf)
{
	return aim_sendflap(hServerConn,0x01,4,buf,seqno);
}

int CAimProto::aim_authkey_request(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char* buf=(char*)alloca(SNAC_SIZE+TLV_HEADER_SIZE*3+strlen(username));
	aim_writesnac(0x17,0x06,offset,buf);
	aim_writetlv(0x01,(unsigned short)strlen(username),username,offset,buf);
	aim_writetlv(0x4B,0,0,offset,buf);
	aim_writetlv(0x5A,0,0,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_auth_request(HANDLE hServerConn,unsigned short &seqno,const char* key,const char* language,
								const char* country, const char* username, const char* password)
{
	unsigned short offset=0;
	mir_md5_byte_t pass_hash[16];
	mir_md5_byte_t auth_hash[16];
	mir_md5_state_t state;
	
	mir_md5_init(&state);
	mir_md5_append(&state,(const mir_md5_byte_t *)password, (int)strlen(password));
	mir_md5_finish(&state,pass_hash);
	mir_md5_init(&state);
	mir_md5_append(&state,(mir_md5_byte_t*)key, (int)strlen(key));
	mir_md5_append(&state,(mir_md5_byte_t*)pass_hash,MD5_HASH_LENGTH);
	mir_md5_append(&state,(mir_md5_byte_t*)AIM_MD5_STRING, sizeof(AIM_MD5_STRING)-1);
	mir_md5_finish(&state,auth_hash);

	char client_id[64], mirver[64];
	CallService(MS_SYSTEM_GETVERSIONTEXT, sizeof(mirver), (LPARAM)mirver);
	int client_id_len = mir_snprintf(client_id, sizeof(client_id), "Miranda AIM, version %s", mirver);

	char* buf=(char*)alloca(SNAC_SIZE+TLV_HEADER_SIZE*14+MD5_HASH_LENGTH+strlen(username)+client_id_len+30+strlen(language)+strlen(country));

	aim_writesnac(0x17,0x02,offset,buf);
	aim_writetlv(0x01,(unsigned short)strlen(username),username,offset,buf);
	aim_writetlv(0x25,MD5_HASH_LENGTH,(char*)auth_hash,offset,buf);
	aim_writetlv(0x4C,0,0,offset,buf);//signifies new password hash instead of old method
	aim_writetlv(0x03,(unsigned short)client_id_len,client_id,offset,buf);
	aim_writetlvshort(0x17,AIM_CLIENT_MAJOR_VERSION,offset,buf);
	aim_writetlvshort(0x18,AIM_CLIENT_MINOR_VERSION,offset,buf);
	aim_writetlvshort(0x19,AIM_CLIENT_LESSER_VERSION,offset,buf);
	aim_writetlvshort(0x1A,AIM_CLIENT_BUILD_NUMBER,offset,buf);
	aim_writetlvshort(0x16,AIM_CLIENT_ID_NUMBER,offset,buf);
	aim_writetlvlong(0x14,AIM_CLIENT_DISTRIBUTION_NUMBER,offset,buf);
	aim_writetlv(0x0F,(unsigned short)strlen(language),language,offset,buf);
	aim_writetlv(0x0E,(unsigned short)strlen(country),country,offset,buf);
	aim_writetlvchar(0x4A,getByte(AIM_KEY_FSC, 0) ? 3 : 1,offset,buf);
//    aim_writetlvchar(0x94,0,offset,buf);
	if (!getByte(AIM_KEY_DSSL, 0))
		aim_writetlv(0x8c,0,NULL,offset,buf);                       // Request SSL connection
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_send_cookie(HANDLE hServerConn,unsigned short &seqno,int cookie_size,char * cookie)
{
	unsigned short offset=0;
	char* buf=(char*)alloca(TLV_HEADER_SIZE*2+cookie_size);
	aim_writelong(0x01,offset,buf);//protocol version number
	aim_writetlv(0x06,(unsigned short)cookie_size,cookie,offset,buf);
	return aim_sendflap(hServerConn,0x01,offset,buf,seqno);
}

int CAimProto::aim_send_service_request(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE*12];
	aim_writesnac(0x01,0x17,offset,buf);
	aim_writefamily(AIM_SERVICE_GENERIC,offset,buf);
	aim_writefamily(AIM_SERVICE_SSI,offset,buf);
	aim_writefamily(AIM_SERVICE_LOCATION,offset,buf);
	aim_writefamily(AIM_SERVICE_BUDDYLIST,offset,buf);
	aim_writefamily(AIM_SERVICE_MESSAGING,offset,buf);
	aim_writefamily(AIM_SERVICE_ICQ,offset,buf);
	aim_writefamily(AIM_SERVICE_INVITATION,offset,buf);
	aim_writefamily(AIM_SERVICE_POPUP,offset,buf);
	aim_writefamily(AIM_SERVICE_BOS,offset,buf);
	aim_writefamily(AIM_SERVICE_USERLOOKUP,offset,buf);
	aim_writefamily(AIM_SERVICE_STATS,offset,buf);
	aim_writefamily(AIM_SERVICE_UNKNOWN,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_new_service_request(HANDLE hServerConn,unsigned short &seqno,unsigned short service)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+2+TLV_HEADER_SIZE];
	aim_writesnac(0x01,0x04,offset,buf);
	aim_writeshort(service,offset,buf);
	if (!getByte(AIM_KEY_DSSL, 0))
		aim_writetlv(0x8c,0,NULL,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_request_rates(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE];
	aim_writesnac(0x01,0x06,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_accept_rates(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE*2];
	aim_writesnac(0x01,0x08,offset,buf);
	aim_writegeneric(10,AIM_SERVICE_RATES,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_request_icbm(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE];
	aim_writesnac(0x04,0x04,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_set_icbm(HANDLE hServerConn,unsigned short &seqno)
{
	const unsigned icbm_flags = ICBM_CHANNEL_MSGS_ALLOWED | ICBM_MISSED_CALLS_ENABLED | 
		ICBM_EVENTS_ALLOWED | ICBM_SMS_SUPPORTED | ICBM_OFFLINE_MSGS_ALLOWED;
//	const unsigned icbm_flags = 0x3db;

	unsigned short offset=0;
	char buf[SNAC_SIZE+16];
	aim_writesnac(0x04,0x02,offset,buf);
	aim_writeshort(0,offset,buf);           //channel
	aim_writelong(icbm_flags,offset,buf);   //flags
	aim_writeshort(0x1f40,offset,buf);      //max snac size              8000
	aim_writeshort(0x03e7,offset,buf);      //max sender warning level   999 (0-1000) WinAim default
	aim_writeshort(0x03e7,offset,buf);      //max receiver warning level 999 (0-1000) WinAim default
	aim_writelong(0,offset,buf);            //min message interval, ms   0
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_request_offline_msgs(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE];
	aim_writesnac(0x04,0x10,offset,buf); // Subtype for offline messages 0x10
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_request_list(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE];
	aim_writesnac(0x13,0x04,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_activate_list(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE];
	aim_writesnac(0x13,0x07,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

/*
0000   00 05 00 02 00 17 00 06 00 03 00 00 00 00 07 00  ................
0010   01 00 00 08 00 01 01 00 0a 00 14 00 02 00 08 66  ...............f
0020   61 63 65 62 6f 6f 6b 00 06 67 6f 6f 67 6c 65     acebook..google

int CAimProto::aim_request_rights(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+50];
	aim_writesnac(0x03,0x02,offset,buf);
	aim_writetlvshort(0x05,0x17,offset,buf);
	aim_writetlv(0x06,3,"\x0\x0",offset,buf);
	aim_writetlvchar(0x07,0x01,offset,buf);
	aim_writetlvshort(0x05,0x17,offset,buf);
	aim_writetlvshort(0x05,0x17,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}
*/
int CAimProto::aim_set_caps(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	int i=1;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE*3+AIM_CAPS_LENGTH*50+sizeof(AIM_MSG_TYPE)];
	char temp[AIM_CAPS_LENGTH*20];
	memcpy(temp,AIM_CAP_SHORT_CAPS,AIM_CAPS_LENGTH);
	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_HOST_STATUS_TEXT_AWARE,AIM_CAPS_LENGTH);
	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_SMART_CAPS,AIM_CAPS_LENGTH);
	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_FILE_TRANSFER,AIM_CAPS_LENGTH);
//	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_HAS_MICROPHONE,AIM_CAPS_LENGTH);
//	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_RTCAUDIO,AIM_CAPS_LENGTH);
//	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_HAS_CAMERA,AIM_CAPS_LENGTH);
//	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_RTCVIDEO,AIM_CAPS_LENGTH);
	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_BUDDY_ICON,AIM_CAPS_LENGTH);
	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_CHAT,AIM_CAPS_LENGTH);
	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_SUPPORT_ICQ,AIM_CAPS_LENGTH);
//	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_ICQ_SERVER_RELAY,AIM_CAPS_LENGTH);
	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_UTF8,AIM_CAPS_LENGTH);
	memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_MIRANDA,AIM_CAPS_LENGTH);
	if(getByte(AIM_KEY_HF, 0))
		memcpy(&temp[AIM_CAPS_LENGTH*i++],AIM_CAP_HIPTOP,AIM_CAPS_LENGTH);
	aim_writesnac(0x02,0x04,offset,buf);
	aim_writetlv(0x05,(unsigned short)(AIM_CAPS_LENGTH*i),temp,offset,buf);

	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_set_profile(HANDLE hServerConn,unsigned short &seqno, char* amsg)//user info
{
	aimString str(amsg);
	const char *charset = str.isUnicode() ? AIM_MSG_TYPE_UNICODE : AIM_MSG_TYPE;
	const unsigned short charset_len = (unsigned short)strlen(charset);

	const char* msg = str.getBuf();
	const unsigned short msg_len = str.getSize();

	char* buf=(char*)alloca(SNAC_SIZE+TLV_HEADER_SIZE*3+1+charset_len+msg_len);
	unsigned short offset=0;

	aim_writesnac(0x02,0x04,offset,buf);
	aim_writetlvchar(0x0c,1,offset,buf);
	aim_writetlv(0x01,charset_len,charset,offset,buf);
	aim_writetlv(0x02,msg_len,msg,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_client_ready(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset = 0;

	NETLIBCONNINFO connInfo = { sizeof(connInfo) }; 
	CallService(MS_NETLIB_GETCONNECTIONINFO, (WPARAM)hServerConn, (LPARAM)&connInfo);
	
	internal_ip = connInfo.dwIpv4;

	char buf[SNAC_SIZE+TLV_HEADER_SIZE*22];
	aim_writesnac(0x01,0x02,offset,buf);
	aim_writefamily(AIM_SERVICE_GENERIC,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_SSI,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_LOCATION,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_BUDDYLIST,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_MESSAGING,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_ICQ,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_INVITATION,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	//removed extra generic server 
	aim_writefamily(AIM_SERVICE_POPUP,offset,buf);
	aim_writegeneric(4,"\x01\x04\0\x01",offset,buf);//different version number like trillian 3.1
	aim_writefamily(AIM_SERVICE_BOS,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_USERLOOKUP,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_STATS,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_mail_ready(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE*4];
	aim_writesnac(0x01,0x02,offset,buf);
	aim_writefamily(AIM_SERVICE_GENERIC,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_MAIL,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_avatar_ready(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE*4];
	aim_writesnac(0x01,0x02,offset,buf);
	aim_writefamily(AIM_SERVICE_GENERIC,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_AVATAR,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_chatnav_ready(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE*4];
	aim_writesnac(0x01,0x02,offset,buf);
	aim_writefamily(AIM_SERVICE_GENERIC,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_CHATNAV,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_chat_ready(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE*4];
	aim_writesnac(0x01,0x02,offset,buf);
	aim_writefamily(AIM_SERVICE_GENERIC,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_CHAT,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_send_message(HANDLE hServerConn,unsigned short &seqno,const char* sn,char* amsg,bool auto_response, bool blast)
{	
	aimString str(amsg);

	const char* msg = str.getBuf();
	const unsigned short msg_len = str.getSize();

	unsigned short tlv_offset=0;
	char* tlv_buf=(char*)alloca(5+msg_len+8);

	char icbm_cookie[8];
	CallService(MS_UTILS_GETRANDOM, 8, (LPARAM)icbm_cookie);
 
	aim_writegeneric(5,"\x05\x01\x00\x01\x01",tlv_offset,tlv_buf);   // icbm im capabilities
	aim_writeshort(0x0101,tlv_offset,tlv_buf);                       // icbm im text tag
	aim_writeshort(msg_len+4,tlv_offset,tlv_buf);                    // icbm im text tag length
	aim_writeshort(str.isUnicode()?2:0,tlv_offset,tlv_buf);          // character set
	aim_writeshort(0,tlv_offset,tlv_buf);                            // language

	aim_writegeneric(msg_len,msg,tlv_offset,tlv_buf);                // message text
	
	unsigned short offset=0;
	unsigned short sn_length=(unsigned short)strlen(sn);
	char* buf= (char*)alloca(SNAC_SIZE+8+3+sn_length+TLV_HEADER_SIZE*3+tlv_offset);
	
	aim_writesnac(0x04,0x06,offset,buf,get_random());
	aim_writegeneric(8,icbm_cookie,offset,buf);                      // icbm cookie
	aim_writeshort(0x01,offset,buf);                                 // channel
	aim_writechar((unsigned char)sn_length,offset,buf);              // screen name len
	aim_writegeneric(sn_length,sn,offset,buf);                       // screen name

	aim_writetlv(0x02,tlv_offset,tlv_buf,offset,buf);

	if (!blast)
	{
		if(auto_response)
			aim_writetlv(0x04,0,0,offset,buf);                       // auto-response message
		else
		{
			aim_writetlv(0x03,0,0,offset,buf);                       // message ack request
			aim_writetlv(0x06,0,0,offset,buf);                       // offline message storage
		}
	}
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno) ? 0 : *(int*)icbm_cookie & 0x7fffffff;
}

int CAimProto::aim_query_profile(HANDLE hServerConn,unsigned short &seqno,char* sn)
{
	unsigned short offset=0;
	unsigned short sn_length=(unsigned short)strlen(sn);
	char* buf=(char*)alloca(SNAC_SIZE+5+sn_length);
	aim_writesnac(0x02,0x15,offset,buf);
	aim_writelong(0x01,offset,buf);
	aim_writechar((unsigned char)sn_length,offset,buf);
	aim_writegeneric(sn_length,sn,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno)==0;
}

int CAimProto::aim_delete_contact(HANDLE hServerConn, unsigned short &seqno, char* sn, unsigned short item_id,
								  unsigned short group_id, unsigned short list, bool nil)
{
	unsigned short offset=0;
	unsigned short sn_length=(unsigned short)strlen(sn);
	char* buf=(char*)alloca(SNAC_SIZE+sn_length+10);
	aim_writesnac(0x13,0x0a,offset,buf, get_random());              // SSI Delete
	aim_writeshort(sn_length,offset,buf);                           // screen name length
	aim_writegeneric(sn_length,sn,offset,buf);                      // screen name
	aim_writeshort(group_id,offset,buf);                            // group id
	aim_writeshort(item_id,offset,buf);                             // buddy id
	aim_writeshort(list,offset,buf);                                // buddy type
	aim_writeshort(nil?4:0,offset,buf);                             // length of extra data
	if (nil) aim_writetlv(0x6a,0,NULL,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_add_contact(HANDLE hServerConn, unsigned short &seqno, const char* sn, unsigned short item_id,
							   unsigned short group_id, unsigned short list, char* nick, char* note)
{
	unsigned short offset=0;
	unsigned short sn_length=(unsigned short)strlen(sn);
	unsigned short nick_length = (unsigned short)_strlens(nick);
	unsigned short note_length = (unsigned short)_strlens(note);
	unsigned short tlv_len = nick || note ? TLV_HEADER_SIZE * 2 + nick_length + note_length : 0;

	char* buf=(char*)alloca(SNAC_SIZE + sn_length + 10 + tlv_len);
	aim_writesnac(0x13,0x08,offset,buf, get_random());               // SSI Add
	aim_writeshort(sn_length,offset,buf);                            // screen name length
	aim_writegeneric(sn_length,sn,offset,buf);                       // screen name
	aim_writeshort(group_id,offset,buf);                             // group id
	aim_writeshort(item_id,offset,buf);                              // buddy id
	aim_writeshort(list,offset, buf);                                // buddy type
	aim_writeshort(tlv_len,offset,buf);                              // length of extra data

	if (nick || note)
	{
		aim_writetlv(0x13c,note_length,note,offset,buf);
		aim_writetlv(0x131,nick_length,nick,offset,buf);
	}
	return aim_sendflap(hServerConn, 0x02, offset, buf, seqno);
}

int CAimProto::aim_mod_group(HANDLE hServerConn, unsigned short &seqno, const char* name, unsigned short group_id,
							 char* members, unsigned short members_length)
{
	unsigned short offset=0;
	unsigned short name_length=(unsigned short)strlen(name);
	char* buf=(char*)alloca(SNAC_SIZE+TLV_HEADER_SIZE+name_length+members_length+10);
	aim_writesnac(0x13,0x09,offset,buf, get_random());              // SSI Edit
	aim_writeshort(name_length,offset,buf);                         // group name length
	aim_writegeneric(name_length,name,offset,buf);                  // group name 
	aim_writeshort(group_id,offset,buf);                            // group id
	aim_writeshort(0,offset,buf);                                   // buddy id
	aim_writeshort(1,offset,buf);                                   // buddy type: Group
	aim_writeshort(TLV_HEADER_SIZE+members_length,offset,buf);      // length of extra data 
	aim_writetlv(0xc8,members_length,members,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_mod_buddy(HANDLE hServerConn, unsigned short &seqno, const char* sn, 
							 unsigned short buddy_id, unsigned short group_id,
							 char* nick, char* note)
{
	unsigned short offset=0;
	unsigned short sn_length = (unsigned short)strlen(sn);
	unsigned short nick_length = (unsigned short)_strlens(nick);
	unsigned short note_length = (unsigned short)_strlens(note);
	unsigned short tlv_len = TLV_HEADER_SIZE * 2 + nick_length + note_length;
	
	char* buf=(char*)alloca(SNAC_SIZE+sn_length+10+tlv_len);
	aim_writesnac(0x13,0x09,offset,buf, get_random());              // SSI Edit
	aim_writeshort(sn_length,offset,buf);                           // screen name length
	aim_writegeneric(sn_length,sn,offset,buf);                      // screen name 
	aim_writeshort(buddy_id,offset,buf);                            // buddy id
	aim_writeshort(group_id,offset,buf);                            // group id
	aim_writeshort(0,offset,buf);                                   // buddy type: Buddy
	aim_writeshort(tlv_len,offset,buf);                             // length of extra data 

	aim_writetlv(0x13c,note_length,note,offset,buf);
	aim_writetlv(0x131,nick_length,nick,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_set_pd_info(HANDLE hServerConn, unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE*3+20];
	unsigned short req = 0x09;
	if (pd_info_id == 0)
	{
		pd_info_id = get_random();
		req = 0x08;
	}
	aim_writesnac(0x13,req,offset,buf, get_random());               // SSI Edit/Add
	aim_writeshort(0,offset,buf);                                   // name length
	aim_writeshort(0,offset,buf);                                   // group id (root)
	aim_writeshort(pd_info_id,offset,buf);                          // buddy id
	aim_writeshort(0x4,offset,buf);                                 // pd info id
	aim_writeshort(0x15,offset,buf);                                // size
	aim_writetlvchar(0xca,pd_mode,offset,buf);                      // pd mode
	aim_writetlvlong(0xcb,0xffffffff,offset,buf);                   // pd mask
	aim_writetlvlong(0xcc,pd_flags,offset,buf);                     // pd flags
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_ssi_update_preferences(HANDLE hServerConn, unsigned short &seqno)
{
	unsigned short offset = 0;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE*4+100];
	unsigned short req = 0x09;
	if (pref1_id == 0)
	{
		pref1_id = get_random();
		req = 0x08;
	}
	aim_writesnac(0x13,req,offset,buf, get_random());               // SSI Edit/Add
	aim_writeshort(0,offset,buf);									// group name length
	aim_writeshort(0,offset,buf);									// group id (root)
	aim_writeshort(pref1_id,offset,buf);                            // buddy id
	aim_writeshort(5,offset,buf);                                   // buddy type: Presence

	unsigned short tlv_len = TLV_HEADER_SIZE * 2 + 8;
	if (pref2_len) tlv_len += TLV_HEADER_SIZE + pref2_len;
	if (pref2_set_len) tlv_len += TLV_HEADER_SIZE + pref2_set_len;

	aim_writeshort(tlv_len,offset,buf);								// length of extra data
	aim_writetlvlong(0xc9,pref1_flags,offset,buf);					// Update Buddy preferences 1
	aim_writetlvlong(0xd6,pref1_set_flags,offset,buf);				// Update Buddy preferences 1
	if (pref2_len) 
		aim_writetlv(0xd7,pref2_len,pref2_flags,offset,buf);		// Update Buddy preferences 2
	if (pref2_set_len) 
		aim_writetlv(0xd8,pref2_set_len,pref2_set_flags,offset,buf);// Update Buddy preferences 2
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_ssi_update(HANDLE hServerConn, unsigned short &seqno, bool start)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE];
	aim_writesnac(0x13,start?0x11:0x12,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno)==0;
}

int CAimProto::aim_keepalive(HANDLE hServerConn,unsigned short &seqno)
{
	return aim_sendflap(hServerConn,0x05,4,"\x0\x0\x0\xEE",seqno);
}

// used when requesting a regular file transfer
int CAimProto::aim_send_file(HANDLE hServerConn, unsigned short &seqno,
							 unsigned long ip, unsigned short port, 
							 bool force_proxy, file_transfer *ft)
{	
	char msg_frag[2048];
	unsigned short frag_offset=0;

	aim_writeshort(0,frag_offset,msg_frag);                             // request type
	aim_writegeneric(8,ft->icbm_cookie,frag_offset,msg_frag);           // icbm cookie
	aim_writegeneric(AIM_CAPS_LENGTH, AIM_CAP_FILE_TRANSFER,
		frag_offset, msg_frag);                                         // uuid
	aim_writetlvshort(0x0a,++ft->req_num,frag_offset,msg_frag);         // request number
//    aim_writetlvshort(0x12,2,frag_offset,msg_frag);                     // max protocol version

	aim_writetlvlong(0x02,ip,frag_offset,msg_frag);                     // ip
	aim_writetlvlong(0x16,~ip,frag_offset,msg_frag);                    // ip check

	aim_writetlvshort(0x05,port,frag_offset,msg_frag);                  // port
	aim_writetlvshort(0x17,~port,frag_offset,msg_frag);                 // port ip check
	
	if (force_proxy)
		aim_writetlv(0x10,0,0,frag_offset,msg_frag);                    // request proxy transfer
	else
		aim_writetlvlong(0x03,internal_ip,frag_offset,msg_frag);        // ip

	if (ft->req_num == 1)
	{
		if (ft->message)
		{
			aimString dscr(ft->message);

			const char* charset = dscr.isUnicode() ? "unicode-2-0" : "us-ascii";
			const unsigned short charset_len = (unsigned short)strlen(charset);

			const char* desc_msg = dscr.getBuf();
			const unsigned short desc_len = dscr.getSize();

			aim_writetlv(0x0e,2,"en",frag_offset,msg_frag);             // language used by the data
			aim_writetlv(0x0d,charset_len,charset,frag_offset,msg_frag);// charset used by the data
			aim_writetlv(0x0c,desc_len,desc_msg,frag_offset,msg_frag);  // invitaion text
		}

		aim_writetlv(0x0f,0,0,frag_offset,msg_frag);                    // request host check

		const char* fname = get_fname(ft->file);
		const unsigned short fnlen = (unsigned short)strlen(fname);

		char* fblock = (char*)alloca(9 + fnlen);
		*(unsigned short*)&fblock[0] = _htons(ft->pfts.totalFiles > 1 ? 2 : 1);     // single file transfer
		*(unsigned short*)&fblock[2] = _htons(ft->pfts.totalFiles);     // number of files
		*(unsigned long*) &fblock[4] = _htonl(ft->pfts.totalBytes);     // total bytes in files
		memcpy(&fblock[8], fname, fnlen + 1);

		const char* enc = is_utf(fname) ? "utf-8" : "us-ascii";
		aim_writetlv(0x2711,9+fnlen,fblock,frag_offset,msg_frag);               // extra data, file names, size
		aim_writetlv(0x2712,8,enc,frag_offset,msg_frag);                        // character set used by data
//        aim_writetlvlong64(0x2713,ft->pfts.totalBytes,frag_offset,msg_frag);    // file length

		LOG("Attempting to Send a file to a buddy.");
	}
	else
	{
		aim_writetlvshort(0x14,0x0a,frag_offset,msg_frag);              // Counter proposal reason
	}

	unsigned short offset=0;
	unsigned short sn_length=(unsigned short)strlen(ft->sn);
	char* buf=(char*)alloca(SNAC_SIZE+TLV_HEADER_SIZE*2+12+frag_offset+sn_length);
	aim_writesnac(0x04,0x06,offset,buf);                                // msg to host
	aim_writegeneric(8,ft->icbm_cookie,offset,buf);                     // icbm cookie
	aim_writeshort(2,offset,buf);                                       // icbm channel
	aim_writechar((unsigned char)sn_length,offset,buf);                 // screen name length
	aim_writegeneric(sn_length,ft->sn,offset,buf);                      // screen name
	aim_writetlv(0x05,frag_offset,msg_frag,offset,buf);                 // icbm tags
	aim_writetlv(0x03,0,0,offset,buf);                                  // request ack

	char cip[20];
	long_ip_to_char_ip(ip, cip);
	LOG("IP for Buddy to connect to: %s:%u", cip, port);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno)==0;
}


int CAimProto::aim_file_ad(HANDLE hServerConn,unsigned short &seqno,char* sn, char* icbm_cookie, bool deny, unsigned short max_ver)
{	
	unsigned short frag_offset=0;
	char msg_frag[10+AIM_CAPS_LENGTH+TLV_HEADER_SIZE*2+6];
	aim_writeshort(deny ? 1 : 2,frag_offset,msg_frag);              // icbm accept / deny
	aim_writegeneric(8,icbm_cookie,frag_offset,msg_frag);           // icbm cookie
	aim_writegeneric(AIM_CAPS_LENGTH,
		AIM_CAP_FILE_TRANSFER,frag_offset,msg_frag);                // uuid

//    if (max_ver > 1)
//        aim_writetlvshort(0x12,2,frag_offset,msg_frag);             // max protocol version

	unsigned short sn_length=(unsigned short)strlen(sn);
	unsigned short offset=0;
	char* buf=(char*)alloca(SNAC_SIZE+TLV_HEADER_SIZE+21+frag_offset+sn_length);
	aim_writesnac(0x04,0x06,offset,buf);                            // msg to host
	aim_writegeneric(8,icbm_cookie,offset,buf);                     // icbm cookie
	aim_writeshort(2,offset,buf);                                   // icbm channel
	aim_writechar((unsigned char)sn_length,offset,buf);             // screen name length
	aim_writegeneric(sn_length,sn,offset,buf);                      // screen name
	aim_writetlv(0x05,frag_offset,msg_frag,offset,buf);             // icbm tags

	LOG("%s a file transfer.", deny ? "Denying" : "Accepting");
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno)==0;
}

int CAimProto::aim_typing_notification(HANDLE hServerConn,unsigned short &seqno,char* sn,unsigned short type)
{
	unsigned short offset=0;
	unsigned short sn_length=(unsigned short)strlen(sn);
	char* buf= (char*)alloca(SNAC_SIZE+sn_length+13);
	aim_writesnac(0x04,0x14,offset,buf);
	aim_writegeneric(8,"\0\0\0\0\0\0\0\0",offset,buf);              // icbm cookie
	aim_writeshort(1,offset,buf);                                   // icbm channel
	aim_writechar((unsigned char)sn_length,offset,buf);             // screen name length
	aim_writegeneric(sn_length,sn,offset,buf);                      // screen name
	aim_writeshort(type,offset,buf);                                // typing event
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_set_idle(HANDLE hServerConn,unsigned short &seqno,unsigned long seconds)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+4];
	aim_writesnac(0x01,0x11,offset,buf);
	aim_writelong(seconds,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_request_mail(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+34];
	aim_writesnac(0x18,0x06,offset,buf);
	aim_writegeneric(34,
		"\x00\x02"
		"\xb3\x80\x9a\xd8\x0d\xba\x11\xd5\x9f\x8a\x00\x60\xb0\xee\x06\x31"
		"\x5d\x5e\x17\x08\x55\xaa\x11\xd3\xb1\x43\x00\x60\xb0\xfb\x1e\xcb",
		offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_activate_mail(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+17];
	aim_writesnac(0x18,0x16,offset,buf);
	aim_writegeneric(17,"\x02\x04\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00",offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_request_avatar(HANDLE hServerConn,unsigned short &seqno, const char* sn, unsigned short bart_type, const char* hash, unsigned short hash_size)
{
	unsigned short offset=0;
	unsigned char sn_length=(unsigned char)strlen(sn);
	char* buf= (char*)alloca(SNAC_SIZE+sn_length+hash_size+12);
	aim_writesnac(0x10,0x06,offset,buf);
	aim_writechar(sn_length,offset,buf);                            // screen name length
	aim_writegeneric(sn_length,sn,offset,buf);                      // screen name
	aim_writechar(1,offset,buf);                                    // number of BART ID
	aim_writebartid(bart_type,0,hash_size,hash,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_set_avatar_hash(HANDLE hServerConn, unsigned short &seqno, char flags, unsigned short bart_type, unsigned short &id, char size, const char* hash)
{
	unsigned short offset=0;
	
	char bart_type_txt[8];
	ultoa(bart_type, bart_type_txt, 10);
	unsigned short bart_type_len = (unsigned short)strlen(bart_type_txt);

	unsigned short req = 0x09;
	if (id == 0)
	{
		id = get_random();
		req = 0x08;
	}

	char* buf = (char*)alloca(SNAC_SIZE + TLV_HEADER_SIZE * 2 + 20 + size + bart_type_len);
	aim_writesnac(0x13,req,offset,buf, get_random());               // SSI Edit/Add
	aim_writeshort(bart_type_len,offset,buf);                       // name length
	aim_writegeneric(bart_type_len,bart_type_txt,offset,buf);       // name 
	aim_writeshort(0,offset,buf);                                   // group id
	aim_writeshort(id,offset,buf);                                  // buddy id
	aim_writeshort(0x14,offset,buf);                                // buddy type: Buddy Icon
	aim_writeshort(2+size+TLV_HEADER_SIZE,offset,buf);              // length of extra data

	char* buf2 = (char*)alloca(2+size);
	buf2[0] = flags;
	buf2[1] = (char)size;
	memcpy(&buf2[2], hash, size);
	aim_writetlv(0xd5, 2+size, buf2, offset, buf);                  // BART

	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_delete_avatar_hash(HANDLE hServerConn, unsigned short &seqno, char flags, unsigned short bart_type, unsigned short & id)
{
	unsigned short offset=0;

	if (id == 0) return -1;
	id = 0;
	
	char bart_type_txt[8];
	ultoa(bart_type, bart_type_txt, 10);
	unsigned short bart_type_len = (unsigned short)strlen(bart_type_txt);

	char* buf = (char*)alloca(SNAC_SIZE + 20 + bart_type_len);
	aim_writesnac(0x13,0x0a,offset,buf, get_random());              // SSI Delete
	aim_writeshort(bart_type_len,offset,buf);                       // name length
	aim_writegeneric(bart_type_len,bart_type_txt,offset,buf);       // name 
	aim_writeshort(0,offset,buf);                                   // group id
	aim_writeshort(id,offset,buf);                                  // buddy id
	aim_writeshort(0x14,offset,buf);                                // buddy type: Buddy Icon
	aim_writeshort(0,offset,buf);                                   // length of extra data

	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_upload_avatar(HANDLE hServerConn, unsigned short &seqno, unsigned short bart_type, const char* avatar, unsigned short avatar_size)
{
	unsigned short offset=0;
	char* buf=(char*)alloca(SNAC_SIZE+22+avatar_size);
	aim_writesnac(0x10,0x02,offset,buf);
	aim_writeshort(bart_type,offset,buf);                                   // BART id
	aim_writeshort(avatar_size,offset,buf);
	aim_writegeneric(avatar_size,avatar,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_search_by_email(HANDLE hServerConn,unsigned short &seqno, const char* email)
{
	unsigned short offset=0;
	char em_length=(char)strlen(email);
	char* buf= (char*)alloca(SNAC_SIZE+em_length);
	aim_writesnac(0x0a,0x02,offset,buf);	                        // Email search
	aim_writegeneric(em_length,email,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_chatnav_request_limits(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE];
	aim_writesnac(0x0d,0x02,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno) ? -1 : 0;
}

int CAimProto::aim_chatnav_create(HANDLE hServerConn,unsigned short &seqno, char* room, unsigned short exchage)
{
	//* Join Pseudo Room (Get's the info we need for the real connection)
	unsigned short room_len = (unsigned short)strlen(room);

	unsigned short offset=0;
	char* buf=(char*)alloca(SNAC_SIZE+10+room_len+26);
	aim_writesnac(0x0d,0x08,offset,buf);
	aim_writeshort(exchage,offset,buf);           				    // Exchange
	aim_writechar(6,offset,buf);		        				    // Command Length
	aim_writegeneric(6,"create",offset,buf);					    // Command
	aim_writeshort(0xffff,offset,buf);      					    // Last Instance
	aim_writechar(1,offset,buf);        						    // Detail
	aim_writeshort(3,offset,buf);          				            // Number of TLVs
	aim_writetlv(0xd3,room_len,room,offset,buf);                    // Room Name
	aim_writetlv(0xd6,8,"us-ascii",offset,buf);		                // Character Set
	aim_writetlv(0xd7,2,"en",offset,buf);			                // Language Encoding

	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_chatnav_room_info(HANDLE hServerConn,unsigned short &seqno, char* chat_cookie, unsigned short exchange, unsigned short instance) 
{ 
	unsigned short offset=0; 
	unsigned short chat_cookie_len = (unsigned short)strlen(chat_cookie); 
	char* buf=(char*)alloca(SNAC_SIZE+7+chat_cookie_len); 
	aim_writesnac(0x0d,0x04,offset,buf); 
	aim_writeshort(exchange,offset,buf);                            // Exchange 
	aim_writechar((unsigned char)chat_cookie_len,offset,buf);       // Chat Cookie Length 
	aim_writegeneric(chat_cookie_len,chat_cookie,offset,buf);       // Chat Cookie 
	aim_writeshort(instance,offset,buf);                            // Last Instance 
	aim_writechar(1,offset,buf);                                    // Detail 
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno); 
}  

int CAimProto::aim_chat_join_room(HANDLE hServerConn,unsigned short &seqno, char* chat_cookie, 
								  unsigned short exchange, unsigned short instance, unsigned short id)
{
	unsigned short offset=0;
	unsigned short cookie_len = (unsigned short)strlen(chat_cookie);
	char* buf=(char*)alloca(SNAC_SIZE+TLV_HEADER_SIZE*2+cookie_len+8);
	aim_writesnac(0x01,0x04,offset,buf,id);
	aim_writeshort(0x0e,offset,buf);	        			        // Service request for Chat

	aim_writeshort(0x01,offset,buf);						        // Tag
	aim_writeshort(cookie_len+5,offset,buf);				        // Length
	aim_writeshort(exchange,offset,buf);					        // Value - Exchange
	aim_writechar((unsigned char)cookie_len,offset,buf);	        // Value - Cookie Length
	aim_writegeneric(cookie_len,chat_cookie,offset,buf);	        // Value - Cookie
	aim_writeshort(instance,offset,buf);					        // Value - Instance

	if (!getByte(AIM_KEY_DSSL, 0))
		aim_writetlv(0x8c,0,NULL,offset,buf);                       // Request SSL connection

	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_chat_send_message(HANDLE hServerConn, unsigned short &seqno, char *amsg)
{
	aimString str(amsg);

	const char* charset = str.isUnicode() ? "unicode-2-0" : "us-ascii";
	const unsigned short chrset_len = (unsigned short)strlen(charset);

	const char* msg = str.getBuf();
	const unsigned short msg_len = str.getSize();

	unsigned short tlv_offset=0;
	char* tlv_buf=(char*)alloca(TLV_HEADER_SIZE*4+chrset_len+msg_len+20);
	aim_writetlv(0x04,13,"text/x-aolrtf",tlv_offset,tlv_buf);   // Format
	aim_writetlv(0x02,chrset_len,charset,tlv_offset,tlv_buf);   // Character Set
	aim_writetlv(0x03,2,"en",tlv_offset,tlv_buf);			    // Language Encoding
	aim_writetlv(0x01,msg_len,msg,tlv_offset,tlv_buf);			// Message

	unsigned short offset=0;
	char* buf=(char*)alloca(SNAC_SIZE+8+2+TLV_HEADER_SIZE*3+tlv_offset);
	aim_writesnac(0x0e,0x05,offset,buf);
	aim_writegeneric(8,"\0\0\0\0\0\0\0\0",offset,buf);			// Message Cookie (can be random)
	aim_writeshort(0x03,offset,buf);							// Message Channel (Always 3 for chat)
	aim_writetlv(0x01,0,NULL,offset,buf);						// Public/Whisper flag
	aim_writetlv(0x06,0,NULL,offset,buf);						// Enable Reflection flag
	aim_writetlv(0x05,tlv_offset,tlv_buf,offset,buf);			// Message Information TLV

	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_chat_invite(HANDLE hServerConn,unsigned short &seqno, char* chat_cookie, unsigned short exchange, unsigned short instance, char* sn, char* msg)
{
	unsigned short offset=0;
	unsigned short chat_cookie_len = (unsigned short)strlen(chat_cookie);
	unsigned short sn_len = (unsigned short)strlen(sn);
	unsigned short msg_len = (unsigned short)strlen(msg);
	char* buf=(char*)alloca(SNAC_SIZE+64+chat_cookie_len+sn_len+msg_len);
	aim_writesnac(0x04,0x06,offset,buf);
	aim_writegeneric(8,"\0\0\0\0\0\0\0\0",offset,buf);		    // ICBM Cookie
	aim_writeshort(2,offset,buf);				                // ICBM Channel
	aim_writechar((unsigned char)sn_len,offset,buf);		    // Screen Name Length
	aim_writegeneric(sn_len,sn,offset,buf);					    // Screen Name

	aim_writeshort(0x05,offset,buf);						    // Rendezvous Message Data TLV
	aim_writeshort(49+msg_len+chat_cookie_len,offset,buf);	    // TLV size
	
	aim_writeshort(0,offset,buf);							    // Message Type (0) - Request
	aim_writegeneric(8,"\0\0\0\0\0\0\0\0",offset,buf);		    // ICBM Cookie (same as above)
	aim_writegeneric(16,AIM_CAP_CHAT,offset,buf);			    // Capability

	aim_writetlvshort(0x0a,1,offset,buf);				        // Sequence Number TLV
	aim_writetlv(0x0f,0,NULL,offset,buf);					    // Request Host Caps Check TLV
	aim_writetlv(0x0c,msg_len,msg,offset,buf);				    // Invitation Message TLV

	aim_writeshort(0x2711,offset,buf);							// Capability TLV
	aim_writeshort(chat_cookie_len+5,offset,buf);				// Length
	aim_writeshort(exchange,offset,buf);						// Value - Exchange
	aim_writechar((unsigned char)chat_cookie_len,offset,buf);	// Value - Cookie Length
	aim_writegeneric(chat_cookie_len,chat_cookie,offset,buf);	// Value - Cookie
	aim_writeshort(instance,offset,buf);						// Value - Instance

	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_chat_deny(HANDLE hServerConn,unsigned short &seqno,char* sn,char* icbm_cookie)
{	
	unsigned short offset=0;
	unsigned short sn_length=(unsigned short)strlen(sn);
	char* buf=(char*)alloca(SNAC_SIZE+20+sn_length);
	aim_writesnac(0x04,0x0b,offset,buf);
	aim_writegeneric(8,icbm_cookie,offset,buf);             // ICBM Cookie
	aim_writeshort(2,offset,buf);                           // Channel
	aim_writechar((unsigned char)sn_length,offset,buf);     // Screen Name length
	aim_writegeneric(sn_length,sn,offset,buf);              // Screen Name
	aim_writeshort(3,offset,buf);                           // Error code
	aim_writeshort(2,offset,buf);                           // Error code
	aim_writeshort(1,offset,buf);                           // Error code
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno)==0;
}

int CAimProto::aim_admin_ready(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE*4];
	aim_writesnac(0x01,0x02,offset,buf);
	aim_writefamily(AIM_SERVICE_GENERIC,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	aim_writefamily(AIM_SERVICE_ADMIN,offset,buf);
	aim_writegeneric(4,AIM_TOOL_VERSION,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_admin_format_name(HANDLE hServerConn,unsigned short &seqno, const char* sn)
{
	unsigned short offset=0;
	unsigned short sn_len = (unsigned short)strlen(sn);
	char* buf=(char*)alloca(SNAC_SIZE+TLV_HEADER_SIZE+sn_len);
	aim_writesnac(0x07,0x04,offset,buf);
	aim_writetlv(0x01,sn_len,sn,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_admin_change_email(HANDLE hServerConn,unsigned short &seqno, const char* email)
{
	unsigned short offset=0;
	unsigned short email_len = (unsigned short)strlen(email);
	char* buf=(char*)alloca(SNAC_SIZE+TLV_HEADER_SIZE+email_len);
	aim_writesnac(0x07,0x04,offset,buf);
	aim_writetlv(0x11,email_len,email,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_admin_change_password(HANDLE hServerConn,unsigned short &seqno, const char* cur_pw, const char* new_pw)
{
	unsigned short offset=0;
	unsigned short cur_pw_len = (unsigned short)strlen(cur_pw);
	unsigned short new_pw_len = (unsigned short)strlen(new_pw);
	char* buf=(char*)alloca(SNAC_SIZE+2*TLV_HEADER_SIZE+cur_pw_len+new_pw_len);
	aim_writesnac(0x07,0x04,offset,buf);
	aim_writetlv(0x02,new_pw_len,new_pw,offset,buf);
	aim_writetlv(0x12,cur_pw_len,cur_pw,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_admin_request_info(HANDLE hServerConn,unsigned short &seqno, const unsigned short &type)
{
	// types: 0x01 - nickname, 0x11 - email info, 0x13 - registration status
	unsigned short offset=0;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE];
	aim_writesnac(0x07,0x02,offset,buf);
	aim_writetlv(type,0,NULL,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}

int CAimProto::aim_admin_account_confirm(HANDLE hServerConn,unsigned short &seqno)
{
	unsigned short offset=0;
	char buf[SNAC_SIZE+TLV_HEADER_SIZE];
	aim_writesnac(0x07,0x06,offset,buf);
	return aim_sendflap(hServerConn,0x02,offset,buf,seqno);
}