source: protocols/oscar/chat.c @ 2f9027c

Last change on this file since 2f9027c was 6042a54, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-10-19T23:38:33Z

Massive cleanup in OSCAR.

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