]> andersk Git - libfaim.git/blob - src/im.c
c656557bc2a00f94cc3c148a81731338f2d80a3e
[libfaim.git] / src / im.c
1 /*
2  *  aim_im.c
3  *
4  *  The routines for sending/receiving Instant Messages.
5  *
6  *  Note the term ICBM (Inter-Client Basic Message) which blankets
7  *  all types of genericly routed through-server messages.  Within
8  *  the ICBM types (family 4), a channel is defined.  Each channel
9  *  represents a different type of message.  Channel 1 is used for
10  *  what would commonly be called an "instant message".  Channel 2
11  *  is used for negotiating "rendezvous".  These transactions end in
12  *  something more complex happening, such as a chat invitation, or
13  *  a file transfer.
14  *
15  *  In addition to the channel, every ICBM contains a cookie.  For
16  *  standard IMs, these are only used for error messages.  However,
17  *  the more complex rendezvous messages make suitably more complex
18  *  use of this field.
19  *
20  */
21
22 #define FAIM_INTERNAL
23 #include <aim.h>
24
25 /*
26  * Takes a msghdr (and a length) and returns a client type
27  * code.  Note that this is *only a guess* and has a low likelihood
28  * of actually being accurate.
29  *
30  * Its based on experimental data, with the help of Eric Warmenhoven
31  * who seems to have collected a wide variety of different AIM clients.
32  *
33  *
34  * Heres the current collection:
35  *  0501 0003 0101 0101 01       AOL Mobile Communicator, WinAIM 1.0.414
36  *  0501 0003 0101 0201 01       WinAIM 2.0.847, 2.1.1187, 3.0.1464, 
37  *                                      4.3.2229, 4.4.2286
38  *  0501 0004 0101 0102 0101     WinAIM 4.1.2010, libfaim (right here)
39  *  0501 0001 0101 01            AOL v6.0, CompuServe 2000 v6.0, any
40  *                                      TOC client
41  *
42  * Note that in this function, only the feature bytes are tested, since
43  * the rest will always be the same.
44  *
45  */
46 faim_export fu16_t aim_fingerprintclient(fu8_t *msghdr, int len)
47 {
48         static const struct {
49                 fu16_t clientid;
50                 int len;
51                 fu8_t data[10];
52         } fingerprints[] = {
53                 /* AOL Mobile Communicator, WinAIM 1.0.414 */
54                 { AIM_CLIENTTYPE_MC, 
55                   3, {0x01, 0x01, 0x01}},
56
57                 /* WinAIM 2.0.847, 2.1.1187, 3.0.1464, 4.3.2229, 4.4.2286 */
58                 { AIM_CLIENTTYPE_WINAIM, 
59                   3, {0x01, 0x01, 0x02}},
60
61                 /* WinAIM 4.1.2010, libfaim */
62                 { AIM_CLIENTTYPE_WINAIM41,
63                   4, {0x01, 0x01, 0x01, 0x02}},
64
65                 /* AOL v6.0, CompuServe 2000 v6.0, any TOC client */
66                 { AIM_CLIENTTYPE_AOL_TOC,
67                   1, {0x01}},
68
69                 { 0, 0}
70         };
71         int i;
72
73         if (!msghdr || (len <= 0))
74                 return AIM_CLIENTTYPE_UNKNOWN;
75
76         for (i = 0; fingerprints[i].len; i++) {
77                 if (fingerprints[i].len != len)
78                         continue;
79                 if (memcmp(fingerprints[i].data, msghdr, fingerprints[i].len) == 0)
80                         return fingerprints[i].clientid;
81         }
82
83         return AIM_CLIENTTYPE_UNKNOWN;
84 }
85
86 /* This should be endian-safe now... but who knows... */
87 faim_export fu32_t aim_iconsum(const fu8_t *buf, int buflen)
88 {
89         fu32_t sum;
90         int i;
91
92         for (i = 0, sum = 0; i < buflen; i += 2)
93                 sum += (buf[i+1] << 8) + buf[i];
94
95         sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff);
96
97         return sum;
98 }
99
100 /*
101  * Send an ICBM (instant message).  
102  *
103  *
104  * Possible flags:
105  *   AIM_IMFLAGS_AWAY  -- Marks the message as an autoresponse
106  *   AIM_IMFLAGS_ACK   -- Requests that the server send an ack
107  *                        when the message is received (of type 0x0004/0x000c)
108  *   AIM_IMFLAGS_UNICODE--Instead of ASCII7, the passed message is
109  *                        made up of UNICODE duples.  If you set
110  *                        this, you'd better be damn sure you know
111  *                        what you're doing.
112  *   AIM_IMFLAGS_ISO_8859_1 -- The message contains the ASCII8 subset
113  *                        known as ISO-8859-1.  
114  *
115  * Generally, you should use the lowest encoding possible to send
116  * your message.  If you only use basic punctuation and the generic
117  * Latin alphabet, use ASCII7 (no flags).  If you happen to use non-ASCII7
118  * characters, but they are all clearly defined in ISO-8859-1, then 
119  * use that.  Keep in mind that not all characters in the PC ASCII8
120  * character set are defined in the ISO standard. For those cases (most
121  * notably when the (r) symbol is used), you must use the full UNICODE
122  * encoding for your message.  In UNICODE mode, _all_ characters must
123  * occupy 16bits, including ones that are not special.  (Remember that
124  * the first 128 UNICODE symbols are equivelent to ASCII7, however they
125  * must be prefixed with a zero high order byte.)
126  *
127  * I strongly discourage the use of UNICODE mode, mainly because none
128  * of the clients I use can parse those messages (and besides that,
129  * wchars are difficult and non-portable to handle in most UNIX environments).
130  * If you really need to include special characters, use the HTML UNICODE 
131  * entities.  These are of the form &#2026; where 2026 is the hex 
132  * representation of the UNICODE index (in this case, UNICODE 
133  * "Horizontal Ellipsis", or 133 in in ASCII8).
134  *
135  * Implementation note:  Since this is one of the most-used functions
136  * in all of libfaim, it is written with performance in mind.  As such,
137  * it is not as clear as it could be in respect to how this message is
138  * supposed to be layed out. Most obviously, tlvlists should be used 
139  * instead of writing out the bytes manually. 
140  *
141  * XXX support multipart
142  *
143  */
144 faim_export int aim_send_im_ext(aim_session_t *sess, aim_conn_t *conn, struct aim_sendimext_args *args)
145 {
146         int i;
147         aim_frame_t *fr;
148         aim_snacid_t snacid;
149
150         if (!sess || !conn || !args)
151                 return -EINVAL;
152
153         if (!args->msg || (args->msglen <= 0))
154                 return -EINVAL;
155
156         if (args->msglen >= MAXMSGLEN)
157                 return -E2BIG;
158
159         if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, args->msglen+512)))
160                 return -ENOMEM;
161
162         /* XXX should be optional */    
163         snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, args->destsn, strlen(args->destsn)+1);
164         aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
165
166         /* 
167          * Generate a random message cookie 
168          *
169          * We could cache these like we do SNAC IDs.  (In fact, it 
170          * might be a good idea.)  In the message error functions, 
171          * the 8byte message cookie is returned as well as the 
172          * SNAC ID.
173          *
174          */
175         for (i = 0; i < 8; i++)
176                 aimbs_put8(&fr->data, (fu8_t) rand());
177
178         /*
179          * Channel ID
180          */
181         aimbs_put16(&fr->data, 0x0001);
182
183         /*
184          * Destination SN (prepended with byte length)
185          */
186         aimbs_put8(&fr->data, strlen(args->destsn));
187         aimbs_putraw(&fr->data, args->destsn, strlen(args->destsn));
188
189         /*
190          * metaTLV start.
191          */
192         aimbs_put16(&fr->data, 0x0002);
193         aimbs_put16(&fr->data, args->msglen + 0x10);
194
195         /*
196          * Flag data / ICBM Parameters?
197          *
198          * I don't know what these are...
199          *
200          */
201         aimbs_put8(&fr->data, 0x05);
202         aimbs_put8(&fr->data, 0x01);
203
204         /* number of bytes to follow */
205         aimbs_put16(&fr->data, 0x0004);
206         aimbs_put8(&fr->data, 0x01);
207         aimbs_put8(&fr->data, 0x01);
208         aimbs_put8(&fr->data, 0x01);
209         aimbs_put8(&fr->data, 0x02);
210
211         aimbs_put16(&fr->data, 0x0101);
212
213         /* 
214          * Message block length.
215          */
216         aimbs_put16(&fr->data, args->msglen + 0x04);
217
218         /*
219          * Character set.
220          */
221         if (args->flags & AIM_IMFLAGS_UNICODE)
222                 aimbs_put16(&fr->data, 0x0002);
223         else if (args->flags & AIM_IMFLAGS_ISO_8859_1)
224                 aimbs_put16(&fr->data, 0x0003);
225         else
226                 aimbs_put16(&fr->data, 0x0000);
227
228         aimbs_put16(&fr->data, 0x0000);
229
230         /*
231          * Message.  Not terminated.
232          */
233         aimbs_putraw(&fr->data, args->msg, args->msglen);
234
235         /*
236          * Set the Request Acknowledge flag.  
237          */
238         if (args->flags & AIM_IMFLAGS_ACK) {
239                 aimbs_put16(&fr->data, 0x0003);
240                 aimbs_put16(&fr->data, 0x0000);
241         }
242
243         /*
244          * Set the Autoresponse flag.
245          */
246         if (args->flags & AIM_IMFLAGS_AWAY) {
247                 aimbs_put16(&fr->data, 0x0004);
248                 aimbs_put16(&fr->data, 0x0000);
249         }
250
251         /*
252          * Set the Buddy Icon Requested flag.
253          */
254         if (args->flags & AIM_IMFLAGS_BUDDYREQ) {
255                 aimbs_put16(&fr->data, 0x0009);
256                 aimbs_put16(&fr->data, 0x0000);
257         }
258
259         /*
260          * Set the I HAVE A REALLY PURTY ICON flag.
261          */
262         if (args->flags & AIM_IMFLAGS_HASICON) {
263                 aimbs_put16(&fr->data, 0x0008);
264                 aimbs_put16(&fr->data, 0x000c);
265                 aimbs_put32(&fr->data, args->iconlen);
266                 aimbs_put32(&fr->data, args->iconsum);
267                 aimbs_put32(&fr->data, args->iconstamp);
268         }
269
270         aim_tx_enqueue(sess, fr);
271
272 #if 1 /* XXX do this with autoconf or something... */
273         aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
274 #endif
275
276         return 0;
277 }
278
279 /*
280  * Simple wrapper for aim_send_im_ext() 
281  *
282  * You cannot use aim_send_im if you need the HASICON flag.  You must
283  * use aim_send_im_ext directly for that.
284  *
285  * aim_send_im also cannot be used if you require UNICODE messages, because
286  * that requires an explicit message length.  Use aim_send_im_ext().
287  *
288  */
289 faim_export int aim_send_im(aim_session_t *sess, aim_conn_t *conn, const char *destsn, fu16_t flags, const char *msg)
290 {
291         struct aim_sendimext_args args;
292
293         args.destsn = destsn;
294         args.flags = flags;
295         args.msg = msg;
296         args.msglen = strlen(msg);
297
298         return aim_send_im_ext(sess, conn, &args);
299 }
300
301 /*
302  * This is also performance sensative. (If you can believe it...)
303  *
304  */
305 faim_export int aim_send_icon(aim_session_t *sess, aim_conn_t *conn, const char *sn, const fu8_t *icon, int iconlen, time_t stamp, fu32_t iconsum)
306 {
307         int i;
308         fu8_t ck[8];
309         aim_frame_t *fr;
310         aim_snacid_t snacid;
311
312         if (!sess || !conn || !sn || !icon || (iconlen <= 0) || (iconlen >= MAXICONLEN))
313                 return -EINVAL;
314
315         if (conn->type != AIM_CONN_TYPE_BOS)
316                 return -EINVAL;
317
318         for (i = 0; i < 8; i++)
319                 aimutil_put8(ck+i, (fu8_t) rand());
320
321         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)))
322                 return -ENOMEM;
323
324         snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
325         aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
326
327         /*
328          * Cookie
329          */
330         aimbs_putraw(&fr->data, ck, 8);
331
332         /*
333          * Channel (2)
334          */
335         aimbs_put16(&fr->data, 0x0002);
336
337         /*
338          * Dest sn
339          */
340         aimbs_put8(&fr->data, strlen(sn));
341         aimbs_putraw(&fr->data, sn, strlen(sn));
342
343         /*
344          * TLV t(0005)
345          *
346          * Encompasses everything below.
347          */
348         aimbs_put16(&fr->data, 0x0005);
349         aimbs_put16(&fr->data, 2+8+16+6+4+4+iconlen+4+4+4+strlen(AIM_ICONIDENT));
350
351         aimbs_put16(&fr->data, 0x0000);
352         aimbs_putraw(&fr->data, ck, 8);
353         aim_putcap(&fr->data, AIM_CAPS_BUDDYICON);
354
355         /* TLV t(000a) */
356         aimbs_put16(&fr->data, 0x000a);
357         aimbs_put16(&fr->data, 0x0002);
358         aimbs_put16(&fr->data, 0x0001);
359
360         /* TLV t(000f) */
361         aimbs_put16(&fr->data, 0x000f);
362         aimbs_put16(&fr->data, 0x0000);
363
364         /* TLV t(2711) */
365         aimbs_put16(&fr->data, 0x2711);
366         aimbs_put16(&fr->data, 4+4+4+iconlen+strlen(AIM_ICONIDENT));
367         aimbs_put32(&fr->data, iconsum);
368         aimbs_put32(&fr->data, iconlen);
369         aimbs_put32(&fr->data, stamp);
370         aimbs_putraw(&fr->data, icon, iconlen);
371         aimbs_putraw(&fr->data, AIM_ICONIDENT, strlen(AIM_ICONIDENT));
372
373         /* TLV t(0003) */
374         aimbs_put16(&fr->data, 0x0003);
375         aimbs_put16(&fr->data, 0x0000);
376
377         aim_tx_enqueue(sess, fr);
378
379         return 0;
380 }
381
382 static int outgoingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
383 {
384         int i, ret = 0;
385         aim_rxcallback_t userfunc;
386         fu8_t cookie[8];
387         fu16_t channel;
388         aim_tlvlist_t *tlvlist;
389         char *sn;
390         int snlen;
391         fu16_t icbmflags = 0;
392         fu8_t flag1 = 0, flag2 = 0;
393         fu8_t *msg = NULL;
394         aim_tlv_t *msgblock;
395
396         /* ICBM Cookie. */
397         for (i = 0; i < 8; i++)
398                 cookie[i] = aimbs_get8(bs);
399
400         /* Channel ID */
401         channel = aimbs_get16(bs);
402
403         if (channel != 0x01) {
404                 faimdprintf(sess, 0, "icbm: ICBM recieved on unsupported channel.  Ignoring. (chan = %04x)\n", channel);
405                 return 0;
406         }
407
408         snlen = aimbs_get8(bs);
409         sn = aimbs_getstr(bs, snlen);
410
411         tlvlist = aim_readtlvchain(bs);
412
413         if (aim_gettlv(tlvlist, 0x0003, 1))
414                 icbmflags |= AIM_IMFLAGS_ACK;
415         if (aim_gettlv(tlvlist, 0x0004, 1))
416                 icbmflags |= AIM_IMFLAGS_AWAY;
417
418         if ((msgblock = aim_gettlv(tlvlist, 0x0002, 1))) {
419                 aim_bstream_t mbs;
420                 int featurelen, msglen;
421
422                 aim_bstream_init(&mbs, msgblock->value, msgblock->length);
423
424                 aimbs_get8(&mbs);
425                 aimbs_get8(&mbs);
426                 for (featurelen = aimbs_get16(&mbs); featurelen; featurelen--)
427                         aimbs_get8(&mbs);
428                 aimbs_get8(&mbs);
429                 aimbs_get8(&mbs);
430
431                 msglen = aimbs_get16(&mbs) - 4; /* final block length */
432
433                 flag1 = aimbs_get16(&mbs);
434                 flag2 = aimbs_get16(&mbs);
435
436                 msg = aimbs_getstr(&mbs, msglen);
437         }
438
439         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
440                 ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2);
441
442         free(sn);
443         aim_freetlvchain(&tlvlist);
444
445         return ret;
446 }
447
448 /*
449  *
450  * This should use tlvlists, but doesn't for performance reasons.
451  *
452  * XXX support multipart IMs:
453  *
454  * 0004 0007 0000 8f08 d295 
455  *      0031 6520 3b7b f9fd
456  *      0001 
457  *      06 XXXX XXXX XXXX
458  *      0000 
459  *      0004 
460  *              0001 0002 0004 
461  *              0010 0004 0000 01a3
462  *              0002 0004 3ab6 94fa
463  *              0003 0004 3b7b f85a
464  *      0002 003c 
465  *              0501 0001 01
466  *              0101 000a 0000 0000 3c48 544d 4c3e   ASCII part 
467  *              ISO-8859 part:
468  *              0101 0016 0003 0000 6c6b 7364 6a6b 6c6a 676c a56b 3b73 646a 6b6a
469  *              0101 000b 0000 0000 3c2f 4854 4d4c 3e   another ASCII part
470  *
471  */
472 static int incomingim_ch1(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, fu16_t channel, struct aim_userinfo_s *userinfo, aim_bstream_t *bs, fu8_t *cookie)
473 {
474         fu16_t type, length;
475         aim_rxcallback_t userfunc;
476         int ret = 0;
477         struct aim_incomingim_ch1_args args;
478         int endpos;
479
480         memset(&args, 0, sizeof(args));
481
482         /*
483          * This used to be done using tlvchains.  For performance reasons,
484          * I've changed it to process the TLVs in-place.  This avoids lots
485          * of per-IM memory allocations.
486          */
487         while (aim_bstream_empty(bs)) {
488
489                 type = aimbs_get16(bs);
490                 length = aimbs_get16(bs);
491
492                 endpos = aim_bstream_curpos(bs) + length;
493
494                 if (type == 0x0002) { /* Message Block */
495                         fu16_t featureslen;
496                         int z;
497
498                         /*
499                          * This TLV consists of the following:
500                          *   - 0501 -- Unknown
501                          *   - Features: Don't know how to interpret these
502                          *   - 0101 -- Unknown
503                          *   - Message
504                          *
505                          */
506
507                         aimbs_get8(bs); /* 05 */
508                         aimbs_get8(bs); /* 01 */
509                         featureslen = aimbs_get16(bs);
510                         for (z = 0, args.finlen = 0; z < featureslen; z++) {
511                                 fu8_t tmp;
512
513                                 tmp = aimbs_get8(bs);
514                                 if (z < sizeof(args.fingerprint)) {
515                                         args.fingerprint[z] = tmp;
516                                         args.finlen++;
517                                 }
518                         }
519                         aimbs_get8(bs); /* 01 */
520                         aimbs_get8(bs); /* 01 */
521
522                         /* Message string length, including flag words. */
523                         args.msglen = aimbs_get16(bs);
524
525                         /* Flag words. */
526                         args.flag1 = aimbs_get16(bs);
527                         if (args.flag1 == 0x0000)
528                                 ; /* ASCII */
529                         else if (args.flag1 == 0x0002)
530                                 args.icbmflags |= AIM_IMFLAGS_UNICODE;
531                         else if (args.flag1 == 0x0003)
532                                 args.icbmflags |= AIM_IMFLAGS_ISO_8859_1;
533                         else if (args.flag1 == 0xffff)
534                                 ; /* no encoding (yeep!) */
535
536                         args.flag2 = aimbs_get16(bs);
537                         if (args.flag2 == 0x0000)
538                                 ; /* standard subencoding? */
539                         else if (args.flag2 == 0x000b)
540                                 args.icbmflags |= AIM_IMFLAGS_SUBENC_MACINTOSH;
541                         else if (args.flag2 == 0xffff)
542                                 ; /* no subencoding */
543
544                         /* XXX this isn't really necesary... */ 
545                         if (    ((args.flag1 != 0x0000) &&
546                                  (args.flag1 != 0x0002) &&
547                                  (args.flag1 != 0x0003) &&
548                                  (args.flag1 != 0xffff)) ||
549                                 ((args.flag2 != 0x0000) &&
550                                  (args.flag2 != 0x000b) &&
551                                  (args.flag2 != 0xffff))) {
552                                 faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", args.flag1, args.flag2);
553                         }
554
555                         /* Message. */
556                         args.msglen -= 4;
557                         if (args.icbmflags & AIM_IMFLAGS_UNICODE) {
558                                 fu8_t *umsg;
559
560                                 /* Can't use getstr because of wide null */
561                                 umsg = aimbs_getraw(bs, args.msglen);
562                                 args.msg = malloc(args.msglen+2);
563                                 memcpy(args.msg, umsg, args.msglen);
564                                 args.msg[args.msglen] = '\0'; /* wide NULL */
565                                 args.msg[args.msglen+1] = '\0';
566
567                                 free(umsg);
568
569                         } else
570                                 args.msg = aimbs_getstr(bs, args.msglen);
571
572                 } else if (type == 0x0003) { /* Server Ack Requested */
573
574                         args.icbmflags |= AIM_IMFLAGS_ACK;
575
576                 } else if (type == 0x0004) { /* Message is Auto Response */
577
578                         args.icbmflags |= AIM_IMFLAGS_AWAY;
579
580                 } else if (type == 0x0008) { /* I-HAVE-A-REALLY-PURTY-ICON Flag */
581
582                         args.iconchecksum = aimbs_get32(bs);
583                         args.iconlength = aimbs_get32(bs);
584                         args.iconstamp = aimbs_get32(bs);
585                         args.icbmflags |= AIM_IMFLAGS_HASICON;
586
587                 } else if (type == 0x0009) {
588
589                         args.icbmflags |= AIM_IMFLAGS_BUDDYREQ;
590
591                 } else if (type == 0x0017) {
592
593                         args.extdatalen = length;
594                         args.extdata = aimbs_getraw(bs, args.extdatalen);
595
596                 } else {
597                         faimdprintf(sess, 0, "incomingim_ch1: unknown TLV 0x%04x (len %d)\n", type, length);
598                 }
599
600                 /*
601                  * This is here to protect ourselves from ourselves.  That
602                  * is, if something above doesn't completly parse its value
603                  * section, or, worse, overparses it, this will set the
604                  * stream where it needs to be in order to land on the next
605                  * TLV when the loop continues.
606                  *
607                  */
608                 aim_bstream_setpos(bs, endpos);
609         }
610
611
612         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
613                 ret = userfunc(sess, rx, channel, userinfo, &args);
614
615         free(args.extdata);
616         free(args.msg);
617
618         return ret;
619 }
620
621 /* XXX Ugh.  I think its obvious. */
622 static int incomingim_ch2(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, fu16_t channel, struct aim_userinfo_s *userinfo, aim_tlvlist_t *tlvlist, fu8_t *cookie)
623 {
624         aim_rxcallback_t userfunc;
625         aim_tlv_t *block1;
626         aim_tlvlist_t *list2;
627         int ret = 0;
628         struct aim_incomingim_ch2_args args;
629         aim_bstream_t bbs;
630         fu8_t *cookie2;
631
632         memset(&args, 0, sizeof(args));
633
634         /*
635          * There's another block of TLVs embedded in the type 5 here. 
636          */
637         if (!(block1 = aim_gettlv(tlvlist, 0x0005, 1)) || !block1->value) {
638                 faimdprintf(sess, 0, "no tlv 0x0005 in rendezvous transaction!\n");
639                 return 0;
640         }
641
642         aim_bstream_init(&bbs, block1->value, block1->length);
643
644         /*
645          * First two bytes represent the status of the connection.
646          *
647          * 0 is a request, 2 is an accept
648          */ 
649         args.status = aimbs_get16(&bbs);
650
651         /*
652          * Next comes the cookie.  Should match the ICBM cookie.
653          */
654         cookie2 = aimbs_getraw(&bbs, 8);
655         if (memcmp(cookie, cookie2, 8) != 0) 
656                 faimdprintf(sess, 0, "rend: warning cookies don't match!\n");
657         free(cookie2);
658
659         /*
660          * The next 16bytes are a capability block so we can
661          * identify what type of rendezvous this is.
662          *
663          * Thanks to Eric Warmenhoven <warmenhoven@linux.com> (of GAIM)
664          * for pointing some of this out to me.  In fact, a lot of 
665          * the client-to-client info comes from the work of the GAIM 
666          * developers. Thanks!
667          *
668          * Read off one capability string and we should have it ID'd.
669          * 
670          */
671         if ((args.reqclass = aim_getcap(sess, &bbs, 0x10)) == 0x0000) {
672                 faimdprintf(sess, 0, "rend: no ID block\n");
673                 return 0;
674         }
675
676         /* 
677          * What follows may be TLVs or nothing, depending on the
678          * purpose of the message.
679          *
680          * Ack packets for instance have nothing more to them.
681          */
682         list2 = aim_readtlvchain(&bbs);
683
684         if (!list2 || ((args.reqclass != AIM_CAPS_IMIMAGE) && !(aim_gettlv(list2, 0x2711, 1)))) {
685                 aim_msgcookie_t *cook;
686                 int type;
687
688                 type = aim_msgcookie_gettype(args.reqclass); /* XXX: fix this shitty code */
689
690                 if ((cook = aim_checkcookie(sess, cookie, type)) == NULL) {
691                         faimdprintf(sess, 0, "non-data rendezvous thats not in cache (type %d)\n", type);
692                         aim_freetlvchain(&list2);
693                         return 1;
694                 }
695
696                 if (cook->type == AIM_COOKIETYPE_OFTGET) {
697                         struct aim_filetransfer_priv *ft;
698
699                         if (cook->data) {
700                                 int errorcode = -1; /* XXX shouldnt this be 0? */
701
702                                 ft = (struct aim_filetransfer_priv *)cook->data;
703
704                                 if (args.status != 0x0002) {
705
706                                         if (aim_gettlv(list2, 0x000b, 1))
707                                                 errorcode = aim_gettlv16(list2, 0x000b, 1);
708
709                                         /* XXX this should make it up to the client, you know.. */
710                                         if (errorcode)
711                                                 faimdprintf(sess, 0, "transfer from %s (%s) for %s cancelled (error code %d)\n", ft->sn, ft->ip, ft->fh.name, errorcode);
712                                 } /* args.status != 0x0002 */
713
714                         } else {
715                                 faimdprintf(sess, 0, "no data attached to file transfer\n");
716                         } /* !cook->data */
717
718                 } else if (cook->type == AIM_CAPS_VOICE) {
719
720                         faimdprintf(sess, 0, "voice request cancelled\n");
721                 
722                 } else {
723                 
724                         faimdprintf(sess, 0, "unknown cookie cache type %d\n", cook->type);
725                 }
726
727                 aim_freetlvchain(&list2);
728
729                 return 1;
730         }
731
732         /*
733          * The rest of the handling depends on what type it is.
734          */
735         if (args.reqclass & AIM_CAPS_BUDDYICON) {
736                 aim_tlv_t *miscinfo;
737                 aim_bstream_t tbs;
738
739                 miscinfo = aim_gettlv(list2, 0x2711, 1);
740                 aim_bstream_init(&tbs, miscinfo->value, miscinfo->length);
741
742                 args.info.icon.checksum = aimbs_get32(&tbs);
743                 args.info.icon.length = aimbs_get32(&tbs);
744                 args.info.icon.timestamp = aimbs_get32(&tbs);
745                 args.info.icon.icon = aimbs_getraw(&tbs, args.info.icon.length);
746
747                 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
748                         ret = userfunc(sess, rx, channel, userinfo, &args);
749
750                 free(args.info.icon.icon);
751
752         } else if (args.reqclass & AIM_CAPS_VOICE) {
753                 aim_msgcookie_t *cachedcook;
754
755                 faimdprintf(sess, 1, "rend: voice!\n");
756
757                 if(!(cachedcook = (aim_msgcookie_t*)calloc(1, sizeof(aim_msgcookie_t)))) {
758                         aim_freetlvchain(&list2);
759                         return 0;
760                 }
761
762                 memcpy(cachedcook->cookie, cookie, 8);
763                 cachedcook->type = AIM_COOKIETYPE_OFTVOICE;
764                 cachedcook->data = NULL;
765
766                 if (aim_cachecookie(sess, cachedcook) == -1)
767                         faimdprintf(sess, 0, "ERROR caching message cookie\n");
768
769                 /* XXX: implement all this */
770
771                 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) 
772                         ret = userfunc(sess, rx, channel, userinfo, &args);
773
774         } else if (args.reqclass & AIM_CAPS_IMIMAGE) {
775                 char ip[30];
776                 struct aim_directim_priv *priv;
777
778                 memset(ip, 0, sizeof(ip));
779
780                 if (aim_gettlv(list2, 0x0003, 1) && aim_gettlv(list2, 0x0005, 1)) {
781                         aim_tlv_t *iptlv, *porttlv;
782                           
783                         iptlv = aim_gettlv(list2, 0x0003, 1);
784                         porttlv = aim_gettlv(list2, 0x0005, 1);
785
786                         snprintf(ip, 30, "%d.%d.%d.%d:%d", 
787                                 aimutil_get8(iptlv->value+0),
788                                 aimutil_get8(iptlv->value+1),
789                                 aimutil_get8(iptlv->value+2),
790                                 aimutil_get8(iptlv->value+3),
791                                 4443 /*aimutil_get16(porttlv->value)*/);
792                 }
793
794                 faimdprintf(sess, 1, "rend: directIM request from %s (%s)\n",
795                                 userinfo->sn, ip);
796
797                 /* 
798                  * XXX: there are a couple of different request packets for
799                  *          different things 
800                  */
801
802                 args.info.directim = priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)); /* XXX error */
803                 memcpy(priv->ip, ip, sizeof(priv->ip));
804                 memcpy(priv->sn, userinfo->sn, sizeof(priv->sn));
805                 memcpy(priv->cookie, cookie, sizeof(priv->cookie));
806
807                 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
808                         ret = userfunc(sess, rx, channel, userinfo, &args);
809
810         } else if (args.reqclass & AIM_CAPS_CHAT) {
811                 aim_tlv_t *miscinfo;
812                 aim_bstream_t tbs;
813
814                 miscinfo = aim_gettlv(list2, 0x2711, 1);
815
816                 aim_bstream_init(&tbs, miscinfo->value, miscinfo->length);
817
818                 aim_chat_readroominfo(&tbs, &args.info.chat.roominfo);
819                           
820                 if (aim_gettlv(list2, 0x000c, 1))
821                         args.info.chat.msg = aim_gettlv_str(list2, 0x000c, 1);
822                 
823                 if (aim_gettlv(list2, 0x000d, 1))
824                         args.info.chat.encoding = aim_gettlv_str(list2, 0x000d, 1);
825                 
826                 if (aim_gettlv(list2, 0x000e, 1))
827                         args.info.chat.lang = aim_gettlv_str(list2, 0x000e, 1);
828
829                 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
830                         ret = userfunc(sess, rx, channel, userinfo, &args);
831
832                 free(args.info.chat.roominfo.name);
833                 free(args.info.chat.msg);
834                 free(args.info.chat.encoding);
835                 free(args.info.chat.lang);
836
837         } else if (args.reqclass & AIM_CAPS_GETFILE) {
838                 char ip[30];
839                 aim_msgcookie_t *cachedcook;
840                 aim_tlv_t *miscinfo;
841                 aim_tlv_t *iptlv, *porttlv;
842
843                 memset(ip, 0, 30);
844
845                 if (!(cachedcook = calloc(1, sizeof(aim_msgcookie_t)))) {
846                         aim_freetlvchain(&list2);
847                         return 0;
848                 }
849
850                 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) || 
851                         !(iptlv = aim_gettlv(list2, 0x0003, 1)) || 
852                         !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
853                         
854                         faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
855                         aim_cookie_free(sess, cachedcook);
856                         aim_freetlvchain(&list2);
857                         
858                         return 0;
859                 }
860
861                 snprintf(ip, 30, "%d.%d.%d.%d:%d",
862                         aimutil_get8(iptlv->value+0),
863                         aimutil_get8(iptlv->value+1),
864                         aimutil_get8(iptlv->value+2),
865                         aimutil_get8(iptlv->value+3),
866                         aimutil_get16(porttlv->value));
867
868                 faimdprintf(sess, 0, "rend: file get request from %s (%s)\n", userinfo->sn, ip);
869
870                 args.info.getfile.ip = ip;
871                 args.info.getfile.cookie = cookie;
872
873                 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
874                         ret = userfunc(sess, rx, channel, userinfo, &args);
875
876         } else if (args.reqclass & AIM_CAPS_SENDFILE) {
877 #if 0 
878                 char ip[30];
879                 aim_msgcookie_t *cachedcook;
880                 aim_tlv_t *miscinfo;
881                 aim_tlv_t *iptlv, *porttlv;
882
883                 memset(ip, 0, 30);
884
885                 if (!(cachedcook = calloc(1, sizeof(aim_msgcookie_t)))) {
886                         aim_freetlvchain(&list2);
887                         return 0;
888                 }
889
890                 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) || 
891                         !(iptlv = aim_gettlv(list2, 0x0003, 1)) || 
892                         !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
893                 
894                         faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
895                         aim_cookie_free(sess, cachedcook);
896                         aim_freetlvchain(&list2);
897
898                         return 0;
899                 }
900
901                 snprintf(ip, 30, "%d.%d.%d.%d:%d",
902                         aimutil_get8(iptlv->value+0),
903                         aimutil_get8(iptlv->value+1),
904                         aimutil_get8(iptlv->value+2),
905                         aimutil_get8(iptlv->value+3),
906                         aimutil_get16(porttlv->value));
907
908                 if (aim_gettlv(list2, 0x000c, 1))
909                         desc = aim_gettlv_str(list2, 0x000c, 1);
910
911                 faimdprintf(sess, 0, "rend: file transfer request from %s: %s (%s)\n",
912                         userinfo->sn, desc, ip);
913
914                 memcpy(cachedcook->cookie, cookie, 8);
915
916                 ft = malloc(sizeof(struct aim_filetransfer_priv)); /* XXX */
917                 strncpy(ft->sn, userinfo.sn, sizeof(ft->sn));
918                 strncpy(ft->ip, ip, sizeof(ft->ip));
919                 strncpy(ft->fh.name, miscinfo->value+8, sizeof(ft->fh.name));
920                 cachedcook->type = AIM_COOKIETYPE_OFTSEND;
921                 cachedcook->data = ft;
922
923                 if (aim_cachecookie(sess, cachedcook) == -1)
924                         faimdprintf(sess, 0, "ERROR caching message cookie\n");
925
926                 aim_accepttransfer(sess, rx->conn, ft->sn, cookie, AIM_CAPS_SENDFILE);
927
928                 if (desc)
929                         free(desc);
930
931                 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
932                         ret = userfunc(sess, rx, channel, userinfo, &args);
933
934 #endif  
935         } else
936                 faimdprintf(sess, 0, "rend: unknown rendezvous 0x%04x\n", args.reqclass);
937
938         aim_freetlvchain(&list2);
939
940         return ret;
941 }
942
943 /*
944  * It can easily be said that parsing ICBMs is THE single
945  * most difficult thing to do in the in AIM protocol.  In
946  * fact, I think I just did say that.
947  *
948  * Below is the best damned solution I've come up with
949  * over the past sixteen months of battling with it. This
950  * can parse both away and normal messages from every client
951  * I have access to.  Its not fast, its not clean.  But it works.
952  *
953  */
954 static int incomingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
955 {
956         int i, ret = 0;
957         fu8_t cookie[8];
958         fu16_t channel;
959         struct aim_userinfo_s userinfo;
960
961         memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
962
963         /*
964          * Read ICBM Cookie.  And throw away.
965          */
966         for (i = 0; i < 8; i++)
967                 cookie[i] = aimbs_get8(bs);
968
969         /*
970          * Channel ID.
971          *
972          * Channel 0x0001 is the message channel.  There are 
973          * other channels for things called "rendevous"
974          * which represent chat and some of the other new
975          * features of AIM2/3/3.5. 
976          *
977          * Channel 0x0002 is the Rendevous channel, which
978          * is where Chat Invitiations and various client-client
979          * connection negotiations come from.
980          * 
981          */
982         channel = aimbs_get16(bs);
983
984         /*
985          * Technically Channel 3 in chat could be done here too.
986          */
987         if ((channel != 0x01) && (channel != 0x02)) {
988                 faimdprintf(sess, 0, "icbm: ICBM received on an unsupported channel.  Ignoring.\n (chan = %04x)", channel);
989                 return 0;
990         }
991
992         /*
993          * Extract the standard user info block.
994          *
995          * Note that although this contains TLVs that appear contiguous
996          * with the TLVs read below, they are two different pieces.  The
997          * userinfo block contains the number of TLVs that contain user
998          * information, the rest are not even though there is no seperation.
999          * aim_extractuserinfo() returns the number of bytes used by the
1000          * userinfo tlvs, so you can start reading the rest of them right
1001          * afterward.  
1002          *
1003          * That also means that TLV types can be duplicated between the
1004          * userinfo block and the rest of the message, however there should
1005          * never be two TLVs of the same type in one block.
1006          * 
1007          */
1008         aim_extractuserinfo(sess, bs, &userinfo);
1009
1010         /*
1011          * From here on, its depends on what channel we're on.
1012          *
1013          * Technically all channels have a TLV list have this, however,
1014          * for the common channel 1 case, in-place parsing is used for
1015          * performance reasons (less memory allocation).
1016          */
1017         if (channel == 1) {
1018
1019                 ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, bs, cookie);
1020
1021         } else if (channel == 0x0002) {
1022                 aim_tlvlist_t *tlvlist;
1023
1024                 /*
1025                  * Read block of TLVs (not including the userinfo data).  All 
1026                  * further data is derived from what is parsed here.
1027                  */
1028                 tlvlist = aim_readtlvchain(bs);
1029
1030                 ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
1031
1032                 /*
1033                  * Free up the TLV chain.
1034                  */
1035                 aim_freetlvchain(&tlvlist);
1036         }
1037
1038         return ret;
1039 }
1040
1041 /*
1042  * Possible codes:
1043  *    AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
1044  *    AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
1045  *    AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
1046  * 
1047  */
1048 faim_export int aim_denytransfer(aim_session_t *sess, aim_conn_t *conn, const char *sender, const char *cookie, fu16_t code)
1049 {
1050         aim_frame_t *fr;
1051         aim_snacid_t snacid;
1052         aim_tlvlist_t *tl = NULL;
1053         
1054         if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sender)+6)))
1055                 return -ENOMEM;
1056
1057         snacid = aim_cachesnac(sess, 0x0004, 0x000b, 0x0000, NULL, 0);
1058         aim_putsnac(&fr->data, 0x0004, 0x000b, 0x0000, snacid);
1059         
1060         aimbs_putraw(&fr->data, cookie, 8);
1061
1062         aimbs_put16(&fr->data, 0x0002); /* channel */
1063         aimbs_put8(&fr->data, strlen(sender));
1064         aimbs_putraw(&fr->data, sender, strlen(sender));
1065
1066         aim_addtlvtochain16(&tl, 0x0003, code);
1067         aim_writetlvchain(&fr->data, &tl);
1068         aim_freetlvchain(&tl);
1069
1070         aim_tx_enqueue(sess, fr);
1071
1072         return 0;
1073 }
1074
1075 /*
1076  * aim_reqicbmparaminfo()
1077  *
1078  * Request ICBM parameter information.
1079  *
1080  */
1081 faim_export int aim_reqicbmparams(aim_session_t *sess, aim_conn_t *conn)
1082 {
1083         return aim_genericreq_n(sess, conn, 0x0004, 0x0004);
1084 }
1085
1086 /*
1087  *
1088  * I definitly recommend sending this.  If you don't, you'll be stuck
1089  * with the rather unreasonable defaults.  You don't want those.  Send this.
1090  * 
1091  */
1092 faim_export int aim_seticbmparam(aim_session_t *sess, aim_conn_t *conn, struct aim_icbmparameters *params)
1093 {
1094         aim_frame_t *fr;
1095         aim_snacid_t snacid;
1096
1097         if (!sess || !conn || !params)
1098                 return -EINVAL;
1099
1100         if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+16)))
1101                 return -ENOMEM;
1102
1103         snacid = aim_cachesnac(sess, 0x0004, 0x0002, 0x0000, NULL, 0);
1104         aim_putsnac(&fr->data, 0x0004, 0x0002, 0x0000, snacid);
1105
1106         /* This is read-only (see Parameter Reply). Must be set to zero here. */
1107         aimbs_put16(&fr->data, 0x0000);
1108
1109         /* These are all read-write */
1110         aimbs_put32(&fr->data, params->flags); 
1111         aimbs_put16(&fr->data, params->maxmsglen);
1112         aimbs_put16(&fr->data, params->maxsenderwarn); 
1113         aimbs_put16(&fr->data, params->maxrecverwarn); 
1114         aimbs_put32(&fr->data, params->minmsginterval);
1115
1116         aim_tx_enqueue(sess, fr);
1117
1118         return 0;
1119 }
1120
1121 static int paraminfo(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1122 {
1123         struct aim_icbmparameters params;
1124         aim_rxcallback_t userfunc;
1125
1126         params.maxchan = aimbs_get16(bs);
1127         params.flags = aimbs_get32(bs);
1128         params.maxmsglen = aimbs_get16(bs);
1129         params.maxsenderwarn = aimbs_get16(bs);
1130         params.maxrecverwarn = aimbs_get16(bs);
1131         params.minmsginterval = aimbs_get32(bs);
1132         
1133         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1134                 return userfunc(sess, rx, &params);
1135
1136         return 0;
1137 }
1138
1139 static int missedcall(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1140 {
1141         int ret = 0;
1142         aim_rxcallback_t userfunc;
1143         fu16_t channel, nummissed, reason;
1144         struct aim_userinfo_s userinfo;
1145
1146         while (aim_bstream_empty(bs)) { 
1147
1148                 channel = aimbs_get16(bs);
1149                 aim_extractuserinfo(sess, bs, &userinfo);
1150                 nummissed = aimbs_get16(bs);
1151                 reason = aimbs_get16(bs);
1152
1153                 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1154                          ret = userfunc(sess, rx, channel, &userinfo, nummissed, reason);
1155         }
1156
1157         return ret;
1158 }
1159
1160 static int clienterr(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1161 {
1162         int ret = 0;
1163         aim_rxcallback_t userfunc;
1164         fu16_t channel, reason;
1165         char *sn;
1166         fu8_t *ck, snlen;
1167
1168         ck = aimbs_getraw(bs, 8);
1169         channel = aimbs_get16(bs);
1170         snlen = aimbs_get8(bs);
1171         sn = aimbs_getstr(bs, snlen);
1172         reason = aimbs_get16(bs);
1173
1174         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1175                  ret = userfunc(sess, rx, channel, sn, reason);
1176
1177         return ret;
1178 }
1179
1180 static int msgack(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1181 {
1182         aim_rxcallback_t userfunc;
1183         fu16_t type;
1184         fu8_t snlen, *ck;
1185         char *sn;
1186
1187         ck = aimbs_getraw(bs, 8);
1188         type = aimbs_get16(bs);
1189         snlen = aimbs_get8(bs);
1190         sn = aimbs_getstr(bs, snlen);
1191
1192         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1193                 return userfunc(sess, rx, type, sn);
1194
1195         free(sn);
1196         free(ck);
1197
1198         return 0;
1199 }
1200
1201 static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1202 {
1203
1204         if (snac->subtype == 0x0005)
1205                 return paraminfo(sess, mod, rx, snac, bs);
1206         else if (snac->subtype == 0x0006)
1207                 return outgoingim(sess, mod, rx, snac, bs);
1208         else if (snac->subtype == 0x0007)
1209                 return incomingim(sess, mod, rx, snac, bs);
1210         else if (snac->subtype == 0x000a)
1211                 return missedcall(sess, mod, rx, snac, bs);
1212         else if (snac->subtype == 0x000b)
1213                 return clienterr(sess, mod, rx, snac, bs);
1214         else if (snac->subtype == 0x000c)
1215                 return msgack(sess, mod, rx, snac, bs);
1216
1217         return 0;
1218 }
1219
1220 faim_internal int msg_modfirst(aim_session_t *sess, aim_module_t *mod)
1221 {
1222
1223         mod->family = 0x0004;
1224         mod->version = 0x0000;
1225         mod->flags = 0;
1226         strncpy(mod->name, "messaging", sizeof(mod->name));
1227         mod->snachandler = snachandler;
1228
1229         return 0;
1230 }
This page took 0.266116 seconds and 3 git commands to generate.