]>
Commit | Line | Data |
---|---|---|
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 | 46 | faim_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 | 87 | faim_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 ߪ 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 | * | |
141 | * XXX support multipart | |
142 | * | |
9de3ca7e | 143 | */ |
d410cf58 | 144 | faim_export int aim_send_im_ext(aim_session_t *sess, aim_conn_t *conn, struct aim_sendimext_args *args) |
a2244dd9 | 145 | { |
84e0ca17 | 146 | static const fu8_t deffeatures[] = { |
147 | 0x01, 0x01, 0x01, 0x02, 0x42, | |
148 | }; | |
149 | int i, msgtlvlen; | |
d410cf58 | 150 | aim_frame_t *fr; |
151 | aim_snacid_t snacid; | |
2d5fd943 | 152 | |
153 | if (!sess || !conn || !args) | |
d410cf58 | 154 | return -EINVAL; |
2d5fd943 | 155 | |
156 | if (!args->msg || (args->msglen <= 0)) | |
157 | return -EINVAL; | |
158 | ||
159 | if (args->msglen >= MAXMSGLEN) | |
160 | return -E2BIG; | |
161 | ||
84e0ca17 | 162 | msgtlvlen = 12 + args->msglen; |
163 | if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES) | |
164 | msgtlvlen += args->featureslen; | |
165 | else | |
166 | msgtlvlen += sizeof(deffeatures); | |
167 | ||
d410cf58 | 168 | if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, args->msglen+512))) |
2d5fd943 | 169 | return -ENOMEM; |
170 | ||
d410cf58 | 171 | /* XXX should be optional */ |
172 | snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, args->destsn, strlen(args->destsn)+1); | |
173 | aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid); | |
2d5fd943 | 174 | |
175 | /* | |
176 | * Generate a random message cookie | |
177 | * | |
178 | * We could cache these like we do SNAC IDs. (In fact, it | |
179 | * might be a good idea.) In the message error functions, | |
180 | * the 8byte message cookie is returned as well as the | |
181 | * SNAC ID. | |
182 | * | |
183 | */ | |
d410cf58 | 184 | for (i = 0; i < 8; i++) |
185 | aimbs_put8(&fr->data, (fu8_t) rand()); | |
2d5fd943 | 186 | |
187 | /* | |
188 | * Channel ID | |
189 | */ | |
d410cf58 | 190 | aimbs_put16(&fr->data, 0x0001); |
2d5fd943 | 191 | |
192 | /* | |
193 | * Destination SN (prepended with byte length) | |
194 | */ | |
d410cf58 | 195 | aimbs_put8(&fr->data, strlen(args->destsn)); |
196 | aimbs_putraw(&fr->data, args->destsn, strlen(args->destsn)); | |
2d5fd943 | 197 | |
198 | /* | |
199 | * metaTLV start. | |
200 | */ | |
d410cf58 | 201 | aimbs_put16(&fr->data, 0x0002); |
84e0ca17 | 202 | aimbs_put16(&fr->data, msgtlvlen); |
2d5fd943 | 203 | |
204 | /* | |
84e0ca17 | 205 | * Features |
2d5fd943 | 206 | * |
207 | */ | |
d410cf58 | 208 | aimbs_put8(&fr->data, 0x05); |
209 | aimbs_put8(&fr->data, 0x01); | |
2d5fd943 | 210 | |
84e0ca17 | 211 | if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES) { |
212 | aimbs_put16(&fr->data, args->featureslen); | |
213 | aimbs_putraw(&fr->data, args->features, args->featureslen); | |
214 | } else { | |
215 | aimbs_put16(&fr->data, sizeof(deffeatures)); | |
216 | aimbs_putraw(&fr->data, deffeatures, sizeof(deffeatures)); | |
217 | } | |
2d5fd943 | 218 | |
d410cf58 | 219 | aimbs_put16(&fr->data, 0x0101); |
2d5fd943 | 220 | |
221 | /* | |
222 | * Message block length. | |
223 | */ | |
d410cf58 | 224 | aimbs_put16(&fr->data, args->msglen + 0x04); |
2d5fd943 | 225 | |
226 | /* | |
227 | * Character set. | |
228 | */ | |
229 | if (args->flags & AIM_IMFLAGS_UNICODE) | |
d410cf58 | 230 | aimbs_put16(&fr->data, 0x0002); |
2d5fd943 | 231 | else if (args->flags & AIM_IMFLAGS_ISO_8859_1) |
d410cf58 | 232 | aimbs_put16(&fr->data, 0x0003); |
2d5fd943 | 233 | else |
d410cf58 | 234 | aimbs_put16(&fr->data, 0x0000); |
2d5fd943 | 235 | |
d410cf58 | 236 | aimbs_put16(&fr->data, 0x0000); |
2d5fd943 | 237 | |
238 | /* | |
239 | * Message. Not terminated. | |
240 | */ | |
d410cf58 | 241 | aimbs_putraw(&fr->data, args->msg, args->msglen); |
2d5fd943 | 242 | |
243 | /* | |
244 | * Set the Request Acknowledge flag. | |
245 | */ | |
246 | if (args->flags & AIM_IMFLAGS_ACK) { | |
d410cf58 | 247 | aimbs_put16(&fr->data, 0x0003); |
248 | aimbs_put16(&fr->data, 0x0000); | |
2d5fd943 | 249 | } |
d410cf58 | 250 | |
2d5fd943 | 251 | /* |
252 | * Set the Autoresponse flag. | |
253 | */ | |
254 | if (args->flags & AIM_IMFLAGS_AWAY) { | |
d410cf58 | 255 | aimbs_put16(&fr->data, 0x0004); |
256 | aimbs_put16(&fr->data, 0x0000); | |
2d5fd943 | 257 | } |
258 | ||
259 | /* | |
260 | * Set the Buddy Icon Requested flag. | |
261 | */ | |
262 | if (args->flags & AIM_IMFLAGS_BUDDYREQ) { | |
d410cf58 | 263 | aimbs_put16(&fr->data, 0x0009); |
264 | aimbs_put16(&fr->data, 0x0000); | |
2d5fd943 | 265 | } |
266 | ||
267 | /* | |
d410cf58 | 268 | * Set the I HAVE A REALLY PURTY ICON flag. |
2d5fd943 | 269 | */ |
270 | if (args->flags & AIM_IMFLAGS_HASICON) { | |
e1b8c575 | 271 | aimbs_put16(&fr->data, 0x0008); |
d410cf58 | 272 | aimbs_put16(&fr->data, 0x000c); |
273 | aimbs_put32(&fr->data, args->iconlen); | |
e1b8c575 | 274 | aimbs_put32(&fr->data, args->iconsum); |
d410cf58 | 275 | aimbs_put32(&fr->data, args->iconstamp); |
2d5fd943 | 276 | } |
277 | ||
d410cf58 | 278 | aim_tx_enqueue(sess, fr); |
49c8a2fa | 279 | |
7b91722d | 280 | #if 1 /* XXX do this with autoconf or something... */ |
2d5fd943 | 281 | aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */ |
7b91722d | 282 | #endif |
9de3ca7e | 283 | |
2d5fd943 | 284 | return 0; |
9de3ca7e | 285 | } |
286 | ||
7b91722d | 287 | /* |
288 | * Simple wrapper for aim_send_im_ext() | |
289 | * | |
290 | * You cannot use aim_send_im if you need the HASICON flag. You must | |
291 | * use aim_send_im_ext directly for that. | |
292 | * | |
293 | * aim_send_im also cannot be used if you require UNICODE messages, because | |
294 | * that requires an explicit message length. Use aim_send_im_ext(). | |
295 | * | |
296 | */ | |
d410cf58 | 297 | faim_export int aim_send_im(aim_session_t *sess, aim_conn_t *conn, const char *destsn, fu16_t flags, const char *msg) |
7b91722d | 298 | { |
2d5fd943 | 299 | struct aim_sendimext_args args; |
7b91722d | 300 | |
2d5fd943 | 301 | args.destsn = destsn; |
302 | args.flags = flags; | |
303 | args.msg = msg; | |
304 | args.msglen = strlen(msg); | |
7b91722d | 305 | |
84e0ca17 | 306 | /* Make these don't get set by accident -- they need aim_send_im_ext */ |
307 | args.flags &= ~(AIM_IMFLAGS_CUSTOMFEATURES | AIM_IMFLAGS_HASICON); | |
308 | ||
2d5fd943 | 309 | return aim_send_im_ext(sess, conn, &args); |
7b91722d | 310 | } |
311 | ||
d410cf58 | 312 | /* |
313 | * This is also performance sensative. (If you can believe it...) | |
314 | * | |
315 | */ | |
e1b8c575 | 316 | faim_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 | 317 | { |
d410cf58 | 318 | int i; |
319 | fu8_t ck[8]; | |
320 | aim_frame_t *fr; | |
321 | aim_snacid_t snacid; | |
6ae3c48d | 322 | |
d410cf58 | 323 | if (!sess || !conn || !sn || !icon || (iconlen <= 0) || (iconlen >= MAXICONLEN)) |
324 | return -EINVAL; | |
6ae3c48d | 325 | |
326 | if (conn->type != AIM_CONN_TYPE_BOS) | |
327 | return -EINVAL; | |
328 | ||
d410cf58 | 329 | for (i = 0; i < 8; i++) |
330 | aimutil_put8(ck+i, (fu8_t) rand()); | |
6ae3c48d | 331 | |
d410cf58 | 332 | 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 | 333 | return -ENOMEM; |
334 | ||
d410cf58 | 335 | snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0); |
336 | aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid); | |
6ae3c48d | 337 | |
338 | /* | |
339 | * Cookie | |
340 | */ | |
d410cf58 | 341 | aimbs_putraw(&fr->data, ck, 8); |
6ae3c48d | 342 | |
343 | /* | |
344 | * Channel (2) | |
345 | */ | |
d410cf58 | 346 | aimbs_put16(&fr->data, 0x0002); |
6ae3c48d | 347 | |
348 | /* | |
349 | * Dest sn | |
350 | */ | |
d410cf58 | 351 | aimbs_put8(&fr->data, strlen(sn)); |
352 | aimbs_putraw(&fr->data, sn, strlen(sn)); | |
6ae3c48d | 353 | |
354 | /* | |
355 | * TLV t(0005) | |
d410cf58 | 356 | * |
357 | * Encompasses everything below. | |
6ae3c48d | 358 | */ |
d410cf58 | 359 | aimbs_put16(&fr->data, 0x0005); |
360 | aimbs_put16(&fr->data, 2+8+16+6+4+4+iconlen+4+4+4+strlen(AIM_ICONIDENT)); | |
6ae3c48d | 361 | |
d410cf58 | 362 | aimbs_put16(&fr->data, 0x0000); |
363 | aimbs_putraw(&fr->data, ck, 8); | |
364 | aim_putcap(&fr->data, AIM_CAPS_BUDDYICON); | |
6ae3c48d | 365 | |
366 | /* TLV t(000a) */ | |
d410cf58 | 367 | aimbs_put16(&fr->data, 0x000a); |
368 | aimbs_put16(&fr->data, 0x0002); | |
369 | aimbs_put16(&fr->data, 0x0001); | |
6ae3c48d | 370 | |
371 | /* TLV t(000f) */ | |
d410cf58 | 372 | aimbs_put16(&fr->data, 0x000f); |
373 | aimbs_put16(&fr->data, 0x0000); | |
6ae3c48d | 374 | |
375 | /* TLV t(2711) */ | |
d410cf58 | 376 | aimbs_put16(&fr->data, 0x2711); |
377 | aimbs_put16(&fr->data, 4+4+4+iconlen+strlen(AIM_ICONIDENT)); | |
e1b8c575 | 378 | aimbs_put32(&fr->data, iconsum); |
d410cf58 | 379 | aimbs_put32(&fr->data, iconlen); |
380 | aimbs_put32(&fr->data, stamp); | |
381 | aimbs_putraw(&fr->data, icon, iconlen); | |
382 | aimbs_putraw(&fr->data, AIM_ICONIDENT, strlen(AIM_ICONIDENT)); | |
6ae3c48d | 383 | |
384 | /* TLV t(0003) */ | |
d410cf58 | 385 | aimbs_put16(&fr->data, 0x0003); |
386 | aimbs_put16(&fr->data, 0x0000); | |
6ae3c48d | 387 | |
d410cf58 | 388 | aim_tx_enqueue(sess, fr); |
6ae3c48d | 389 | |
390 | return 0; | |
7b91722d | 391 | } |
392 | ||
d410cf58 | 393 | static int outgoingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs) |
e5012450 | 394 | { |
d410cf58 | 395 | int i, ret = 0; |
6ae3c48d | 396 | aim_rxcallback_t userfunc; |
d410cf58 | 397 | fu8_t cookie[8]; |
398 | fu16_t channel; | |
399 | aim_tlvlist_t *tlvlist; | |
400 | char *sn; | |
401 | int snlen; | |
402 | fu16_t icbmflags = 0; | |
403 | fu8_t flag1 = 0, flag2 = 0; | |
404 | fu8_t *msg = NULL; | |
405 | aim_tlv_t *msgblock; | |
6ae3c48d | 406 | |
407 | /* ICBM Cookie. */ | |
408 | for (i = 0; i < 8; i++) | |
d410cf58 | 409 | cookie[i] = aimbs_get8(bs); |
6ae3c48d | 410 | |
411 | /* Channel ID */ | |
d410cf58 | 412 | channel = aimbs_get16(bs); |
6ae3c48d | 413 | |
414 | if (channel != 0x01) { | |
415 | faimdprintf(sess, 0, "icbm: ICBM recieved on unsupported channel. Ignoring. (chan = %04x)\n", channel); | |
d410cf58 | 416 | return 0; |
6ae3c48d | 417 | } |
418 | ||
d410cf58 | 419 | snlen = aimbs_get8(bs); |
420 | sn = aimbs_getstr(bs, snlen); | |
6ae3c48d | 421 | |
d410cf58 | 422 | tlvlist = aim_readtlvchain(bs); |
6ae3c48d | 423 | |
424 | if (aim_gettlv(tlvlist, 0x0003, 1)) | |
425 | icbmflags |= AIM_IMFLAGS_ACK; | |
426 | if (aim_gettlv(tlvlist, 0x0004, 1)) | |
427 | icbmflags |= AIM_IMFLAGS_AWAY; | |
e5012450 | 428 | |
d410cf58 | 429 | if ((msgblock = aim_gettlv(tlvlist, 0x0002, 1))) { |
430 | aim_bstream_t mbs; | |
431 | int featurelen, msglen; | |
432 | ||
433 | aim_bstream_init(&mbs, msgblock->value, msgblock->length); | |
6ae3c48d | 434 | |
d410cf58 | 435 | aimbs_get8(&mbs); |
436 | aimbs_get8(&mbs); | |
437 | for (featurelen = aimbs_get16(&mbs); featurelen; featurelen--) | |
438 | aimbs_get8(&mbs); | |
439 | aimbs_get8(&mbs); | |
440 | aimbs_get8(&mbs); | |
6ae3c48d | 441 | |
d410cf58 | 442 | msglen = aimbs_get16(&mbs) - 4; /* final block length */ |
6ae3c48d | 443 | |
d410cf58 | 444 | flag1 = aimbs_get16(&mbs); |
445 | flag2 = aimbs_get16(&mbs); | |
6ae3c48d | 446 | |
d410cf58 | 447 | msg = aimbs_getstr(&mbs, msglen); |
6ae3c48d | 448 | } |
449 | ||
450 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) | |
451 | ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2); | |
452 | ||
d410cf58 | 453 | free(sn); |
6ae3c48d | 454 | aim_freetlvchain(&tlvlist); |
455 | ||
456 | return ret; | |
00ef5271 | 457 | } |
458 | ||
6ae3c48d | 459 | /* |
d410cf58 | 460 | * |
461 | * This should use tlvlists, but doesn't for performance reasons. | |
462 | * | |
463 | * XXX support multipart IMs: | |
6ae3c48d | 464 | * |
465 | * 0004 0007 0000 8f08 d295 | |
466 | * 0031 6520 3b7b f9fd | |
467 | * 0001 | |
468 | * 06 XXXX XXXX XXXX | |
469 | * 0000 | |
470 | * 0004 | |
471 | * 0001 0002 0004 | |
472 | * 0010 0004 0000 01a3 | |
473 | * 0002 0004 3ab6 94fa | |
474 | * 0003 0004 3b7b f85a | |
475 | * 0002 003c | |
476 | * 0501 0001 01 | |
477 | * 0101 000a 0000 0000 3c48 544d 4c3e ASCII part | |
478 | * ISO-8859 part: | |
479 | * 0101 0016 0003 0000 6c6b 7364 6a6b 6c6a 676c a56b 3b73 646a 6b6a | |
480 | * 0101 000b 0000 0000 3c2f 4854 4d4c 3e another ASCII part | |
481 | * | |
482 | */ | |
d410cf58 | 483 | static int incomingim_ch1(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, fu16_t channel, struct aim_userinfo_s *userinfo, aim_bstream_t *bs, fu8_t *cookie) |
00ef5271 | 484 | { |
d410cf58 | 485 | fu16_t type, length; |
1dcd23dd | 486 | aim_rxcallback_t userfunc; |
d410cf58 | 487 | int ret = 0; |
1dcd23dd | 488 | struct aim_incomingim_ch1_args args; |
d410cf58 | 489 | int endpos; |
00ef5271 | 490 | |
1dcd23dd | 491 | memset(&args, 0, sizeof(args)); |
00ef5271 | 492 | |
1dcd23dd | 493 | /* |
494 | * This used to be done using tlvchains. For performance reasons, | |
495 | * I've changed it to process the TLVs in-place. This avoids lots | |
496 | * of per-IM memory allocations. | |
497 | */ | |
d410cf58 | 498 | while (aim_bstream_empty(bs)) { |
499 | ||
500 | type = aimbs_get16(bs); | |
501 | length = aimbs_get16(bs); | |
1dcd23dd | 502 | |
d410cf58 | 503 | endpos = aim_bstream_curpos(bs) + length; |
1dcd23dd | 504 | |
505 | if (type == 0x0002) { /* Message Block */ | |
1dcd23dd | 506 | |
1dcd23dd | 507 | /* |
d410cf58 | 508 | * This TLV consists of the following: |
509 | * - 0501 -- Unknown | |
510 | * - Features: Don't know how to interpret these | |
511 | * - 0101 -- Unknown | |
512 | * - Message | |
1dcd23dd | 513 | * |
514 | */ | |
515 | ||
d410cf58 | 516 | aimbs_get8(bs); /* 05 */ |
517 | aimbs_get8(bs); /* 01 */ | |
84e0ca17 | 518 | |
519 | args.featureslen = aimbs_get16(bs); | |
520 | /* XXX XXX this is all evil! */ | |
521 | args.features = bs->data + bs->offset; | |
522 | aim_bstream_advance(bs, args.featureslen); | |
523 | args.icbmflags |= AIM_IMFLAGS_CUSTOMFEATURES; | |
524 | ||
d410cf58 | 525 | aimbs_get8(bs); /* 01 */ |
526 | aimbs_get8(bs); /* 01 */ | |
1dcd23dd | 527 | |
528 | /* Message string length, including flag words. */ | |
d410cf58 | 529 | args.msglen = aimbs_get16(bs); |
1dcd23dd | 530 | |
531 | /* Flag words. */ | |
d410cf58 | 532 | args.flag1 = aimbs_get16(bs); |
1dcd23dd | 533 | if (args.flag1 == 0x0000) |
534 | ; /* ASCII */ | |
535 | else if (args.flag1 == 0x0002) | |
536 | args.icbmflags |= AIM_IMFLAGS_UNICODE; | |
537 | else if (args.flag1 == 0x0003) | |
538 | args.icbmflags |= AIM_IMFLAGS_ISO_8859_1; | |
539 | else if (args.flag1 == 0xffff) | |
540 | ; /* no encoding (yeep!) */ | |
1dcd23dd | 541 | |
d410cf58 | 542 | args.flag2 = aimbs_get16(bs); |
1dcd23dd | 543 | if (args.flag2 == 0x0000) |
544 | ; /* standard subencoding? */ | |
545 | else if (args.flag2 == 0x000b) | |
546 | args.icbmflags |= AIM_IMFLAGS_SUBENC_MACINTOSH; | |
547 | else if (args.flag2 == 0xffff) | |
548 | ; /* no subencoding */ | |
d410cf58 | 549 | |
550 | /* XXX this isn't really necesary... */ | |
1dcd23dd | 551 | if ( ((args.flag1 != 0x0000) && |
552 | (args.flag1 != 0x0002) && | |
553 | (args.flag1 != 0x0003) && | |
554 | (args.flag1 != 0xffff)) || | |
555 | ((args.flag2 != 0x0000) && | |
556 | (args.flag2 != 0x000b) && | |
557 | (args.flag2 != 0xffff))) { | |
558 | faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", args.flag1, args.flag2); | |
559 | } | |
560 | ||
d410cf58 | 561 | /* Message. */ |
1dcd23dd | 562 | args.msglen -= 4; |
563 | if (args.icbmflags & AIM_IMFLAGS_UNICODE) { | |
d410cf58 | 564 | fu8_t *umsg; |
565 | ||
566 | /* Can't use getstr because of wide null */ | |
567 | umsg = aimbs_getraw(bs, args.msglen); | |
1dcd23dd | 568 | args.msg = malloc(args.msglen+2); |
d410cf58 | 569 | memcpy(args.msg, umsg, args.msglen); |
1dcd23dd | 570 | args.msg[args.msglen] = '\0'; /* wide NULL */ |
571 | args.msg[args.msglen+1] = '\0'; | |
d410cf58 | 572 | |
573 | free(umsg); | |
574 | ||
575 | } else | |
576 | args.msg = aimbs_getstr(bs, args.msglen); | |
1dcd23dd | 577 | |
578 | } else if (type == 0x0003) { /* Server Ack Requested */ | |
579 | ||
580 | args.icbmflags |= AIM_IMFLAGS_ACK; | |
581 | ||
582 | } else if (type == 0x0004) { /* Message is Auto Response */ | |
583 | ||
584 | args.icbmflags |= AIM_IMFLAGS_AWAY; | |
585 | ||
d410cf58 | 586 | } else if (type == 0x0008) { /* I-HAVE-A-REALLY-PURTY-ICON Flag */ |
1dcd23dd | 587 | |
84e0ca17 | 588 | args.iconsum = aimbs_get32(bs); |
589 | args.iconlen = aimbs_get32(bs); | |
d410cf58 | 590 | args.iconstamp = aimbs_get32(bs); |
1dcd23dd | 591 | args.icbmflags |= AIM_IMFLAGS_HASICON; |
592 | ||
593 | } else if (type == 0x0009) { | |
594 | ||
595 | args.icbmflags |= AIM_IMFLAGS_BUDDYREQ; | |
596 | ||
597 | } else if (type == 0x0017) { | |
598 | ||
599 | args.extdatalen = length; | |
d410cf58 | 600 | args.extdata = aimbs_getraw(bs, args.extdatalen); |
1dcd23dd | 601 | |
602 | } else { | |
6ae3c48d | 603 | faimdprintf(sess, 0, "incomingim_ch1: unknown TLV 0x%04x (len %d)\n", type, length); |
1dcd23dd | 604 | } |
605 | ||
d410cf58 | 606 | /* |
607 | * This is here to protect ourselves from ourselves. That | |
608 | * is, if something above doesn't completly parse its value | |
609 | * section, or, worse, overparses it, this will set the | |
610 | * stream where it needs to be in order to land on the next | |
611 | * TLV when the loop continues. | |
612 | * | |
613 | */ | |
614 | aim_bstream_setpos(bs, endpos); | |
1dcd23dd | 615 | } |
00ef5271 | 616 | |
00ef5271 | 617 | |
1dcd23dd | 618 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) |
619 | ret = userfunc(sess, rx, channel, userinfo, &args); | |
00ef5271 | 620 | |
d410cf58 | 621 | free(args.extdata); |
1dcd23dd | 622 | free(args.msg); |
00ef5271 | 623 | |
1dcd23dd | 624 | return ret; |
00ef5271 | 625 | } |
626 | ||
c5f5b7f1 | 627 | static int incomingim_ch2_buddyicon(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, struct aim_userinfo_s *userinfo, struct aim_incomingim_ch2_args *args, aim_tlvlist_t *list2) |
628 | { | |
629 | aim_rxcallback_t userfunc; | |
630 | int ret = 0; | |
631 | aim_tlv_t *miscinfo; | |
632 | aim_bstream_t tbs; | |
633 | ||
634 | miscinfo = aim_gettlv(list2, 0x2711, 1); | |
635 | aim_bstream_init(&tbs, miscinfo->value, miscinfo->length); | |
636 | ||
637 | args->info.icon.checksum = aimbs_get32(&tbs); | |
638 | args->info.icon.length = aimbs_get32(&tbs); | |
639 | args->info.icon.timestamp = aimbs_get32(&tbs); | |
640 | args->info.icon.icon = aimbs_getraw(&tbs, args->info.icon.length); | |
641 | ||
642 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) | |
643 | ret = userfunc(sess, rx, 0x0002, userinfo, args); | |
644 | ||
645 | free(args->info.icon.icon); | |
646 | ||
647 | return ret; | |
648 | } | |
649 | ||
650 | static int incomingim_ch2_imimage(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, struct aim_userinfo_s *userinfo, struct aim_incomingim_ch2_args *args, aim_tlvlist_t *list2) | |
651 | { | |
652 | aim_rxcallback_t userfunc; | |
653 | int ret = 0; | |
654 | ||
655 | /* Primary IP address */ | |
656 | if (aim_gettlv(list2, 0x0003, 1)) { | |
657 | aim_tlv_t *tlv; | |
658 | ||
659 | tlv = aim_gettlv(list2, 0x0003, 1); | |
660 | ||
661 | snprintf(args->info.imimage.ip, sizeof(args->info.imimage.ip), | |
662 | "%d.%d.%d.%d:4443", | |
663 | tlv->value[0], | |
664 | tlv->value[1], | |
665 | tlv->value[2], | |
666 | tlv->value[3]); | |
667 | } | |
668 | ||
669 | /* | |
670 | * Alternate IP address | |
671 | * | |
672 | * Sort of. The peer doesn't send this -- the OSCAR | |
673 | * server does. So it will be the IP address that the | |
674 | * peer is directly connected to the internet with, which | |
675 | * may not be the same as the IP above. If these two | |
676 | * values differ, it's rather unlikely that this | |
677 | * rendezvous is going to happen... | |
678 | * | |
679 | */ | |
680 | if (aim_gettlv(list2, 0x0004, 1)) | |
681 | ; | |
682 | ||
683 | /* Port number (not correct -- ignore) */ | |
684 | if (aim_gettlv(list2, 0x0005, 1)) | |
685 | ; | |
686 | ||
687 | /* Unknown -- two bytes = 0x0001 */ | |
688 | if (aim_gettlv(list2, 0x000a, 1)) | |
689 | ; | |
690 | ||
691 | /* Unknown -- no value */ | |
692 | if (aim_gettlv(list2, 0x000f, 1)) | |
693 | ; | |
694 | ||
695 | faimdprintf(sess, 1, "rend: directIM request from %s (%s)\n", userinfo->sn, args->info.imimage.ip); | |
696 | ||
697 | /* | |
698 | * XXX: there are a couple of different request packets for | |
699 | * different things | |
700 | */ | |
701 | ||
702 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) | |
703 | ret = userfunc(sess, rx, 0x0002, userinfo, args); | |
704 | ||
705 | return ret; | |
706 | } | |
707 | ||
d410cf58 | 708 | /* XXX Ugh. I think its obvious. */ |
709 | static int incomingim_ch2(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, fu16_t channel, struct aim_userinfo_s *userinfo, aim_tlvlist_t *tlvlist, fu8_t *cookie) | |
00ef5271 | 710 | { |
6ae3c48d | 711 | aim_rxcallback_t userfunc; |
d410cf58 | 712 | aim_tlv_t *block1; |
713 | aim_tlvlist_t *list2; | |
6ae3c48d | 714 | int ret = 0; |
715 | struct aim_incomingim_ch2_args args; | |
d410cf58 | 716 | aim_bstream_t bbs; |
717 | fu8_t *cookie2; | |
6ae3c48d | 718 | |
719 | memset(&args, 0, sizeof(args)); | |
720 | ||
721 | /* | |
722 | * There's another block of TLVs embedded in the type 5 here. | |
723 | */ | |
355229fe | 724 | if (!(block1 = aim_gettlv(tlvlist, 0x0005, 1)) || !block1->value) { |
6ae3c48d | 725 | faimdprintf(sess, 0, "no tlv 0x0005 in rendezvous transaction!\n"); |
726 | return 0; | |
727 | } | |
728 | ||
d410cf58 | 729 | aim_bstream_init(&bbs, block1->value, block1->length); |
730 | ||
6ae3c48d | 731 | /* |
732 | * First two bytes represent the status of the connection. | |
733 | * | |
734 | * 0 is a request, 2 is an accept | |
735 | */ | |
d410cf58 | 736 | args.status = aimbs_get16(&bbs); |
6ae3c48d | 737 | |
738 | /* | |
739 | * Next comes the cookie. Should match the ICBM cookie. | |
740 | */ | |
d410cf58 | 741 | cookie2 = aimbs_getraw(&bbs, 8); |
742 | if (memcmp(cookie, cookie2, 8) != 0) | |
6ae3c48d | 743 | faimdprintf(sess, 0, "rend: warning cookies don't match!\n"); |
c5f5b7f1 | 744 | memcpy(args.cookie, cookie2, 8); |
d410cf58 | 745 | free(cookie2); |
6ae3c48d | 746 | |
747 | /* | |
748 | * The next 16bytes are a capability block so we can | |
749 | * identify what type of rendezvous this is. | |
750 | * | |
751 | * Thanks to Eric Warmenhoven <warmenhoven@linux.com> (of GAIM) | |
752 | * for pointing some of this out to me. In fact, a lot of | |
753 | * the client-to-client info comes from the work of the GAIM | |
754 | * developers. Thanks! | |
755 | * | |
756 | * Read off one capability string and we should have it ID'd. | |
757 | * | |
758 | */ | |
d410cf58 | 759 | if ((args.reqclass = aim_getcap(sess, &bbs, 0x10)) == 0x0000) { |
6ae3c48d | 760 | faimdprintf(sess, 0, "rend: no ID block\n"); |
761 | return 0; | |
762 | } | |
763 | ||
764 | /* | |
70b889c5 | 765 | * What follows may be TLVs or nothing, depending on the |
766 | * purpose of the message. | |
767 | * | |
768 | * Ack packets for instance have nothing more to them. | |
769 | */ | |
d410cf58 | 770 | list2 = aim_readtlvchain(&bbs); |
6ae3c48d | 771 | |
c5f5b7f1 | 772 | #if 0 /* this should be in the per-type blocks */ |
6ae3c48d | 773 | if (!list2 || ((args.reqclass != AIM_CAPS_IMIMAGE) && !(aim_gettlv(list2, 0x2711, 1)))) { |
d410cf58 | 774 | aim_msgcookie_t *cook; |
6ae3c48d | 775 | int type; |
776 | ||
777 | type = aim_msgcookie_gettype(args.reqclass); /* XXX: fix this shitty code */ | |
778 | ||
779 | if ((cook = aim_checkcookie(sess, cookie, type)) == NULL) { | |
780 | faimdprintf(sess, 0, "non-data rendezvous thats not in cache (type %d)\n", type); | |
d410cf58 | 781 | aim_freetlvchain(&list2); |
782 | return 1; | |
6ae3c48d | 783 | } |
784 | ||
785 | if (cook->type == AIM_COOKIETYPE_OFTGET) { | |
786 | struct aim_filetransfer_priv *ft; | |
787 | ||
788 | if (cook->data) { | |
789 | int errorcode = -1; /* XXX shouldnt this be 0? */ | |
790 | ||
791 | ft = (struct aim_filetransfer_priv *)cook->data; | |
792 | ||
793 | if (args.status != 0x0002) { | |
794 | ||
d410cf58 | 795 | if (aim_gettlv(list2, 0x000b, 1)) |
796 | errorcode = aim_gettlv16(list2, 0x000b, 1); | |
6ae3c48d | 797 | |
d410cf58 | 798 | /* XXX this should make it up to the client, you know.. */ |
799 | if (errorcode) | |
800 | faimdprintf(sess, 0, "transfer from %s (%s) for %s cancelled (error code %d)\n", ft->sn, ft->ip, ft->fh.name, errorcode); | |
6ae3c48d | 801 | } /* args.status != 0x0002 */ |
d410cf58 | 802 | |
6ae3c48d | 803 | } else { |
804 | faimdprintf(sess, 0, "no data attached to file transfer\n"); | |
805 | } /* !cook->data */ | |
806 | ||
807 | } else if (cook->type == AIM_CAPS_VOICE) { | |
808 | ||
809 | faimdprintf(sess, 0, "voice request cancelled\n"); | |
810 | ||
811 | } else { | |
812 | ||
813 | faimdprintf(sess, 0, "unknown cookie cache type %d\n", cook->type); | |
814 | } | |
815 | ||
816 | aim_freetlvchain(&list2); | |
817 | ||
818 | return 1; | |
00ef5271 | 819 | } |
c5f5b7f1 | 820 | #endif |
6ae3c48d | 821 | |
822 | /* | |
823 | * The rest of the handling depends on what type it is. | |
824 | */ | |
825 | if (args.reqclass & AIM_CAPS_BUDDYICON) { | |
6ae3c48d | 826 | |
c5f5b7f1 | 827 | ret = incomingim_ch2_buddyicon(sess, mod, rx, snac, userinfo, &args, list2); |
6ae3c48d | 828 | |
829 | } else if (args.reqclass & AIM_CAPS_VOICE) { | |
d410cf58 | 830 | aim_msgcookie_t *cachedcook; |
6ae3c48d | 831 | |
832 | faimdprintf(sess, 1, "rend: voice!\n"); | |
833 | ||
d410cf58 | 834 | if(!(cachedcook = (aim_msgcookie_t*)calloc(1, sizeof(aim_msgcookie_t)))) { |
6ae3c48d | 835 | aim_freetlvchain(&list2); |
836 | return 0; | |
837 | } | |
838 | ||
839 | memcpy(cachedcook->cookie, cookie, 8); | |
840 | cachedcook->type = AIM_COOKIETYPE_OFTVOICE; | |
841 | cachedcook->data = NULL; | |
842 | ||
843 | if (aim_cachecookie(sess, cachedcook) == -1) | |
844 | faimdprintf(sess, 0, "ERROR caching message cookie\n"); | |
845 | ||
846 | /* XXX: implement all this */ | |
847 | ||
848 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) | |
849 | ret = userfunc(sess, rx, channel, userinfo, &args); | |
850 | ||
851 | } else if (args.reqclass & AIM_CAPS_IMIMAGE) { | |
6ae3c48d | 852 | |
c5f5b7f1 | 853 | ret = incomingim_ch2_imimage(sess, mod, rx, snac, userinfo, &args, list2); |
6ae3c48d | 854 | |
855 | } else if (args.reqclass & AIM_CAPS_CHAT) { | |
d410cf58 | 856 | aim_tlv_t *miscinfo; |
857 | aim_bstream_t tbs; | |
6ae3c48d | 858 | |
859 | miscinfo = aim_gettlv(list2, 0x2711, 1); | |
d410cf58 | 860 | |
861 | aim_bstream_init(&tbs, miscinfo->value, miscinfo->length); | |
862 | ||
863 | aim_chat_readroominfo(&tbs, &args.info.chat.roominfo); | |
6ae3c48d | 864 | |
865 | if (aim_gettlv(list2, 0x000c, 1)) | |
866 | args.info.chat.msg = aim_gettlv_str(list2, 0x000c, 1); | |
867 | ||
868 | if (aim_gettlv(list2, 0x000d, 1)) | |
869 | args.info.chat.encoding = aim_gettlv_str(list2, 0x000d, 1); | |
870 | ||
871 | if (aim_gettlv(list2, 0x000e, 1)) | |
872 | args.info.chat.lang = aim_gettlv_str(list2, 0x000e, 1); | |
873 | ||
874 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) | |
875 | ret = userfunc(sess, rx, channel, userinfo, &args); | |
876 | ||
877 | free(args.info.chat.roominfo.name); | |
878 | free(args.info.chat.msg); | |
879 | free(args.info.chat.encoding); | |
880 | free(args.info.chat.lang); | |
881 | ||
882 | } else if (args.reqclass & AIM_CAPS_GETFILE) { | |
883 | char ip[30]; | |
d410cf58 | 884 | aim_msgcookie_t *cachedcook; |
885 | aim_tlv_t *miscinfo; | |
886 | aim_tlv_t *iptlv, *porttlv; | |
6ae3c48d | 887 | |
888 | memset(ip, 0, 30); | |
889 | ||
d410cf58 | 890 | if (!(cachedcook = calloc(1, sizeof(aim_msgcookie_t)))) { |
6ae3c48d | 891 | aim_freetlvchain(&list2); |
892 | return 0; | |
893 | } | |
894 | ||
895 | if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) || | |
896 | !(iptlv = aim_gettlv(list2, 0x0003, 1)) || | |
897 | !(porttlv = aim_gettlv(list2, 0x0005, 1))) { | |
898 | ||
899 | faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn); | |
900 | aim_cookie_free(sess, cachedcook); | |
901 | aim_freetlvchain(&list2); | |
902 | ||
903 | return 0; | |
904 | } | |
905 | ||
906 | snprintf(ip, 30, "%d.%d.%d.%d:%d", | |
907 | aimutil_get8(iptlv->value+0), | |
908 | aimutil_get8(iptlv->value+1), | |
909 | aimutil_get8(iptlv->value+2), | |
910 | aimutil_get8(iptlv->value+3), | |
911 | aimutil_get16(porttlv->value)); | |
912 | ||
913 | faimdprintf(sess, 0, "rend: file get request from %s (%s)\n", userinfo->sn, ip); | |
914 | ||
915 | args.info.getfile.ip = ip; | |
916 | args.info.getfile.cookie = cookie; | |
917 | ||
918 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) | |
919 | ret = userfunc(sess, rx, channel, userinfo, &args); | |
920 | ||
921 | } else if (args.reqclass & AIM_CAPS_SENDFILE) { | |
922 | #if 0 | |
923 | char ip[30]; | |
d410cf58 | 924 | aim_msgcookie_t *cachedcook; |
925 | aim_tlv_t *miscinfo; | |
926 | aim_tlv_t *iptlv, *porttlv; | |
6ae3c48d | 927 | |
928 | memset(ip, 0, 30); | |
929 | ||
d410cf58 | 930 | if (!(cachedcook = calloc(1, sizeof(aim_msgcookie_t)))) { |
6ae3c48d | 931 | aim_freetlvchain(&list2); |
932 | return 0; | |
933 | } | |
934 | ||
935 | if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) || | |
936 | !(iptlv = aim_gettlv(list2, 0x0003, 1)) || | |
937 | !(porttlv = aim_gettlv(list2, 0x0005, 1))) { | |
938 | ||
939 | faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn); | |
940 | aim_cookie_free(sess, cachedcook); | |
941 | aim_freetlvchain(&list2); | |
942 | ||
943 | return 0; | |
944 | } | |
945 | ||
946 | snprintf(ip, 30, "%d.%d.%d.%d:%d", | |
947 | aimutil_get8(iptlv->value+0), | |
948 | aimutil_get8(iptlv->value+1), | |
949 | aimutil_get8(iptlv->value+2), | |
950 | aimutil_get8(iptlv->value+3), | |
951 | aimutil_get16(porttlv->value)); | |
952 | ||
953 | if (aim_gettlv(list2, 0x000c, 1)) | |
954 | desc = aim_gettlv_str(list2, 0x000c, 1); | |
955 | ||
956 | faimdprintf(sess, 0, "rend: file transfer request from %s: %s (%s)\n", | |
957 | userinfo->sn, desc, ip); | |
958 | ||
959 | memcpy(cachedcook->cookie, cookie, 8); | |
960 | ||
961 | ft = malloc(sizeof(struct aim_filetransfer_priv)); /* XXX */ | |
962 | strncpy(ft->sn, userinfo.sn, sizeof(ft->sn)); | |
963 | strncpy(ft->ip, ip, sizeof(ft->ip)); | |
964 | strncpy(ft->fh.name, miscinfo->value+8, sizeof(ft->fh.name)); | |
965 | cachedcook->type = AIM_COOKIETYPE_OFTSEND; | |
966 | cachedcook->data = ft; | |
967 | ||
968 | if (aim_cachecookie(sess, cachedcook) == -1) | |
969 | faimdprintf(sess, 0, "ERROR caching message cookie\n"); | |
970 | ||
971 | aim_accepttransfer(sess, rx->conn, ft->sn, cookie, AIM_CAPS_SENDFILE); | |
972 | ||
973 | if (desc) | |
974 | free(desc); | |
975 | ||
976 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) | |
977 | ret = userfunc(sess, rx, channel, userinfo, &args); | |
00ef5271 | 978 | |
979 | #endif | |
6ae3c48d | 980 | } else |
981 | faimdprintf(sess, 0, "rend: unknown rendezvous 0x%04x\n", args.reqclass); | |
00ef5271 | 982 | |
6ae3c48d | 983 | aim_freetlvchain(&list2); |
00ef5271 | 984 | |
6ae3c48d | 985 | return ret; |
e5012450 | 986 | } |
987 | ||
49c8a2fa | 988 | /* |
989 | * It can easily be said that parsing ICBMs is THE single | |
990 | * most difficult thing to do in the in AIM protocol. In | |
991 | * fact, I think I just did say that. | |
992 | * | |
993 | * Below is the best damned solution I've come up with | |
994 | * over the past sixteen months of battling with it. This | |
995 | * can parse both away and normal messages from every client | |
996 | * I have access to. Its not fast, its not clean. But it works. | |
997 | * | |
49c8a2fa | 998 | */ |
d410cf58 | 999 | static int incomingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs) |
9de3ca7e | 1000 | { |
6ae3c48d | 1001 | int i, ret = 0; |
d410cf58 | 1002 | fu8_t cookie[8]; |
1003 | fu16_t channel; | |
6ae3c48d | 1004 | struct aim_userinfo_s userinfo; |
1005 | ||
1006 | memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s)); | |
1007 | ||
1008 | /* | |
1009 | * Read ICBM Cookie. And throw away. | |
1010 | */ | |
1011 | for (i = 0; i < 8; i++) | |
d410cf58 | 1012 | cookie[i] = aimbs_get8(bs); |
6ae3c48d | 1013 | |
1014 | /* | |
1015 | * Channel ID. | |
1016 | * | |
1017 | * Channel 0x0001 is the message channel. There are | |
1018 | * other channels for things called "rendevous" | |
1019 | * which represent chat and some of the other new | |
1020 | * features of AIM2/3/3.5. | |
1021 | * | |
1022 | * Channel 0x0002 is the Rendevous channel, which | |
1023 | * is where Chat Invitiations and various client-client | |
1024 | * connection negotiations come from. | |
1025 | * | |
1026 | */ | |
d410cf58 | 1027 | channel = aimbs_get16(bs); |
6ae3c48d | 1028 | |
1029 | /* | |
1030 | * Technically Channel 3 in chat could be done here too. | |
1031 | */ | |
1032 | if ((channel != 0x01) && (channel != 0x02)) { | |
1033 | faimdprintf(sess, 0, "icbm: ICBM received on an unsupported channel. Ignoring.\n (chan = %04x)", channel); | |
d410cf58 | 1034 | return 0; |
6ae3c48d | 1035 | } |
1036 | ||
1037 | /* | |
1038 | * Extract the standard user info block. | |
1039 | * | |
1040 | * Note that although this contains TLVs that appear contiguous | |
1041 | * with the TLVs read below, they are two different pieces. The | |
1042 | * userinfo block contains the number of TLVs that contain user | |
1043 | * information, the rest are not even though there is no seperation. | |
1044 | * aim_extractuserinfo() returns the number of bytes used by the | |
1045 | * userinfo tlvs, so you can start reading the rest of them right | |
1046 | * afterward. | |
1047 | * | |
1048 | * That also means that TLV types can be duplicated between the | |
1049 | * userinfo block and the rest of the message, however there should | |
1050 | * never be two TLVs of the same type in one block. | |
1051 | * | |
1052 | */ | |
d410cf58 | 1053 | aim_extractuserinfo(sess, bs, &userinfo); |
6ae3c48d | 1054 | |
1055 | /* | |
1056 | * From here on, its depends on what channel we're on. | |
1057 | * | |
1058 | * Technically all channels have a TLV list have this, however, | |
1059 | * for the common channel 1 case, in-place parsing is used for | |
1060 | * performance reasons (less memory allocation). | |
1061 | */ | |
1062 | if (channel == 1) { | |
1063 | ||
d410cf58 | 1064 | ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, bs, cookie); |
6ae3c48d | 1065 | |
1066 | } else if (channel == 0x0002) { | |
d410cf58 | 1067 | aim_tlvlist_t *tlvlist; |
6ae3c48d | 1068 | |
1069 | /* | |
1070 | * Read block of TLVs (not including the userinfo data). All | |
1071 | * further data is derived from what is parsed here. | |
1072 | */ | |
d410cf58 | 1073 | tlvlist = aim_readtlvchain(bs); |
6ae3c48d | 1074 | |
1075 | ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie); | |
1076 | ||
1077 | /* | |
1078 | * Free up the TLV chain. | |
1079 | */ | |
1080 | aim_freetlvchain(&tlvlist); | |
1081 | } | |
1082 | ||
1083 | return ret; | |
49c8a2fa | 1084 | } |
1085 | ||
040457cc | 1086 | /* |
1087 | * Possible codes: | |
1088 | * AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support" | |
1089 | * AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer" | |
1090 | * AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers" | |
1091 | * | |
1092 | */ | |
d410cf58 | 1093 | faim_export int aim_denytransfer(aim_session_t *sess, aim_conn_t *conn, const char *sender, const char *cookie, fu16_t code) |
040457cc | 1094 | { |
d410cf58 | 1095 | aim_frame_t *fr; |
1096 | aim_snacid_t snacid; | |
1097 | aim_tlvlist_t *tl = NULL; | |
1098 | ||
1099 | if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sender)+6))) | |
6ae3c48d | 1100 | return -ENOMEM; |
040457cc | 1101 | |
d410cf58 | 1102 | snacid = aim_cachesnac(sess, 0x0004, 0x000b, 0x0000, NULL, 0); |
1103 | aim_putsnac(&fr->data, 0x0004, 0x000b, 0x0000, snacid); | |
1104 | ||
1105 | aimbs_putraw(&fr->data, cookie, 8); | |
040457cc | 1106 | |
d410cf58 | 1107 | aimbs_put16(&fr->data, 0x0002); /* channel */ |
1108 | aimbs_put8(&fr->data, strlen(sender)); | |
1109 | aimbs_putraw(&fr->data, sender, strlen(sender)); | |
040457cc | 1110 | |
d410cf58 | 1111 | aim_addtlvtochain16(&tl, 0x0003, code); |
1112 | aim_writetlvchain(&fr->data, &tl); | |
1113 | aim_freetlvchain(&tl); | |
1114 | ||
1115 | aim_tx_enqueue(sess, fr); | |
040457cc | 1116 | |
6ae3c48d | 1117 | return 0; |
040457cc | 1118 | } |
1119 | ||
49c8a2fa | 1120 | /* |
355229fe | 1121 | * aim_reqicbmparaminfo() |
49c8a2fa | 1122 | * |
355229fe | 1123 | * Request ICBM parameter information. |
49c8a2fa | 1124 | * |
1125 | */ | |
d410cf58 | 1126 | faim_export int aim_reqicbmparams(aim_session_t *sess, aim_conn_t *conn) |
355229fe | 1127 | { |
1128 | return aim_genericreq_n(sess, conn, 0x0004, 0x0004); | |
1129 | } | |
1130 | ||
1131 | /* | |
1132 | * | |
d410cf58 | 1133 | * I definitly recommend sending this. If you don't, you'll be stuck |
1134 | * with the rather unreasonable defaults. You don't want those. Send this. | |
1135 | * | |
355229fe | 1136 | */ |
d410cf58 | 1137 | faim_export int aim_seticbmparam(aim_session_t *sess, aim_conn_t *conn, struct aim_icbmparameters *params) |
49c8a2fa | 1138 | { |
d410cf58 | 1139 | aim_frame_t *fr; |
1140 | aim_snacid_t snacid; | |
49c8a2fa | 1141 | |
355229fe | 1142 | if (!sess || !conn || !params) |
1143 | return -EINVAL; | |
1144 | ||
d410cf58 | 1145 | if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+16))) |
6ae3c48d | 1146 | return -ENOMEM; |
5b79dc93 | 1147 | |
d410cf58 | 1148 | snacid = aim_cachesnac(sess, 0x0004, 0x0002, 0x0000, NULL, 0); |
1149 | aim_putsnac(&fr->data, 0x0004, 0x0002, 0x0000, snacid); | |
5b79dc93 | 1150 | |
d410cf58 | 1151 | /* This is read-only (see Parameter Reply). Must be set to zero here. */ |
1152 | aimbs_put16(&fr->data, 0x0000); | |
355229fe | 1153 | |
1154 | /* These are all read-write */ | |
d410cf58 | 1155 | aimbs_put32(&fr->data, params->flags); |
1156 | aimbs_put16(&fr->data, params->maxmsglen); | |
1157 | aimbs_put16(&fr->data, params->maxsenderwarn); | |
1158 | aimbs_put16(&fr->data, params->maxrecverwarn); | |
1159 | aimbs_put32(&fr->data, params->minmsginterval); | |
5b79dc93 | 1160 | |
d410cf58 | 1161 | aim_tx_enqueue(sess, fr); |
a25832e6 | 1162 | |
6ae3c48d | 1163 | return 0; |
a25832e6 | 1164 | } |
1165 | ||
d410cf58 | 1166 | static int paraminfo(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs) |
a25832e6 | 1167 | { |
355229fe | 1168 | struct aim_icbmparameters params; |
6ae3c48d | 1169 | aim_rxcallback_t userfunc; |
e6b05d80 | 1170 | |
d410cf58 | 1171 | params.maxchan = aimbs_get16(bs); |
1172 | params.flags = aimbs_get32(bs); | |
1173 | params.maxmsglen = aimbs_get16(bs); | |
1174 | params.maxsenderwarn = aimbs_get16(bs); | |
1175 | params.maxrecverwarn = aimbs_get16(bs); | |
1176 | params.minmsginterval = aimbs_get32(bs); | |
1177 | ||
6ae3c48d | 1178 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) |
355229fe | 1179 | return userfunc(sess, rx, ¶ms); |
00ef5271 | 1180 | |
6ae3c48d | 1181 | return 0; |
00ef5271 | 1182 | } |
1183 | ||
d410cf58 | 1184 | static int missedcall(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs) |
96f8b1ed | 1185 | { |
d410cf58 | 1186 | int ret = 0; |
6ae3c48d | 1187 | aim_rxcallback_t userfunc; |
d410cf58 | 1188 | fu16_t channel, nummissed, reason; |
6ae3c48d | 1189 | struct aim_userinfo_s userinfo; |
6ae3c48d | 1190 | |
d410cf58 | 1191 | while (aim_bstream_empty(bs)) { |
6ae3c48d | 1192 | |
d410cf58 | 1193 | channel = aimbs_get16(bs); |
1194 | aim_extractuserinfo(sess, bs, &userinfo); | |
1195 | nummissed = aimbs_get16(bs); | |
1196 | reason = aimbs_get16(bs); | |
6ae3c48d | 1197 | |
36a61e0d | 1198 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) |
1199 | ret = userfunc(sess, rx, channel, &userinfo, nummissed, reason); | |
1200 | } | |
6ae3c48d | 1201 | |
36a61e0d | 1202 | return ret; |
00ef5271 | 1203 | } |
1204 | ||
d410cf58 | 1205 | static int clienterr(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs) |
00ef5271 | 1206 | { |
d410cf58 | 1207 | int ret = 0; |
6ae3c48d | 1208 | aim_rxcallback_t userfunc; |
d410cf58 | 1209 | fu16_t channel, reason; |
1210 | char *sn; | |
1211 | fu8_t *ck, snlen; | |
00ef5271 | 1212 | |
d410cf58 | 1213 | ck = aimbs_getraw(bs, 8); |
1214 | channel = aimbs_get16(bs); | |
1215 | snlen = aimbs_get8(bs); | |
1216 | sn = aimbs_getstr(bs, snlen); | |
1217 | reason = aimbs_get16(bs); | |
00ef5271 | 1218 | |
d410cf58 | 1219 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) |
1220 | ret = userfunc(sess, rx, channel, sn, reason); | |
00ef5271 | 1221 | |
d410cf58 | 1222 | return ret; |
1223 | } | |
00ef5271 | 1224 | |
d410cf58 | 1225 | static int msgack(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs) |
1226 | { | |
1227 | aim_rxcallback_t userfunc; | |
1228 | fu16_t type; | |
1229 | fu8_t snlen, *ck; | |
1230 | char *sn; | |
1231 | ||
1232 | ck = aimbs_getraw(bs, 8); | |
1233 | type = aimbs_get16(bs); | |
1234 | snlen = aimbs_get8(bs); | |
1235 | sn = aimbs_getstr(bs, snlen); | |
00ef5271 | 1236 | |
6ae3c48d | 1237 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) |
1238 | return userfunc(sess, rx, type, sn); | |
00ef5271 | 1239 | |
d410cf58 | 1240 | free(sn); |
1241 | free(ck); | |
1242 | ||
6ae3c48d | 1243 | return 0; |
00ef5271 | 1244 | } |
1245 | ||
d410cf58 | 1246 | static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs) |
00ef5271 | 1247 | { |
1248 | ||
6ae3c48d | 1249 | if (snac->subtype == 0x0005) |
d410cf58 | 1250 | return paraminfo(sess, mod, rx, snac, bs); |
6ae3c48d | 1251 | else if (snac->subtype == 0x0006) |
d410cf58 | 1252 | return outgoingim(sess, mod, rx, snac, bs); |
6ae3c48d | 1253 | else if (snac->subtype == 0x0007) |
d410cf58 | 1254 | return incomingim(sess, mod, rx, snac, bs); |
6ae3c48d | 1255 | else if (snac->subtype == 0x000a) |
d410cf58 | 1256 | return missedcall(sess, mod, rx, snac, bs); |
1257 | else if (snac->subtype == 0x000b) | |
1258 | return clienterr(sess, mod, rx, snac, bs); | |
6ae3c48d | 1259 | else if (snac->subtype == 0x000c) |
d410cf58 | 1260 | return msgack(sess, mod, rx, snac, bs); |
6ae3c48d | 1261 | |
1262 | return 0; | |
00ef5271 | 1263 | } |
1264 | ||
d410cf58 | 1265 | faim_internal int msg_modfirst(aim_session_t *sess, aim_module_t *mod) |
00ef5271 | 1266 | { |
1267 | ||
6ae3c48d | 1268 | mod->family = 0x0004; |
1269 | mod->version = 0x0000; | |
1270 | mod->flags = 0; | |
1271 | strncpy(mod->name, "messaging", sizeof(mod->name)); | |
1272 | mod->snachandler = snachandler; | |
00ef5271 | 1273 | |
6ae3c48d | 1274 | return 0; |
96f8b1ed | 1275 | } |