]> andersk Git - libfaim.git/blame - src/im.c
- Sat Mar 24 03:16:32 UTC 2001
[libfaim.git] / src / im.c
CommitLineData
9de3ca7e 1/*
2 * aim_im.c
3 *
4 * The routines for sending/receiving Instant Messages.
5 *
6 */
7
37ee990e 8#define FAIM_INTERNAL
dd60ff8b 9#include <aim.h>
9de3ca7e 10
9d83220c 11/*
12 * Takes a msghdr (and a length) and returns a client type
13 * code. Note that this is *only a guess* and has a low likelihood
14 * of actually being accurate.
15 *
16 * Its based on experimental data, with the help of Eric Warmenhoven
17 * who seems to have collected a wide variety of different AIM clients.
18 *
19 *
20 * Heres the current collection:
21 * 0501 0003 0101 0101 01 AOL Mobile Communicator, WinAIM 1.0.414
22 * 0501 0003 0101 0201 01 WinAIM 2.0.847, 2.1.1187, 3.0.1464,
23 * 4.3.2229, 4.4.2286
24 * 0501 0004 0101 0102 0101 WinAIM 4.1.2010, libfaim (right here)
25 * 0501 0001 0101 01 AOL v6.0, CompuServe 2000 v6.0, any
26 * TOC client
27 */
28faim_export unsigned short aim_fingerprintclient(unsigned char *msghdr, int len)
29{
30 static const struct {
31 unsigned short clientid;
32 int len;
33 unsigned char data[10];
34 } fingerprints[] = {
35 /* AOL Mobile Communicator, WinAIM 1.0.414 */
36 { AIM_CLIENTTYPE_MC,
37 9, {0x05, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01}},
38
39 /* WinAIM 2.0.847, 2.1.1187, 3.0.1464, 4.3.2229, 4.4.2286 */
40 { AIM_CLIENTTYPE_WINAIM,
41 9, {0x05, 0x01, 0x00, 0x03, 0x01, 0x01, 0x02, 0x01, 0x01}},
42
43 /* WinAIM 4.1.2010, libfaim */
44 { AIM_CLIENTTYPE_WINAIM41,
45 10, {0x05, 0x01, 0x00, 0x04, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01}},
46
47 /* AOL v6.0, CompuServe 2000 v6.0, any TOC client */
48 { AIM_CLIENTTYPE_AOL_TOC,
49 7, {0x05, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01}},
50
51 { 0, 0}
52 };
53 int i;
54
55 if (!msghdr || (len <= 0))
56 return 0;
57
58 for (i = 0; fingerprints[i].len; i++) {
59 if (fingerprints[i].len != len)
60 continue;
61 if (memcmp(fingerprints[i].data, msghdr, fingerprints[i].len) == 0)
62 return fingerprints[i].clientid;
63 }
64
65 return AIM_CLIENTTYPE_UNKNOWN;
66}
67
9de3ca7e 68/*
69 * Send an ICBM (instant message).
70 *
71 *
72 * Possible flags:
73 * AIM_IMFLAGS_AWAY -- Marks the message as an autoresponse
74 * AIM_IMFLAGS_ACK -- Requests that the server send an ack
75 * when the message is received (of type 0x0004/0x000c)
76 *
9de3ca7e 77 */
78b3fb13 78faim_export unsigned long aim_send_im(struct aim_session_t *sess,
79 struct aim_conn_t *conn,
80 char *destsn, u_int flags, char *msg)
9de3ca7e 81{
82
49c8a2fa 83 int curbyte,i;
5b79dc93 84 struct command_tx_struct *newpacket;
9de3ca7e 85
91931247 86 if (strlen(msg) >= MAXMSGLEN)
87 return -1;
88
646c6b52 89 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, strlen(msg)+256)))
5b79dc93 90 return -1;
9de3ca7e 91
5b79dc93 92 newpacket->lock = 1; /* lock struct */
9de3ca7e 93
94 curbyte = 0;
5b79dc93 95 curbyte += aim_putsnac(newpacket->data+curbyte,
a25832e6 96 0x0004, 0x0006, 0x0000, sess->snac_nextid);
9de3ca7e 97
49c8a2fa 98 /*
99 * Generate a random message cookie
a25832e6 100 *
101 * We could cache these like we do SNAC IDs. (In fact, it
102 * might be a good idea.) In the message error functions,
103 * the 8byte message cookie is returned as well as the
104 * SNAC ID.
105 *
49c8a2fa 106 */
107 for (i=0;i<8;i++)
5ac21963 108 curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) rand());
9de3ca7e 109
49c8a2fa 110 /*
111 * Channel ID
112 */
5b79dc93 113 curbyte += aimutil_put16(newpacket->data+curbyte,0x0001);
9de3ca7e 114
49c8a2fa 115 /*
116 * Destination SN (prepended with byte length)
117 */
5b79dc93 118 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
119 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
9de3ca7e 120
49c8a2fa 121 /*
122 * metaTLV start.
123 */
5b79dc93 124 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
e5012450 125 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg) + 0x10);
9de3ca7e 126
49c8a2fa 127 /*
e5012450 128 * Flag data / ICBM Parameters?
9d83220c 129 *
130 * I don't know what these are...
131 *
49c8a2fa 132 */
e5012450 133 curbyte += aimutil_put8(newpacket->data+curbyte, 0x05);
134 curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
135
136 /* number of bytes to follow */
137 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
138 curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
139 curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
140 curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
141 curbyte += aimutil_put8(newpacket->data+curbyte, 0x02);
142
5b79dc93 143 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0101);
9de3ca7e 144
49c8a2fa 145 /*
146 * Message block length.
147 */
5b79dc93 148 curbyte += aimutil_put16(newpacket->data+curbyte, strlen(msg) + 0x04);
9de3ca7e 149
49c8a2fa 150 /*
151 * Character set data?
152 */
5b79dc93 153 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
154 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
9de3ca7e 155
49c8a2fa 156 /*
157 * Message. Not terminated.
158 */
5b79dc93 159 curbyte += aimutil_putstr(newpacket->data+curbyte,msg, strlen(msg));
9de3ca7e 160
49c8a2fa 161 /*
162 * Set the Request Acknowledge flag.
163 */
5b79dc93 164 if (flags & AIM_IMFLAGS_ACK) {
165 curbyte += aimutil_put16(newpacket->data+curbyte,0x0003);
166 curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
167 }
49c8a2fa 168
169 /*
170 * Set the Autoresponse flag.
171 */
5b79dc93 172 if (flags & AIM_IMFLAGS_AWAY) {
173 curbyte += aimutil_put16(newpacket->data+curbyte,0x0004);
174 curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
175 }
49c8a2fa 176
5b79dc93 177 newpacket->commandlen = curbyte;
178 newpacket->lock = 0;
9de3ca7e 179
5b79dc93 180 aim_tx_enqueue(sess, newpacket);
49c8a2fa 181
1ea867e3 182 aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, destsn, strlen(destsn)+1);
183 aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
9de3ca7e 184
1ea867e3 185 return sess->snac_nextid;
9de3ca7e 186}
187
00ef5271 188static int outgoingim(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
e5012450 189{
00ef5271 190 unsigned int i, ret = 0;
191 rxcallback_t userfunc;
e5012450 192 unsigned char cookie[8];
193 int channel;
194 struct aim_tlvlist_t *tlvlist;
195 char sn[MAXSNLEN];
196 unsigned short icbmflags = 0;
197 unsigned char flag1 = 0, flag2 = 0;
198 unsigned char *msgblock = NULL, *msg = NULL;
199
e5012450 200 /* ICBM Cookie. */
00ef5271 201 for (i = 0; i < 8; i++)
202 cookie[i] = aimutil_get8(data+i);
e5012450 203
204 /* Channel ID */
00ef5271 205 channel = aimutil_get16(data+i);
e5012450 206 i += 2;
207
208 if (channel != 0x01) {
646c6b52 209 faimdprintf(sess, 0, "icbm: ICBM recieved on unsupported channel. Ignoring. (chan = %04x)\n", channel);
e5012450 210 return 1;
211 }
212
00ef5271 213 strncpy(sn, (char *) data+i+1, (int) *(data+i));
214 i += 1 + (int) *(data+i);
e5012450 215
00ef5271 216 tlvlist = aim_readtlvchain(data+i, datalen-i);
e5012450 217
218 if (aim_gettlv(tlvlist, 0x0003, 1))
219 icbmflags |= AIM_IMFLAGS_ACK;
220 if (aim_gettlv(tlvlist, 0x0004, 1))
221 icbmflags |= AIM_IMFLAGS_AWAY;
222
223 if (aim_gettlv(tlvlist, 0x0002, 1)) {
224 int j = 0;
225
78b3fb13 226 msgblock = (unsigned char *)aim_gettlv_str(tlvlist, 0x0002, 1);
e5012450 227
228 /* no, this really is correct. I'm not high or anything either. */
229 j += 2;
230 j += 2 + aimutil_get16(msgblock+j);
231 j += 2;
232
233 j += 2; /* final block length */
234
235 flag1 = aimutil_get16(msgblock);
236 j += 2;
237 flag2 = aimutil_get16(msgblock);
238 j += 2;
239
240 msg = msgblock+j;
241 }
242
00ef5271 243 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
244 ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2);
e5012450 245
246 if (msgblock)
247 free(msgblock);
248 aim_freetlvchain(&tlvlist);
249
00ef5271 250 return ret;
251}
252
253static int incomingim_ch1(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned short channel, struct aim_userinfo_s *userinfo, struct aim_tlvlist_t *tlvlist, unsigned char *cookie)
254{
255 rxcallback_t userfunc;
256 int i, j = 0, y = 0, z = 0, ret = 0;
257 char *msg = NULL;
258 unsigned long icbmflags = 0;
259 struct aim_tlv_t *msgblocktlv;
260 unsigned char *msgblock;
261 unsigned short flag1, flag2;
262 int finlen = 0;
263 unsigned char fingerprint[10];
264 unsigned short wastebits;
265
266 /*
267 * Check Autoresponse status. If it is an autoresponse,
268 * it will contain a type 0x0004 TLV, with zero length.
269 */
270 if (aim_gettlv(tlvlist, 0x0004, 1))
271 icbmflags |= AIM_IMFLAGS_AWAY;
272
273 /*
274 * Check Ack Request status.
275 */
276 if (aim_gettlv(tlvlist, 0x0003, 1))
277 icbmflags |= AIM_IMFLAGS_ACK;
278
279 /*
280 * Message block.
281 */
282 msgblocktlv = aim_gettlv(tlvlist, 0x0002, 1);
283 if (!msgblocktlv || !(msgblock = msgblocktlv->value)) {
284 faimdprintf(sess, 0, "icbm: major error! no message block TLV found!\n");
285 return 0;
286 }
287
288 /*
289 * Extracting the message from the unknown cruft.
290 *
291 * This is a bit messy, and I'm not really qualified,
292 * even as the author, to comment on it. At least
293 * its not as bad as a while loop shooting into infinity.
294 *
295 * "Do you believe in magic?"
296 *
297 */
298
299 wastebits = aimutil_get8(msgblock+j++);
300 wastebits = aimutil_get8(msgblock+j++);
301
302 y = aimutil_get16(msgblock+j);
303 j += 2;
304 for (z = 0; z < y; z++)
305 wastebits = aimutil_get8(msgblock+j++);
306 wastebits = aimutil_get8(msgblock+j++);
307 wastebits = aimutil_get8(msgblock+j++);
308
309 finlen = j;
310 if (finlen > sizeof(fingerprint))
311 finlen = sizeof(fingerprint);
312 memcpy(fingerprint, msgblocktlv->value, finlen);
313
314 /*
315 * Message string length, including flag words.
316 */
317 i = aimutil_get16(msgblock+j);
318 j += 2;
319
320 /*
321 * Flag words.
322 *
323 * Its rumored that these can kick in some funky
324 * 16bit-wide char stuff that used to really kill
325 * libfaim. Hopefully the latter is no longer true.
326 *
327 * Though someone should investiagte the former.
328 *
329 */
330 flag1 = aimutil_get16(msgblock+j);
331 j += 2;
332 flag2 = aimutil_get16(msgblock+j);
333 j += 2;
334
335 if (flag1 || flag2)
336 faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", flag1, flag2);
337
338 /*
339 * Message string.
340 */
341 i -= 4;
342 msg = (char *)malloc(i+1);
343 memcpy(msg, msgblock+j, i);
344 msg[i] = '\0';
345
346 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
347 ret = userfunc(sess, rx, channel, userinfo, msg, icbmflags, flag1, flag2, finlen, fingerprint);
348
349 free(msg);
350
351 return ret;
352}
353
354static int incomingim_ch2(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned short channel, struct aim_userinfo_s *userinfo, struct aim_tlvlist_t *tlvlist, unsigned char *cookie)
355{
356 rxcallback_t userfunc;
357 struct aim_tlv_t *block1;
358 struct aim_tlvlist_t *list2;
359 unsigned short reqclass = 0;
360 unsigned short status = 0;
361 int ret = 0;
362
363 /*
364 * There's another block of TLVs embedded in the type 5 here.
365 */
366 block1 = aim_gettlv(tlvlist, 0x0005, 1);
367 if (!block1 || !block1->value) {
368 faimdprintf(sess, 0, "no tlv 0x0005 in rendezvous transaction!\n");
369 return 0;
370 }
371
372 /*
373 * First two bytes represent the status of the connection.
374 *
375 * 0 is a request, 2 is an accept
376 */
377 status = aimutil_get16(block1->value+0);
378
379 /*
380 * Next comes the cookie. Should match the ICBM cookie.
381 */
382 if (memcmp(block1->value+2, cookie, 8) != 0)
383 faimdprintf(sess, 0, "rend: warning cookies don't match!\n");
384
385 /*
386 * The next 16bytes are a capability block so we can
387 * identify what type of rendezvous this is.
388 *
389 * Thanks to Eric Warmenhoven <warmenhoven@linux.com> (of GAIM)
390 * for pointing some of this out to me. In fact, a lot of
391 * the client-to-client info comes from the work of the GAIM
392 * developers. Thanks!
393 *
394 * Read off one capability string and we should have it ID'd.
395 *
396 */
397 reqclass = aim_getcap(sess, block1->value+2+8, 0x10);
398 if (reqclass == 0x0000) {
399 faimdprintf(sess, 0, "rend: no ID block\n");
400 return 0;
401 }
402
403 /*
404 * What follows may be TLVs or nothing, depending on the
405 * purpose of the message.
406 *
407 * Ack packets for instance have nothing more to them.
408 */
409 list2 = aim_readtlvchain(block1->value+2+8+16, block1->length-2-8-16);
410
411 if (!list2 || ((reqclass != AIM_CAPS_IMIMAGE) && !(aim_gettlv(list2, 0x2711, 1)))) {
412 struct aim_msgcookie_t *cook;
413 int type;
414
415 type = aim_msgcookie_gettype(reqclass); /* XXX: fix this shitty code */
416
417 if ((cook = aim_checkcookie(sess, cookie, type)) == NULL) {
418 faimdprintf(sess, 0, "non-data rendezvous thats not in cache %d/%s!\n", type, cookie);
419 aim_freetlvchain(&list2);
420 return 0;
421 }
422
423 if (cook->type == AIM_COOKIETYPE_OFTGET) {
424 struct aim_filetransfer_priv *ft;
425
426 if (cook->data) {
427 int errorcode = -1; /* XXX shouldnt this be 0? */
428
429 ft = (struct aim_filetransfer_priv *)cook->data;
430
431 if(status != 0x0002) {
432 if (aim_gettlv(list2, 0x000b, 1))
433 errorcode = aim_gettlv16(list2, 0x000b, 1);
434
435 /* XXX this should make it up to the client, you know.. */
436 if (errorcode)
437 faimdprintf(sess, 0, "transfer from %s (%s) for %s cancelled (error code %d)\n", ft->sn, ft->ip, ft->fh.name, errorcode);
438 }
439 } else {
440 faimdprintf(sess, 0, "no data attached to file transfer\n");
441 }
442 } else if (cook->type == AIM_CAPS_VOICE) {
443 faimdprintf(sess, 0, "voice request cancelled\n");
444 } else {
445 faimdprintf(sess, 0, "unknown cookie cache type %d\n", cook->type);
446 }
447
448 aim_freetlvchain(&list2);
449
450 return 1;
451 }
452
453 /*
454 * The rest of the handling depends on what type it is.
455 */
456 if (reqclass & AIM_CAPS_BUDDYICON) {
457
458 /* XXX implement this (its in ActiveBuddy...) */
459 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
460 ret = userfunc(sess, rx, channel, reqclass, userinfo);
461
462 } else if (reqclass & AIM_CAPS_VOICE) {
463 struct aim_msgcookie_t *cachedcook;
464
465 faimdprintf(sess, 0, "rend: voice!\n");
466
467 if(!(cachedcook = (struct aim_msgcookie_t*)calloc(1, sizeof(struct aim_msgcookie_t)))) {
468 aim_freetlvchain(&list2);
469 return 0;
470 }
471
472 memcpy(cachedcook->cookie, cookie, 8);
473 cachedcook->type = AIM_COOKIETYPE_OFTVOICE;
474 cachedcook->data = NULL;
475
476 if (aim_cachecookie(sess, cachedcook) == -1)
477 faimdprintf(sess, 0, "ERROR caching message cookie\n");
478
479 /* XXX: implement all this */
480
481 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
482 ret = userfunc(sess, rx, channel, reqclass, &userinfo);
483
484 } else if ((reqclass & AIM_CAPS_IMIMAGE) ||
485 (reqclass & AIM_CAPS_BUDDYICON)) {
486 char ip[30];
487 struct aim_directim_priv *priv;
488
489 memset(ip, 0, sizeof(ip));
490
491 if (aim_gettlv(list2, 0x0003, 1) && aim_gettlv(list2, 0x0005, 1)) {
492 struct aim_tlv_t *iptlv, *porttlv;
493
494 iptlv = aim_gettlv(list2, 0x0003, 1);
495 porttlv = aim_gettlv(list2, 0x0005, 1);
496
497 snprintf(ip, 30, "%d.%d.%d.%d:%d",
498 aimutil_get8(iptlv->value+0),
499 aimutil_get8(iptlv->value+1),
500 aimutil_get8(iptlv->value+2),
501 aimutil_get8(iptlv->value+3),
502 4443 /*aimutil_get16(porttlv->value)*/);
503 }
504
505 faimdprintf(sess, 0, "rend: directIM request from %s (%s)\n",
506 userinfo->sn, ip);
507
508 /*
509 * XXX: there are a couple of different request packets for
510 * different things
511 */
512
513 priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
514 memcpy(priv->ip, ip, sizeof(priv->ip));
515 memcpy(priv->sn, userinfo->sn, sizeof(priv->sn));
516 memcpy(priv->cookie, cookie, sizeof(priv->cookie));
517
518 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
519 ret = userfunc(sess, rx, channel, reqclass, userinfo, priv);
520
521 } else if (reqclass & AIM_CAPS_CHAT) {
522 struct aim_tlv_t *miscinfo;
523 struct aim_chat_roominfo roominfo;
524 char *msg=NULL,*encoding=NULL,*lang=NULL;
525
526 miscinfo = aim_gettlv(list2, 0x2711, 1);
527 aim_chat_readroominfo(miscinfo->value, &roominfo);
528
529 if (aim_gettlv(list2, 0x000c, 1))
530 msg = aim_gettlv_str(list2, 0x000c, 1);
531
532 if (aim_gettlv(list2, 0x000d, 1))
533 encoding = aim_gettlv_str(list2, 0x000d, 1);
534
535 if (aim_gettlv(list2, 0x000e, 1))
536 lang = aim_gettlv_str(list2, 0x000e, 1);
537
538 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
539 ret = userfunc(sess, rx, channel, reqclass, userinfo, &roominfo, msg, encoding?encoding+1:NULL, lang?lang+1:NULL);
540
541 free(roominfo.name);
542 free(msg);
543 free(encoding);
544 free(lang);
545
546 } else if (reqclass & AIM_CAPS_GETFILE) {
547 char ip[30];
548 struct aim_msgcookie_t *cachedcook;
549 struct aim_tlv_t *miscinfo;
550 struct aim_tlv_t *iptlv, *porttlv;
551
552 memset(ip, 0, 30);
553
554 if (!(cachedcook = calloc(1, sizeof(struct aim_msgcookie_t)))) {
555 aim_freetlvchain(&list2);
556 return 0;
557 }
558
559 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) ||
560 !(iptlv = aim_gettlv(list2, 0x0003, 1)) ||
561 !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
562 faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
563 aim_cookie_free(sess, cachedcook);
564 aim_freetlvchain(&list2);
565 return 0;
566 }
567
568 snprintf(ip, 30, "%d.%d.%d.%d:%d",
569 aimutil_get8(iptlv->value+0),
570 aimutil_get8(iptlv->value+1),
571 aimutil_get8(iptlv->value+2),
572 aimutil_get8(iptlv->value+3),
573 aimutil_get16(porttlv->value));
574
575 faimdprintf(sess, 0, "rend: file get request from %s (%s)\n", userinfo->sn, ip);
576 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
577 ret = userfunc(sess, rx, channel, reqclass, userinfo, ip, cookie);
578
579 } else if (reqclass & AIM_CAPS_SENDFILE) {
580#if 0
581 char ip[30];
582 struct aim_msgcookie_t *cachedcook;
583 struct aim_tlv_t *miscinfo;
584 struct aim_tlv_t *iptlv, *porttlv;
585
586 memset(ip, 0, 30);
587
588 if (!(cachedcook = calloc(1, sizeof(struct aim_msgcookie_t)))) {
589 aim_freetlvchain(&list2);
590 return 0;
591 }
592
593 if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) ||
594 !(iptlv = aim_gettlv(list2, 0x0003, 1)) ||
595 !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
596 faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
597 aim_cookie_free(sess, cachedcook);
598 aim_freetlvchain(&list2);
599 return 0;
600 }
601
602 snprintf(ip, 30, "%d.%d.%d.%d:%d",
603 aimutil_get8(iptlv->value+0),
604 aimutil_get8(iptlv->value+1),
605 aimutil_get8(iptlv->value+2),
606 aimutil_get8(iptlv->value+3),
607 aimutil_get16(porttlv->value));
608
609 if (aim_gettlv(list2, 0x000c, 1))
610 desc = aim_gettlv_str(list2, 0x000c, 1);
611
612 faimdprintf(sess, 0, "rend: file transfer request from %s for %s: %s (%s)\n",
613 userinfo->sn, miscinfo->value+8,
614 desc, ip);
615
616 memcpy(cachedcook->cookie, cookie, 8);
617
618 ft = malloc(sizeof(struct aim_filetransfer_priv));
619 strncpy(ft->sn, userinfo.sn, sizeof(ft->sn));
620 strncpy(ft->ip, ip, sizeof(ft->ip));
621 strncpy(ft->fh.name, miscinfo->value+8, sizeof(ft->fh.name));
622 cachedcook->type = AIM_COOKIETYPE_OFTSEND;
623 cachedcook->data = ft;
624
625 if (aim_cachecookie(sess, cachedcook) == -1)
626 faimdprintf(sess, 0, "ERROR caching message cookie\n");
627
628 aim_accepttransfer(sess, rx->conn, ft->sn, cookie, AIM_CAPS_SENDFILE);
629
630 if (desc)
631 free(desc);
632
633 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
634 ret = userfunc(sess, rx, channel, reqclass, userinfo);
635
636#endif
637 } else
638 faimdprintf(sess, 0, "rend: unknown rendezvous 0x%04x\n", reqclass);
639
640 aim_freetlvchain(&list2);
641
642 return ret;
e5012450 643}
644
49c8a2fa 645/*
646 * It can easily be said that parsing ICBMs is THE single
647 * most difficult thing to do in the in AIM protocol. In
648 * fact, I think I just did say that.
649 *
650 * Below is the best damned solution I've come up with
651 * over the past sixteen months of battling with it. This
652 * can parse both away and normal messages from every client
653 * I have access to. Its not fast, its not clean. But it works.
654 *
49c8a2fa 655 */
00ef5271 656static int incomingim(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
9de3ca7e 657{
00ef5271 658 int i, ret = 0;
659 unsigned char cookie[8];
49c8a2fa 660 int channel;
661 struct aim_tlvlist_t *tlvlist;
26af6789 662 struct aim_userinfo_s userinfo;
49c8a2fa 663
95d7332a 664 memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
665
9de3ca7e 666 /*
49c8a2fa 667 * Read ICBM Cookie. And throw away.
9de3ca7e 668 */
00ef5271 669 for (i = 0; i < 8; i++)
670 cookie[i] = aimutil_get8(data+i);
9de3ca7e 671
49c8a2fa 672 /*
673 * Channel ID.
674 *
675 * Channel 0x0001 is the message channel. There are
676 * other channels for things called "rendevous"
677 * which represent chat and some of the other new
26af6789 678 * features of AIM2/3/3.5.
679 *
680 * Channel 0x0002 is the Rendevous channel, which
50443ea0 681 * is where Chat Invitiations and various client-client
682 * connection negotiations come from.
26af6789 683 *
49c8a2fa 684 */
00ef5271 685 channel = aimutil_get16(data+i);
9de3ca7e 686 i += 2;
26af6789 687
688 /*
689 *
690 */
646c6b52 691 if ((channel != 0x01) && (channel != 0x02)) {
692 faimdprintf(sess, 0, "icbm: ICBM received on an unsupported channel. Ignoring.\n (chan = %04x)", channel);
693 return 1;
694 }
9de3ca7e 695
49c8a2fa 696 /*
68ac63c2 697 * Extract the standard user info block.
698 *
699 * Note that although this contains TLVs that appear contiguous
700 * with the TLVs read below, they are two different pieces. The
701 * userinfo block contains the number of TLVs that contain user
702 * information, the rest are not even though there is no seperation.
703 * aim_extractuserinfo() returns the number of bytes used by the
704 * userinfo tlvs, so you can start reading the rest of them right
705 * afterward.
706 *
707 * That also means that TLV types can be duplicated between the
708 * userinfo block and the rest of the message, however there should
709 * never be two TLVs of the same type in one block.
710 *
26af6789 711 */
00ef5271 712 i += aim_extractuserinfo(sess, data+i, &userinfo);
26af6789 713
49c8a2fa 714 /*
68ac63c2 715 * Read block of TLVs (not including the userinfo data). All
716 * further data is derived from what is parsed here.
49c8a2fa 717 */
00ef5271 718 tlvlist = aim_readtlvchain(data+i, datalen-i);
9de3ca7e 719
49c8a2fa 720 /*
26af6789 721 * From here on, its depends on what channel we're on.
49c8a2fa 722 */
26af6789 723 if (channel == 1)
00ef5271 724 ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
26af6789 725 else if (channel == 0x0002)
00ef5271 726 ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
9de3ca7e 727
49c8a2fa 728 /*
729 * Free up the TLV chain.
730 */
731 aim_freetlvchain(&tlvlist);
732
00ef5271 733 return ret;
49c8a2fa 734}
735
040457cc 736/*
737 * Possible codes:
738 * AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
739 * AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
740 * AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
741 *
742 */
78b3fb13 743faim_export unsigned long aim_denytransfer(struct aim_session_t *sess,
744 struct aim_conn_t *conn,
745 char *sender,
746 char *cookie,
747 unsigned short code)
040457cc 748{
749 struct command_tx_struct *newpacket;
750 int curbyte, i;
751
646c6b52 752 if(!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(sender)+6)))
040457cc 753 return -1;
754
755 newpacket->lock = 1;
756
757 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x000b, 0x0000, sess->snac_nextid);
758 for (i = 0; i < 8; i++)
759 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
760 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
761 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sender));
762 curbyte += aimutil_putstr(newpacket->data+curbyte, sender, strlen(sender));
763 curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0003, code);
764
765 newpacket->lock = 0;
766 aim_tx_enqueue(sess, newpacket);
767
768 return (sess->snac_nextid++);
769}
770
49c8a2fa 771/*
772 * Not real sure what this does, nor does anyone I've talk to.
773 *
774 * Didn't use to send it. But now I think it might be a good
775 * idea.
776 *
777 */
78b3fb13 778faim_export unsigned long aim_seticbmparam(struct aim_session_t *sess,
779 struct aim_conn_t *conn)
49c8a2fa 780{
5b79dc93 781 struct command_tx_struct *newpacket;
49c8a2fa 782 int curbyte;
783
646c6b52 784 if(!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+16)))
5b79dc93 785 return -1;
786
787 newpacket->lock = 1;
788
789 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0002, 0x0000, sess->snac_nextid);
790 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
791 curbyte += aimutil_put32(newpacket->data+curbyte, 0x00000003);
00ef5271 792 curbyte += aimutil_put16(newpacket->data+curbyte, 0x1f40);
793 curbyte += aimutil_put16(newpacket->data+curbyte, 0x03e7);
794 curbyte += aimutil_put16(newpacket->data+curbyte, 0x03e7);
795 curbyte += aimutil_put32(newpacket->data+curbyte, 0x00000000);
5b79dc93 796
797 newpacket->lock = 0;
798 aim_tx_enqueue(sess, newpacket);
a25832e6 799
800 return (sess->snac_nextid++);
801}
802
00ef5271 803static int paraminfo(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
a25832e6 804{
00ef5271 805 unsigned long defflags, minmsginterval;
806 unsigned short maxicbmlen, maxsenderwarn, maxrecverwarn, maxchannel;
807 rxcallback_t userfunc;
808 int i = 0;
a25832e6 809
00ef5271 810 maxchannel = aimutil_get16(data+i);
811 i += 2;
a25832e6 812
00ef5271 813 defflags = aimutil_get32(data+i);
814 i += 4;
96f8b1ed 815
00ef5271 816 maxicbmlen = aimutil_get16(data+i);
817 i += 2;
a25832e6 818
00ef5271 819 maxsenderwarn = aimutil_get16(data+i);
820 i += 2;
49c8a2fa 821
00ef5271 822 maxrecverwarn = aimutil_get16(data+i);
823 i += 2;
e6b05d80 824
00ef5271 825 minmsginterval = aimutil_get32(data+i);
826 i += 4;
e6b05d80 827
00ef5271 828 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
829 return userfunc(sess, rx, maxchannel, defflags, maxicbmlen, maxsenderwarn, maxrecverwarn, minmsginterval);
830
831 return 0;
832}
833
834static int missedcall(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
96f8b1ed 835{
00ef5271 836 int i = 0;
837 rxcallback_t userfunc;
96f8b1ed 838 unsigned short channel, nummissed, reason;
839 struct aim_userinfo_s userinfo;
840
96f8b1ed 841 /*
842 * XXX: supposedly, this entire packet can repeat as many times
843 * as necessary. Should implement that.
844 */
845
846 /*
847 * Channel ID.
848 */
00ef5271 849 channel = aimutil_get16(data+i);
96f8b1ed 850 i += 2;
851
852 /*
853 * Extract the standard user info block.
854 */
00ef5271 855 i += aim_extractuserinfo(sess, data+i, &userinfo);
96f8b1ed 856
00ef5271 857 nummissed = aimutil_get16(data+i);
96f8b1ed 858 i += 2;
859
00ef5271 860 reason = aimutil_get16(data+i);
96f8b1ed 861 i += 2;
862
00ef5271 863 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
864 return userfunc(sess, rx, channel, &userinfo, nummissed, reason);
96f8b1ed 865
00ef5271 866 return 0;
867}
868
869static int msgack(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
870{
871 rxcallback_t userfunc;
872 char sn[MAXSNLEN];
873 unsigned char ck[8];
874 unsigned short type;
875 int i = 0;
876 unsigned char snlen;
877
878 memcpy(ck, data, 8);
879 i += 8;
880
881 type = aimutil_get16(data+i);
882 i += 2;
883
884 snlen = aimutil_get8(data+i);
885 i++;
886
887 memset(sn, 0, sizeof(sn));
888 strncpy(sn, (char *)data+i, snlen);
889
890 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
891 return userfunc(sess, rx, type, sn);
892
893 return 0;
894}
895
896static int snachandler(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
897{
898
899 if (snac->subtype == 0x0005)
900 return paraminfo(sess, mod, rx, snac, data, datalen);
901 else if (snac->subtype == 0x0006)
902 return outgoingim(sess, mod, rx, snac, data, datalen);
903 else if (snac->subtype == 0x0007)
904 return incomingim(sess, mod, rx, snac, data, datalen);
905 else if (snac->subtype == 0x000a)
906 return missedcall(sess, mod, rx, snac, data, datalen);
907 else if (snac->subtype == 0x000c)
908 return msgack(sess, mod, rx, snac, data, datalen);
909
910 return 0;
911}
912
913faim_internal int msg_modfirst(struct aim_session_t *sess, aim_module_t *mod)
914{
915
916 mod->family = 0x0004;
917 mod->version = 0x0000;
918 mod->flags = 0;
919 strncpy(mod->name, "messaging", sizeof(mod->name));
920 mod->snachandler = snachandler;
921
922 return 0;
96f8b1ed 923}
This page took 2.880241 seconds and 5 git commands to generate.