4 * Routines for the Chat service.
11 faim_export char *aim_chat_getname(struct aim_conn_t *conn)
15 if (conn->type != AIM_CONN_TYPE_CHAT)
18 return (char *)conn->priv; /* yuck ! */
21 faim_export struct aim_conn_t *aim_chat_getconn(struct aim_session_t *sess, char *name)
23 struct aim_conn_t *cur;
25 faim_mutex_lock(&sess->connlistlock);
26 for (cur = sess->connlist; cur; cur = cur->next) {
27 if (cur->type != AIM_CONN_TYPE_CHAT)
30 faimdprintf(sess, 0, "faim: chat: chat connection with no name! (fd = %d)\n", cur->fd);
33 if (strcmp((char *)cur->priv, name) == 0)
36 faim_mutex_unlock(&sess->connlistlock);
41 faim_export int aim_chat_attachname(struct aim_conn_t *conn, char *roomname)
43 if (!conn || !roomname)
49 conn->priv = strdup(roomname);
54 /* XXX convert this to use tlvchains */
55 faim_export unsigned long aim_chat_send_im(struct aim_session_t *sess,
56 struct aim_conn_t *conn,
61 struct command_tx_struct *newpacket;
62 struct aim_msgcookie_t *cookie;
64 if (!sess || !conn || !msg)
67 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 1152)))
70 newpacket->lock = 1; /* lock struct */
73 curbyte += aim_putsnac(newpacket->data+curbyte,
74 0x000e, 0x0005, 0x0000, sess->snac_nextid);
77 * Generate a random message cookie
80 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) rand());
82 cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL);
83 cookie->data = strdup(conn->priv); /* chat hack dependent */
85 aim_cachecookie(sess, cookie);
90 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
93 * Type 1: Unknown. Blank.
95 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
96 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
99 * Type 6: Unknown. Blank.
101 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0006);
102 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
105 * Type 5: Message block. Contains more TLVs.
107 * This could include other information... We just
108 * put in a message TLV however.
111 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
112 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg)+4);
115 * SubTLV: Type 1: Message
117 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(msg), msg);
119 newpacket->commandlen = curbyte;
122 aim_tx_enqueue(sess, newpacket);
124 return (sess->snac_nextid++);
128 * Join a room of name roomname. This is the first
129 * step to joining an already created room. It's
130 * basically a Service Request for family 0x000e,
131 * with a little added on to specify the exchange
135 faim_export unsigned long aim_chat_join(struct aim_session_t *sess,
136 struct aim_conn_t *conn,
138 const char *roomname)
140 struct command_tx_struct *newpacket;
143 if (!sess || !conn || !roomname)
146 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+9+strlen(roomname)+2)))
151 i = aim_putsnac(newpacket->data, 0x0001, 0x0004, 0x0000, sess->snac_nextid);
153 i+= aimutil_put16(newpacket->data+i, 0x000e);
156 * this is techinally a TLV, but we can't use normal functions
157 * because we need the extraneous nulls and other weird things.
159 i+= aimutil_put16(newpacket->data+i, 0x0001);
160 i+= aimutil_put16(newpacket->data+i, 2+1+strlen(roomname)+2);
161 i+= aimutil_put16(newpacket->data+i, exchange);
162 i+= aimutil_put8(newpacket->data+i, strlen(roomname));
163 i+= aimutil_putstr(newpacket->data+i, roomname, strlen(roomname));
164 i+= aimutil_put16(newpacket->data+i, 0x0000); /* instance? */
169 * XXX: A problem occurs here if we request a channel
170 * join but it fails....pendingjoin will be nonnull
171 * even though the channel is never going to get a
175 sess->pendingjoin = strdup(roomname);
176 sess->pendingjoinexchange = exchange;
179 aim_tx_enqueue(sess, newpacket);
181 aim_cachesnac(sess, 0x0001, 0x0004, 0x0000, roomname, strlen(roomname)+1);
183 return sess->snac_nextid;
186 faim_internal int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
191 if (!buf || !outinfo)
194 outinfo->exchange = aimutil_get16(buf+i);
197 namelen = aimutil_get8(buf+i);
200 outinfo->name = (char *)malloc(namelen+1);
201 memcpy(outinfo->name, buf+i, namelen);
202 outinfo->name[namelen] = '\0';
205 outinfo->instance = aimutil_get16(buf+i);
213 * General room information. Lots of stuff.
215 * Values I know are in here but I havent attached
216 * them to any of the 'Unknown's:
217 * - Language (English)
221 faim_internal int aim_chat_parse_infoupdate(struct aim_session_t *sess,
222 struct command_rx_struct *command)
224 struct aim_userinfo_s *userinfo = NULL;
225 rxcallback_t userfunc=NULL;
228 u_char detaillevel = 0;
229 char *roomname = NULL;
230 struct aim_chat_roominfo roominfo;
231 u_short tlvcount = 0;
232 struct aim_tlvlist_t *tlvlist;
233 char *roomdesc = NULL;
234 unsigned short unknown_c9 = 0;
235 unsigned long creationtime = 0;
236 unsigned short maxmsglen = 0;
237 unsigned short unknown_d2 = 0, unknown_d5 = 0;
240 i += aim_chat_readroominfo(command->data+i, &roominfo);
242 detaillevel = aimutil_get8(command->data+i);
245 if (detaillevel != 0x02) {
246 if (detaillevel == 0x01)
247 faimdprintf(sess, 0, "faim: chat_roomupdateinfo: detail level 1 not supported\n");
249 faimdprintf(sess, 0, "faim: chat_roomupdateinfo: unknown detail level %d\n", detaillevel);
253 tlvcount = aimutil_get16(command->data+i);
257 * Everything else are TLVs.
259 tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
262 * TLV type 0x006a is the room name in Human Readable Form.
264 if (aim_gettlv(tlvlist, 0x006a, 1))
265 roomname = aim_gettlv_str(tlvlist, 0x006a, 1);
268 * Type 0x006f: Number of occupants.
270 if (aim_gettlv(tlvlist, 0x006f, 1))
271 usercount = aim_gettlv16(tlvlist, 0x006f, 1);
274 * Type 0x0073: Occupant list.
276 if (aim_gettlv(tlvlist, 0x0073, 1)) {
278 struct aim_tlv_t *tmptlv;
280 tmptlv = aim_gettlv(tlvlist, 0x0073, 1);
282 /* Allocate enough userinfo structs for all occupants */
283 userinfo = calloc(usercount, sizeof(struct aim_userinfo_s));
286 while (curoccupant < usercount)
287 i += aim_extractuserinfo(sess, tmptlv->value+i, &userinfo[curoccupant++]);
291 * Type 0x00c9: Unknown. (2 bytes)
293 if (aim_gettlv(tlvlist, 0x00c9, 1))
294 unknown_c9 = aim_gettlv16(tlvlist, 0x00c9, 1);
297 * Type 0x00ca: Creation time (4 bytes)
299 if (aim_gettlv(tlvlist, 0x00ca, 1))
300 creationtime = aim_gettlv32(tlvlist, 0x00ca, 1);
303 * Type 0x00d1: Maximum Message Length
305 if (aim_gettlv(tlvlist, 0x00d1, 1))
306 maxmsglen = aim_gettlv16(tlvlist, 0x00d1, 1);
309 * Type 0x00d2: Unknown. (2 bytes)
311 if (aim_gettlv(tlvlist, 0x00d2, 1))
312 unknown_d2 = aim_gettlv16(tlvlist, 0x00d2, 1);
315 * Type 0x00d3: Room Description
317 if (aim_gettlv(tlvlist, 0x00d3, 1))
318 roomdesc = aim_gettlv_str(tlvlist, 0x00d3, 1);
321 * Type 0x00d5: Unknown. (1 byte)
323 if (aim_gettlv(tlvlist, 0x00d5, 1))
324 unknown_d5 = aim_gettlv8(tlvlist, 0x00d5, 1);
327 if ((userfunc = aim_callhandler(sess, command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_ROOMINFOUPDATE))) {
345 aim_freetlvchain(&tlvlist);
350 faim_internal int aim_chat_parse_joined(struct aim_session_t *sess,
351 struct command_rx_struct *command)
353 struct aim_userinfo_s *userinfo = NULL;
354 rxcallback_t userfunc=NULL;
355 int i = 10, curcount = 0, ret = 1;
357 while (i < command->commandlen) {
359 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
360 i += aim_extractuserinfo(sess, command->data+i, &userinfo[curcount-1]);
363 if ((userfunc = aim_callhandler(sess, command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERJOIN))) {
375 faim_internal int aim_chat_parse_leave(struct aim_session_t *sess,
376 struct command_rx_struct *command)
379 struct aim_userinfo_s *userinfo = NULL;
380 rxcallback_t userfunc=NULL;
381 int i = 10, curcount = 0, ret = 1;
383 while (i < command->commandlen) {
385 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
386 i += aim_extractuserinfo(sess, command->data+i, &userinfo[curcount-1]);
389 if ((userfunc = aim_callhandler(sess, command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERLEAVE))) {
402 * We could probably include this in the normal ICBM parsing
403 * code as channel 0x0003, however, since only the start
404 * would be the same, we might as well do it here.
406 * General outline of this SNAC:
424 faim_internal int aim_chat_parse_incoming(struct aim_session_t *sess,
425 struct command_rx_struct *command)
427 struct aim_userinfo_s userinfo;
428 rxcallback_t userfunc=NULL;
429 int ret = 1, i = 0, z = 0;
430 unsigned char cookie[8];
432 struct aim_tlvlist_t *outerlist;
434 struct aim_msgcookie_t *ck;
436 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
438 i = 10; /* skip snac */
441 * ICBM Cookie. Cache it.
443 for (z=0; z<8; z++,i++)
444 cookie[z] = command->data[i];
446 if ((ck = aim_uncachecookie(sess, cookie, AIM_COOKIETYPE_CHAT))) {
455 * Channels 1 and 2 are implemented in the normal ICBM
458 * We only do channel 3 here.
461 channel = aimutil_get16(command->data+i);
464 if (channel != 0x0003) {
465 faimdprintf(sess, 0, "faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
470 * Start parsing TLVs right away.
472 outerlist = aim_readtlvchain(command->data+i, command->commandlen-i);
475 * Type 0x0003: Source User Information
477 if (aim_gettlv(outerlist, 0x0003, 1)) {
478 struct aim_tlv_t *userinfotlv;
480 userinfotlv = aim_gettlv(outerlist, 0x0003, 1);
481 aim_extractuserinfo(sess, userinfotlv->value, &userinfo);
485 * Type 0x0001: Unknown.
487 if (aim_gettlv(outerlist, 0x0001, 1))
491 * Type 0x0005: Message Block. Conains more TLVs.
493 if (aim_gettlv(outerlist, 0x0005, 1))
495 struct aim_tlvlist_t *innerlist;
496 struct aim_tlv_t *msgblock;
498 msgblock = aim_gettlv(outerlist, 0x0005, 1);
499 innerlist = aim_readtlvchain(msgblock->value, msgblock->length);
502 * Type 0x0001: Message.
504 if (aim_gettlv(innerlist, 0x0001, 1))
505 msg = aim_gettlv_str(innerlist, 0x0001, 1);
507 aim_freetlvchain(&innerlist);
510 userfunc = aim_callhandler(sess, command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_INCOMINGMSG);
519 aim_freetlvchain(&outerlist);
524 faim_export unsigned long aim_chat_clientready(struct aim_session_t *sess,
525 struct aim_conn_t *conn)
527 struct command_tx_struct *newpacket;
530 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 0x20)))
535 i = aim_putsnac(newpacket->data, 0x0001, 0x0002, 0x0000, sess->snac_nextid);
537 i+= aimutil_put16(newpacket->data+i, 0x000e);
538 i+= aimutil_put16(newpacket->data+i, 0x0001);
540 i+= aimutil_put16(newpacket->data+i, 0x0004);
541 i+= aimutil_put16(newpacket->data+i, 0x0001);
543 i+= aimutil_put16(newpacket->data+i, 0x0001);
544 i+= aimutil_put16(newpacket->data+i, 0x0003);
546 i+= aimutil_put16(newpacket->data+i, 0x0004);
547 i+= aimutil_put16(newpacket->data+i, 0x0686);
550 aim_tx_enqueue(sess, newpacket);
552 return (sess->snac_nextid++);
555 faim_export int aim_chat_leaveroom(struct aim_session_t *sess, char *name)
557 struct aim_conn_t *conn;
559 if ((conn = aim_chat_getconn(sess, name)))
560 aim_conn_close(conn);
568 * conn must be a BOS connection!
570 faim_export unsigned long aim_chat_invite(struct aim_session_t *sess,
571 struct aim_conn_t *conn,
578 struct command_tx_struct *newpacket;
580 struct aim_msgcookie_t *cookie;
581 struct aim_invite_priv *priv;
583 if (!sess || !conn || !sn || !msg || !roomname)
586 if (conn->type != AIM_CONN_TYPE_BOS)
589 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
594 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
600 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char)rand());
602 /* XXX this should get uncached by the unwritten 'invite accept' handler */
603 if(!(priv = calloc(sizeof(struct aim_invite_priv), 1)))
605 priv->sn = strdup(sn);
606 priv->roomname = strdup(roomname);
607 priv->exchange = exchange;
608 priv->instance = instance;
610 if(!(cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_INVITE, priv)))
612 aim_cachecookie(sess, cookie);
617 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
622 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
623 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
628 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
629 curbyte += aimutil_put16(newpacket->data+curbyte, 0x28+strlen(msg)+0x04+0x03+strlen(roomname)+0x02);
634 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
635 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3131);
636 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3538);
637 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3446);
638 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4100);
639 curbyte += aimutil_put16(newpacket->data+curbyte, 0x748f);
640 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2420);
641 curbyte += aimutil_put16(newpacket->data+curbyte, 0x6287);
642 curbyte += aimutil_put16(newpacket->data+curbyte, 0x11d1);
643 curbyte += aimutil_put16(newpacket->data+curbyte, 0x8222);
644 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4445);
645 curbyte += aimutil_put16(newpacket->data+curbyte, 0x5354);
646 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
649 * TLV t(000a) -- Unknown
651 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
652 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
653 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
656 * TLV t(000f) -- Unknown
658 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
659 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
662 * TLV t(000c) -- Invitation message
664 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000c, strlen(msg), msg);
667 * TLV t(2711) -- Container for room information
669 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711);
670 curbyte += aimutil_put16(newpacket->data+curbyte, 3+strlen(roomname)+2);
671 curbyte += aimutil_put16(newpacket->data+curbyte, exchange);
672 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(roomname));
673 curbyte += aimutil_putstr(newpacket->data+curbyte, roomname, strlen(roomname));
674 curbyte += aimutil_put16(newpacket->data+curbyte, instance);
676 newpacket->commandlen = curbyte;
678 aim_tx_enqueue(sess, newpacket);
680 return (sess->snac_nextid++);