source: libfaim/chat.c @ 9ceee9d

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 9ceee9d was 862371b, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
*** empty log message ***
  • Property mode set to 100644
File size: 15.5 KB
Line 
1/*
2 * Family 0x000e - Routines for the Chat service.
3 *
4 */
5
6#define FAIM_INTERNAL
7#include <aim.h>
8
9/* Stored in the ->priv of chat connections */
10struct chatconnpriv {
11        fu16_t exchange;
12        char *name;
13        fu16_t instance;
14};
15
16faim_internal void aim_conn_kill_chat(aim_session_t *sess, aim_conn_t *conn)
17{
18        struct chatconnpriv *ccp = (struct chatconnpriv *)conn->priv;
19
20        if (ccp)
21                free(ccp->name);
22        free(ccp);
23
24        return;
25}
26
27faim_export char *aim_chat_getname(aim_conn_t *conn)
28{
29        struct chatconnpriv *ccp;
30
31        if (!conn)
32                return NULL;
33
34        if (conn->type != AIM_CONN_TYPE_CHAT)
35                return NULL;
36
37        ccp = (struct chatconnpriv *)conn->priv;
38
39        return ccp->name;
40}
41
42/* XXX get this into conn.c -- evil!! */
43faim_export aim_conn_t *aim_chat_getconn(aim_session_t *sess, const char *name)
44{
45        aim_conn_t *cur;
46
47        for (cur = sess->connlist; cur; cur = cur->next) {
48                struct chatconnpriv *ccp = (struct chatconnpriv *)cur->priv;
49
50                if (cur->type != AIM_CONN_TYPE_CHAT)
51                        continue;
52                if (!cur->priv) {
53                        faimdprintf(sess, 0, "faim: chat: chat connection with no name! (fd = %d)\n", cur->fd);
54                        continue;
55                }
56
57                if (strcmp(ccp->name, name) == 0)
58                        break;
59        }
60
61        return cur;
62}
63
64faim_export int aim_chat_attachname(aim_conn_t *conn, fu16_t exchange, const char *roomname, fu16_t instance)
65{
66        struct chatconnpriv *ccp;
67
68        if (!conn || !roomname)
69                return -EINVAL;
70
71        if (conn->priv)
72                free(conn->priv);
73
74        if (!(ccp = malloc(sizeof(struct chatconnpriv))))
75                return -ENOMEM;
76
77        ccp->exchange = exchange;
78        ccp->name = strdup(roomname);
79        ccp->instance = instance;
80
81        conn->priv = (void *)ccp;
82
83        return 0;
84}
85
86static int aim_addtlvtochain_chatroom(aim_tlvlist_t **list, fu16_t type, fu16_t exchange, const char *roomname, fu16_t instance)
87{
88        fu8_t *buf;
89        int buflen;
90        aim_bstream_t bs;
91
92        buflen = 2 + 1 + strlen(roomname) + 2;
93       
94        if (!(buf = malloc(buflen)))
95                return 0;
96
97        aim_bstream_init(&bs, buf, buflen);
98
99        aimbs_put16(&bs, exchange);
100        aimbs_put8(&bs, strlen(roomname));
101        aimbs_putraw(&bs, roomname, strlen(roomname));
102        aimbs_put16(&bs, instance);
103
104        aim_addtlvtochain_raw(list, type, aim_bstream_curpos(&bs), buf);
105
106        free(buf);
107
108        return 0;
109}
110
111/*
112 * Join a room of name roomname.  This is the first step to joining an
113 * already created room.  It's basically a Service Request for
114 * family 0x000e, with a little added on to specify the exchange and room
115 * name.
116 */
117faim_export int aim_chat_join(aim_session_t *sess, aim_conn_t *conn, fu16_t exchange, const char *roomname, fu16_t instance)
118{
119        aim_frame_t *fr;
120        aim_snacid_t snacid;
121        aim_tlvlist_t *tl = NULL;
122        struct chatsnacinfo csi;
123       
124        if (!sess || !conn || !roomname || !strlen(roomname))
125                return -EINVAL;
126
127        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 512)))
128                return -ENOMEM;
129
130        memset(&csi, 0, sizeof(csi));
131        csi.exchange = exchange;
132        strncpy(csi.name, roomname, sizeof(csi.name));
133        csi.instance = instance;
134
135        snacid = aim_cachesnac(sess, 0x0001, 0x0004, 0x0000, &csi, sizeof(csi));
136        aim_putsnac(&fr->data, 0x0001, 0x0004, 0x0000, snacid);
137
138        /*
139         * Requesting service chat (0x000e)
140         */
141        aimbs_put16(&fr->data, 0x000e);
142
143        aim_addtlvtochain_chatroom(&tl, 0x0001, exchange, roomname, instance);
144        aim_writetlvchain(&fr->data, &tl);
145        aim_freetlvchain(&tl);
146
147        aim_tx_enqueue(sess, fr);
148
149        return 0; 
150}
151
152faim_internal int aim_chat_readroominfo(aim_bstream_t *bs, struct aim_chat_roominfo *outinfo)
153{
154        int namelen;
155
156        if (!bs || !outinfo)
157                return 0;
158
159        outinfo->exchange = aimbs_get16(bs);
160        namelen = aimbs_get8(bs);
161        outinfo->name = aimbs_getstr(bs, namelen);
162        outinfo->instance = aimbs_get16(bs);
163
164        return 0;
165}
166
167faim_export int aim_chat_leaveroom(aim_session_t *sess, const char *name)
168{
169        aim_conn_t *conn;
170
171        if (!(conn = aim_chat_getconn(sess, name)))
172                return -ENOENT;
173
174        aim_conn_close(conn);
175
176        return 0;
177}
178
179/*
180 * conn must be a BOS connection!
181 */
182faim_export int aim_chat_invite(aim_session_t *sess, aim_conn_t *conn, const char *sn, const char *msg, fu16_t exchange, const char *roomname, fu16_t instance)
183{
184        int i;
185        aim_frame_t *fr;
186        aim_msgcookie_t *cookie;
187        struct aim_invite_priv *priv;
188        fu8_t ckstr[8];
189        aim_snacid_t snacid;
190        aim_tlvlist_t *otl = NULL, *itl = NULL;
191        fu8_t *hdr;
192        int hdrlen;
193        aim_bstream_t hdrbs;
194       
195        if (!sess || !conn || !sn || !msg || !roomname)
196                return -EINVAL;
197
198        if (conn->type != AIM_CONN_TYPE_BOS)
199                return -EINVAL;
200
201        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 1152+strlen(sn)+strlen(roomname)+strlen(msg))))
202                return -ENOMEM;
203
204        snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, sn, strlen(sn)+1);
205        aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
206
207
208        /*
209         * Cookie
210         */
211        for (i = 0; i < sizeof(ckstr); i++)
212                aimutil_put8(ckstr, (fu8_t) rand());
213
214        /* XXX should be uncached by an unwritten 'invite accept' handler */
215        if ((priv = malloc(sizeof(struct aim_invite_priv)))) {
216                priv->sn = strdup(sn);
217                priv->roomname = strdup(roomname);
218                priv->exchange = exchange;
219                priv->instance = instance;
220        }
221
222        if ((cookie = aim_mkcookie(ckstr, AIM_COOKIETYPE_INVITE, priv)))
223                aim_cachecookie(sess, cookie);
224        else
225                free(priv);
226
227        for (i = 0; i < sizeof(ckstr); i++)
228                aimbs_put8(&fr->data, ckstr[i]);
229
230
231        /*
232         * Channel (2)
233         */
234        aimbs_put16(&fr->data, 0x0002);
235
236        /*
237         * Dest sn
238         */
239        aimbs_put8(&fr->data, strlen(sn));
240        aimbs_putraw(&fr->data, sn, strlen(sn));
241
242        /*
243         * TLV t(0005)
244         *
245         * Everything else is inside this TLV.
246         *
247         * Sigh.  AOL was rather inconsistent right here.  So we have
248         * to play some minor tricks.  Right inside the type 5 is some
249         * raw data, followed by a series of TLVs. 
250         *
251         */
252        hdrlen = 2+8+16+6+4+4+strlen(msg)+4+2+1+strlen(roomname)+2;
253        hdr = malloc(hdrlen);
254        aim_bstream_init(&hdrbs, hdr, hdrlen);
255       
256        aimbs_put16(&hdrbs, 0x0000); /* Unknown! */
257        aimbs_putraw(&hdrbs, ckstr, sizeof(ckstr)); /* I think... */
258        aim_putcap(&hdrbs, AIM_CAPS_CHAT);
259
260        aim_addtlvtochain16(&itl, 0x000a, 0x0001);
261        aim_addtlvtochain_noval(&itl, 0x000f);
262        aim_addtlvtochain_raw(&itl, 0x000c, strlen(msg), msg);
263        aim_addtlvtochain_chatroom(&itl, 0x2711, exchange, roomname, instance);
264        aim_writetlvchain(&hdrbs, &itl);
265       
266        aim_addtlvtochain_raw(&otl, 0x0005, aim_bstream_curpos(&hdrbs), hdr);
267
268        aim_writetlvchain(&fr->data, &otl);
269
270        free(hdr);
271        aim_freetlvchain(&itl);
272        aim_freetlvchain(&otl);
273       
274        aim_tx_enqueue(sess, fr);
275
276        return 0;
277}
278
279/*
280 * Subtype 0x0002 - General room information.  Lots of stuff.
281 *
282 * Values I know are in here but I havent attached
283 * them to any of the 'Unknown's:
284 *      - Language (English)
285 *
286 */
287static int infoupdate(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
288{
289        aim_userinfo_t *userinfo = NULL;
290        aim_rxcallback_t userfunc;
291        int ret = 0;
292        int usercount = 0;
293        fu8_t detaillevel = 0;
294        char *roomname = NULL;
295        struct aim_chat_roominfo roominfo;
296        fu16_t tlvcount = 0;
297        aim_tlvlist_t *tlvlist;
298        char *roomdesc = NULL;
299        fu16_t flags = 0;
300        fu32_t creationtime = 0;
301        fu16_t maxmsglen = 0, maxvisiblemsglen = 0;
302        fu16_t unknown_d2 = 0, unknown_d5 = 0;
303
304        aim_chat_readroominfo(bs, &roominfo);
305
306        detaillevel = aimbs_get8(bs);
307
308        if (detaillevel != 0x02) {
309                faimdprintf(sess, 0, "faim: chat_roomupdateinfo: detail level %d not supported\n", detaillevel);
310                return 1;
311        }
312
313        tlvcount = aimbs_get16(bs);
314
315        /*
316         * Everything else are TLVs.
317         */ 
318        tlvlist = aim_readtlvchain(bs);
319
320        /*
321         * TLV type 0x006a is the room name in Human Readable Form.
322         */
323        if (aim_gettlv(tlvlist, 0x006a, 1))
324                roomname = aim_gettlv_str(tlvlist, 0x006a, 1);
325
326        /*
327         * Type 0x006f: Number of occupants.
328         */
329        if (aim_gettlv(tlvlist, 0x006f, 1))
330                usercount = aim_gettlv16(tlvlist, 0x006f, 1);
331
332        /*
333         * Type 0x0073:  Occupant list.
334         */
335        if (aim_gettlv(tlvlist, 0x0073, 1)) {   
336                int curoccupant = 0;
337                aim_tlv_t *tmptlv;
338                aim_bstream_t occbs;
339
340                tmptlv = aim_gettlv(tlvlist, 0x0073, 1);
341
342                /* Allocate enough userinfo structs for all occupants */
343                userinfo = calloc(usercount, sizeof(aim_userinfo_t));
344
345                aim_bstream_init(&occbs, tmptlv->value, tmptlv->length);
346
347                while (curoccupant < usercount)
348                        aim_extractuserinfo(sess, &occbs, &userinfo[curoccupant++]);
349        }
350
351        /*
352         * Type 0x00c9: Flags. (AIM_CHATROOM_FLAG)
353         */
354        if (aim_gettlv(tlvlist, 0x00c9, 1))
355                flags = aim_gettlv16(tlvlist, 0x00c9, 1);
356
357        /*
358         * Type 0x00ca: Creation time (4 bytes)
359         */
360        if (aim_gettlv(tlvlist, 0x00ca, 1))
361                creationtime = aim_gettlv32(tlvlist, 0x00ca, 1);
362
363        /*
364         * Type 0x00d1: Maximum Message Length
365         */
366        if (aim_gettlv(tlvlist, 0x00d1, 1))
367                maxmsglen = aim_gettlv16(tlvlist, 0x00d1, 1);
368
369        /*
370         * Type 0x00d2: Unknown. (2 bytes)
371         */
372        if (aim_gettlv(tlvlist, 0x00d2, 1))
373                unknown_d2 = aim_gettlv16(tlvlist, 0x00d2, 1);
374
375        /*
376         * Type 0x00d3: Room Description
377         */
378        if (aim_gettlv(tlvlist, 0x00d3, 1))
379                roomdesc = aim_gettlv_str(tlvlist, 0x00d3, 1);
380
381        /*
382         * Type 0x000d4: Unknown (flag only)
383         */
384        if (aim_gettlv(tlvlist, 0x000d4, 1))
385                ;
386
387        /*
388         * Type 0x00d5: Unknown. (1 byte)
389         */
390        if (aim_gettlv(tlvlist, 0x00d5, 1))
391                unknown_d5 = aim_gettlv8(tlvlist, 0x00d5, 1);
392
393
394        /*
395         * Type 0x00d6: Encoding 1 ("us-ascii")
396         */
397        if (aim_gettlv(tlvlist, 0x000d6, 1))
398                ;
399       
400        /*
401         * Type 0x00d7: Language 1 ("en")
402         */
403        if (aim_gettlv(tlvlist, 0x000d7, 1))
404                ;
405
406        /*
407         * Type 0x00d8: Encoding 2 ("us-ascii")
408         */
409        if (aim_gettlv(tlvlist, 0x000d8, 1))
410                ;
411       
412        /*
413         * Type 0x00d9: Language 2 ("en")
414         */
415        if (aim_gettlv(tlvlist, 0x000d9, 1))
416                ;
417
418        /*
419         * Type 0x00da: Maximum visible message length
420         */
421        if (aim_gettlv(tlvlist, 0x000da, 1))
422                maxvisiblemsglen = aim_gettlv16(tlvlist, 0x00da, 1);
423
424        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) {
425                ret = userfunc(sess,
426                                rx, 
427                                &roominfo,
428                                roomname,
429                                usercount,
430                                userinfo,       
431                                roomdesc,
432                                flags,
433                                creationtime,
434                                maxmsglen,
435                                unknown_d2,
436                                unknown_d5,
437                                maxvisiblemsglen);
438        }
439
440        free(roominfo.name);
441        free(userinfo);
442        free(roomname);
443        free(roomdesc);
444        aim_freetlvchain(&tlvlist);
445
446        return ret;
447}
448
449/* Subtypes 0x0003 and 0x0004 */
450static int userlistchange(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
451{
452        aim_userinfo_t *userinfo = NULL;
453        aim_rxcallback_t userfunc;
454        int curcount = 0, ret = 0;
455
456        while (aim_bstream_empty(bs)) {
457                curcount++;
458                userinfo = realloc(userinfo, curcount * sizeof(aim_userinfo_t));
459                aim_extractuserinfo(sess, bs, &userinfo[curcount-1]);
460        }
461
462        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
463                ret = userfunc(sess, rx, curcount, userinfo);
464
465        free(userinfo);
466
467        return ret;
468}
469
470/*
471 * Subtype 0x0005 - Send a Chat Message.
472 *
473 * Possible flags:
474 *   AIM_CHATFLAGS_NOREFLECT   --  Unset the flag that requests messages
475 *                                 should be sent to their sender.
476 *   AIM_CHATFLAGS_AWAY        --  Mark the message as an autoresponse
477 *                                 (Note that WinAIM does not honor this,
478 *                                 and displays the message as normal.)
479 *
480 * XXX convert this to use tlvchains
481 */
482faim_export int aim_chat_send_im(aim_session_t *sess, aim_conn_t *conn, fu16_t flags, const char *msg, int msglen)
483{   
484        int i;
485        aim_frame_t *fr;
486        aim_msgcookie_t *cookie;
487        aim_snacid_t snacid;
488        fu8_t ckstr[8];
489        aim_tlvlist_t *otl = NULL, *itl = NULL;
490
491        if (!sess || !conn || !msg || (msglen <= 0))
492                return 0;
493
494        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 1152)))
495                return -ENOMEM;
496
497        snacid = aim_cachesnac(sess, 0x000e, 0x0005, 0x0000, NULL, 0);
498        aim_putsnac(&fr->data, 0x000e, 0x0005, 0x0000, snacid);
499
500
501        /*
502         * Generate a random message cookie.
503         *
504         * XXX mkcookie should generate the cookie and cache it in one
505         * operation to preserve uniqueness.
506         *
507         */
508        for (i = 0; i < sizeof(ckstr); i++)
509                aimutil_put8(ckstr+i, (fu8_t) rand());
510
511        cookie = aim_mkcookie(ckstr, AIM_COOKIETYPE_CHAT, NULL);
512        cookie->data = NULL; /* XXX store something useful here */
513
514        aim_cachecookie(sess, cookie);
515
516        for (i = 0; i < sizeof(ckstr); i++)
517                aimbs_put8(&fr->data, ckstr[i]);
518
519
520        /*
521         * Channel ID.
522         */
523        aimbs_put16(&fr->data, 0x0003);
524
525
526        /*
527         * Type 1: Flag meaning this message is destined to the room.
528         */
529        aim_addtlvtochain_noval(&otl, 0x0001);
530
531        /*
532         * Type 6: Reflect
533         */
534        if (!(flags & AIM_CHATFLAGS_NOREFLECT))
535                aim_addtlvtochain_noval(&otl, 0x0006);
536
537        /*
538         * Type 7: Autoresponse
539         */
540        if (flags & AIM_CHATFLAGS_AWAY)
541                aim_addtlvtochain_noval(&otl, 0x0007);
542
543        /*
544         * SubTLV: Type 1: Message
545         */
546        aim_addtlvtochain_raw(&itl, 0x0001, strlen(msg), msg);
547
548        /*
549         * Type 5: Message block.  Contains more TLVs.
550         *
551         * This could include other information... We just
552         * put in a message TLV however. 
553         *
554         */
555        aim_addtlvtochain_frozentlvlist(&otl, 0x0005, &itl);
556
557        aim_writetlvchain(&fr->data, &otl);
558       
559        aim_freetlvchain(&itl);
560        aim_freetlvchain(&otl);
561       
562        aim_tx_enqueue(sess, fr);
563
564        return 0;
565}
566
567/*
568 * Subtype 0x0006
569 *
570 * We could probably include this in the normal ICBM parsing
571 * code as channel 0x0003, however, since only the start
572 * would be the same, we might as well do it here.
573 *
574 * General outline of this SNAC:
575 *   snac
576 *   cookie
577 *   channel id
578 *   tlvlist
579 *     unknown
580 *     source user info
581 *       name
582 *       evility
583 *       userinfo tlvs
584 *         online time
585 *         etc
586 *     message metatlv
587 *       message tlv
588 *         message string
589 *       possibly others
590 * 
591 */
592static int incomingmsg(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
593{
594        aim_userinfo_t userinfo;
595        aim_rxcallback_t userfunc;     
596        int ret = 0;
597        fu8_t *cookie;
598        fu16_t channel;
599        aim_tlvlist_t *otl;
600        char *msg = NULL;
601        aim_msgcookie_t *ck;
602
603        memset(&userinfo, 0, sizeof(aim_userinfo_t));
604
605        /*
606         * ICBM Cookie.  Uncache it.
607         */
608        cookie = aimbs_getraw(bs, 8);
609
610        if ((ck = aim_uncachecookie(sess, cookie, AIM_COOKIETYPE_CHAT))) {
611                free(ck->data);
612                free(ck);
613        }
614
615        /*
616         * Channel ID
617         *
618         * Channels 1 and 2 are implemented in the normal ICBM
619         * parser.
620         *
621         * We only do channel 3 here.
622         *
623         */
624        channel = aimbs_get16(bs);
625
626        if (channel != 0x0003) {
627                faimdprintf(sess, 0, "faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
628                return 0;
629        }
630
631        /*
632         * Start parsing TLVs right away.
633         */
634        otl = aim_readtlvchain(bs);
635
636        /*
637         * Type 0x0003: Source User Information
638         */
639        if (aim_gettlv(otl, 0x0003, 1)) {
640                aim_tlv_t *userinfotlv;
641                aim_bstream_t tbs;
642
643                userinfotlv = aim_gettlv(otl, 0x0003, 1);
644
645                aim_bstream_init(&tbs, userinfotlv->value, userinfotlv->length);
646                aim_extractuserinfo(sess, &tbs, &userinfo);
647        }
648
649        /*
650         * Type 0x0001: If present, it means it was a message to the
651         * room (as opposed to a whisper).
652         */
653        if (aim_gettlv(otl, 0x0001, 1))
654                ;
655
656        /*
657         * Type 0x0005: Message Block.  Conains more TLVs.
658         */
659        if (aim_gettlv(otl, 0x0005, 1)) {
660                aim_tlvlist_t *itl;
661                aim_tlv_t *msgblock;
662                aim_bstream_t tbs;
663
664                msgblock = aim_gettlv(otl, 0x0005, 1);
665                aim_bstream_init(&tbs, msgblock->value, msgblock->length);
666                itl = aim_readtlvchain(&tbs);
667
668                /*
669                 * Type 0x0001: Message.
670                 */     
671                if (aim_gettlv(itl, 0x0001, 1))
672                        msg = aim_gettlv_str(itl, 0x0001, 1);
673
674                aim_freetlvchain(&itl); 
675        }
676
677        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
678                ret = userfunc(sess, rx, &userinfo, msg);
679
680        free(cookie);
681        free(msg);
682        aim_freetlvchain(&otl);
683
684        return ret;
685}
686
687static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
688{
689
690        if (snac->subtype == 0x0002)
691                return infoupdate(sess, mod, rx, snac, bs);
692        else if ((snac->subtype == 0x0003) || (snac->subtype == 0x0004))
693                return userlistchange(sess, mod, rx, snac, bs);
694        else if (snac->subtype == 0x0006)
695                return incomingmsg(sess, mod, rx, snac, bs);
696
697        return 0;
698}
699
700faim_internal int chat_modfirst(aim_session_t *sess, aim_module_t *mod)
701{
702
703        mod->family = 0x000e;
704        mod->version = 0x0001;
705        mod->toolid = 0x0004; /* XXX this doesn't look right */
706        mod->toolversion = 0x0001; /* nor does this */
707        mod->flags = 0;
708        strncpy(mod->name, "chat", sizeof(mod->name));
709        mod->snachandler = snachandler;
710
711        return 0;
712}
Note: See TracBrowser for help on using the repository browser.