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 if (!(newpacket = aim_tx_new(0x0002, conn, 1152)))
31 newpacket->lock = 1; /* lock struct */
34 curbyte += aim_putsnac(newpacket->data+curbyte,
35 0x0004, 0x0006, 0x0000, sess->snac_nextid);
38 * Generate a random message cookie
40 * We could cache these like we do SNAC IDs. (In fact, it
41 * might be a good idea.) In the message error functions,
42 * the 8byte message cookie is returned as well as the
47 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) random());
52 curbyte += aimutil_put16(newpacket->data+curbyte,0x0001);
55 * Destination SN (prepended with byte length)
57 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
58 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
63 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
64 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg) + 0x0d);
69 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0501);
70 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
71 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0101);
72 curbyte += aimutil_put8 (newpacket->data+curbyte, 0x01);
75 * Message block length.
77 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg) + 0x04);
82 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
83 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
86 * Message. Not terminated.
88 curbyte += aimutil_putstr(newpacket->data+curbyte,msg, strlen(msg));
91 * Set the Request Acknowledge flag.
93 if (flags & AIM_IMFLAGS_ACK) {
94 curbyte += aimutil_put16(newpacket->data+curbyte,0x0003);
95 curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
99 * Set the Autoresponse flag.
101 if (flags & AIM_IMFLAGS_AWAY) {
102 curbyte += aimutil_put16(newpacket->data+curbyte,0x0004);
103 curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
106 newpacket->commandlen = curbyte;
109 aim_tx_enqueue(sess, newpacket);
111 #ifdef USE_SNAC_FOR_IMS
113 struct aim_snac_t snac;
115 snac.id = sess->snac_nextid;
116 snac.family = 0x0004;
120 snac.data = malloc(strlen(destsn)+1);
121 memcpy(snac.data, destsn, strlen(destsn)+1);
123 aim_newsnac(sess, &snac);
126 aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
129 return (sess->snac_nextid++);
133 * It can easily be said that parsing ICBMs is THE single
134 * most difficult thing to do in the in AIM protocol. In
135 * fact, I think I just did say that.
137 * Below is the best damned solution I've come up with
138 * over the past sixteen months of battling with it. This
139 * can parse both away and normal messages from every client
140 * I have access to. Its not fast, its not clean. But it works.
142 * We should also support at least minimal parsing of
143 * Channel 2, so that we can at least know the name of the
144 * room we're invited to, but obviously can't attend...
147 int aim_parse_incoming_im_middle(struct aim_session_t *sess,
148 struct command_rx_struct *command)
151 rxcallback_t userfunc = NULL;
154 struct aim_tlvlist_t *tlvlist;
155 struct aim_userinfo_s userinfo;
158 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
160 i = 10; /* Skip SNAC header */
163 * Read ICBM Cookie. And throw away.
165 for (z=0; z<8; z++,i++)
166 cookie[z] = command->data[i];
171 * Channel 0x0001 is the message channel. There are
172 * other channels for things called "rendevous"
173 * which represent chat and some of the other new
174 * features of AIM2/3/3.5.
176 * Channel 0x0002 is the Rendevous channel, which
177 * is where Chat Invitiations and various client-client
178 * connection negotiations come from.
181 channel = aimutil_get16(command->data+i);
187 if ((channel != 0x01) && (channel != 0x02))
189 printf("faim: icbm: ICBM received on an unsupported channel. Ignoring.\n (chan = %04x)", channel);
194 * Source screen name.
196 memcpy(userinfo.sn, command->data+i+1, (int)command->data[i]);
197 userinfo.sn[(int)command->data[i]] = '\0';
198 i += 1 + (int)command->data[i];
203 userinfo.warnlevel = aimutil_get16(command->data+i); /* guess */
207 * Number of TLVs that follow. Not needed.
209 wastebits = aimutil_get16(command->data+i);
213 * Read block of TLVs. All further data is derived
214 * from what is parsed here.
216 tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
219 * From here on, its depends on what channel we're on.
223 u_int j = 0, y = 0, z = 0;
226 struct aim_tlv_t *msgblocktlv, *tmptlv;
231 * Check Autoresponse status. If it is an autoresponse,
232 * it will contain a second type 0x0004 TLV, with zero length.
234 if (aim_gettlv(tlvlist, 0x0004, 2))
235 icbmflags |= AIM_IMFLAGS_AWAY;
238 * Check Ack Request status.
240 if (aim_gettlv(tlvlist, 0x0003, 2))
241 icbmflags |= AIM_IMFLAGS_ACK;
244 * Extract the various pieces of the userinfo struct.
247 if ((tmptlv = aim_gettlv(tlvlist, 0x0001, 1)))
248 userinfo.class = aimutil_get16(tmptlv->value);
249 /* Member-since date. */
250 if ((tmptlv = aim_gettlv(tlvlist, 0x0002, 1)))
252 /* If this is larger than 4, its probably the message block, skip */
253 if (tmptlv->length <= 4)
254 userinfo.membersince = aimutil_get32(tmptlv->value);
257 if ((tmptlv = aim_gettlv(tlvlist, 0x0003, 1)))
258 userinfo.onlinesince = aimutil_get32(tmptlv->value);
260 if ((tmptlv = aim_gettlv(tlvlist, 0x0004, 1)))
261 userinfo.idletime = aimutil_get16(tmptlv->value);
262 /* Session Length (AIM) */
263 if ((tmptlv = aim_gettlv(tlvlist, 0x000f, 1)))
264 userinfo.sessionlen = aimutil_get16(tmptlv->value);
265 /* Session Length (AOL) */
266 if ((tmptlv = aim_gettlv(tlvlist, 0x0010, 1)))
267 userinfo.sessionlen = aimutil_get16(tmptlv->value);
272 * XXX: Will the msgblock always be the second 0x0002?
274 msgblocktlv = aim_gettlv(tlvlist, 0x0002, 1);
277 printf("faim: icbm: major error! no message block TLV found!\n");
278 aim_freetlvchain(&tlvlist);
283 * Extracting the message from the unknown cruft.
285 * This is a bit messy, and I'm not really qualified,
286 * even as the author, to comment on it. At least
287 * its not as bad as a while loop shooting into infinity.
289 * "Do you believe in magic?"
292 msgblock = msgblocktlv->value;
295 wastebits = aimutil_get8(msgblock+j++);
296 wastebits = aimutil_get8(msgblock+j++);
298 y = aimutil_get16(msgblock+j);
300 for (z = 0; z < y; z++)
301 wastebits = aimutil_get8(msgblock+j++);
302 wastebits = aimutil_get8(msgblock+j++);
303 wastebits = aimutil_get8(msgblock+j++);
306 * Message string length, including flag words.
308 i = aimutil_get16(msgblock+j);
314 * Its rumored that these can kick in some funky
315 * 16bit-wide char stuff that used to really kill
316 * libfaim. Hopefully the latter is no longer true.
318 * Though someone should investiagte the former.
321 flag1 = aimutil_get16(msgblock+j);
323 flag2 = aimutil_get16(msgblock+j);
327 printf("faim: icbm: **warning: encoding flags are being used! {%04x, %04x}\n", flag1, flag2);
333 msg = (char *)malloc(i+1);
334 memcpy(msg, msgblock+j, i);
340 userfunc = aim_callhandler(command->conn, 0x0004, 0x0007);
342 i = userfunc(sess, command, channel, &userinfo, msg, icbmflags, flag1, flag2);
348 else if (channel == 0x0002)
351 struct aim_tlv_t *block1;
352 struct aim_tlvlist_t *list2;
353 struct aim_tlv_t *tmptlv;
357 if ((tmptlv = aim_gettlv(tlvlist, 0x0001, 1)))
358 userinfo.class = aimutil_get16(tmptlv->value);
360 if ((tmptlv = aim_gettlv(tlvlist, 0x0003, 1)))
361 userinfo.onlinesince = aimutil_get32(tmptlv->value);
363 if ((tmptlv = aim_gettlv(tlvlist, 0x0004, 1)))
364 userinfo.idletime = aimutil_get16(tmptlv->value);
365 /* Session Length (AIM) */
366 if ((tmptlv = aim_gettlv(tlvlist, 0x000f, 1)))
367 userinfo.sessionlen = aimutil_get16(tmptlv->value);
368 /* Session Length (AOL) */
369 if ((tmptlv = aim_gettlv(tlvlist, 0x0010, 1)))
370 userinfo.sessionlen = aimutil_get16(tmptlv->value);
373 * There's another block of TLVs embedded in the type 5 here.
375 block1 = aim_gettlv(tlvlist, 0x0005, 1);
377 return 1; /* major problem */
379 a = 0x1a; /* skip -- not sure what this information is! */
382 * XXX: Ignore if there's no data, only cookie information.
384 * Its probably just an accepted invitation or something.
387 if (block1->length <= 0x1a)
389 aim_freetlvchain(&tlvlist);
393 list2 = aim_readtlvchain(block1->value+a, block1->length-a);
395 if (aim_gettlv(list2, 0x0004, 1) /* start connection */ ||
396 aim_gettlv(list2, 0x000b, 1) /* close conncetion */)
398 rendtype = 1; /* voice request */
403 userfunc = aim_callhandler(command->conn, 0x0004, 0x0007);
415 struct aim_chat_roominfo roominfo;
416 char *msg=NULL,*encoding=NULL,*lang=NULL;
418 rendtype = 0; /* chat invite */
419 if (aim_gettlv(list2, 0x2711, 1))
421 struct aim_tlv_t *nametlv;
423 nametlv = aim_gettlv(list2, 0x2711, 1);
424 aim_chat_readroominfo(nametlv->value, &roominfo);
427 if (aim_gettlv(list2, 0x000c, 1))
428 msg = aim_gettlv_str(list2, 0x000c, 1);
430 if (aim_gettlv(list2, 0x000d, 1))
431 encoding = aim_gettlv_str(list2, 0x000d, 1);
433 if (aim_gettlv(list2, 0x000e, 1))
434 lang = aim_gettlv_str(list2, 0x000e, 1);
439 userfunc = aim_callhandler(command->conn, 0x0004, 0x0007);
448 encoding?encoding+1:NULL,
458 aim_freetlvchain(&list2);
462 * Free up the TLV chain.
464 aim_freetlvchain(&tlvlist);
471 * Not real sure what this does, nor does anyone I've talk to.
473 * Didn't use to send it. But now I think it might be a good
477 u_long aim_seticbmparam(struct aim_session_t *sess,
478 struct aim_conn_t *conn)
480 struct command_tx_struct *newpacket;
483 if(!(newpacket = aim_tx_new(0x0002, conn, 10+16)))
488 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0002, 0x0000, sess->snac_nextid);
489 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
490 curbyte += aimutil_put32(newpacket->data+curbyte, 0x00000003);
491 curbyte += aimutil_put8(newpacket->data+curbyte, 0x1f);
492 curbyte += aimutil_put8(newpacket->data+curbyte, 0x40);
493 curbyte += aimutil_put8(newpacket->data+curbyte, 0x03);
494 curbyte += aimutil_put8(newpacket->data+curbyte, 0xe7);
495 curbyte += aimutil_put8(newpacket->data+curbyte, 0x03);
496 curbyte += aimutil_put8(newpacket->data+curbyte, 0xe7);
497 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
498 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
501 aim_tx_enqueue(sess, newpacket);
503 return (sess->snac_nextid++);
506 int aim_parse_msgerror_middle(struct aim_session_t *sess,
507 struct command_rx_struct *command)
509 u_long snacid = 0x000000000;
510 struct aim_snac_t *snac = NULL;
512 rxcallback_t userfunc = NULL;
515 * Get SNAC from packet and look it up
516 * the list of unrepliedto/outstanding
519 * After its looked up, the SN that the
520 * message should've gone to will be
521 * in the ->data element of the snac struct.
524 snacid = aimutil_get32(command->data+6);
525 snac = aim_remsnac(sess, snacid);
529 printf("faim: msgerr: got an ICBM-failed error on an unknown SNAC ID! (%08lx)\n", snacid);
535 userfunc = aim_callhandler(command->conn, 0x0004, 0x0001);
537 ret = userfunc(sess, command, (snac)?snac->data:"(UNKNOWN)");