]> andersk Git - libfaim.git/blame - src/im.c
- Sun Oct 14 19:45:54 PDT 2001
[libfaim.git] / src / im.c
CommitLineData
9de3ca7e 1/*
2 * aim_im.c
3 *
4 * The routines for sending/receiving Instant Messages.
5 *
6ae3c48d 6 * Note the term ICBM (Inter-Client Basic Message) which blankets
7 * all types of genericly routed through-server messages. Within
8 * the ICBM types (family 4), a channel is defined. Each channel
9 * represents a different type of message. Channel 1 is used for
10 * what would commonly be called an "instant message". Channel 2
11 * is used for negotiating "rendezvous". These transactions end in
12 * something more complex happening, such as a chat invitation, or
13 * a file transfer.
14 *
15 * In addition to the channel, every ICBM contains a cookie. For
16 * standard IMs, these are only used for error messages. However,
17 * the more complex rendezvous messages make suitably more complex
18 * use of this field.
19 *
9de3ca7e 20 */
21
37ee990e 22#define FAIM_INTERNAL
dd60ff8b 23#include <aim.h>
9de3ca7e 24
9d83220c 25/*
26 * Takes a msghdr (and a length) and returns a client type
27 * code. Note that this is *only a guess* and has a low likelihood
28 * of actually being accurate.
29 *
30 * Its based on experimental data, with the help of Eric Warmenhoven
31 * who seems to have collected a wide variety of different AIM clients.
32 *
33 *
34 * Heres the current collection:
35 * 0501 0003 0101 0101 01 AOL Mobile Communicator, WinAIM 1.0.414
36 * 0501 0003 0101 0201 01 WinAIM 2.0.847, 2.1.1187, 3.0.1464,
37 * 4.3.2229, 4.4.2286
38 * 0501 0004 0101 0102 0101 WinAIM 4.1.2010, libfaim (right here)
39 * 0501 0001 0101 01 AOL v6.0, CompuServe 2000 v6.0, any
40 * TOC client
d410cf58 41 *
42 * Note that in this function, only the feature bytes are tested, since
43 * the rest will always be the same.
44 *
9d83220c 45 */
d410cf58 46faim_export fu16_t aim_fingerprintclient(fu8_t *msghdr, int len)
9d83220c 47{
6ae3c48d 48 static const struct {
d410cf58 49 fu16_t clientid;
6ae3c48d 50 int len;
d410cf58 51 fu8_t data[10];
6ae3c48d 52 } fingerprints[] = {
53 /* AOL Mobile Communicator, WinAIM 1.0.414 */
54 { AIM_CLIENTTYPE_MC,
d410cf58 55 3, {0x01, 0x01, 0x01}},
6ae3c48d 56
57 /* WinAIM 2.0.847, 2.1.1187, 3.0.1464, 4.3.2229, 4.4.2286 */
58 { AIM_CLIENTTYPE_WINAIM,
d410cf58 59 3, {0x01, 0x01, 0x02}},
6ae3c48d 60
61 /* WinAIM 4.1.2010, libfaim */
62 { AIM_CLIENTTYPE_WINAIM41,
d410cf58 63 4, {0x01, 0x01, 0x01, 0x02}},
6ae3c48d 64
65 /* AOL v6.0, CompuServe 2000 v6.0, any TOC client */
66 { AIM_CLIENTTYPE_AOL_TOC,
d410cf58 67 1, {0x01}},
6ae3c48d 68
69 { 0, 0}
70 };
71 int i;
72
73 if (!msghdr || (len <= 0))
74 return AIM_CLIENTTYPE_UNKNOWN;
75
76 for (i = 0; fingerprints[i].len; i++) {
77 if (fingerprints[i].len != len)
78 continue;
79 if (memcmp(fingerprints[i].data, msghdr, fingerprints[i].len) == 0)
80 return fingerprints[i].clientid;
81 }
82
83 return AIM_CLIENTTYPE_UNKNOWN;
9d83220c 84}
85
50038c74 86/* This should be endian-safe now... but who knows... */
e1b8c575 87faim_export fu32_t aim_iconsum(const fu8_t *buf, int buflen)
50038c74 88{
d410cf58 89 fu32_t sum;
6ae3c48d 90 int i;
50038c74 91
6ae3c48d 92 for (i = 0, sum = 0; i < buflen; i += 2)
93 sum += (buf[i+1] << 8) + buf[i];
50038c74 94
6ae3c48d 95 sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff);
50038c74 96
e1b8c575 97 return sum;
50038c74 98}
99
9de3ca7e 100/*
101 * Send an ICBM (instant message).
102 *
103 *
104 * Possible flags:
105 * AIM_IMFLAGS_AWAY -- Marks the message as an autoresponse
106 * AIM_IMFLAGS_ACK -- Requests that the server send an ack
107 * when the message is received (of type 0x0004/0x000c)
7b91722d 108 * AIM_IMFLAGS_UNICODE--Instead of ASCII7, the passed message is
109 * made up of UNICODE duples. If you set
110 * this, you'd better be damn sure you know
111 * what you're doing.
112 * AIM_IMFLAGS_ISO_8859_1 -- The message contains the ASCII8 subset
113 * known as ISO-8859-1.
114 *
115 * Generally, you should use the lowest encoding possible to send
116 * your message. If you only use basic punctuation and the generic
117 * Latin alphabet, use ASCII7 (no flags). If you happen to use non-ASCII7
118 * characters, but they are all clearly defined in ISO-8859-1, then
119 * use that. Keep in mind that not all characters in the PC ASCII8
120 * character set are defined in the ISO standard. For those cases (most
121 * notably when the (r) symbol is used), you must use the full UNICODE
122 * encoding for your message. In UNICODE mode, _all_ characters must
123 * occupy 16bits, including ones that are not special. (Remember that
124 * the first 128 UNICODE symbols are equivelent to ASCII7, however they
125 * must be prefixed with a zero high order byte.)
126 *
127 * I strongly discourage the use of UNICODE mode, mainly because none
128 * of the clients I use can parse those messages (and besides that,
129 * wchars are difficult and non-portable to handle in most UNIX environments).
130 * If you really need to include special characters, use the HTML UNICODE
131 * entities. These are of the form &#2026; where 2026 is the hex
132 * representation of the UNICODE index (in this case, UNICODE
133 * "Horizontal Ellipsis", or 133 in in ASCII8).
9de3ca7e 134 *
d410cf58 135 * Implementation note: Since this is one of the most-used functions
136 * in all of libfaim, it is written with performance in mind. As such,
137 * it is not as clear as it could be in respect to how this message is
138 * supposed to be layed out. Most obviously, tlvlists should be used
139 * instead of writing out the bytes manually.
140 *
603fdcaa 141 * XXX more precise verification that we never send SNACs larger than 8192
142 * XXX check SNAC size for multipart
d410cf58 143 *
9de3ca7e 144 */
d410cf58 145faim_export int aim_send_im_ext(aim_session_t *sess, aim_conn_t *conn, struct aim_sendimext_args *args)
a2244dd9 146{
84e0ca17 147 static const fu8_t deffeatures[] = {
25aaf30e 148 0x01, 0x01, 0x01, 0x02
84e0ca17 149 };
150 int i, msgtlvlen;
d410cf58 151 aim_frame_t *fr;
152 aim_snacid_t snacid;
2d5fd943 153
154 if (!sess || !conn || !args)
d410cf58 155 return -EINVAL;
2d5fd943 156
603fdcaa 157 if (args->flags & AIM_IMFLAGS_MULTIPART) {
158 if (args->mpmsg->numparts <= 0)
159 return -EINVAL;
160 } else {
161 if (!args->msg || (args->msglen <= 0))
162 return -EINVAL;
163
164 if (args->msglen >= MAXMSGLEN)
165 return -E2BIG;
166 }
2d5fd943 167
603fdcaa 168 /* Painfully calculate the size of the message TLV */
169 msgtlvlen = 1 + 1; /* 0501 */
2d5fd943 170
84e0ca17 171 if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES)
603fdcaa 172 msgtlvlen += 2 + args->featureslen;
84e0ca17 173 else
603fdcaa 174 msgtlvlen += 2 + sizeof(deffeatures);
175
176 if (args->flags & AIM_IMFLAGS_MULTIPART) {
177 aim_mpmsg_section_t *sec;
178
179 for (sec = args->mpmsg->parts; sec; sec = sec->next) {
180 msgtlvlen += 2 /* 0101 */ + 2 /* block len */;
181 msgtlvlen += 4 /* charset */ + sec->datalen;
182 }
183
184 } else {
185 msgtlvlen += 2 /* 0101 */ + 2 /* block len */;
186 msgtlvlen += 4 /* charset */ + args->msglen;
187 }
188
189
190 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, msgtlvlen+128)))
2d5fd943 191 return -ENOMEM;
192
d410cf58 193 /* XXX should be optional */
194 snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, args->destsn, strlen(args->destsn)+1);
195 aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
2d5fd943 196
197 /*
198 * Generate a random message cookie
199 *
200 * We could cache these like we do SNAC IDs. (In fact, it
201 * might be a good idea.) In the message error functions,
202 * the 8byte message cookie is returned as well as the
203 * SNAC ID.
204 *
205 */
d410cf58 206 for (i = 0; i < 8; i++)
207 aimbs_put8(&fr->data, (fu8_t) rand());
2d5fd943 208
209 /*
210 * Channel ID
211 */
d410cf58 212 aimbs_put16(&fr->data, 0x0001);
2d5fd943 213
214 /*
215 * Destination SN (prepended with byte length)
216 */
d410cf58 217 aimbs_put8(&fr->data, strlen(args->destsn));
218 aimbs_putraw(&fr->data, args->destsn, strlen(args->destsn));
2d5fd943 219
220 /*
221 * metaTLV start.
222 */
d410cf58 223 aimbs_put16(&fr->data, 0x0002);
84e0ca17 224 aimbs_put16(&fr->data, msgtlvlen);
2d5fd943 225
226 /*
84e0ca17 227 * Features
2d5fd943 228 *
229 */
d410cf58 230 aimbs_put8(&fr->data, 0x05);
231 aimbs_put8(&fr->data, 0x01);
2d5fd943 232
84e0ca17 233 if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES) {
234 aimbs_put16(&fr->data, args->featureslen);
235 aimbs_putraw(&fr->data, args->features, args->featureslen);
236 } else {
237 aimbs_put16(&fr->data, sizeof(deffeatures));
238 aimbs_putraw(&fr->data, deffeatures, sizeof(deffeatures));
239 }
2d5fd943 240
603fdcaa 241 if (args->flags & AIM_IMFLAGS_MULTIPART) {
242 aim_mpmsg_section_t *sec;
2d5fd943 243
603fdcaa 244 for (sec = args->mpmsg->parts; sec; sec = sec->next) {
245 aimbs_put16(&fr->data, 0x0101);
246 aimbs_put16(&fr->data, sec->datalen + 4);
247 aimbs_put16(&fr->data, sec->charset);
248 aimbs_put16(&fr->data, sec->charsubset);
249 aimbs_putraw(&fr->data, sec->data, sec->datalen);
250 }
2d5fd943 251
603fdcaa 252 } else {
2d5fd943 253
603fdcaa 254 aimbs_put16(&fr->data, 0x0101);
2d5fd943 255
603fdcaa 256 /*
257 * Message block length.
258 */
259 aimbs_put16(&fr->data, args->msglen + 0x04);
260
261 /*
262 * Character set.
263 */
264 if (args->flags & AIM_IMFLAGS_CUSTOMCHARSET) {
265
266 aimbs_put16(&fr->data, args->charset);
267 aimbs_put16(&fr->data, args->charsubset);
268
269 } else {
270 if (args->flags & AIM_IMFLAGS_UNICODE)
271 aimbs_put16(&fr->data, 0x0002);
272 else if (args->flags & AIM_IMFLAGS_ISO_8859_1)
273 aimbs_put16(&fr->data, 0x0003);
274 else
275 aimbs_put16(&fr->data, 0x0000);
276
277 aimbs_put16(&fr->data, 0x0000);
278 }
279
280 /*
281 * Message. Not terminated.
282 */
283 aimbs_putraw(&fr->data, args->msg, args->msglen);
284 }
2d5fd943 285
286 /*
287 * Set the Request Acknowledge flag.
288 */
289 if (args->flags & AIM_IMFLAGS_ACK) {
d410cf58 290 aimbs_put16(&fr->data, 0x0003);
291 aimbs_put16(&fr->data, 0x0000);
2d5fd943 292 }
d410cf58 293
2d5fd943 294 /*
295 * Set the Autoresponse flag.
296 */
297 if (args->flags & AIM_IMFLAGS_AWAY) {
d410cf58 298 aimbs_put16(&fr->data, 0x0004);
299 aimbs_put16(&fr->data, 0x0000);
2d5fd943 300 }
301
302 /*
303 * Set the Buddy Icon Requested flag.
304 */
305 if (args->flags & AIM_IMFLAGS_BUDDYREQ) {
d410cf58 306 aimbs_put16(&fr->data, 0x0009);
307 aimbs_put16(&fr->data, 0x0000);
2d5fd943 308 }
309
310 /*
d410cf58 311 * Set the I HAVE A REALLY PURTY ICON flag.
2d5fd943 312 */
313 if (args->flags & AIM_IMFLAGS_HASICON) {
e1b8c575 314 aimbs_put16(&fr->data, 0x0008);
d410cf58 315 aimbs_put16(&fr->data, 0x000c);
316 aimbs_put32(&fr->data, args->iconlen);
e1b8c575 317 aimbs_put32(&fr->data, args->iconsum);
d410cf58 318 aimbs_put32(&fr->data, args->iconstamp);
2d5fd943 319 }
320
d410cf58 321 aim_tx_enqueue(sess, fr);
49c8a2fa 322
7b91722d 323#if 1 /* XXX do this with autoconf or something... */
2d5fd943 324 aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
7b91722d 325#endif
9de3ca7e 326
2d5fd943 327 return 0;
9de3ca7e 328}
329
7b91722d 330/*
331 * Simple wrapper for aim_send_im_ext()
332 *
333 * You cannot use aim_send_im if you need the HASICON flag. You must
334 * use aim_send_im_ext directly for that.
335 *
336 * aim_send_im also cannot be used if you require UNICODE messages, because
337 * that requires an explicit message length. Use aim_send_im_ext().
338 *
339 */
d410cf58 340faim_export int aim_send_im(aim_session_t *sess, aim_conn_t *conn, const char *destsn, fu16_t flags, const char *msg)
7b91722d 341{
2d5fd943 342 struct aim_sendimext_args args;
7b91722d 343
2d5fd943 344 args.destsn = destsn;
345 args.flags = flags;
346 args.msg = msg;
347 args.msglen = strlen(msg);
7b91722d 348
84e0ca17 349 /* Make these don't get set by accident -- they need aim_send_im_ext */
603fdcaa 350 args.flags &= ~(AIM_IMFLAGS_CUSTOMFEATURES | AIM_IMFLAGS_HASICON | AIM_IMFLAGS_MULTIPART);
84e0ca17 351
2d5fd943 352 return aim_send_im_ext(sess, conn, &args);
7b91722d 353}
354
d410cf58 355/*
603fdcaa 356 * This is also performance sensitive. (If you can believe it...)
d410cf58 357 *
358 */
e1b8c575 359faim_export int aim_send_icon(aim_session_t *sess, aim_conn_t *conn, const char *sn, const fu8_t *icon, int iconlen, time_t stamp, fu32_t iconsum)
7b91722d 360{
d410cf58 361 int i;
362 fu8_t ck[8];
363 aim_frame_t *fr;
364 aim_snacid_t snacid;
6ae3c48d 365
d410cf58 366 if (!sess || !conn || !sn || !icon || (iconlen <= 0) || (iconlen >= MAXICONLEN))
367 return -EINVAL;
6ae3c48d 368
369 if (conn->type != AIM_CONN_TYPE_BOS)
370 return -EINVAL;
371
d410cf58 372 for (i = 0; i < 8; i++)
373 aimutil_put8(ck+i, (fu8_t) rand());
6ae3c48d 374
d410cf58 375 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)))
6ae3c48d 376 return -ENOMEM;
377
d410cf58 378 snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
379 aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
6ae3c48d 380
381 /*
382 * Cookie
383 */
d410cf58 384 aimbs_putraw(&fr->data, ck, 8);
6ae3c48d 385
386 /*
387 * Channel (2)
388 */
d410cf58 389 aimbs_put16(&fr->data, 0x0002);
6ae3c48d 390
391 /*
392 * Dest sn
393 */
d410cf58 394 aimbs_put8(&fr->data, strlen(sn));
395 aimbs_putraw(&fr->data, sn, strlen(sn));
6ae3c48d 396
397 /*
398 * TLV t(0005)
d410cf58 399 *
400 * Encompasses everything below.
6ae3c48d 401 */
d410cf58 402 aimbs_put16(&fr->data, 0x0005);
403 aimbs_put16(&fr->data, 2+8+16+6+4+4+iconlen+4+4+4+strlen(AIM_ICONIDENT));
6ae3c48d 404
d410cf58 405 aimbs_put16(&fr->data, 0x0000);
406 aimbs_putraw(&fr->data, ck, 8);
407 aim_putcap(&fr->data, AIM_CAPS_BUDDYICON);
6ae3c48d 408
409 /* TLV t(000a) */
d410cf58 410 aimbs_put16(&fr->data, 0x000a);
411 aimbs_put16(&fr->data, 0x0002);
412 aimbs_put16(&fr->data, 0x0001);
6ae3c48d 413
414 /* TLV t(000f) */
d410cf58 415 aimbs_put16(&fr->data, 0x000f);
416 aimbs_put16(&fr->data, 0x0000);
6ae3c48d 417
418 /* TLV t(2711) */
d410cf58 419 aimbs_put16(&fr->data, 0x2711);
420 aimbs_put16(&fr->data, 4+4+4+iconlen+strlen(AIM_ICONIDENT));
e1b8c575 421 aimbs_put32(&fr->data, iconsum);
d410cf58 422 aimbs_put32(&fr->data, iconlen);
423 aimbs_put32(&fr->data, stamp);
424 aimbs_putraw(&fr->data, icon, iconlen);
425 aimbs_putraw(&fr->data, AIM_ICONIDENT, strlen(AIM_ICONIDENT));
6ae3c48d 426
427 /* TLV t(0003) */
d410cf58 428 aimbs_put16(&fr->data, 0x0003);
429 aimbs_put16(&fr->data, 0x0000);
6ae3c48d 430
d410cf58 431 aim_tx_enqueue(sess, fr);
6ae3c48d 432
433 return 0;
7b91722d 434}
435
d410cf58 436static int outgoingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
e5012450 437{
d410cf58 438 int i, ret = 0;
6ae3c48d 439 aim_rxcallback_t userfunc;
d410cf58 440 fu8_t cookie[8];
441 fu16_t channel;
442 aim_tlvlist_t *tlvlist;
443 char *sn;
444 int snlen;
445 fu16_t icbmflags = 0;
446 fu8_t flag1 = 0, flag2 = 0;
447 fu8_t *msg = NULL;
448 aim_tlv_t *msgblock;
6ae3c48d 449
450 /* ICBM Cookie. */
451 for (i = 0; i < 8; i++)
d410cf58 452 cookie[i] = aimbs_get8(bs);
6ae3c48d 453
454 /* Channel ID */
d410cf58 455 channel = aimbs_get16(bs);
6ae3c48d 456
457 if (channel != 0x01) {
458 faimdprintf(sess, 0, "icbm: ICBM recieved on unsupported channel. Ignoring. (chan = %04x)\n", channel);
d410cf58 459 return 0;
6ae3c48d 460 }
461
d410cf58 462 snlen = aimbs_get8(bs);
463 sn = aimbs_getstr(bs, snlen);
6ae3c48d 464
d410cf58 465 tlvlist = aim_readtlvchain(bs);
6ae3c48d 466
467 if (aim_gettlv(tlvlist, 0x0003, 1))
468 icbmflags |= AIM_IMFLAGS_ACK;
469 if (aim_gettlv(tlvlist, 0x0004, 1))
470 icbmflags |= AIM_IMFLAGS_AWAY;
e5012450 471
d410cf58 472 if ((msgblock = aim_gettlv(tlvlist, 0x0002, 1))) {
473 aim_bstream_t mbs;
474 int featurelen, msglen;
475
476 aim_bstream_init(&mbs, msgblock->value, msgblock->length);
6ae3c48d 477
d410cf58 478 aimbs_get8(&mbs);
479 aimbs_get8(&mbs);
480 for (featurelen = aimbs_get16(&mbs); featurelen; featurelen--)
481 aimbs_get8(&mbs);
482 aimbs_get8(&mbs);
483 aimbs_get8(&mbs);
6ae3c48d 484
d410cf58 485 msglen = aimbs_get16(&mbs) - 4; /* final block length */
6ae3c48d 486
d410cf58 487 flag1 = aimbs_get16(&mbs);
488 flag2 = aimbs_get16(&mbs);
6ae3c48d 489
d410cf58 490 msg = aimbs_getstr(&mbs, msglen);
6ae3c48d 491 }
492
493 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
494 ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2);
495
d410cf58 496 free(sn);
6ae3c48d 497 aim_freetlvchain(&tlvlist);
498
499 return ret;
00ef5271 500}
501
603fdcaa 502/*
503 * Ahh, the joys of nearly ridiculous over-engineering.
504 *
505 * Not only do AIM ICBM's support multiple channels. Not only do they
506 * support multiple character sets. But they support multiple character
507 * sets / encodings within the same ICBM.
508 *
509 * These multipart messages allow for complex space savings techniques, which
510 * seem utterly unnecessary by today's standards. In fact, there is only
511 * one client still in popular use that still uses this method: AOL for the
512 * Macintosh, Version 5.0. Obscure, yes, I know.
513 *
514 * In modern (non-"legacy") clients, if the user tries to send a character
515 * that is not ISO-8859-1 or ASCII, the client will send the entire message
516 * as UNICODE, meaning that every character in the message will occupy the
517 * full 16 bit UNICODE field, even if the high order byte would be zero.
518 * Multipart messages prevent this wasted space by allowing the client to
519 * only send the characters in UNICODE that need to be sent that way, and
520 * the rest of the message can be sent in whatever the native character
521 * set is (probably ASCII).
522 *
523 * An important note is that sections will be displayed in the order that
524 * they appear in the ICBM. There is no facility for merging or rearranging
525 * sections at run time. So if you have, say, ASCII then UNICODE then ASCII,
526 * you must supply two ASCII sections with a UNICODE in the middle, and incur
527 * the associated overhead.
528 *
529 * Normally I would have laughed and given a firm 'no' to supporting this
530 * seldom-used feature, but something is attracting me to it. In the future,
531 * it may be possible to abuse this to send mixed-media messages to other
532 * open source clients (like encryption or something) -- see faimtest for
533 * examples of how to do this.
534 *
535 * I would definitly recommend avoiding this feature unless you really
536 * know what you are doing, and/or you have something neat to do with it.
537 *
538 */
539faim_export int aim_mpmsg_init(aim_session_t *sess, aim_mpmsg_t *mpm)
540{
541
542 memset(mpm, 0, sizeof(aim_mpmsg_t));
543
544 return 0;
545}
546
547static int mpmsg_addsection(aim_session_t *sess, aim_mpmsg_t *mpm, fu16_t charset, fu16_t charsubset, fu8_t *data, fu16_t datalen)
548{
549 aim_mpmsg_section_t *sec;
550
551 if (!(sec = malloc(sizeof(aim_mpmsg_section_t))))
552 return -1;
553
554 sec->charset = charset;
555 sec->charsubset = charsubset;
556 sec->data = data;
557 sec->datalen = datalen;
558 sec->next = NULL;
559
560 if (!mpm->parts)
561 mpm->parts = sec;
562 else {
563 aim_mpmsg_section_t *cur;
564
565 for (cur = mpm->parts; cur->next; cur = cur->next)
566 ;
567 cur->next = sec;
568 }
569
570 mpm->numparts++;
571
572 return 0;
573}
574
575faim_export int aim_mpmsg_addraw(aim_session_t *sess, aim_mpmsg_t *mpm, fu16_t charset, fu16_t charsubset, const fu8_t *data, fu16_t datalen)
576{
577 fu8_t *dup;
578
579 if (!(dup = malloc(datalen)))
580 return -1;
581 memcpy(dup, data, datalen);
582
583 if (mpmsg_addsection(sess, mpm, charset, charsubset, dup, datalen) == -1) {
584 free(dup);
585 return -1;
586 }
587
588 return 0;
589}
590
591/* XXX should provide a way of saying ISO-8859-1 specifically */
592faim_export int aim_mpmsg_addascii(aim_session_t *sess, aim_mpmsg_t *mpm, const char *ascii)
593{
594 fu8_t *dup;
595
596 if (!(dup = strdup(ascii)))
597 return -1;
598
599 if (mpmsg_addsection(sess, mpm, 0x0000, 0x0000, dup, strlen(ascii)) == -1) {
600 free(dup);
601 return -1;
602 }
603
604 return 0;
605}
606
607faim_export int aim_mpmsg_addunicode(aim_session_t *sess, aim_mpmsg_t *mpm, const fu16_t *unicode, fu16_t unicodelen)
608{
609 fu8_t *buf;
610 aim_bstream_t bs;
611 int i;
612
613 if (!(buf = malloc(unicodelen * 2)))
614 return -1;
615
616 aim_bstream_init(&bs, buf, unicodelen * 2);
617
618 /* We assume unicode is in /host/ byte order -- convert to network */
619 for (i = 0; i < unicodelen; i++)
620 aimbs_put16(&bs, unicode[i]);
621
622 if (mpmsg_addsection(sess, mpm, 0x0002, 0x0000, buf, aim_bstream_curpos(&bs)) == -1) {
623 free(buf);
624 return -1;
625 }
626
627 return 0;
628}
629
630faim_export void aim_mpmsg_free(aim_session_t *sess, aim_mpmsg_t *mpm)
631{
632 aim_mpmsg_section_t *cur;
633
634 for (cur = mpm->parts; cur; ) {
635 aim_mpmsg_section_t *tmp;
636
637 tmp = cur->next;
638 free(cur->data);
639 free(cur);
640 cur = tmp;
641 }
642
643 mpm->numparts = 0;
644 mpm->parts = NULL;
645
646 return;
647}
648
649/*
650 * Start by building the multipart structures, then pick the first
651 * human-readable section and stuff it into args->msg so no one gets
652 * suspicious.
653 *
654 */
655static int incomingim_ch1_parsemsgs(aim_session_t *sess, fu8_t *data, int len, struct aim_incomingim_ch1_args *args)
656{
657 static const fu16_t charsetpri[] = {
658 0x0000, /* ASCII first */
659 0x0003, /* then ISO-8859-1 */
660 0x0002, /* UNICODE as last resort */
661 };
662 static const int charsetpricount = 3;
663 int i;
664 aim_bstream_t mbs;
665 aim_mpmsg_section_t *sec;
666
667 aim_bstream_init(&mbs, data, len);
668
669 while (aim_bstream_empty(&mbs)) {
670 fu16_t msglen, flag1, flag2;
671 fu8_t *msgbuf;
672
673 aimbs_get8(&mbs); /* 01 */
674 aimbs_get8(&mbs); /* 01 */
675
676 /* Message string length, including character set info. */
677 msglen = aimbs_get16(&mbs);
678
679 /* Character set info */
680 flag1 = aimbs_get16(&mbs);
681 flag2 = aimbs_get16(&mbs);
682
683 /* Message. */
684 msglen -= 4;
685
686 /*
687 * For now, we don't care what the encoding is. Just copy
688 * it into a multipart struct and deal with it later. However,
689 * always pad the ending with a NULL. This makes it easier
690 * to treat ASCII sections as strings. It won't matter for
691 * UNICODE or binary data, as you should never read past
692 * the specified data length, which will not include the pad.
693 *
694 * XXX There's an API bug here. For sending, the UNICODE is
695 * given in host byte order (aim_mpmsg_addunicode), but here
696 * the received messages are given in network byte order.
697 *
698 */
699 msgbuf = aimbs_getstr(&mbs, msglen);
700 mpmsg_addsection(sess, &args->mpmsg, flag1, flag2, msgbuf, msglen);
701
702 } /* while */
703
704 args->icbmflags |= AIM_IMFLAGS_MULTIPART; /* always set */
705
706 /*
707 * Clients that support multiparts should never use args->msg, as it
708 * will point to an arbitrary section.
709 *
710 * Here, we attempt to provide clients that do not support multipart
711 * messages with something to look at -- hopefully a human-readable
712 * string. But, failing that, a UNICODE message, or nothing at all.
713 *
714 * Which means that even if args->msg is NULL, it does not mean the
715 * message was blank.
716 *
717 */
718 for (i = 0; i < charsetpricount; i++) {
719 for (sec = args->mpmsg.parts; sec; sec = sec->next) {
720
721 if (sec->charset != charsetpri[i])
722 continue;
723
724 /* Great. We found one. Fill it in. */
725 args->charset = sec->charset;
726 args->charsubset = sec->charsubset;
727 args->icbmflags |= AIM_IMFLAGS_CUSTOMCHARSET;
728
729 /* Set up the simple flags */
730 if (args->charset == 0x0000)
731 ; /* ASCII */
732 else if (args->charset == 0x0002)
733 args->icbmflags |= AIM_IMFLAGS_UNICODE;
734 else if (args->charset == 0x0003)
735 args->icbmflags |= AIM_IMFLAGS_ISO_8859_1;
736 else if (args->charset == 0xffff)
737 ; /* no encoding (yeep!) */
738
739 if (args->charsubset == 0x0000)
740 ; /* standard subencoding? */
741 else if (args->charsubset == 0x000b)
742 args->icbmflags |= AIM_IMFLAGS_SUBENC_MACINTOSH;
743 else if (args->charsubset == 0xffff)
744 ; /* no subencoding */
745#if 0
746 /* XXX this isn't really necesary... */
747 if ( ((args.flag1 != 0x0000) &&
748 (args.flag1 != 0x0002) &&
749 (args.flag1 != 0x0003) &&
750 (args.flag1 != 0xffff)) ||
751 ((args.flag2 != 0x0000) &&
752 (args.flag2 != 0x000b) &&
753 (args.flag2 != 0xffff))) {
754 faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", args.flag1, args.flag2);
755 }
756#endif
757
758 args->msg = sec->data;
759 args->msglen = sec->datalen;
760
761 return 0;
762 }
763 }
764
765 /* No human-readable sections found. Oh well. */
766 args->charset = args->charsubset = 0xffff;
767 args->msg = NULL;
768 args->msglen = 0;
769
770 return 0;
771}
772
6ae3c48d 773/*
d410cf58 774 *
775 * This should use tlvlists, but doesn't for performance reasons.
776 *
777 * XXX support multipart IMs:
6ae3c48d 778 *
779 * 0004 0007 0000 8f08 d295
780 * 0031 6520 3b7b f9fd
781 * 0001
782 * 06 XXXX XXXX XXXX
783 * 0000
784 * 0004
785 * 0001 0002 0004
786 * 0010 0004 0000 01a3
787 * 0002 0004 3ab6 94fa
788 * 0003 0004 3b7b f85a
789 * 0002 003c
790 * 0501 0001 01
791 * 0101 000a 0000 0000 3c48 544d 4c3e ASCII part
792 * ISO-8859 part:
793 * 0101 0016 0003 0000 6c6b 7364 6a6b 6c6a 676c a56b 3b73 646a 6b6a
794 * 0101 000b 0000 0000 3c2f 4854 4d4c 3e another ASCII part
795 *
796 */
7b0bc7f2 797static int incomingim_ch1(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, fu16_t channel, aim_userinfo_t *userinfo, aim_bstream_t *bs, fu8_t *cookie)
00ef5271 798{
d410cf58 799 fu16_t type, length;
1dcd23dd 800 aim_rxcallback_t userfunc;
d410cf58 801 int ret = 0;
1dcd23dd 802 struct aim_incomingim_ch1_args args;
d410cf58 803 int endpos;
00ef5271 804
1dcd23dd 805 memset(&args, 0, sizeof(args));
00ef5271 806
603fdcaa 807 aim_mpmsg_init(sess, &args.mpmsg);
808
1dcd23dd 809 /*
810 * This used to be done using tlvchains. For performance reasons,
811 * I've changed it to process the TLVs in-place. This avoids lots
812 * of per-IM memory allocations.
813 */
d410cf58 814 while (aim_bstream_empty(bs)) {
815
816 type = aimbs_get16(bs);
817 length = aimbs_get16(bs);
1dcd23dd 818
d410cf58 819 endpos = aim_bstream_curpos(bs) + length;
1dcd23dd 820
821 if (type == 0x0002) { /* Message Block */
1dcd23dd 822
1dcd23dd 823 /*
d410cf58 824 * This TLV consists of the following:
825 * - 0501 -- Unknown
826 * - Features: Don't know how to interpret these
827 * - 0101 -- Unknown
828 * - Message
1dcd23dd 829 *
830 */
831
d410cf58 832 aimbs_get8(bs); /* 05 */
833 aimbs_get8(bs); /* 01 */
84e0ca17 834
835 args.featureslen = aimbs_get16(bs);
836 /* XXX XXX this is all evil! */
837 args.features = bs->data + bs->offset;
838 aim_bstream_advance(bs, args.featureslen);
839 args.icbmflags |= AIM_IMFLAGS_CUSTOMFEATURES;
840
603fdcaa 841 /*
842 * The rest of the TLV contains one or more message
843 * blocks...
844 */
845 incomingim_ch1_parsemsgs(sess, bs->data + bs->offset /* XXX evil!!! */, length - 2 - 2 - args.featureslen, &args);
1dcd23dd 846
847 } else if (type == 0x0003) { /* Server Ack Requested */
848
849 args.icbmflags |= AIM_IMFLAGS_ACK;
850
851 } else if (type == 0x0004) { /* Message is Auto Response */
852
853 args.icbmflags |= AIM_IMFLAGS_AWAY;
854
d410cf58 855 } else if (type == 0x0008) { /* I-HAVE-A-REALLY-PURTY-ICON Flag */
1dcd23dd 856
84e0ca17 857 args.iconsum = aimbs_get32(bs);
858 args.iconlen = aimbs_get32(bs);
d410cf58 859 args.iconstamp = aimbs_get32(bs);
1dcd23dd 860 args.icbmflags |= AIM_IMFLAGS_HASICON;
861
862 } else if (type == 0x0009) {
863
864 args.icbmflags |= AIM_IMFLAGS_BUDDYREQ;
865
866 } else if (type == 0x0017) {
867
868 args.extdatalen = length;
d410cf58 869 args.extdata = aimbs_getraw(bs, args.extdatalen);
1dcd23dd 870
871 } else {
6ae3c48d 872 faimdprintf(sess, 0, "incomingim_ch1: unknown TLV 0x%04x (len %d)\n", type, length);
1dcd23dd 873 }
874
d410cf58 875 /*
876 * This is here to protect ourselves from ourselves. That
877 * is, if something above doesn't completly parse its value
878 * section, or, worse, overparses it, this will set the
879 * stream where it needs to be in order to land on the next
880 * TLV when the loop continues.
881 *
882 */
883 aim_bstream_setpos(bs, endpos);
1dcd23dd 884 }
00ef5271 885
00ef5271 886
1dcd23dd 887 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
888 ret = userfunc(sess, rx, channel, userinfo, &args);
00ef5271 889
603fdcaa 890 aim_mpmsg_free(sess, &args.mpmsg);
d410cf58 891 free(args.extdata);
00ef5271 892
1dcd23dd 893 return ret;
00ef5271 894}
895
7b0bc7f2 896static int 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_tlvlist_t *list2)
8722e782 897{
898 aim_rxcallback_t userfunc;
899 int ret = 0;
900 aim_tlv_t *tse;
901 aim_bstream_t tbs;
902
903 if (args->status != 0x0000)
904 return 1; /* ignore it -- not sure what it means */
905
906 tse = aim_gettlv(list2, 0x2711, 1);
907 aim_bstream_init(&tbs, tse->value, tse->length);
908
909 /*
910 * This goes like this...
911 *
912 * group name length
913 * group name
914 * num of buddies in group
915 * buddy name length
916 * buddy name
917 * buddy name length
918 * buddy name
919 * ...
920 * group name length
921 * group name
922 * num of buddies in group
923 * buddy name length
924 * buddy name
925 * ...
926 * ...
927 */
928 while (aim_bstream_empty(&tbs)) {
929 fu16_t gnlen, numb;
930 int i;
931 char *gn;
932
933 gnlen = aimbs_get16(&tbs);
934 gn = aimbs_getstr(&tbs, gnlen);
935 numb = aimbs_get16(&tbs);
936
937 for (i = 0; i < numb; i++) {
938 fu16_t bnlen;
939 char *bn;
940
941 bnlen = aimbs_get16(&tbs);
942 bn = aimbs_getstr(&tbs, bnlen);
943
944 faimdprintf(sess, 0, "got a buddy list from %s: group %s, buddy %s\n", userinfo->sn, gn, bn);
945
946 free(bn);
947 }
948
949 free(gn);
950 }
951
952 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
953 ret = userfunc(sess, rx, 0x0002, userinfo, args);
954
955 return ret;
956}
957
7b0bc7f2 958static int 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_tlvlist_t *list2)
c5f5b7f1 959{
960 aim_rxcallback_t userfunc;
961 int ret = 0;
962 aim_tlv_t *miscinfo;
963 aim_bstream_t tbs;
964
25aaf30e 965 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)))
966 return 0;
c5f5b7f1 967 aim_bstream_init(&tbs, miscinfo->value, miscinfo->length);
968
969 args->info.icon.checksum = aimbs_get32(&tbs);
970 args->info.icon.length = aimbs_get32(&tbs);
971 args->info.icon.timestamp = aimbs_get32(&tbs);
972 args->info.icon.icon = aimbs_getraw(&tbs, args->info.icon.length);
973
974 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
975 ret = userfunc(sess, rx, 0x0002, userinfo, args);
976
977 free(args->info.icon.icon);
978
979 return ret;
980}
981
7b0bc7f2 982static int incomingim_ch2_voice(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_tlvlist_t *list2)
8722e782 983{
984 aim_msgcookie_t *cachedcook;
985 int ret = 0;
986 aim_rxcallback_t userfunc;
987
988 faimdprintf(sess, 1, "rend: voice!\n");
989
990 if (!(cachedcook = (aim_msgcookie_t*)calloc(1, sizeof(aim_msgcookie_t))))
991 return 0;
992
993 memcpy(cachedcook->cookie, args->cookie, 8);
994 cachedcook->type = AIM_COOKIETYPE_OFTVOICE;
995 cachedcook->data = NULL;
996
997 if (aim_cachecookie(sess, cachedcook) == -1)
998 faimdprintf(sess, 0, "ERROR caching message cookie\n");
999
1000 /* XXX: implement all this */
1001
1002 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
5588b2ee 1003 ret = userfunc(sess, rx, 0x0002, userinfo, args);
8722e782 1004
1005 return ret;
1006}
1007
7b0bc7f2 1008static int 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_tlvlist_t *list2)
8722e782 1009{
1010 aim_tlv_t *miscinfo;
1011 aim_bstream_t tbs;
1012 aim_rxcallback_t userfunc;
1013 int ret = 0;
1014
1015 miscinfo = aim_gettlv(list2, 0x2711, 1);
1016 aim_bstream_init(&tbs, miscinfo->value, miscinfo->length);
1017
1018 aim_chat_readroominfo(&tbs, &args->info.chat.roominfo);
1019
1020 if (aim_gettlv(list2, 0x000c, 1))
1021 args->info.chat.msg = aim_gettlv_str(list2, 0x000c, 1);
1022
1023 if (aim_gettlv(list2, 0x000d, 1))
1024 args->info.chat.encoding = aim_gettlv_str(list2, 0x000d, 1);
1025
1026 if (aim_gettlv(list2, 0x000e, 1))
1027 args->info.chat.lang = aim_gettlv_str(list2, 0x000e, 1);
1028
1029 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
5588b2ee 1030 ret = userfunc(sess, rx, 0x0002, userinfo, args);
8722e782 1031
1032 /* XXX free_roominfo */
1033 free(args->info.chat.roominfo.name);
1034 free(args->info.chat.msg);
1035 free(args->info.chat.encoding);
1036 free(args->info.chat.lang);
1037
1038 return ret;
1039}
1040
7b0bc7f2 1041static int incomingim_ch2_getfile(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_tlvlist_t *list2)
8722e782 1042{
1043 char ip[30];
1044 aim_msgcookie_t *cachedcook;
1045 aim_tlv_t *miscinfo;
1046 aim_tlv_t *iptlv, *porttlv;
1047 int ret = 0;
1048 aim_rxcallback_t userfunc;
1049
1050 memset(ip, 0, 30);
1051
1052 if (!(cachedcook = calloc(1, sizeof(aim_msgcookie_t)))) {
1053 aim_freetlvchain(&list2);
1054 return 0;
1055 }
1056
1057 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) ||
1058 !(iptlv = aim_gettlv(list2, 0x0003, 1)) ||
1059 !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
1060
1061 faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
1062 aim_cookie_free(sess, cachedcook);
1063 aim_freetlvchain(&list2);
1064
1065 return 0;
1066 }
1067
1068 snprintf(ip, 30, "%d.%d.%d.%d:%d",
1069 aimutil_get8(iptlv->value+0),
1070 aimutil_get8(iptlv->value+1),
1071 aimutil_get8(iptlv->value+2),
1072 aimutil_get8(iptlv->value+3),
1073 aimutil_get16(porttlv->value));
1074
1075 faimdprintf(sess, 0, "rend: file get request from %s (%s)\n", userinfo->sn, ip);
1076
1077 args->info.getfile.ip = ip;
1078 memcpy(args->info.getfile.cookie, args->cookie, 8);
1079
1080 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
5588b2ee 1081 ret = userfunc(sess, rx, 0x0002, userinfo, args);
8722e782 1082
1083 return ret;
1084}
1085
7b0bc7f2 1086static int 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_tlvlist_t *list2)
8722e782 1087{
1088#if 0
1089 char ip[30];
1090 aim_msgcookie_t *cachedcook;
1091 aim_tlv_t *miscinfo;
1092 aim_tlv_t *iptlv, *porttlv;
1093 int ret =0;
1094 aim_rxcallback_t userfunc;
1095 char *desc = NULL;
1096
1097 memset(ip, 0, 30);
1098
1099 if (!(cachedcook = calloc(1, sizeof(aim_msgcookie_t))))
1100 return 0;
1101
1102 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) ||
1103 !(iptlv = aim_gettlv(list2, 0x0003, 1)) ||
1104 !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
1105
1106 faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
1107 aim_cookie_free(sess, cachedcook);
1108
1109 return 0;
1110 }
1111
1112 snprintf(ip, 30, "%d.%d.%d.%d:%d",
1113 aimutil_get8(iptlv->value+0),
1114 aimutil_get8(iptlv->value+1),
1115 aimutil_get8(iptlv->value+2),
1116 aimutil_get8(iptlv->value+3),
1117 aimutil_get16(porttlv->value));
1118
1119 if (aim_gettlv(list2, 0x000c, 1))
1120 desc = aim_gettlv_str(list2, 0x000c, 1);
1121
1122 faimdprintf(sess, 0, "rend: file transfer request from %s: %s (%s)\n",
1123 userinfo->sn, desc, ip);
1124
1125 memcpy(cachedcook->cookie, args->cookie, 8);
1126
1127 ft = malloc(sizeof(struct aim_filetransfer_priv)); /* XXX */
1128 strncpy(ft->sn, userinfo.sn, sizeof(ft->sn));
1129 strncpy(ft->ip, ip, sizeof(ft->ip));
1130 strncpy(ft->fh.name, miscinfo->value+8, sizeof(ft->fh.name));
1131 cachedcook->type = AIM_COOKIETYPE_OFTSEND;
1132 cachedcook->data = ft;
1133
1134 if (aim_cachecookie(sess, cachedcook) == -1)
1135 faimdprintf(sess, 0, "ERROR caching message cookie\n");
1136
1137 aim_accepttransfer(sess, rx->conn, ft->sn, cookie, AIM_CAPS_SENDFILE);
1138
1139 if (desc)
1140 free(desc);
1141
1142 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1143 ret = userfunc(sess, rx, 0x0002, userinfo, &args);
1144#endif
1145 return 0;
1146}
1147
7b0bc7f2 1148static int incomingim_ch2_imimage(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_tlvlist_t *list2)
c5f5b7f1 1149{
1150 aim_rxcallback_t userfunc;
1151 int ret = 0;
1152
1153 /* Primary IP address */
1154 if (aim_gettlv(list2, 0x0003, 1)) {
1155 aim_tlv_t *tlv;
1156
1157 tlv = aim_gettlv(list2, 0x0003, 1);
1158
1159 snprintf(args->info.imimage.ip, sizeof(args->info.imimage.ip),
1160 "%d.%d.%d.%d:4443",
1161 tlv->value[0],
1162 tlv->value[1],
1163 tlv->value[2],
1164 tlv->value[3]);
1165 }
1166
1167 /*
1168 * Alternate IP address
1169 *
1170 * Sort of. The peer doesn't send this -- the OSCAR
1171 * server does. So it will be the IP address that the
1172 * peer is directly connected to the internet with, which
1173 * may not be the same as the IP above. If these two
1174 * values differ, it's rather unlikely that this
1175 * rendezvous is going to happen...
1176 *
1177 */
1178 if (aim_gettlv(list2, 0x0004, 1))
1179 ;
1180
1181 /* Port number (not correct -- ignore) */
1182 if (aim_gettlv(list2, 0x0005, 1))
1183 ;
1184
1185 /* Unknown -- two bytes = 0x0001 */
1186 if (aim_gettlv(list2, 0x000a, 1))
1187 ;
1188
1189 /* Unknown -- no value */
1190 if (aim_gettlv(list2, 0x000f, 1))
1191 ;
1192
1193 faimdprintf(sess, 1, "rend: directIM request from %s (%s)\n", userinfo->sn, args->info.imimage.ip);
1194
1195 /*
1196 * XXX: there are a couple of different request packets for
1197 * different things
1198 */
1199
1200 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1201 ret = userfunc(sess, rx, 0x0002, userinfo, args);
1202
1203 return ret;
1204}
1205
d410cf58 1206/* XXX Ugh. I think its obvious. */
7b0bc7f2 1207static int incomingim_ch2(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, fu16_t channel, aim_userinfo_t *userinfo, aim_tlvlist_t *tlvlist, fu8_t *cookie)
00ef5271 1208{
d410cf58 1209 aim_tlv_t *block1;
1210 aim_tlvlist_t *list2;
6ae3c48d 1211 int ret = 0;
1212 struct aim_incomingim_ch2_args args;
d410cf58 1213 aim_bstream_t bbs;
1214 fu8_t *cookie2;
6ae3c48d 1215
1216 memset(&args, 0, sizeof(args));
1217
1218 /*
1219 * There's another block of TLVs embedded in the type 5 here.
1220 */
355229fe 1221 if (!(block1 = aim_gettlv(tlvlist, 0x0005, 1)) || !block1->value) {
6ae3c48d 1222 faimdprintf(sess, 0, "no tlv 0x0005 in rendezvous transaction!\n");
1223 return 0;
1224 }
1225
d410cf58 1226 aim_bstream_init(&bbs, block1->value, block1->length);
1227
6ae3c48d 1228 /*
1229 * First two bytes represent the status of the connection.
1230 *
1231 * 0 is a request, 2 is an accept
1232 */
d410cf58 1233 args.status = aimbs_get16(&bbs);
6ae3c48d 1234
1235 /*
1236 * Next comes the cookie. Should match the ICBM cookie.
1237 */
d410cf58 1238 cookie2 = aimbs_getraw(&bbs, 8);
1239 if (memcmp(cookie, cookie2, 8) != 0)
6ae3c48d 1240 faimdprintf(sess, 0, "rend: warning cookies don't match!\n");
c5f5b7f1 1241 memcpy(args.cookie, cookie2, 8);
d410cf58 1242 free(cookie2);
6ae3c48d 1243
1244 /*
1245 * The next 16bytes are a capability block so we can
1246 * identify what type of rendezvous this is.
1247 *
1248 * Thanks to Eric Warmenhoven <warmenhoven@linux.com> (of GAIM)
1249 * for pointing some of this out to me. In fact, a lot of
1250 * the client-to-client info comes from the work of the GAIM
1251 * developers. Thanks!
1252 *
1253 * Read off one capability string and we should have it ID'd.
1254 *
1255 */
d410cf58 1256 if ((args.reqclass = aim_getcap(sess, &bbs, 0x10)) == 0x0000) {
6ae3c48d 1257 faimdprintf(sess, 0, "rend: no ID block\n");
1258 return 0;
1259 }
1260
1261 /*
70b889c5 1262 * What follows may be TLVs or nothing, depending on the
1263 * purpose of the message.
1264 *
1265 * Ack packets for instance have nothing more to them.
1266 */
d410cf58 1267 list2 = aim_readtlvchain(&bbs);
6ae3c48d 1268
c5f5b7f1 1269#if 0 /* this should be in the per-type blocks */
6ae3c48d 1270 if (!list2 || ((args.reqclass != AIM_CAPS_IMIMAGE) && !(aim_gettlv(list2, 0x2711, 1)))) {
d410cf58 1271 aim_msgcookie_t *cook;
6ae3c48d 1272 int type;
1273
1274 type = aim_msgcookie_gettype(args.reqclass); /* XXX: fix this shitty code */
1275
1276 if ((cook = aim_checkcookie(sess, cookie, type)) == NULL) {
1277 faimdprintf(sess, 0, "non-data rendezvous thats not in cache (type %d)\n", type);
d410cf58 1278 aim_freetlvchain(&list2);
1279 return 1;
6ae3c48d 1280 }
1281
1282 if (cook->type == AIM_COOKIETYPE_OFTGET) {
1283 struct aim_filetransfer_priv *ft;
1284
1285 if (cook->data) {
1286 int errorcode = -1; /* XXX shouldnt this be 0? */
1287
1288 ft = (struct aim_filetransfer_priv *)cook->data;
1289
1290 if (args.status != 0x0002) {
1291
d410cf58 1292 if (aim_gettlv(list2, 0x000b, 1))
1293 errorcode = aim_gettlv16(list2, 0x000b, 1);
6ae3c48d 1294
d410cf58 1295 /* XXX this should make it up to the client, you know.. */
1296 if (errorcode)
1297 faimdprintf(sess, 0, "transfer from %s (%s) for %s cancelled (error code %d)\n", ft->sn, ft->ip, ft->fh.name, errorcode);
6ae3c48d 1298 } /* args.status != 0x0002 */
d410cf58 1299
6ae3c48d 1300 } else {
1301 faimdprintf(sess, 0, "no data attached to file transfer\n");
1302 } /* !cook->data */
1303
1304 } else if (cook->type == AIM_CAPS_VOICE) {
1305
1306 faimdprintf(sess, 0, "voice request cancelled\n");
1307
1308 } else {
1309
1310 faimdprintf(sess, 0, "unknown cookie cache type %d\n", cook->type);
1311 }
1312
1313 aim_freetlvchain(&list2);
1314
1315 return 1;
00ef5271 1316 }
c5f5b7f1 1317#endif
6ae3c48d 1318
1319 /*
1320 * The rest of the handling depends on what type it is.
1321 */
8722e782 1322 if (args.reqclass & AIM_CAPS_BUDDYICON)
c5f5b7f1 1323 ret = incomingim_ch2_buddyicon(sess, mod, rx, snac, userinfo, &args, list2);
8722e782 1324 else if (args.reqclass & AIM_CAPS_SENDBUDDYLIST)
1325 ret = incomingim_ch2_buddylist(sess, mod, rx, snac, userinfo, &args, list2);
1326 else if (args.reqclass & AIM_CAPS_VOICE)
1327 ret = incomingim_ch2_voice(sess, mod, rx, snac, userinfo, &args, list2);
1328 else if (args.reqclass & AIM_CAPS_IMIMAGE)
c5f5b7f1 1329 ret = incomingim_ch2_imimage(sess, mod, rx, snac, userinfo, &args, list2);
8722e782 1330 else if (args.reqclass & AIM_CAPS_CHAT)
1331 ret = incomingim_ch2_chat(sess, mod, rx, snac, userinfo, &args, list2);
1332 else if (args.reqclass & AIM_CAPS_GETFILE)
1333 ret = incomingim_ch2_getfile(sess, mod, rx, snac, userinfo, &args, list2);
1334 else if (args.reqclass & AIM_CAPS_SENDFILE)
1335 ret = incomingim_ch2_sendfile(sess, mod, rx, snac, userinfo, &args, list2);
1336 else
6ae3c48d 1337 faimdprintf(sess, 0, "rend: unknown rendezvous 0x%04x\n", args.reqclass);
00ef5271 1338
6ae3c48d 1339 aim_freetlvchain(&list2);
00ef5271 1340
6ae3c48d 1341 return ret;
e5012450 1342}
1343
49c8a2fa 1344/*
1345 * It can easily be said that parsing ICBMs is THE single
1346 * most difficult thing to do in the in AIM protocol. In
1347 * fact, I think I just did say that.
1348 *
1349 * Below is the best damned solution I've come up with
1350 * over the past sixteen months of battling with it. This
1351 * can parse both away and normal messages from every client
1352 * I have access to. Its not fast, its not clean. But it works.
1353 *
49c8a2fa 1354 */
d410cf58 1355static int incomingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
9de3ca7e 1356{
6ae3c48d 1357 int i, ret = 0;
d410cf58 1358 fu8_t cookie[8];
1359 fu16_t channel;
7b0bc7f2 1360 aim_userinfo_t userinfo;
6ae3c48d 1361
7b0bc7f2 1362 memset(&userinfo, 0x00, sizeof(aim_userinfo_t));
6ae3c48d 1363
1364 /*
1365 * Read ICBM Cookie. And throw away.
1366 */
1367 for (i = 0; i < 8; i++)
d410cf58 1368 cookie[i] = aimbs_get8(bs);
6ae3c48d 1369
1370 /*
1371 * Channel ID.
1372 *
1373 * Channel 0x0001 is the message channel. There are
1374 * other channels for things called "rendevous"
1375 * which represent chat and some of the other new
1376 * features of AIM2/3/3.5.
1377 *
1378 * Channel 0x0002 is the Rendevous channel, which
1379 * is where Chat Invitiations and various client-client
1380 * connection negotiations come from.
1381 *
1382 */
d410cf58 1383 channel = aimbs_get16(bs);
6ae3c48d 1384
6ae3c48d 1385 /*
1386 * Extract the standard user info block.
1387 *
1388 * Note that although this contains TLVs that appear contiguous
1389 * with the TLVs read below, they are two different pieces. The
1390 * userinfo block contains the number of TLVs that contain user
1391 * information, the rest are not even though there is no seperation.
1392 * aim_extractuserinfo() returns the number of bytes used by the
1393 * userinfo tlvs, so you can start reading the rest of them right
1394 * afterward.
1395 *
1396 * That also means that TLV types can be duplicated between the
1397 * userinfo block and the rest of the message, however there should
1398 * never be two TLVs of the same type in one block.
1399 *
1400 */
d410cf58 1401 aim_extractuserinfo(sess, bs, &userinfo);
6ae3c48d 1402
1403 /*
1404 * From here on, its depends on what channel we're on.
1405 *
1406 * Technically all channels have a TLV list have this, however,
1407 * for the common channel 1 case, in-place parsing is used for
1408 * performance reasons (less memory allocation).
1409 */
1410 if (channel == 1) {
1411
d410cf58 1412 ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, bs, cookie);
6ae3c48d 1413
1414 } else if (channel == 0x0002) {
d410cf58 1415 aim_tlvlist_t *tlvlist;
6ae3c48d 1416
1417 /*
1418 * Read block of TLVs (not including the userinfo data). All
1419 * further data is derived from what is parsed here.
1420 */
d410cf58 1421 tlvlist = aim_readtlvchain(bs);
6ae3c48d 1422
1423 ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
1424
1425 /*
1426 * Free up the TLV chain.
1427 */
1428 aim_freetlvchain(&tlvlist);
c9f9223f 1429 } else {
1430 faimdprintf(sess, 0, "icbm: ICBM received on an unsupported channel. Ignoring.\n (chan = %04x)", channel);
1431 return 0;
6ae3c48d 1432 }
1433
1434 return ret;
49c8a2fa 1435}
1436
040457cc 1437/*
1438 * Possible codes:
1439 * AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
1440 * AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
1441 * AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
1442 *
1443 */
d410cf58 1444faim_export int aim_denytransfer(aim_session_t *sess, aim_conn_t *conn, const char *sender, const char *cookie, fu16_t code)
040457cc 1445{
d410cf58 1446 aim_frame_t *fr;
1447 aim_snacid_t snacid;
1448 aim_tlvlist_t *tl = NULL;
1449
1450 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sender)+6)))
6ae3c48d 1451 return -ENOMEM;
040457cc 1452
d410cf58 1453 snacid = aim_cachesnac(sess, 0x0004, 0x000b, 0x0000, NULL, 0);
1454 aim_putsnac(&fr->data, 0x0004, 0x000b, 0x0000, snacid);
1455
1456 aimbs_putraw(&fr->data, cookie, 8);
040457cc 1457
d410cf58 1458 aimbs_put16(&fr->data, 0x0002); /* channel */
1459 aimbs_put8(&fr->data, strlen(sender));
1460 aimbs_putraw(&fr->data, sender, strlen(sender));
040457cc 1461
d410cf58 1462 aim_addtlvtochain16(&tl, 0x0003, code);
1463 aim_writetlvchain(&fr->data, &tl);
1464 aim_freetlvchain(&tl);
1465
1466 aim_tx_enqueue(sess, fr);
040457cc 1467
6ae3c48d 1468 return 0;
040457cc 1469}
1470
49c8a2fa 1471/*
355229fe 1472 * aim_reqicbmparaminfo()
49c8a2fa 1473 *
355229fe 1474 * Request ICBM parameter information.
49c8a2fa 1475 *
1476 */
d410cf58 1477faim_export int aim_reqicbmparams(aim_session_t *sess, aim_conn_t *conn)
355229fe 1478{
1479 return aim_genericreq_n(sess, conn, 0x0004, 0x0004);
1480}
1481
1482/*
1483 *
d410cf58 1484 * I definitly recommend sending this. If you don't, you'll be stuck
1485 * with the rather unreasonable defaults. You don't want those. Send this.
1486 *
355229fe 1487 */
d410cf58 1488faim_export int aim_seticbmparam(aim_session_t *sess, aim_conn_t *conn, struct aim_icbmparameters *params)
49c8a2fa 1489{
d410cf58 1490 aim_frame_t *fr;
1491 aim_snacid_t snacid;
49c8a2fa 1492
355229fe 1493 if (!sess || !conn || !params)
1494 return -EINVAL;
1495
d410cf58 1496 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+16)))
6ae3c48d 1497 return -ENOMEM;
5b79dc93 1498
d410cf58 1499 snacid = aim_cachesnac(sess, 0x0004, 0x0002, 0x0000, NULL, 0);
1500 aim_putsnac(&fr->data, 0x0004, 0x0002, 0x0000, snacid);
5b79dc93 1501
d410cf58 1502 /* This is read-only (see Parameter Reply). Must be set to zero here. */
1503 aimbs_put16(&fr->data, 0x0000);
355229fe 1504
1505 /* These are all read-write */
d410cf58 1506 aimbs_put32(&fr->data, params->flags);
1507 aimbs_put16(&fr->data, params->maxmsglen);
1508 aimbs_put16(&fr->data, params->maxsenderwarn);
1509 aimbs_put16(&fr->data, params->maxrecverwarn);
1510 aimbs_put32(&fr->data, params->minmsginterval);
5b79dc93 1511
d410cf58 1512 aim_tx_enqueue(sess, fr);
a25832e6 1513
6ae3c48d 1514 return 0;
a25832e6 1515}
1516
d410cf58 1517static int paraminfo(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
a25832e6 1518{
355229fe 1519 struct aim_icbmparameters params;
6ae3c48d 1520 aim_rxcallback_t userfunc;
e6b05d80 1521
d410cf58 1522 params.maxchan = aimbs_get16(bs);
1523 params.flags = aimbs_get32(bs);
1524 params.maxmsglen = aimbs_get16(bs);
1525 params.maxsenderwarn = aimbs_get16(bs);
1526 params.maxrecverwarn = aimbs_get16(bs);
1527 params.minmsginterval = aimbs_get32(bs);
1528
6ae3c48d 1529 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
355229fe 1530 return userfunc(sess, rx, &params);
00ef5271 1531
6ae3c48d 1532 return 0;
00ef5271 1533}
1534
d410cf58 1535static int missedcall(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
96f8b1ed 1536{
d410cf58 1537 int ret = 0;
6ae3c48d 1538 aim_rxcallback_t userfunc;
d410cf58 1539 fu16_t channel, nummissed, reason;
7b0bc7f2 1540 aim_userinfo_t userinfo;
6ae3c48d 1541
d410cf58 1542 while (aim_bstream_empty(bs)) {
6ae3c48d 1543
d410cf58 1544 channel = aimbs_get16(bs);
1545 aim_extractuserinfo(sess, bs, &userinfo);
1546 nummissed = aimbs_get16(bs);
1547 reason = aimbs_get16(bs);
6ae3c48d 1548
36a61e0d 1549 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1550 ret = userfunc(sess, rx, channel, &userinfo, nummissed, reason);
1551 }
6ae3c48d 1552
36a61e0d 1553 return ret;
00ef5271 1554}
1555
d410cf58 1556static int clienterr(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
00ef5271 1557{
d410cf58 1558 int ret = 0;
6ae3c48d 1559 aim_rxcallback_t userfunc;
d410cf58 1560 fu16_t channel, reason;
1561 char *sn;
1562 fu8_t *ck, snlen;
00ef5271 1563
d410cf58 1564 ck = aimbs_getraw(bs, 8);
1565 channel = aimbs_get16(bs);
1566 snlen = aimbs_get8(bs);
1567 sn = aimbs_getstr(bs, snlen);
1568 reason = aimbs_get16(bs);
00ef5271 1569
d410cf58 1570 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1571 ret = userfunc(sess, rx, channel, sn, reason);
00ef5271 1572
1b0f0322 1573 free(ck);
1574 free(sn);
1575
d410cf58 1576 return ret;
1577}
00ef5271 1578
d410cf58 1579static int msgack(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1580{
1581 aim_rxcallback_t userfunc;
1582 fu16_t type;
1583 fu8_t snlen, *ck;
1584 char *sn;
1b0f0322 1585 int ret = 0;
d410cf58 1586
1587 ck = aimbs_getraw(bs, 8);
1588 type = aimbs_get16(bs);
1589 snlen = aimbs_get8(bs);
1590 sn = aimbs_getstr(bs, snlen);
00ef5271 1591
6ae3c48d 1592 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1b0f0322 1593 ret = userfunc(sess, rx, type, sn);
00ef5271 1594
d410cf58 1595 free(sn);
1596 free(ck);
1597
1b0f0322 1598 return ret;
00ef5271 1599}
1600
d410cf58 1601static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
00ef5271 1602{
1603
6ae3c48d 1604 if (snac->subtype == 0x0005)
d410cf58 1605 return paraminfo(sess, mod, rx, snac, bs);
6ae3c48d 1606 else if (snac->subtype == 0x0006)
d410cf58 1607 return outgoingim(sess, mod, rx, snac, bs);
6ae3c48d 1608 else if (snac->subtype == 0x0007)
d410cf58 1609 return incomingim(sess, mod, rx, snac, bs);
6ae3c48d 1610 else if (snac->subtype == 0x000a)
d410cf58 1611 return missedcall(sess, mod, rx, snac, bs);
1612 else if (snac->subtype == 0x000b)
1613 return clienterr(sess, mod, rx, snac, bs);
6ae3c48d 1614 else if (snac->subtype == 0x000c)
d410cf58 1615 return msgack(sess, mod, rx, snac, bs);
6ae3c48d 1616
1617 return 0;
00ef5271 1618}
1619
d410cf58 1620faim_internal int msg_modfirst(aim_session_t *sess, aim_module_t *mod)
00ef5271 1621{
1622
6ae3c48d 1623 mod->family = 0x0004;
1624 mod->version = 0x0000;
1625 mod->flags = 0;
1626 strncpy(mod->name, "messaging", sizeof(mod->name));
1627 mod->snachandler = snachandler;
00ef5271 1628
6ae3c48d 1629 return 0;
96f8b1ed 1630}
This page took 1.925476 seconds and 5 git commands to generate.