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)
28 if (strcmp((char *)cur->priv, name) == 0)
31 faim_mutex_unlock(&sess->connlistlock);
36 faim_export int aim_chat_attachname(struct aim_conn_t *conn, char *roomname)
38 if (!conn || !roomname)
41 conn->priv = malloc(strlen(roomname)+1);
42 strcpy(conn->priv, roomname);
47 faim_export unsigned long aim_chat_send_im(struct aim_session_t *sess,
48 struct aim_conn_t *conn,
53 struct command_tx_struct *newpacket;
55 if (!sess || !conn || !msg)
58 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
61 newpacket->lock = 1; /* lock struct */
64 curbyte += aim_putsnac(newpacket->data+curbyte,
65 0x000e, 0x0005, 0x0000, sess->snac_nextid);
68 * Generate a random message cookie
71 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) rand());
73 aim_cachecookie(sess, aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL));
76 * metaTLV start. -- i assume this is a metaTLV. it could be the
79 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
82 * Type 1: Unknown. Blank.
84 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
85 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
88 * Type 6: Unknown. Blank.
90 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0006);
91 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
94 * Type 5: Message block. Contains more TLVs.
96 * This could include other information... We just
97 * put in a message TLV however.
100 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
101 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg)+4);
104 * SubTLV: Type 1: Message
106 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(msg), msg);
108 newpacket->commandlen = curbyte;
111 aim_tx_enqueue(sess, newpacket);
113 return (sess->snac_nextid++);
117 * Join a room of name roomname. This is the first
118 * step to joining an already created room. It's
119 * basically a Service Request for family 0x000e,
120 * with a little added on to specify the exchange
124 faim_export unsigned long aim_chat_join(struct aim_session_t *sess,
125 struct aim_conn_t *conn,
127 const char *roomname)
129 struct command_tx_struct *newpacket;
132 if (!sess || !conn || !roomname)
135 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+9+strlen(roomname)+2)))
140 i = aim_putsnac(newpacket->data, 0x0001, 0x0004, 0x0000, sess->snac_nextid);
142 i+= aimutil_put16(newpacket->data+i, 0x000e);
145 * this is techinally a TLV, but we can't use normal functions
146 * because we need the extraneous nulls and other weird things.
148 i+= aimutil_put16(newpacket->data+i, 0x0001);
149 i+= aimutil_put16(newpacket->data+i, 2+1+strlen(roomname)+2);
150 i+= aimutil_put16(newpacket->data+i, exchange);
151 i+= aimutil_put8(newpacket->data+i, strlen(roomname));
152 memcpy(newpacket->data+i, roomname, strlen(roomname));
153 i+= strlen(roomname);
154 //i+= aimutil_putstr(newpacket->data+i, roomname, strlen(roomname));
155 i+= aimutil_put16(newpacket->data+i, 0x0000);
160 * XXX: A problem occurs here if we request a channel
161 * join but it fails....pendingjoin will be nonnull
162 * even though the channel is never going to get a
166 sess->pendingjoin = (char *)malloc(strlen(roomname)+1);
167 strcpy(sess->pendingjoin, roomname);
170 aim_tx_enqueue(sess, newpacket);
174 struct aim_snac_t snac;
176 snac.id = sess->snac_nextid;
177 snac.family = 0x0001;
181 snac.data = malloc(strlen(roomname)+1);
182 strcpy(snac.data, roomname);
184 aim_newsnac(sess, &snac);
188 return (sess->snac_nextid++);
191 faim_internal int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
196 if (!buf || !outinfo)
199 outinfo->exchange = aimutil_get16(buf+i);
202 namelen = aimutil_get8(buf+i);
205 outinfo->name = (char *)malloc(namelen+1);
206 memcpy(outinfo->name, buf+i, namelen);
207 outinfo->name[namelen] = '\0';
210 outinfo->instance = aimutil_get16(buf+i);
218 * General room information. Lots of stuff.
220 * Values I know are in here but I havent attached
221 * them to any of the 'Unknown's:
222 * - Language (English)
226 faim_internal int aim_chat_parse_infoupdate(struct aim_session_t *sess,
227 struct command_rx_struct *command)
229 struct aim_userinfo_s *userinfo = NULL;
230 rxcallback_t userfunc=NULL;
233 u_char detaillevel = 0;
234 char *roomname = NULL;
235 struct aim_chat_roominfo roominfo;
236 u_short tlvcount = 0;
237 struct aim_tlvlist_t *tlvlist;
238 char *roomdesc = NULL;
239 struct aim_tlv_t *tmptlv;
240 unsigned short unknown_c9 = 0;
241 unsigned long creationtime = 0;
242 unsigned short maxmsglen = 0;
243 unsigned short unknown_d2 = 0, unknown_d5 = 0;
246 i += aim_chat_readroominfo(command->data+i, &roominfo);
248 detaillevel = aimutil_get8(command->data+i);
251 if (detaillevel != 0x02) {
252 if (detaillevel == 0x01)
253 printf("faim: chat_roomupdateinfo: detail level 2 not supported\n");
255 printf("faim: chat_roomupdateinfo: unknown detail level %d\n", detaillevel);
259 tlvcount = aimutil_get16(command->data+i);
263 * Everything else are TLVs.
265 tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
268 * TLV type 0x006a is the room name in Human Readable Form.
270 if (aim_gettlv(tlvlist, 0x006a, 1))
271 roomname = aim_gettlv_str(tlvlist, 0x006a, 1);
274 * Type 0x006f: Number of occupants.
276 if (aim_gettlv(tlvlist, 0x006f, 1)) {
277 struct aim_tlv_t *tmptlv;
278 tmptlv = aim_gettlv(tlvlist, 0x006f, 1);
280 usercount = aimutil_get16(tmptlv->value);
284 * Type 0x0073: Occupant list.
286 if (aim_gettlv(tlvlist, 0x0073, 1)) {
288 struct aim_tlv_t *tmptlv;
290 tmptlv = aim_gettlv(tlvlist, 0x0073, 1);
292 /* Allocate enough userinfo structs for all occupants */
293 userinfo = calloc(usercount, sizeof(struct aim_userinfo_s));
296 while (curoccupant < usercount)
297 i += aim_extractuserinfo(tmptlv->value+i, &userinfo[curoccupant++]);
301 * Type 0x00c9: Unknown. (2 bytes)
303 if ((tmptlv = aim_gettlv(tlvlist, 0x00c9, 1)))
304 unknown_c9 = aimutil_get16(tmptlv->value);
307 * Type 0x00ca: Creation time (4 bytes)
309 if ((tmptlv = aim_gettlv(tlvlist, 0x00ca, 1)))
310 creationtime = aimutil_get32(tmptlv->value);
313 * Type 0x00d1: Maximum Message Length
315 if ((tmptlv = aim_gettlv(tlvlist, 0x00d1, 1)))
316 maxmsglen = aimutil_get16(tmptlv->value);
319 * Type 0x00d2: Unknown. (2 bytes)
321 if ((tmptlv = aim_gettlv(tlvlist, 0x00d2, 1)))
322 unknown_d2 = aimutil_get16(tmptlv->value);;
325 * Type 0x00d3: Room Description
327 if (aim_gettlv(tlvlist, 0x00d3, 1))
328 roomdesc = aim_gettlv_str(tlvlist, 0x00d3, 1);
331 * Type 0x00d5: Unknown. (1 byte)
333 if ((tmptlv = aim_gettlv(tlvlist, 0x00d5, 1)))
334 unknown_d5 = aimutil_get8(tmptlv->value);;
337 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_ROOMINFOUPDATE);
356 aim_freetlvchain(&tlvlist);
361 faim_internal int aim_chat_parse_joined(struct aim_session_t *sess,
362 struct command_rx_struct *command)
364 struct aim_userinfo_s *userinfo = NULL;
365 rxcallback_t userfunc=NULL;
366 int i = 10, curcount = 0, ret = 1;
368 while (i < command->commandlen) {
370 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
371 i += aim_extractuserinfo(command->data+i, &userinfo[curcount-1]);
374 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERJOIN);
387 faim_internal int aim_chat_parse_leave(struct aim_session_t *sess,
388 struct command_rx_struct *command)
391 struct aim_userinfo_s *userinfo = NULL;
392 rxcallback_t userfunc=NULL;
393 int i = 10, curcount = 0, ret = 1;
395 while (i < command->commandlen) {
397 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
398 i += aim_extractuserinfo(command->data+i, &userinfo[curcount-1]);
401 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERLEAVE);
415 * We could probably include this in the normal ICBM parsing
416 * code as channel 0x0003, however, since only the start
417 * would be the same, we might as well do it here.
419 faim_internal int aim_chat_parse_incoming(struct aim_session_t *sess,
420 struct command_rx_struct *command)
422 struct aim_userinfo_s userinfo;
423 rxcallback_t userfunc=NULL;
424 int ret = 1, i = 0, z = 0;
427 struct aim_tlvlist_t *outerlist;
430 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
432 i = 10; /* skip snac */
435 * ICBM Cookie. Cache it.
437 for (z=0; z<8; z++,i++)
438 cookie[z] = command->data[i];
440 aim_cachecookie(sess, aim_mkcookie(cookie, AIM_COOKIETYPE_ICBM, NULL));
445 * Channels 1 and 2 are implemented in the normal ICBM
448 * We only do channel 3 here.
451 channel = aimutil_get16(command->data+i);
454 if (channel != 0x0003) {
455 printf("faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
460 * Start parsing TLVs right away.
462 outerlist = aim_readtlvchain(command->data+i, command->commandlen-i);
465 * Type 0x0003: Source User Information
467 if (aim_gettlv(outerlist, 0x0003, 1)) {
468 struct aim_tlv_t *userinfotlv;
470 userinfotlv = aim_gettlv(outerlist, 0x0003, 1);
471 aim_extractuserinfo(userinfotlv->value, &userinfo);
475 * Type 0x0001: Unknown.
477 if (aim_gettlv(outerlist, 0x0001, 1))
481 * Type 0x0005: Message Block. Conains more TLVs.
483 if (aim_gettlv(outerlist, 0x0005, 1))
485 struct aim_tlvlist_t *innerlist;
486 struct aim_tlv_t *msgblock;
488 msgblock = aim_gettlv(outerlist, 0x0005, 1);
489 innerlist = aim_readtlvchain(msgblock->value, msgblock->length);
492 * Type 0x0001: Message.
494 if (aim_gettlv(innerlist, 0x0001, 1))
495 msg = aim_gettlv_str(innerlist, 0x0001, 1);
497 aim_freetlvchain(&innerlist);
500 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_INCOMINGMSG);
509 aim_freetlvchain(&outerlist);
514 faim_export unsigned long aim_chat_clientready(struct aim_session_t *sess,
515 struct aim_conn_t *conn)
517 struct command_tx_struct *newpacket;
520 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 0x20)))
525 i = aim_putsnac(newpacket->data, 0x0001, 0x0002, 0x0000, sess->snac_nextid);
527 i+= aimutil_put16(newpacket->data+i, 0x000e);
528 i+= aimutil_put16(newpacket->data+i, 0x0001);
530 i+= aimutil_put16(newpacket->data+i, 0x0004);
531 i+= aimutil_put16(newpacket->data+i, 0x0001);
533 i+= aimutil_put16(newpacket->data+i, 0x0001);
534 i+= aimutil_put16(newpacket->data+i, 0x0003);
536 i+= aimutil_put16(newpacket->data+i, 0x0004);
537 i+= aimutil_put16(newpacket->data+i, 0x0686);
540 aim_tx_enqueue(sess, newpacket);
542 return (sess->snac_nextid++);
545 faim_export int aim_chat_leaveroom(struct aim_session_t *sess, char *name)
547 struct aim_conn_t *conn;
549 if ((conn = aim_chat_getconn(sess, name)))
550 aim_conn_kill(sess, &conn);
558 * conn must be a BOS connection!
560 faim_export unsigned long aim_chat_invite(struct aim_session_t *sess,
561 struct aim_conn_t *conn,
568 struct command_tx_struct *newpacket;
571 if (!sess || !conn || !sn || !msg || !roomname)
574 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
579 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
585 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char)rand());
586 aim_cachecookie(sess, aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL));
591 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
596 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
597 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
602 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
603 curbyte += aimutil_put16(newpacket->data+curbyte, 0x28+strlen(msg)+0x04+0x03+strlen(roomname)+0x02);
608 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
609 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3131);
610 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3538);
611 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3446);
612 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4100);
613 curbyte += aimutil_put16(newpacket->data+curbyte, 0x748f);
614 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2420);
615 curbyte += aimutil_put16(newpacket->data+curbyte, 0x6287);
616 curbyte += aimutil_put16(newpacket->data+curbyte, 0x11d1);
617 curbyte += aimutil_put16(newpacket->data+curbyte, 0x8222);
618 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4445);
619 curbyte += aimutil_put16(newpacket->data+curbyte, 0x5354);
620 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
623 * TLV t(000a) -- Unknown
625 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
626 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
627 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
630 * TLV t(000f) -- Unknown
632 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
633 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
636 * TLV t(000c) -- Invitation message
638 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000c, strlen(msg), msg);
641 * TLV t(2711) -- Container for room information
643 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711);
644 curbyte += aimutil_put16(newpacket->data+curbyte, 3+strlen(roomname)+2);
645 curbyte += aimutil_put16(newpacket->data+curbyte, exchange);
646 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(roomname));
647 curbyte += aimutil_putstr(newpacket->data+curbyte, roomname, strlen(roomname));
648 curbyte += aimutil_put16(newpacket->data+curbyte, instance);
650 newpacket->commandlen = curbyte;
652 aim_tx_enqueue(sess, newpacket);
654 return (sess->snac_nextid++);