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);
211 faim_export unsigned long aim_chat_clientready(struct aim_session_t *sess,
212 struct aim_conn_t *conn)
214 struct command_tx_struct *newpacket;
217 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 0x20)))
222 i = aim_putsnac(newpacket->data, 0x0001, 0x0002, 0x0000, sess->snac_nextid);
224 i+= aimutil_put16(newpacket->data+i, 0x000e);
225 i+= aimutil_put16(newpacket->data+i, 0x0001);
227 i+= aimutil_put16(newpacket->data+i, 0x0004);
228 i+= aimutil_put16(newpacket->data+i, 0x0001);
230 i+= aimutil_put16(newpacket->data+i, 0x0001);
231 i+= aimutil_put16(newpacket->data+i, 0x0003);
233 i+= aimutil_put16(newpacket->data+i, 0x0004);
234 i+= aimutil_put16(newpacket->data+i, 0x0686);
237 aim_tx_enqueue(sess, newpacket);
239 return (sess->snac_nextid++);
242 faim_export int aim_chat_leaveroom(struct aim_session_t *sess, char *name)
244 struct aim_conn_t *conn;
246 if ((conn = aim_chat_getconn(sess, name)))
247 aim_conn_close(conn);
255 * conn must be a BOS connection!
257 faim_export unsigned long aim_chat_invite(struct aim_session_t *sess,
258 struct aim_conn_t *conn,
265 struct command_tx_struct *newpacket;
267 struct aim_msgcookie_t *cookie;
268 struct aim_invite_priv *priv;
270 if (!sess || !conn || !sn || !msg || !roomname)
273 if (conn->type != AIM_CONN_TYPE_BOS)
276 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
281 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
287 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char)rand());
289 /* XXX this should get uncached by the unwritten 'invite accept' handler */
290 if(!(priv = calloc(sizeof(struct aim_invite_priv), 1)))
292 priv->sn = strdup(sn);
293 priv->roomname = strdup(roomname);
294 priv->exchange = exchange;
295 priv->instance = instance;
297 if(!(cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_INVITE, priv)))
299 aim_cachecookie(sess, cookie);
304 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
309 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
310 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
315 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
316 curbyte += aimutil_put16(newpacket->data+curbyte, 0x28+strlen(msg)+0x04+0x03+strlen(roomname)+0x02);
321 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
322 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3131);
323 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3538);
324 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3446);
325 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4100);
326 curbyte += aimutil_put16(newpacket->data+curbyte, 0x748f);
327 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2420);
328 curbyte += aimutil_put16(newpacket->data+curbyte, 0x6287);
329 curbyte += aimutil_put16(newpacket->data+curbyte, 0x11d1);
330 curbyte += aimutil_put16(newpacket->data+curbyte, 0x8222);
331 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4445);
332 curbyte += aimutil_put16(newpacket->data+curbyte, 0x5354);
333 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
336 * TLV t(000a) -- Unknown
338 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
339 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
340 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
343 * TLV t(000f) -- Unknown
345 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
346 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
349 * TLV t(000c) -- Invitation message
351 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000c, strlen(msg), msg);
354 * TLV t(2711) -- Container for room information
356 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711);
357 curbyte += aimutil_put16(newpacket->data+curbyte, 3+strlen(roomname)+2);
358 curbyte += aimutil_put16(newpacket->data+curbyte, exchange);
359 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(roomname));
360 curbyte += aimutil_putstr(newpacket->data+curbyte, roomname, strlen(roomname));
361 curbyte += aimutil_put16(newpacket->data+curbyte, instance);
363 newpacket->commandlen = curbyte;
365 aim_tx_enqueue(sess, newpacket);
367 return (sess->snac_nextid++);
371 * General room information. Lots of stuff.
373 * Values I know are in here but I havent attached
374 * them to any of the 'Unknown's:
375 * - Language (English)
379 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)
381 struct aim_userinfo_s *userinfo = NULL;
382 rxcallback_t userfunc=NULL;
385 unsigned char detaillevel = 0;
386 char *roomname = NULL;
387 struct aim_chat_roominfo roominfo;
388 unsigned short tlvcount = 0;
389 struct aim_tlvlist_t *tlvlist;
390 char *roomdesc = NULL;
391 unsigned short unknown_c9 = 0;
392 unsigned long creationtime = 0;
393 unsigned short maxmsglen = 0;
394 unsigned short unknown_d2 = 0, unknown_d5 = 0;
396 i += aim_chat_readroominfo(data+i, &roominfo);
398 detaillevel = aimutil_get8(data+i);
401 if (detaillevel != 0x02) {
402 if (detaillevel == 0x01)
403 faimdprintf(sess, 0, "faim: chat_roomupdateinfo: detail level 1 not supported\n");
405 faimdprintf(sess, 0, "faim: chat_roomupdateinfo: unknown detail level %d\n", detaillevel);
409 tlvcount = aimutil_get16(data+i);
413 * Everything else are TLVs.
415 tlvlist = aim_readtlvchain(data+i, datalen-i);
418 * TLV type 0x006a is the room name in Human Readable Form.
420 if (aim_gettlv(tlvlist, 0x006a, 1))
421 roomname = aim_gettlv_str(tlvlist, 0x006a, 1);
424 * Type 0x006f: Number of occupants.
426 if (aim_gettlv(tlvlist, 0x006f, 1))
427 usercount = aim_gettlv16(tlvlist, 0x006f, 1);
430 * Type 0x0073: Occupant list.
432 if (aim_gettlv(tlvlist, 0x0073, 1)) {
434 struct aim_tlv_t *tmptlv;
436 tmptlv = aim_gettlv(tlvlist, 0x0073, 1);
438 /* Allocate enough userinfo structs for all occupants */
439 userinfo = calloc(usercount, sizeof(struct aim_userinfo_s));
442 while (curoccupant < usercount)
443 i += aim_extractuserinfo(sess, tmptlv->value+i, &userinfo[curoccupant++]);
447 * Type 0x00c9: Unknown. (2 bytes)
449 if (aim_gettlv(tlvlist, 0x00c9, 1))
450 unknown_c9 = aim_gettlv16(tlvlist, 0x00c9, 1);
453 * Type 0x00ca: Creation time (4 bytes)
455 if (aim_gettlv(tlvlist, 0x00ca, 1))
456 creationtime = aim_gettlv32(tlvlist, 0x00ca, 1);
459 * Type 0x00d1: Maximum Message Length
461 if (aim_gettlv(tlvlist, 0x00d1, 1))
462 maxmsglen = aim_gettlv16(tlvlist, 0x00d1, 1);
465 * Type 0x00d2: Unknown. (2 bytes)
467 if (aim_gettlv(tlvlist, 0x00d2, 1))
468 unknown_d2 = aim_gettlv16(tlvlist, 0x00d2, 1);
471 * Type 0x00d3: Room Description
473 if (aim_gettlv(tlvlist, 0x00d3, 1))
474 roomdesc = aim_gettlv_str(tlvlist, 0x00d3, 1);
477 * Type 0x00d5: Unknown. (1 byte)
479 if (aim_gettlv(tlvlist, 0x00d5, 1))
480 unknown_d5 = aim_gettlv8(tlvlist, 0x00d5, 1);
483 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) {
502 aim_freetlvchain(&tlvlist);
507 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)
509 struct aim_userinfo_s *userinfo = NULL;
510 rxcallback_t userfunc;
511 int i = 0, curcount = 0, ret = 0;
513 while (i < datalen) {
515 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
516 i += aim_extractuserinfo(sess, data+i, &userinfo[curcount-1]);
519 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
520 ret = userfunc(sess, rx, curcount, userinfo);
528 * We could probably include this in the normal ICBM parsing
529 * code as channel 0x0003, however, since only the start
530 * would be the same, we might as well do it here.
532 * General outline of this SNAC:
550 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)
552 struct aim_userinfo_s userinfo;
553 rxcallback_t userfunc=NULL;
555 unsigned char cookie[8];
557 struct aim_tlvlist_t *outerlist;
559 struct aim_msgcookie_t *ck;
561 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
564 * ICBM Cookie. Cache it.
566 memcpy(cookie, data, 8);
569 if ((ck = aim_uncachecookie(sess, cookie, AIM_COOKIETYPE_CHAT))) {
578 * Channels 1 and 2 are implemented in the normal ICBM
581 * We only do channel 3 here.
584 channel = aimutil_get16(data+i);
587 if (channel != 0x0003) {
588 faimdprintf(sess, 0, "faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
593 * Start parsing TLVs right away.
595 outerlist = aim_readtlvchain(data+8+2, datalen-8-2);
598 * Type 0x0003: Source User Information
600 if (aim_gettlv(outerlist, 0x0003, 1)) {
601 struct aim_tlv_t *userinfotlv;
603 userinfotlv = aim_gettlv(outerlist, 0x0003, 1);
604 aim_extractuserinfo(sess, userinfotlv->value, &userinfo);
608 * Type 0x0001: Unknown.
610 if (aim_gettlv(outerlist, 0x0001, 1))
614 * Type 0x0005: Message Block. Conains more TLVs.
616 if (aim_gettlv(outerlist, 0x0005, 1)) {
617 struct aim_tlvlist_t *innerlist;
618 struct aim_tlv_t *msgblock;
620 msgblock = aim_gettlv(outerlist, 0x0005, 1);
621 innerlist = aim_readtlvchain(msgblock->value, msgblock->length);
624 * Type 0x0001: Message.
626 if (aim_gettlv(innerlist, 0x0001, 1))
627 msg = aim_gettlv_str(innerlist, 0x0001, 1);
629 aim_freetlvchain(&innerlist);
632 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
633 ret = userfunc(sess, rx, &userinfo, msg);
636 aim_freetlvchain(&outerlist);
641 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)
644 if (snac->subtype == 0x0002)
645 return infoupdate(sess, mod, rx, snac, data, datalen);
646 else if ((snac->subtype == 0x0003) || (snac->subtype == 0x0004))
647 return userlistchange(sess, mod, rx, snac, data, datalen);
648 else if (snac->subtype == 0x0006)
649 return incomingmsg(sess, mod, rx, snac, data, datalen);
654 faim_internal int chat_modfirst(struct aim_session_t *sess, aim_module_t *mod)
657 mod->family = 0x000e;
658 mod->version = 0x0000;
660 strncpy(mod->name, "chat", sizeof(mod->name));
661 mod->snachandler = snachandler;