source: protocols/oscar/chatnav.c @ be1efa3

Last change on this file since be1efa3 was b041b52, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-12-06T21:50:43Z

Merging compiler warning fixes from vmiklos.

This change also uncovered one bug in groupchat handling in OSCAR, which
I fixed during the merge.

  • Property mode set to 100644
File size: 10.5 KB
Line 
1/*
2 * Handle ChatNav.
3 *
4 * [The ChatNav(igation) service does various things to keep chat
5 *  alive.  It provides room information, room searching and creating,
6 *  as well as giving users the right ("permission") to use chat.]
7 *
8 */
9
10#include <aim.h>
11#include "chatnav.h"
12
13/*
14 * conn must be a chatnav connection!
15 */
16int aim_chatnav_reqrights(aim_session_t *sess, aim_conn_t *conn)
17{
18        return aim_genericreq_n_snacid(sess, conn, 0x000d, 0x0002);
19}
20
21int aim_chatnav_createroom(aim_session_t *sess, aim_conn_t *conn, const char *name, guint16 exchange)
22{
23        static const char ck[] = {"create"};
24        static const char lang[] = {"en"};
25        static const char charset[] = {"us-ascii"};
26        aim_frame_t *fr;
27        aim_snacid_t snacid;
28        aim_tlvlist_t *tl = NULL;
29
30        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 1152)))
31                return -ENOMEM;
32
33        snacid = aim_cachesnac(sess, 0x000d, 0x0008, 0x0000, NULL, 0);
34        aim_putsnac(&fr->data, 0x000d, 0x0008, 0x0000, snacid);
35
36        /* exchange */
37        aimbs_put16(&fr->data, exchange);
38
39        /*
40         * This looks to be a big hack.  You'll note that this entire
41         * SNAC is just a room info structure, but the hard room name,
42         * here, is set to "create". 
43         *
44         * Either this goes on the "list of questions concerning
45         * why-the-hell-did-you-do-that", or this value is completly
46         * ignored.  Without experimental evidence, but a good knowledge of
47         * AOL style, I'm going to guess that it is the latter, and that
48         * the value of the room name in create requests is ignored.
49         */
50        aimbs_put8(&fr->data, strlen(ck));
51        aimbs_putraw(&fr->data, (guint8 *)ck, strlen(ck));
52
53        /*
54         * instance
55         *
56         * Setting this to 0xffff apparently assigns the last instance.
57         *
58         */
59        aimbs_put16(&fr->data, 0xffff);
60
61        /* detail level */
62        aimbs_put8(&fr->data, 0x01);
63
64        aim_addtlvtochain_raw(&tl, 0x00d3, strlen(name), (guint8 *)name);
65        aim_addtlvtochain_raw(&tl, 0x00d6, strlen(charset), (guint8 *)charset);
66        aim_addtlvtochain_raw(&tl, 0x00d7, strlen(lang), (guint8 *)lang);
67
68        /* tlvcount */
69        aimbs_put16(&fr->data, aim_counttlvchain(&tl));
70        aim_writetlvchain(&fr->data, &tl);
71
72        aim_freetlvchain(&tl);
73
74        aim_tx_enqueue(sess, fr);
75
76        return 0;
77}
78
79static int parseinfo_perms(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs, aim_snac_t *snac2)
80{
81        aim_rxcallback_t userfunc;
82        int ret = 0;
83        struct aim_chat_exchangeinfo *exchanges = NULL;
84        int curexchange;
85        aim_tlv_t *exchangetlv;
86        guint8 maxrooms = 0;
87        aim_tlvlist_t *tlvlist, *innerlist;
88
89        tlvlist = aim_readtlvchain(bs);
90
91        /*
92         * Type 0x0002: Maximum concurrent rooms.
93         */ 
94        if (aim_gettlv(tlvlist, 0x0002, 1))
95                maxrooms = aim_gettlv8(tlvlist, 0x0002, 1);
96
97        /*
98         * Type 0x0003: Exchange information
99         *
100         * There can be any number of these, each one
101         * representing another exchange. 
102         *
103         */
104        for (curexchange = 0; ((exchangetlv = aim_gettlv(tlvlist, 0x0003, curexchange+1))); ) {
105                aim_bstream_t tbs;
106
107                aim_bstream_init(&tbs, exchangetlv->value, exchangetlv->length);
108
109                curexchange++;
110
111                exchanges = g_realloc(exchanges, curexchange * sizeof(struct aim_chat_exchangeinfo));
112
113                /* exchange number */
114                exchanges[curexchange-1].number = aimbs_get16(&tbs);
115                innerlist = aim_readtlvchain(&tbs);
116
117                /*
118                 * Type 0x000a: Unknown.
119                 *
120                 * Usually three bytes: 0x0114 (exchange 1) or 0x010f (others).
121                 *
122                 */
123                if (aim_gettlv(innerlist, 0x000a, 1))
124                        ;
125
126                /*
127                 * Type 0x000d: Unknown.
128                 */
129                if (aim_gettlv(innerlist, 0x000d, 1))
130                        ;
131
132                /*
133                 * Type 0x0004: Unknown
134                 */
135                if (aim_gettlv(innerlist, 0x0004, 1))
136                        ;
137
138                /*
139                 * Type 0x0002: Unknown
140                 */
141                if (aim_gettlv(innerlist, 0x0002, 1))
142                        ;
143
144                /*
145                 * Type 0x00c9: Flags
146                 *
147                 * 1 Evilable
148                 * 2 Nav Only
149                 * 4 Instancing Allowed
150                 * 8 Occupant Peek Allowed
151                 *
152                 */ 
153                if (aim_gettlv(innerlist, 0x00c9, 1))
154                        exchanges[curexchange-1].flags = aim_gettlv16(innerlist, 0x00c9, 1);
155                     
156                /*
157                 * Type 0x00ca: Creation Date
158                 */
159                if (aim_gettlv(innerlist, 0x00ca, 1))
160                        ;
161                     
162                /*
163                 * Type 0x00d0: Mandatory Channels?
164                 */
165                if (aim_gettlv(innerlist, 0x00d0, 1))
166                        ;
167
168                /*
169                 * Type 0x00d1: Maximum Message length
170                 */
171                if (aim_gettlv(innerlist, 0x00d1, 1))
172                        ;
173
174                /*
175                 * Type 0x00d2: Maximum Occupancy?
176                 */
177                if (aim_gettlv(innerlist, 0x00d2, 1))   
178                        ;
179
180                /*
181                 * Type 0x00d3: Exchange Description
182                 */
183                if (aim_gettlv(innerlist, 0x00d3, 1))   
184                        exchanges[curexchange-1].name = aim_gettlv_str(innerlist, 0x00d3, 1);
185                else
186                        exchanges[curexchange-1].name = NULL;
187
188                /*
189                 * Type 0x00d4: Exchange Description URL
190                 */
191                if (aim_gettlv(innerlist, 0x00d4, 1))   
192                        ;
193
194                /*
195                 * Type 0x00d5: Creation Permissions
196                 *
197                 * 0  Creation not allowed
198                 * 1  Room creation allowed
199                 * 2  Exchange creation allowed
200                 *
201                 */
202                if (aim_gettlv(innerlist, 0x00d5, 1)) {
203                        aim_gettlv8(innerlist, 0x00d5, 1); /* createperms */
204                }
205
206                /*
207                 * Type 0x00d6: Character Set (First Time)
208                 */           
209                if (aim_gettlv(innerlist, 0x00d6, 1))   
210                        exchanges[curexchange-1].charset1 = aim_gettlv_str(innerlist, 0x00d6, 1);
211                else
212                        exchanges[curexchange-1].charset1 = NULL;
213                     
214                /*
215                 * Type 0x00d7: Language (First Time)
216                 */           
217                if (aim_gettlv(innerlist, 0x00d7, 1))   
218                        exchanges[curexchange-1].lang1 = aim_gettlv_str(innerlist, 0x00d7, 1);
219                else
220                        exchanges[curexchange-1].lang1 = NULL;
221
222                /*
223                 * Type 0x00d8: Character Set (Second Time)
224                 */           
225                if (aim_gettlv(innerlist, 0x00d8, 1))   
226                        exchanges[curexchange-1].charset2 = aim_gettlv_str(innerlist, 0x00d8, 1);
227                else
228                        exchanges[curexchange-1].charset2 = NULL;
229
230                /*
231                 * Type 0x00d9: Language (Second Time)
232                 */           
233                if (aim_gettlv(innerlist, 0x00d9, 1))   
234                        exchanges[curexchange-1].lang2 = aim_gettlv_str(innerlist, 0x00d9, 1);
235                else
236                        exchanges[curexchange-1].lang2 = NULL;
237                     
238                /*
239                 * Type 0x00da: Unknown
240                 */
241                if (aim_gettlv(innerlist, 0x00da, 1))   
242                        ;
243
244                aim_freetlvchain(&innerlist);
245        }
246
247        /*
248         * Call client.
249         */
250        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
251                ret = userfunc(sess, rx, snac2->type, maxrooms, curexchange, exchanges);
252
253        for (curexchange--; curexchange >= 0; curexchange--) {
254                g_free(exchanges[curexchange].name);
255                g_free(exchanges[curexchange].charset1);
256                g_free(exchanges[curexchange].lang1);
257                g_free(exchanges[curexchange].charset2);
258                g_free(exchanges[curexchange].lang2);
259        }
260        g_free(exchanges);
261        aim_freetlvchain(&tlvlist);
262
263        return ret;
264}
265
266static int parseinfo_create(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs, aim_snac_t *snac2)
267{
268        aim_rxcallback_t userfunc;
269        aim_tlvlist_t *tlvlist, *innerlist;
270        char *ck = NULL, *fqcn = NULL, *name = NULL;
271        guint16 exchange = 0, instance = 0, unknown = 0, flags = 0, maxmsglen = 0, maxoccupancy = 0;
272        guint32 createtime = 0;
273        guint8 createperms = 0, detaillevel;
274        int cklen;
275        aim_tlv_t *bigblock;
276        int ret = 0;
277        aim_bstream_t bbbs;
278
279        tlvlist = aim_readtlvchain(bs);
280
281        if (!(bigblock = aim_gettlv(tlvlist, 0x0004, 1))) {
282                imcb_error(sess->aux_data, "no bigblock in top tlv in create room response");
283       
284                aim_freetlvchain(&tlvlist);
285                return 0;
286        }
287
288        aim_bstream_init(&bbbs, bigblock->value, bigblock->length);
289
290        exchange = aimbs_get16(&bbbs);
291        cklen = aimbs_get8(&bbbs);
292        ck = aimbs_getstr(&bbbs, cklen);
293        instance = aimbs_get16(&bbbs);
294        detaillevel = aimbs_get8(&bbbs);
295
296        if (detaillevel != 0x02) {
297                imcb_error(sess->aux_data, "unknown detaillevel in create room response");
298                aim_freetlvchain(&tlvlist);
299                g_free(ck);
300                return 0;
301        }
302
303        unknown = aimbs_get16(&bbbs);
304
305        innerlist = aim_readtlvchain(&bbbs);
306
307        if (aim_gettlv(innerlist, 0x006a, 1))
308                fqcn = aim_gettlv_str(innerlist, 0x006a, 1);
309
310        if (aim_gettlv(innerlist, 0x00c9, 1))
311                flags = aim_gettlv16(innerlist, 0x00c9, 1);
312
313        if (aim_gettlv(innerlist, 0x00ca, 1))
314                createtime = aim_gettlv32(innerlist, 0x00ca, 1);
315
316        if (aim_gettlv(innerlist, 0x00d1, 1))
317                maxmsglen = aim_gettlv16(innerlist, 0x00d1, 1);
318
319        if (aim_gettlv(innerlist, 0x00d2, 1))
320                maxoccupancy = aim_gettlv16(innerlist, 0x00d2, 1);
321
322        if (aim_gettlv(innerlist, 0x00d3, 1))
323                name = aim_gettlv_str(innerlist, 0x00d3, 1);
324
325        if (aim_gettlv(innerlist, 0x00d5, 1))
326                createperms = aim_gettlv8(innerlist, 0x00d5, 1);
327
328        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) {
329                ret = userfunc(sess, rx, snac2->type, fqcn, instance, exchange, flags, createtime, maxmsglen, maxoccupancy, createperms, unknown, name, ck);
330        }
331
332        g_free(ck);
333        g_free(name);
334        g_free(fqcn);
335        aim_freetlvchain(&innerlist);
336        aim_freetlvchain(&tlvlist);
337
338        return ret;
339}
340
341/*
342 * Since multiple things can trigger this callback, we must lookup the
343 * snacid to determine the original snac subtype that was called.
344 *
345 * XXX This isn't really how this works.  But this is:  Every d/9 response
346 * has a 16bit value at the beginning. That matches to:
347 *    Short Desc = 1
348 *    Full Desc = 2
349 *    Instance Info = 4
350 *    Nav Short Desc = 8
351 *    Nav Instance Info = 16
352 * And then everything is really asynchronous.  There is no specific
353 * attachment of a response to a create room request, for example.  Creating
354 * the room yields no different a response than requesting the room's info.
355 *
356 */
357static int parseinfo(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
358{
359        aim_snac_t *snac2;
360        int ret = 0;
361
362        if (!(snac2 = aim_remsnac(sess, snac->id))) {
363                imcb_error(sess->aux_data, "received response to unknown request!");
364                return 0;
365        }
366
367        if (snac2->family != 0x000d) {
368                imcb_error(sess->aux_data, "received response that maps to corrupt request!");
369                return 0;
370        }
371
372        /*
373         * We now know what the original SNAC subtype was.
374         */
375        if (snac2->type == 0x0002) /* request chat rights */
376                ret = parseinfo_perms(sess, mod, rx, snac, bs, snac2);
377        else if (snac2->type == 0x0003) {} /* request exchange info */
378        else if (snac2->type == 0x0004) {} /* request room info */
379        else if (snac2->type == 0x0005) {} /* request more room info */
380        else if (snac2->type == 0x0006) {} /* request occupant list */
381        else if (snac2->type == 0x0007) {} /* search for a room */
382        else if (snac2->type == 0x0008) /* create room */
383                ret = parseinfo_create(sess, mod, rx, snac, bs, snac2);
384        else
385                imcb_error(sess->aux_data, "unknown request subtype");
386
387        if (snac2)
388                g_free(snac2->data);
389        g_free(snac2);
390
391        return ret;
392}
393
394static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
395{
396
397        if (snac->subtype == 0x0009)
398                return parseinfo(sess, mod, rx, snac, bs);
399
400        return 0;
401}
402
403int chatnav_modfirst(aim_session_t *sess, aim_module_t *mod)
404{
405
406        mod->family = 0x000d;
407        mod->version = 0x0003;
408        mod->toolid = 0x0010;
409        mod->toolversion = 0x0629;
410        mod->flags = 0;
411        strncpy(mod->name, "chatnav", sizeof(mod->name));
412        mod->snachandler = snachandler;
413
414        return 0;
415}
Note: See TracBrowser for help on using the repository browser.