]> andersk Git - libfaim.git/blame_incremental - src/chat.c
- Sat Mar 24 03:16:32 UTC 2001
[libfaim.git] / src / chat.c
... / ...
CommitLineData
1/*
2 * aim_chat.c
3 *
4 * Routines for the Chat service.
5 *
6 */
7
8#define FAIM_INTERNAL
9#include <aim.h>
10
11faim_export char *aim_chat_getname(struct aim_conn_t *conn)
12{
13 if (!conn)
14 return NULL;
15 if (conn->type != AIM_CONN_TYPE_CHAT)
16 return NULL;
17
18 return (char *)conn->priv; /* yuck ! */
19}
20
21faim_export struct aim_conn_t *aim_chat_getconn(struct aim_session_t *sess, char *name)
22{
23 struct aim_conn_t *cur;
24
25 faim_mutex_lock(&sess->connlistlock);
26 for (cur = sess->connlist; cur; cur = cur->next) {
27 if (cur->type != AIM_CONN_TYPE_CHAT)
28 continue;
29 if (!cur->priv) {
30 faimdprintf(sess, 0, "faim: chat: chat connection with no name! (fd = %d)\n", cur->fd);
31 continue;
32 }
33 if (strcmp((char *)cur->priv, name) == 0)
34 break;
35 }
36 faim_mutex_unlock(&sess->connlistlock);
37
38 return cur;
39}
40
41faim_export int aim_chat_attachname(struct aim_conn_t *conn, char *roomname)
42{
43 if (!conn || !roomname)
44 return -1;
45
46 if (conn->priv)
47 free(conn->priv);
48
49 conn->priv = strdup(roomname);
50
51 return 0;
52}
53
54/* XXX convert this to use tlvchains */
55faim_export unsigned long aim_chat_send_im(struct aim_session_t *sess,
56 struct aim_conn_t *conn,
57 char *msg)
58{
59
60 int curbyte,i;
61 struct command_tx_struct *newpacket;
62 struct aim_msgcookie_t *cookie;
63
64 if (!sess || !conn || !msg)
65 return 0;
66
67 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 1152)))
68 return -1;
69
70 newpacket->lock = 1; /* lock struct */
71
72 curbyte = 0;
73 curbyte += aim_putsnac(newpacket->data+curbyte,
74 0x000e, 0x0005, 0x0000, sess->snac_nextid);
75
76 /*
77 * Generate a random message cookie
78 */
79 for (i=0;i<8;i++)
80 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) rand());
81
82 cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL);
83 cookie->data = strdup(conn->priv); /* chat hack dependent */
84
85 aim_cachecookie(sess, cookie);
86
87 /*
88 * Channel ID.
89 */
90 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
91
92 /*
93 * Type 1: Unknown. Blank.
94 */
95 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
96 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
97
98 /*
99 * Type 6: Unknown. Blank.
100 */
101 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0006);
102 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
103
104 /*
105 * Type 5: Message block. Contains more TLVs.
106 *
107 * This could include other information... We just
108 * put in a message TLV however.
109 *
110 */
111 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
112 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg)+4);
113
114 /*
115 * SubTLV: Type 1: Message
116 */
117 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x0001, strlen(msg), msg);
118
119 newpacket->commandlen = curbyte;
120
121 newpacket->lock = 0;
122 aim_tx_enqueue(sess, newpacket);
123
124 return (sess->snac_nextid++);
125}
126
127/*
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
132 * and room name.
133 *
134 */
135faim_export unsigned long aim_chat_join(struct aim_session_t *sess,
136 struct aim_conn_t *conn,
137 u_short exchange,
138 const char *roomname)
139{
140 struct command_tx_struct *newpacket;
141 int i;
142
143 if (!sess || !conn || !roomname)
144 return 0;
145
146 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+9+strlen(roomname)+2)))
147 return -1;
148
149 newpacket->lock = 1;
150
151 i = aim_putsnac(newpacket->data, 0x0001, 0x0004, 0x0000, sess->snac_nextid);
152
153 i+= aimutil_put16(newpacket->data+i, 0x000e);
154
155 /*
156 * this is techinally a TLV, but we can't use normal functions
157 * because we need the extraneous nulls and other weird things.
158 */
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? */
165
166 /*
167 * Chat hack.
168 *
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
172 * redirect!
173 *
174 */
175 sess->pendingjoin = strdup(roomname);
176 sess->pendingjoinexchange = exchange;
177
178 newpacket->lock = 0;
179 aim_tx_enqueue(sess, newpacket);
180
181 aim_cachesnac(sess, 0x0001, 0x0004, 0x0000, roomname, strlen(roomname)+1);
182
183 return sess->snac_nextid;
184}
185
186faim_internal int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo)
187{
188 int namelen = 0;
189 int i = 0;
190
191 if (!buf || !outinfo)
192 return 0;
193
194 outinfo->exchange = aimutil_get16(buf+i);
195 i += 2;
196
197 namelen = aimutil_get8(buf+i);
198 i += 1;
199
200 outinfo->name = (char *)malloc(namelen+1);
201 memcpy(outinfo->name, buf+i, namelen);
202 outinfo->name[namelen] = '\0';
203 i += namelen;
204
205 outinfo->instance = aimutil_get16(buf+i);
206 i += 2;
207
208 return i;
209}
210
211faim_export unsigned long aim_chat_clientready(struct aim_session_t *sess,
212 struct aim_conn_t *conn)
213{
214 struct command_tx_struct *newpacket;
215 int i;
216
217 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 0x20)))
218 return -1;
219
220 newpacket->lock = 1;
221
222 i = aim_putsnac(newpacket->data, 0x0001, 0x0002, 0x0000, sess->snac_nextid);
223
224 i+= aimutil_put16(newpacket->data+i, 0x000e);
225 i+= aimutil_put16(newpacket->data+i, 0x0001);
226
227 i+= aimutil_put16(newpacket->data+i, 0x0004);
228 i+= aimutil_put16(newpacket->data+i, 0x0001);
229
230 i+= aimutil_put16(newpacket->data+i, 0x0001);
231 i+= aimutil_put16(newpacket->data+i, 0x0003);
232
233 i+= aimutil_put16(newpacket->data+i, 0x0004);
234 i+= aimutil_put16(newpacket->data+i, 0x0686);
235
236 newpacket->lock = 0;
237 aim_tx_enqueue(sess, newpacket);
238
239 return (sess->snac_nextid++);
240}
241
242faim_export int aim_chat_leaveroom(struct aim_session_t *sess, char *name)
243{
244 struct aim_conn_t *conn;
245
246 if ((conn = aim_chat_getconn(sess, name)))
247 aim_conn_close(conn);
248
249 if (!conn)
250 return -1;
251 return 0;
252}
253
254/*
255 * conn must be a BOS connection!
256 */
257faim_export unsigned long aim_chat_invite(struct aim_session_t *sess,
258 struct aim_conn_t *conn,
259 char *sn,
260 char *msg,
261 u_short exchange,
262 char *roomname,
263 u_short instance)
264{
265 struct command_tx_struct *newpacket;
266 int i,curbyte=0;
267 struct aim_msgcookie_t *cookie;
268 struct aim_invite_priv *priv;
269
270 if (!sess || !conn || !sn || !msg || !roomname)
271 return -1;
272
273 if (conn->type != AIM_CONN_TYPE_BOS)
274 return -1;
275
276 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
277 return -1;
278
279 newpacket->lock = 1;
280
281 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
282
283 /*
284 * Cookie
285 */
286 for (i=0;i<8;i++)
287 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char)rand());
288
289 /* XXX this should get uncached by the unwritten 'invite accept' handler */
290 if(!(priv = calloc(sizeof(struct aim_invite_priv), 1)))
291 return -1;
292 priv->sn = strdup(sn);
293 priv->roomname = strdup(roomname);
294 priv->exchange = exchange;
295 priv->instance = instance;
296
297 if(!(cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_INVITE, priv)))
298 return -1;
299 aim_cachecookie(sess, cookie);
300
301 /*
302 * Channel (2)
303 */
304 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
305
306 /*
307 * Dest sn
308 */
309 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
310 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
311
312 /*
313 * TLV t(0005)
314 */
315 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
316 curbyte += aimutil_put16(newpacket->data+curbyte, 0x28+strlen(msg)+0x04+0x03+strlen(roomname)+0x02);
317
318 /*
319 * Unknown info
320 */
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);
334
335 /*
336 * TLV t(000a) -- Unknown
337 */
338 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
339 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
340 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
341
342 /*
343 * TLV t(000f) -- Unknown
344 */
345 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
346 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
347
348 /*
349 * TLV t(000c) -- Invitation message
350 */
351 curbyte += aim_puttlv_str(newpacket->data+curbyte, 0x000c, strlen(msg), msg);
352
353 /*
354 * TLV t(2711) -- Container for room information
355 */
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);
362
363 newpacket->commandlen = curbyte;
364 newpacket->lock = 0;
365 aim_tx_enqueue(sess, newpacket);
366
367 return (sess->snac_nextid++);
368}
369
370/*
371 * General room information. Lots of stuff.
372 *
373 * Values I know are in here but I havent attached
374 * them to any of the 'Unknown's:
375 * - Language (English)
376 *
377 * SNAC 000e/0002
378 */
379static 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)
380{
381 struct aim_userinfo_s *userinfo = NULL;
382 rxcallback_t userfunc=NULL;
383 int ret = 0, i = 0;
384 int usercount = 0;
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;
395
396 i += aim_chat_readroominfo(data+i, &roominfo);
397
398 detaillevel = aimutil_get8(data+i);
399 i++;
400
401 if (detaillevel != 0x02) {
402 if (detaillevel == 0x01)
403 faimdprintf(sess, 0, "faim: chat_roomupdateinfo: detail level 1 not supported\n");
404 else
405 faimdprintf(sess, 0, "faim: chat_roomupdateinfo: unknown detail level %d\n", detaillevel);
406 return 1;
407 }
408
409 tlvcount = aimutil_get16(data+i);
410 i += 2;
411
412 /*
413 * Everything else are TLVs.
414 */
415 tlvlist = aim_readtlvchain(data+i, datalen-i);
416
417 /*
418 * TLV type 0x006a is the room name in Human Readable Form.
419 */
420 if (aim_gettlv(tlvlist, 0x006a, 1))
421 roomname = aim_gettlv_str(tlvlist, 0x006a, 1);
422
423 /*
424 * Type 0x006f: Number of occupants.
425 */
426 if (aim_gettlv(tlvlist, 0x006f, 1))
427 usercount = aim_gettlv16(tlvlist, 0x006f, 1);
428
429 /*
430 * Type 0x0073: Occupant list.
431 */
432 if (aim_gettlv(tlvlist, 0x0073, 1)) {
433 int curoccupant = 0;
434 struct aim_tlv_t *tmptlv;
435
436 tmptlv = aim_gettlv(tlvlist, 0x0073, 1);
437
438 /* Allocate enough userinfo structs for all occupants */
439 userinfo = calloc(usercount, sizeof(struct aim_userinfo_s));
440
441 i = 0;
442 while (curoccupant < usercount)
443 i += aim_extractuserinfo(sess, tmptlv->value+i, &userinfo[curoccupant++]);
444 }
445
446 /*
447 * Type 0x00c9: Unknown. (2 bytes)
448 */
449 if (aim_gettlv(tlvlist, 0x00c9, 1))
450 unknown_c9 = aim_gettlv16(tlvlist, 0x00c9, 1);
451
452 /*
453 * Type 0x00ca: Creation time (4 bytes)
454 */
455 if (aim_gettlv(tlvlist, 0x00ca, 1))
456 creationtime = aim_gettlv32(tlvlist, 0x00ca, 1);
457
458 /*
459 * Type 0x00d1: Maximum Message Length
460 */
461 if (aim_gettlv(tlvlist, 0x00d1, 1))
462 maxmsglen = aim_gettlv16(tlvlist, 0x00d1, 1);
463
464 /*
465 * Type 0x00d2: Unknown. (2 bytes)
466 */
467 if (aim_gettlv(tlvlist, 0x00d2, 1))
468 unknown_d2 = aim_gettlv16(tlvlist, 0x00d2, 1);
469
470 /*
471 * Type 0x00d3: Room Description
472 */
473 if (aim_gettlv(tlvlist, 0x00d3, 1))
474 roomdesc = aim_gettlv_str(tlvlist, 0x00d3, 1);
475
476 /*
477 * Type 0x00d5: Unknown. (1 byte)
478 */
479 if (aim_gettlv(tlvlist, 0x00d5, 1))
480 unknown_d5 = aim_gettlv8(tlvlist, 0x00d5, 1);
481
482
483 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) {
484 ret = userfunc(sess,
485 rx,
486 &roominfo,
487 roomname,
488 usercount,
489 userinfo,
490 roomdesc,
491 unknown_c9,
492 creationtime,
493 maxmsglen,
494 unknown_d2,
495 unknown_d5);
496 }
497
498 free(roominfo.name);
499 free(userinfo);
500 free(roomname);
501 free(roomdesc);
502 aim_freetlvchain(&tlvlist);
503
504 return ret;
505}
506
507static 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)
508{
509 struct aim_userinfo_s *userinfo = NULL;
510 rxcallback_t userfunc;
511 int i = 0, curcount = 0, ret = 0;
512
513 while (i < datalen) {
514 curcount++;
515 userinfo = realloc(userinfo, curcount * sizeof(struct aim_userinfo_s));
516 i += aim_extractuserinfo(sess, data+i, &userinfo[curcount-1]);
517 }
518
519 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
520 ret = userfunc(sess, rx, curcount, userinfo);
521
522 free(userinfo);
523
524 return ret;
525}
526
527/*
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.
531 *
532 * General outline of this SNAC:
533 * snac
534 * cookie
535 * channel id
536 * tlvlist
537 * unknown
538 * source user info
539 * name
540 * evility
541 * userinfo tlvs
542 * online time
543 * etc
544 * message metatlv
545 * message tlv
546 * message string
547 * possibly others
548 *
549 */
550static 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)
551{
552 struct aim_userinfo_s userinfo;
553 rxcallback_t userfunc=NULL;
554 int ret = 0, i = 0;
555 unsigned char cookie[8];
556 int channel;
557 struct aim_tlvlist_t *outerlist;
558 char *msg = NULL;
559 struct aim_msgcookie_t *ck;
560
561 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
562
563 /*
564 * ICBM Cookie. Cache it.
565 */
566 memcpy(cookie, data, 8);
567 i += 8;
568
569 if ((ck = aim_uncachecookie(sess, cookie, AIM_COOKIETYPE_CHAT))) {
570 if (ck->data)
571 free(ck->data);
572 free(ck);
573 }
574
575 /*
576 * Channel ID
577 *
578 * Channels 1 and 2 are implemented in the normal ICBM
579 * parser.
580 *
581 * We only do channel 3 here.
582 *
583 */
584 channel = aimutil_get16(data+i);
585 i += 2;
586
587 if (channel != 0x0003) {
588 faimdprintf(sess, 0, "faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
589 return 0;
590 }
591
592 /*
593 * Start parsing TLVs right away.
594 */
595 outerlist = aim_readtlvchain(data+8+2, datalen-8-2);
596
597 /*
598 * Type 0x0003: Source User Information
599 */
600 if (aim_gettlv(outerlist, 0x0003, 1)) {
601 struct aim_tlv_t *userinfotlv;
602
603 userinfotlv = aim_gettlv(outerlist, 0x0003, 1);
604 aim_extractuserinfo(sess, userinfotlv->value, &userinfo);
605 }
606
607 /*
608 * Type 0x0001: Unknown.
609 */
610 if (aim_gettlv(outerlist, 0x0001, 1))
611 ;
612
613 /*
614 * Type 0x0005: Message Block. Conains more TLVs.
615 */
616 if (aim_gettlv(outerlist, 0x0005, 1)) {
617 struct aim_tlvlist_t *innerlist;
618 struct aim_tlv_t *msgblock;
619
620 msgblock = aim_gettlv(outerlist, 0x0005, 1);
621 innerlist = aim_readtlvchain(msgblock->value, msgblock->length);
622
623 /*
624 * Type 0x0001: Message.
625 */
626 if (aim_gettlv(innerlist, 0x0001, 1))
627 msg = aim_gettlv_str(innerlist, 0x0001, 1);
628
629 aim_freetlvchain(&innerlist);
630 }
631
632 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
633 ret = userfunc(sess, rx, &userinfo, msg);
634
635 free(msg);
636 aim_freetlvchain(&outerlist);
637
638 return ret;
639}
640
641static 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)
642{
643
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);
650
651 return 0;
652}
653
654faim_internal int chat_modfirst(struct aim_session_t *sess, aim_module_t *mod)
655{
656
657 mod->family = 0x000e;
658 mod->version = 0x0000;
659 mod->flags = 0;
660 strncpy(mod->name, "chat", sizeof(mod->name));
661 mod->snachandler = snachandler;
662
663 return 0;
664}
This page took 0.044295 seconds and 5 git commands to generate.