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);
55 * Send a Chat Message.
58 * AIM_CHATFLAGS_NOREFLECT -- Unset the flag that requests messages
59 * should be sent to their sender.
60 * AIM_CHATFLAGS_AWAY -- Mark the message as an autoresponse
61 * (Note that WinAIM does not honor this,
62 * and displays the message as normal.)
64 * XXX convert this to use tlvchains
66 faim_export unsigned long aim_chat_send_im(struct aim_session_t *sess,
67 struct aim_conn_t *conn,
74 struct command_tx_struct *newpacket;
75 struct aim_msgcookie_t *cookie;
77 if (!sess || !conn || !msg || (msglen <= 0))
80 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 1152)))
83 newpacket->lock = 1; /* lock struct */
86 curbyte += aim_putsnac(newpacket->data+curbyte,
87 0x000e, 0x0005, 0x0000, sess->snac_nextid);
90 * Generate a random message cookie
93 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) rand());
95 cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL);
96 cookie->data = strdup(conn->priv); /* chat hack dependent */
98 aim_cachecookie(sess, cookie);
103 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
106 * Type 1: Flag meaning this message is destined to the room.
108 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
109 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
114 if (!(flags & AIM_CHATFLAGS_NOREFLECT)) {
115 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0006);
116 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
120 * Type 7: Autoresponse
122 if (flags & AIM_CHATFLAGS_AWAY) {
123 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0007);
124 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
128 * Type 5: Message block. Contains more TLVs.
130 * This could include other information... We just
131 * put in a message TLV however.
134 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
135 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg)+4);
138 * SubTLV: Type 1: Message
140 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(msg), msg);
142 newpacket->commandlen = curbyte;
145 aim_tx_enqueue(sess, newpacket);
147 return (sess->snac_nextid++);
151 * Join a room of name roomname. This is the first
152 * step to joining an already created room. It's
153 * basically a Service Request for family 0x000e,
154 * with a little added on to specify the exchange
158 faim_export unsigned long aim_chat_join(struct aim_session_t *sess,
159 struct aim_conn_t *conn,
161 const char *roomname)
163 struct command_tx_struct *newpacket;
166 if (!sess || !conn || !roomname)
169 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+9+strlen(roomname)+2)))
174 i = aim_putsnac(newpacket->data, 0x0001, 0x0004, 0x0000, sess->snac_nextid);
176 i+= aimutil_put16(newpacket->data+i, 0x000e);
179 * this is techinally a TLV, but we can't use normal functions
180 * because we need the extraneous nulls and other weird things.
182 i+= aimutil_put16(newpacket->data+i, 0x0001);
183 i+= aimutil_put16(newpacket->data+i, 2+1+strlen(roomname)+2);
184 i+= aimutil_put16(newpacket->data+i, exchange);
185 i+= aimutil_put8(newpacket->data+i, strlen(roomname));
186 i+= aimutil_putstr(newpacket->data+i, roomname, strlen(roomname));
187 i+= aimutil_put16(newpacket->data+i, 0x0000); /* instance? */
192 * XXX: A problem occurs here if we request a channel
193 * join but it fails....pendingjoin will be nonnull
194 * even though the channel is never going to get a
198 sess->pendingjoin = strdup(roomname);
199 sess->pendingjoinexchange = exchange;
202 aim_tx_enqueue(sess, newpacket);
204 aim_cachesnac(sess, 0x0001, 0x0004, 0x0000, roomname, strlen(roomname)+1);
206 return sess->snac_nextid;
209 faim_internal int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
214 if (!buf || !outinfo)
217 outinfo->exchange = aimutil_get16(buf+i);
220 namelen = aimutil_get8(buf+i);
223 outinfo->name = (char *)malloc(namelen+1);
224 memcpy(outinfo->name, buf+i, namelen);
225 outinfo->name[namelen] = '\0';
228 outinfo->instance = aimutil_get16(buf+i);
234 faim_export unsigned long aim_chat_clientready(struct aim_session_t *sess,
235 struct aim_conn_t *conn)
237 struct command_tx_struct *newpacket;
240 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 0x20)))
245 i = aim_putsnac(newpacket->data, 0x0001, 0x0002, 0x0000, sess->snac_nextid);
247 i+= aimutil_put16(newpacket->data+i, 0x000e);
248 i+= aimutil_put16(newpacket->data+i, 0x0001);
250 i+= aimutil_put16(newpacket->data+i, 0x0004);
251 i+= aimutil_put16(newpacket->data+i, 0x0001);
253 i+= aimutil_put16(newpacket->data+i, 0x0001);
254 i+= aimutil_put16(newpacket->data+i, 0x0003);
256 i+= aimutil_put16(newpacket->data+i, 0x0004);
257 i+= aimutil_put16(newpacket->data+i, 0x0686);
260 aim_tx_enqueue(sess, newpacket);
262 return (sess->snac_nextid++);
265 faim_export int aim_chat_leaveroom(struct aim_session_t *sess, char *name)
267 struct aim_conn_t *conn;
269 if ((conn = aim_chat_getconn(sess, name)))
270 aim_conn_close(conn);
278 * conn must be a BOS connection!
280 faim_export unsigned long aim_chat_invite(struct aim_session_t *sess,
281 struct aim_conn_t *conn,
288 struct command_tx_struct *newpacket;
290 struct aim_msgcookie_t *cookie;
291 struct aim_invite_priv *priv;
293 if (!sess || !conn || !sn || !msg || !roomname)
296 if (conn->type != AIM_CONN_TYPE_BOS)
299 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
304 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
310 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char)rand());
312 /* XXX this should get uncached by the unwritten 'invite accept' handler */
313 if(!(priv = calloc(sizeof(struct aim_invite_priv), 1)))
315 priv->sn = strdup(sn);
316 priv->roomname = strdup(roomname);
317 priv->exchange = exchange;
318 priv->instance = instance;
320 if(!(cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_INVITE, priv)))
322 aim_cachecookie(sess, cookie);
327 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
332 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
333 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
338 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
339 curbyte += aimutil_put16(newpacket->data+curbyte, 0x28+strlen(msg)+0x04+0x03+strlen(roomname)+0x02);
344 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
345 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3131);
346 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3538);
347 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3446);
348 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4100);
349 curbyte += aimutil_put16(newpacket->data+curbyte, 0x748f);
350 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2420);
351 curbyte += aimutil_put16(newpacket->data+curbyte, 0x6287);
352 curbyte += aimutil_put16(newpacket->data+curbyte, 0x11d1);
353 curbyte += aimutil_put16(newpacket->data+curbyte, 0x8222);
354 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4445);
355 curbyte += aimutil_put16(newpacket->data+curbyte, 0x5354);
356 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
359 * TLV t(000a) -- Unknown
361 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
362 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
363 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
366 * TLV t(000f) -- Unknown
368 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
369 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
372 * TLV t(000c) -- Invitation message
374 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000c, strlen(msg), msg);
377 * TLV t(2711) -- Container for room information
379 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711);
380 curbyte += aimutil_put16(newpacket->data+curbyte, 3+strlen(roomname)+2);
381 curbyte += aimutil_put16(newpacket->data+curbyte, exchange);
382 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(roomname));
383 curbyte += aimutil_putstr(newpacket->data+curbyte, roomname, strlen(roomname));
384 curbyte += aimutil_put16(newpacket->data+curbyte, instance);
386 newpacket->commandlen = curbyte;
388 aim_tx_enqueue(sess, newpacket);
390 return (sess->snac_nextid++);
394 * General room information. Lots of stuff.
396 * Values I know are in here but I havent attached
397 * them to any of the 'Unknown's:
398 * - Language (English)
402 static int infoupdate(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
404 struct aim_userinfo_s *userinfo = NULL;
405 aim_rxcallback_t userfunc=NULL;
408 unsigned char detaillevel = 0;
409 char *roomname = NULL;
410 struct aim_chat_roominfo roominfo;
411 unsigned short tlvcount = 0;
412 struct aim_tlvlist_t *tlvlist;
413 char *roomdesc = NULL;
414 unsigned short unknown_c9 = 0;
415 unsigned long creationtime = 0;
416 unsigned short maxmsglen = 0;
417 unsigned short unknown_d2 = 0, unknown_d5 = 0;
419 i += aim_chat_readroominfo(data+i, &roominfo);
421 detaillevel = aimutil_get8(data+i);
424 if (detaillevel != 0x02) {
425 if (detaillevel == 0x01)
426 faimdprintf(sess, 0, "faim: chat_roomupdateinfo: detail level 1 not supported\n");
428 faimdprintf(sess, 0, "faim: chat_roomupdateinfo: unknown detail level %d\n", detaillevel);
432 tlvcount = aimutil_get16(data+i);
436 * Everything else are TLVs.
438 tlvlist = aim_readtlvchain(data+i, datalen-i);
441 * TLV type 0x006a is the room name in Human Readable Form.
443 if (aim_gettlv(tlvlist, 0x006a, 1))
444 roomname = aim_gettlv_str(tlvlist, 0x006a, 1);
447 * Type 0x006f: Number of occupants.
449 if (aim_gettlv(tlvlist, 0x006f, 1))
450 usercount = aim_gettlv16(tlvlist, 0x006f, 1);
453 * Type 0x0073: Occupant list.
455 if (aim_gettlv(tlvlist, 0x0073, 1)) {
457 struct aim_tlv_t *tmptlv;
459 tmptlv = aim_gettlv(tlvlist, 0x0073, 1);
461 /* Allocate enough userinfo structs for all occupants */
462 userinfo = calloc(usercount, sizeof(struct aim_userinfo_s));
465 while (curoccupant < usercount)
466 i += aim_extractuserinfo(sess, tmptlv->value+i, &userinfo[curoccupant++]);
470 * Type 0x00c9: Unknown. (2 bytes)
472 if (aim_gettlv(tlvlist, 0x00c9, 1))
473 unknown_c9 = aim_gettlv16(tlvlist, 0x00c9, 1);
476 * Type 0x00ca: Creation time (4 bytes)
478 if (aim_gettlv(tlvlist, 0x00ca, 1))
479 creationtime = aim_gettlv32(tlvlist, 0x00ca, 1);
482 * Type 0x00d1: Maximum Message Length
484 if (aim_gettlv(tlvlist, 0x00d1, 1))
485 maxmsglen = aim_gettlv16(tlvlist, 0x00d1, 1);
488 * Type 0x00d2: Unknown. (2 bytes)
490 if (aim_gettlv(tlvlist, 0x00d2, 1))
491 unknown_d2 = aim_gettlv16(tlvlist, 0x00d2, 1);
494 * Type 0x00d3: Room Description
496 if (aim_gettlv(tlvlist, 0x00d3, 1))
497 roomdesc = aim_gettlv_str(tlvlist, 0x00d3, 1);
500 * Type 0x00d5: Unknown. (1 byte)
502 if (aim_gettlv(tlvlist, 0x00d5, 1))
503 unknown_d5 = aim_gettlv8(tlvlist, 0x00d5, 1);
506 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) {
525 aim_freetlvchain(&tlvlist);
530 static int userlistchange(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
532 struct aim_userinfo_s *userinfo = NULL;
533 aim_rxcallback_t userfunc;
534 int i = 0, curcount = 0, ret = 0;
536 while (i < datalen) {
538 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
539 i += aim_extractuserinfo(sess, data+i, &userinfo[curcount-1]);
542 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
543 ret = userfunc(sess, rx, curcount, userinfo);
551 * We could probably include this in the normal ICBM parsing
552 * code as channel 0x0003, however, since only the start
553 * would be the same, we might as well do it here.
555 * General outline of this SNAC:
573 static int incomingmsg(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
575 struct aim_userinfo_s userinfo;
576 aim_rxcallback_t userfunc=NULL;
578 unsigned char cookie[8];
580 struct aim_tlvlist_t *outerlist;
582 struct aim_msgcookie_t *ck;
584 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
587 * ICBM Cookie. Cache it.
589 memcpy(cookie, data, 8);
592 if ((ck = aim_uncachecookie(sess, cookie, AIM_COOKIETYPE_CHAT))) {
601 * Channels 1 and 2 are implemented in the normal ICBM
604 * We only do channel 3 here.
607 channel = aimutil_get16(data+i);
610 if (channel != 0x0003) {
611 faimdprintf(sess, 0, "faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
616 * Start parsing TLVs right away.
618 outerlist = aim_readtlvchain(data+8+2, datalen-8-2);
621 * Type 0x0003: Source User Information
623 if (aim_gettlv(outerlist, 0x0003, 1)) {
624 struct aim_tlv_t *userinfotlv;
626 userinfotlv = aim_gettlv(outerlist, 0x0003, 1);
627 aim_extractuserinfo(sess, userinfotlv->value, &userinfo);
631 * Type 0x0001: Unknown.
633 if (aim_gettlv(outerlist, 0x0001, 1))
637 * Type 0x0005: Message Block. Conains more TLVs.
639 if (aim_gettlv(outerlist, 0x0005, 1)) {
640 struct aim_tlvlist_t *innerlist;
641 struct aim_tlv_t *msgblock;
643 msgblock = aim_gettlv(outerlist, 0x0005, 1);
644 innerlist = aim_readtlvchain(msgblock->value, msgblock->length);
647 * Type 0x0001: Message.
649 if (aim_gettlv(innerlist, 0x0001, 1))
650 msg = aim_gettlv_str(innerlist, 0x0001, 1);
652 aim_freetlvchain(&innerlist);
655 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
656 ret = userfunc(sess, rx, &userinfo, msg);
659 aim_freetlvchain(&outerlist);
664 static int snachandler(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
667 if (snac->subtype == 0x0002)
668 return infoupdate(sess, mod, rx, snac, data, datalen);
669 else if ((snac->subtype == 0x0003) || (snac->subtype == 0x0004))
670 return userlistchange(sess, mod, rx, snac, data, datalen);
671 else if (snac->subtype == 0x0006)
672 return incomingmsg(sess, mod, rx, snac, data, datalen);
677 faim_internal int chat_modfirst(struct aim_session_t *sess, aim_module_t *mod)
680 mod->family = 0x000e;
681 mod->version = 0x0000;
683 strncpy(mod->name, "chat", sizeof(mod->name));
684 mod->snachandler = snachandler;