4 * Routines for the Chat service.
10 faim_export char *aim_chat_getname(struct aim_conn_t *conn)
14 if (conn->type != AIM_CONN_TYPE_CHAT)
17 return (char *)conn->priv; /* yuck ! */
20 faim_export struct aim_conn_t *aim_chat_getconn(struct aim_session_t *sess, char *name)
22 struct aim_conn_t *cur;
24 faim_mutex_lock(&sess->connlistlock);
25 for (cur = sess->connlist; cur; cur = cur->next) {
26 if (cur->type != AIM_CONN_TYPE_CHAT)
29 printf("faim: chat: chat connection with no name! (fd = %d)\n", cur->fd);
32 if (strcmp((char *)cur->priv, name) == 0)
35 faim_mutex_unlock(&sess->connlistlock);
40 faim_export int aim_chat_attachname(struct aim_conn_t *conn, char *roomname)
42 if (!conn || !roomname)
48 conn->priv = strdup(roomname);
53 faim_export unsigned long aim_chat_send_im(struct aim_session_t *sess,
54 struct aim_conn_t *conn,
59 struct command_tx_struct *newpacket;
61 if (!sess || !conn || !msg)
64 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
67 newpacket->lock = 1; /* lock struct */
70 curbyte += aim_putsnac(newpacket->data+curbyte,
71 0x000e, 0x0005, 0x0000, sess->snac_nextid);
74 * Generate a random message cookie
77 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) rand());
79 aim_cachecookie(sess, aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL));
82 * metaTLV start. -- i assume this is a metaTLV. it could be the
85 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
88 * Type 1: Unknown. Blank.
90 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
91 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
94 * Type 6: Unknown. Blank.
96 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0006);
97 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
100 * Type 5: Message block. Contains more TLVs.
102 * This could include other information... We just
103 * put in a message TLV however.
106 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
107 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg)+4);
110 * SubTLV: Type 1: Message
112 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(msg), msg);
114 newpacket->commandlen = curbyte;
117 aim_tx_enqueue(sess, newpacket);
119 return (sess->snac_nextid++);
123 * Join a room of name roomname. This is the first
124 * step to joining an already created room. It's
125 * basically a Service Request for family 0x000e,
126 * with a little added on to specify the exchange
130 faim_export unsigned long aim_chat_join(struct aim_session_t *sess,
131 struct aim_conn_t *conn,
133 const char *roomname)
135 struct command_tx_struct *newpacket;
138 if (!sess || !conn || !roomname)
141 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+9+strlen(roomname)+2)))
146 i = aim_putsnac(newpacket->data, 0x0001, 0x0004, 0x0000, sess->snac_nextid);
148 i+= aimutil_put16(newpacket->data+i, 0x000e);
151 * this is techinally a TLV, but we can't use normal functions
152 * because we need the extraneous nulls and other weird things.
154 i+= aimutil_put16(newpacket->data+i, 0x0001);
155 i+= aimutil_put16(newpacket->data+i, 2+1+strlen(roomname)+2);
156 i+= aimutil_put16(newpacket->data+i, exchange);
157 i+= aimutil_put8(newpacket->data+i, strlen(roomname));
158 memcpy(newpacket->data+i, roomname, strlen(roomname));
159 i+= strlen(roomname);
160 //i+= aimutil_putstr(newpacket->data+i, roomname, strlen(roomname));
161 i+= aimutil_put16(newpacket->data+i, 0x0000);
166 * XXX: A problem occurs here if we request a channel
167 * join but it fails....pendingjoin will be nonnull
168 * even though the channel is never going to get a
172 sess->pendingjoin = (char *)malloc(strlen(roomname)+1);
173 strcpy(sess->pendingjoin, roomname);
176 aim_tx_enqueue(sess, newpacket);
180 struct aim_snac_t snac;
182 snac.id = sess->snac_nextid;
183 snac.family = 0x0001;
187 snac.data = malloc(strlen(roomname)+1);
188 strcpy(snac.data, roomname);
190 aim_newsnac(sess, &snac);
194 return (sess->snac_nextid++);
197 faim_internal int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
202 if (!buf || !outinfo)
205 outinfo->exchange = aimutil_get16(buf+i);
208 namelen = aimutil_get8(buf+i);
211 outinfo->name = (char *)malloc(namelen+1);
212 memcpy(outinfo->name, buf+i, namelen);
213 outinfo->name[namelen] = '\0';
216 outinfo->instance = aimutil_get16(buf+i);
224 * General room information. Lots of stuff.
226 * Values I know are in here but I havent attached
227 * them to any of the 'Unknown's:
228 * - Language (English)
232 faim_internal int aim_chat_parse_infoupdate(struct aim_session_t *sess,
233 struct command_rx_struct *command)
235 struct aim_userinfo_s *userinfo = NULL;
236 rxcallback_t userfunc=NULL;
239 u_char detaillevel = 0;
240 char *roomname = NULL;
241 struct aim_chat_roominfo roominfo;
242 u_short tlvcount = 0;
243 struct aim_tlvlist_t *tlvlist;
244 char *roomdesc = NULL;
245 struct aim_tlv_t *tmptlv;
246 unsigned short unknown_c9 = 0;
247 unsigned long creationtime = 0;
248 unsigned short maxmsglen = 0;
249 unsigned short unknown_d2 = 0, unknown_d5 = 0;
252 i += aim_chat_readroominfo(command->data+i, &roominfo);
254 detaillevel = aimutil_get8(command->data+i);
257 if (detaillevel != 0x02) {
258 if (detaillevel == 0x01)
259 printf("faim: chat_roomupdateinfo: detail level 1 not supported\n");
261 printf("faim: chat_roomupdateinfo: unknown detail level %d\n", detaillevel);
265 tlvcount = aimutil_get16(command->data+i);
269 * Everything else are TLVs.
271 tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
274 * TLV type 0x006a is the room name in Human Readable Form.
276 if (aim_gettlv(tlvlist, 0x006a, 1))
277 roomname = aim_gettlv_str(tlvlist, 0x006a, 1);
280 * Type 0x006f: Number of occupants.
282 if (aim_gettlv(tlvlist, 0x006f, 1)) {
283 struct aim_tlv_t *tmptlv;
284 tmptlv = aim_gettlv(tlvlist, 0x006f, 1);
286 usercount = aimutil_get16(tmptlv->value);
290 * Type 0x0073: Occupant list.
292 if (aim_gettlv(tlvlist, 0x0073, 1)) {
294 struct aim_tlv_t *tmptlv;
296 tmptlv = aim_gettlv(tlvlist, 0x0073, 1);
298 /* Allocate enough userinfo structs for all occupants */
299 userinfo = calloc(usercount, sizeof(struct aim_userinfo_s));
302 while (curoccupant < usercount)
303 i += aim_extractuserinfo(tmptlv->value+i, &userinfo[curoccupant++]);
307 * Type 0x00c9: Unknown. (2 bytes)
309 if ((tmptlv = aim_gettlv(tlvlist, 0x00c9, 1)))
310 unknown_c9 = aimutil_get16(tmptlv->value);
313 * Type 0x00ca: Creation time (4 bytes)
315 if ((tmptlv = aim_gettlv(tlvlist, 0x00ca, 1)))
316 creationtime = aimutil_get32(tmptlv->value);
319 * Type 0x00d1: Maximum Message Length
321 if ((tmptlv = aim_gettlv(tlvlist, 0x00d1, 1)))
322 maxmsglen = aimutil_get16(tmptlv->value);
325 * Type 0x00d2: Unknown. (2 bytes)
327 if ((tmptlv = aim_gettlv(tlvlist, 0x00d2, 1)))
328 unknown_d2 = aimutil_get16(tmptlv->value);;
331 * Type 0x00d3: Room Description
333 if (aim_gettlv(tlvlist, 0x00d3, 1))
334 roomdesc = aim_gettlv_str(tlvlist, 0x00d3, 1);
337 * Type 0x00d5: Unknown. (1 byte)
339 if ((tmptlv = aim_gettlv(tlvlist, 0x00d5, 1)))
340 unknown_d5 = aimutil_get8(tmptlv->value);;
343 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_ROOMINFOUPDATE);
362 aim_freetlvchain(&tlvlist);
367 faim_internal int aim_chat_parse_joined(struct aim_session_t *sess,
368 struct command_rx_struct *command)
370 struct aim_userinfo_s *userinfo = NULL;
371 rxcallback_t userfunc=NULL;
372 int i = 10, curcount = 0, ret = 1;
374 while (i < command->commandlen) {
376 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
377 i += aim_extractuserinfo(command->data+i, &userinfo[curcount-1]);
380 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERJOIN);
393 faim_internal int aim_chat_parse_leave(struct aim_session_t *sess,
394 struct command_rx_struct *command)
397 struct aim_userinfo_s *userinfo = NULL;
398 rxcallback_t userfunc=NULL;
399 int i = 10, curcount = 0, ret = 1;
401 while (i < command->commandlen) {
403 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
404 i += aim_extractuserinfo(command->data+i, &userinfo[curcount-1]);
407 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERLEAVE);
421 * We could probably include this in the normal ICBM parsing
422 * code as channel 0x0003, however, since only the start
423 * would be the same, we might as well do it here.
425 faim_internal int aim_chat_parse_incoming(struct aim_session_t *sess,
426 struct command_rx_struct *command)
428 struct aim_userinfo_s userinfo;
429 rxcallback_t userfunc=NULL;
430 int ret = 1, i = 0, z = 0;
431 unsigned char cookie[8];
433 struct aim_tlvlist_t *outerlist;
435 struct aim_msgcookie_t *ck;
437 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
439 i = 10; /* skip snac */
442 * ICBM Cookie. Cache it.
444 for (z=0; z<8; z++,i++)
445 cookie[z] = command->data[i];
447 if ((ck = aim_uncachecookie(sess, cookie, AIM_COOKIETYPE_CHAT))) {
456 * Channels 1 and 2 are implemented in the normal ICBM
459 * We only do channel 3 here.
462 channel = aimutil_get16(command->data+i);
465 if (channel != 0x0003) {
466 printf("faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
471 * Start parsing TLVs right away.
473 outerlist = aim_readtlvchain(command->data+i, command->commandlen-i);
476 * Type 0x0003: Source User Information
478 if (aim_gettlv(outerlist, 0x0003, 1)) {
479 struct aim_tlv_t *userinfotlv;
481 userinfotlv = aim_gettlv(outerlist, 0x0003, 1);
482 aim_extractuserinfo(userinfotlv->value, &userinfo);
486 * Type 0x0001: Unknown.
488 if (aim_gettlv(outerlist, 0x0001, 1))
492 * Type 0x0005: Message Block. Conains more TLVs.
494 if (aim_gettlv(outerlist, 0x0005, 1))
496 struct aim_tlvlist_t *innerlist;
497 struct aim_tlv_t *msgblock;
499 msgblock = aim_gettlv(outerlist, 0x0005, 1);
500 innerlist = aim_readtlvchain(msgblock->value, msgblock->length);
503 * Type 0x0001: Message.
505 if (aim_gettlv(innerlist, 0x0001, 1))
506 msg = aim_gettlv_str(innerlist, 0x0001, 1);
508 aim_freetlvchain(&innerlist);
511 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_INCOMINGMSG);
520 aim_freetlvchain(&outerlist);
525 faim_export unsigned long aim_chat_clientready(struct aim_session_t *sess,
526 struct aim_conn_t *conn)
528 struct command_tx_struct *newpacket;
531 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 0x20)))
536 i = aim_putsnac(newpacket->data, 0x0001, 0x0002, 0x0000, sess->snac_nextid);
538 i+= aimutil_put16(newpacket->data+i, 0x000e);
539 i+= aimutil_put16(newpacket->data+i, 0x0001);
541 i+= aimutil_put16(newpacket->data+i, 0x0004);
542 i+= aimutil_put16(newpacket->data+i, 0x0001);
544 i+= aimutil_put16(newpacket->data+i, 0x0001);
545 i+= aimutil_put16(newpacket->data+i, 0x0003);
547 i+= aimutil_put16(newpacket->data+i, 0x0004);
548 i+= aimutil_put16(newpacket->data+i, 0x0686);
551 aim_tx_enqueue(sess, newpacket);
553 return (sess->snac_nextid++);
556 faim_export int aim_chat_leaveroom(struct aim_session_t *sess, char *name)
558 struct aim_conn_t *conn;
560 if ((conn = aim_chat_getconn(sess, name)))
561 aim_conn_close(conn);
569 * conn must be a BOS connection!
571 faim_export unsigned long aim_chat_invite(struct aim_session_t *sess,
572 struct aim_conn_t *conn,
579 struct command_tx_struct *newpacket;
582 if (!sess || !conn || !sn || !msg || !roomname)
585 if (conn->type != AIM_CONN_TYPE_BOS)
588 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
593 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
599 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char)rand());
601 /* XXX this should get uncached by the unwritten 'invite accept' handler */
602 aim_cachecookie(sess, aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL));
607 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
612 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
613 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
618 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
619 curbyte += aimutil_put16(newpacket->data+curbyte, 0x28+strlen(msg)+0x04+0x03+strlen(roomname)+0x02);
624 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
625 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3131);
626 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3538);
627 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3446);
628 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4100);
629 curbyte += aimutil_put16(newpacket->data+curbyte, 0x748f);
630 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2420);
631 curbyte += aimutil_put16(newpacket->data+curbyte, 0x6287);
632 curbyte += aimutil_put16(newpacket->data+curbyte, 0x11d1);
633 curbyte += aimutil_put16(newpacket->data+curbyte, 0x8222);
634 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4445);
635 curbyte += aimutil_put16(newpacket->data+curbyte, 0x5354);
636 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
639 * TLV t(000a) -- Unknown
641 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
642 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
643 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
646 * TLV t(000f) -- Unknown
648 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
649 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
652 * TLV t(000c) -- Invitation message
654 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000c, strlen(msg), msg);
657 * TLV t(2711) -- Container for room information
659 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711);
660 curbyte += aimutil_put16(newpacket->data+curbyte, 3+strlen(roomname)+2);
661 curbyte += aimutil_put16(newpacket->data+curbyte, exchange);
662 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(roomname));
663 curbyte += aimutil_putstr(newpacket->data+curbyte, roomname, strlen(roomname));
664 curbyte += aimutil_put16(newpacket->data+curbyte, instance);
666 newpacket->commandlen = curbyte;
668 aim_tx_enqueue(sess, newpacket);
670 return (sess->snac_nextid++);