summaryrefslogtreecommitdiff
path: root/libpurple/protocols/oscar/family_chatnav.c
blob: cffb398fe81f2e7b6ae86a2f9ef72fe1d1d5d193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Gaim's oscar protocol plugin
 * This file is the legal property of its developers.
 * Please see the AUTHORS file distributed alongside this file.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
 * Family 0x000d - Handle ChatNav.
 *
 * The ChatNav(igation) service does various things to keep chat
 * alive.  It provides room information, room searching and creating,
 * as well as giving users the right ("permission") to use chat.
 *
 */

#include "oscar.h"

/*
 * Subtype 0x0002
 *
 * conn must be a chatnav connection!
 *
 */
void aim_chatnav_reqrights(OscarData *od, FlapConnection *conn)
{
	aim_genericreq_n_snacid(od, conn, 0x000d, 0x0002);
}

/*
 * Subtype 0x0008
 */
int aim_chatnav_createroom(OscarData *od, FlapConnection *conn, const char *name, guint16 exchange)
{
	static const char ck[] = {"create"};
	static const char lang[] = {"en"};
	static const char charset[] = {"us-ascii"};
	FlapFrame *frame;
	aim_snacid_t snacid;
	aim_tlvlist_t *tl = NULL;

	frame = flap_frame_new(od, 0x02, 1152);

	snacid = aim_cachesnac(od, 0x000d, 0x0008, 0x0000, NULL, 0);
	aim_putsnac(&frame->data, 0x000d, 0x0008, 0x0000, snacid);

	/* exchange */
	byte_stream_put16(&frame->data, exchange);

	/*
	 * This looks to be a big hack.  You'll note that this entire
	 * SNAC is just a room info structure, but the hard room name,
	 * here, is set to "create".
	 *
	 * Either this goes on the "list of questions concerning
	 * why-the-hell-did-you-do-that", or this value is completely
	 * ignored.  Without experimental evidence, but a good knowledge of
	 * AOL style, I'm going to guess that it is the latter, and that
	 * the value of the room name in create requests is ignored.
	 */
	byte_stream_put8(&frame->data, strlen(ck));
	byte_stream_putstr(&frame->data, ck);

	/*
	 * instance
	 *
	 * Setting this to 0xffff apparently assigns the last instance.
	 *
	 */
	byte_stream_put16(&frame->data, 0xffff);

	/* detail level */
	byte_stream_put8(&frame->data, 0x01);

	aim_tlvlist_add_str(&tl, 0x00d3, name);
	aim_tlvlist_add_str(&tl, 0x00d6, charset);
	aim_tlvlist_add_str(&tl, 0x00d7, lang);

	/* tlvcount */
	byte_stream_put16(&frame->data, aim_tlvlist_count(&tl));
	aim_tlvlist_write(&frame->data, &tl);

	aim_tlvlist_free(&tl);

	flap_connection_send(conn, frame);

	return 0;
}

static int
parseinfo_perms(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs, aim_snac_t *snac2)
{
	aim_rxcallback_t userfunc;
	int ret = 0;
	struct aim_chat_exchangeinfo *exchanges = NULL;
	int curexchange;
	aim_tlv_t *exchangetlv;
	guint8 maxrooms = 0;
	aim_tlvlist_t *tlvlist, *innerlist;

	tlvlist = aim_tlvlist_read(bs);

	/*
	 * Type 0x0002: Maximum concurrent rooms.
	 */
	if (aim_tlv_gettlv(tlvlist, 0x0002, 1))
		maxrooms = aim_tlv_get8(tlvlist, 0x0002, 1);

	/*
	 * Type 0x0003: Exchange information
	 *
	 * There can be any number of these, each one
	 * representing another exchange.
	 *
	 */
	for (curexchange = 0; ((exchangetlv = aim_tlv_gettlv(tlvlist, 0x0003, curexchange+1))); ) {
		ByteStream tbs;

		byte_stream_init(&tbs, exchangetlv->value, exchangetlv->length);

		curexchange++;

		exchanges = realloc(exchanges, curexchange * sizeof(struct aim_chat_exchangeinfo));

		/* exchange number */
		exchanges[curexchange-1].number = byte_stream_get16(&tbs);
		innerlist = aim_tlvlist_read(&tbs);

#if 0
		/*
		 * Type 0x000a: Unknown.
		 *
		 * Usually three bytes: 0x0114 (exchange 1) or 0x010f (others).
		 *
		 */
		if (aim_tlv_gettlv(innerlist, 0x000a, 1)) {
			/* Unhandled */
		}

		/*
		 * Type 0x000d: Unknown.
		 */
		if (aim_tlv_gettlv(innerlist, 0x000d, 1)) {
			/* Unhandled */
		}

		/*
		 * Type 0x0004: Unknown
		 */
		if (aim_tlv_gettlv(innerlist, 0x0004, 1)) {
			/* Unhandled */
		}
#endif

		/*
		 * Type 0x0002: Unknown
		 */
		if (aim_tlv_gettlv(innerlist, 0x0002, 1)) {
			guint16 classperms;

			classperms = aim_tlv_get16(innerlist, 0x0002, 1);

			gaim_debug_misc("oscar", "faim: class permissions %x\n", classperms);
		}

		/*
		 * Type 0x00c9: Flags
		 *
		 * 1 Evilable
		 * 2 Nav Only
		 * 4 Instancing Allowed
		 * 8 Occupant Peek Allowed
		 *
		 */
		if (aim_tlv_gettlv(innerlist, 0x00c9, 1))
			exchanges[curexchange-1].flags = aim_tlv_get16(innerlist, 0x00c9, 1);

#if 0
		/*
		 * Type 0x00ca: Creation Date
		 */
		if (aim_tlv_gettlv(innerlist, 0x00ca, 1)) {
			/* Unhandled */
		}

		/*
		 * Type 0x00d0: Mandatory Channels?
		 */
		if (aim_tlv_gettlv(innerlist, 0x00d0, 1)) {
			/* Unhandled */
		}

		/*
		 * Type 0x00d1: Maximum Message length
		 */
		if (aim_tlv_gettlv(innerlist, 0x00d1, 1)) {
			/* Unhandled */
		}

		/*
		 * Type 0x00d2: Maximum Occupancy?
		 */
		if (aim_tlv_gettlv(innerlist, 0x00d2, 1)) {
			/* Unhandled */
		}
#endif

		/*
		 * Type 0x00d3: Exchange Description
		 */
		if (aim_tlv_gettlv(innerlist, 0x00d3, 1))
			exchanges[curexchange-1].name = aim_tlv_getstr(innerlist, 0x00d3, 1);
		else
			exchanges[curexchange-1].name = NULL;

#if 0
		/*
		 * Type 0x00d4: Exchange Description URL
		 */
		if (aim_tlv_gettlv(innerlist, 0x00d4, 1)) {
			/* Unhandled */
		}
#endif

		/*
		 * Type 0x00d5: Creation Permissions
		 *
		 * 0  Creation not allowed
		 * 1  Room creation allowed
		 * 2  Exchange creation allowed
		 *
		 */
		if (aim_tlv_gettlv(innerlist, 0x00d5, 1)) {
			guint8 createperms;

			createperms = aim_tlv_get8(innerlist, 0x00d5, 1);
		}

		/*
		 * Type 0x00d6: Character Set (First Time)
		 */
		if (aim_tlv_gettlv(innerlist, 0x00d6, 1))
			exchanges[curexchange-1].charset1 = aim_tlv_getstr(innerlist, 0x00d6, 1);
		else
			exchanges[curexchange-1].charset1 = NULL;

		/*
		 * Type 0x00d7: Language (First Time)
		 */
		if (aim_tlv_gettlv(innerlist, 0x00d7, 1))
			exchanges[curexchange-1].lang1 = aim_tlv_getstr(innerlist, 0x00d7, 1);
		else
			exchanges[curexchange-1].lang1 = NULL;

		/*
		 * Type 0x00d8: Character Set (Second Time)
		 */
		if (aim_tlv_gettlv(innerlist, 0x00d8, 1))
			exchanges[curexchange-1].charset2 = aim_tlv_getstr(innerlist, 0x00d8, 1);
		else
			exchanges[curexchange-1].charset2 = NULL;

		/*
		 * Type 0x00d9: Language (Second Time)
		 */
		if (aim_tlv_gettlv(innerlist, 0x00d9, 1))
			exchanges[curexchange-1].lang2 = aim_tlv_getstr(innerlist, 0x00d9, 1);
		else
			exchanges[curexchange-1].lang2 = NULL;

#if 0
		/*
		 * Type 0x00da: Unknown
		 */
		if (aim_tlv_gettlv(innerlist, 0x00da, 1)) {
			/* Unhandled */
		}
#endif

		aim_tlvlist_free(&innerlist);
	}

	/*
	 * Call client.
	 */
	if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
		ret = userfunc(od, conn, frame, snac2->type, maxrooms, curexchange, exchanges);

	for (curexchange--; curexchange >= 0; curexchange--) {
		free(exchanges[curexchange].name);
		free(exchanges[curexchange].charset1);
		free(exchanges[curexchange].lang1);
		free(exchanges[curexchange].charset2);
		free(exchanges[curexchange].lang2);
	}
	free(exchanges);
	aim_tlvlist_free(&tlvlist);

	return ret;
}

static int
parseinfo_create(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs, aim_snac_t *snac2)
{
	aim_rxcallback_t userfunc;
	aim_tlvlist_t *tlvlist, *innerlist;
	char *ck = NULL, *fqcn = NULL, *name = NULL;
	guint16 exchange = 0, instance = 0, unknown = 0, flags = 0, maxmsglen = 0, maxoccupancy = 0;
	guint32 createtime = 0;
	guint8 createperms = 0, detaillevel;
	int cklen;
	aim_tlv_t *bigblock;
	int ret = 0;
	ByteStream bbbs;

	tlvlist = aim_tlvlist_read(bs);

	if (!(bigblock = aim_tlv_gettlv(tlvlist, 0x0004, 1))) {
		gaim_debug_misc("oscar", "no bigblock in top tlv in create room response\n");
		aim_tlvlist_free(&tlvlist);
		return 0;
	}

	byte_stream_init(&bbbs, bigblock->value, bigblock->length);

	exchange = byte_stream_get16(&bbbs);
	cklen = byte_stream_get8(&bbbs);
	ck = byte_stream_getstr(&bbbs, cklen);
	instance = byte_stream_get16(&bbbs);
	detaillevel = byte_stream_get8(&bbbs);

	if (detaillevel != 0x02) {
		gaim_debug_misc("oscar", "unknown detaillevel in create room response (0x%02x)\n", detaillevel);
		aim_tlvlist_free(&tlvlist);
		free(ck);
		return 0;
	}

	unknown = byte_stream_get16(&bbbs);

	innerlist = aim_tlvlist_read(&bbbs);

	if (aim_tlv_gettlv(innerlist, 0x006a, 1))
		fqcn = aim_tlv_getstr(innerlist, 0x006a, 1);

	if (aim_tlv_gettlv(innerlist, 0x00c9, 1))
		flags = aim_tlv_get16(innerlist, 0x00c9, 1);

	if (aim_tlv_gettlv(innerlist, 0x00ca, 1))
		createtime = aim_tlv_get32(innerlist, 0x00ca, 1);

	if (aim_tlv_gettlv(innerlist, 0x00d1, 1))
		maxmsglen = aim_tlv_get16(innerlist, 0x00d1, 1);

	if (aim_tlv_gettlv(innerlist, 0x00d2, 1))
		maxoccupancy = aim_tlv_get16(innerlist, 0x00d2, 1);

	if (aim_tlv_gettlv(innerlist, 0x00d3, 1))
		name = aim_tlv_getstr(innerlist, 0x00d3, 1);

	if (aim_tlv_gettlv(innerlist, 0x00d5, 1))
		createperms = aim_tlv_get8(innerlist, 0x00d5, 1);

	if ((userfunc = aim_callhandler(od, snac->family, snac->subtype))) {
		ret = userfunc(od, conn, frame, snac2->type, fqcn, instance, exchange, flags, createtime, maxmsglen, maxoccupancy, createperms, unknown, name, ck);
	}

	free(ck);
	free(name);
	free(fqcn);
	aim_tlvlist_free(&innerlist);
	aim_tlvlist_free(&tlvlist);

	return ret;
}

/*
 * Subtype 0x0009
 *
 * Since multiple things can trigger this callback, we must lookup the
 * snacid to determine the original snac subtype that was called.
 *
 * XXX This isn't really how this works.  But this is:  Every d/9 response
 * has a 16bit value at the beginning. That matches to:
 *    Short Desc = 1
 *    Full Desc = 2
 *    Instance Info = 4
 *    Nav Short Desc = 8
 *    Nav Instance Info = 16
 * And then everything is really asynchronous.  There is no specific
 * attachment of a response to a create room request, for example.  Creating
 * the room yields no different a response than requesting the room's info.
 *
 */
static int
parseinfo(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
{
	aim_snac_t *snac2;
	int ret = 0;

	if (!(snac2 = aim_remsnac(od, snac->id))) {
		gaim_debug_misc("oscar", "faim: chatnav_parse_info: received response to unknown request! (%08lx)\n", snac->id);
		return 0;
	}

	if (snac2->family != 0x000d) {
		gaim_debug_misc("oscar", "faim: chatnav_parse_info: received response that maps to corrupt request! (fam=%04x)\n", snac2->family);
		return 0;
	}

	/*
	 * We now know what the original SNAC subtype was.
	 */
	if (snac2->type == 0x0002) /* request chat rights */
		ret = parseinfo_perms(od, conn, mod, frame, snac, bs, snac2);
	else if (snac2->type == 0x0003) /* request exchange info */
		gaim_debug_misc("oscar", "chatnav_parse_info: resposne to exchange info\n");
	else if (snac2->type == 0x0004) /* request room info */
		gaim_debug_misc("oscar", "chatnav_parse_info: response to room info\n");
	else if (snac2->type == 0x0005) /* request more room info */
		gaim_debug_misc("oscar", "chatnav_parse_info: response to more room info\n");
	else if (snac2->type == 0x0006) /* request occupant list */
		gaim_debug_misc("oscar", "chatnav_parse_info: response to occupant info\n");
	else if (snac2->type == 0x0007) /* search for a room */
		gaim_debug_misc("oscar", "chatnav_parse_info: search results\n");
	else if (snac2->type == 0x0008) /* create room */
		ret = parseinfo_create(od, conn, mod, frame, snac, bs, snac2);
	else
		gaim_debug_misc("oscar", "chatnav_parse_info: unknown request subtype (%04x)\n", snac2->type);

	if (snac2)
		free(snac2->data);
	free(snac2);

	return ret;
}

static int
snachandler(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
{
	if (snac->subtype == 0x0009)
		return parseinfo(od, conn, mod, frame, snac, bs);

	return 0;
}

int
chatnav_modfirst(OscarData *od, aim_module_t *mod)
{
	mod->family = 0x000d;
	mod->version = 0x0001;
	mod->toolid = 0x0010;
	mod->toolversion = 0x0629;
	mod->flags = 0;
	strncpy(mod->name, "chatnav", sizeof(mod->name));
	mod->snachandler = snachandler;

	return 0;
}