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.iconchecksum = aimutil_get32(data+i);
582 args.iconlength = aimutil_get32(data+i+4);
583 args.iconstamp = aimutil_get32(data+i+8);
584 args.icbmflags |= AIM_IMFLAGS_HASICON;
586 } else if (type == 0x0009) {
588 args.icbmflags |= AIM_IMFLAGS_BUDDYREQ;
590 } else if (type == 0x0017) {
592 args.extdatalen = length;
593 args.extdata = data+i;
596 faimdprintf(sess, 0, "incomingim_ch1: unknown TLV 0x%04x (len %d)\n", type, length);
603 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
604 ret = userfunc(sess, rx, channel, userinfo, &args);
611 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)
613 aim_rxcallback_t userfunc;
614 struct aim_tlv_t *block1;
615 struct aim_tlvlist_t *list2;
617 struct aim_incomingim_ch2_args args;
619 memset(&args, 0, sizeof(args));
622 * There's another block of TLVs embedded in the type 5 here.
624 if (!(block1 = aim_gettlv(tlvlist, 0x0005, 1)) || !block1->value) {
625 faimdprintf(sess, 0, "no tlv 0x0005 in rendezvous transaction!\n");
630 * First two bytes represent the status of the connection.
632 * 0 is a request, 2 is an accept
634 args.status = aimutil_get16(block1->value+0);
637 * Next comes the cookie. Should match the ICBM cookie.
639 if (memcmp(block1->value+2, cookie, 8) != 0)
640 faimdprintf(sess, 0, "rend: warning cookies don't match!\n");
643 * The next 16bytes are a capability block so we can
644 * identify what type of rendezvous this is.
646 * Thanks to Eric Warmenhoven <warmenhoven@linux.com> (of GAIM)
647 * for pointing some of this out to me. In fact, a lot of
648 * the client-to-client info comes from the work of the GAIM
649 * developers. Thanks!
651 * Read off one capability string and we should have it ID'd.
654 if ((args.reqclass = aim_getcap(sess, block1->value+2+8, 0x10)) == 0x0000) {
655 faimdprintf(sess, 0, "rend: no ID block\n");
660 * What follows may be TLVs or nothing, depending on the
661 * purpose of the message.
663 * Ack packets for instance have nothing more to them.
665 list2 = aim_readtlvchain(block1->value+2+8+16, block1->length-2-8-16);
667 if (!list2 || ((args.reqclass != AIM_CAPS_IMIMAGE) && !(aim_gettlv(list2, 0x2711, 1)))) {
668 struct aim_msgcookie_t *cook;
671 type = aim_msgcookie_gettype(args.reqclass); /* XXX: fix this shitty code */
673 if ((cook = aim_checkcookie(sess, cookie, type)) == NULL) {
674 faimdprintf(sess, 0, "non-data rendezvous thats not in cache (type %d)\n", type);
675 aim_freetlvchain(&list2);
679 if (cook->type == AIM_COOKIETYPE_OFTGET) {
680 struct aim_filetransfer_priv *ft;
683 int errorcode = -1; /* XXX shouldnt this be 0? */
685 ft = (struct aim_filetransfer_priv *)cook->data;
687 if (args.status != 0x0002) {
689 if (aim_gettlv(list2, 0x000b, 1))
690 errorcode = aim_gettlv16(list2, 0x000b, 1);
692 /* XXX this should make it up to the client, you know.. */
694 faimdprintf(sess, 0, "transfer from %s (%s) for %s cancelled (error code %d)\n", ft->sn, ft->ip, ft->fh.name, errorcode);
695 } /* args.status != 0x0002 */
698 faimdprintf(sess, 0, "no data attached to file transfer\n");
701 } else if (cook->type == AIM_CAPS_VOICE) {
703 faimdprintf(sess, 0, "voice request cancelled\n");
707 faimdprintf(sess, 0, "unknown cookie cache type %d\n", cook->type);
710 aim_freetlvchain(&list2);
716 * The rest of the handling depends on what type it is.
718 if (args.reqclass & AIM_CAPS_BUDDYICON) {
719 struct aim_tlv_t *miscinfo;
722 miscinfo = aim_gettlv(list2, 0x2711, 1);
724 args.info.icon.checksum = aimutil_get32(miscinfo->value+curpos);
726 args.info.icon.length = aimutil_get32(miscinfo->value+curpos);
728 args.info.icon.timestamp = aimutil_get32(miscinfo->value+curpos);
730 args.info.icon.icon = malloc(args.info.icon.length);
731 memcpy(args.info.icon.icon, miscinfo->value+curpos, args.info.icon.length);
733 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
734 ret = userfunc(sess, rx, channel, userinfo, &args);
736 free(args.info.icon.icon);
738 } else if (args.reqclass & AIM_CAPS_VOICE) {
739 struct aim_msgcookie_t *cachedcook;
741 faimdprintf(sess, 1, "rend: voice!\n");
743 if(!(cachedcook = (struct aim_msgcookie_t*)calloc(1, sizeof(struct aim_msgcookie_t)))) {
744 aim_freetlvchain(&list2);
748 memcpy(cachedcook->cookie, cookie, 8);
749 cachedcook->type = AIM_COOKIETYPE_OFTVOICE;
750 cachedcook->data = NULL;
752 if (aim_cachecookie(sess, cachedcook) == -1)
753 faimdprintf(sess, 0, "ERROR caching message cookie\n");
755 /* XXX: implement all this */
757 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
758 ret = userfunc(sess, rx, channel, userinfo, &args);
760 } else if (args.reqclass & AIM_CAPS_IMIMAGE) {
762 struct aim_directim_priv *priv;
764 memset(ip, 0, sizeof(ip));
766 if (aim_gettlv(list2, 0x0003, 1) && aim_gettlv(list2, 0x0005, 1)) {
767 struct aim_tlv_t *iptlv, *porttlv;
769 iptlv = aim_gettlv(list2, 0x0003, 1);
770 porttlv = aim_gettlv(list2, 0x0005, 1);
772 snprintf(ip, 30, "%d.%d.%d.%d:%d",
773 aimutil_get8(iptlv->value+0),
774 aimutil_get8(iptlv->value+1),
775 aimutil_get8(iptlv->value+2),
776 aimutil_get8(iptlv->value+3),
777 4443 /*aimutil_get16(porttlv->value)*/);
780 faimdprintf(sess, 1, "rend: directIM request from %s (%s)\n",
784 * XXX: there are a couple of different request packets for
788 args.info.directim = priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)); /* XXX error */
789 memcpy(priv->ip, ip, sizeof(priv->ip));
790 memcpy(priv->sn, userinfo->sn, sizeof(priv->sn));
791 memcpy(priv->cookie, cookie, sizeof(priv->cookie));
793 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
794 ret = userfunc(sess, rx, channel, userinfo, &args);
796 } else if (args.reqclass & AIM_CAPS_CHAT) {
797 struct aim_tlv_t *miscinfo;
799 miscinfo = aim_gettlv(list2, 0x2711, 1);
800 aim_chat_readroominfo(miscinfo->value, &args.info.chat.roominfo);
802 if (aim_gettlv(list2, 0x000c, 1))
803 args.info.chat.msg = aim_gettlv_str(list2, 0x000c, 1);
805 if (aim_gettlv(list2, 0x000d, 1))
806 args.info.chat.encoding = aim_gettlv_str(list2, 0x000d, 1);
808 if (aim_gettlv(list2, 0x000e, 1))
809 args.info.chat.lang = aim_gettlv_str(list2, 0x000e, 1);
811 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
812 ret = userfunc(sess, rx, channel, userinfo, &args);
814 free(args.info.chat.roominfo.name);
815 free(args.info.chat.msg);
816 free(args.info.chat.encoding);
817 free(args.info.chat.lang);
819 } else if (args.reqclass & AIM_CAPS_GETFILE) {
821 struct aim_msgcookie_t *cachedcook;
822 struct aim_tlv_t *miscinfo;
823 struct aim_tlv_t *iptlv, *porttlv;
827 if (!(cachedcook = calloc(1, sizeof(struct aim_msgcookie_t)))) {
828 aim_freetlvchain(&list2);
832 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) ||
833 !(iptlv = aim_gettlv(list2, 0x0003, 1)) ||
834 !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
836 faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
837 aim_cookie_free(sess, cachedcook);
838 aim_freetlvchain(&list2);
843 snprintf(ip, 30, "%d.%d.%d.%d:%d",
844 aimutil_get8(iptlv->value+0),
845 aimutil_get8(iptlv->value+1),
846 aimutil_get8(iptlv->value+2),
847 aimutil_get8(iptlv->value+3),
848 aimutil_get16(porttlv->value));
850 faimdprintf(sess, 0, "rend: file get request from %s (%s)\n", userinfo->sn, ip);
852 args.info.getfile.ip = ip;
853 args.info.getfile.cookie = cookie;
855 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
856 ret = userfunc(sess, rx, channel, userinfo, &args);
858 } else if (args.reqclass & AIM_CAPS_SENDFILE) {
861 struct aim_msgcookie_t *cachedcook;
862 struct aim_tlv_t *miscinfo;
863 struct aim_tlv_t *iptlv, *porttlv;
867 if (!(cachedcook = calloc(1, sizeof(struct aim_msgcookie_t)))) {
868 aim_freetlvchain(&list2);
872 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) ||
873 !(iptlv = aim_gettlv(list2, 0x0003, 1)) ||
874 !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
876 faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
877 aim_cookie_free(sess, cachedcook);
878 aim_freetlvchain(&list2);
883 snprintf(ip, 30, "%d.%d.%d.%d:%d",
884 aimutil_get8(iptlv->value+0),
885 aimutil_get8(iptlv->value+1),
886 aimutil_get8(iptlv->value+2),
887 aimutil_get8(iptlv->value+3),
888 aimutil_get16(porttlv->value));
890 if (aim_gettlv(list2, 0x000c, 1))
891 desc = aim_gettlv_str(list2, 0x000c, 1);
893 faimdprintf(sess, 0, "rend: file transfer request from %s: %s (%s)\n",
894 userinfo->sn, desc, ip);
896 memcpy(cachedcook->cookie, cookie, 8);
898 ft = malloc(sizeof(struct aim_filetransfer_priv)); /* XXX */
899 strncpy(ft->sn, userinfo.sn, sizeof(ft->sn));
900 strncpy(ft->ip, ip, sizeof(ft->ip));
901 strncpy(ft->fh.name, miscinfo->value+8, sizeof(ft->fh.name));
902 cachedcook->type = AIM_COOKIETYPE_OFTSEND;
903 cachedcook->data = ft;
905 if (aim_cachecookie(sess, cachedcook) == -1)
906 faimdprintf(sess, 0, "ERROR caching message cookie\n");
908 aim_accepttransfer(sess, rx->conn, ft->sn, cookie, AIM_CAPS_SENDFILE);
913 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
914 ret = userfunc(sess, rx, channel, userinfo, &args);
918 faimdprintf(sess, 0, "rend: unknown rendezvous 0x%04x\n", args.reqclass);
920 aim_freetlvchain(&list2);
926 * It can easily be said that parsing ICBMs is THE single
927 * most difficult thing to do in the in AIM protocol. In
928 * fact, I think I just did say that.
930 * Below is the best damned solution I've come up with
931 * over the past sixteen months of battling with it. This
932 * can parse both away and normal messages from every client
933 * I have access to. Its not fast, its not clean. But it works.
936 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)
939 unsigned char cookie[8];
941 struct aim_userinfo_s userinfo;
943 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
946 * Read ICBM Cookie. And throw away.
948 for (i = 0; i < 8; i++)
949 cookie[i] = aimutil_get8(data+i);
954 * Channel 0x0001 is the message channel. There are
955 * other channels for things called "rendevous"
956 * which represent chat and some of the other new
957 * features of AIM2/3/3.5.
959 * Channel 0x0002 is the Rendevous channel, which
960 * is where Chat Invitiations and various client-client
961 * connection negotiations come from.
964 channel = aimutil_get16(data+i);
968 * Technically Channel 3 in chat could be done here too.
970 if ((channel != 0x01) && (channel != 0x02)) {
971 faimdprintf(sess, 0, "icbm: ICBM received on an unsupported channel. Ignoring.\n (chan = %04x)", channel);
976 * Extract the standard user info block.
978 * Note that although this contains TLVs that appear contiguous
979 * with the TLVs read below, they are two different pieces. The
980 * userinfo block contains the number of TLVs that contain user
981 * information, the rest are not even though there is no seperation.
982 * aim_extractuserinfo() returns the number of bytes used by the
983 * userinfo tlvs, so you can start reading the rest of them right
986 * That also means that TLV types can be duplicated between the
987 * userinfo block and the rest of the message, however there should
988 * never be two TLVs of the same type in one block.
991 i += aim_extractuserinfo(sess, data+i, &userinfo);
994 * From here on, its depends on what channel we're on.
996 * Technically all channels have a TLV list have this, however,
997 * for the common channel 1 case, in-place parsing is used for
998 * performance reasons (less memory allocation).
1002 ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, data+i, datalen-i, cookie);
1004 } else if (channel == 0x0002) {
1005 struct aim_tlvlist_t *tlvlist;
1008 * Read block of TLVs (not including the userinfo data). All
1009 * further data is derived from what is parsed here.
1011 tlvlist = aim_readtlvchain(data+i, datalen-i);
1013 ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
1016 * Free up the TLV chain.
1018 aim_freetlvchain(&tlvlist);
1026 * AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
1027 * AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
1028 * AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
1031 faim_export int aim_denytransfer(struct aim_session_t *sess,
1032 struct aim_conn_t *conn,
1035 unsigned short code)
1037 struct command_tx_struct *newpacket;
1040 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(sender)+6)))
1043 newpacket->lock = 1;
1045 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x000b, 0x0000, sess->snac_nextid++);
1046 for (i = 0; i < 8; i++)
1047 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
1048 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1049 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sender));
1050 curbyte += aimutil_putstr(newpacket->data+curbyte, sender, strlen(sender));
1051 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0003, code);
1053 newpacket->lock = 0;
1054 aim_tx_enqueue(sess, newpacket);
1060 * aim_reqicbmparaminfo()
1062 * Request ICBM parameter information.
1065 faim_export unsigned long aim_reqicbmparams(struct aim_session_t *sess, struct aim_conn_t *conn)
1067 return aim_genericreq_n(sess, conn, 0x0004, 0x0004);
1073 faim_export unsigned long aim_seticbmparam(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_icbmparameters *params)
1075 struct command_tx_struct *newpacket;
1078 if (!sess || !conn || !params)
1081 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+16)))
1084 newpacket->lock = 1;
1086 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0002, 0x0000, sess->snac_nextid++);
1088 /* This is read-only (in Parameter Reply). Must be set to zero here. */
1089 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1091 /* These are all read-write */
1092 curbyte += aimutil_put32(newpacket->data+curbyte, params->flags);
1093 curbyte += aimutil_put16(newpacket->data+curbyte, params->maxmsglen);
1094 curbyte += aimutil_put16(newpacket->data+curbyte, params->maxsenderwarn);
1095 curbyte += aimutil_put16(newpacket->data+curbyte, params->maxrecverwarn);
1096 curbyte += aimutil_put32(newpacket->data+curbyte, params->minmsginterval);
1098 newpacket->lock = 0;
1099 aim_tx_enqueue(sess, newpacket);
1104 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)
1106 struct aim_icbmparameters params;
1107 aim_rxcallback_t userfunc;
1110 params.maxchan = aimutil_get16(data+i);
1113 params.flags = aimutil_get32(data+i);
1116 params.maxmsglen = aimutil_get16(data+i);
1119 params.maxsenderwarn = aimutil_get16(data+i);
1122 params.maxrecverwarn = aimutil_get16(data+i);
1125 params.minmsginterval = aimutil_get32(data+i);
1128 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1129 return userfunc(sess, rx, ¶ms);
1134 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)
1137 aim_rxcallback_t userfunc;
1138 unsigned short channel, nummissed, reason;
1139 struct aim_userinfo_s userinfo;
1141 for (i = 0; i < datalen; ) {
1144 channel = aimutil_get16(data+i);
1147 /* Extract the standard user info block. */
1148 i += aim_extractuserinfo(sess, data+i, &userinfo);
1150 nummissed = aimutil_get16(data+i);
1153 reason = aimutil_get16(data+i);
1156 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1157 ret = userfunc(sess, rx, channel, &userinfo, nummissed, reason);
1163 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)
1165 aim_rxcallback_t userfunc;
1167 unsigned char ck[8];
1168 unsigned short type;
1170 unsigned char snlen;
1172 memcpy(ck, data, 8);
1175 type = aimutil_get16(data+i);
1178 snlen = aimutil_get8(data+i);
1181 memset(sn, 0, sizeof(sn));
1182 strncpy(sn, (char *)data+i, snlen);
1184 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1185 return userfunc(sess, rx, type, sn);
1190 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)
1193 if (snac->subtype == 0x0005)
1194 return paraminfo(sess, mod, rx, snac, data, datalen);
1195 else if (snac->subtype == 0x0006)
1196 return outgoingim(sess, mod, rx, snac, data, datalen);
1197 else if (snac->subtype == 0x0007)
1198 return incomingim(sess, mod, rx, snac, data, datalen);
1199 else if (snac->subtype == 0x000a)
1200 return missedcall(sess, mod, rx, snac, data, datalen);
1201 else if (snac->subtype == 0x000c)
1202 return msgack(sess, mod, rx, snac, data, datalen);
1207 faim_internal int msg_modfirst(struct aim_session_t *sess, aim_module_t *mod)
1210 mod->family = 0x0004;
1211 mod->version = 0x0000;
1213 strncpy(mod->name, "messaging", sizeof(mod->name));
1214 mod->snachandler = snachandler;