forked from aglasgall/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathim.c
2298 lines (1863 loc) · 63.2 KB
/
im.c
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
995
996
997
998
999
1000
/*
* Family 0x0004 - Routines for sending/receiving Instant Messages.
*
* Note the term ICBM (Inter-Client Basic Message) which blankets
* all types of genericly routed through-server messages. Within
* the ICBM types (family 4), a channel is defined. Each channel
* represents a different type of message. Channel 1 is used for
* what would commonly be called an "instant message". Channel 2
* is used for negotiating "rendezvous". These transactions end in
* something more complex happening, such as a chat invitation, or
* a file transfer. Channel 3 is used for chat messages (not in
* the same family as these channels). Channel 4 is used for
* various ICQ messages. Examples are normal messages, URLs, and
* old-style authorization.
*
* In addition to the channel, every ICBM contains a cookie. For
* standard IMs, these are only used for error messages. However,
* the more complex rendezvous messages make suitably more complex
* use of this field.
*
* TODO: Split this up into an im.c file an an icbm.c file. It
* will be beautiful, you'll see.
*
* Need to rename all the mpmsg messages to aim_im_bleh.
*
* Make sure aim_conn_findbygroup is used by all functions.
*/
#define FAIM_INTERNAL
#include <aim.h>
#ifdef _WIN32
#include "win32dep.h"
#endif
/**
* Add a standard ICBM header to the given bstream with the given
* information.
*
* @param bs The bstream to write the ICBM header to.
* @param c c is for cookie, and cookie is for me.
* @param ch The ICBM channel (1 through 4).
* @param sn Null-terminated scrizeen nizame.
* @return The number of bytes written. It's really not useful.
*/
static int aim_im_puticbm(aim_bstream_t *bs, const fu8_t *c, fu16_t ch, const char *sn)
{
aimbs_putraw(bs, c, 8);
aimbs_put16(bs, ch);
aimbs_put8(bs, strlen(sn));
aimbs_putraw(bs, sn, strlen(sn));
return 8+2+1+strlen(sn);
}
/*
* Takes a msghdr (and a length) and returns a client type
* code. Note that this is *only a guess* and has a low likelihood
* of actually being accurate.
*
* Its based on experimental data, with the help of Eric Warmenhoven
* who seems to have collected a wide variety of different AIM clients.
*
*
* Heres the current collection:
* 0501 0003 0101 0101 01 AOL Mobile Communicator, WinAIM 1.0.414
* 0501 0003 0101 0201 01 WinAIM 2.0.847, 2.1.1187, 3.0.1464,
* 4.3.2229, 4.4.2286
* 0501 0004 0101 0102 0101 WinAIM 4.1.2010, libfaim (right here)
* 0501 0003 0101 02 WinAIM 5
* 0501 0001 01 iChat x.x, mobile buddies
* 0501 0001 0101 01 AOL v6.0, CompuServe 2000 v6.0, any TOC client
* 0501 0002 0106 WinICQ 5.45.1.3777.85
*
* Note that in this function, only the feature bytes are tested, since
* the rest will always be the same.
*
*/
faim_export fu16_t aim_im_fingerprint(const fu8_t *msghdr, int len)
{
static const struct {
fu16_t clientid;
int len;
fu8_t data[10];
} fingerprints[] = {
/* AOL Mobile Communicator, WinAIM 1.0.414 */
{ AIM_CLIENTTYPE_MC,
3, {0x01, 0x01, 0x01}},
/* WinAIM 2.0.847, 2.1.1187, 3.0.1464, 4.3.2229, 4.4.2286 */
{ AIM_CLIENTTYPE_WINAIM,
3, {0x01, 0x01, 0x02}},
/* WinAIM 4.1.2010, libfaim */
{ AIM_CLIENTTYPE_WINAIM41,
4, {0x01, 0x01, 0x01, 0x02}},
/* AOL v6.0, CompuServe 2000 v6.0, any TOC client */
{ AIM_CLIENTTYPE_AOL_TOC,
1, {0x01}},
{ 0, 0}
};
int i;
if (!msghdr || (len <= 0))
return AIM_CLIENTTYPE_UNKNOWN;
for (i = 0; fingerprints[i].len; i++) {
if (fingerprints[i].len != len)
continue;
if (memcmp(fingerprints[i].data, msghdr, fingerprints[i].len) == 0)
return fingerprints[i].clientid;
}
return AIM_CLIENTTYPE_UNKNOWN;
}
/**
* Subtype 0x0002 - Set ICBM parameters.
*
* I definitly recommend sending this. If you don't, you'll be stuck
* with the rather unreasonable defaults.
*
*/
faim_export int aim_im_setparams(aim_session_t *sess, struct aim_icbmparameters *params)
{
aim_conn_t *conn;
aim_frame_t *fr;
aim_snacid_t snacid;
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
return -EINVAL;
if (!params)
return -EINVAL;
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+16)))
return -ENOMEM;
snacid = aim_cachesnac(sess, 0x0004, 0x0002, 0x0000, NULL, 0);
aim_putsnac(&fr->data, 0x0004, 0x0002, 0x0000, snacid);
/* This is read-only (see Parameter Reply). Must be set to zero here. */
aimbs_put16(&fr->data, 0x0000);
/* These are all read-write */
aimbs_put32(&fr->data, params->flags);
aimbs_put16(&fr->data, params->maxmsglen);
aimbs_put16(&fr->data, params->maxsenderwarn);
aimbs_put16(&fr->data, params->maxrecverwarn);
aimbs_put32(&fr->data, params->minmsginterval);
aim_tx_enqueue(sess, fr);
return 0;
}
/**
* Subtype 0x0004 - Request ICBM parameter information.
*
*/
faim_export int aim_im_reqparams(aim_session_t *sess)
{
aim_conn_t *conn;
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
return -EINVAL;
return aim_genericreq_n_snacid(sess, conn, 0x0004, 0x0004);
}
/**
* Subtype 0x0005 - Receive parameter information.
*
*/
static int aim_im_paraminfo(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
{
aim_rxcallback_t userfunc;
struct aim_icbmparameters params;
params.maxchan = aimbs_get16(bs);
params.flags = aimbs_get32(bs);
params.maxmsglen = aimbs_get16(bs);
params.maxsenderwarn = aimbs_get16(bs);
params.maxrecverwarn = aimbs_get16(bs);
params.minmsginterval = aimbs_get32(bs);
if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
return userfunc(sess, rx, ¶ms);
return 0;
}
/**
* Subtype 0x0006 - Send an ICBM (instant message).
*
*
* Possible flags:
* AIM_IMFLAGS_AWAY -- Marks the message as an autoresponse
* AIM_IMFLAGS_ACK -- Requests that the server send an ack
* when the message is received (of type 0x0004/0x000c)
* AIM_IMFLAGS_OFFLINE--If destination is offline, store it until they are
* online (probably ICQ only).
* AIM_IMFLAGS_UNICODE--Instead of ASCII7, the passed message is
* made up of UNICODE duples. If you set
* this, you'd better be damn sure you know
* what you're doing.
* AIM_IMFLAGS_ISO_8859_1 -- The message contains the ASCII8 subset
* known as ISO-8859-1.
*
* Generally, you should use the lowest encoding possible to send
* your message. If you only use basic punctuation and the generic
* Latin alphabet, use ASCII7 (no flags). If you happen to use non-ASCII7
* characters, but they are all clearly defined in ISO-8859-1, then
* use that. Keep in mind that not all characters in the PC ASCII8
* character set are defined in the ISO standard. For those cases (most
* notably when the (r) symbol is used), you must use the full UNICODE
* encoding for your message. In UNICODE mode, _all_ characters must
* occupy 16bits, including ones that are not special. (Remember that
* the first 128 UNICODE symbols are equivelent to ASCII7, however they
* must be prefixed with a zero high order byte.)
*
* I strongly discourage the use of UNICODE mode, mainly because none
* of the clients I use can parse those messages (and besides that,
* wchars are difficult and non-portable to handle in most UNIX environments).
* If you really need to include special characters, use the HTML UNICODE
* entities. These are of the form ߪ where 2026 is the hex
* representation of the UNICODE index (in this case, UNICODE
* "Horizontal Ellipsis", or 133 in in ASCII8).
*
* Implementation note: Since this is one of the most-used functions
* in all of libfaim, it is written with performance in mind. As such,
* it is not as clear as it could be in respect to how this message is
* supposed to be layed out. Most obviously, tlvlists should be used
* instead of writing out the bytes manually.
*
* XXX - more precise verification that we never send SNACs larger than 8192
* XXX - check SNAC size for multipart
*
*/
faim_export int aim_im_sendch1_ext(aim_session_t *sess, struct aim_sendimext_args *args)
{
aim_conn_t *conn;
aim_frame_t *fr;
aim_snacid_t snacid;
fu8_t ck[8];
int i, msgtlvlen;
static const fu8_t deffeatures[] = { 0x01, 0x01, 0x01, 0x02 };
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
return -EINVAL;
if (!args)
return -EINVAL;
if (args->flags & AIM_IMFLAGS_MULTIPART) {
if (args->mpmsg->numparts <= 0)
return -EINVAL;
} else {
if (!args->msg || (args->msglen <= 0))
return -EINVAL;
if (args->msglen >= MAXMSGLEN)
return -E2BIG;
}
/* Painfully calculate the size of the message TLV */
msgtlvlen = 1 + 1; /* 0501 */
if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES)
msgtlvlen += 2 + args->featureslen;
else
msgtlvlen += 2 + sizeof(deffeatures);
if (args->flags & AIM_IMFLAGS_MULTIPART) {
aim_mpmsg_section_t *sec;
for (sec = args->mpmsg->parts; sec; sec = sec->next) {
msgtlvlen += 2 /* 0101 */ + 2 /* block len */;
msgtlvlen += 4 /* charset */ + sec->datalen;
}
} else {
msgtlvlen += 2 /* 0101 */ + 2 /* block len */;
msgtlvlen += 4 /* charset */ + args->msglen;
}
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, msgtlvlen+128)))
return -ENOMEM;
/* XXX - should be optional */
snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, args->destsn, strlen(args->destsn)+1);
aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
/*
* Generate a random message cookie
*
* We could cache these like we do SNAC IDs. (In fact, it
* might be a good idea.) In the message error functions,
* the 8byte message cookie is returned as well as the
* SNAC ID.
*
*/
for (i = 0; i < 8; i++)
ck[i] = (fu8_t)rand();
/* ICBM header */
aim_im_puticbm(&fr->data, ck, 0x0001, args->destsn);
/* Message TLV (type 0x0002) */
aimbs_put16(&fr->data, 0x0002);
aimbs_put16(&fr->data, msgtlvlen);
/* Features TLV (type 0x0501) */
aimbs_put16(&fr->data, 0x0501);
if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES) {
aimbs_put16(&fr->data, args->featureslen);
aimbs_putraw(&fr->data, args->features, args->featureslen);
} else {
aimbs_put16(&fr->data, sizeof(deffeatures));
aimbs_putraw(&fr->data, deffeatures, sizeof(deffeatures));
}
if (args->flags & AIM_IMFLAGS_MULTIPART) {
aim_mpmsg_section_t *sec;
/* Insert each message part in a TLV (type 0x0101) */
for (sec = args->mpmsg->parts; sec; sec = sec->next) {
aimbs_put16(&fr->data, 0x0101);
aimbs_put16(&fr->data, sec->datalen + 4);
aimbs_put16(&fr->data, sec->charset);
aimbs_put16(&fr->data, sec->charsubset);
aimbs_putraw(&fr->data, sec->data, sec->datalen);
}
} else {
/* Insert message text in a TLV (type 0x0101) */
aimbs_put16(&fr->data, 0x0101);
/* Message block length */
aimbs_put16(&fr->data, args->msglen + 0x04);
/* Character set */
aimbs_put16(&fr->data, args->charset);
aimbs_put16(&fr->data, args->charsubset);
/* Message. Not terminated */
aimbs_putraw(&fr->data, args->msg, args->msglen);
}
/* Set the Autoresponse flag */
if (args->flags & AIM_IMFLAGS_AWAY) {
aimbs_put16(&fr->data, 0x0004);
aimbs_put16(&fr->data, 0x0000);
} else if (args->flags & AIM_IMFLAGS_ACK) {
/* Set the Request Acknowledge flag */
aimbs_put16(&fr->data, 0x0003);
aimbs_put16(&fr->data, 0x0000);
}
if (args->flags & AIM_IMFLAGS_OFFLINE) {
aimbs_put16(&fr->data, 0x0006);
aimbs_put16(&fr->data, 0x0000);
}
/*
* Set the I HAVE A REALLY PURTY ICON flag.
* XXX - This should really only be sent on initial
* IMs and when you change your icon.
*/
if (args->flags & AIM_IMFLAGS_HASICON) {
aimbs_put16(&fr->data, 0x0008);
aimbs_put16(&fr->data, 0x000c);
aimbs_put32(&fr->data, args->iconlen);
aimbs_put16(&fr->data, 0x0001);
aimbs_put16(&fr->data, args->iconsum);
aimbs_put32(&fr->data, args->iconstamp);
}
/*
* Set the Buddy Icon Requested flag.
* XXX - Everytime? Surely not...
*/
if (args->flags & AIM_IMFLAGS_BUDDYREQ) {
aimbs_put16(&fr->data, 0x0009);
aimbs_put16(&fr->data, 0x0000);
}
aim_tx_enqueue(sess, fr);
/* clean out SNACs over 60sec old */
aim_cleansnacs(sess, 60);
return 0;
}
/*
* Simple wrapper for aim_im_sendch1_ext()
*
* You cannot use aim_send_im if you need the HASICON flag. You must
* use aim_im_sendch1_ext directly for that.
*
* aim_send_im also cannot be used if you require UNICODE messages, because
* that requires an explicit message length. Use aim_im_sendch1_ext().
*
*/
faim_export int aim_im_sendch1(aim_session_t *sess, const char *sn, fu16_t flags, const char *msg)
{
struct aim_sendimext_args args;
args.destsn = sn;
args.flags = flags;
args.msg = msg;
args.msglen = strlen(msg);
args.charset = 0x0000;
args.charsubset = 0x0000;
/* Make these don't get set by accident -- they need aim_im_sendch1_ext */
args.flags &= ~(AIM_IMFLAGS_CUSTOMFEATURES | AIM_IMFLAGS_HASICON | AIM_IMFLAGS_MULTIPART);
return aim_im_sendch1_ext(sess, &args);
}
/**
* Subtype 0x0006 - Send your icon to a given user.
*
* This is also performance sensitive. (If you can believe it...)
*
*/
faim_export int aim_im_sendch2_icon(aim_session_t *sess, const char *sn, const fu8_t *icon, int iconlen, time_t stamp, fu16_t iconsum)
{
aim_conn_t *conn;
aim_frame_t *fr;
aim_snacid_t snacid;
fu8_t ck[8];
int i;
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
return -EINVAL;
if (!sn || !icon || (iconlen <= 0) || (iconlen >= MAXICONLEN))
return -EINVAL;
for (i = 0; i < 8; i++)
ck[i] = (fu8_t)rand();
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sn)+2+2+2+8+16+2+2+2+2+2+2+2+4+4+4+iconlen+strlen(AIM_ICONIDENT)+2+2)))
return -ENOMEM;
snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
/* ICBM header */
aim_im_puticbm(&fr->data, ck, 0x0002, sn);
/*
* TLV t(0005)
*
* Encompasses everything below.
*/
aimbs_put16(&fr->data, 0x0005);
aimbs_put16(&fr->data, 2+8+16+6+4+4+iconlen+4+4+4+strlen(AIM_ICONIDENT));
aimbs_put16(&fr->data, 0x0000);
aimbs_putraw(&fr->data, ck, 8);
aim_putcap(&fr->data, AIM_CAPS_BUDDYICON);
/* TLV t(000a) */
aimbs_put16(&fr->data, 0x000a);
aimbs_put16(&fr->data, 0x0002);
aimbs_put16(&fr->data, 0x0001);
/* TLV t(000f) */
aimbs_put16(&fr->data, 0x000f);
aimbs_put16(&fr->data, 0x0000);
/* TLV t(2711) */
aimbs_put16(&fr->data, 0x2711);
aimbs_put16(&fr->data, 4+4+4+iconlen+strlen(AIM_ICONIDENT));
aimbs_put16(&fr->data, 0x0000);
aimbs_put16(&fr->data, iconsum);
aimbs_put32(&fr->data, iconlen);
aimbs_put32(&fr->data, stamp);
aimbs_putraw(&fr->data, icon, iconlen);
aimbs_putraw(&fr->data, AIM_ICONIDENT, strlen(AIM_ICONIDENT));
/* TLV t(0003) */
aimbs_put16(&fr->data, 0x0003);
aimbs_put16(&fr->data, 0x0000);
aim_tx_enqueue(sess, fr);
return 0;
}
/*
* Subtype 0x0006 - Send a rich text message.
*
* This only works for ICQ 2001b (thats 2001 not 2000). Better, only
* send it to clients advertising the RTF capability. In fact, if you send
* it to a client that doesn't support that capability, the server will gladly
* bounce it back to you.
*
* You'd think this would be in icq.c, but, well, I'm trying to stick with
* the one-group-per-file scheme as much as possible. This could easily
* be an exception, since Rendezvous IMs are external of the Oscar core,
* and therefore are undefined. Really I just need to think of a good way to
* make an interface similar to what AOL actually uses. But I'm not using COM.
*
*/
faim_export int aim_im_sendch2_rtfmsg(aim_session_t *sess, struct aim_sendrtfmsg_args *args)
{
aim_conn_t *conn;
aim_frame_t *fr;
aim_snacid_t snacid;
fu8_t ck[8];
const char rtfcap[] = {"{97B12751-243C-4334-AD22-D6ABF73F1492}"}; /* AIM_CAPS_ICQRTF capability in string form */
int i, servdatalen;
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
return -EINVAL;
if (!args || !args->destsn || !args->rtfmsg)
return -EINVAL;
servdatalen = 2+2+16+2+4+1+2 + 2+2+4+4+4 + 2+4+2+strlen(args->rtfmsg)+1 + 4+4+4+strlen(rtfcap)+1;
for (i = 0; i < 8; i++)
ck[i] = (fu8_t)rand();
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+128+servdatalen)))
return -ENOMEM;
snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
/* ICBM header */
aim_im_puticbm(&fr->data, ck, 0x0002, args->destsn);
/* TLV t(0005) - Encompasses everything below. */
aimbs_put16(&fr->data, 0x0005);
aimbs_put16(&fr->data, 2+8+16 + 2+2+2 + 2+2 + 2+2+servdatalen);
aimbs_put16(&fr->data, 0x0000);
aimbs_putraw(&fr->data, ck, 8);
aim_putcap(&fr->data, AIM_CAPS_ICQSERVERRELAY);
/* t(000a) l(0002) v(0001) */
aimbs_put16(&fr->data, 0x000a);
aimbs_put16(&fr->data, 0x0002);
aimbs_put16(&fr->data, 0x0001);
/* t(000f) l(0000) v() */
aimbs_put16(&fr->data, 0x000f);
aimbs_put16(&fr->data, 0x0000);
/* Service Data TLV */
aimbs_put16(&fr->data, 0x2711);
aimbs_put16(&fr->data, servdatalen);
aimbs_putle16(&fr->data, 11 + 16 /* 11 + (sizeof CLSID) */);
aimbs_putle16(&fr->data, 9);
aim_putcap(&fr->data, AIM_CAPS_EMPTY);
aimbs_putle16(&fr->data, 0);
aimbs_putle32(&fr->data, 0);
aimbs_putle8(&fr->data, 0);
aimbs_putle16(&fr->data, 0x03ea); /* trid1 */
aimbs_putle16(&fr->data, 14);
aimbs_putle16(&fr->data, 0x03eb); /* trid2 */
aimbs_putle32(&fr->data, 0);
aimbs_putle32(&fr->data, 0);
aimbs_putle32(&fr->data, 0);
aimbs_putle16(&fr->data, 0x0001);
aimbs_putle32(&fr->data, 0);
aimbs_putle16(&fr->data, strlen(args->rtfmsg)+1);
aimbs_putraw(&fr->data, args->rtfmsg, strlen(args->rtfmsg)+1);
aimbs_putle32(&fr->data, args->fgcolor);
aimbs_putle32(&fr->data, args->bgcolor);
aimbs_putle32(&fr->data, strlen(rtfcap)+1);
aimbs_putraw(&fr->data, rtfcap, strlen(rtfcap)+1);
aim_tx_enqueue(sess, fr);
return 0;
}
/**
* Subtype 0x0006 - Send an "I want to directly connect to you" message
*
*/
faim_export int aim_im_sendch2_odcrequest(aim_session_t *sess, fu8_t *cookie, const char *sn, const fu8_t *ip, fu16_t port)
{
aim_conn_t *conn;
aim_frame_t *fr;
aim_snacid_t snacid;
fu8_t ck[8];
aim_tlvlist_t *tl = NULL, *itl = NULL;
int hdrlen, i;
fu8_t *hdr;
aim_bstream_t hdrbs;
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
return -EINVAL;
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 256+strlen(sn))))
return -ENOMEM;
snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
/*
* Generate a random message cookie
*
* This cookie needs to be alphanumeric and NULL-terminated to be
* TOC-compatible.
*
* XXX - have I mentioned these should be generated in msgcookie.c?
*
*/
for (i = 0; i < 7; i++)
ck[i] = 0x30 + ((fu8_t) rand() % 10);
ck[7] = '\0';
if (cookie)
memcpy(cookie, ck, 8);
/* ICBM header */
aim_im_puticbm(&fr->data, ck, 0x0002, sn);
aim_tlvlist_add_noval(&tl, 0x0003);
hdrlen = 2+8+16+6+8+6+4;
hdr = malloc(hdrlen);
aim_bstream_init(&hdrbs, hdr, hdrlen);
aimbs_put16(&hdrbs, 0x0000);
aimbs_putraw(&hdrbs, ck, 8);
aim_putcap(&hdrbs, AIM_CAPS_DIRECTIM);
aim_tlvlist_add_16(&itl, 0x000a, 0x0001);
aim_tlvlist_add_raw(&itl, 0x0003, 4, ip);
aim_tlvlist_add_16(&itl, 0x0005, port);
aim_tlvlist_add_noval(&itl, 0x000f);
aim_tlvlist_write(&hdrbs, &itl);
aim_tlvlist_add_raw(&tl, 0x0005, aim_bstream_curpos(&hdrbs), hdr);
aim_tlvlist_write(&fr->data, &tl);
free(hdr);
aim_tlvlist_free(&itl);
aim_tlvlist_free(&tl);
aim_tx_enqueue(sess, fr);
return 0;
}
/**
* Subtype 0x0006 - Send an "I want to send you this file" message
*
*/
faim_export int aim_im_sendch2_sendfile_ask(aim_session_t *sess, struct aim_oft_info *oft_info)
{
aim_conn_t *conn;
aim_frame_t *fr;
aim_snacid_t snacid;
aim_tlvlist_t *tl=NULL, *subtl=NULL;
int i;
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)) || !oft_info)
return -EINVAL;
/* XXX - Should be like "21CBF95" and null terminated */
for (i = 0; i < 7; i++)
oft_info->cookie[i] = 0x30 + ((fu8_t)rand() % 10);
oft_info->cookie[7] = '\0';
{ /* Create the subTLV chain */
fu8_t *buf;
int buflen;
aim_bstream_t bs;
aim_tlvlist_add_16(&subtl, 0x000a, 0x0001);
aim_tlvlist_add_noval(&subtl, 0x000f);
/* aim_tlvlist_add_raw(&subtl, 0x000e, 2, "en");
aim_tlvlist_add_raw(&subtl, 0x000d, 8, "us-ascii");
aim_tlvlist_add_raw(&subtl, 0x000c, 24, "Please accept this file."); */
if (oft_info->clientip) {
fu8_t ip[4];
char *nexttoken;
int i = 0;
nexttoken = strtok(oft_info->clientip, ".");
while (nexttoken && i<4) {
ip[i] = atoi(nexttoken);
nexttoken = strtok(NULL, ".");
i++;
}
aim_tlvlist_add_raw(&subtl, 0x0003, 4, ip);
}
aim_tlvlist_add_16(&subtl, 0x0005, oft_info->port);
/* TLV t(2711) */
buflen = 2+2+4+strlen(oft_info->fh.name)+1;
buf = malloc(buflen);
aim_bstream_init(&bs, buf, buflen);
aimbs_put16(&bs, (oft_info->fh.totfiles > 1) ? 0x0002 : 0x0001);
aimbs_put16(&bs, oft_info->fh.totfiles);
aimbs_put32(&bs, oft_info->fh.totsize);
/* Filename - NULL terminated, for some odd reason */
aimbs_putraw(&bs, oft_info->fh.name, strlen(oft_info->fh.name));
aimbs_put8(&bs, 0x00);
aim_tlvlist_add_raw(&subtl, 0x2711, bs.len, bs.data);
free(buf);
}
{ /* Create the main TLV chain */
fu8_t *buf;
int buflen;
aim_bstream_t bs;
/* TLV t(0005) - Encompasses everything from above. Gee. */
buflen = 2+8+16+aim_tlvlist_size(&subtl);
buf = malloc(buflen);
aim_bstream_init(&bs, buf, buflen);
aimbs_put16(&bs, AIM_RENDEZVOUS_PROPOSE);
aimbs_putraw(&bs, oft_info->cookie, 8);
aim_putcap(&bs, AIM_CAPS_SENDFILE);
aim_tlvlist_write(&bs, &subtl);
aim_tlvlist_free(&subtl);
aim_tlvlist_add_raw(&tl, 0x0005, bs.len, bs.data);
free(buf);
/* TLV t(0003) - Request an ack */
aim_tlvlist_add_noval(&tl, 0x0003);
}
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10 + 11+strlen(oft_info->sn) + aim_tlvlist_size(&tl))))
return -ENOMEM;
snacid = aim_cachesnac(sess, 0x0004, 0x0006, AIM_SNACFLAGS_DESTRUCTOR, oft_info->cookie, sizeof(oft_info->cookie));
aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
/* ICBM header */
aim_im_puticbm(&fr->data, oft_info->cookie, 0x0002, oft_info->sn);
/* All that crap from above (the 0x0005 TLV and the 0x0003 TLV) */
aim_tlvlist_write(&fr->data, &tl);
aim_tlvlist_free(&tl);
aim_tx_enqueue(sess, fr);
return 0;
}
/**
* Subtype 0x0006 - Send an "I will accept this file" message?
*
* @param rendid Capability type (AIM_CAPS_GETFILE or AIM_CAPS_SENDFILE)
*/
faim_export int aim_im_sendch2_sendfile_accept(aim_session_t *sess, struct aim_oft_info *oft_info)
{
aim_conn_t *conn;
aim_frame_t *fr;
aim_snacid_t snacid;
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)) || !oft_info)
return -EINVAL;
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10 + 11+strlen(oft_info->sn) + 4+2+8+16)))
return -ENOMEM;
snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
/* ICBM header */
aim_im_puticbm(&fr->data, oft_info->cookie, 0x0002, oft_info->sn);
aimbs_put16(&fr->data, 0x0005);
aimbs_put16(&fr->data, 0x001a);
aimbs_put16(&fr->data, AIM_RENDEZVOUS_ACCEPT);
aimbs_putraw(&fr->data, oft_info->cookie, 8);
aim_putcap(&fr->data, AIM_CAPS_SENDFILE);
aim_tx_enqueue(sess, fr);
return 0;
}
/**
* Subtype 0x0006 - Send a "cancel this file transfer" message?
*
*/
faim_export int aim_im_sendch2_sendfile_cancel(aim_session_t *sess, struct aim_oft_info *oft_info)
{
aim_conn_t *conn;
aim_frame_t *fr;
aim_snacid_t snacid;
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)) || !oft_info)
return -EINVAL;
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10 + 11+strlen(oft_info->sn) + 4+2+8+16)))
return -ENOMEM;
snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
/* ICBM header */
aim_im_puticbm(&fr->data, oft_info->cookie, 0x0002, oft_info->sn);
aimbs_put16(&fr->data, 0x0005);
aimbs_put16(&fr->data, 0x001a);
aimbs_put16(&fr->data, AIM_RENDEZVOUS_CANCEL);
aimbs_putraw(&fr->data, oft_info->cookie, 8);
aim_putcap(&fr->data, AIM_CAPS_SENDFILE);
aim_tx_enqueue(sess, fr);
return 0;
}
/**
* Subtype 0x0006 - Request the status message of the given ICQ user.
*
* @param sess The oscar session.
* @param sn The UIN of the user of whom you wish to request info.
* @param type The type of info you wish to request. This should be the current
* state of the user, as one of the AIM_ICQ_STATE_* defines.
* @return Return 0 if no errors, otherwise return the error number.
*/
faim_export int aim_im_sendch2_geticqaway(aim_session_t *sess, const char *sn, int type)
{
aim_conn_t *conn;
aim_frame_t *fr;
aim_snacid_t snacid;
int i;
fu8_t ck[8];
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)) || !sn)
return -EINVAL;
for (i = 0; i < 8; i++)
ck[i] = (fu8_t)rand();
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sn) + 4+0x5e + 4)))
return -ENOMEM;
snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
/* ICBM header */
aim_im_puticbm(&fr->data, ck, 0x0002, sn);
/* TLV t(0005) - Encompasses almost everything below. */
aimbs_put16(&fr->data, 0x0005); /* T */
aimbs_put16(&fr->data, 0x005e); /* L */
{ /* V */
aimbs_put16(&fr->data, 0x0000);
/* Cookie */
aimbs_putraw(&fr->data, ck, 8);
/* Put the 16 byte server relay capability */
aim_putcap(&fr->data, AIM_CAPS_ICQSERVERRELAY);
/* TLV t(000a) */
aimbs_put16(&fr->data, 0x000a);
aimbs_put16(&fr->data, 0x0002);
aimbs_put16(&fr->data, 0x0001);
/* TLV t(000f) */
aimbs_put16(&fr->data, 0x000f);
aimbs_put16(&fr->data, 0x0000);
/* TLV t(2711) */
aimbs_put16(&fr->data, 0x2711);
aimbs_put16(&fr->data, 0x0036);
{ /* V */
aimbs_putle16(&fr->data, 0x001b); /* L */
aimbs_putle16(&fr->data, 0x0008); /* XXX - Protocol version */
aim_putcap(&fr->data, AIM_CAPS_EMPTY);
aimbs_putle16(&fr->data, 0x0000); /* Unknown */
aimbs_putle16(&fr->data, 0x0003); /* Client features? */
aimbs_putle16(&fr->data, 0x0000); /* Unknown */
aimbs_putle8(&fr->data, 0x00); /* Unkizown */
aimbs_putle16(&fr->data, 0xffff); /* Sequence number? XXX - This should decrement by 1 with each request */
aimbs_putle16(&fr->data, 0x000e); /* L */
aimbs_putle16(&fr->data, 0xffff); /* Sequence number? XXX - This should decrement by 1 with each request */
aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
/* The type of status message being requested */
if (type & AIM_ICQ_STATE_CHAT)
aimbs_putle16(&fr->data, 0x03ec);
else if(type & AIM_ICQ_STATE_DND)
aimbs_putle16(&fr->data, 0x03eb);
else if(type & AIM_ICQ_STATE_OUT)
aimbs_putle16(&fr->data, 0x03ea);
else if(type & AIM_ICQ_STATE_BUSY)
aimbs_putle16(&fr->data, 0x03e9);
else if(type & AIM_ICQ_STATE_AWAY)
aimbs_putle16(&fr->data, 0x03e8);
aimbs_putle16(&fr->data, 0x0000); /* Status? */
aimbs_putle16(&fr->data, 0x0001); /* Priority of this message? */
aimbs_putle16(&fr->data, 0x0001); /* L */
aimbs_putle8(&fr->data, 0x00); /* String of length L */
} /* End TLV t(2711) */
} /* End TLV t(0005) */
/* TLV t(0003) */
aimbs_put16(&fr->data, 0x0003);
aimbs_put16(&fr->data, 0x0000);
aim_tx_enqueue(sess, fr);
return 0;
}
/**
* Subtype 0x0006 - Send an ICQ-esque ICBM.
*
* This can be used to send an ICQ authorization reply (deny or grant). It is the "old way."
* The new way is to use SSI. I like the new way a lot better. This seems like such a hack,
* mostly because it's in network byte order. Figuring this stuff out sometimes takes a while,
* but thats ok, because it gives me time to try to figure out what kind of drugs the AOL people
* were taking when they merged the two protocols.
*
* @param sn The destination screen name.
* @param type The type of message. 0x0007 for authorization denied. 0x0008 for authorization granted.
* @param message The message you want to send, it should be null terminated.
* @return Return 0 if no errors, otherwise return the error number.
*/
faim_export int aim_im_sendch4(aim_session_t *sess, char *sn, fu16_t type, fu8_t *message)
{
aim_conn_t *conn;
aim_frame_t *fr;
aim_snacid_t snacid;
int i;
char ck[8];
if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0002)))
return -EINVAL;
if (!sn || !type || !message)
return -EINVAL;
if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+3+strlen(sn)+12+strlen(message)+1+4)))
return -ENOMEM;
snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
/* Cookie */
for (i=0; i<8; i++)
ck[i] = (fu8_t)rand();
/* ICBM header */
aim_im_puticbm(&fr->data, ck, 0x0004, sn);
/*
* TLV t(0005)
*
* ICQ data (the UIN and the message).
*/
aimbs_put16(&fr->data, 0x0005);
aimbs_put16(&fr->data, 4 + 2+2+strlen(message)+1);
/*
* Your UIN
*/
aimbs_putle32(&fr->data, atoi(sess->sn));
/*
* TLV t(type) l(strlen(message)+1) v(message+NULL)
*/
aimbs_putle16(&fr->data, type);
aimbs_putle16(&fr->data, strlen(message)+1);
aimbs_putraw(&fr->data, message, strlen(message)+1);
/*
* TLV t(0006) l(0000) v()
*/
aimbs_put16(&fr->data, 0x0006);
aimbs_put16(&fr->data, 0x0000);
aim_tx_enqueue(sess, fr);
return 0;
}