4 * The routines for sending/receiving Instant Messages.
11 * Send an ICBM (instant message).
15 * AIM_IMFLAGS_AWAY -- Marks the message as an autoresponse
16 * AIM_IMFLAGS_ACK -- Requests that the server send an ack
17 * when the message is received (of type 0x0004/0x000c)
20 u_long aim_send_im(struct aim_session_t *sess,
21 struct aim_conn_t *conn,
22 char *destsn, u_int flags, char *msg)
26 struct command_tx_struct newpacket;
28 newpacket.lock = 1; /* lock struct */
29 newpacket.type = 0x02; /* IMs are always family 0x02 */
31 newpacket.conn = conn;
33 newpacket.conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
36 * Its simplest to set this arbitrarily large and waste
37 * space. Precalculating is costly here.
39 newpacket.commandlen = 1152;
41 newpacket.data = (u_char *) calloc(1, newpacket.commandlen);
44 curbyte += aim_putsnac(newpacket.data+curbyte,
45 0x0004, 0x0006, 0x0000, sess->snac_nextid);
48 * Generate a random message cookie
50 * We could cache these like we do SNAC IDs. (In fact, it
51 * might be a good idea.) In the message error functions,
52 * the 8byte message cookie is returned as well as the
57 curbyte += aimutil_put8(newpacket.data+curbyte, (u_char) random());
62 curbyte += aimutil_put16(newpacket.data+curbyte,0x0001);
65 * Destination SN (prepended with byte length)
67 curbyte += aimutil_put8(newpacket.data+curbyte,strlen(destsn));
68 curbyte += aimutil_putstr(newpacket.data+curbyte, destsn, strlen(destsn));
73 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0002);
74 curbyte += aimutil_put16(newpacket.data+curbyte, strlen(msg) + 0x0d);
79 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0501);
80 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0001);
81 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0101);
82 curbyte += aimutil_put8 (newpacket.data+curbyte, 0x01);
85 * Message block length.
87 curbyte += aimutil_put16(newpacket.data+curbyte, strlen(msg) + 0x04);
92 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
93 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
96 * Message. Not terminated.
98 curbyte += aimutil_putstr(newpacket.data+curbyte,msg, strlen(msg));
101 * Set the Request Acknowledge flag.
103 if (flags & AIM_IMFLAGS_ACK)
105 curbyte += aimutil_put16(newpacket.data+curbyte,0x0003);
106 curbyte += aimutil_put16(newpacket.data+curbyte,0x0000);
110 * Set the Autoresponse flag.
112 if (flags & AIM_IMFLAGS_AWAY)
114 curbyte += aimutil_put16(newpacket.data+curbyte,0x0004);
115 curbyte += aimutil_put16(newpacket.data+curbyte,0x0000);
118 newpacket.commandlen = curbyte;
120 aim_tx_enqueue(sess, &newpacket);
122 #ifdef USE_SNAC_FOR_IMS
124 struct aim_snac_t snac;
126 snac.id = sess->snac_nextid;
127 snac.family = 0x0004;
131 snac.data = malloc(strlen(destsn)+1);
132 memcpy(snac.data, destsn, strlen(destsn)+1);
134 aim_newsnac(sess, &snac);
137 aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
140 return (sess->snac_nextid++);
144 * It can easily be said that parsing ICBMs is THE single
145 * most difficult thing to do in the in AIM protocol. In
146 * fact, I think I just did say that.
148 * Below is the best damned solution I've come up with
149 * over the past sixteen months of battling with it. This
150 * can parse both away and normal messages from every client
151 * I have access to. Its not fast, its not clean. But it works.
153 * We should also support at least minimal parsing of
154 * Channel 2, so that we can at least know the name of the
155 * room we're invited to, but obviously can't attend...
158 int aim_parse_incoming_im_middle(struct aim_session_t *sess,
159 struct command_rx_struct *command)
162 rxcallback_t userfunc = NULL;
165 struct aim_tlvlist_t *tlvlist;
166 struct aim_userinfo_s userinfo;
169 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
171 i = 10; /* Skip SNAC header */
174 * Read ICBM Cookie. And throw away.
176 for (z=0; z<8; z++,i++)
177 cookie[z] = command->data[i];
182 * Channel 0x0001 is the message channel. There are
183 * other channels for things called "rendevous"
184 * which represent chat and some of the other new
185 * features of AIM2/3/3.5.
187 * Channel 0x0002 is the Rendevous channel, which
188 * is where Chat Invitiations come from.
191 channel = aimutil_get16(command->data+i);
197 if ((channel != 0x01) && (channel != 0x02))
199 printf("faim: icbm: ICBM received on an unsupported channel. Ignoring.\n (chan = %04x)", channel);
204 * Source screen name.
206 memcpy(userinfo.sn, command->data+i+1, (int)command->data[i]);
207 userinfo.sn[(int)command->data[i]] = '\0';
208 i += 1 + (int)command->data[i];
213 userinfo.warnlevel = aimutil_get16(command->data+i); /* guess */
217 * Number of TLVs that follow. Not needed.
219 wastebits = aimutil_get16(command->data+i);
223 * Read block of TLVs. All further data is derived
224 * from what is parsed here.
226 tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
229 * From here on, its depends on what channel we're on.
233 u_int j = 0, y = 0, z = 0;
236 struct aim_tlv_t *msgblocktlv, *tmptlv;
241 * Check Autoresponse status. If it is an autoresponse,
242 * it will contain a second type 0x0004 TLV, with zero length.
244 if (aim_gettlv(tlvlist, 0x0004, 2))
245 icbmflags |= AIM_IMFLAGS_AWAY;
248 * Check Ack Request status.
250 if (aim_gettlv(tlvlist, 0x0003, 2))
251 icbmflags |= AIM_IMFLAGS_ACK;
254 * Extract the various pieces of the userinfo struct.
257 if ((tmptlv = aim_gettlv(tlvlist, 0x0001, 1)))
258 userinfo.class = aimutil_get16(tmptlv->value);
259 /* Member-since date. */
260 if ((tmptlv = aim_gettlv(tlvlist, 0x0002, 1)))
262 /* If this is larger than 4, its probably the message block, skip */
263 if (tmptlv->length <= 4)
264 userinfo.membersince = aimutil_get32(tmptlv->value);
267 if ((tmptlv = aim_gettlv(tlvlist, 0x0003, 1)))
268 userinfo.onlinesince = aimutil_get32(tmptlv->value);
270 if ((tmptlv = aim_gettlv(tlvlist, 0x0004, 1)))
271 userinfo.idletime = aimutil_get16(tmptlv->value);
272 /* Session Length (AIM) */
273 if ((tmptlv = aim_gettlv(tlvlist, 0x000f, 1)))
274 userinfo.sessionlen = aimutil_get16(tmptlv->value);
275 /* Session Length (AOL) */
276 if ((tmptlv = aim_gettlv(tlvlist, 0x0010, 1)))
277 userinfo.sessionlen = aimutil_get16(tmptlv->value);
282 * XXX: Will the msgblock always be the second 0x0002?
284 msgblocktlv = aim_gettlv(tlvlist, 0x0002, 1);
287 printf("faim: icbm: major error! no message block TLV found!\n");
288 aim_freetlvchain(&tlvlist);
292 * Extracting the message from the unknown cruft.
294 * This is a bit messy, and I'm not really qualified,
295 * even as the author, to comment on it. At least
296 * its not as bad as a while loop shooting into infinity.
298 * "Do you believe in magic?"
301 msgblock = msgblocktlv->value;
304 wastebits = aimutil_get8(msgblock+j++);
305 wastebits = aimutil_get8(msgblock+j++);
307 y = aimutil_get16(msgblock+j);
309 for (z = 0; z < y; z++)
310 wastebits = aimutil_get8(msgblock+j++);
311 wastebits = aimutil_get8(msgblock+j++);
312 wastebits = aimutil_get8(msgblock+j++);
315 * Message string length, including flag words.
317 i = aimutil_get16(msgblock+j);
323 * Its rumored that these can kick in some funky
324 * 16bit-wide char stuff that used to really kill
325 * libfaim. Hopefully the latter is no longer true.
327 * Though someone should investiagte the former.
330 flag1 = aimutil_get16(msgblock+j);
332 flag2 = aimutil_get16(msgblock+j);
336 printf("faim: icbm: **warning: encoding flags are being used! {%04x, %04x}\n", flag1, flag2);
342 msg = (char *)malloc(i+1);
343 memcpy(msg, msgblock+j, i);
349 userfunc = aim_callhandler(command->conn, 0x0004, 0x0007);
351 i = userfunc(sess, command, channel, &userinfo, msg, icbmflags, flag1, flag2);
357 else if (channel == 0x0002)
360 struct aim_tlv_t *block1;
361 struct aim_tlvlist_t *list2;
362 struct aim_tlv_t *tmptlv;
366 if ((tmptlv = aim_gettlv(tlvlist, 0x0001, 1)))
367 userinfo.class = aimutil_get16(tmptlv->value);
369 if ((tmptlv = aim_gettlv(tlvlist, 0x0003, 1)))
370 userinfo.onlinesince = aimutil_get32(tmptlv->value);
372 if ((tmptlv = aim_gettlv(tlvlist, 0x0004, 1)))
373 userinfo.idletime = aimutil_get16(tmptlv->value);
374 /* Session Length (AIM) */
375 if ((tmptlv = aim_gettlv(tlvlist, 0x000f, 1)))
376 userinfo.sessionlen = aimutil_get16(tmptlv->value);
377 /* Session Length (AOL) */
378 if ((tmptlv = aim_gettlv(tlvlist, 0x0010, 1)))
379 userinfo.sessionlen = aimutil_get16(tmptlv->value);
382 * There's another block of TLVs embedded in the type 5 here.
384 block1 = aim_gettlv(tlvlist, 0x0005, 1);
386 return 1; /* major problem */
388 a = 0x1a; /* skip -- not sure what this information is! */
391 * XXX: Ignore if there's no data, only cookie information.
393 * Its probably just an accepted invitation or something.
396 if (block1->length <= 0x1a)
398 aim_freetlvchain(&tlvlist);
402 list2 = aim_readtlvchain(block1->value+a, block1->length-a);
404 if (aim_gettlv(list2, 0x0004, 1) /* start connection */ ||
405 aim_gettlv(list2, 0x000b, 1) /* close conncetion */)
407 rendtype = 1; /* voice request */
412 userfunc = aim_callhandler(command->conn, 0x0004, 0x0007);
424 struct aim_chat_roominfo roominfo;
425 char *msg=NULL,*encoding=NULL,*lang=NULL;
427 rendtype = 0; /* chat invite */
428 if (aim_gettlv(list2, 0x2711, 1))
430 struct aim_tlv_t *nametlv;
432 nametlv = aim_gettlv(list2, 0x2711, 1);
433 aim_chat_readroominfo(nametlv->value, &roominfo);
436 if (aim_gettlv(list2, 0x000c, 1))
437 msg = aim_gettlv_str(list2, 0x000c, 1);
439 if (aim_gettlv(list2, 0x000d, 1))
440 encoding = aim_gettlv_str(list2, 0x000d, 1);
442 if (aim_gettlv(list2, 0x000e, 1))
443 lang = aim_gettlv_str(list2, 0x000e, 1);
448 userfunc = aim_callhandler(command->conn, 0x0004, 0x0007);
457 encoding?encoding+1:NULL,
467 aim_freetlvchain(&list2);
471 * Free up the TLV chain.
473 aim_freetlvchain(&tlvlist);
480 * Not real sure what this does, nor does anyone I've talk to.
482 * Didn't use to send it. But now I think it might be a good
486 u_long aim_seticbmparam(struct aim_session_t *sess,
487 struct aim_conn_t *conn)
489 struct command_tx_struct newpacket;
494 newpacket.conn = conn;
496 newpacket.conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
497 newpacket.type = 0x02;
499 newpacket.commandlen = 10 + 16;
500 newpacket.data = (u_char *) malloc (newpacket.commandlen);
502 curbyte = aim_putsnac(newpacket.data, 0x0004, 0x0002, 0x0000, sess->snac_nextid);
503 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
504 curbyte += aimutil_put32(newpacket.data+curbyte, 0x00000003);
505 curbyte += aimutil_put8(newpacket.data+curbyte, 0x1f);
506 curbyte += aimutil_put8(newpacket.data+curbyte, 0x40);
507 curbyte += aimutil_put8(newpacket.data+curbyte, 0x03);
508 curbyte += aimutil_put8(newpacket.data+curbyte, 0xe7);
509 curbyte += aimutil_put8(newpacket.data+curbyte, 0x03);
510 curbyte += aimutil_put8(newpacket.data+curbyte, 0xe7);
511 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
512 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
514 aim_tx_enqueue(sess, &newpacket);
516 return (sess->snac_nextid++);
519 int aim_parse_msgerror_middle(struct aim_session_t *sess,
520 struct command_rx_struct *command)
522 u_long snacid = 0x000000000;
523 struct aim_snac_t *snac = NULL;
525 rxcallback_t userfunc = NULL;
528 * Get SNAC from packet and look it up
529 * the list of unrepliedto/outstanding
532 * After its looked up, the SN that the
533 * message should've gone to will be
534 * in the ->data element of the snac struct.
537 snacid = aimutil_get32(command->data+6);
538 snac = aim_remsnac(sess, snacid);
542 printf("faim: msgerr: got an ICBM-failed error on an unknown SNAC ID! (%08lx)\n", snacid);
548 userfunc = aim_callhandler(command->conn, 0x0004, 0x0001);
550 ret = userfunc(sess, command, (snac)?snac->data:"(UNKNOWN)");