4 * The routines for sending/receiving Instant Messages.
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
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
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.
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.
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,
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
42 faim_export unsigned short aim_fingerprintclient(unsigned char *msghdr, int len)
45 unsigned short clientid;
47 unsigned char data[10];
49 /* AOL Mobile Communicator, WinAIM 1.0.414 */
51 9, {0x05, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01}},
53 /* WinAIM 2.0.847, 2.1.1187, 3.0.1464, 4.3.2229, 4.4.2286 */
54 { AIM_CLIENTTYPE_WINAIM,
55 9, {0x05, 0x01, 0x00, 0x03, 0x01, 0x01, 0x02, 0x01, 0x01}},
57 /* WinAIM 4.1.2010, libfaim */
58 { AIM_CLIENTTYPE_WINAIM41,
59 10, {0x05, 0x01, 0x00, 0x04, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01}},
61 /* AOL v6.0, CompuServe 2000 v6.0, any TOC client */
62 { AIM_CLIENTTYPE_AOL_TOC,
63 7, {0x05, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01}},
69 if (!msghdr || (len <= 0))
70 return AIM_CLIENTTYPE_UNKNOWN;
72 for (i = 0; fingerprints[i].len; i++) {
73 if (fingerprints[i].len != len)
75 if (memcmp(fingerprints[i].data, msghdr, fingerprints[i].len) == 0)
76 return fingerprints[i].clientid;
79 return AIM_CLIENTTYPE_UNKNOWN;
82 /* This should be endian-safe now... but who knows... */
83 faim_export unsigned short aim_iconsum(const unsigned char *buf, int buflen)
88 for (i = 0, sum = 0; i < buflen; i += 2)
89 sum += (buf[i+1] << 8) + buf[i];
91 sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff);
97 * Send an ICBM (instant message).
101 * AIM_IMFLAGS_AWAY -- Marks the message as an autoresponse
102 * AIM_IMFLAGS_ACK -- Requests that the server send an ack
103 * when the message is received (of type 0x0004/0x000c)
104 * AIM_IMFLAGS_UNICODE--Instead of ASCII7, the passed message is
105 * made up of UNICODE duples. If you set
106 * this, you'd better be damn sure you know
108 * AIM_IMFLAGS_ISO_8859_1 -- The message contains the ASCII8 subset
109 * known as ISO-8859-1.
111 * Generally, you should use the lowest encoding possible to send
112 * your message. If you only use basic punctuation and the generic
113 * Latin alphabet, use ASCII7 (no flags). If you happen to use non-ASCII7
114 * characters, but they are all clearly defined in ISO-8859-1, then
115 * use that. Keep in mind that not all characters in the PC ASCII8
116 * character set are defined in the ISO standard. For those cases (most
117 * notably when the (r) symbol is used), you must use the full UNICODE
118 * encoding for your message. In UNICODE mode, _all_ characters must
119 * occupy 16bits, including ones that are not special. (Remember that
120 * the first 128 UNICODE symbols are equivelent to ASCII7, however they
121 * must be prefixed with a zero high order byte.)
123 * I strongly discourage the use of UNICODE mode, mainly because none
124 * of the clients I use can parse those messages (and besides that,
125 * wchars are difficult and non-portable to handle in most UNIX environments).
126 * If you really need to include special characters, use the HTML UNICODE
127 * entities. These are of the form ߪ where 2026 is the hex
128 * representation of the UNICODE index (in this case, UNICODE
129 * "Horizontal Ellipsis", or 133 in in ASCII8).
132 faim_export int aim_send_im_ext(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_sendimext_args *args)
135 struct command_tx_struct *newpacket;
137 if (!sess || !conn || !args)
140 if (!args->msg || (args->msglen <= 0))
143 if (args->msglen >= MAXMSGLEN)
146 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, args->msglen+512)))
149 newpacket->lock = 1; /* lock struct */
152 curbyte += aim_putsnac(newpacket->data+curbyte,
153 0x0004, 0x0006, 0x0000, sess->snac_nextid);
156 * Generate a random message cookie
158 * We could cache these like we do SNAC IDs. (In fact, it
159 * might be a good idea.) In the message error functions,
160 * the 8byte message cookie is returned as well as the
164 for (i = 0; i < 8; i++) {
165 curbyte += aimutil_put8(newpacket->data+curbyte,
166 (unsigned char) rand());
172 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
175 * Destination SN (prepended with byte length)
177 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(args->destsn));
178 curbyte += aimutil_putstr(newpacket->data+curbyte,
179 args->destsn, strlen(args->destsn));
184 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
185 curbyte += aimutil_put16(newpacket->data+curbyte, args->msglen + 0x10);
188 * Flag data / ICBM Parameters?
190 * I don't know what these are...
193 curbyte += aimutil_put8(newpacket->data+curbyte, 0x05);
194 curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
196 /* number of bytes to follow */
197 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
198 curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
199 curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
200 curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
201 curbyte += aimutil_put8(newpacket->data+curbyte, 0x02);
203 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0101);
206 * Message block length.
208 curbyte += aimutil_put16(newpacket->data+curbyte, args->msglen + 0x04);
213 if (args->flags & AIM_IMFLAGS_UNICODE)
214 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
215 else if (args->flags & AIM_IMFLAGS_ISO_8859_1)
216 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
218 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
220 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
223 * Message. Not terminated.
225 curbyte += aimutil_putstr(newpacket->data+curbyte,
226 args->msg, args->msglen);
229 * Set the Request Acknowledge flag.
231 if (args->flags & AIM_IMFLAGS_ACK) {
232 curbyte += aimutil_put16(newpacket->data+curbyte,0x0003);
233 curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
237 * Set the Autoresponse flag.
239 if (args->flags & AIM_IMFLAGS_AWAY) {
240 curbyte += aimutil_put16(newpacket->data+curbyte,0x0004);
241 curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
245 * Set the Buddy Icon Requested flag.
247 if (args->flags & AIM_IMFLAGS_BUDDYREQ) {
248 curbyte += aimutil_put16(newpacket->data+curbyte,0x0009);
249 curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
253 * Set the I HAVE A REALLY PURTY ICON flag (with timestamp).
255 if (args->flags & AIM_IMFLAGS_HASICON) {
256 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0008);
257 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000c);
258 curbyte += aimutil_put32(newpacket->data+curbyte, args->iconlen);
259 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
260 curbyte += aimutil_put16(newpacket->data+curbyte, args->iconsum);
261 curbyte += aimutil_put32(newpacket->data+curbyte, args->iconstamp);
264 newpacket->commandlen = curbyte;
267 aim_tx_enqueue(sess, newpacket);
269 #if 1 /* XXX do this with autoconf or something... */
270 aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, args->destsn, strlen(args->destsn)+1);
271 aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
278 * Simple wrapper for aim_send_im_ext()
280 * You cannot use aim_send_im if you need the HASICON flag. You must
281 * use aim_send_im_ext directly for that.
283 * aim_send_im also cannot be used if you require UNICODE messages, because
284 * that requires an explicit message length. Use aim_send_im_ext().
287 faim_export int aim_send_im(struct aim_session_t *sess, struct aim_conn_t *conn, const char *destsn, unsigned short flags, const char *msg)
289 struct aim_sendimext_args args;
291 args.destsn = destsn;
294 args.msglen = strlen(msg);
296 return aim_send_im_ext(sess, conn, &args);
299 faim_export int aim_send_icon(struct aim_session_t *sess, struct aim_conn_t *conn, const char *sn, const unsigned char *icon, int iconlen, time_t stamp, unsigned short iconsum)
301 struct command_tx_struct *np;
305 if (!sess || !conn || !sn || !icon ||
306 (iconlen <= 0) || (iconlen >= MAXICONLEN))
309 if (conn->type != AIM_CONN_TYPE_BOS)
312 for (i = 0, curbyte = 0; i < 8; i++)
313 curbyte += aimutil_put8(ck+curbyte, (u_char)rand());
315 if (!(np = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 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)))
320 curbyte = aim_putsnac(np->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
325 memcpy(np->data+curbyte, ck, 8);
331 curbyte += aimutil_put16(np->data+curbyte, 0x0002);
336 curbyte += aimutil_put8(np->data+curbyte, strlen(sn));
337 curbyte += aimutil_putstr(np->data+curbyte, sn, strlen(sn));
342 curbyte += aimutil_put16(np->data+curbyte, 0x0005);
343 curbyte += aimutil_put16(np->data+curbyte, 2+8+16+6+4+4+iconlen+4+4+4+strlen(AIM_ICONIDENT));
345 curbyte += aimutil_put16(np->data+curbyte, 0x0000);
347 memcpy(np->data+curbyte, ck, 8);
350 curbyte += aim_putcap(np->data+curbyte, 16, AIM_CAPS_BUDDYICON);
353 curbyte += aimutil_put16(np->data+curbyte, 0x000a);
354 curbyte += aimutil_put16(np->data+curbyte, 0x0002);
355 curbyte += aimutil_put16(np->data+curbyte, 0x0001);
358 curbyte += aimutil_put16(np->data+curbyte, 0x000f);
359 curbyte += aimutil_put16(np->data+curbyte, 0x0000);
362 curbyte += aimutil_put16(np->data+curbyte, 0x2711);
363 curbyte += aimutil_put16(np->data+curbyte, 4+4+4+iconlen+strlen(AIM_ICONIDENT));
364 curbyte += aimutil_put16(np->data+curbyte, 0x0000);
365 curbyte += aimutil_put16(np->data+curbyte, iconsum);
366 curbyte += aimutil_put32(np->data+curbyte, iconlen);
367 curbyte += aimutil_put32(np->data+curbyte, stamp);
368 memcpy(np->data+curbyte, icon, iconlen);
370 memcpy(np->data+curbyte, AIM_ICONIDENT, strlen(AIM_ICONIDENT));
371 curbyte += strlen(AIM_ICONIDENT);
374 curbyte += aimutil_put16(np->data+curbyte, 0x0003);
375 curbyte += aimutil_put16(np->data+curbyte, 0x0000);
377 np->commandlen = curbyte;
379 aim_tx_enqueue(sess, np);
384 static int outgoingim(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
386 unsigned int i, ret = 0;
387 aim_rxcallback_t userfunc;
388 unsigned char cookie[8];
390 struct aim_tlvlist_t *tlvlist;
392 unsigned short icbmflags = 0;
393 unsigned char flag1 = 0, flag2 = 0;
394 unsigned char *msgblock = NULL, *msg = NULL;
397 for (i = 0; i < 8; i++)
398 cookie[i] = aimutil_get8(data+i);
401 channel = aimutil_get16(data+i);
404 if (channel != 0x01) {
405 faimdprintf(sess, 0, "icbm: ICBM recieved on unsupported channel. Ignoring. (chan = %04x)\n", channel);
409 strncpy(sn, (char *) data+i+1, (int) *(data+i));
410 i += 1 + (int) *(data+i);
412 tlvlist = aim_readtlvchain(data+i, datalen-i);
414 if (aim_gettlv(tlvlist, 0x0003, 1))
415 icbmflags |= AIM_IMFLAGS_ACK;
416 if (aim_gettlv(tlvlist, 0x0004, 1))
417 icbmflags |= AIM_IMFLAGS_AWAY;
419 if ((msgblock = (unsigned char *)aim_gettlv_str(tlvlist, 0x0002, 1))) {
422 /* no, this really is correct. I'm not high or anything either. */
424 j += 2 + aimutil_get16(msgblock+j);
427 j += 2; /* final block length */
429 flag1 = aimutil_get16(msgblock);
431 flag2 = aimutil_get16(msgblock);
437 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
438 ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2);
441 aim_freetlvchain(&tlvlist);
449 * 0004 0007 0000 8f08 d295
450 * 0031 6520 3b7b f9fd
456 * 0010 0004 0000 01a3
457 * 0002 0004 3ab6 94fa
458 * 0003 0004 3b7b f85a
461 * 0101 000a 0000 0000 3c48 544d 4c3e ASCII part
463 * 0101 0016 0003 0000 6c6b 7364 6a6b 6c6a 676c a56b 3b73 646a 6b6a
464 * 0101 000b 0000 0000 3c2f 4854 4d4c 3e another ASCII part
467 static int incomingim_ch1(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned short channel, struct aim_userinfo_s *userinfo, unsigned char *data, int datalen, unsigned char *cookie)
469 unsigned short type, length;
470 aim_rxcallback_t userfunc;
472 struct aim_incomingim_ch1_args args;
474 memset(&args, 0, sizeof(args));
477 * This used to be done using tlvchains. For performance reasons,
478 * I've changed it to process the TLVs in-place. This avoids lots
479 * of per-IM memory allocations.
481 for (i = 0; i < datalen; ) {
483 type = aimutil_get16(data+i);
486 length = aimutil_get16(data+i);
489 if (type == 0x0002) { /* Message Block */
490 unsigned short wastebits;
491 unsigned char *msgblock;
492 int j = 0, y = 0, z = 0;
497 * Extracting the message from the unknown cruft.
499 * This is a bit messy, and I'm not really qualified,
500 * even as the author, to comment on it. At least
501 * its not as bad as a while loop shooting into
504 * "Do you believe in magic?"
508 wastebits = aimutil_get8(msgblock+j++);
509 wastebits = aimutil_get8(msgblock+j++);
511 y = aimutil_get16(msgblock+j);
513 for (z = 0; z < y; z++)
514 wastebits = aimutil_get8(msgblock+j++);
515 wastebits = aimutil_get8(msgblock+j++);
516 wastebits = aimutil_get8(msgblock+j++);
519 if (args.finlen > sizeof(args.fingerprint))
520 args.finlen = sizeof(args.fingerprint);
521 memcpy(args.fingerprint, msgblock, args.finlen);
523 /* Message string length, including flag words. */
524 args.msglen = aimutil_get16(msgblock+j);
528 args.flag1 = aimutil_get16(msgblock+j);
529 if (args.flag1 == 0x0000)
531 else if (args.flag1 == 0x0002)
532 args.icbmflags |= AIM_IMFLAGS_UNICODE;
533 else if (args.flag1 == 0x0003)
534 args.icbmflags |= AIM_IMFLAGS_ISO_8859_1;
535 else if (args.flag1 == 0xffff)
536 ; /* no encoding (yeep!) */
539 args.flag2 = aimutil_get16(msgblock+j);
540 if (args.flag2 == 0x0000)
541 ; /* standard subencoding? */
542 else if (args.flag2 == 0x000b)
543 args.icbmflags |= AIM_IMFLAGS_SUBENC_MACINTOSH;
544 else if (args.flag2 == 0xffff)
545 ; /* no subencoding */
548 if ( ((args.flag1 != 0x0000) &&
549 (args.flag1 != 0x0002) &&
550 (args.flag1 != 0x0003) &&
551 (args.flag1 != 0xffff)) ||
552 ((args.flag2 != 0x0000) &&
553 (args.flag2 != 0x000b) &&
554 (args.flag2 != 0xffff))) {
555 faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", args.flag1, args.flag2);
558 /* Message string. */
560 if (args.icbmflags & AIM_IMFLAGS_UNICODE) {
561 args.msg = malloc(args.msglen+2);
562 memcpy(args.msg, msgblock+j, args.msglen);
563 args.msg[args.msglen] = '\0'; /* wide NULL */
564 args.msg[args.msglen+1] = '\0';
566 args.msg = malloc(args.msglen+1);
567 memcpy(args.msg, msgblock+j, args.msglen);
568 args.msg[args.msglen] = '\0';
571 } else if (type == 0x0003) { /* Server Ack Requested */
573 args.icbmflags |= AIM_IMFLAGS_ACK;
575 } else if (type == 0x0004) { /* Message is Auto Response */
577 args.icbmflags |= AIM_IMFLAGS_AWAY;
579 } else if ((type == 0x0008) && (length == 0x000c)) { /* I-HAVE-A-REALLY-PURTY-ICON Flag */
581 args.iconstamp = aimutil_get32(data+i+8);
582 args.icbmflags |= AIM_IMFLAGS_HASICON;
584 } else if (type == 0x0009) {
586 args.icbmflags |= AIM_IMFLAGS_BUDDYREQ;
588 } else if (type == 0x0017) {
590 args.extdatalen = length;
591 args.extdata = data+i;
594 faimdprintf(sess, 0, "incomingim_ch1: unknown TLV 0x%04x (len %d)\n", type, length);
601 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
602 ret = userfunc(sess, rx, channel, userinfo, &args);
609 static int incomingim_ch2(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned short channel, struct aim_userinfo_s *userinfo, struct aim_tlvlist_t *tlvlist, unsigned char *cookie)
611 aim_rxcallback_t userfunc;
612 struct aim_tlv_t *block1;
613 struct aim_tlvlist_t *list2;
615 struct aim_incomingim_ch2_args args;
617 memset(&args, 0, sizeof(args));
620 * There's another block of TLVs embedded in the type 5 here.
622 if (!(block1 = aim_gettlv(tlvlist, 0x0005, 1)) || !block1->value) {
623 faimdprintf(sess, 0, "no tlv 0x0005 in rendezvous transaction!\n");
628 * First two bytes represent the status of the connection.
630 * 0 is a request, 2 is an accept
632 args.status = aimutil_get16(block1->value+0);
635 * Next comes the cookie. Should match the ICBM cookie.
637 if (memcmp(block1->value+2, cookie, 8) != 0)
638 faimdprintf(sess, 0, "rend: warning cookies don't match!\n");
641 * The next 16bytes are a capability block so we can
642 * identify what type of rendezvous this is.
644 * Thanks to Eric Warmenhoven <warmenhoven@linux.com> (of GAIM)
645 * for pointing some of this out to me. In fact, a lot of
646 * the client-to-client info comes from the work of the GAIM
647 * developers. Thanks!
649 * Read off one capability string and we should have it ID'd.
652 if ((args.reqclass = aim_getcap(sess, block1->value+2+8, 0x10)) == 0x0000) {
653 faimdprintf(sess, 0, "rend: no ID block\n");
658 * What follows may be TLVs or nothing, depending on the
659 * purpose of the message.
661 * Ack packets for instance have nothing more to them.
663 list2 = aim_readtlvchain(block1->value+2+8+16, block1->length-2-8-16);
665 if (!list2 || ((args.reqclass != AIM_CAPS_IMIMAGE) && !(aim_gettlv(list2, 0x2711, 1)))) {
666 struct aim_msgcookie_t *cook;
669 type = aim_msgcookie_gettype(args.reqclass); /* XXX: fix this shitty code */
671 if ((cook = aim_checkcookie(sess, cookie, type)) == NULL) {
672 faimdprintf(sess, 0, "non-data rendezvous thats not in cache (type %d)\n", type);
673 aim_freetlvchain(&list2);
677 if (cook->type == AIM_COOKIETYPE_OFTGET) {
678 struct aim_filetransfer_priv *ft;
681 int errorcode = -1; /* XXX shouldnt this be 0? */
683 ft = (struct aim_filetransfer_priv *)cook->data;
685 if (args.status != 0x0002) {
687 if (aim_gettlv(list2, 0x000b, 1))
688 errorcode = aim_gettlv16(list2, 0x000b, 1);
690 /* XXX this should make it up to the client, you know.. */
692 faimdprintf(sess, 0, "transfer from %s (%s) for %s cancelled (error code %d)\n", ft->sn, ft->ip, ft->fh.name, errorcode);
693 } /* args.status != 0x0002 */
696 faimdprintf(sess, 0, "no data attached to file transfer\n");
699 } else if (cook->type == AIM_CAPS_VOICE) {
701 faimdprintf(sess, 0, "voice request cancelled\n");
705 faimdprintf(sess, 0, "unknown cookie cache type %d\n", cook->type);
708 aim_freetlvchain(&list2);
714 * The rest of the handling depends on what type it is.
716 if (args.reqclass & AIM_CAPS_BUDDYICON) {
717 struct aim_tlv_t *miscinfo;
720 miscinfo = aim_gettlv(list2, 0x2711, 1);
722 /* aimutil_get32(miscinfo->value+curpos); i don't know what this is */
724 args.info.icon.length = aimutil_get32(miscinfo->value+curpos);
726 args.info.icon.timestamp = aimutil_get32(miscinfo->value+curpos);
728 args.info.icon.icon = malloc(args.info.icon.length);
729 memcpy(args.info.icon.icon, miscinfo->value+curpos, args.info.icon.length);
731 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
732 ret = userfunc(sess, rx, channel, userinfo, &args);
734 free(args.info.icon.icon);
736 } else if (args.reqclass & AIM_CAPS_VOICE) {
737 struct aim_msgcookie_t *cachedcook;
739 faimdprintf(sess, 1, "rend: voice!\n");
741 if(!(cachedcook = (struct aim_msgcookie_t*)calloc(1, sizeof(struct aim_msgcookie_t)))) {
742 aim_freetlvchain(&list2);
746 memcpy(cachedcook->cookie, cookie, 8);
747 cachedcook->type = AIM_COOKIETYPE_OFTVOICE;
748 cachedcook->data = NULL;
750 if (aim_cachecookie(sess, cachedcook) == -1)
751 faimdprintf(sess, 0, "ERROR caching message cookie\n");
753 /* XXX: implement all this */
755 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
756 ret = userfunc(sess, rx, channel, userinfo, &args);
758 } else if (args.reqclass & AIM_CAPS_IMIMAGE) {
760 struct aim_directim_priv *priv;
762 memset(ip, 0, sizeof(ip));
764 if (aim_gettlv(list2, 0x0003, 1) && aim_gettlv(list2, 0x0005, 1)) {
765 struct aim_tlv_t *iptlv, *porttlv;
767 iptlv = aim_gettlv(list2, 0x0003, 1);
768 porttlv = aim_gettlv(list2, 0x0005, 1);
770 snprintf(ip, 30, "%d.%d.%d.%d:%d",
771 aimutil_get8(iptlv->value+0),
772 aimutil_get8(iptlv->value+1),
773 aimutil_get8(iptlv->value+2),
774 aimutil_get8(iptlv->value+3),
775 4443 /*aimutil_get16(porttlv->value)*/);
778 faimdprintf(sess, 1, "rend: directIM request from %s (%s)\n",
782 * XXX: there are a couple of different request packets for
786 args.info.directim = priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)); /* XXX error */
787 memcpy(priv->ip, ip, sizeof(priv->ip));
788 memcpy(priv->sn, userinfo->sn, sizeof(priv->sn));
789 memcpy(priv->cookie, cookie, sizeof(priv->cookie));
791 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
792 ret = userfunc(sess, rx, channel, userinfo, &args);
794 } else if (args.reqclass & AIM_CAPS_CHAT) {
795 struct aim_tlv_t *miscinfo;
797 miscinfo = aim_gettlv(list2, 0x2711, 1);
798 aim_chat_readroominfo(miscinfo->value, &args.info.chat.roominfo);
800 if (aim_gettlv(list2, 0x000c, 1))
801 args.info.chat.msg = aim_gettlv_str(list2, 0x000c, 1);
803 if (aim_gettlv(list2, 0x000d, 1))
804 args.info.chat.encoding = aim_gettlv_str(list2, 0x000d, 1);
806 if (aim_gettlv(list2, 0x000e, 1))
807 args.info.chat.lang = aim_gettlv_str(list2, 0x000e, 1);
809 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
810 ret = userfunc(sess, rx, channel, userinfo, &args);
812 free(args.info.chat.roominfo.name);
813 free(args.info.chat.msg);
814 free(args.info.chat.encoding);
815 free(args.info.chat.lang);
817 } else if (args.reqclass & AIM_CAPS_GETFILE) {
819 struct aim_msgcookie_t *cachedcook;
820 struct aim_tlv_t *miscinfo;
821 struct aim_tlv_t *iptlv, *porttlv;
825 if (!(cachedcook = calloc(1, sizeof(struct aim_msgcookie_t)))) {
826 aim_freetlvchain(&list2);
830 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) ||
831 !(iptlv = aim_gettlv(list2, 0x0003, 1)) ||
832 !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
834 faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
835 aim_cookie_free(sess, cachedcook);
836 aim_freetlvchain(&list2);
841 snprintf(ip, 30, "%d.%d.%d.%d:%d",
842 aimutil_get8(iptlv->value+0),
843 aimutil_get8(iptlv->value+1),
844 aimutil_get8(iptlv->value+2),
845 aimutil_get8(iptlv->value+3),
846 aimutil_get16(porttlv->value));
848 faimdprintf(sess, 0, "rend: file get request from %s (%s)\n", userinfo->sn, ip);
850 args.info.getfile.ip = ip;
851 args.info.getfile.cookie = cookie;
853 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
854 ret = userfunc(sess, rx, channel, userinfo, &args);
856 } else if (args.reqclass & AIM_CAPS_SENDFILE) {
859 struct aim_msgcookie_t *cachedcook;
860 struct aim_tlv_t *miscinfo;
861 struct aim_tlv_t *iptlv, *porttlv;
865 if (!(cachedcook = calloc(1, sizeof(struct aim_msgcookie_t)))) {
866 aim_freetlvchain(&list2);
870 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) ||
871 !(iptlv = aim_gettlv(list2, 0x0003, 1)) ||
872 !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
874 faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
875 aim_cookie_free(sess, cachedcook);
876 aim_freetlvchain(&list2);
881 snprintf(ip, 30, "%d.%d.%d.%d:%d",
882 aimutil_get8(iptlv->value+0),
883 aimutil_get8(iptlv->value+1),
884 aimutil_get8(iptlv->value+2),
885 aimutil_get8(iptlv->value+3),
886 aimutil_get16(porttlv->value));
888 if (aim_gettlv(list2, 0x000c, 1))
889 desc = aim_gettlv_str(list2, 0x000c, 1);
891 faimdprintf(sess, 0, "rend: file transfer request from %s: %s (%s)\n",
892 userinfo->sn, desc, ip);
894 memcpy(cachedcook->cookie, cookie, 8);
896 ft = malloc(sizeof(struct aim_filetransfer_priv)); /* XXX */
897 strncpy(ft->sn, userinfo.sn, sizeof(ft->sn));
898 strncpy(ft->ip, ip, sizeof(ft->ip));
899 strncpy(ft->fh.name, miscinfo->value+8, sizeof(ft->fh.name));
900 cachedcook->type = AIM_COOKIETYPE_OFTSEND;
901 cachedcook->data = ft;
903 if (aim_cachecookie(sess, cachedcook) == -1)
904 faimdprintf(sess, 0, "ERROR caching message cookie\n");
906 aim_accepttransfer(sess, rx->conn, ft->sn, cookie, AIM_CAPS_SENDFILE);
911 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
912 ret = userfunc(sess, rx, channel, userinfo, &args);
916 faimdprintf(sess, 0, "rend: unknown rendezvous 0x%04x\n", args.reqclass);
918 aim_freetlvchain(&list2);
924 * It can easily be said that parsing ICBMs is THE single
925 * most difficult thing to do in the in AIM protocol. In
926 * fact, I think I just did say that.
928 * Below is the best damned solution I've come up with
929 * over the past sixteen months of battling with it. This
930 * can parse both away and normal messages from every client
931 * I have access to. Its not fast, its not clean. But it works.
934 static int incomingim(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
937 unsigned char cookie[8];
939 struct aim_userinfo_s userinfo;
941 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
944 * Read ICBM Cookie. And throw away.
946 for (i = 0; i < 8; i++)
947 cookie[i] = aimutil_get8(data+i);
952 * Channel 0x0001 is the message channel. There are
953 * other channels for things called "rendevous"
954 * which represent chat and some of the other new
955 * features of AIM2/3/3.5.
957 * Channel 0x0002 is the Rendevous channel, which
958 * is where Chat Invitiations and various client-client
959 * connection negotiations come from.
962 channel = aimutil_get16(data+i);
966 * Technically Channel 3 in chat could be done here too.
968 if ((channel != 0x01) && (channel != 0x02)) {
969 faimdprintf(sess, 0, "icbm: ICBM received on an unsupported channel. Ignoring.\n (chan = %04x)", channel);
974 * Extract the standard user info block.
976 * Note that although this contains TLVs that appear contiguous
977 * with the TLVs read below, they are two different pieces. The
978 * userinfo block contains the number of TLVs that contain user
979 * information, the rest are not even though there is no seperation.
980 * aim_extractuserinfo() returns the number of bytes used by the
981 * userinfo tlvs, so you can start reading the rest of them right
984 * That also means that TLV types can be duplicated between the
985 * userinfo block and the rest of the message, however there should
986 * never be two TLVs of the same type in one block.
989 i += aim_extractuserinfo(sess, data+i, &userinfo);
992 * From here on, its depends on what channel we're on.
994 * Technically all channels have a TLV list have this, however,
995 * for the common channel 1 case, in-place parsing is used for
996 * performance reasons (less memory allocation).
1000 ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, data+i, datalen-i, cookie);
1002 } else if (channel == 0x0002) {
1003 struct aim_tlvlist_t *tlvlist;
1006 * Read block of TLVs (not including the userinfo data). All
1007 * further data is derived from what is parsed here.
1009 tlvlist = aim_readtlvchain(data+i, datalen-i);
1011 ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
1014 * Free up the TLV chain.
1016 aim_freetlvchain(&tlvlist);
1024 * AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
1025 * AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
1026 * AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
1029 faim_export int aim_denytransfer(struct aim_session_t *sess,
1030 struct aim_conn_t *conn,
1033 unsigned short code)
1035 struct command_tx_struct *newpacket;
1038 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(sender)+6)))
1041 newpacket->lock = 1;
1043 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x000b, 0x0000, sess->snac_nextid++);
1044 for (i = 0; i < 8; i++)
1045 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
1046 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1047 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sender));
1048 curbyte += aimutil_putstr(newpacket->data+curbyte, sender, strlen(sender));
1049 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0003, code);
1051 newpacket->lock = 0;
1052 aim_tx_enqueue(sess, newpacket);
1058 * aim_reqicbmparaminfo()
1060 * Request ICBM parameter information.
1063 faim_export unsigned long aim_reqicbmparams(struct aim_session_t *sess, struct aim_conn_t *conn)
1065 return aim_genericreq_n(sess, conn, 0x0004, 0x0004);
1071 faim_export unsigned long aim_seticbmparam(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_icbmparameters *params)
1073 struct command_tx_struct *newpacket;
1076 if (!sess || !conn || !params)
1079 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+16)))
1082 newpacket->lock = 1;
1084 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0002, 0x0000, sess->snac_nextid++);
1086 /* This is read-only (in Parameter Reply). Must be set to zero here. */
1087 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1089 /* These are all read-write */
1090 curbyte += aimutil_put32(newpacket->data+curbyte, params->flags);
1091 curbyte += aimutil_put16(newpacket->data+curbyte, params->maxmsglen);
1092 curbyte += aimutil_put16(newpacket->data+curbyte, params->maxsenderwarn);
1093 curbyte += aimutil_put16(newpacket->data+curbyte, params->maxrecverwarn);
1094 curbyte += aimutil_put32(newpacket->data+curbyte, params->minmsginterval);
1096 newpacket->lock = 0;
1097 aim_tx_enqueue(sess, newpacket);
1102 static int paraminfo(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
1104 struct aim_icbmparameters params;
1105 aim_rxcallback_t userfunc;
1108 params.maxchan = aimutil_get16(data+i);
1111 params.flags = aimutil_get32(data+i);
1114 params.maxmsglen = aimutil_get16(data+i);
1117 params.maxsenderwarn = aimutil_get16(data+i);
1120 params.maxrecverwarn = aimutil_get16(data+i);
1123 params.minmsginterval = aimutil_get32(data+i);
1126 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1127 return userfunc(sess, rx, ¶ms);
1132 static int missedcall(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
1135 aim_rxcallback_t userfunc;
1136 unsigned short channel, nummissed, reason;
1137 struct aim_userinfo_s userinfo;
1139 for (i = 0; i < datalen; ) {
1142 channel = aimutil_get16(data+i);
1145 /* Extract the standard user info block. */
1146 i += aim_extractuserinfo(sess, data+i, &userinfo);
1148 nummissed = aimutil_get16(data+i);
1151 reason = aimutil_get16(data+i);
1154 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1155 ret = userfunc(sess, rx, channel, &userinfo, nummissed, reason);
1161 static int msgack(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
1163 aim_rxcallback_t userfunc;
1165 unsigned char ck[8];
1166 unsigned short type;
1168 unsigned char snlen;
1170 memcpy(ck, data, 8);
1173 type = aimutil_get16(data+i);
1176 snlen = aimutil_get8(data+i);
1179 memset(sn, 0, sizeof(sn));
1180 strncpy(sn, (char *)data+i, snlen);
1182 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1183 return userfunc(sess, rx, type, sn);
1188 static int snachandler(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
1191 if (snac->subtype == 0x0005)
1192 return paraminfo(sess, mod, rx, snac, data, datalen);
1193 else if (snac->subtype == 0x0006)
1194 return outgoingim(sess, mod, rx, snac, data, datalen);
1195 else if (snac->subtype == 0x0007)
1196 return incomingim(sess, mod, rx, snac, data, datalen);
1197 else if (snac->subtype == 0x000a)
1198 return missedcall(sess, mod, rx, snac, data, datalen);
1199 else if (snac->subtype == 0x000c)
1200 return msgack(sess, mod, rx, snac, data, datalen);
1205 faim_internal int msg_modfirst(struct aim_session_t *sess, aim_module_t *mod)
1208 mod->family = 0x0004;
1209 mod->version = 0x0000;
1211 strncpy(mod->name, "messaging", sizeof(mod->name));
1212 mod->snachandler = snachandler;