comparison src/protocols/oscar/family_icbm.c @ 13234:f2431a7e33aa

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