14192
|
1 /*
|
|
2 * Gaim's oscar protocol plugin
|
|
3 * This file is the legal property of its developers.
|
|
4 * Please see the AUTHORS file distributed alongside this file.
|
|
5 *
|
|
6 * This library is free software; you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU Lesser General Public
|
|
8 * License as published by the Free Software Foundation; either
|
|
9 * version 2 of the License, or (at your option) any later version.
|
|
10 *
|
|
11 * This library is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * Lesser General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU Lesser General Public
|
|
17 * License along with this library; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20
|
|
21 /*
|
|
22 * Family 0x000e - Routines for the Chat service.
|
|
23 *
|
|
24 */
|
|
25
|
|
26 #include "oscar.h"
|
|
27
|
|
28 #include <string.h>
|
|
29
|
|
30 /* Stored in the ->internal of chat connections */
|
|
31 struct chatconnpriv
|
|
32 {
|
|
33 guint16 exchange;
|
|
34 char *name;
|
|
35 guint16 instance;
|
|
36 };
|
|
37
|
|
38 void
|
|
39 flap_connection_destroy_chat(OscarData *od, FlapConnection *conn)
|
|
40 {
|
|
41 struct chatconnpriv *ccp = (struct chatconnpriv *)conn->internal;
|
|
42
|
|
43 if (ccp)
|
|
44 free(ccp->name);
|
|
45 free(ccp);
|
|
46
|
|
47 return;
|
|
48 }
|
|
49
|
|
50 char *
|
|
51 aim_chat_getname(FlapConnection *conn)
|
|
52 {
|
|
53 struct chatconnpriv *ccp;
|
|
54
|
|
55 if (!conn)
|
|
56 return NULL;
|
|
57
|
|
58 if (conn->type != SNAC_FAMILY_CHAT)
|
|
59 return NULL;
|
|
60
|
|
61 ccp = (struct chatconnpriv *)conn->internal;
|
|
62
|
|
63 return ccp->name;
|
|
64 }
|
|
65
|
|
66 /* XXX get this into conn.c -- evil!! */
|
|
67 FlapConnection *
|
|
68 aim_chat_getconn(OscarData *od, const char *name)
|
|
69 {
|
14348
|
70 GSList *cur;
|
14192
|
71
|
|
72 for (cur = od->oscar_connections; cur; cur = cur->next)
|
|
73 {
|
|
74 FlapConnection *conn;
|
|
75 struct chatconnpriv *ccp;
|
|
76
|
|
77 conn = cur->data;
|
|
78 ccp = (struct chatconnpriv *)conn->internal;
|
|
79
|
|
80 if (conn->type != SNAC_FAMILY_CHAT)
|
|
81 continue;
|
|
82 if (!conn->internal) {
|
|
83 gaim_debug_misc("oscar", "faim: chat: chat connection with no name! (fd = %d)\n", conn->fd);
|
|
84 continue;
|
|
85 }
|
|
86
|
|
87 if (strcmp(ccp->name, name) == 0)
|
|
88 return conn;;
|
|
89 }
|
|
90
|
|
91 return NULL;
|
|
92 }
|
|
93
|
|
94 int
|
|
95 aim_chat_attachname(FlapConnection *conn, guint16 exchange, const char *roomname, guint16 instance)
|
|
96 {
|
|
97 struct chatconnpriv *ccp;
|
|
98
|
|
99 if (!conn || !roomname)
|
|
100 return -EINVAL;
|
|
101
|
|
102 if (conn->internal)
|
|
103 free(conn->internal);
|
|
104
|
|
105 ccp = g_new(struct chatconnpriv, 1);
|
|
106
|
|
107 ccp->exchange = exchange;
|
|
108 ccp->name = strdup(roomname);
|
|
109 ccp->instance = instance;
|
|
110
|
|
111 conn->internal = (void *)ccp;
|
|
112
|
|
113 return 0;
|
|
114 }
|
|
115
|
|
116 int
|
|
117 aim_chat_readroominfo(ByteStream *bs, struct aim_chat_roominfo *outinfo)
|
|
118 {
|
|
119 int namelen;
|
|
120
|
|
121 if (!bs || !outinfo)
|
|
122 return 0;
|
|
123
|
|
124 outinfo->exchange = byte_stream_get16(bs);
|
|
125 namelen = byte_stream_get8(bs);
|
|
126 outinfo->name = byte_stream_getstr(bs, namelen);
|
|
127 outinfo->instance = byte_stream_get16(bs);
|
|
128
|
|
129 return 0;
|
|
130 }
|
|
131
|
|
132 int
|
|
133 aim_chat_leaveroom(OscarData *od, const char *name)
|
|
134 {
|
|
135 FlapConnection *conn;
|
|
136
|
|
137 if (!(conn = aim_chat_getconn(od, name)))
|
|
138 return -ENOENT;
|
|
139
|
|
140 flap_connection_close(od, conn);
|
|
141
|
|
142 return 0;
|
|
143 }
|
|
144
|
|
145 /*
|
|
146 * Subtype 0x0002 - General room information. Lots of stuff.
|
|
147 *
|
|
148 * Values I know are in here but I haven't attached
|
|
149 * them to any of the 'Unknown's:
|
|
150 * - Language (English)
|
|
151 *
|
|
152 */
|
|
153 static int
|
|
154 infoupdate(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
|
|
155 {
|
|
156 aim_userinfo_t *userinfo = NULL;
|
|
157 aim_rxcallback_t userfunc;
|
|
158 int ret = 0;
|
|
159 int usercount;
|
|
160 guint8 detaillevel = 0;
|
|
161 char *roomname;
|
|
162 struct aim_chat_roominfo roominfo;
|
|
163 guint16 tlvcount = 0;
|
|
164 aim_tlvlist_t *tlvlist;
|
|
165 aim_tlv_t *tlv;
|
|
166 char *roomdesc;
|
|
167 guint16 flags;
|
|
168 guint32 creationtime;
|
|
169 guint16 maxmsglen, maxvisiblemsglen;
|
|
170 guint16 unknown_d2, unknown_d5;
|
|
171
|
|
172 aim_chat_readroominfo(bs, &roominfo);
|
|
173
|
|
174 detaillevel = byte_stream_get8(bs);
|
|
175
|
|
176 if (detaillevel != 0x02) {
|
|
177 gaim_debug_misc("oscar", "faim: chat_roomupdateinfo: detail level %d not supported\n", detaillevel);
|
|
178 return 1;
|
|
179 }
|
|
180
|
|
181 tlvcount = byte_stream_get16(bs);
|
|
182
|
|
183 /*
|
|
184 * Everything else are TLVs.
|
|
185 */
|
|
186 tlvlist = aim_tlvlist_read(bs);
|
|
187
|
|
188 /*
|
|
189 * TLV type 0x006a is the room name in Human Readable Form.
|
|
190 */
|
|
191 roomname = aim_tlv_getstr(tlvlist, 0x006a, 1);
|
|
192
|
|
193 /*
|
|
194 * Type 0x006f: Number of occupants.
|
|
195 */
|
|
196 usercount = aim_tlv_get16(tlvlist, 0x006f, 1);
|
|
197
|
|
198 /*
|
|
199 * Type 0x0073: Occupant list.
|
|
200 */
|
|
201 tlv = aim_tlv_gettlv(tlvlist, 0x0073, 1);
|
|
202 if (tlv != NULL)
|
|
203 {
|
|
204 int curoccupant = 0;
|
|
205 ByteStream occbs;
|
|
206
|
|
207 /* Allocate enough userinfo structs for all occupants */
|
|
208 userinfo = calloc(usercount, sizeof(aim_userinfo_t));
|
|
209
|
|
210 byte_stream_init(&occbs, tlv->value, tlv->length);
|
|
211
|
|
212 while (curoccupant < usercount)
|
|
213 aim_info_extract(od, &occbs, &userinfo[curoccupant++]);
|
|
214 }
|
|
215
|
|
216 /*
|
|
217 * Type 0x00c9: Flags. (AIM_CHATROOM_FLAG)
|
|
218 */
|
|
219 flags = aim_tlv_get16(tlvlist, 0x00c9, 1);
|
|
220
|
|
221 /*
|
|
222 * Type 0x00ca: Creation time (4 bytes)
|
|
223 */
|
|
224 creationtime = aim_tlv_get32(tlvlist, 0x00ca, 1);
|
|
225
|
|
226 /*
|
|
227 * Type 0x00d1: Maximum Message Length
|
|
228 */
|
|
229 maxmsglen = aim_tlv_get16(tlvlist, 0x00d1, 1);
|
|
230
|
|
231 /*
|
|
232 * Type 0x00d2: Unknown. (2 bytes)
|
|
233 */
|
|
234 unknown_d2 = aim_tlv_get16(tlvlist, 0x00d2, 1);
|
|
235
|
|
236 /*
|
|
237 * Type 0x00d3: Room Description
|
|
238 */
|
|
239 roomdesc = aim_tlv_getstr(tlvlist, 0x00d3, 1);
|
|
240
|
|
241 #if 0
|
|
242 /*
|
|
243 * Type 0x000d4: Unknown (flag only)
|
|
244 */
|
|
245 if (aim_tlv_gettlv(tlvlist, 0x000d4, 1)) {
|
|
246 /* Unhandled */
|
|
247 }
|
|
248 #endif
|
|
249
|
|
250 /*
|
|
251 * Type 0x00d5: Unknown. (1 byte)
|
|
252 */
|
|
253 unknown_d5 = aim_tlv_get8(tlvlist, 0x00d5, 1);
|
|
254
|
|
255 #if 0
|
|
256 /*
|
|
257 * Type 0x00d6: Encoding 1 ("us-ascii")
|
|
258 */
|
|
259 if (aim_tlv_gettlv(tlvlist, 0x000d6, 1)) {
|
|
260 /* Unhandled */
|
|
261 }
|
|
262
|
|
263 /*
|
|
264 * Type 0x00d7: Language 1 ("en")
|
|
265 */
|
|
266 if (aim_tlv_gettlv(tlvlist, 0x000d7, 1)) {
|
|
267 /* Unhandled */
|
|
268 }
|
|
269
|
|
270 /*
|
|
271 * Type 0x00d8: Encoding 2 ("us-ascii")
|
|
272 */
|
|
273 if (aim_tlv_gettlv(tlvlist, 0x000d8, 1)) {
|
|
274 /* Unhandled */
|
|
275 }
|
|
276
|
|
277 /*
|
|
278 * Type 0x00d9: Language 2 ("en")
|
|
279 */
|
|
280 if (aim_tlv_gettlv(tlvlist, 0x000d9, 1)) {
|
|
281 /* Unhandled */
|
|
282 }
|
|
283 #endif
|
|
284
|
|
285 /*
|
|
286 * Type 0x00da: Maximum visible message length
|
|
287 */
|
|
288 maxvisiblemsglen = aim_tlv_get16(tlvlist, 0x00da, 1);
|
|
289
|
|
290 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype))) {
|
|
291 ret = userfunc(od, conn,
|
|
292 frame,
|
|
293 &roominfo,
|
|
294 roomname,
|
|
295 usercount,
|
|
296 userinfo,
|
|
297 roomdesc,
|
|
298 flags,
|
|
299 creationtime,
|
|
300 maxmsglen,
|
|
301 unknown_d2,
|
|
302 unknown_d5,
|
|
303 maxvisiblemsglen);
|
|
304 }
|
|
305
|
|
306 free(roominfo.name);
|
|
307
|
|
308 while (usercount > 0)
|
|
309 aim_info_free(&userinfo[--usercount]);
|
|
310
|
|
311 free(userinfo);
|
|
312 free(roomname);
|
|
313 free(roomdesc);
|
|
314 aim_tlvlist_free(&tlvlist);
|
|
315
|
|
316 return ret;
|
|
317 }
|
|
318
|
|
319 /* Subtypes 0x0003 and 0x0004 */
|
|
320 static int
|
|
321 userlistchange(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
|
|
322 {
|
|
323 aim_userinfo_t *userinfo = NULL;
|
|
324 aim_rxcallback_t userfunc;
|
|
325 int curcount = 0, ret = 0;
|
|
326
|
|
327 while (byte_stream_empty(bs)) {
|
|
328 curcount++;
|
|
329 userinfo = realloc(userinfo, curcount * sizeof(aim_userinfo_t));
|
|
330 aim_info_extract(od, bs, &userinfo[curcount-1]);
|
|
331 }
|
|
332
|
|
333 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
|
|
334 ret = userfunc(od, conn, frame, curcount, userinfo);
|
|
335
|
|
336 aim_info_free(userinfo);
|
|
337 free(userinfo);
|
|
338
|
|
339 return ret;
|
|
340 }
|
|
341
|
|
342 /*
|
|
343 * Subtype 0x0005 - Send a Chat Message.
|
|
344 *
|
|
345 * Possible flags:
|
|
346 * AIM_CHATFLAGS_NOREFLECT -- Unset the flag that requests messages
|
|
347 * should be sent to their sender.
|
|
348 * AIM_CHATFLAGS_AWAY -- Mark the message as an autoresponse
|
|
349 * (Note that WinAIM does not honor this,
|
|
350 * and displays the message as normal.)
|
|
351 *
|
|
352 * XXX convert this to use tlvchains
|
|
353 */
|
|
354 int
|
|
355 aim_chat_send_im(OscarData *od, FlapConnection *conn, guint16 flags, const gchar *msg, int msglen, const char *encoding, const char *language)
|
|
356 {
|
|
357 int i;
|
|
358 FlapFrame *frame;
|
|
359 IcbmCookie *cookie;
|
|
360 aim_snacid_t snacid;
|
|
361 guint8 ckstr[8];
|
|
362 aim_tlvlist_t *tlvlist = NULL, *inner_tlvlist = NULL;
|
|
363
|
|
364 if (!od || !conn || !msg || (msglen <= 0))
|
|
365 return 0;
|
|
366
|
|
367 frame = flap_frame_new(od, 0x02, 1152);
|
|
368
|
|
369 snacid = aim_cachesnac(od, 0x000e, 0x0005, 0x0000, NULL, 0);
|
|
370 aim_putsnac(&frame->data, 0x000e, 0x0005, 0x0000, snacid);
|
|
371
|
|
372 /*
|
|
373 * Cookie
|
|
374 *
|
|
375 * XXX mkcookie should generate the cookie and cache it in one
|
|
376 * operation to preserve uniqueness.
|
|
377 */
|
|
378 for (i = 0; i < 8; i++)
|
|
379 ckstr[i] = (guint8)rand();
|
|
380
|
|
381 cookie = aim_mkcookie(ckstr, AIM_COOKIETYPE_CHAT, NULL);
|
|
382 cookie->data = NULL; /* XXX store something useful here */
|
|
383
|
|
384 aim_cachecookie(od, cookie);
|
|
385
|
|
386 /* ICBM Header */
|
|
387 byte_stream_putraw(&frame->data, ckstr, 8); /* Cookie */
|
|
388 byte_stream_put16(&frame->data, 0x0003); /* Channel */
|
|
389
|
|
390 /*
|
|
391 * Type 1: Flag meaning this message is destined to the room.
|
|
392 */
|
|
393 aim_tlvlist_add_noval(&tlvlist, 0x0001);
|
|
394
|
|
395 /*
|
|
396 * Type 6: Reflect
|
|
397 */
|
|
398 if (!(flags & AIM_CHATFLAGS_NOREFLECT))
|
|
399 aim_tlvlist_add_noval(&tlvlist, 0x0006);
|
|
400
|
|
401 /*
|
|
402 * Type 7: Autoresponse
|
|
403 */
|
|
404 if (flags & AIM_CHATFLAGS_AWAY)
|
|
405 aim_tlvlist_add_noval(&tlvlist, 0x0007);
|
|
406
|
|
407 /*
|
|
408 * SubTLV: Type 1: Message
|
|
409 */
|
|
410 aim_tlvlist_add_raw(&inner_tlvlist, 0x0001, msglen, (guchar *)msg);
|
|
411
|
|
412 /*
|
|
413 * SubTLV: Type 2: Encoding
|
|
414 */
|
|
415 if (encoding != NULL)
|
|
416 aim_tlvlist_add_str(&inner_tlvlist, 0x0002, encoding);
|
|
417
|
|
418 /*
|
|
419 * SubTLV: Type 3: Language
|
|
420 */
|
|
421 if (language != NULL)
|
|
422 aim_tlvlist_add_str(&inner_tlvlist, 0x0003, language);
|
|
423
|
|
424 /*
|
|
425 * Type 5: Message block. Contains more TLVs.
|
|
426 *
|
|
427 * This could include other information... We just
|
|
428 * put in a message TLV however.
|
|
429 *
|
|
430 */
|
|
431 aim_tlvlist_add_frozentlvlist(&tlvlist, 0x0005, &inner_tlvlist);
|
|
432
|
|
433 aim_tlvlist_write(&frame->data, &tlvlist);
|
|
434
|
|
435 aim_tlvlist_free(&inner_tlvlist);
|
|
436 aim_tlvlist_free(&tlvlist);
|
|
437
|
|
438 flap_connection_send(conn, frame);
|
|
439
|
|
440 return 0;
|
|
441 }
|
|
442
|
|
443 /*
|
|
444 * Subtype 0x0006
|
|
445 *
|
|
446 * We could probably include this in the normal ICBM parsing
|
|
447 * code as channel 0x0003, however, since only the start
|
|
448 * would be the same, we might as well do it here.
|
|
449 *
|
|
450 * General outline of this SNAC:
|
|
451 * snac
|
|
452 * cookie
|
|
453 * channel id
|
|
454 * tlvlist
|
|
455 * unknown
|
|
456 * source user info
|
|
457 * name
|
|
458 * evility
|
|
459 * userinfo tlvs
|
|
460 * online time
|
|
461 * etc
|
|
462 * message metatlv
|
|
463 * message tlv
|
|
464 * message string
|
|
465 * possibly others
|
|
466 *
|
|
467 */
|
|
468 static int
|
|
469 incomingim_ch3(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
|
|
470 {
|
|
471 int ret = 0, i;
|
|
472 aim_rxcallback_t userfunc;
|
|
473 aim_userinfo_t userinfo;
|
|
474 guint8 cookie[8];
|
|
475 guint16 channel;
|
|
476 aim_tlvlist_t *tlvlist;
|
|
477 char *msg = NULL;
|
|
478 int len = 0;
|
|
479 char *encoding = NULL, *language = NULL;
|
|
480 IcbmCookie *ck;
|
|
481 aim_tlv_t *tlv;
|
|
482 ByteStream tbs;
|
|
483
|
|
484 memset(&userinfo, 0, sizeof(aim_userinfo_t));
|
|
485
|
|
486 /*
|
|
487 * Read ICBM Cookie.
|
|
488 */
|
|
489 for (i = 0; i < 8; i++)
|
|
490 cookie[i] = byte_stream_get8(bs);
|
|
491
|
|
492 if ((ck = aim_uncachecookie(od, cookie, AIM_COOKIETYPE_CHAT))) {
|
|
493 free(ck->data);
|
|
494 free(ck);
|
|
495 }
|
|
496
|
|
497 /*
|
|
498 * Channel ID
|
|
499 *
|
|
500 * Channel 0x0003 is used for chat messages.
|
|
501 *
|
|
502 */
|
|
503 channel = byte_stream_get16(bs);
|
|
504
|
|
505 if (channel != 0x0003) {
|
|
506 gaim_debug_misc("oscar", "faim: chat_incoming: unknown channel! (0x%04x)\n", channel);
|
|
507 return 0;
|
|
508 }
|
|
509
|
|
510 /*
|
|
511 * Start parsing TLVs right away.
|
|
512 */
|
|
513 tlvlist = aim_tlvlist_read(bs);
|
|
514
|
|
515 /*
|
|
516 * Type 0x0003: Source User Information
|
|
517 */
|
|
518 tlv = aim_tlv_gettlv(tlvlist, 0x0003, 1);
|
|
519 if (tlv != NULL)
|
|
520 {
|
|
521 byte_stream_init(&tbs, tlv->value, tlv->length);
|
|
522 aim_info_extract(od, &tbs, &userinfo);
|
|
523 }
|
|
524
|
|
525 #if 0
|
|
526 /*
|
|
527 * Type 0x0001: If present, it means it was a message to the
|
|
528 * room (as opposed to a whisper).
|
|
529 */
|
|
530 if (aim_tlv_gettlv(tlvlist, 0x0001, 1)) {
|
|
531 /* Unhandled */
|
|
532 }
|
|
533 #endif
|
|
534
|
|
535 /*
|
|
536 * Type 0x0005: Message Block. Conains more TLVs.
|
|
537 */
|
|
538 tlv = aim_tlv_gettlv(tlvlist, 0x0005, 1);
|
|
539 if (tlv != NULL)
|
|
540 {
|
|
541 aim_tlvlist_t *inner_tlvlist;
|
|
542 aim_tlv_t *inner_tlv;
|
|
543
|
|
544 byte_stream_init(&tbs, tlv->value, tlv->length);
|
|
545 inner_tlvlist = aim_tlvlist_read(&tbs);
|
|
546
|
|
547 /*
|
|
548 * Type 0x0001: Message.
|
|
549 */
|
|
550 inner_tlv = aim_tlv_gettlv(inner_tlvlist, 0x0001, 1);
|
|
551 if (inner_tlv != NULL)
|
|
552 {
|
|
553 len = inner_tlv->length;
|
|
554 msg = aim_tlv_getvalue_as_string(inner_tlv);
|
|
555 }
|
|
556
|
|
557 /*
|
|
558 * Type 0x0002: Encoding.
|
|
559 */
|
|
560 encoding = aim_tlv_getstr(inner_tlvlist, 0x0002, 1);
|
|
561
|
|
562 /*
|
|
563 * Type 0x0003: Language.
|
|
564 */
|
|
565 language = aim_tlv_getstr(inner_tlvlist, 0x0003, 1);
|
|
566
|
|
567 aim_tlvlist_free(&inner_tlvlist);
|
|
568 }
|
|
569
|
|
570 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
|
|
571 ret = userfunc(od, conn, frame, &userinfo, len, msg, encoding, language);
|
|
572
|
|
573 aim_info_free(&userinfo);
|
|
574 free(msg);
|
|
575 free(encoding);
|
|
576 free(language);
|
|
577 aim_tlvlist_free(&tlvlist);
|
|
578
|
|
579 return ret;
|
|
580 }
|
|
581
|
|
582 static int
|
|
583 snachandler(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
|
|
584 {
|
|
585 if (snac->subtype == 0x0002)
|
|
586 return infoupdate(od, conn, mod, frame, snac, bs);
|
|
587 else if ((snac->subtype == 0x0003) || (snac->subtype == 0x0004))
|
|
588 return userlistchange(od, conn, mod, frame, snac, bs);
|
|
589 else if (snac->subtype == 0x0006)
|
|
590 return incomingim_ch3(od, conn, mod, frame, snac, bs);
|
|
591
|
|
592 return 0;
|
|
593 }
|
|
594
|
|
595 int
|
|
596 chat_modfirst(OscarData *od, aim_module_t *mod)
|
|
597 {
|
|
598 mod->family = 0x000e;
|
|
599 mod->version = 0x0001;
|
|
600 mod->toolid = 0x0010;
|
|
601 mod->toolversion = 0x0629;
|
|
602 mod->flags = 0;
|
|
603 strncpy(mod->name, "chat", sizeof(mod->name));
|
|
604 mod->snachandler = snachandler;
|
|
605
|
|
606 return 0;
|
|
607 }
|