comparison libgaim/protocols/oscar/family_icbm.c @ 14192:60b1bc8dbf37

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