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

Last change on this file since 73f0a01 was 5ebff60, checked in by dequis <dx@…>, at 2015-02-20T22:50:54Z

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

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