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