comparison libpurple/protocols/oscar/family_icbm.c @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
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 0x0004 - Routines for sending/receiving Instant Messages.
23 *
24 * Note the term ICBM (Inter-Client Basic Message) which blankets
25 * all types of generically routed through-server messages. Within
26 * the ICBM types (family 4), a channel is defined. Each channel
27 * represents a different type of message. Channel 1 is used for
28 * what would commonly be called an "instant message". Channel 2
29 * is used for negotiating "rendezvous". These transactions end in
30 * something more complex happening, such as a chat invitation, or
31 * a file transfer. Channel 3 is used for chat messages (not in
32 * the same family as these channels). Channel 4 is used for
33 * various ICQ messages. Examples are normal messages, URLs, and
34 * old-style authorization.
35 *
36 * In addition to the channel, every ICBM contains a cookie. For
37 * standard IMs, these are only used for error messages. However,
38 * the more complex rendezvous messages make suitably more complex
39 * use of this field.
40 *
41 * TODO: Split this up into an im.c file an an icbm.c file. It
42 * will be beautiful, you'll see.
43 *
44 * Make sure flap_connection_findbygroup is used by all functions.
45 */
46
47 #include "oscar.h"
48 #include "peer.h"
49
50 #ifdef _WIN32
51 #include "win32dep.h"
52 #endif
53
54 /**
55 * Add a standard ICBM header to the given bstream with the given
56 * information.
57 *
58 * @param bs The bstream to write the ICBM header to.
59 * @param c c is for cookie, and cookie is for me.
60 * @param channel The ICBM channel (1 through 4).
61 * @param sn Null-terminated scrizeen nizame.
62 * @return The number of bytes written. It's really not useful.
63 */
64 static int aim_im_puticbm(ByteStream *bs, const guchar *c, guint16 channel, const char *sn)
65 {
66 byte_stream_putraw(bs, c, 8);
67 byte_stream_put16(bs, channel);
68 byte_stream_put8(bs, strlen(sn));
69 byte_stream_putstr(bs, sn);
70 return 8+2+1+strlen(sn);
71 }
72
73 /**
74 * Generates a random ICBM cookie in a character array of length 8
75 * and copies it into the variable passed as cookie
76 * TODO: Maybe we should stop limiting our characters to the visible range?
77 */
78 void aim_icbm_makecookie(guchar *cookie)
79 {
80 int i;
81
82 /* Should be like "21CBF95" and null terminated */
83 for (i = 0; i < 7; i++)
84 cookie[i] = 0x30 + ((guchar)rand() % 10);
85 cookie[7] = '\0';
86 }
87
88 /*
89 * Takes a msghdr (and a length) and returns a client type
90 * code. Note that this is *only a guess* and has a low likelihood
91 * of actually being accurate.
92 *
93 * Its based on experimental data, with the help of Eric Warmenhoven
94 * who seems to have collected a wide variety of different AIM clients.
95 *
96 *
97 * Heres the current collection:
98 * 0501 0003 0101 0101 01 AOL Mobile Communicator, WinAIM 1.0.414
99 * 0501 0003 0101 0201 01 WinAIM 2.0.847, 2.1.1187, 3.0.1464,
100 * 4.3.2229, 4.4.2286
101 * 0501 0004 0101 0102 0101 WinAIM 4.1.2010, libfaim (right here)
102 * 0501 0003 0101 02 WinAIM 5
103 * 0501 0001 01 iChat x.x, mobile buddies
104 * 0501 0001 0101 01 AOL v6.0, CompuServe 2000 v6.0, any TOC client
105 * 0501 0002 0106 WinICQ 5.45.1.3777.85
106 *
107 * Note that in this function, only the feature bytes are tested, since
108 * the rest will always be the same.
109 *
110 */
111 guint16 aim_im_fingerprint(const guint8 *msghdr, int len)
112 {
113 static const struct {
114 guint16 clientid;
115 int len;
116 guint8 data[10];
117 } fingerprints[] = {
118 /* AOL Mobile Communicator, WinAIM 1.0.414 */
119 { AIM_CLIENTTYPE_MC,
120 3, {0x01, 0x01, 0x01}},
121
122 /* WinAIM 2.0.847, 2.1.1187, 3.0.1464, 4.3.2229, 4.4.2286 */
123 { AIM_CLIENTTYPE_WINAIM,
124 3, {0x01, 0x01, 0x02}},
125
126 /* WinAIM 4.1.2010, libfaim */
127 { AIM_CLIENTTYPE_WINAIM41,
128 4, {0x01, 0x01, 0x01, 0x02}},
129
130 /* AOL v6.0, CompuServe 2000 v6.0, any TOC client */
131 { AIM_CLIENTTYPE_AOL_TOC,
132 1, {0x01}},
133
134 { 0, 0, {0x00}}
135 };
136 int i;
137
138 if (!msghdr || (len <= 0))
139 return AIM_CLIENTTYPE_UNKNOWN;
140
141 for (i = 0; fingerprints[i].len; i++) {
142 if (fingerprints[i].len != len)
143 continue;
144 if (memcmp(fingerprints[i].data, msghdr, fingerprints[i].len) == 0)
145 return fingerprints[i].clientid;
146 }
147
148 return AIM_CLIENTTYPE_UNKNOWN;
149 }
150
151 /**
152 * Subtype 0x0002 - Set ICBM parameters.
153 *
154 * I definitely recommend sending this. If you don't, you'll be stuck
155 * with the rather unreasonable defaults.
156 *
157 */
158 int aim_im_setparams(OscarData *od, struct aim_icbmparameters *params)
159 {
160 FlapConnection *conn;
161 FlapFrame *frame;
162 aim_snacid_t snacid;
163
164 if (!od || !(conn = flap_connection_findbygroup(od, 0x0004)))
165 return -EINVAL;
166
167 if (!params)
168 return -EINVAL;
169
170 frame = flap_frame_new(od, 0x02, 10+16);
171
172 snacid = aim_cachesnac(od, 0x0004, 0x0002, 0x0000, NULL, 0);
173 aim_putsnac(&frame->data, 0x0004, 0x0002, 0x0000, snacid);
174
175 /* This is read-only (see Parameter Reply). Must be set to zero here. */
176 byte_stream_put16(&frame->data, 0x0000);
177
178 /* These are all read-write */
179 byte_stream_put32(&frame->data, params->flags);
180 byte_stream_put16(&frame->data, params->maxmsglen);
181 byte_stream_put16(&frame->data, params->maxsenderwarn);
182 byte_stream_put16(&frame->data, params->maxrecverwarn);
183 byte_stream_put32(&frame->data, params->minmsginterval);
184
185 flap_connection_send(conn, frame);
186
187 return 0;
188 }
189
190 /**
191 * Subtype 0x0004 - Request ICBM parameter information.
192 *
193 */
194 int aim_im_reqparams(OscarData *od)
195 {
196 FlapConnection *conn;
197
198 if (!od || !(conn = flap_connection_findbygroup(od, 0x0004)))
199 return -EINVAL;
200
201 aim_genericreq_n_snacid(od, conn, 0x0004, 0x0004);
202
203 return 0;
204 }
205
206 /**
207 * Subtype 0x0005 - Receive parameter information.
208 *
209 */
210 static int aim_im_paraminfo(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
211 {
212 aim_rxcallback_t userfunc;
213 struct aim_icbmparameters params;
214
215 params.maxchan = byte_stream_get16(bs);
216 params.flags = byte_stream_get32(bs);
217 params.maxmsglen = byte_stream_get16(bs);
218 params.maxsenderwarn = byte_stream_get16(bs);
219 params.maxrecverwarn = byte_stream_get16(bs);
220 params.minmsginterval = byte_stream_get32(bs);
221
222 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
223 return userfunc(od, conn, frame, &params);
224
225 return 0;
226 }
227
228 /**
229 * Subtype 0x0006 - Send an ICBM (instant message).
230 *
231 *
232 * Possible flags:
233 * AIM_IMFLAGS_AWAY -- Marks the message as an autoresponse
234 * AIM_IMFLAGS_ACK -- Requests that the server send an ack
235 * when the message is received (of type 0x0004/0x000c)
236 * AIM_IMFLAGS_OFFLINE--If destination is offline, store it until they are
237 * online (probably ICQ only).
238 *
239 * Generally, you should use the lowest encoding possible to send
240 * your message. If you only use basic punctuation and the generic
241 * Latin alphabet, use ASCII7 (no flags). If you happen to use non-ASCII7
242 * characters, but they are all clearly defined in ISO-8859-1, then
243 * use that. Keep in mind that not all characters in the PC ASCII8
244 * character set are defined in the ISO standard. For those cases (most
245 * notably when the (r) symbol is used), you must use the full UNICODE
246 * encoding for your message. In UNICODE mode, _all_ characters must
247 * occupy 16bits, including ones that are not special. (Remember that
248 * the first 128 UNICODE symbols are equivalent to ASCII7, however they
249 * must be prefixed with a zero high order byte.)
250 *
251 * I strongly discourage the use of UNICODE mode, mainly because none
252 * of the clients I use can parse those messages (and besides that,
253 * wchars are difficult and non-portable to handle in most UNIX environments).
254 * If you really need to include special characters, use the HTML UNICODE
255 * entities. These are of the form &#2026; where 2026 is the hex
256 * representation of the UNICODE index (in this case, UNICODE
257 * "Horizontal Ellipsis", or 133 in in ASCII8).
258 *
259 * Implementation note: Since this is one of the most-used functions
260 * in all of libfaim, it is written with performance in mind. As such,
261 * it is not as clear as it could be in respect to how this message is
262 * supposed to be layed out. Most obviously, tlvlists should be used
263 * instead of writing out the bytes manually.
264 *
265 * XXX - more precise verification that we never send SNACs larger than 8192
266 * XXX - check SNAC size for multipart
267 *
268 */
269 int aim_im_sendch1_ext(OscarData *od, struct aim_sendimext_args *args)
270 {
271 FlapConnection *conn;
272 aim_snacid_t snacid;
273 ByteStream data;
274 guchar cookie[8];
275 int msgtlvlen;
276 static const guint8 deffeatures[] = { 0x01, 0x01, 0x01, 0x02 };
277
278 if (!od || !(conn = flap_connection_findbygroup(od, 0x0004)))
279 return -EINVAL;
280
281 if (!args)
282 return -EINVAL;
283
284 if (args->flags & AIM_IMFLAGS_MULTIPART) {
285 if (args->mpmsg->numparts == 0)
286 return -EINVAL;
287 } else {
288 if (!args->msg || (args->msglen <= 0))
289 return -EINVAL;
290
291 if (args->msglen >= MAXMSGLEN)
292 return -E2BIG;
293 }
294
295 /* Painfully calculate the size of the message TLV */
296 msgtlvlen = 1 + 1; /* 0501 */
297
298 if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES)
299 msgtlvlen += 2 + args->featureslen;
300 else
301 msgtlvlen += 2 + sizeof(deffeatures);
302
303 if (args->flags & AIM_IMFLAGS_MULTIPART) {
304 aim_mpmsg_section_t *sec;
305
306 for (sec = args->mpmsg->parts; sec; sec = sec->next) {
307 msgtlvlen += 2 /* 0101 */ + 2 /* block len */;
308 msgtlvlen += 4 /* charset */ + sec->datalen;
309 }
310
311 } else {
312 msgtlvlen += 2 /* 0101 */ + 2 /* block len */;
313 msgtlvlen += 4 /* charset */ + args->msglen;
314 }
315
316 byte_stream_new(&data, msgtlvlen + 128);
317
318 /* Generate an ICBM cookie */
319 aim_icbm_makecookie(cookie);
320
321 /* ICBM header */
322 aim_im_puticbm(&data, cookie, 0x0001, args->destsn);
323
324 /* Message TLV (type 0x0002) */
325 byte_stream_put16(&data, 0x0002);
326 byte_stream_put16(&data, msgtlvlen);
327
328 /* Features TLV (type 0x0501) */
329 byte_stream_put16(&data, 0x0501);
330 if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES) {
331 byte_stream_put16(&data, args->featureslen);
332 byte_stream_putraw(&data, args->features, args->featureslen);
333 } else {
334 byte_stream_put16(&data, sizeof(deffeatures));
335 byte_stream_putraw(&data, deffeatures, sizeof(deffeatures));
336 }
337
338 if (args->flags & AIM_IMFLAGS_MULTIPART) {
339 aim_mpmsg_section_t *sec;
340
341 /* Insert each message part in a TLV (type 0x0101) */
342 for (sec = args->mpmsg->parts; sec; sec = sec->next) {
343 byte_stream_put16(&data, 0x0101);
344 byte_stream_put16(&data, sec->datalen + 4);
345 byte_stream_put16(&data, sec->charset);
346 byte_stream_put16(&data, sec->charsubset);
347 byte_stream_putraw(&data, (guchar *)sec->data, sec->datalen);
348 }
349
350 } else {
351
352 /* Insert message text in a TLV (type 0x0101) */
353 byte_stream_put16(&data, 0x0101);
354
355 /* Message block length */
356 byte_stream_put16(&data, args->msglen + 0x04);
357
358 /* Character set */
359 byte_stream_put16(&data, args->charset);
360 byte_stream_put16(&data, args->charsubset);
361
362 /* Message. Not terminated */
363 byte_stream_putraw(&data, (guchar *)args->msg, args->msglen);
364 }
365
366 /* Set the Autoresponse flag */
367 if (args->flags & AIM_IMFLAGS_AWAY) {
368 byte_stream_put16(&data, 0x0004);
369 byte_stream_put16(&data, 0x0000);
370 } else if (args->flags & AIM_IMFLAGS_ACK) {
371 /* Set the Request Acknowledge flag */
372 byte_stream_put16(&data, 0x0003);
373 byte_stream_put16(&data, 0x0000);
374 }
375
376 if (args->flags & AIM_IMFLAGS_OFFLINE) {
377 byte_stream_put16(&data, 0x0006);
378 byte_stream_put16(&data, 0x0000);
379 }
380
381 /*
382 * Set the I HAVE A REALLY PURTY ICON flag.
383 * XXX - This should really only be sent on initial
384 * IMs and when you change your icon.
385 */
386 if (args->flags & AIM_IMFLAGS_HASICON) {
387 byte_stream_put16(&data, 0x0008);
388 byte_stream_put16(&data, 0x000c);
389 byte_stream_put32(&data, args->iconlen);
390 byte_stream_put16(&data, 0x0001);
391 byte_stream_put16(&data, args->iconsum);
392 byte_stream_put32(&data, args->iconstamp);
393 }
394
395 /*
396 * Set the Buddy Icon Requested flag.
397 * XXX - Every time? Surely not...
398 */
399 if (args->flags & AIM_IMFLAGS_BUDDYREQ) {
400 byte_stream_put16(&data, 0x0009);
401 byte_stream_put16(&data, 0x0000);
402 }
403
404 /* XXX - should be optional */
405 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, args->destsn, strlen(args->destsn)+1);
406
407 flap_connection_send_snac(od, conn, 0x0004, 0x0006, 0x0000, snacid, &data);
408 g_free(data.data);
409
410 /* clean out SNACs over 60sec old */
411 aim_cleansnacs(od, 60);
412
413 return 0;
414 }
415
416 /*
417 * Simple wrapper for aim_im_sendch1_ext()
418 *
419 * You cannot use aim_send_im if you need the HASICON flag. You must
420 * use aim_im_sendch1_ext directly for that.
421 *
422 * aim_send_im also cannot be used if you require UNICODE messages, because
423 * that requires an explicit message length. Use aim_im_sendch1_ext().
424 *
425 */
426 int aim_im_sendch1(OscarData *od, const char *sn, guint16 flags, const char *msg)
427 {
428 struct aim_sendimext_args args;
429
430 args.destsn = sn;
431 args.flags = flags;
432 args.msg = msg;
433 args.msglen = strlen(msg);
434 args.charset = 0x0000;
435 args.charsubset = 0x0000;
436
437 /* Make these don't get set by accident -- they need aim_im_sendch1_ext */
438 args.flags &= ~(AIM_IMFLAGS_CUSTOMFEATURES | AIM_IMFLAGS_HASICON | AIM_IMFLAGS_MULTIPART);
439
440 return aim_im_sendch1_ext(od, &args);
441 }
442
443 /*
444 * Subtype 0x0006 - Send a chat invitation.
445 */
446 int aim_im_sendch2_chatinvite(OscarData *od, const char *sn, const char *msg, guint16 exchange, const char *roomname, guint16 instance)
447 {
448 FlapConnection *conn;
449 FlapFrame *frame;
450 aim_snacid_t snacid;
451 IcbmCookie *msgcookie;
452 struct aim_invite_priv *priv;
453 guchar cookie[8];
454 aim_tlvlist_t *otl = NULL, *itl = NULL;
455 ByteStream hdrbs;
456
457 if (!od || !(conn = flap_connection_findbygroup(od, 0x0004)))
458 return -EINVAL;
459
460 if (!sn || !msg || !roomname)
461 return -EINVAL;
462
463 aim_icbm_makecookie(cookie);
464
465 frame = flap_frame_new(od, 0x02, 1152+strlen(sn)+strlen(roomname)+strlen(msg));
466
467 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, sn, strlen(sn)+1);
468 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
469
470 /* XXX should be uncached by an unwritten 'invite accept' handler */
471 priv = malloc(sizeof(struct aim_invite_priv));
472 priv->sn = strdup(sn);
473 priv->roomname = strdup(roomname);
474 priv->exchange = exchange;
475 priv->instance = instance;
476
477 if ((msgcookie = aim_mkcookie(cookie, AIM_COOKIETYPE_INVITE, priv)))
478 aim_cachecookie(od, msgcookie);
479 else
480 free(priv);
481
482 /* ICBM Header */
483 aim_im_puticbm(&frame->data, cookie, 0x0002, sn);
484
485 /*
486 * TLV t(0005)
487 *
488 * Everything else is inside this TLV.
489 *
490 * Sigh. AOL was rather inconsistent right here. So we have
491 * to play some minor tricks. Right inside the type 5 is some
492 * raw data, followed by a series of TLVs.
493 *
494 */
495 byte_stream_new(&hdrbs, 2+8+16+6+4+4+strlen(msg)+4+2+1+strlen(roomname)+2);
496
497 byte_stream_put16(&hdrbs, 0x0000); /* Unknown! */
498 byte_stream_putraw(&hdrbs, cookie, sizeof(cookie)); /* I think... */
499 byte_stream_putcaps(&hdrbs, OSCAR_CAPABILITY_CHAT);
500
501 aim_tlvlist_add_16(&itl, 0x000a, 0x0001);
502 aim_tlvlist_add_noval(&itl, 0x000f);
503 aim_tlvlist_add_str(&itl, 0x000c, msg);
504 aim_tlvlist_add_chatroom(&itl, 0x2711, exchange, roomname, instance);
505 aim_tlvlist_write(&hdrbs, &itl);
506
507 aim_tlvlist_add_raw(&otl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
508 g_free(hdrbs.data);
509
510 aim_tlvlist_write(&frame->data, &otl);
511
512 aim_tlvlist_free(&itl);
513 aim_tlvlist_free(&otl);
514
515 flap_connection_send(conn, frame);
516
517 return 0;
518 }
519
520 /**
521 * Subtype 0x0006 - Send your icon to a given user.
522 *
523 * This is also performance sensitive. (If you can believe it...)
524 *
525 */
526 int aim_im_sendch2_icon(OscarData *od, const char *sn, const guint8 *icon, int iconlen, time_t stamp, guint16 iconsum)
527 {
528 FlapConnection *conn;
529 FlapFrame *frame;
530 aim_snacid_t snacid;
531 guchar cookie[8];
532
533 if (!od || !(conn = flap_connection_findbygroup(od, 0x0004)))
534 return -EINVAL;
535
536 if (!sn || !icon || (iconlen <= 0) || (iconlen >= MAXICONLEN))
537 return -EINVAL;
538
539 aim_icbm_makecookie(cookie);
540
541 frame = flap_frame_new(od, 0x02, 10+8+2+1+strlen(sn)+2+2+2+8+16+2+2+2+2+2+2+2+4+4+4+iconlen+strlen(AIM_ICONIDENT)+2+2);
542
543 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, NULL, 0);
544 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
545
546 /* ICBM header */
547 aim_im_puticbm(&frame->data, cookie, 0x0002, sn);
548
549 /*
550 * TLV t(0005)
551 *
552 * Encompasses everything below.
553 */
554 byte_stream_put16(&frame->data, 0x0005);
555 byte_stream_put16(&frame->data, 2+8+16+6+4+4+iconlen+4+4+4+strlen(AIM_ICONIDENT));
556
557 byte_stream_put16(&frame->data, 0x0000);
558 byte_stream_putraw(&frame->data, cookie, 8);
559 byte_stream_putcaps(&frame->data, OSCAR_CAPABILITY_BUDDYICON);
560
561 /* TLV t(000a) */
562 byte_stream_put16(&frame->data, 0x000a);
563 byte_stream_put16(&frame->data, 0x0002);
564 byte_stream_put16(&frame->data, 0x0001);
565
566 /* TLV t(000f) */
567 byte_stream_put16(&frame->data, 0x000f);
568 byte_stream_put16(&frame->data, 0x0000);
569
570 /* TLV t(2711) */
571 byte_stream_put16(&frame->data, 0x2711);
572 byte_stream_put16(&frame->data, 4+4+4+iconlen+strlen(AIM_ICONIDENT));
573 byte_stream_put16(&frame->data, 0x0000);
574 byte_stream_put16(&frame->data, iconsum);
575 byte_stream_put32(&frame->data, iconlen);
576 byte_stream_put32(&frame->data, stamp);
577 byte_stream_putraw(&frame->data, icon, iconlen);
578 byte_stream_putstr(&frame->data, AIM_ICONIDENT);
579
580 /* TLV t(0003) */
581 byte_stream_put16(&frame->data, 0x0003);
582 byte_stream_put16(&frame->data, 0x0000);
583
584 flap_connection_send(conn, frame);
585
586 return 0;
587 }
588
589 /*
590 * Subtype 0x0006 - Send a rich text message.
591 *
592 * This only works for ICQ 2001b (thats 2001 not 2000). Better, only
593 * send it to clients advertising the RTF capability. In fact, if you send
594 * it to a client that doesn't support that capability, the server will gladly
595 * bounce it back to you.
596 *
597 * You'd think this would be in icq.c, but, well, I'm trying to stick with
598 * the one-group-per-file scheme as much as possible. This could easily
599 * be an exception, since Rendezvous IMs are external of the Oscar core,
600 * and therefore are undefined. Really I just need to think of a good way to
601 * make an interface similar to what AOL actually uses. But I'm not using COM.
602 *
603 */
604 int aim_im_sendch2_rtfmsg(OscarData *od, struct aim_sendrtfmsg_args *args)
605 {
606 FlapConnection *conn;
607 FlapFrame *frame;
608 aim_snacid_t snacid;
609 guchar cookie[8];
610 const char rtfcap[] = {"{97B12751-243C-4334-AD22-D6ABF73F1492}"}; /* OSCAR_CAPABILITY_ICQRTF capability in string form */
611 int servdatalen;
612
613 if (!od || !(conn = flap_connection_findbygroup(od, 0x0004)))
614 return -EINVAL;
615
616 if (!args || !args->destsn || !args->rtfmsg)
617 return -EINVAL;
618
619 servdatalen = 2+2+16+2+4+1+2 + 2+2+4+4+4 + 2+4+2+strlen(args->rtfmsg)+1 + 4+4+4+strlen(rtfcap)+1;
620
621 aim_icbm_makecookie(cookie);
622
623 frame = flap_frame_new(od, 0x02, 10+128+servdatalen);
624
625 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, NULL, 0);
626 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
627
628 /* ICBM header */
629 aim_im_puticbm(&frame->data, cookie, 0x0002, args->destsn);
630
631 /* TLV t(0005) - Encompasses everything below. */
632 byte_stream_put16(&frame->data, 0x0005);
633 byte_stream_put16(&frame->data, 2+8+16 + 2+2+2 + 2+2 + 2+2+servdatalen);
634
635 byte_stream_put16(&frame->data, 0x0000);
636 byte_stream_putraw(&frame->data, cookie, 8);
637 byte_stream_putcaps(&frame->data, OSCAR_CAPABILITY_ICQSERVERRELAY);
638
639 /* t(000a) l(0002) v(0001) */
640 byte_stream_put16(&frame->data, 0x000a);
641 byte_stream_put16(&frame->data, 0x0002);
642 byte_stream_put16(&frame->data, 0x0001);
643
644 /* t(000f) l(0000) v() */
645 byte_stream_put16(&frame->data, 0x000f);
646 byte_stream_put16(&frame->data, 0x0000);
647
648 /* Service Data TLV */
649 byte_stream_put16(&frame->data, 0x2711);
650 byte_stream_put16(&frame->data, servdatalen);
651
652 byte_stream_putle16(&frame->data, 11 + 16 /* 11 + (sizeof CLSID) */);
653 byte_stream_putle16(&frame->data, 9);
654 byte_stream_putcaps(&frame->data, OSCAR_CAPABILITY_EMPTY);
655 byte_stream_putle16(&frame->data, 0);
656 byte_stream_putle32(&frame->data, 0);
657 byte_stream_putle8(&frame->data, 0);
658 byte_stream_putle16(&frame->data, 0x03ea); /* trid1 */
659
660 byte_stream_putle16(&frame->data, 14);
661 byte_stream_putle16(&frame->data, 0x03eb); /* trid2 */
662 byte_stream_putle32(&frame->data, 0);
663 byte_stream_putle32(&frame->data, 0);
664 byte_stream_putle32(&frame->data, 0);
665
666 byte_stream_putle16(&frame->data, 0x0001);
667 byte_stream_putle32(&frame->data, 0);
668 byte_stream_putle16(&frame->data, strlen(args->rtfmsg)+1);
669 byte_stream_putraw(&frame->data, (const guint8 *)args->rtfmsg, strlen(args->rtfmsg)+1);
670
671 byte_stream_putle32(&frame->data, args->fgcolor);
672 byte_stream_putle32(&frame->data, args->bgcolor);
673 byte_stream_putle32(&frame->data, strlen(rtfcap)+1);
674 byte_stream_putraw(&frame->data, (const guint8 *)rtfcap, strlen(rtfcap)+1);
675
676 flap_connection_send(conn, frame);
677
678 return 0;
679 }
680
681 /**
682 * Cancel a rendezvous invitation. It could be an invitation to
683 * establish a direct connection, or a file-send, or a chat invite.
684 */
685 void
686 aim_im_sendch2_cancel(PeerConnection *peer_conn)
687 {
688 OscarData *od;
689 FlapConnection *conn;
690 FlapFrame *frame;
691 aim_snacid_t snacid;
692 aim_tlvlist_t *tl = NULL, *itl = NULL;
693 ByteStream hdrbs;
694
695 od = peer_conn->od;
696 conn = flap_connection_findbygroup(od, 0x0004);
697 if (conn == NULL)
698 return;
699
700 frame = flap_frame_new(od, 0x02, 128+strlen(peer_conn->sn));
701
702 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, NULL, 0);
703 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
704
705 /* ICBM header */
706 aim_im_puticbm(&frame->data, peer_conn->cookie, 0x0002, peer_conn->sn);
707
708 aim_tlvlist_add_noval(&tl, 0x0003);
709
710 byte_stream_new(&hdrbs, 64);
711
712 byte_stream_put16(&hdrbs, AIM_RENDEZVOUS_CANCEL);
713 byte_stream_putraw(&hdrbs, peer_conn->cookie, 8);
714 byte_stream_putcaps(&hdrbs, peer_conn->type);
715
716 /* This TLV means "cancel!" */
717 aim_tlvlist_add_16(&itl, 0x000b, 0x0001);
718 aim_tlvlist_write(&hdrbs, &itl);
719
720 aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
721 g_free(hdrbs.data);
722
723 aim_tlvlist_write(&frame->data, &tl);
724
725 aim_tlvlist_free(&itl);
726 aim_tlvlist_free(&tl);
727
728 flap_connection_send(conn, frame);
729 }
730
731 /**
732 * Subtype 0x0006 - Send an "I accept and I've connected to
733 * you" message.
734 */
735 void
736 aim_im_sendch2_connected(PeerConnection *peer_conn)
737 {
738 OscarData *od;
739 FlapConnection *conn;
740 FlapFrame *frame;
741 aim_snacid_t snacid;
742
743 od = peer_conn->od;
744 conn = flap_connection_findbygroup(od, 0x0004);
745 if (conn == NULL)
746 return;
747
748 frame = flap_frame_new(od, 0x02, 10 + 11+strlen(peer_conn->sn) + 4+2+8+16);
749
750 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, NULL, 0);
751 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
752
753 /* ICBM header */
754 aim_im_puticbm(&frame->data, peer_conn->cookie, 0x0002, peer_conn->sn);
755
756 byte_stream_put16(&frame->data, 0x0005);
757 byte_stream_put16(&frame->data, 0x001a);
758 byte_stream_put16(&frame->data, AIM_RENDEZVOUS_CONNECTED);
759 byte_stream_putraw(&frame->data, peer_conn->cookie, 8);
760 byte_stream_putcaps(&frame->data, peer_conn->type);
761
762 flap_connection_send(conn, frame);
763 }
764
765 /**
766 * Subtype 0x0006 - Send a direct connect rendezvous ICBM. This
767 * could have a number of meanings, depending on the content:
768 * "I want you to connect to me"
769 * "I want to connect to you"
770 * "I want to connect through a proxy server"
771 */
772 void
773 aim_im_sendch2_odc_requestdirect(OscarData *od, guchar *cookie, const char *sn, const guint8 *ip, guint16 port, guint16 requestnumber)
774 {
775 FlapConnection *conn;
776 FlapFrame *frame;
777 aim_snacid_t snacid;
778 aim_tlvlist_t *tl = NULL, *itl = NULL;
779 ByteStream hdrbs;
780
781 conn = flap_connection_findbygroup(od, 0x0004);
782 if (conn == NULL)
783 return;
784
785 frame = flap_frame_new(od, 0x02, 256+strlen(sn));
786
787 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, NULL, 0);
788 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
789
790 /* ICBM header */
791 aim_im_puticbm(&frame->data, cookie, 0x0002, sn);
792
793 aim_tlvlist_add_noval(&tl, 0x0003);
794
795 byte_stream_new(&hdrbs, 128);
796
797 byte_stream_put16(&hdrbs, AIM_RENDEZVOUS_PROPOSE);
798 byte_stream_putraw(&hdrbs, cookie, 8);
799 byte_stream_putcaps(&hdrbs, OSCAR_CAPABILITY_DIRECTIM);
800
801 aim_tlvlist_add_raw(&itl, 0x0002, 4, ip);
802 aim_tlvlist_add_raw(&itl, 0x0003, 4, ip);
803 aim_tlvlist_add_16(&itl, 0x0005, port);
804 aim_tlvlist_add_16(&itl, 0x000a, requestnumber);
805 aim_tlvlist_add_noval(&itl, 0x000f);
806 aim_tlvlist_write(&hdrbs, &itl);
807
808 aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
809 g_free(hdrbs.data);
810
811 aim_tlvlist_write(&frame->data, &tl);
812
813 aim_tlvlist_free(&itl);
814 aim_tlvlist_free(&tl);
815
816 flap_connection_send(conn, frame);
817 }
818
819 /**
820 * Subtype 0x0006 - Send a direct connect rendezvous ICBM asking the
821 * remote user to connect to us via a proxy server.
822 */
823 void
824 aim_im_sendch2_odc_requestproxy(OscarData *od, guchar *cookie, const char *sn, const guint8 *ip, guint16 pin, guint16 requestnumber)
825 {
826 FlapConnection *conn;
827 FlapFrame *frame;
828 aim_snacid_t snacid;
829 aim_tlvlist_t *tl = NULL, *itl = NULL;
830 ByteStream hdrbs;
831 guint8 ip_comp[4];
832
833 conn = flap_connection_findbygroup(od, 0x0004);
834 if (conn == NULL)
835 return;
836
837 frame = flap_frame_new(od, 0x02, 256+strlen(sn));
838
839 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, NULL, 0);
840 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
841
842 /* ICBM header */
843 aim_im_puticbm(&frame->data, cookie, 0x0002, sn);
844
845 aim_tlvlist_add_noval(&tl, 0x0003);
846
847 byte_stream_new(&hdrbs, 128);
848
849 byte_stream_put16(&hdrbs, AIM_RENDEZVOUS_PROPOSE);
850 byte_stream_putraw(&hdrbs, cookie, 8);
851 byte_stream_putcaps(&hdrbs, OSCAR_CAPABILITY_DIRECTIM);
852
853 aim_tlvlist_add_raw(&itl, 0x0002, 4, ip);
854 aim_tlvlist_add_raw(&itl, 0x0003, 4, ip);
855 aim_tlvlist_add_16(&itl, 0x0005, pin);
856 aim_tlvlist_add_16(&itl, 0x000a, requestnumber);
857 aim_tlvlist_add_noval(&itl, 0x000f);
858 aim_tlvlist_add_noval(&itl, 0x0010);
859
860 /* Send the bitwise complement of the port and ip. As a check? */
861 ip_comp[0] = ~ip[0];
862 ip_comp[1] = ~ip[1];
863 ip_comp[2] = ~ip[2];
864 ip_comp[3] = ~ip[3];
865 aim_tlvlist_add_raw(&itl, 0x0016, 4, ip_comp);
866 aim_tlvlist_add_16(&itl, 0x0017, ~pin);
867
868 aim_tlvlist_write(&hdrbs, &itl);
869
870 aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
871 g_free(hdrbs.data);
872
873 aim_tlvlist_write(&frame->data, &tl);
874
875 aim_tlvlist_free(&itl);
876 aim_tlvlist_free(&tl);
877
878 flap_connection_send(conn, frame);
879 }
880
881 /**
882 * Subtype 0x0006 - Send an "I want to send you this file" message
883 *
884 */
885 void
886 aim_im_sendch2_sendfile_requestdirect(OscarData *od, guchar *cookie, const char *sn, const guint8 *ip, guint16 port, guint16 requestnumber, const gchar *filename, guint32 size, guint16 numfiles)
887 {
888 FlapConnection *conn;
889 FlapFrame *frame;
890 aim_snacid_t snacid;
891 aim_tlvlist_t *tl = NULL, *itl = NULL;
892 ByteStream hdrbs;
893
894 conn = flap_connection_findbygroup(od, 0x0004);
895 if (conn == NULL)
896 return;
897
898 frame = flap_frame_new(od, 0x02, 1024);
899
900 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, NULL, 0);
901 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
902
903 /* ICBM header */
904 aim_im_puticbm(&frame->data, cookie, 0x0002, sn);
905
906 aim_tlvlist_add_noval(&tl, 0x0003);
907
908 byte_stream_new(&hdrbs, 512);
909
910 byte_stream_put16(&hdrbs, AIM_RENDEZVOUS_PROPOSE);
911 byte_stream_putraw(&hdrbs, cookie, 8);
912 byte_stream_putcaps(&hdrbs, OSCAR_CAPABILITY_SENDFILE);
913
914 aim_tlvlist_add_raw(&itl, 0x0002, 4, ip);
915 aim_tlvlist_add_raw(&itl, 0x0003, 4, ip);
916 aim_tlvlist_add_16(&itl, 0x0005, port);
917 aim_tlvlist_add_16(&itl, 0x000a, requestnumber);
918 aim_tlvlist_add_noval(&itl, 0x000f);
919 /* TODO: Send 0x0016 and 0x0017 */
920
921 #if 0
922 /* TODO: If the following is ever enabled, ensure that it is
923 * not sent with a receive redirect or stage 3 proxy
924 * redirect for a file receive (same conditions for
925 * sending 0x000f above)
926 */
927 aim_tlvlist_add_raw(&itl, 0x000e, 2, "en");
928 aim_tlvlist_add_raw(&itl, 0x000d, 8, "us-ascii");
929 aim_tlvlist_add_raw(&itl, 0x000c, 24, "Please accept this file.");
930 #endif
931
932 if (filename != NULL)
933 {
934 ByteStream bs;
935
936 /* Begin TLV t(2711) */
937 byte_stream_new(&bs, 2+2+4+strlen(filename)+1);
938 byte_stream_put16(&bs, (numfiles > 1) ? 0x0002 : 0x0001);
939 byte_stream_put16(&bs, numfiles);
940 byte_stream_put32(&bs, size);
941
942 /* Filename - NULL terminated, for some odd reason */
943 byte_stream_putstr(&bs, filename);
944 byte_stream_put8(&bs, 0x00);
945
946 aim_tlvlist_add_raw(&itl, 0x2711, bs.len, bs.data);
947 g_free(bs.data);
948 /* End TLV t(2711) */
949 }
950
951 aim_tlvlist_write(&hdrbs, &itl);
952 aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
953 g_free(hdrbs.data);
954
955 aim_tlvlist_write(&frame->data, &tl);
956
957 aim_tlvlist_free(&itl);
958 aim_tlvlist_free(&tl);
959
960 flap_connection_send(conn, frame);
961 }
962
963 /**
964 * Subtype 0x0006 - Send a sendfile connect rendezvous ICBM asking the
965 * remote user to connect to us via a proxy server.
966 */
967 void
968 aim_im_sendch2_sendfile_requestproxy(OscarData *od, guchar *cookie, const char *sn, const guint8 *ip, guint16 pin, guint16 requestnumber, const gchar *filename, guint32 size, guint16 numfiles)
969 {
970 FlapConnection *conn;
971 FlapFrame *frame;
972 aim_snacid_t snacid;
973 aim_tlvlist_t *tl = NULL, *itl = NULL;
974 ByteStream hdrbs;
975 guint8 ip_comp[4];
976
977 conn = flap_connection_findbygroup(od, 0x0004);
978 if (conn == NULL)
979 return;
980
981 frame = flap_frame_new(od, 0x02, 1024);
982
983 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, NULL, 0);
984 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
985
986 /* ICBM header */
987 aim_im_puticbm(&frame->data, cookie, 0x0002, sn);
988
989 aim_tlvlist_add_noval(&tl, 0x0003);
990
991 byte_stream_new(&hdrbs, 512);
992
993 byte_stream_put16(&hdrbs, AIM_RENDEZVOUS_PROPOSE);
994 byte_stream_putraw(&hdrbs, cookie, 8);
995 byte_stream_putcaps(&hdrbs, OSCAR_CAPABILITY_SENDFILE);
996
997 aim_tlvlist_add_raw(&itl, 0x0002, 4, ip);
998 aim_tlvlist_add_raw(&itl, 0x0003, 4, ip);
999 aim_tlvlist_add_16(&itl, 0x0005, pin);
1000 aim_tlvlist_add_16(&itl, 0x000a, requestnumber);
1001 aim_tlvlist_add_noval(&itl, 0x000f);
1002 aim_tlvlist_add_noval(&itl, 0x0010);
1003
1004 /* Send the bitwise complement of the port and ip. As a check? */
1005 ip_comp[0] = ~ip[0];
1006 ip_comp[1] = ~ip[1];
1007 ip_comp[2] = ~ip[2];
1008 ip_comp[3] = ~ip[3];
1009 aim_tlvlist_add_raw(&itl, 0x0016, 4, ip_comp);
1010 aim_tlvlist_add_16(&itl, 0x0017, ~pin);
1011
1012 #if 0
1013 /* TODO: If the following is ever enabled, ensure that it is
1014 * not sent with a receive redirect or stage 3 proxy
1015 * redirect for a file receive (same conditions for
1016 * sending 0x000f above)
1017 */
1018 aim_tlvlist_add_raw(&itl, 0x000e, 2, "en");
1019 aim_tlvlist_add_raw(&itl, 0x000d, 8, "us-ascii");
1020 aim_tlvlist_add_raw(&itl, 0x000c, 24, "Please accept this file.");
1021 #endif
1022
1023 if (filename != NULL)
1024 {
1025 ByteStream bs;
1026
1027 /* Begin TLV t(2711) */
1028 byte_stream_new(&bs, 2+2+4+strlen(filename)+1);
1029 byte_stream_put16(&bs, (numfiles > 1) ? 0x0002 : 0x0001);
1030 byte_stream_put16(&bs, numfiles);
1031 byte_stream_put32(&bs, size);
1032
1033 /* Filename - NULL terminated, for some odd reason */
1034 byte_stream_putstr(&bs, filename);
1035 byte_stream_put8(&bs, 0x00);
1036
1037 aim_tlvlist_add_raw(&itl, 0x2711, bs.len, bs.data);
1038 g_free(bs.data);
1039 /* End TLV t(2711) */
1040 }
1041
1042 aim_tlvlist_write(&hdrbs, &itl);
1043
1044 aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
1045 g_free(hdrbs.data);
1046
1047 aim_tlvlist_write(&frame->data, &tl);
1048
1049 aim_tlvlist_free(&itl);
1050 aim_tlvlist_free(&tl);
1051
1052 flap_connection_send(conn, frame);
1053 }
1054
1055 /**
1056 * Subtype 0x0006 - Request the status message of the given ICQ user.
1057 *
1058 * @param od The oscar session.
1059 * @param sn The UIN of the user of whom you wish to request info.
1060 * @param type The type of info you wish to request. This should be the current
1061 * state of the user, as one of the AIM_ICQ_STATE_* defines.
1062 * @return Return 0 if no errors, otherwise return the error number.
1063 */
1064 int aim_im_sendch2_geticqaway(OscarData *od, const char *sn, int type)
1065 {
1066 FlapConnection *conn;
1067 FlapFrame *frame;
1068 aim_snacid_t snacid;
1069 guchar cookie[8];
1070
1071 if (!od || !(conn = flap_connection_findbygroup(od, 0x0004)) || !sn)
1072 return -EINVAL;
1073
1074 aim_icbm_makecookie(cookie);
1075
1076 frame = flap_frame_new(od, 0x02, 10+8+2+1+strlen(sn) + 4+0x5e + 4);
1077
1078 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, NULL, 0);
1079 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
1080
1081 /* ICBM header */
1082 aim_im_puticbm(&frame->data, cookie, 0x0002, sn);
1083
1084 /* TLV t(0005) - Encompasses almost everything below. */
1085 byte_stream_put16(&frame->data, 0x0005); /* T */
1086 byte_stream_put16(&frame->data, 0x005e); /* L */
1087 { /* V */
1088 byte_stream_put16(&frame->data, 0x0000);
1089
1090 /* Cookie */
1091 byte_stream_putraw(&frame->data, cookie, 8);
1092
1093 /* Put the 16 byte server relay capability */
1094 byte_stream_putcaps(&frame->data, OSCAR_CAPABILITY_ICQSERVERRELAY);
1095
1096 /* TLV t(000a) */
1097 byte_stream_put16(&frame->data, 0x000a);
1098 byte_stream_put16(&frame->data, 0x0002);
1099 byte_stream_put16(&frame->data, 0x0001);
1100
1101 /* TLV t(000f) */
1102 byte_stream_put16(&frame->data, 0x000f);
1103 byte_stream_put16(&frame->data, 0x0000);
1104
1105 /* TLV t(2711) */
1106 byte_stream_put16(&frame->data, 0x2711);
1107 byte_stream_put16(&frame->data, 0x0036);
1108 { /* V */
1109 byte_stream_putle16(&frame->data, 0x001b); /* L */
1110 byte_stream_putle16(&frame->data, 0x0009); /* Protocol version */
1111 byte_stream_putcaps(&frame->data, OSCAR_CAPABILITY_EMPTY);
1112 byte_stream_putle16(&frame->data, 0x0000); /* Unknown */
1113 byte_stream_putle16(&frame->data, 0x0001); /* Client features? */
1114 byte_stream_putle16(&frame->data, 0x0000); /* Unknown */
1115 byte_stream_putle8(&frame->data, 0x00); /* Unkizown */
1116 byte_stream_putle16(&frame->data, 0xffff); /* Sequence number? XXX - This should decrement by 1 with each request */
1117
1118 byte_stream_putle16(&frame->data, 0x000e); /* L */
1119 byte_stream_putle16(&frame->data, 0xffff); /* Sequence number? XXX - This should decrement by 1 with each request */
1120 byte_stream_putle32(&frame->data, 0x00000000); /* Unknown */
1121 byte_stream_putle32(&frame->data, 0x00000000); /* Unknown */
1122 byte_stream_putle32(&frame->data, 0x00000000); /* Unknown */
1123
1124 /* The type of status message being requested */
1125 if (type & AIM_ICQ_STATE_CHAT)
1126 byte_stream_putle16(&frame->data, 0x03ec);
1127 else if(type & AIM_ICQ_STATE_DND)
1128 byte_stream_putle16(&frame->data, 0x03eb);
1129 else if(type & AIM_ICQ_STATE_OUT)
1130 byte_stream_putle16(&frame->data, 0x03ea);
1131 else if(type & AIM_ICQ_STATE_BUSY)
1132 byte_stream_putle16(&frame->data, 0x03e9);
1133 else if(type & AIM_ICQ_STATE_AWAY)
1134 byte_stream_putle16(&frame->data, 0x03e8);
1135
1136 byte_stream_putle16(&frame->data, 0x0001); /* Status? */
1137 byte_stream_putle16(&frame->data, 0x0001); /* Priority of this message? */
1138 byte_stream_putle16(&frame->data, 0x0001); /* L */
1139 byte_stream_putle8(&frame->data, 0x00); /* String of length L */
1140 } /* End TLV t(2711) */
1141 } /* End TLV t(0005) */
1142
1143 /* TLV t(0003) */
1144 byte_stream_put16(&frame->data, 0x0003);
1145 byte_stream_put16(&frame->data, 0x0000);
1146
1147 flap_connection_send(conn, frame);
1148
1149 return 0;
1150 }
1151
1152 /**
1153 * Subtype 0x0006 - Send an ICQ-esque ICBM.
1154 *
1155 * This can be used to send an ICQ authorization reply (deny or grant). It is the "old way."
1156 * The new way is to use SSI. I like the new way a lot better. This seems like such a hack,
1157 * mostly because it's in network byte order. Figuring this stuff out sometimes takes a while,
1158 * but thats ok, because it gives me time to try to figure out what kind of drugs the AOL people
1159 * were taking when they merged the two protocols.
1160 *
1161 * @param sn The destination screen name.
1162 * @param type The type of message. 0x0007 for authorization denied. 0x0008 for authorization granted.
1163 * @param message The message you want to send, it should be null terminated.
1164 * @return Return 0 if no errors, otherwise return the error number.
1165 */
1166 int aim_im_sendch4(OscarData *od, const char *sn, guint16 type, const char *message)
1167 {
1168 FlapConnection *conn;
1169 FlapFrame *frame;
1170 aim_snacid_t snacid;
1171 guchar cookie[8];
1172
1173 if (!od || !(conn = flap_connection_findbygroup(od, 0x0002)))
1174 return -EINVAL;
1175
1176 if (!sn || !type || !message)
1177 return -EINVAL;
1178
1179 frame = flap_frame_new(od, 0x02, 10+8+3+strlen(sn)+12+strlen(message)+1+4);
1180
1181 snacid = aim_cachesnac(od, 0x0004, 0x0006, 0x0000, NULL, 0);
1182 aim_putsnac(&frame->data, 0x0004, 0x0006, 0x0000, snacid);
1183
1184 aim_icbm_makecookie(cookie);
1185
1186 /* ICBM header */
1187 aim_im_puticbm(&frame->data, cookie, 0x0004, sn);
1188
1189 /*
1190 * TLV t(0005)
1191 *
1192 * ICQ data (the UIN and the message).
1193 */
1194 byte_stream_put16(&frame->data, 0x0005);
1195 byte_stream_put16(&frame->data, 4 + 2+2+strlen(message)+1);
1196
1197 /*
1198 * Your UIN
1199 */
1200 byte_stream_putle32(&frame->data, atoi(od->sn));
1201
1202 /*
1203 * TLV t(type) l(strlen(message)+1) v(message+NULL)
1204 */
1205 byte_stream_putle16(&frame->data, type);
1206 byte_stream_putle16(&frame->data, strlen(message)+1);
1207 byte_stream_putraw(&frame->data, (const guint8 *)message, strlen(message)+1);
1208
1209 /*
1210 * TLV t(0006) l(0000) v()
1211 */
1212 byte_stream_put16(&frame->data, 0x0006);
1213 byte_stream_put16(&frame->data, 0x0000);
1214
1215 flap_connection_send(conn, frame);
1216
1217 return 0;
1218 }
1219
1220 /*
1221 * XXX - I don't see when this would ever get called...
1222 */
1223 static int outgoingim(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
1224 {
1225 int ret = 0;
1226 aim_rxcallback_t userfunc;
1227 guchar cookie[8];
1228 guint16 channel;
1229 aim_tlvlist_t *tlvlist;
1230 char *sn;
1231 int snlen;
1232 guint16 icbmflags = 0;
1233 guint8 flag1 = 0, flag2 = 0;
1234 gchar *msg = NULL;
1235 aim_tlv_t *msgblock;
1236
1237 /* ICBM Cookie. */
1238 aim_icbm_makecookie(cookie);
1239
1240 /* Channel ID */
1241 channel = byte_stream_get16(bs);
1242
1243 if (channel != 0x01) {
1244 gaim_debug_misc("oscar", "icbm: ICBM recieved on unsupported channel. Ignoring. (chan = %04x)\n", channel);
1245 return 0;
1246 }
1247
1248 snlen = byte_stream_get8(bs);
1249 sn = byte_stream_getstr(bs, snlen);
1250
1251 tlvlist = aim_tlvlist_read(bs);
1252
1253 if (aim_tlv_gettlv(tlvlist, 0x0003, 1))
1254 icbmflags |= AIM_IMFLAGS_ACK;
1255 if (aim_tlv_gettlv(tlvlist, 0x0004, 1))
1256 icbmflags |= AIM_IMFLAGS_AWAY;
1257
1258 if ((msgblock = aim_tlv_gettlv(tlvlist, 0x0002, 1))) {
1259 ByteStream mbs;
1260 int featurelen, msglen;
1261
1262 byte_stream_init(&mbs, msgblock->value, msgblock->length);
1263
1264 byte_stream_get8(&mbs);
1265 byte_stream_get8(&mbs);
1266 for (featurelen = byte_stream_get16(&mbs); featurelen; featurelen--)
1267 byte_stream_get8(&mbs);
1268 byte_stream_get8(&mbs);
1269 byte_stream_get8(&mbs);
1270
1271 msglen = byte_stream_get16(&mbs) - 4; /* final block length */
1272
1273 flag1 = byte_stream_get16(&mbs);
1274 flag2 = byte_stream_get16(&mbs);
1275
1276 msg = byte_stream_getstr(&mbs, msglen);
1277 }
1278
1279 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
1280 ret = userfunc(od, conn, frame, channel, sn, msg, icbmflags, flag1, flag2);
1281
1282 free(sn);
1283 free(msg);
1284 aim_tlvlist_free(&tlvlist);
1285
1286 return ret;
1287 }
1288
1289 /*
1290 * Ahh, the joys of nearly ridiculous over-engineering.
1291 *
1292 * Not only do AIM ICBM's support multiple channels. Not only do they
1293 * support multiple character sets. But they support multiple character
1294 * sets / encodings within the same ICBM.
1295 *
1296 * These multipart messages allow for complex space savings techniques, which
1297 * seem utterly unnecessary by today's standards. In fact, there is only
1298 * one client still in popular use that still uses this method: AOL for the
1299 * Macintosh, Version 5.0. Obscure, yes, I know.
1300 *
1301 * In modern (non-"legacy") clients, if the user tries to send a character
1302 * that is not ISO-8859-1 or ASCII, the client will send the entire message
1303 * as UNICODE, meaning that every character in the message will occupy the
1304 * full 16 bit UNICODE field, even if the high order byte would be zero.
1305 * Multipart messages prevent this wasted space by allowing the client to
1306 * only send the characters in UNICODE that need to be sent that way, and
1307 * the rest of the message can be sent in whatever the native character
1308 * set is (probably ASCII).
1309 *
1310 * An important note is that sections will be displayed in the order that
1311 * they appear in the ICBM. There is no facility for merging or rearranging
1312 * sections at run time. So if you have, say, ASCII then UNICODE then ASCII,
1313 * you must supply two ASCII sections with a UNICODE in the middle, and incur
1314 * the associated overhead.
1315 *
1316 * Normally I would have laughed and given a firm 'no' to supporting this
1317 * seldom-used feature, but something is attracting me to it. In the future,
1318 * it may be possible to abuse this to send mixed-media messages to other
1319 * open source clients (like encryption or something) -- see faimtest for
1320 * examples of how to do this.
1321 *
1322 * I would definitely recommend avoiding this feature unless you really
1323 * know what you are doing, and/or you have something neat to do with it.
1324 *
1325 */
1326 int aim_mpmsg_init(OscarData *od, aim_mpmsg_t *mpm)
1327 {
1328
1329 memset(mpm, 0, sizeof(aim_mpmsg_t));
1330
1331 return 0;
1332 }
1333
1334 static int mpmsg_addsection(OscarData *od, aim_mpmsg_t *mpm, guint16 charset, guint16 charsubset, gchar *data, guint16 datalen)
1335 {
1336 aim_mpmsg_section_t *sec;
1337
1338 sec = malloc(sizeof(aim_mpmsg_section_t));
1339
1340 sec->charset = charset;
1341 sec->charsubset = charsubset;
1342 sec->data = data;
1343 sec->datalen = datalen;
1344 sec->next = NULL;
1345
1346 if (!mpm->parts)
1347 mpm->parts = sec;
1348 else {
1349 aim_mpmsg_section_t *cur;
1350
1351 for (cur = mpm->parts; cur->next; cur = cur->next)
1352 ;
1353 cur->next = sec;
1354 }
1355
1356 mpm->numparts++;
1357
1358 return 0;
1359 }
1360
1361 int aim_mpmsg_addraw(OscarData *od, aim_mpmsg_t *mpm, guint16 charset, guint16 charsubset, const gchar *data, guint16 datalen)
1362 {
1363 gchar *dup;
1364
1365 dup = malloc(datalen);
1366 memcpy(dup, data, datalen);
1367
1368 if (mpmsg_addsection(od, mpm, charset, charsubset, dup, datalen) == -1) {
1369 free(dup);
1370 return -1;
1371 }
1372
1373 return 0;
1374 }
1375
1376 /* XXX - should provide a way of saying ISO-8859-1 specifically */
1377 int aim_mpmsg_addascii(OscarData *od, aim_mpmsg_t *mpm, const char *ascii)
1378 {
1379 gchar *dup;
1380
1381 if (!(dup = strdup(ascii)))
1382 return -1;
1383
1384 if (mpmsg_addsection(od, mpm, 0x0000, 0x0000, dup, strlen(ascii)) == -1) {
1385 free(dup);
1386 return -1;
1387 }
1388
1389 return 0;
1390 }
1391
1392 int aim_mpmsg_addunicode(OscarData *od, aim_mpmsg_t *mpm, const guint16 *unicode, guint16 unicodelen)
1393 {
1394 gchar *buf;
1395 ByteStream bs;
1396 int i;
1397
1398 buf = malloc(unicodelen * 2);
1399
1400 byte_stream_init(&bs, (guchar *)buf, unicodelen * 2);
1401
1402 /* We assume unicode is in /host/ byte order -- convert to network */
1403 for (i = 0; i < unicodelen; i++)
1404 byte_stream_put16(&bs, unicode[i]);
1405
1406 if (mpmsg_addsection(od, mpm, 0x0002, 0x0000, buf, byte_stream_curpos(&bs)) == -1) {
1407 free(buf);
1408 return -1;
1409 }
1410
1411 return 0;
1412 }
1413
1414 void aim_mpmsg_free(OscarData *od, aim_mpmsg_t *mpm)
1415 {
1416 aim_mpmsg_section_t *cur;
1417
1418 for (cur = mpm->parts; cur; ) {
1419 aim_mpmsg_section_t *tmp;
1420
1421 tmp = cur->next;
1422 free(cur->data);
1423 free(cur);
1424 cur = tmp;
1425 }
1426
1427 mpm->numparts = 0;
1428 mpm->parts = NULL;
1429
1430 return;
1431 }
1432
1433 /*
1434 * Start by building the multipart structures, then pick the first
1435 * human-readable section and stuff it into args->msg so no one gets
1436 * suspicious.
1437 */
1438 static int incomingim_ch1_parsemsgs(OscarData *od, aim_userinfo_t *userinfo, guint8 *data, int len, struct aim_incomingim_ch1_args *args)
1439 {
1440 /* Should this be ASCII -> UNICODE -> Custom */
1441 static const guint16 charsetpri[] = {
1442 AIM_CHARSET_ASCII, /* ASCII first */
1443 AIM_CHARSET_CUSTOM, /* then ISO-8859-1 */
1444 AIM_CHARSET_UNICODE, /* UNICODE as last resort */
1445 };
1446 static const int charsetpricount = 3;
1447 int i;
1448 ByteStream mbs;
1449 aim_mpmsg_section_t *sec;
1450
1451 byte_stream_init(&mbs, data, len);
1452
1453 while (byte_stream_empty(&mbs)) {
1454 guint16 msglen, flag1, flag2;
1455 gchar *msgbuf;
1456
1457 byte_stream_get8(&mbs); /* 01 */
1458 byte_stream_get8(&mbs); /* 01 */
1459
1460 /* Message string length, including character set info. */
1461 msglen = byte_stream_get16(&mbs);
1462 if (msglen > byte_stream_empty(&mbs))
1463 {
1464 gaim_debug_misc("oscar", "Received an IM containing an invalid message part from %s. They are probably trying to do something malicious.", userinfo->sn);
1465 break;
1466 }
1467
1468 /* Character set info */
1469 flag1 = byte_stream_get16(&mbs);
1470 flag2 = byte_stream_get16(&mbs);
1471
1472 /* Message. */
1473 msglen -= 4;
1474
1475 /*
1476 * For now, we don't care what the encoding is. Just copy
1477 * it into a multipart struct and deal with it later. However,
1478 * always pad the ending with a NULL. This makes it easier
1479 * to treat ASCII sections as strings. It won't matter for
1480 * UNICODE or binary data, as you should never read past
1481 * the specified data length, which will not include the pad.
1482 *
1483 * XXX - There's an API bug here. For sending, the UNICODE is
1484 * given in host byte order (aim_mpmsg_addunicode), but here
1485 * the received messages are given in network byte order.
1486 *
1487 */
1488 msgbuf = (gchar *)byte_stream_getraw(&mbs, msglen);
1489 mpmsg_addsection(od, &args->mpmsg, flag1, flag2, msgbuf, msglen);
1490
1491 } /* while */
1492
1493 args->icbmflags |= AIM_IMFLAGS_MULTIPART; /* always set */
1494
1495 /*
1496 * Clients that support multiparts should never use args->msg, as it
1497 * will point to an arbitrary section.
1498 *
1499 * Here, we attempt to provide clients that do not support multipart
1500 * messages with something to look at -- hopefully a human-readable
1501 * string. But, failing that, a UNICODE message, or nothing at all.
1502 *
1503 * Which means that even if args->msg is NULL, it does not mean the
1504 * message was blank.
1505 *
1506 */
1507 for (i = 0; i < charsetpricount; i++) {
1508 for (sec = args->mpmsg.parts; sec; sec = sec->next) {
1509
1510 if (sec->charset != charsetpri[i])
1511 continue;
1512
1513 /* Great. We found one. Fill it in. */
1514 args->charset = sec->charset;
1515 args->charsubset = sec->charsubset;
1516
1517 /* Set up the simple flags */
1518 switch (args->charsubset)
1519 {
1520 case 0x0000:
1521 /* standard subencoding? */
1522 break;
1523 case 0x000b:
1524 args->icbmflags |= AIM_IMFLAGS_SUBENC_MACINTOSH;
1525 break;
1526 case 0xffff:
1527 /* no subencoding */
1528 break;
1529 default:
1530 break;
1531 }
1532
1533 args->msg = sec->data;
1534 args->msglen = sec->datalen;
1535
1536 return 0;
1537 }
1538 }
1539
1540 /* No human-readable sections found. Oh well. */
1541 args->charset = args->charsubset = 0xffff;
1542 args->msg = NULL;
1543 args->msglen = 0;
1544
1545 return 0;
1546 }
1547
1548 static int incomingim_ch1(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, guint16 channel, aim_userinfo_t *userinfo, ByteStream *bs, guint8 *cookie)
1549 {
1550 guint16 type, length;
1551 aim_rxcallback_t userfunc;
1552 int ret = 0;
1553 struct aim_incomingim_ch1_args args;
1554 unsigned int endpos;
1555
1556 memset(&args, 0, sizeof(args));
1557
1558 aim_mpmsg_init(od, &args.mpmsg);
1559
1560 /*
1561 * This used to be done using tlvchains. For performance reasons,
1562 * I've changed it to process the TLVs in-place. This avoids lots
1563 * of per-IM memory allocations.
1564 */
1565 while (byte_stream_empty(bs) >= 4)
1566 {
1567 type = byte_stream_get16(bs);
1568 length = byte_stream_get16(bs);
1569
1570 if (length > byte_stream_empty(bs))
1571 {
1572 gaim_debug_misc("oscar", "Received an IM containing an invalid message part from %s. They are probably trying to do something malicious.\n", userinfo->sn);
1573 break;
1574 }
1575
1576 endpos = byte_stream_curpos(bs) + length;
1577
1578 if (type == 0x0002) { /* Message Block */
1579
1580 /*
1581 * This TLV consists of the following:
1582 * - 0501 -- Unknown
1583 * - Features: Don't know how to interpret these
1584 * - 0101 -- Unknown
1585 * - Message
1586 *
1587 */
1588
1589 byte_stream_get8(bs); /* 05 */
1590 byte_stream_get8(bs); /* 01 */
1591
1592 args.featureslen = byte_stream_get16(bs);
1593 if (args.featureslen > byte_stream_empty(bs))
1594 {
1595 gaim_debug_misc("oscar", "Received an IM containing an invalid message part from %s. They are probably trying to do something malicious.\n", userinfo->sn);
1596 break;
1597 }
1598 if (args.featureslen == 0)
1599 {
1600 args.features = NULL;
1601 }
1602 else
1603 {
1604 args.features = byte_stream_getraw(bs, args.featureslen);
1605 args.icbmflags |= AIM_IMFLAGS_CUSTOMFEATURES;
1606 }
1607
1608 /*
1609 * The rest of the TLV contains one or more message
1610 * blocks...
1611 */
1612 incomingim_ch1_parsemsgs(od, userinfo, bs->data + bs->offset /* XXX evil!!! */, length - 2 - 2 - args.featureslen, &args);
1613
1614 } else if (type == 0x0003) { /* Server Ack Requested */
1615
1616 args.icbmflags |= AIM_IMFLAGS_ACK;
1617
1618 } else if (type == 0x0004) { /* Message is Auto Response */
1619
1620 args.icbmflags |= AIM_IMFLAGS_AWAY;
1621
1622 } else if (type == 0x0006) { /* Message was received offline. */
1623
1624 /* XXX - not sure if this actually gets sent. */
1625 args.icbmflags |= AIM_IMFLAGS_OFFLINE;
1626
1627 } else if (type == 0x0008) { /* I-HAVE-A-REALLY-PURTY-ICON Flag */
1628
1629 args.iconlen = byte_stream_get32(bs);
1630 byte_stream_get16(bs); /* 0x0001 */
1631 args.iconsum = byte_stream_get16(bs);
1632 args.iconstamp = byte_stream_get32(bs);
1633
1634 /*
1635 * This looks to be a client bug. MacAIM 4.3 will
1636 * send this tag, but with all zero values, in the
1637 * first message of a conversation. This makes no
1638 * sense whatsoever, so I'm going to say its a bug.
1639 *
1640 * You really shouldn't advertise a zero-length icon
1641 * anyway.
1642 *
1643 */
1644 if (args.iconlen)
1645 args.icbmflags |= AIM_IMFLAGS_HASICON;
1646
1647 } else if (type == 0x0009) {
1648
1649 args.icbmflags |= AIM_IMFLAGS_BUDDYREQ;
1650
1651 } else if (type == 0x000b) { /* Non-direct connect typing notification */
1652
1653 args.icbmflags |= AIM_IMFLAGS_TYPINGNOT;
1654
1655 } else if (type == 0x0017) {
1656
1657 if (length > byte_stream_empty(bs))
1658 {
1659 gaim_debug_misc("oscar", "Received an IM containing an invalid message part from %s. They are probably trying to do something malicious.\n", userinfo->sn);
1660 break;
1661 }
1662 free(args.extdata);
1663 args.extdatalen = length;
1664 if (args.extdatalen == 0)
1665 args.extdata = NULL;
1666 else
1667 args.extdata = byte_stream_getraw(bs, args.extdatalen);
1668
1669 } else {
1670 gaim_debug_misc("oscar", "incomingim_ch1: unknown TLV 0x%04x (len %d)\n", type, length);
1671 }
1672
1673 /*
1674 * This is here to protect ourselves from ourselves. That
1675 * is, if something above doesn't completely parse its value
1676 * section, or, worse, overparses it, this will set the
1677 * stream where it needs to be in order to land on the next
1678 * TLV when the loop continues.
1679 *
1680 */
1681 byte_stream_setpos(bs, endpos);
1682 }
1683
1684
1685 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
1686 ret = userfunc(od, conn, frame, channel, userinfo, &args);
1687
1688 aim_mpmsg_free(od, &args.mpmsg);
1689 free(args.features);
1690 free(args.extdata);
1691
1692 return ret;
1693 }
1694
1695 static void
1696 incomingim_ch2_buddylist(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, aim_userinfo_t *userinfo, IcbmArgsCh2 *args, ByteStream *servdata)
1697 {
1698 /*
1699 * This goes like this...
1700 *
1701 * group name length
1702 * group name
1703 * num of buddies in group
1704 * buddy name length
1705 * buddy name
1706 * buddy name length
1707 * buddy name
1708 * ...
1709 * group name length
1710 * group name
1711 * num of buddies in group
1712 * buddy name length
1713 * buddy name
1714 * ...
1715 * ...
1716 */
1717 while (byte_stream_empty(servdata))
1718 {
1719 guint16 gnlen, numb;
1720 int i;
1721 char *gn;
1722
1723 gnlen = byte_stream_get16(servdata);
1724 gn = byte_stream_getstr(servdata, gnlen);
1725 numb = byte_stream_get16(servdata);
1726
1727 for (i = 0; i < numb; i++) {
1728 guint16 bnlen;
1729 char *bn;
1730
1731 bnlen = byte_stream_get16(servdata);
1732 bn = byte_stream_getstr(servdata, bnlen);
1733
1734 gaim_debug_misc("oscar", "got a buddy list from %s: group %s, buddy %s\n", userinfo->sn, gn, bn);
1735
1736 free(bn);
1737 }
1738
1739 free(gn);
1740 }
1741
1742 return;
1743 }
1744
1745 static void
1746 incomingim_ch2_buddyicon_free(OscarData *od, IcbmArgsCh2 *args)
1747 {
1748 free(args->info.icon.icon);
1749
1750 return;
1751 }
1752
1753 static void
1754 incomingim_ch2_buddyicon(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, aim_userinfo_t *userinfo, IcbmArgsCh2 *args, ByteStream *servdata)
1755 {
1756 args->info.icon.checksum = byte_stream_get32(servdata);
1757 args->info.icon.length = byte_stream_get32(servdata);
1758 args->info.icon.timestamp = byte_stream_get32(servdata);
1759 args->info.icon.icon = byte_stream_getraw(servdata, args->info.icon.length);
1760
1761 args->destructor = (void *)incomingim_ch2_buddyicon_free;
1762
1763 return;
1764 }
1765
1766 static void
1767 incomingim_ch2_chat_free(OscarData *od, IcbmArgsCh2 *args)
1768 {
1769 /* XXX - aim_chat_roominfo_free() */
1770 free(args->info.chat.roominfo.name);
1771
1772 return;
1773 }
1774
1775 static void
1776 incomingim_ch2_chat(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, aim_userinfo_t *userinfo, IcbmArgsCh2 *args, ByteStream *servdata)
1777 {
1778 /*
1779 * Chat room info.
1780 */
1781 aim_chat_readroominfo(servdata, &args->info.chat.roominfo);
1782
1783 args->destructor = (void *)incomingim_ch2_chat_free;
1784 }
1785
1786 static void
1787 incomingim_ch2_icqserverrelay_free(OscarData *od, IcbmArgsCh2 *args)
1788 {
1789 free((char *)args->info.rtfmsg.rtfmsg);
1790 }
1791
1792 /*
1793 * The relationship between OSCAR_CAPABILITY_ICQSERVERRELAY and OSCAR_CAPABILITY_ICQRTF is
1794 * kind of odd. This sends the client ICQRTF since that is all that I've seen
1795 * SERVERRELAY used for.
1796 *
1797 * Note that this is all little-endian. Cringe.
1798 *
1799 */
1800 static void
1801 incomingim_ch2_icqserverrelay(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, aim_userinfo_t *userinfo, IcbmArgsCh2 *args, ByteStream *servdata)
1802 {
1803 guint16 hdrlen, anslen, msglen;
1804
1805 if (byte_stream_empty(servdata) < 24)
1806 /* Someone sent us a short server relay ICBM. Weird. (Maybe?) */
1807 return;
1808
1809 hdrlen = byte_stream_getle16(servdata);
1810 byte_stream_advance(servdata, hdrlen);
1811
1812 hdrlen = byte_stream_getle16(servdata);
1813 byte_stream_advance(servdata, hdrlen);
1814
1815 args->info.rtfmsg.msgtype = byte_stream_getle16(servdata);
1816
1817 anslen = byte_stream_getle32(servdata);
1818 byte_stream_advance(servdata, anslen);
1819
1820 msglen = byte_stream_getle16(servdata);
1821 args->info.rtfmsg.rtfmsg = byte_stream_getstr(servdata, msglen);
1822
1823 args->info.rtfmsg.fgcolor = byte_stream_getle32(servdata);
1824 args->info.rtfmsg.bgcolor = byte_stream_getle32(servdata);
1825
1826 hdrlen = byte_stream_getle32(servdata);
1827 byte_stream_advance(servdata, hdrlen);
1828
1829 args->destructor = (void *)incomingim_ch2_icqserverrelay_free;
1830 }
1831
1832 static void
1833 incomingim_ch2_sendfile_free(OscarData *od, IcbmArgsCh2 *args)
1834 {
1835 free(args->info.sendfile.filename);
1836 }
1837
1838 /* Someone is sending us a file */
1839 static void
1840 incomingim_ch2_sendfile(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, aim_userinfo_t *userinfo, IcbmArgsCh2 *args, ByteStream *servdata)
1841 {
1842 int flen;
1843
1844 args->destructor = (void *)incomingim_ch2_sendfile_free;
1845
1846 /* Maybe there is a better way to tell what kind of sendfile
1847 * this is? Maybe TLV t(000a)? */
1848
1849 /* subtype is one of AIM_OFT_SUBTYPE_* */
1850 args->info.sendfile.subtype = byte_stream_get16(servdata);
1851 args->info.sendfile.totfiles = byte_stream_get16(servdata);
1852 args->info.sendfile.totsize = byte_stream_get32(servdata);
1853
1854 /*
1855 * I hope to God I'm right when I guess that there is a
1856 * 32 char max filename length for single files. I think
1857 * OFT tends to do that. Gotta love inconsistency. I saw
1858 * a 26 byte filename?
1859 */
1860 /* AAA - create an byte_stream_getnullstr function (don't anymore)(maybe) */
1861 /* Use an inelegant way of getting the null-terminated filename,
1862 * since there's no easy bstream routine. */
1863 for (flen = 0; byte_stream_get8(servdata); flen++);
1864 byte_stream_advance(servdata, -flen -1);
1865 args->info.sendfile.filename = byte_stream_getstr(servdata, flen);
1866
1867 /* There is sometimes more after the null-terminated filename,
1868 * but I'm unsure of its format. */
1869 /* I don't believe him. */
1870 /* There is sometimes a null byte inside a unicode filename,
1871 * but as far as I can tell the filename is the last
1872 * piece of data that will be in this message. --Jonathan */
1873 }
1874
1875 typedef void (*ch2_args_destructor_t)(OscarData *od, IcbmArgsCh2 *args);
1876
1877 static int incomingim_ch2(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, guint16 channel, aim_userinfo_t *userinfo, aim_tlvlist_t *tlvlist, guint8 *cookie)
1878 {
1879 aim_rxcallback_t userfunc;
1880 aim_tlv_t *block1, *servdatatlv;
1881 aim_tlvlist_t *list2;
1882 aim_tlv_t *tlv;
1883 IcbmArgsCh2 args;
1884 ByteStream bbs, sdbs, *sdbsptr = NULL;
1885 guint8 *cookie2;
1886 int ret = 0;
1887
1888 char proxyip[30] = {""};
1889 char clientip[30] = {""};
1890 char verifiedip[30] = {""};
1891
1892 memset(&args, 0, sizeof(args));
1893
1894 /*
1895 * There's another block of TLVs embedded in the type 5 here.
1896 */
1897 block1 = aim_tlv_gettlv(tlvlist, 0x0005, 1);
1898 if (block1 == NULL)
1899 {
1900 /* The server sent us ch2 ICBM without ch2 info? Weird. */
1901 return 1;
1902 }
1903 byte_stream_init(&bbs, block1->value, block1->length);
1904
1905 /*
1906 * First two bytes represent the status of the connection.
1907 * One of the AIM_RENDEZVOUS_ defines.
1908 *
1909 * 0 is a request, 1 is a cancel, 2 is an accept
1910 */
1911 args.status = byte_stream_get16(&bbs);
1912
1913 /*
1914 * Next comes the cookie. Should match the ICBM cookie.
1915 */
1916 cookie2 = byte_stream_getraw(&bbs, 8);
1917 if (memcmp(cookie, cookie2, 8) != 0)
1918 {
1919 gaim_debug_warning("oscar",
1920 "Cookies don't match in rendezvous ICBM, bailing out.\n");
1921 free(cookie2);
1922 return 1;
1923 }
1924 memcpy(args.cookie, cookie2, 8);
1925 free(cookie2);
1926
1927 /*
1928 * The next 16bytes are a capability block so we can
1929 * identify what type of rendezvous this is.
1930 */
1931 args.type = aim_locate_getcaps(od, &bbs, 0x10);
1932
1933 /*
1934 * What follows may be TLVs or nothing, depending on the
1935 * purpose of the message.
1936 *
1937 * Ack packets for instance have nothing more to them.
1938 */
1939 list2 = aim_tlvlist_read(&bbs);
1940
1941 /*
1942 * IP address to proxy the file transfer through.
1943 *
1944 * TODO: I don't like this. Maybe just read in an int? Or inet_ntoa...
1945 */
1946 tlv = aim_tlv_gettlv(list2, 0x0002, 1);
1947 if ((tlv != NULL) && (tlv->length == 4))
1948 snprintf(proxyip, sizeof(proxyip), "%hhu.%hhu.%hhu.%hhu",
1949 tlv->value[0], tlv->value[1],
1950 tlv->value[2], tlv->value[3]);
1951
1952 /*
1953 * IP address from the perspective of the client.
1954 */
1955 tlv = aim_tlv_gettlv(list2, 0x0003, 1);
1956 if ((tlv != NULL) && (tlv->length == 4))
1957 snprintf(clientip, sizeof(clientip), "%hhu.%hhu.%hhu.%hhu",
1958 tlv->value[0], tlv->value[1],
1959 tlv->value[2], tlv->value[3]);
1960
1961 /*
1962 * Verified IP address (from the perspective of Oscar).
1963 *
1964 * This is added by the server.
1965 */
1966 tlv = aim_tlv_gettlv(list2, 0x0004, 1);
1967 if ((tlv != NULL) && (tlv->length == 4))
1968 snprintf(verifiedip, sizeof(verifiedip), "%hhu.%hhu.%hhu.%hhu",
1969 tlv->value[0], tlv->value[1],
1970 tlv->value[2], tlv->value[3]);
1971
1972 /*
1973 * Port number for something.
1974 */
1975 if (aim_tlv_gettlv(list2, 0x0005, 1))
1976 args.port = aim_tlv_get16(list2, 0x0005, 1);
1977
1978 /*
1979 * File transfer "request number":
1980 * 0x0001 - Initial file transfer request for no proxy or stage 1 proxy
1981 * 0x0002 - "Reply request" for a stage 2 proxy (receiver wants to use proxy)
1982 * 0x0003 - A third request has been sent; applies only to stage 3 proxied transfers
1983 */
1984 if (aim_tlv_gettlv(list2, 0x000a, 1))
1985 args.requestnumber = aim_tlv_get16(list2, 0x000a, 1);
1986
1987 /*
1988 * Terminate connection/error code. 0x0001 means the other user
1989 * canceled the connection.
1990 */
1991 if (aim_tlv_gettlv(list2, 0x000b, 1))
1992 args.errorcode = aim_tlv_get16(list2, 0x000b, 1);
1993
1994 /*
1995 * Invitation message / chat description.
1996 */
1997 if (aim_tlv_gettlv(list2, 0x000c, 1)) {
1998 args.msg = aim_tlv_getstr(list2, 0x000c, 1);
1999 args.msglen = aim_tlv_getlength(list2, 0x000c, 1);
2000 }
2001
2002 /*
2003 * Character set.
2004 */
2005 if (aim_tlv_gettlv(list2, 0x000d, 1))
2006 args.encoding = aim_tlv_getstr(list2, 0x000d, 1);
2007
2008 /*
2009 * Language.
2010 */
2011 if (aim_tlv_gettlv(list2, 0x000e, 1))
2012 args.language = aim_tlv_getstr(list2, 0x000e, 1);
2013
2014 #if 0
2015 /*
2016 * Unknown -- no value
2017 *
2018 * Maybe means we should connect directly to transfer the file?
2019 * Also used in ICQ Lite Beta 4.0 URLs. Also empty.
2020 */
2021 /* I don't think this indicates a direct transfer; this flag is
2022 * also present in a stage 1 proxied file send request -- Jonathan */
2023 if (aim_tlv_gettlv(list2, 0x000f, 1)) {
2024 /* Unhandled */
2025 }
2026 #endif
2027
2028 /*
2029 * Flag meaning we should proxy the file transfer through an AIM server
2030 */
2031 if (aim_tlv_gettlv(list2, 0x0010, 1))
2032 args.use_proxy = TRUE;
2033
2034 if (strlen(proxyip))
2035 args.proxyip = (char *)proxyip;
2036 if (strlen(clientip))
2037 args.clientip = (char *)clientip;
2038 if (strlen(verifiedip))
2039 args.verifiedip = (char *)verifiedip;
2040
2041 /*
2042 * This must be present in PROPOSALs, but will probably not
2043 * exist in CANCELs and ACCEPTs. Also exists in ICQ Lite
2044 * Beta 4.0 URLs (OSCAR_CAPABILITY_ICQSERVERRELAY).
2045 *
2046 * Service Data blocks are module-specific in format.
2047 */
2048 if ((servdatatlv = aim_tlv_gettlv(list2, 0x2711 /* 10001 */, 1))) {
2049
2050 byte_stream_init(&sdbs, servdatatlv->value, servdatatlv->length);
2051 sdbsptr = &sdbs;
2052
2053 /*
2054 * The rest of the handling depends on what type it is.
2055 *
2056 * Not all of them have special handling (yet).
2057 */
2058 if (args.type & OSCAR_CAPABILITY_BUDDYICON)
2059 incomingim_ch2_buddyicon(od, conn, mod, frame, snac, userinfo, &args, sdbsptr);
2060 else if (args.type & OSCAR_CAPABILITY_SENDBUDDYLIST)
2061 incomingim_ch2_buddylist(od, conn, mod, frame, snac, userinfo, &args, sdbsptr);
2062 else if (args.type & OSCAR_CAPABILITY_CHAT)
2063 incomingim_ch2_chat(od, conn, mod, frame, snac, userinfo, &args, sdbsptr);
2064 else if (args.type & OSCAR_CAPABILITY_ICQSERVERRELAY)
2065 incomingim_ch2_icqserverrelay(od, conn, mod, frame, snac, userinfo, &args, sdbsptr);
2066 else if (args.type & OSCAR_CAPABILITY_SENDFILE)
2067 incomingim_ch2_sendfile(od, conn, mod, frame, snac, userinfo, &args, sdbsptr);
2068 }
2069
2070 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
2071 ret = userfunc(od, conn, frame, channel, userinfo, &args);
2072
2073
2074 if (args.destructor)
2075 ((ch2_args_destructor_t)args.destructor)(od, &args);
2076
2077 free((char *)args.msg);
2078 free((char *)args.encoding);
2079 free((char *)args.language);
2080
2081 aim_tlvlist_free(&list2);
2082
2083 return ret;
2084 }
2085
2086 static int incomingim_ch4(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, guint16 channel, aim_userinfo_t *userinfo, aim_tlvlist_t *tlvlist, guint8 *cookie)
2087 {
2088 ByteStream meat;
2089 aim_rxcallback_t userfunc;
2090 aim_tlv_t *block;
2091 struct aim_incomingim_ch4_args args;
2092 int ret = 0;
2093
2094 /*
2095 * Make a bstream for the meaty part. Yum. Meat.
2096 */
2097 if (!(block = aim_tlv_gettlv(tlvlist, 0x0005, 1)))
2098 return -1;
2099 byte_stream_init(&meat, block->value, block->length);
2100
2101 args.uin = byte_stream_getle32(&meat);
2102 args.type = byte_stream_getle8(&meat);
2103 args.flags = byte_stream_getle8(&meat);
2104 args.msglen = byte_stream_getle16(&meat);
2105 args.msg = (gchar *)byte_stream_getraw(&meat, args.msglen);
2106
2107 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
2108 ret = userfunc(od, conn, frame, channel, userinfo, &args);
2109
2110 free(args.msg);
2111
2112 return ret;
2113 }
2114
2115 /*
2116 * Subtype 0x0007
2117 *
2118 * It can easily be said that parsing ICBMs is THE single
2119 * most difficult thing to do in the in AIM protocol. In
2120 * fact, I think I just did say that.
2121 *
2122 * Below is the best damned solution I've come up with
2123 * over the past sixteen months of battling with it. This
2124 * can parse both away and normal messages from every client
2125 * I have access to. Its not fast, its not clean. But it works.
2126 *
2127 */
2128 static int incomingim(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
2129 {
2130 int ret = 0;
2131 guchar *cookie;
2132 guint16 channel;
2133 aim_userinfo_t userinfo;
2134
2135 memset(&userinfo, 0x00, sizeof(aim_userinfo_t));
2136
2137 /*
2138 * Read ICBM Cookie.
2139 */
2140 cookie = byte_stream_getraw(bs, 8);
2141
2142 /*
2143 * Channel ID.
2144 *
2145 * Channel 0x0001 is the message channel. It is
2146 * used to send basic ICBMs.
2147 *
2148 * Channel 0x0002 is the Rendezvous channel, which
2149 * is where Chat Invitiations and various client-client
2150 * connection negotiations come from.
2151 *
2152 * Channel 0x0003 is used for chat messages.
2153 *
2154 * Channel 0x0004 is used for ICQ authorization, or
2155 * possibly any system notice.
2156 *
2157 */
2158 channel = byte_stream_get16(bs);
2159
2160 /*
2161 * Extract the standard user info block.
2162 *
2163 * Note that although this contains TLVs that appear contiguous
2164 * with the TLVs read below, they are two different pieces. The
2165 * userinfo block contains the number of TLVs that contain user
2166 * information, the rest are not even though there is no separation.
2167 * You can start reading the message TLVs after aim_info_extract()
2168 * parses out the standard userinfo block.
2169 *
2170 * That also means that TLV types can be duplicated between the
2171 * userinfo block and the rest of the message, however there should
2172 * never be two TLVs of the same type in one block.
2173 *
2174 */
2175 aim_info_extract(od, bs, &userinfo);
2176
2177 /*
2178 * From here on, its depends on what channel we're on.
2179 *
2180 * Technically all channels have a TLV list have this, however,
2181 * for the common channel 1 case, in-place parsing is used for
2182 * performance reasons (less memory allocation).
2183 */
2184 if (channel == 1) {
2185
2186 ret = incomingim_ch1(od, conn, mod, frame, snac, channel, &userinfo, bs, cookie);
2187
2188 } else if (channel == 2) {
2189 aim_tlvlist_t *tlvlist;
2190
2191 /*
2192 * Read block of TLVs (not including the userinfo data). All
2193 * further data is derived from what is parsed here.
2194 */
2195 tlvlist = aim_tlvlist_read(bs);
2196
2197 ret = incomingim_ch2(od, conn, mod, frame, snac, channel, &userinfo, tlvlist, cookie);
2198
2199 aim_tlvlist_free(&tlvlist);
2200
2201 } else if (channel == 4) {
2202 aim_tlvlist_t *tlvlist;
2203
2204 tlvlist = aim_tlvlist_read(bs);
2205 ret = incomingim_ch4(od, conn, mod, frame, snac, channel, &userinfo, tlvlist, cookie);
2206 aim_tlvlist_free(&tlvlist);
2207
2208 } else {
2209 gaim_debug_misc("oscar", "icbm: ICBM received on an unsupported channel. Ignoring. (chan = %04x)\n", channel);
2210 }
2211
2212 aim_info_free(&userinfo);
2213 free(cookie);
2214
2215 return ret;
2216 }
2217
2218 /*
2219 * Subtype 0x0008 - Send a warning to sn.
2220 *
2221 * Flags:
2222 * AIM_WARN_ANON Send as an anonymous (doesn't count as much)
2223 *
2224 * returns -1 on error (couldn't alloc packet), 0 on success.
2225 *
2226 */
2227 int aim_im_warn(OscarData *od, FlapConnection *conn, const char *sn, guint32 flags)
2228 {
2229 FlapFrame *frame;
2230 aim_snacid_t snacid;
2231
2232 if (!od || !conn || !sn)
2233 return -EINVAL;
2234
2235 frame = flap_frame_new(od, 0x02, strlen(sn)+13);
2236
2237 snacid = aim_cachesnac(od, 0x0004, 0x0008, 0x0000, sn, strlen(sn)+1);
2238 aim_putsnac(&frame->data, 0x0004, 0x0008, 0x0000, snacid);
2239
2240 byte_stream_put16(&frame->data, (flags & AIM_WARN_ANON) ? 0x0001 : 0x0000);
2241 byte_stream_put8(&frame->data, strlen(sn));
2242 byte_stream_putstr(&frame->data, sn);
2243
2244 flap_connection_send(conn, frame);
2245
2246 return 0;
2247 }
2248
2249 /* Subtype 0x000a */
2250 static int missedcall(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
2251 {
2252 int ret = 0;
2253 aim_rxcallback_t userfunc;
2254 guint16 channel, nummissed, reason;
2255 aim_userinfo_t userinfo;
2256
2257 while (byte_stream_empty(bs)) {
2258
2259 channel = byte_stream_get16(bs);
2260 aim_info_extract(od, bs, &userinfo);
2261 nummissed = byte_stream_get16(bs);
2262 reason = byte_stream_get16(bs);
2263
2264 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
2265 ret = userfunc(od, conn, frame, channel, &userinfo, nummissed, reason);
2266
2267 aim_info_free(&userinfo);
2268 }
2269
2270 return ret;
2271 }
2272
2273 /*
2274 * Subtype 0x000b
2275 *
2276 * Possible codes:
2277 * AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
2278 * AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
2279 * AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
2280 *
2281 */
2282 int aim_im_denytransfer(OscarData *od, const char *sn, const guchar *cookie, guint16 code)
2283 {
2284 FlapConnection *conn;
2285 FlapFrame *frame;
2286 aim_snacid_t snacid;
2287 aim_tlvlist_t *tl = NULL;
2288
2289 if (!od || !(conn = flap_connection_findbygroup(od, 0x0004)))
2290 return -EINVAL;
2291
2292 frame = flap_frame_new(od, 0x02, 10+8+2+1+strlen(sn)+6);
2293
2294 snacid = aim_cachesnac(od, 0x0004, 0x000b, 0x0000, NULL, 0);
2295 aim_putsnac(&frame->data, 0x0004, 0x000b, 0x0000, snacid);
2296
2297 byte_stream_putraw(&frame->data, cookie, 8);
2298
2299 byte_stream_put16(&frame->data, 0x0002); /* channel */
2300 byte_stream_put8(&frame->data, strlen(sn));
2301 byte_stream_putstr(&frame->data, sn);
2302
2303 aim_tlvlist_add_16(&tl, 0x0003, code);
2304 aim_tlvlist_write(&frame->data, &tl);
2305 aim_tlvlist_free(&tl);
2306
2307 flap_connection_send(conn, frame);
2308
2309 return 0;
2310 }
2311
2312 /*
2313 * Subtype 0x000b - Receive the response from an ICQ status message
2314 * request (in which case this contains the ICQ status message) or
2315 * a file transfer or direct IM request was declined.
2316 */
2317 static int clientautoresp(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
2318 {
2319 int ret = 0;
2320 aim_rxcallback_t userfunc;
2321 guint16 channel, reason;
2322 char *sn;
2323 guchar *cookie;
2324 guint8 snlen;
2325
2326 cookie = byte_stream_getraw(bs, 8);
2327 channel = byte_stream_get16(bs);
2328 snlen = byte_stream_get8(bs);
2329 sn = byte_stream_getstr(bs, snlen);
2330 reason = byte_stream_get16(bs);
2331
2332 if (channel == 0x0002) { /* File transfer declined */
2333 byte_stream_get16(bs); /* Unknown */
2334 byte_stream_get16(bs); /* Unknown */
2335 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
2336 ret = userfunc(od, conn, frame, channel, sn, reason, cookie);
2337 } else if (channel == 0x0004) { /* ICQ message */
2338 switch (reason) {
2339 case 0x0003: { /* ICQ status message. Maybe other stuff too, you never know with these people. */
2340 guint8 statusmsgtype, *msg;
2341 guint16 len;
2342 guint32 state;
2343
2344 len = byte_stream_getle16(bs); /* Should be 0x001b */
2345 byte_stream_advance(bs, len); /* Unknown */
2346
2347 len = byte_stream_getle16(bs); /* Should be 0x000e */
2348 byte_stream_advance(bs, len); /* Unknown */
2349
2350 statusmsgtype = byte_stream_getle8(bs);
2351 switch (statusmsgtype) {
2352 case 0xe8:
2353 state = AIM_ICQ_STATE_AWAY;
2354 break;
2355 case 0xe9:
2356 state = AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_BUSY;
2357 break;
2358 case 0xea:
2359 state = AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_OUT;
2360 break;
2361 case 0xeb:
2362 state = AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_DND | AIM_ICQ_STATE_BUSY;
2363 break;
2364 case 0xec:
2365 state = AIM_ICQ_STATE_CHAT;
2366 break;
2367 default:
2368 state = 0;
2369 break;
2370 }
2371
2372 byte_stream_getle8(bs); /* Unknown - 0x03 Maybe this means this is an auto-reply */
2373 byte_stream_getle16(bs); /* Unknown - 0x0000 */
2374 byte_stream_getle16(bs); /* Unknown - 0x0000 */
2375
2376 len = byte_stream_getle16(bs);
2377 msg = byte_stream_getraw(bs, len);
2378
2379 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
2380 ret = userfunc(od, conn, frame, channel, sn, reason, state, msg);
2381
2382 free(msg);
2383 } break;
2384
2385 default: {
2386 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
2387 ret = userfunc(od, conn, frame, channel, sn, reason);
2388 } break;
2389 } /* end switch */
2390 }
2391
2392 free(cookie);
2393 free(sn);
2394
2395 return ret;
2396 }
2397
2398 /*
2399 * Subtype 0x000c - Receive an ack after sending an ICBM.
2400 *
2401 * You have to have send the message with the AIM_IMFLAGS_ACK flag set
2402 * (TLV t(0003)). The ack contains the ICBM header of the message you
2403 * sent.
2404 *
2405 */
2406 static int msgack(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
2407 {
2408 aim_rxcallback_t userfunc;
2409 guint16 ch;
2410 guchar *cookie;
2411 char *sn;
2412 int ret = 0;
2413
2414 cookie = byte_stream_getraw(bs, 8);
2415 ch = byte_stream_get16(bs);
2416 sn = byte_stream_getstr(bs, byte_stream_get8(bs));
2417
2418 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
2419 ret = userfunc(od, conn, frame, ch, sn);
2420
2421 free(sn);
2422 free(cookie);
2423
2424 return ret;
2425 }
2426
2427 /*
2428 * Subtype 0x0014 - Send a mini typing notification (mtn) packet.
2429 *
2430 * This is supported by winaim5 and newer, MacAIM bleh and newer, iChat bleh and newer,
2431 * and Gaim 0.60 and newer.
2432 *
2433 */
2434 int aim_im_sendmtn(OscarData *od, guint16 type1, const char *sn, guint16 type2)
2435 {
2436 FlapConnection *conn;
2437 FlapFrame *frame;
2438 aim_snacid_t snacid;
2439
2440 if (!od || !(conn = flap_connection_findbygroup(od, 0x0002)))
2441 return -EINVAL;
2442
2443 if (!sn)
2444 return -EINVAL;
2445
2446 frame = flap_frame_new(od, 0x02, 10+11+strlen(sn)+2);
2447
2448 snacid = aim_cachesnac(od, 0x0004, 0x0014, 0x0000, NULL, 0);
2449 aim_putsnac(&frame->data, 0x0004, 0x0014, 0x0000, snacid);
2450
2451 /*
2452 * 8 days of light
2453 * Er, that is to say, 8 bytes of 0's
2454 */
2455 byte_stream_put16(&frame->data, 0x0000);
2456 byte_stream_put16(&frame->data, 0x0000);
2457 byte_stream_put16(&frame->data, 0x0000);
2458 byte_stream_put16(&frame->data, 0x0000);
2459
2460 /*
2461 * Type 1 (should be 0x0001 for mtn)
2462 */
2463 byte_stream_put16(&frame->data, type1);
2464
2465 /*
2466 * Dest sn
2467 */
2468 byte_stream_put8(&frame->data, strlen(sn));
2469 byte_stream_putstr(&frame->data, sn);
2470
2471 /*
2472 * Type 2 (should be 0x0000, 0x0001, or 0x0002 for mtn)
2473 */
2474 byte_stream_put16(&frame->data, type2);
2475
2476 flap_connection_send(conn, frame);
2477
2478 return 0;
2479 }
2480
2481 /*
2482 * Subtype 0x0014 - Receive a mini typing notification (mtn) packet.
2483 *
2484 * This is supported by winaim5 and newer, MacAIM bleh and newer, iChat bleh and newer,
2485 * and Gaim 0.60 and newer.
2486 *
2487 */
2488 static int mtn_receive(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
2489 {
2490 int ret = 0;
2491 aim_rxcallback_t userfunc;
2492 char *sn;
2493 guint8 snlen;
2494 guint16 type1, type2;
2495
2496 byte_stream_advance(bs, 8); /* Unknown - All 0's */
2497 type1 = byte_stream_get16(bs);
2498 snlen = byte_stream_get8(bs);
2499 sn = byte_stream_getstr(bs, snlen);
2500 type2 = byte_stream_get16(bs);
2501
2502 if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
2503 ret = userfunc(od, conn, frame, type1, sn, type2);
2504
2505 free(sn);
2506
2507 return ret;
2508 }
2509
2510 static int
2511 snachandler(OscarData *od, FlapConnection *conn, aim_module_t *mod, FlapFrame *frame, aim_modsnac_t *snac, ByteStream *bs)
2512 {
2513 if (snac->subtype == 0x0005)
2514 return aim_im_paraminfo(od, conn, mod, frame, snac, bs);
2515 else if (snac->subtype == 0x0006)
2516 return outgoingim(od, conn, mod, frame, snac, bs);
2517 else if (snac->subtype == 0x0007)
2518 return incomingim(od, conn, mod, frame, snac, bs);
2519 else if (snac->subtype == 0x000a)
2520 return missedcall(od, conn, mod, frame, snac, bs);
2521 else if (snac->subtype == 0x000b)
2522 return clientautoresp(od, conn, mod, frame, snac, bs);
2523 else if (snac->subtype == 0x000c)
2524 return msgack(od, conn, mod, frame, snac, bs);
2525 else if (snac->subtype == 0x0014)
2526 return mtn_receive(od, conn, mod, frame, snac, bs);
2527
2528 return 0;
2529 }
2530
2531 int
2532 msg_modfirst(OscarData *od, aim_module_t *mod)
2533 {
2534 mod->family = 0x0004;
2535 mod->version = 0x0001;
2536 mod->toolid = 0x0110;
2537 mod->toolversion = 0x0629;
2538 mod->flags = 0;
2539 strncpy(mod->name, "messaging", sizeof(mod->name));
2540 mod->snachandler = snachandler;
2541
2542 return 0;
2543 }