4 * Routines for the Chat service. Nothing works (yet).
10 char *aim_chat_getname(struct aim_conn_t *conn)
14 if (conn->type != AIM_CONN_TYPE_CHAT)
17 return (char *)conn->priv; /* yuck ! */
20 struct aim_conn_t *aim_chat_getconn(struct aim_session_t *sess, char *name)
24 for (i=0;i<AIM_CONN_MAX;i++)
26 if (sess->conns[i].type == AIM_CONN_TYPE_CHAT)
28 if (sess->conns[i].priv)
29 if (!strcmp((char *)sess->conns[i].priv, name))
31 return &sess->conns[i];
38 int aim_chat_attachname(struct aim_conn_t *conn, char *roomname)
40 if (!conn || !roomname)
43 conn->priv = malloc(strlen(roomname)+1);
44 strcpy(conn->priv, roomname);
49 u_long aim_chat_send_im(struct aim_session_t *sess,
50 struct aim_conn_t *conn,
55 struct command_tx_struct newpacket;
57 if (!sess || !conn || !msg)
60 newpacket.lock = 1; /* lock struct */
61 newpacket.type = 0x02; /* IMs are always family 0x02 */
64 * Since we must have a specific connection, then theres
65 * no use in going on if we don't have one...
68 return sess->snac_nextid;
69 newpacket.conn = conn;
72 * Its simplest to set this arbitrarily large and waste
73 * space. Precalculating is costly here.
75 newpacket.commandlen = 1152;
77 newpacket.data = (u_char *) calloc(1, newpacket.commandlen);
80 curbyte += aim_putsnac(newpacket.data+curbyte,
81 0x000e, 0x0005, 0x0000, sess->snac_nextid);
84 * Generate a random message cookie
88 curbyte += aimutil_put8(newpacket.data+curbyte, (u_char) random());
91 * metaTLV start. -- i assume this is a metaTLV. it could be the
94 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0003);
97 * Type 1: Unknown. Blank.
99 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0001);
100 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
103 * Type 6: Unknown. Blank.
105 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0006);
106 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
109 * Type 5: Message block. Contains more TLVs.
111 * This could include other information... We just
112 * put in a message TLV however.
115 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0005);
116 curbyte += aimutil_put16(newpacket.data+curbyte, strlen(msg)+4);
119 * SubTLV: Type 1: Message
121 curbyte += aim_puttlv_str(newpacket.data+curbyte, 0x0001, strlen(msg), msg);
123 newpacket.commandlen = curbyte;
126 aim_tx_enqueue(sess, &newpacket);
128 return (sess->snac_nextid++);
132 * Join a room of name roomname. This is the first
133 * step to joining an already created room. It's
134 * basically a Service Request for family 0x000e,
135 * with a little added on to specify the exchange
139 u_long aim_chat_join(struct aim_session_t *sess,
140 struct aim_conn_t *conn,
142 const char *roomname)
144 struct command_tx_struct newpacket;
147 if (!sess || !conn || !roomname)
152 newpacket.conn = conn;
154 newpacket.conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
156 newpacket.type = 0x02;
158 newpacket.commandlen = 10 + 9 + strlen(roomname) + 2;
159 newpacket.data = (char *) malloc(newpacket.commandlen);
160 memset(newpacket.data, 0x00, newpacket.commandlen);
162 i = aim_putsnac(newpacket.data, 0x0001, 0x0004, 0x0000, sess->snac_nextid);
164 i+= aimutil_put16(newpacket.data+i, 0x000e);
167 * this is techinally a TLV, but we can't use normal functions
168 * because we need the extraneous nulls and other weird things.
170 i+= aimutil_put16(newpacket.data+i, 0x0001);
171 i+= aimutil_put16(newpacket.data+i, 2+1+strlen(roomname)+2);
172 i+= aimutil_put16(newpacket.data+i, exchange);
173 i+= aimutil_put8(newpacket.data+i, strlen(roomname));
174 memcpy(newpacket.data+i, roomname, strlen(roomname));
175 i+= strlen(roomname);
176 //i+= aimutil_putstr(newpacket.data+i, roomname, strlen(roomname));
177 i+= aimutil_put16(newpacket.data+i, 0x0000);
182 * XXX: A problem occurs here if we request a channel
183 * join but it fails....pendingjoin will be nonnull
184 * even though the channel is never going to get a
188 sess->pendingjoin = (char *)malloc(strlen(roomname)+1);
189 strcpy(sess->pendingjoin, roomname);
192 aim_tx_enqueue(sess, &newpacket);
196 struct aim_snac_t snac;
198 snac.id = sess->snac_nextid;
199 snac.family = 0x0001;
203 snac.data = malloc(strlen(roomname)+1);
204 strcpy(snac.data, roomname);
206 aim_newsnac(sess, &snac);
210 return (sess->snac_nextid++);
213 int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
218 if (!buf || !outinfo)
221 outinfo->exchange = aimutil_get16(buf+i);
224 namelen = aimutil_get8(buf+i);
227 outinfo->name = (char *)malloc(namelen+1);
228 memcpy(outinfo->name, buf+i, namelen);
229 outinfo->name[namelen] = '\0';
232 outinfo->instance = aimutil_get16(buf+i);
240 * General room information. Lots of stuff.
242 * Values I know are in here but I havent attached
243 * them to any of the 'Unknown's:
244 * - Language (English)
247 int aim_chat_parse_infoupdate(struct aim_session_t *sess,
248 struct command_rx_struct *command)
250 struct aim_userinfo_s *userinfo = NULL;
251 rxcallback_t userfunc=NULL;
254 u_char detaillevel = 0;
255 char *roomname = NULL;
256 struct aim_chat_roominfo roominfo;
257 u_short tlvcount = 0;
258 struct aim_tlvlist_t *tlvlist;
259 char *roomdesc = NULL;
262 i += aim_chat_readroominfo(command->data+i, &roominfo);
264 detaillevel = aimutil_get8(command->data+i);
267 tlvcount = aimutil_get16(command->data+i);
271 * Everything else are TLVs.
273 tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
276 * TLV type 0x006a is the room name in Human Readable Form.
278 if (aim_gettlv(tlvlist, 0x006a, 1))
279 roomname = aim_gettlv_str(tlvlist, 0x006a, 1);
282 * Type 0x006f: Number of occupants.
284 if (aim_gettlv(tlvlist, 0x006f, 1))
286 struct aim_tlv_t *tmptlv;
287 tmptlv = aim_gettlv(tlvlist, 0x006f, 1);
289 usercount = aimutil_get16(tmptlv->value);
293 * Type 0x0073: Occupant list.
295 if (aim_gettlv(tlvlist, 0x0073, 1))
298 struct aim_tlv_t *tmptlv;
300 tmptlv = aim_gettlv(tlvlist, 0x0073, 1);
302 /* Allocate enough userinfo structs for all occupants */
303 userinfo = calloc(usercount, sizeof(struct aim_userinfo_s));
306 while (curoccupant < usercount)
307 i += aim_extractuserinfo(tmptlv->value+i, &userinfo[curoccupant++]);
311 * Type 0x00c9: Unknown.
313 if (aim_gettlv(tlvlist, 0x00c9, 1))
317 * Type 0x00ca: Creation date
319 if (aim_gettlv(tlvlist, 0x00ca, 1))
323 * Type 0x00d1: Maximum Message Length
325 if (aim_gettlv(tlvlist, 0x00d1, 1))
329 * Type 0x00d2: Unknown.
331 if (aim_gettlv(tlvlist, 0x00d2, 1))
335 * Type 0x00d3: Room Description
337 if (aim_gettlv(tlvlist, 0x00d3, 1))
338 roomdesc = aim_gettlv_str(tlvlist, 0x00d3, 1);
341 * Type 0x00d5: Unknown.
343 if (aim_gettlv(tlvlist, 0x00d5, 1))
347 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_ROOMINFOUPDATE);
362 aim_freetlvchain(&tlvlist);
367 int aim_chat_parse_joined(struct aim_session_t *sess,
368 struct command_rx_struct *command)
370 struct aim_userinfo_s *userinfo = NULL;
371 rxcallback_t userfunc=NULL;
372 int i = 10, curcount = 0, ret = 1;
374 while (i < command->commandlen)
377 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
378 i += aim_extractuserinfo(command->data+i, &userinfo[curcount-1]);
381 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERJOIN);
395 int aim_chat_parse_leave(struct aim_session_t *sess,
396 struct command_rx_struct *command)
399 struct aim_userinfo_s *userinfo = NULL;
400 rxcallback_t userfunc=NULL;
401 int i = 10, curcount = 0, ret = 1;
403 while (i < command->commandlen)
406 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
407 i += aim_extractuserinfo(command->data+i, &userinfo[curcount-1]);
410 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERLEAVE);
425 * We could probably include this in the normal ICBM parsing
426 * code as channel 0x0003, however, since only the start
427 * would be the same, we might as well do it here.
429 int aim_chat_parse_incoming(struct aim_session_t *sess,
430 struct command_rx_struct *command)
432 struct aim_userinfo_s userinfo;
433 rxcallback_t userfunc=NULL;
434 int ret = 1, i = 0, z = 0;
437 struct aim_tlvlist_t *outerlist;
440 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
442 i = 10; /* skip snac */
445 * ICBM Cookie. Ignore it.
447 for (z=0; z<8; z++,i++)
448 cookie[z] = command->data[i];
453 * Channels 1 and 2 are implemented in the normal ICBM
456 * We only do channel 3 here.
459 channel = aimutil_get16(command->data+i);
462 if (channel != 0x0003)
464 printf("faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
469 * Start parsing TLVs right away.
471 outerlist = aim_readtlvchain(command->data+i, command->commandlen-i);
474 * Type 0x0003: Source User Information
476 if (aim_gettlv(outerlist, 0x0003, 1))
478 struct aim_tlv_t *userinfotlv;
480 userinfotlv = aim_gettlv(outerlist, 0x0003, 1);
481 aim_extractuserinfo(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(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_INCOMINGMSG);
519 aim_freetlvchain(&outerlist);
524 u_long aim_chat_clientready(struct aim_session_t *sess,
525 struct aim_conn_t *conn)
527 struct command_tx_struct newpacket;
532 newpacket.conn = conn;
534 newpacket.conn = aim_getconn_type(sess, AIM_CONN_TYPE_CHAT);
535 newpacket.type = 0x02;
536 newpacket.commandlen = 0x20;
538 newpacket.data = (char *) malloc(newpacket.commandlen);
539 i = aim_putsnac(newpacket.data, 0x0001, 0x0002, 0x0000, sess->snac_nextid);
541 i+= aimutil_put16(newpacket.data+i, 0x000e);
542 i+= aimutil_put16(newpacket.data+i, 0x0001);
544 i+= aimutil_put16(newpacket.data+i, 0x0004);
545 i+= aimutil_put16(newpacket.data+i, 0x0001);
547 i+= aimutil_put16(newpacket.data+i, 0x0001);
548 i+= aimutil_put16(newpacket.data+i, 0x0003);
550 i+= aimutil_put16(newpacket.data+i, 0x0004);
551 i+= aimutil_put16(newpacket.data+i, 0x0686);
554 aim_tx_enqueue(sess, &newpacket);
556 return (sess->snac_nextid++);
559 int aim_chat_leaveroom(struct aim_session_t *sess, char *name)
563 for (i=0;i<AIM_CONN_MAX;i++)
565 if (sess->conns[i].type == AIM_CONN_TYPE_CHAT)
567 if (sess->conns[i].priv)
568 if (!strcmp((char *)sess->conns[i].priv, name))
570 aim_conn_close(&sess->conns[i]);
579 * conn must be a BOS connection!
581 u_long aim_chat_invite(struct aim_session_t *sess,
582 struct aim_conn_t *conn,
589 struct command_tx_struct newpacket;
592 if (!sess || !conn || !sn || !msg || !roomname)
597 newpacket.conn = conn;
599 newpacket.conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
600 newpacket.type = 0x02;
601 newpacket.commandlen = 1152+strlen(sn)+strlen(roomname)+strlen(msg);
603 newpacket.data = (char *) malloc(newpacket.commandlen);
604 curbyte = aim_putsnac(newpacket.data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
610 curbyte += aimutil_put8(newpacket.data+curbyte, (u_char)rand());
615 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0002);
620 curbyte += aimutil_put8(newpacket.data+curbyte, strlen(sn));
621 curbyte += aimutil_putstr(newpacket.data+curbyte, sn, strlen(sn));
626 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0005);
627 curbyte += aimutil_put16(newpacket.data+curbyte, 0x28+strlen(msg)+0x04+0x03+strlen(roomname)+0x02);
632 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
633 curbyte += aimutil_put16(newpacket.data+curbyte, 0x3131);
634 curbyte += aimutil_put16(newpacket.data+curbyte, 0x3538);
635 curbyte += aimutil_put16(newpacket.data+curbyte, 0x3446);
636 curbyte += aimutil_put16(newpacket.data+curbyte, 0x4100);
637 curbyte += aimutil_put16(newpacket.data+curbyte, 0x748f);
638 curbyte += aimutil_put16(newpacket.data+curbyte, 0x2420);
639 curbyte += aimutil_put16(newpacket.data+curbyte, 0x6287);
640 curbyte += aimutil_put16(newpacket.data+curbyte, 0x11d1);
641 curbyte += aimutil_put16(newpacket.data+curbyte, 0x8222);
642 curbyte += aimutil_put16(newpacket.data+curbyte, 0x4445);
643 curbyte += aimutil_put16(newpacket.data+curbyte, 0x5354);
644 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
647 * TLV t(000a) -- Unknown
649 curbyte += aimutil_put16(newpacket.data+curbyte, 0x000a);
650 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0002);
651 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0001);
654 * TLV t(000f) -- Unknown
656 curbyte += aimutil_put16(newpacket.data+curbyte, 0x000f);
657 curbyte += aimutil_put16(newpacket.data+curbyte, 0x0000);
660 * TLV t(000c) -- Invitation message
662 curbyte += aim_puttlv_str(newpacket.data+curbyte, 0x000c, strlen(msg), msg);
665 * TLV t(2711) -- Container for room information
667 curbyte += aimutil_put16(newpacket.data+curbyte, 0x2711);
668 curbyte += aimutil_put16(newpacket.data+curbyte, 3+strlen(roomname)+2);
669 curbyte += aimutil_put16(newpacket.data+curbyte, exchange);
670 curbyte += aimutil_put8(newpacket.data+curbyte, strlen(roomname));
671 curbyte += aimutil_putstr(newpacket.data+curbyte, roomname, strlen(roomname));
672 curbyte += aimutil_put16(newpacket.data+curbyte, instance);
674 newpacket.commandlen = curbyte;
676 aim_tx_enqueue(sess, &newpacket);
678 return (sess->snac_nextid++);