]> andersk Git - libfaim.git/blob - src/im.c
- Wed Oct 3 11:07:22 PDT 2001
[libfaim.git] / src / im.c
1 /*
2  *  aim_im.c
3  *
4  *  The routines for sending/receiving Instant Messages.
5  *
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  *
20  */
21
22 #define FAIM_INTERNAL
23 #include <aim.h>
24
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
41  *
42  * Note that in this function, only the feature bytes are tested, since
43  * the rest will always be the same.
44  *
45  */
46 faim_export fu16_t aim_fingerprintclient(fu8_t *msghdr, int len)
47 {
48         static const struct {
49                 fu16_t clientid;
50                 int len;
51                 fu8_t data[10];
52         } fingerprints[] = {
53                 /* AOL Mobile Communicator, WinAIM 1.0.414 */
54                 { AIM_CLIENTTYPE_MC, 
55                   3, {0x01, 0x01, 0x01}},
56
57                 /* WinAIM 2.0.847, 2.1.1187, 3.0.1464, 4.3.2229, 4.4.2286 */
58                 { AIM_CLIENTTYPE_WINAIM, 
59                   3, {0x01, 0x01, 0x02}},
60
61                 /* WinAIM 4.1.2010, libfaim */
62                 { AIM_CLIENTTYPE_WINAIM41,
63                   4, {0x01, 0x01, 0x01, 0x02}},
64
65                 /* AOL v6.0, CompuServe 2000 v6.0, any TOC client */
66                 { AIM_CLIENTTYPE_AOL_TOC,
67                   1, {0x01}},
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;
84 }
85
86 /* This should be endian-safe now... but who knows... */
87 faim_export fu32_t aim_iconsum(const fu8_t *buf, int buflen)
88 {
89         fu32_t sum;
90         int i;
91
92         for (i = 0, sum = 0; i < buflen; i += 2)
93                 sum += (buf[i+1] << 8) + buf[i];
94
95         sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff);
96
97         return sum;
98 }
99
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)
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).
134  *
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 more precise verification that we never send SNACs larger than 8192
142  * XXX check SNAC size for multipart
143  *
144  */
145 faim_export int aim_send_im_ext(aim_session_t *sess, aim_conn_t *conn, struct aim_sendimext_args *args)
146 {
147         static const fu8_t deffeatures[] = {
148                 0x01, 0x01, 0x01, 0x02
149         };
150         int i, msgtlvlen;
151         aim_frame_t *fr;
152         aim_snacid_t snacid;
153
154         if (!sess || !conn || !args)
155                 return -EINVAL;
156
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         }
167
168         /* Painfully calculate the size of the message TLV */
169         msgtlvlen = 1 + 1; /* 0501 */
170
171         if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES)
172                 msgtlvlen += 2 + args->featureslen;
173         else
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)))
191                 return -ENOMEM;
192
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);
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          */
206         for (i = 0; i < 8; i++)
207                 aimbs_put8(&fr->data, (fu8_t) rand());
208
209         /*
210          * Channel ID
211          */
212         aimbs_put16(&fr->data, 0x0001);
213
214         /*
215          * Destination SN (prepended with byte length)
216          */
217         aimbs_put8(&fr->data, strlen(args->destsn));
218         aimbs_putraw(&fr->data, args->destsn, strlen(args->destsn));
219
220         /*
221          * metaTLV start.
222          */
223         aimbs_put16(&fr->data, 0x0002);
224         aimbs_put16(&fr->data, msgtlvlen);
225
226         /*
227          * Features 
228          *
229          */
230         aimbs_put8(&fr->data, 0x05);
231         aimbs_put8(&fr->data, 0x01);
232
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         }
240
241         if (args->flags & AIM_IMFLAGS_MULTIPART) {
242                 aim_mpmsg_section_t *sec;
243
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                 }
251
252         } else {
253
254                 aimbs_put16(&fr->data, 0x0101);
255
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         }
285
286         /*
287          * Set the Request Acknowledge flag.  
288          */
289         if (args->flags & AIM_IMFLAGS_ACK) {
290                 aimbs_put16(&fr->data, 0x0003);
291                 aimbs_put16(&fr->data, 0x0000);
292         }
293
294         /*
295          * Set the Autoresponse flag.
296          */
297         if (args->flags & AIM_IMFLAGS_AWAY) {
298                 aimbs_put16(&fr->data, 0x0004);
299                 aimbs_put16(&fr->data, 0x0000);
300         }
301
302         /*
303          * Set the Buddy Icon Requested flag.
304          */
305         if (args->flags & AIM_IMFLAGS_BUDDYREQ) {
306                 aimbs_put16(&fr->data, 0x0009);
307                 aimbs_put16(&fr->data, 0x0000);
308         }
309
310         /*
311          * Set the I HAVE A REALLY PURTY ICON flag.
312          */
313         if (args->flags & AIM_IMFLAGS_HASICON) {
314                 aimbs_put16(&fr->data, 0x0008);
315                 aimbs_put16(&fr->data, 0x000c);
316                 aimbs_put32(&fr->data, args->iconlen);
317                 aimbs_put32(&fr->data, args->iconsum);
318                 aimbs_put32(&fr->data, args->iconstamp);
319         }
320
321         aim_tx_enqueue(sess, fr);
322
323 #if 1 /* XXX do this with autoconf or something... */
324         aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
325 #endif
326
327         return 0;
328 }
329
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  */
340 faim_export int aim_send_im(aim_session_t *sess, aim_conn_t *conn, const char *destsn, fu16_t flags, const char *msg)
341 {
342         struct aim_sendimext_args args;
343
344         args.destsn = destsn;
345         args.flags = flags;
346         args.msg = msg;
347         args.msglen = strlen(msg);
348
349         /* Make these don't get set by accident -- they need aim_send_im_ext */
350         args.flags &= ~(AIM_IMFLAGS_CUSTOMFEATURES | AIM_IMFLAGS_HASICON | AIM_IMFLAGS_MULTIPART);
351
352         return aim_send_im_ext(sess, conn, &args);
353 }
354
355 /*
356  * This is also performance sensitive. (If you can believe it...)
357  *
358  */
359 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)
360 {
361         int i;
362         fu8_t ck[8];
363         aim_frame_t *fr;
364         aim_snacid_t snacid;
365
366         if (!sess || !conn || !sn || !icon || (iconlen <= 0) || (iconlen >= MAXICONLEN))
367                 return -EINVAL;
368
369         if (conn->type != AIM_CONN_TYPE_BOS)
370                 return -EINVAL;
371
372         for (i = 0; i < 8; i++)
373                 aimutil_put8(ck+i, (fu8_t) rand());
374
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)))
376                 return -ENOMEM;
377
378         snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
379         aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
380
381         /*
382          * Cookie
383          */
384         aimbs_putraw(&fr->data, ck, 8);
385
386         /*
387          * Channel (2)
388          */
389         aimbs_put16(&fr->data, 0x0002);
390
391         /*
392          * Dest sn
393          */
394         aimbs_put8(&fr->data, strlen(sn));
395         aimbs_putraw(&fr->data, sn, strlen(sn));
396
397         /*
398          * TLV t(0005)
399          *
400          * Encompasses everything below.
401          */
402         aimbs_put16(&fr->data, 0x0005);
403         aimbs_put16(&fr->data, 2+8+16+6+4+4+iconlen+4+4+4+strlen(AIM_ICONIDENT));
404
405         aimbs_put16(&fr->data, 0x0000);
406         aimbs_putraw(&fr->data, ck, 8);
407         aim_putcap(&fr->data, AIM_CAPS_BUDDYICON);
408
409         /* TLV t(000a) */
410         aimbs_put16(&fr->data, 0x000a);
411         aimbs_put16(&fr->data, 0x0002);
412         aimbs_put16(&fr->data, 0x0001);
413
414         /* TLV t(000f) */
415         aimbs_put16(&fr->data, 0x000f);
416         aimbs_put16(&fr->data, 0x0000);
417
418         /* TLV t(2711) */
419         aimbs_put16(&fr->data, 0x2711);
420         aimbs_put16(&fr->data, 4+4+4+iconlen+strlen(AIM_ICONIDENT));
421         aimbs_put32(&fr->data, iconsum);
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));
426
427         /* TLV t(0003) */
428         aimbs_put16(&fr->data, 0x0003);
429         aimbs_put16(&fr->data, 0x0000);
430
431         aim_tx_enqueue(sess, fr);
432
433         return 0;
434 }
435
436 static int outgoingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
437 {
438         int i, ret = 0;
439         aim_rxcallback_t userfunc;
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;
449
450         /* ICBM Cookie. */
451         for (i = 0; i < 8; i++)
452                 cookie[i] = aimbs_get8(bs);
453
454         /* Channel ID */
455         channel = aimbs_get16(bs);
456
457         if (channel != 0x01) {
458                 faimdprintf(sess, 0, "icbm: ICBM recieved on unsupported channel.  Ignoring. (chan = %04x)\n", channel);
459                 return 0;
460         }
461
462         snlen = aimbs_get8(bs);
463         sn = aimbs_getstr(bs, snlen);
464
465         tlvlist = aim_readtlvchain(bs);
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;
471
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);
477
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);
484
485                 msglen = aimbs_get16(&mbs) - 4; /* final block length */
486
487                 flag1 = aimbs_get16(&mbs);
488                 flag2 = aimbs_get16(&mbs);
489
490                 msg = aimbs_getstr(&mbs, msglen);
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
496         free(sn);
497         aim_freetlvchain(&tlvlist);
498
499         return ret;
500 }
501
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  */
539 faim_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
547 static 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
575 faim_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 */
592 faim_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
607 faim_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
630 faim_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  */
655 static 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
773 /*
774  *
775  * This should use tlvlists, but doesn't for performance reasons.
776  *
777  * XXX support multipart IMs:
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  */
797 static 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)
798 {
799         fu16_t type, length;
800         aim_rxcallback_t userfunc;
801         int ret = 0;
802         struct aim_incomingim_ch1_args args;
803         int endpos;
804
805         memset(&args, 0, sizeof(args));
806
807         aim_mpmsg_init(sess, &args.mpmsg);
808
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          */
814         while (aim_bstream_empty(bs)) {
815
816                 type = aimbs_get16(bs);
817                 length = aimbs_get16(bs);
818
819                 endpos = aim_bstream_curpos(bs) + length;
820
821                 if (type == 0x0002) { /* Message Block */
822
823                         /*
824                          * This TLV consists of the following:
825                          *   - 0501 -- Unknown
826                          *   - Features: Don't know how to interpret these
827                          *   - 0101 -- Unknown
828                          *   - Message
829                          *
830                          */
831
832                         aimbs_get8(bs); /* 05 */
833                         aimbs_get8(bs); /* 01 */
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
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);
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
855                 } else if (type == 0x0008) { /* I-HAVE-A-REALLY-PURTY-ICON Flag */
856
857                         args.iconsum = aimbs_get32(bs);
858                         args.iconlen = aimbs_get32(bs);
859                         args.iconstamp = aimbs_get32(bs);
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;
869                         args.extdata = aimbs_getraw(bs, args.extdatalen);
870
871                 } else {
872                         faimdprintf(sess, 0, "incomingim_ch1: unknown TLV 0x%04x (len %d)\n", type, length);
873                 }
874
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);
884         }
885
886
887         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
888                 ret = userfunc(sess, rx, channel, userinfo, &args);
889
890         aim_mpmsg_free(sess, &args.mpmsg);
891         free(args.extdata);
892
893         return ret;
894 }
895
896 static 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)
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
958 static 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)
959 {
960         aim_rxcallback_t userfunc;
961         int ret = 0;
962         aim_tlv_t *miscinfo;
963         aim_bstream_t tbs;
964
965         if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)))
966                 return 0;
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
982 static 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)
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))) 
1003                 ret = userfunc(sess, rx, 0x0002, userinfo, args);
1004
1005         return ret;
1006 }
1007
1008 static 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)
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)))
1030                 ret = userfunc(sess, rx, 0x0002, userinfo, args);
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
1041 static 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)
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)))
1081                 ret = userfunc(sess, rx, 0x0002, userinfo, args);
1082
1083         return ret;
1084 }
1085
1086 static 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)
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
1148 static 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)
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
1206 /* XXX Ugh.  I think its obvious. */
1207 static 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)
1208 {
1209         aim_tlv_t *block1;
1210         aim_tlvlist_t *list2;
1211         int ret = 0;
1212         struct aim_incomingim_ch2_args args;
1213         aim_bstream_t bbs;
1214         fu8_t *cookie2;
1215
1216         memset(&args, 0, sizeof(args));
1217
1218         /*
1219          * There's another block of TLVs embedded in the type 5 here. 
1220          */
1221         if (!(block1 = aim_gettlv(tlvlist, 0x0005, 1)) || !block1->value) {
1222                 faimdprintf(sess, 0, "no tlv 0x0005 in rendezvous transaction!\n");
1223                 return 0;
1224         }
1225
1226         aim_bstream_init(&bbs, block1->value, block1->length);
1227
1228         /*
1229          * First two bytes represent the status of the connection.
1230          *
1231          * 0 is a request, 2 is an accept
1232          */ 
1233         args.status = aimbs_get16(&bbs);
1234
1235         /*
1236          * Next comes the cookie.  Should match the ICBM cookie.
1237          */
1238         cookie2 = aimbs_getraw(&bbs, 8);
1239         if (memcmp(cookie, cookie2, 8) != 0) 
1240                 faimdprintf(sess, 0, "rend: warning cookies don't match!\n");
1241         memcpy(args.cookie, cookie2, 8);
1242         free(cookie2);
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          */
1256         if ((args.reqclass = aim_getcap(sess, &bbs, 0x10)) == 0x0000) {
1257                 faimdprintf(sess, 0, "rend: no ID block\n");
1258                 return 0;
1259         }
1260
1261         /* 
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          */
1267         list2 = aim_readtlvchain(&bbs);
1268
1269 #if 0 /* this should be in the per-type blocks */
1270         if (!list2 || ((args.reqclass != AIM_CAPS_IMIMAGE) && !(aim_gettlv(list2, 0x2711, 1)))) {
1271                 aim_msgcookie_t *cook;
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);
1278                         aim_freetlvchain(&list2);
1279                         return 1;
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
1292                                         if (aim_gettlv(list2, 0x000b, 1))
1293                                                 errorcode = aim_gettlv16(list2, 0x000b, 1);
1294
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);
1298                                 } /* args.status != 0x0002 */
1299
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;
1316         }
1317 #endif
1318
1319         /*
1320          * The rest of the handling depends on what type it is.
1321          */
1322         if (args.reqclass & AIM_CAPS_BUDDYICON)
1323                 ret = incomingim_ch2_buddyicon(sess, mod, rx, snac, userinfo, &args, list2);
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)
1329                 ret = incomingim_ch2_imimage(sess, mod, rx, snac, userinfo, &args, list2);
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
1337                 faimdprintf(sess, 0, "rend: unknown rendezvous 0x%04x\n", args.reqclass);
1338
1339         aim_freetlvchain(&list2);
1340
1341         return ret;
1342 }
1343
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  *
1354  */
1355 static int incomingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1356 {
1357         int i, ret = 0;
1358         fu8_t cookie[8];
1359         fu16_t channel;
1360         aim_userinfo_t userinfo;
1361
1362         memset(&userinfo, 0x00, sizeof(aim_userinfo_t));
1363
1364         /*
1365          * Read ICBM Cookie.  And throw away.
1366          */
1367         for (i = 0; i < 8; i++)
1368                 cookie[i] = aimbs_get8(bs);
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          */
1383         channel = aimbs_get16(bs);
1384
1385         /*
1386          * Technically Channel 3 in chat could be done here too.
1387          */
1388         if ((channel != 0x01) && (channel != 0x02)) {
1389                 faimdprintf(sess, 0, "icbm: ICBM received on an unsupported channel.  Ignoring.\n (chan = %04x)", channel);
1390                 return 0;
1391         }
1392
1393         /*
1394          * Extract the standard user info block.
1395          *
1396          * Note that although this contains TLVs that appear contiguous
1397          * with the TLVs read below, they are two different pieces.  The
1398          * userinfo block contains the number of TLVs that contain user
1399          * information, the rest are not even though there is no seperation.
1400          * aim_extractuserinfo() returns the number of bytes used by the
1401          * userinfo tlvs, so you can start reading the rest of them right
1402          * afterward.  
1403          *
1404          * That also means that TLV types can be duplicated between the
1405          * userinfo block and the rest of the message, however there should
1406          * never be two TLVs of the same type in one block.
1407          * 
1408          */
1409         aim_extractuserinfo(sess, bs, &userinfo);
1410
1411         /*
1412          * From here on, its depends on what channel we're on.
1413          *
1414          * Technically all channels have a TLV list have this, however,
1415          * for the common channel 1 case, in-place parsing is used for
1416          * performance reasons (less memory allocation).
1417          */
1418         if (channel == 1) {
1419
1420                 ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, bs, cookie);
1421
1422         } else if (channel == 0x0002) {
1423                 aim_tlvlist_t *tlvlist;
1424
1425                 /*
1426                  * Read block of TLVs (not including the userinfo data).  All 
1427                  * further data is derived from what is parsed here.
1428                  */
1429                 tlvlist = aim_readtlvchain(bs);
1430
1431                 ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
1432
1433                 /*
1434                  * Free up the TLV chain.
1435                  */
1436                 aim_freetlvchain(&tlvlist);
1437         }
1438
1439         return ret;
1440 }
1441
1442 /*
1443  * Possible codes:
1444  *    AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
1445  *    AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
1446  *    AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
1447  * 
1448  */
1449 faim_export int aim_denytransfer(aim_session_t *sess, aim_conn_t *conn, const char *sender, const char *cookie, fu16_t code)
1450 {
1451         aim_frame_t *fr;
1452         aim_snacid_t snacid;
1453         aim_tlvlist_t *tl = NULL;
1454         
1455         if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sender)+6)))
1456                 return -ENOMEM;
1457
1458         snacid = aim_cachesnac(sess, 0x0004, 0x000b, 0x0000, NULL, 0);
1459         aim_putsnac(&fr->data, 0x0004, 0x000b, 0x0000, snacid);
1460         
1461         aimbs_putraw(&fr->data, cookie, 8);
1462
1463         aimbs_put16(&fr->data, 0x0002); /* channel */
1464         aimbs_put8(&fr->data, strlen(sender));
1465         aimbs_putraw(&fr->data, sender, strlen(sender));
1466
1467         aim_addtlvtochain16(&tl, 0x0003, code);
1468         aim_writetlvchain(&fr->data, &tl);
1469         aim_freetlvchain(&tl);
1470
1471         aim_tx_enqueue(sess, fr);
1472
1473         return 0;
1474 }
1475
1476 /*
1477  * aim_reqicbmparaminfo()
1478  *
1479  * Request ICBM parameter information.
1480  *
1481  */
1482 faim_export int aim_reqicbmparams(aim_session_t *sess, aim_conn_t *conn)
1483 {
1484         return aim_genericreq_n(sess, conn, 0x0004, 0x0004);
1485 }
1486
1487 /*
1488  *
1489  * I definitly recommend sending this.  If you don't, you'll be stuck
1490  * with the rather unreasonable defaults.  You don't want those.  Send this.
1491  * 
1492  */
1493 faim_export int aim_seticbmparam(aim_session_t *sess, aim_conn_t *conn, struct aim_icbmparameters *params)
1494 {
1495         aim_frame_t *fr;
1496         aim_snacid_t snacid;
1497
1498         if (!sess || !conn || !params)
1499                 return -EINVAL;
1500
1501         if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+16)))
1502                 return -ENOMEM;
1503
1504         snacid = aim_cachesnac(sess, 0x0004, 0x0002, 0x0000, NULL, 0);
1505         aim_putsnac(&fr->data, 0x0004, 0x0002, 0x0000, snacid);
1506
1507         /* This is read-only (see Parameter Reply). Must be set to zero here. */
1508         aimbs_put16(&fr->data, 0x0000);
1509
1510         /* These are all read-write */
1511         aimbs_put32(&fr->data, params->flags); 
1512         aimbs_put16(&fr->data, params->maxmsglen);
1513         aimbs_put16(&fr->data, params->maxsenderwarn); 
1514         aimbs_put16(&fr->data, params->maxrecverwarn); 
1515         aimbs_put32(&fr->data, params->minmsginterval);
1516
1517         aim_tx_enqueue(sess, fr);
1518
1519         return 0;
1520 }
1521
1522 static int paraminfo(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1523 {
1524         struct aim_icbmparameters params;
1525         aim_rxcallback_t userfunc;
1526
1527         params.maxchan = aimbs_get16(bs);
1528         params.flags = aimbs_get32(bs);
1529         params.maxmsglen = aimbs_get16(bs);
1530         params.maxsenderwarn = aimbs_get16(bs);
1531         params.maxrecverwarn = aimbs_get16(bs);
1532         params.minmsginterval = aimbs_get32(bs);
1533         
1534         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1535                 return userfunc(sess, rx, &params);
1536
1537         return 0;
1538 }
1539
1540 static int missedcall(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1541 {
1542         int ret = 0;
1543         aim_rxcallback_t userfunc;
1544         fu16_t channel, nummissed, reason;
1545         aim_userinfo_t userinfo;
1546
1547         while (aim_bstream_empty(bs)) { 
1548
1549                 channel = aimbs_get16(bs);
1550                 aim_extractuserinfo(sess, bs, &userinfo);
1551                 nummissed = aimbs_get16(bs);
1552                 reason = aimbs_get16(bs);
1553
1554                 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1555                          ret = userfunc(sess, rx, channel, &userinfo, nummissed, reason);
1556         }
1557
1558         return ret;
1559 }
1560
1561 static int clienterr(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1562 {
1563         int ret = 0;
1564         aim_rxcallback_t userfunc;
1565         fu16_t channel, reason;
1566         char *sn;
1567         fu8_t *ck, snlen;
1568
1569         ck = aimbs_getraw(bs, 8);
1570         channel = aimbs_get16(bs);
1571         snlen = aimbs_get8(bs);
1572         sn = aimbs_getstr(bs, snlen);
1573         reason = aimbs_get16(bs);
1574
1575         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1576                  ret = userfunc(sess, rx, channel, sn, reason);
1577
1578         free(ck);
1579         free(sn);
1580
1581         return ret;
1582 }
1583
1584 static int msgack(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1585 {
1586         aim_rxcallback_t userfunc;
1587         fu16_t type;
1588         fu8_t snlen, *ck;
1589         char *sn;
1590         int ret = 0;
1591
1592         ck = aimbs_getraw(bs, 8);
1593         type = aimbs_get16(bs);
1594         snlen = aimbs_get8(bs);
1595         sn = aimbs_getstr(bs, snlen);
1596
1597         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1598                 ret = userfunc(sess, rx, type, sn);
1599
1600         free(sn);
1601         free(ck);
1602
1603         return ret;
1604 }
1605
1606 static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1607 {
1608
1609         if (snac->subtype == 0x0005)
1610                 return paraminfo(sess, mod, rx, snac, bs);
1611         else if (snac->subtype == 0x0006)
1612                 return outgoingim(sess, mod, rx, snac, bs);
1613         else if (snac->subtype == 0x0007)
1614                 return incomingim(sess, mod, rx, snac, bs);
1615         else if (snac->subtype == 0x000a)
1616                 return missedcall(sess, mod, rx, snac, bs);
1617         else if (snac->subtype == 0x000b)
1618                 return clienterr(sess, mod, rx, snac, bs);
1619         else if (snac->subtype == 0x000c)
1620                 return msgack(sess, mod, rx, snac, bs);
1621
1622         return 0;
1623 }
1624
1625 faim_internal int msg_modfirst(aim_session_t *sess, aim_module_t *mod)
1626 {
1627
1628         mod->family = 0x0004;
1629         mod->version = 0x0000;
1630         mod->flags = 0;
1631         strncpy(mod->name, "messaging", sizeof(mod->name));
1632         mod->snachandler = snachandler;
1633
1634         return 0;
1635 }
This page took 0.532412 seconds and 5 git commands to generate.