source: protocols/oscar/chat.c @ 73ee390

Last change on this file since 73ee390 was 8519f45, checked in by dequis <dx@…>, at 2015-01-26T02:43:34Z

Silence some oscar compilation warnings

And that's enough editing of the oscar protocol code for 2015

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