4 * Routines for the Chat service.
11 faim_export char *aim_chat_getname(aim_conn_t *conn)
17 if (conn->type != AIM_CONN_TYPE_CHAT)
20 return (char *)conn->priv; /* yuck ! */
23 faim_export aim_conn_t *aim_chat_getconn(aim_session_t *sess, const char *name)
27 faim_mutex_lock(&sess->connlistlock);
28 for (cur = sess->connlist; cur; cur = cur->next) {
29 if (cur->type != AIM_CONN_TYPE_CHAT)
32 faimdprintf(sess, 0, "faim: chat: chat connection with no name! (fd = %d)\n", cur->fd);
35 if (strcmp((char *)cur->priv, name) == 0)
38 faim_mutex_unlock(&sess->connlistlock);
43 faim_export int aim_chat_attachname(aim_conn_t *conn, const char *roomname)
46 if (!conn || !roomname)
52 conn->priv = strdup(roomname);
58 * Send a Chat Message.
61 * AIM_CHATFLAGS_NOREFLECT -- Unset the flag that requests messages
62 * should be sent to their sender.
63 * AIM_CHATFLAGS_AWAY -- Mark the message as an autoresponse
64 * (Note that WinAIM does not honor this,
65 * and displays the message as normal.)
67 * XXX convert this to use tlvchains
69 faim_export int aim_chat_send_im(aim_session_t *sess, aim_conn_t *conn, fu16_t flags, const char *msg, int msglen)
73 aim_msgcookie_t *cookie;
76 aim_tlvlist_t *otl = NULL, *itl = NULL;
78 if (!sess || !conn || !msg || (msglen <= 0))
81 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 1152)))
84 snacid = aim_cachesnac(sess, 0x000e, 0x0005, 0x0000, NULL, 0);
85 aim_putsnac(&fr->data, 0x000e, 0x0005, 0x0000, snacid);
89 * Generate a random message cookie.
91 * XXX mkcookie should generate the cookie and cache it in one
92 * operation to preserve uniqueness.
95 for (i = 0; i < sizeof(ckstr); i++)
96 aimutil_put8(ckstr+i, (fu8_t) rand());
98 cookie = aim_mkcookie(ckstr, AIM_COOKIETYPE_CHAT, NULL);
99 cookie->data = strdup(conn->priv); /* chat hack dependent */
101 aim_cachecookie(sess, cookie);
103 for (i = 0; i < sizeof(ckstr); i++)
104 aimbs_put8(&fr->data, ckstr[i]);
110 aimbs_put16(&fr->data, 0x0003);
114 * Type 1: Flag meaning this message is destined to the room.
116 aim_addtlvtochain_noval(&otl, 0x0001);
121 if (!(flags & AIM_CHATFLAGS_NOREFLECT))
122 aim_addtlvtochain_noval(&otl, 0x0006);
125 * Type 7: Autoresponse
127 if (flags & AIM_CHATFLAGS_AWAY)
128 aim_addtlvtochain_noval(&otl, 0x0007);
131 * SubTLV: Type 1: Message
133 aim_addtlvtochain_raw(&itl, 0x0001, strlen(msg), msg);
136 * Type 5: Message block. Contains more TLVs.
138 * This could include other information... We just
139 * put in a message TLV however.
142 aim_addtlvtochain_frozentlvlist(&otl, 0x0005, &itl);
144 aim_writetlvchain(&fr->data, &otl);
146 aim_freetlvchain(&itl);
147 aim_freetlvchain(&otl);
149 aim_tx_enqueue(sess, fr);
154 static int aim_addtlvtochain_chatroom(aim_tlvlist_t **list, fu16_t type, fu16_t exchange, const char *roomname, fu16_t instance)
160 buflen = 2 + 1 + strlen(roomname) + 2;
162 if (!(buf = malloc(buflen)))
165 aim_bstream_init(&bs, buf, buflen);
167 aimbs_put16(&bs, exchange);
168 aimbs_put8(&bs, strlen(roomname));
169 aimbs_putraw(&bs, roomname, strlen(roomname));
170 aimbs_put16(&bs, instance);
172 aim_addtlvtochain_raw(list, type, aim_bstream_curpos(&bs), buf);
180 * Join a room of name roomname. This is the first step to joining an
181 * already created room. It's basically a Service Request for
182 * family 0x000e, with a little added on to specify the exchange and room
185 faim_export int aim_chat_join(aim_session_t *sess, aim_conn_t *conn, fu16_t exchange, const char *roomname, fu16_t instance)
189 aim_tlvlist_t *tl = NULL;
191 if (!sess || !conn || !roomname || !strlen(roomname))
194 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+9+strlen(roomname)+2)))
198 snacid = aim_cachesnac(sess, 0x0001, 0x0004, 0x0000, roomname, strlen(roomname)+1);
199 aim_putsnac(&fr->data, 0x0001, 0x0004, 0x0000, snacid);
202 * Requesting service chat (0x000e)
204 aimbs_put16(&fr->data, 0x000e);
206 aim_addtlvtochain_chatroom(&tl, 0x0001, exchange, roomname, instance);
207 aim_writetlvchain(&fr->data, &tl);
208 aim_freetlvchain(&tl);
213 * XXX: A problem occurs here if we request a channel
214 * join but it fails....pendingjoin will be nonnull
215 * even though the channel is never going to get a
219 sess->pendingjoin = strdup(roomname);
220 sess->pendingjoinexchange = exchange;
222 aim_tx_enqueue(sess, fr);
227 faim_internal int aim_chat_readroominfo(aim_bstream_t *bs, struct aim_chat_roominfo *outinfo)
234 outinfo->exchange = aimbs_get16(bs);
235 namelen = aimbs_get8(bs);
236 outinfo->name = aimbs_getstr(bs, namelen);
237 outinfo->instance = aimbs_get16(bs);
242 faim_export int aim_chat_clientready(aim_session_t *sess, aim_conn_t *conn)
247 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 0x20)))
250 snacid = aim_cachesnac(sess, 0x0001, 0x0002, 0x0000, NULL, 0);
251 aim_putsnac(&fr->data, 0x0001, 0x0002, 0x0000, snacid);
253 aimbs_put16(&fr->data, 0x000e);
254 aimbs_put16(&fr->data, 0x0001);
256 aimbs_put16(&fr->data, 0x0004);
257 aimbs_put16(&fr->data, 0x0001);
259 aimbs_put16(&fr->data, 0x0001);
260 aimbs_put16(&fr->data, 0x0003);
262 aimbs_put16(&fr->data, 0x0004);
263 aimbs_put16(&fr->data, 0x0686);
265 aim_tx_enqueue(sess, fr);
270 faim_export int aim_chat_leaveroom(aim_session_t *sess, const char *name)
274 if (!(conn = aim_chat_getconn(sess, name)))
277 aim_conn_close(conn);
283 * conn must be a BOS connection!
285 faim_export int aim_chat_invite(aim_session_t *sess, aim_conn_t *conn, const char *sn, const char *msg, fu16_t exchange, const char *roomname, fu16_t instance)
289 aim_msgcookie_t *cookie;
290 struct aim_invite_priv *priv;
293 aim_tlvlist_t *otl = NULL, *itl = NULL;
298 if (!sess || !conn || !sn || !msg || !roomname)
301 if (conn->type != AIM_CONN_TYPE_BOS)
304 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
307 snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, sn, strlen(sn)+1);
308 aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
314 for (i = 0; i < sizeof(ckstr); i++)
315 aimutil_put8(ckstr, (fu8_t) rand());
317 /* XXX should be uncached by an unwritten 'invite accept' handler */
318 if ((priv = malloc(sizeof(struct aim_invite_priv)))) {
319 priv->sn = strdup(sn);
320 priv->roomname = strdup(roomname);
321 priv->exchange = exchange;
322 priv->instance = instance;
325 if ((cookie = aim_mkcookie(ckstr, AIM_COOKIETYPE_INVITE, priv)))
326 aim_cachecookie(sess, cookie);
330 for (i = 0; i < sizeof(ckstr); i++)
331 aimbs_put8(&fr->data, ckstr[i]);
337 aimbs_put16(&fr->data, 0x0002);
342 aimbs_put8(&fr->data, strlen(sn));
343 aimbs_putraw(&fr->data, sn, strlen(sn));
348 * Everything else is inside this TLV.
350 * Sigh. AOL was rather inconsistent right here. So we have
351 * to play some minor tricks. Right inside the type 5 is some
352 * raw data, followed by a series of TLVs.
355 hdrlen = 2+8+16+6+4+4+strlen(msg)+4+2+1+strlen(roomname)+2;
356 hdr = malloc(hdrlen);
357 aim_bstream_init(&hdrbs, hdr, hdrlen);
359 aimbs_put16(&hdrbs, 0x0000); /* Unknown! */
360 aimbs_putraw(&hdrbs, ckstr, sizeof(ckstr)); /* I think... */
361 aim_putcap(&hdrbs, AIM_CAPS_CHAT);
363 aim_addtlvtochain16(&itl, 0x000a, 0x0001);
364 aim_addtlvtochain_noval(&itl, 0x000f);
365 aim_addtlvtochain_raw(&itl, 0x000c, strlen(msg), msg);
366 aim_addtlvtochain_chatroom(&itl, 0x2711, exchange, roomname, instance);
367 aim_writetlvchain(&hdrbs, &itl);
369 aim_addtlvtochain_raw(&otl, 0x0005, aim_bstream_curpos(&hdrbs), hdr);
371 aim_writetlvchain(&fr->data, &otl);
374 aim_freetlvchain(&itl);
375 aim_freetlvchain(&otl);
377 aim_tx_enqueue(sess, fr);
383 * General room information. Lots of stuff.
385 * Values I know are in here but I havent attached
386 * them to any of the 'Unknown's:
387 * - Language (English)
391 static int infoupdate(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
393 struct aim_userinfo_s *userinfo = NULL;
394 aim_rxcallback_t userfunc;
397 fu8_t detaillevel = 0;
398 char *roomname = NULL;
399 struct aim_chat_roominfo roominfo;
401 aim_tlvlist_t *tlvlist;
402 char *roomdesc = NULL;
403 fu16_t unknown_c9 = 0;
404 fu32_t creationtime = 0;
405 fu16_t maxmsglen = 0, maxvisiblemsglen = 0;
406 fu16_t unknown_d2 = 0, unknown_d5 = 0;
408 aim_chat_readroominfo(bs, &roominfo);
410 detaillevel = aimbs_get8(bs);
412 if (detaillevel != 0x02) {
413 faimdprintf(sess, 0, "faim: chat_roomupdateinfo: detail level %d not supported\n", detaillevel);
417 tlvcount = aimbs_get16(bs);
420 * Everything else are TLVs.
422 tlvlist = aim_readtlvchain(bs);
425 * TLV type 0x006a is the room name in Human Readable Form.
427 if (aim_gettlv(tlvlist, 0x006a, 1))
428 roomname = aim_gettlv_str(tlvlist, 0x006a, 1);
431 * Type 0x006f: Number of occupants.
433 if (aim_gettlv(tlvlist, 0x006f, 1))
434 usercount = aim_gettlv16(tlvlist, 0x006f, 1);
437 * Type 0x0073: Occupant list.
439 if (aim_gettlv(tlvlist, 0x0073, 1)) {
444 tmptlv = aim_gettlv(tlvlist, 0x0073, 1);
446 /* Allocate enough userinfo structs for all occupants */
447 userinfo = calloc(usercount, sizeof(struct aim_userinfo_s));
449 aim_bstream_init(&occbs, tmptlv->value, tmptlv->length);
451 while (curoccupant < usercount)
452 aim_extractuserinfo(sess, &occbs, &userinfo[curoccupant++]);
456 * Type 0x00c9: Unknown. (2 bytes)
458 if (aim_gettlv(tlvlist, 0x00c9, 1))
459 unknown_c9 = aim_gettlv16(tlvlist, 0x00c9, 1);
462 * Type 0x00ca: Creation time (4 bytes)
464 if (aim_gettlv(tlvlist, 0x00ca, 1))
465 creationtime = aim_gettlv32(tlvlist, 0x00ca, 1);
468 * Type 0x00d1: Maximum Message Length
470 if (aim_gettlv(tlvlist, 0x00d1, 1))
471 maxmsglen = aim_gettlv16(tlvlist, 0x00d1, 1);
474 * Type 0x00d2: Unknown. (2 bytes)
476 if (aim_gettlv(tlvlist, 0x00d2, 1))
477 unknown_d2 = aim_gettlv16(tlvlist, 0x00d2, 1);
480 * Type 0x00d3: Room Description
482 if (aim_gettlv(tlvlist, 0x00d3, 1))
483 roomdesc = aim_gettlv_str(tlvlist, 0x00d3, 1);
486 * Type 0x000d4: Unknown (flag only)
488 if (aim_gettlv(tlvlist, 0x000d4, 1))
492 * Type 0x00d5: Unknown. (1 byte)
494 if (aim_gettlv(tlvlist, 0x00d5, 1))
495 unknown_d5 = aim_gettlv8(tlvlist, 0x00d5, 1);
499 * Type 0x00d6: Encoding 1 ("us-ascii")
501 if (aim_gettlv(tlvlist, 0x000d6, 1))
505 * Type 0x00d7: Language 1 ("en")
507 if (aim_gettlv(tlvlist, 0x000d7, 1))
511 * Type 0x00d8: Encoding 2 ("us-ascii")
513 if (aim_gettlv(tlvlist, 0x000d8, 1))
517 * Type 0x00d9: Language 2 ("en")
519 if (aim_gettlv(tlvlist, 0x000d9, 1))
523 * Type 0x00da: Maximum visible message length
525 if (aim_gettlv(tlvlist, 0x000da, 1))
526 maxvisiblemsglen = aim_gettlv16(tlvlist, 0x00da, 1);
528 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) {
548 aim_freetlvchain(&tlvlist);
553 static int userlistchange(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
555 struct aim_userinfo_s *userinfo = NULL;
556 aim_rxcallback_t userfunc;
557 int curcount = 0, ret = 0;
559 while (aim_bstream_empty(bs)) {
561 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
562 aim_extractuserinfo(sess, bs, &userinfo[curcount-1]);
565 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
566 ret = userfunc(sess, rx, curcount, userinfo);
574 * We could probably include this in the normal ICBM parsing
575 * code as channel 0x0003, however, since only the start
576 * would be the same, we might as well do it here.
578 * General outline of this SNAC:
596 static int incomingmsg(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
598 struct aim_userinfo_s userinfo;
599 aim_rxcallback_t userfunc;
607 memset(&userinfo, 0, sizeof(struct aim_userinfo_s));
610 * ICBM Cookie. Uncache it.
612 cookie = aimbs_getraw(bs, 8);
614 if ((ck = aim_uncachecookie(sess, cookie, AIM_COOKIETYPE_CHAT))) {
622 * Channels 1 and 2 are implemented in the normal ICBM
625 * We only do channel 3 here.
628 channel = aimbs_get16(bs);
630 if (channel != 0x0003) {
631 faimdprintf(sess, 0, "faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
636 * Start parsing TLVs right away.
638 otl = aim_readtlvchain(bs);
641 * Type 0x0003: Source User Information
643 if (aim_gettlv(otl, 0x0003, 1)) {
644 aim_tlv_t *userinfotlv;
647 userinfotlv = aim_gettlv(otl, 0x0003, 1);
649 aim_bstream_init(&tbs, userinfotlv->value, userinfotlv->length);
650 aim_extractuserinfo(sess, &tbs, &userinfo);
654 * Type 0x0001: If present, it means it was a message to the
655 * room (as opposed to a whisper).
657 if (aim_gettlv(otl, 0x0001, 1))
661 * Type 0x0005: Message Block. Conains more TLVs.
663 if (aim_gettlv(otl, 0x0005, 1)) {
668 msgblock = aim_gettlv(otl, 0x0005, 1);
669 aim_bstream_init(&tbs, msgblock->value, msgblock->length);
670 itl = aim_readtlvchain(&tbs);
673 * Type 0x0001: Message.
675 if (aim_gettlv(itl, 0x0001, 1))
676 msg = aim_gettlv_str(itl, 0x0001, 1);
678 aim_freetlvchain(&itl);
681 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
682 ret = userfunc(sess, rx, &userinfo, msg);
686 aim_freetlvchain(&otl);
691 static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
694 if (snac->subtype == 0x0002)
695 return infoupdate(sess, mod, rx, snac, bs);
696 else if ((snac->subtype == 0x0003) || (snac->subtype == 0x0004))
697 return userlistchange(sess, mod, rx, snac, bs);
698 else if (snac->subtype == 0x0006)
699 return incomingmsg(sess, mod, rx, snac, bs);
704 faim_internal int chat_modfirst(aim_session_t *sess, aim_module_t *mod)
707 mod->family = 0x000e;
708 mod->version = 0x0000;
710 strncpy(mod->name, "chat", sizeof(mod->name));
711 mod->snachandler = snachandler;