4 * Routines for the Chat service.
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)
22 struct aim_conn_t *cur;
24 faim_mutex_lock(&sess->connlistlock);
25 for (cur = sess->connlist; cur; cur = cur->next) {
26 if (cur->type != AIM_CONN_TYPE_CHAT)
28 if (strcmp((char *)cur->priv, name) == 0)
31 faim_mutex_unlock(&sess->connlistlock);
36 int aim_chat_attachname(struct aim_conn_t *conn, char *roomname)
38 if (!conn || !roomname)
41 conn->priv = malloc(strlen(roomname)+1);
42 strcpy(conn->priv, roomname);
47 u_long aim_chat_send_im(struct aim_session_t *sess,
48 struct aim_conn_t *conn,
53 struct command_tx_struct *newpacket;
55 if (!sess || !conn || !msg)
58 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
61 newpacket->lock = 1; /* lock struct */
64 curbyte += aim_putsnac(newpacket->data+curbyte,
65 0x000e, 0x0005, 0x0000, sess->snac_nextid);
68 * Generate a random message cookie
71 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) random());
74 * metaTLV start. -- i assume this is a metaTLV. it could be the
77 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
80 * Type 1: Unknown. Blank.
82 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
83 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
86 * Type 6: Unknown. Blank.
88 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0006);
89 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
92 * Type 5: Message block. Contains more TLVs.
94 * This could include other information... We just
95 * put in a message TLV however.
98 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
99 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg)+4);
102 * SubTLV: Type 1: Message
104 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(msg), msg);
106 newpacket->commandlen = curbyte;
109 aim_tx_enqueue(sess, newpacket);
111 return (sess->snac_nextid++);
115 * Join a room of name roomname. This is the first
116 * step to joining an already created room. It's
117 * basically a Service Request for family 0x000e,
118 * with a little added on to specify the exchange
122 u_long aim_chat_join(struct aim_session_t *sess,
123 struct aim_conn_t *conn,
125 const char *roomname)
127 struct command_tx_struct *newpacket;
130 if (!sess || !conn || !roomname)
133 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+9+strlen(roomname)+2)))
138 i = aim_putsnac(newpacket->data, 0x0001, 0x0004, 0x0000, sess->snac_nextid);
140 i+= aimutil_put16(newpacket->data+i, 0x000e);
143 * this is techinally a TLV, but we can't use normal functions
144 * because we need the extraneous nulls and other weird things.
146 i+= aimutil_put16(newpacket->data+i, 0x0001);
147 i+= aimutil_put16(newpacket->data+i, 2+1+strlen(roomname)+2);
148 i+= aimutil_put16(newpacket->data+i, exchange);
149 i+= aimutil_put8(newpacket->data+i, strlen(roomname));
150 memcpy(newpacket->data+i, roomname, strlen(roomname));
151 i+= strlen(roomname);
152 //i+= aimutil_putstr(newpacket->data+i, roomname, strlen(roomname));
153 i+= aimutil_put16(newpacket->data+i, 0x0000);
158 * XXX: A problem occurs here if we request a channel
159 * join but it fails....pendingjoin will be nonnull
160 * even though the channel is never going to get a
164 sess->pendingjoin = (char *)malloc(strlen(roomname)+1);
165 strcpy(sess->pendingjoin, roomname);
168 aim_tx_enqueue(sess, newpacket);
172 struct aim_snac_t snac;
174 snac.id = sess->snac_nextid;
175 snac.family = 0x0001;
179 snac.data = malloc(strlen(roomname)+1);
180 strcpy(snac.data, roomname);
182 aim_newsnac(sess, &snac);
186 return (sess->snac_nextid++);
189 int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
194 if (!buf || !outinfo)
197 outinfo->exchange = aimutil_get16(buf+i);
200 namelen = aimutil_get8(buf+i);
203 outinfo->name = (char *)malloc(namelen+1);
204 memcpy(outinfo->name, buf+i, namelen);
205 outinfo->name[namelen] = '\0';
208 outinfo->instance = aimutil_get16(buf+i);
216 * General room information. Lots of stuff.
218 * Values I know are in here but I havent attached
219 * them to any of the 'Unknown's:
220 * - Language (English)
224 int aim_chat_parse_infoupdate(struct aim_session_t *sess,
225 struct command_rx_struct *command)
227 struct aim_userinfo_s *userinfo = NULL;
228 rxcallback_t userfunc=NULL;
231 u_char detaillevel = 0;
232 char *roomname = NULL;
233 struct aim_chat_roominfo roominfo;
234 u_short tlvcount = 0;
235 struct aim_tlvlist_t *tlvlist;
236 char *roomdesc = NULL;
237 struct aim_tlv_t *tmptlv;
238 unsigned short unknown_c9 = 0;
239 unsigned long creationtime = 0;
240 unsigned short maxmsglen = 0;
241 unsigned short unknown_d2 = 0, unknown_d5 = 0;
244 i += aim_chat_readroominfo(command->data+i, &roominfo);
246 detaillevel = aimutil_get8(command->data+i);
249 if (detaillevel != 0x02) {
250 if (detaillevel == 0x01)
251 printf("faim: chat_roomupdateinfo: detail level 2 not supported\n");
253 printf("faim: chat_roomupdateinfo: unknown detail level %d\n", detaillevel);
257 tlvcount = aimutil_get16(command->data+i);
261 * Everything else are TLVs.
263 tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
266 * TLV type 0x006a is the room name in Human Readable Form.
268 if (aim_gettlv(tlvlist, 0x006a, 1))
269 roomname = aim_gettlv_str(tlvlist, 0x006a, 1);
272 * Type 0x006f: Number of occupants.
274 if (aim_gettlv(tlvlist, 0x006f, 1)) {
275 struct aim_tlv_t *tmptlv;
276 tmptlv = aim_gettlv(tlvlist, 0x006f, 1);
278 usercount = aimutil_get16(tmptlv->value);
282 * Type 0x0073: Occupant list.
284 if (aim_gettlv(tlvlist, 0x0073, 1)) {
286 struct aim_tlv_t *tmptlv;
288 tmptlv = aim_gettlv(tlvlist, 0x0073, 1);
290 /* Allocate enough userinfo structs for all occupants */
291 userinfo = calloc(usercount, sizeof(struct aim_userinfo_s));
294 while (curoccupant < usercount)
295 i += aim_extractuserinfo(tmptlv->value+i, &userinfo[curoccupant++]);
299 * Type 0x00c9: Unknown. (2 bytes)
301 if ((tmptlv = aim_gettlv(tlvlist, 0x00c9, 1)))
302 unknown_c9 = aimutil_get16(tmptlv->value);
305 * Type 0x00ca: Creation time (4 bytes)
307 if ((tmptlv = aim_gettlv(tlvlist, 0x00ca, 1)))
308 creationtime = aimutil_get32(tmptlv->value);
311 * Type 0x00d1: Maximum Message Length
313 if ((tmptlv = aim_gettlv(tlvlist, 0x00d1, 1)))
314 maxmsglen = aimutil_get16(tmptlv->value);
317 * Type 0x00d2: Unknown. (2 bytes)
319 if ((tmptlv = aim_gettlv(tlvlist, 0x00d2, 1)))
320 unknown_d2 = aimutil_get16(tmptlv->value);;
323 * Type 0x00d3: Room Description
325 if (aim_gettlv(tlvlist, 0x00d3, 1))
326 roomdesc = aim_gettlv_str(tlvlist, 0x00d3, 1);
329 * Type 0x00d5: Unknown. (1 byte)
331 if ((tmptlv = aim_gettlv(tlvlist, 0x00d5, 1)))
332 unknown_d5 = aimutil_get8(tmptlv->value);;
335 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_ROOMINFOUPDATE);
354 aim_freetlvchain(&tlvlist);
359 int aim_chat_parse_joined(struct aim_session_t *sess,
360 struct command_rx_struct *command)
362 struct aim_userinfo_s *userinfo = NULL;
363 rxcallback_t userfunc=NULL;
364 int i = 10, curcount = 0, ret = 1;
366 while (i < command->commandlen) {
368 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
369 i += aim_extractuserinfo(command->data+i, &userinfo[curcount-1]);
372 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERJOIN);
385 int aim_chat_parse_leave(struct aim_session_t *sess,
386 struct command_rx_struct *command)
389 struct aim_userinfo_s *userinfo = NULL;
390 rxcallback_t userfunc=NULL;
391 int i = 10, curcount = 0, ret = 1;
393 while (i < command->commandlen) {
395 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
396 i += aim_extractuserinfo(command->data+i, &userinfo[curcount-1]);
399 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_USERLEAVE);
413 * We could probably include this in the normal ICBM parsing
414 * code as channel 0x0003, however, since only the start
415 * would be the same, we might as well do it here.
417 int aim_chat_parse_incoming(struct aim_session_t *sess,
418 struct command_rx_struct *command)
420 struct aim_userinfo_s userinfo;
421 rxcallback_t userfunc=NULL;
422 int ret = 1, i = 0, z = 0;
425 struct aim_tlvlist_t *outerlist;
428 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
430 i = 10; /* skip snac */
433 * ICBM Cookie. Ignore it.
435 for (z=0; z<8; z++,i++)
436 cookie[z] = command->data[i];
441 * Channels 1 and 2 are implemented in the normal ICBM
444 * We only do channel 3 here.
447 channel = aimutil_get16(command->data+i);
450 if (channel != 0x0003) {
451 printf("faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
456 * Start parsing TLVs right away.
458 outerlist = aim_readtlvchain(command->data+i, command->commandlen-i);
461 * Type 0x0003: Source User Information
463 if (aim_gettlv(outerlist, 0x0003, 1)) {
464 struct aim_tlv_t *userinfotlv;
466 userinfotlv = aim_gettlv(outerlist, 0x0003, 1);
467 aim_extractuserinfo(userinfotlv->value, &userinfo);
471 * Type 0x0001: Unknown.
473 if (aim_gettlv(outerlist, 0x0001, 1))
477 * Type 0x0005: Message Block. Conains more TLVs.
479 if (aim_gettlv(outerlist, 0x0005, 1))
481 struct aim_tlvlist_t *innerlist;
482 struct aim_tlv_t *msgblock;
484 msgblock = aim_gettlv(outerlist, 0x0005, 1);
485 innerlist = aim_readtlvchain(msgblock->value, msgblock->length);
488 * Type 0x0001: Message.
490 if (aim_gettlv(innerlist, 0x0001, 1))
491 msg = aim_gettlv_str(innerlist, 0x0001, 1);
493 aim_freetlvchain(&innerlist);
496 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_CHT, AIM_CB_CHT_INCOMINGMSG);
505 aim_freetlvchain(&outerlist);
510 u_long aim_chat_clientready(struct aim_session_t *sess,
511 struct aim_conn_t *conn)
513 struct command_tx_struct *newpacket;
516 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 0x20)))
521 i = aim_putsnac(newpacket->data, 0x0001, 0x0002, 0x0000, sess->snac_nextid);
523 i+= aimutil_put16(newpacket->data+i, 0x000e);
524 i+= aimutil_put16(newpacket->data+i, 0x0001);
526 i+= aimutil_put16(newpacket->data+i, 0x0004);
527 i+= aimutil_put16(newpacket->data+i, 0x0001);
529 i+= aimutil_put16(newpacket->data+i, 0x0001);
530 i+= aimutil_put16(newpacket->data+i, 0x0003);
532 i+= aimutil_put16(newpacket->data+i, 0x0004);
533 i+= aimutil_put16(newpacket->data+i, 0x0686);
536 aim_tx_enqueue(sess, newpacket);
538 return (sess->snac_nextid++);
541 int aim_chat_leaveroom(struct aim_session_t *sess, char *name)
543 struct aim_conn_t *conn;
545 if ((conn = aim_chat_getconn(sess, name)))
546 aim_conn_kill(sess, &conn);
554 * conn must be a BOS connection!
556 u_long aim_chat_invite(struct aim_session_t *sess,
557 struct aim_conn_t *conn,
564 struct command_tx_struct *newpacket;
567 if (!sess || !conn || !sn || !msg || !roomname)
570 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
575 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
581 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char)rand());
586 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
591 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
592 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
597 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
598 curbyte += aimutil_put16(newpacket->data+curbyte, 0x28+strlen(msg)+0x04+0x03+strlen(roomname)+0x02);
603 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
604 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3131);
605 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3538);
606 curbyte += aimutil_put16(newpacket->data+curbyte, 0x3446);
607 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4100);
608 curbyte += aimutil_put16(newpacket->data+curbyte, 0x748f);
609 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2420);
610 curbyte += aimutil_put16(newpacket->data+curbyte, 0x6287);
611 curbyte += aimutil_put16(newpacket->data+curbyte, 0x11d1);
612 curbyte += aimutil_put16(newpacket->data+curbyte, 0x8222);
613 curbyte += aimutil_put16(newpacket->data+curbyte, 0x4445);
614 curbyte += aimutil_put16(newpacket->data+curbyte, 0x5354);
615 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
618 * TLV t(000a) -- Unknown
620 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
621 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
622 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
625 * TLV t(000f) -- Unknown
627 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
628 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
631 * TLV t(000c) -- Invitation message
633 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000c, strlen(msg), msg);
636 * TLV t(2711) -- Container for room information
638 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711);
639 curbyte += aimutil_put16(newpacket->data+curbyte, 3+strlen(roomname)+2);
640 curbyte += aimutil_put16(newpacket->data+curbyte, exchange);
641 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(roomname));
642 curbyte += aimutil_putstr(newpacket->data+curbyte, roomname, strlen(roomname));
643 curbyte += aimutil_put16(newpacket->data+curbyte, instance);
645 newpacket->commandlen = curbyte;
647 aim_tx_enqueue(sess, newpacket);
649 return (sess->snac_nextid++);