source: protocols/oscar/im.c @ b7d3cc34

0.99
Last change on this file since b7d3cc34 was b7d3cc34, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-06T18:23:18Z

Initial repository (0.99 release tree)

  • Property mode set to 100644
File size: 55.1 KB
Line 
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#include <aim.h>
23#include "im.h"
24#include "info.h"
25
26/*
27 * Takes a msghdr (and a length) and returns a client type
28 * code.  Note that this is *only a guess* and has a low likelihood
29 * of actually being accurate.
30 *
31 * Its based on experimental data, with the help of Eric Warmenhoven
32 * who seems to have collected a wide variety of different AIM clients.
33 *
34 *
35 * Heres the current collection:
36 *  0501 0003 0101 0101 01       AOL Mobile Communicator, WinAIM 1.0.414
37 *  0501 0003 0101 0201 01       WinAIM 2.0.847, 2.1.1187, 3.0.1464,
38 *                                      4.3.2229, 4.4.2286
39 *  0501 0004 0101 0102 0101     WinAIM 4.1.2010, libfaim (right here)
40 *  0501 0001 0101 01            AOL v6.0, CompuServe 2000 v6.0, any
41 *                                      TOC client
42 *
43 * Note that in this function, only the feature bytes are tested, since
44 * the rest will always be the same.
45 *
46 */
47guint16 aim_fingerprintclient(guint8 *msghdr, int len)
48{
49        static const struct {
50                guint16 clientid;
51                int len;
52                guint8 data[10];
53        } fingerprints[] = {
54                /* AOL Mobile Communicator, WinAIM 1.0.414 */
55                { AIM_CLIENTTYPE_MC, 
56                  3, {0x01, 0x01, 0x01}},
57
58                /* WinAIM 2.0.847, 2.1.1187, 3.0.1464, 4.3.2229, 4.4.2286 */
59                { AIM_CLIENTTYPE_WINAIM, 
60                  3, {0x01, 0x01, 0x02}},
61
62                /* WinAIM 4.1.2010, libfaim */
63                { AIM_CLIENTTYPE_WINAIM41,
64                  4, {0x01, 0x01, 0x01, 0x02}},
65
66                /* AOL v6.0, CompuServe 2000 v6.0, any TOC client */
67                { AIM_CLIENTTYPE_AOL_TOC,
68                  1, {0x01}},
69
70                { 0, 0}
71        };
72        int i;
73
74        if (!msghdr || (len <= 0))
75                return AIM_CLIENTTYPE_UNKNOWN;
76
77        for (i = 0; fingerprints[i].len; i++) {
78                if (fingerprints[i].len != len)
79                        continue;
80                if (memcmp(fingerprints[i].data, msghdr, fingerprints[i].len) == 0)
81                        return fingerprints[i].clientid;
82        }
83
84        return AIM_CLIENTTYPE_UNKNOWN;
85}
86
87/* This should be endian-safe now... but who knows... */
88guint16 aim_iconsum(const guint8 *buf, int buflen)
89{
90        guint32 sum;
91        int i;
92
93        for (i = 0, sum = 0; i + 1 < buflen; i += 2)
94                sum += (buf[i+1] << 8) + buf[i];
95        if (i < buflen)
96                sum += buf[i];
97
98        sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff);
99
100        return (guint16)sum;
101}
102
103/*
104 * Send an ICBM (instant message). 
105 *
106 *
107 * Possible flags:
108 *   AIM_IMFLAGS_AWAY  -- Marks the message as an autoresponse
109 *   AIM_IMFLAGS_ACK   -- Requests that the server send an ack
110 *                        when the message is received (of type 0x0004/0x000c)
111 *   AIM_IMFLAGS_OFFLINE--If destination is offline, store it until they are
112 *                        online (probably ICQ only).
113 *   AIM_IMFLAGS_UNICODE--Instead of ASCII7, the passed message is
114 *                        made up of UNICODE duples.  If you set
115 *                        this, you'd better be damn sure you know
116 *                        what you're doing.
117 *   AIM_IMFLAGS_ISO_8859_1 -- The message contains the ASCII8 subset
118 *                        known as ISO-8859-1. 
119 *
120 * Generally, you should use the lowest encoding possible to send
121 * your message.  If you only use basic punctuation and the generic
122 * Latin alphabet, use ASCII7 (no flags).  If you happen to use non-ASCII7
123 * characters, but they are all clearly defined in ISO-8859-1, then
124 * use that.  Keep in mind that not all characters in the PC ASCII8
125 * character set are defined in the ISO standard. For those cases (most
126 * notably when the (r) symbol is used), you must use the full UNICODE
127 * encoding for your message.  In UNICODE mode, _all_ characters must
128 * occupy 16bits, including ones that are not special.  (Remember that
129 * the first 128 UNICODE symbols are equivelent to ASCII7, however they
130 * must be prefixed with a zero high order byte.)
131 *
132 * I strongly discourage the use of UNICODE mode, mainly because none
133 * of the clients I use can parse those messages (and besides that,
134 * wchars are difficult and non-portable to handle in most UNIX environments).
135 * If you really need to include special characters, use the HTML UNICODE
136 * entities.  These are of the form &#2026; where 2026 is the hex
137 * representation of the UNICODE index (in this case, UNICODE
138 * "Horizontal Ellipsis", or 133 in in ASCII8).
139 *
140 * Implementation note:  Since this is one of the most-used functions
141 * in all of libfaim, it is written with performance in mind.  As such,
142 * it is not as clear as it could be in respect to how this message is
143 * supposed to be layed out. Most obviously, tlvlists should be used
144 * instead of writing out the bytes manually.
145 *
146 * XXX more precise verification that we never send SNACs larger than 8192
147 * XXX check SNAC size for multipart
148 *
149 */
150int aim_send_im_ext(aim_session_t *sess, struct aim_sendimext_args *args)
151{
152        static const guint8 deffeatures[] = {
153                0x01, 0x01, 0x01, 0x02
154        };
155        aim_conn_t *conn;
156        int i, msgtlvlen;
157        aim_frame_t *fr;
158        aim_snacid_t snacid;
159
160        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
161                return -EINVAL;
162
163        if (!args)
164                return -EINVAL;
165
166        if (args->flags & AIM_IMFLAGS_MULTIPART) {
167                if (args->mpmsg->numparts <= 0)
168                        return -EINVAL;
169        } else {
170                if (!args->msg || (args->msglen <= 0))
171                        return -EINVAL;
172
173                if (args->msglen >= MAXMSGLEN)
174                        return -E2BIG;
175        }
176
177        /* Painfully calculate the size of the message TLV */
178        msgtlvlen = 1 + 1; /* 0501 */
179
180        if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES)
181                msgtlvlen += 2 + args->featureslen;
182        else
183                msgtlvlen += 2 + sizeof(deffeatures);
184
185        if (args->flags & AIM_IMFLAGS_MULTIPART) {
186                aim_mpmsg_section_t *sec;
187
188                for (sec = args->mpmsg->parts; sec; sec = sec->next) {
189                        msgtlvlen += 2 /* 0101 */ + 2 /* block len */;
190                        msgtlvlen += 4 /* charset */ + sec->datalen;
191                }
192
193        } else {
194                msgtlvlen += 2 /* 0101 */ + 2 /* block len */;
195                msgtlvlen += 4 /* charset */ + args->msglen;
196        }
197
198
199        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, msgtlvlen+128)))
200                return -ENOMEM;
201
202        /* XXX should be optional */   
203        snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, args->destsn, strlen(args->destsn)+1);
204        aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
205
206        /*
207         * Generate a random message cookie
208         *
209         * We could cache these like we do SNAC IDs.  (In fact, it
210         * might be a good idea.)  In the message error functions,
211         * the 8byte message cookie is returned as well as the
212         * SNAC ID.
213         *
214         */
215        for (i = 0; i < 8; i++)
216                aimbs_put8(&fr->data, (guint8) rand());
217
218        /*
219         * Channel ID
220         */
221        aimbs_put16(&fr->data, 0x0001);
222
223        /*
224         * Destination SN (prepended with byte length)
225         */
226        aimbs_put8(&fr->data, strlen(args->destsn));
227        aimbs_putraw(&fr->data, (guint8 *)args->destsn, strlen(args->destsn));
228
229        /*
230         * Message TLV (type 2).
231         */
232        aimbs_put16(&fr->data, 0x0002);
233        aimbs_put16(&fr->data, msgtlvlen);
234
235        /*
236         * Features
237         *
238         */
239        aimbs_put8(&fr->data, 0x05);
240        aimbs_put8(&fr->data, 0x01);
241
242        if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES) {
243                aimbs_put16(&fr->data, args->featureslen);
244                aimbs_putraw(&fr->data, args->features, args->featureslen);
245        } else {
246                aimbs_put16(&fr->data, sizeof(deffeatures));
247                aimbs_putraw(&fr->data, deffeatures, sizeof(deffeatures));
248        }
249
250        if (args->flags & AIM_IMFLAGS_MULTIPART) {
251                aim_mpmsg_section_t *sec;
252
253                for (sec = args->mpmsg->parts; sec; sec = sec->next) {
254                        aimbs_put16(&fr->data, 0x0101);
255                        aimbs_put16(&fr->data, sec->datalen + 4);
256                        aimbs_put16(&fr->data, sec->charset);
257                        aimbs_put16(&fr->data, sec->charsubset);
258                        aimbs_putraw(&fr->data, sec->data, sec->datalen);
259                }
260
261        } else {
262
263                aimbs_put16(&fr->data, 0x0101);
264
265                /*
266                 * Message block length.
267                 */
268                aimbs_put16(&fr->data, args->msglen + 0x04);
269
270                /*
271                 * Character set.
272                 */
273                if (args->flags & AIM_IMFLAGS_CUSTOMCHARSET) {
274
275                        aimbs_put16(&fr->data, args->charset);
276                        aimbs_put16(&fr->data, args->charsubset);
277
278                } else {
279                        if (args->flags & AIM_IMFLAGS_UNICODE)
280                                aimbs_put16(&fr->data, 0x0002);
281                        else if (args->flags & AIM_IMFLAGS_ISO_8859_1)
282                                aimbs_put16(&fr->data, 0x0003);
283                        else
284                                aimbs_put16(&fr->data, 0x0000);
285
286                        aimbs_put16(&fr->data, 0x0000);
287                }
288
289                /*
290                 * Message.  Not terminated.
291                 */
292                aimbs_putraw(&fr->data, (guint8 *)args->msg, args->msglen);
293        }
294
295        /*
296         * Set the Request Acknowledge flag. 
297         */
298        if (args->flags & AIM_IMFLAGS_ACK) {
299                aimbs_put16(&fr->data, 0x0003);
300                aimbs_put16(&fr->data, 0x0000);
301        }
302
303        /*
304         * Set the Autoresponse flag.
305         */
306        if (args->flags & AIM_IMFLAGS_AWAY) {
307                aimbs_put16(&fr->data, 0x0004);
308                aimbs_put16(&fr->data, 0x0000);
309        }
310
311        if (args->flags & AIM_IMFLAGS_OFFLINE) {
312                aimbs_put16(&fr->data, 0x0006);
313                aimbs_put16(&fr->data, 0x0000);
314        }
315
316        /*
317         * Set the I HAVE A REALLY PURTY ICON flag.
318         */
319        if (args->flags & AIM_IMFLAGS_HASICON) {
320                aimbs_put16(&fr->data, 0x0008);
321                aimbs_put16(&fr->data, 0x000c);
322                aimbs_put32(&fr->data, args->iconlen);
323                aimbs_put16(&fr->data, 0x0001);
324                aimbs_put16(&fr->data, args->iconsum);
325                aimbs_put32(&fr->data, args->iconstamp);
326        }
327
328        /*
329         * Set the Buddy Icon Requested flag.
330         */
331        if (args->flags & AIM_IMFLAGS_BUDDYREQ) {
332                aimbs_put16(&fr->data, 0x0009);
333                aimbs_put16(&fr->data, 0x0000);
334        }
335
336        aim_tx_enqueue(sess, fr);
337
338        if (!(sess->flags & AIM_SESS_FLAGS_DONTTIMEOUTONICBM))
339                aim_cleansnacs(sess, 60); /* clean out SNACs over 60sec old */
340
341        return 0;
342}
343
344/*
345 * Simple wrapper for aim_send_im_ext()
346 *
347 * You cannot use aim_send_im if you need the HASICON flag.  You must
348 * use aim_send_im_ext directly for that.
349 *
350 * aim_send_im also cannot be used if you require UNICODE messages, because
351 * that requires an explicit message length.  Use aim_send_im_ext().
352 *
353 */
354int aim_send_im(aim_session_t *sess, const char *destsn, guint16 flags, const char *msg)
355{
356        struct aim_sendimext_args args;
357
358        args.destsn = destsn;
359        args.flags = flags;
360        args.msg = msg;
361        args.msglen = strlen(msg);
362
363        /* Make these don't get set by accident -- they need aim_send_im_ext */
364        args.flags &= ~(AIM_IMFLAGS_CUSTOMFEATURES | AIM_IMFLAGS_HASICON | AIM_IMFLAGS_MULTIPART);
365
366        return aim_send_im_ext(sess, &args);
367}
368
369/*
370 * This is also performance sensitive. (If you can believe it...)
371 *
372 */
373int aim_send_icon(aim_session_t *sess, const char *sn, const guint8 *icon, int iconlen, time_t stamp, guint16 iconsum)
374{
375        aim_conn_t *conn;
376        int i;
377        guint8 ck[8];
378        aim_frame_t *fr;
379        aim_snacid_t snacid;
380
381        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
382                return -EINVAL;
383
384        if (!sn || !icon || (iconlen <= 0) || (iconlen >= MAXICONLEN))
385                return -EINVAL;
386
387        for (i = 0; i < 8; i++)
388                aimutil_put8(ck+i, (guint8) rand());
389
390        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)))
391                return -ENOMEM;
392
393        snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
394        aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
395
396        /*
397         * Cookie
398         */
399        aimbs_putraw(&fr->data, ck, 8);
400
401        /*
402         * Channel (2)
403         */
404        aimbs_put16(&fr->data, 0x0002);
405
406        /*
407         * Dest sn
408         */
409        aimbs_put8(&fr->data, strlen(sn));
410        aimbs_putraw(&fr->data, (guint8 *)sn, strlen(sn));
411
412        /*
413         * TLV t(0005)
414         *
415         * Encompasses everything below.
416         */
417        aimbs_put16(&fr->data, 0x0005);
418        aimbs_put16(&fr->data, 2+8+16+6+4+4+iconlen+4+4+4+strlen(AIM_ICONIDENT));
419
420        aimbs_put16(&fr->data, 0x0000);
421        aimbs_putraw(&fr->data, ck, 8);
422        aim_putcap(&fr->data, AIM_CAPS_BUDDYICON);
423
424        /* TLV t(000a) */
425        aimbs_put16(&fr->data, 0x000a);
426        aimbs_put16(&fr->data, 0x0002);
427        aimbs_put16(&fr->data, 0x0001);
428
429        /* TLV t(000f) */
430        aimbs_put16(&fr->data, 0x000f);
431        aimbs_put16(&fr->data, 0x0000);
432
433        /* TLV t(2711) */
434        aimbs_put16(&fr->data, 0x2711);
435        aimbs_put16(&fr->data, 4+4+4+iconlen+strlen(AIM_ICONIDENT));
436        aimbs_put16(&fr->data, 0x0000);
437        aimbs_put16(&fr->data, iconsum);
438        aimbs_put32(&fr->data, iconlen);
439        aimbs_put32(&fr->data, stamp);
440        aimbs_putraw(&fr->data, icon, iconlen);
441        aimbs_putraw(&fr->data, (guint8 *)AIM_ICONIDENT, strlen(AIM_ICONIDENT));
442
443        /* TLV t(0003) */
444        aimbs_put16(&fr->data, 0x0003);
445        aimbs_put16(&fr->data, 0x0000);
446
447        aim_tx_enqueue(sess, fr);
448
449        return 0;
450}
451
452/*
453 * This only works for ICQ 2001b (thats 2001 not 2000).  Better, only
454 * send it to clients advertising the RTF capability.  In fact, if you send
455 * it to a client that doesn't support that capability, the server will gladly
456 * bounce it back to you.
457 *
458 * You'd think this would be in icq.c, but, well, I'm trying to stick with
459 * the one-group-per-file scheme as much as possible.  This could easily
460 * be an exception, since Rendezvous IMs are external of the Oscar core,
461 * and therefore are undefined.  Really I just need to think of a good way to
462 * make an interface similar to what AOL actually uses.  But I'm not using COM.
463 *
464 */
465int aim_send_rtfmsg(aim_session_t *sess, struct aim_sendrtfmsg_args *args)
466{
467        const char rtfcap[] = {"{97B12751-243C-4334-AD22-D6ABF73F1492}"}; /* AIM_CAPS_ICQRTF capability in string form */
468        aim_conn_t *conn;
469        int i;
470        guint8 ck[8];
471        aim_frame_t *fr;
472        aim_snacid_t snacid;
473        int servdatalen;
474
475        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
476                return -EINVAL;
477
478        if (!args || !args->destsn || !args->rtfmsg)
479                return -EINVAL;
480
481        servdatalen = 2+2+16+2+4+1+2  +  2+2+4+4+4  +  2+4+2+strlen(args->rtfmsg)+1  +  4+4+4+strlen(rtfcap)+1;
482
483        for (i = 0; i < 8; i++)
484                aimutil_put8(ck+i, (guint8) rand());
485
486        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+128+servdatalen)))
487                return -ENOMEM;
488
489        snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
490        aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
491
492        /*
493         * Cookie
494         */
495        aimbs_putraw(&fr->data, ck, 8);
496
497        /*
498         * Channel (2)
499         */
500        aimbs_put16(&fr->data, 0x0002);
501
502        /*
503         * Dest sn
504         */
505        aimbs_put8(&fr->data, strlen(args->destsn));
506        aimbs_putraw(&fr->data, (guint8 *)args->destsn, strlen(args->destsn));
507
508        /*
509         * TLV t(0005)
510         *
511         * Encompasses everything below.
512         */
513        aimbs_put16(&fr->data, 0x0005);
514        aimbs_put16(&fr->data, 2+8+16  +  2+2+2  +  2+2  +  2+2+servdatalen);
515
516        aimbs_put16(&fr->data, 0x0000);
517        aimbs_putraw(&fr->data, ck, 8);
518        aim_putcap(&fr->data, AIM_CAPS_ICQSERVERRELAY);
519
520        /*
521         * t(000a) l(0002) v(0001)
522         */
523        aimbs_put16(&fr->data, 0x000a);
524        aimbs_put16(&fr->data, 0x0002);
525        aimbs_put16(&fr->data, 0x0001);
526
527        /*
528         * t(000f) l(0000) v()
529         */
530        aimbs_put16(&fr->data, 0x000f);
531        aimbs_put16(&fr->data, 0x0000);
532
533        /*
534         * Service Data TLV
535         */
536        aimbs_put16(&fr->data, 0x2711);
537        aimbs_put16(&fr->data, servdatalen);
538
539        aimbs_putle16(&fr->data, 11 + 16 /* 11 + (sizeof CLSID) */);
540        aimbs_putle16(&fr->data, 9);
541        aim_putcap(&fr->data, AIM_CAPS_EMPTY);
542        aimbs_putle16(&fr->data, 0);
543        aimbs_putle32(&fr->data, 0);
544        aimbs_putle8(&fr->data, 0);
545        aimbs_putle16(&fr->data, 0x03ea); /* trid1 */
546
547        aimbs_putle16(&fr->data, 14);
548        aimbs_putle16(&fr->data, 0x03eb); /* trid2 */
549        aimbs_putle32(&fr->data, 0);
550        aimbs_putle32(&fr->data, 0);
551        aimbs_putle32(&fr->data, 0);
552
553        aimbs_putle16(&fr->data, 0x0001);
554        aimbs_putle32(&fr->data, 0);
555        aimbs_putle16(&fr->data, strlen(args->rtfmsg)+1);
556        aimbs_putraw(&fr->data, (guint8 *)args->rtfmsg, strlen(args->rtfmsg)+1);
557
558        aimbs_putle32(&fr->data, args->fgcolor);
559        aimbs_putle32(&fr->data, args->bgcolor);
560        aimbs_putle32(&fr->data, strlen(rtfcap)+1);
561        aimbs_putraw(&fr->data, (guint8 *)rtfcap, strlen(rtfcap)+1);
562
563        aim_tx_enqueue(sess, fr);
564
565        return 0;
566}
567
568int aim_request_directim(aim_session_t *sess, const char *destsn, guint8 *ip, guint16 port, guint8 *ckret)
569{
570        aim_conn_t *conn;
571        guint8 ck[8];
572        aim_frame_t *fr;
573        aim_snacid_t snacid;
574        aim_tlvlist_t *tl = NULL, *itl = NULL;
575        int hdrlen, i;
576        guint8 *hdr;
577        aim_bstream_t hdrbs;
578
579        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
580                return -EINVAL;
581
582        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 256+strlen(destsn))))
583                return -ENOMEM;
584
585        snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
586        aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
587
588        /*
589         * Generate a random message cookie
590         *
591         * This cookie needs to be alphanumeric and NULL-terminated to be
592         * TOC-compatible.
593         *
594         * XXX have I mentioned these should be generated in msgcookie.c?
595         *
596         */
597        for (i = 0; i < 7; i++)
598                ck[i] = 0x30 + ((guint8) rand() % 10);
599        ck[7] = '\0';
600
601        if (ckret)
602                memcpy(ckret, ck, 8);
603
604        /* Cookie */
605        aimbs_putraw(&fr->data, ck, 8);
606
607        /* Channel */
608        aimbs_put16(&fr->data, 0x0002);
609
610        /* Destination SN */
611        aimbs_put8(&fr->data, strlen(destsn));
612        aimbs_putraw(&fr->data, (guint8 *)destsn, strlen(destsn));
613
614        aim_addtlvtochain_noval(&tl, 0x0003);
615
616        hdrlen = 2+8+16+6+8+6+4;
617        hdr = g_malloc(hdrlen);
618        aim_bstream_init(&hdrbs, hdr, hdrlen);
619
620        aimbs_put16(&hdrbs, 0x0000);
621        aimbs_putraw(&hdrbs, ck, 8);
622        aim_putcap(&hdrbs, AIM_CAPS_IMIMAGE);
623
624        aim_addtlvtochain16(&itl, 0x000a, 0x0001);
625        aim_addtlvtochain_raw(&itl, 0x0003, 4, ip);
626        aim_addtlvtochain16(&itl, 0x0005, port);
627        aim_addtlvtochain_noval(&itl, 0x000f);
628       
629        aim_writetlvchain(&hdrbs, &itl);
630
631        aim_addtlvtochain_raw(&tl, 0x0005, aim_bstream_curpos(&hdrbs), hdr);
632
633        aim_writetlvchain(&fr->data, &tl);
634
635        g_free(hdr);
636        aim_freetlvchain(&itl);
637        aim_freetlvchain(&tl);
638
639        aim_tx_enqueue(sess, fr);
640
641        return 0;
642}
643
644int aim_request_sendfile(aim_session_t *sess, const char *sn, const char *filename, guint16 numfiles, guint32 totsize, guint8 *ip, guint16 port, guint8 *ckret)
645{
646        aim_conn_t *conn;
647        int i;
648        guint8 ck[8];
649        aim_frame_t *fr;
650        aim_snacid_t snacid;
651
652        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
653                return -EINVAL;
654
655        if (!sn || !filename)
656                return -EINVAL;
657
658        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sn)+2+2+2+8+16+6+8+6+4+2+2+2+2+4+strlen(filename)+4)))
659                return -ENOMEM;
660
661        snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
662        aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
663
664        for (i = 0; i < 7; i++)
665                aimutil_put8(ck+i, 0x30 + ((guint8) rand() % 10));
666        ck[7] = '\0';
667
668        if (ckret)
669                memcpy(ckret, ck, 8);
670
671        /*
672         * Cookie
673         */
674        aimbs_putraw(&fr->data, ck, 8);
675
676        /*
677         * Channel (2)
678         */
679        aimbs_put16(&fr->data, 0x0002);
680
681        /*
682         * Dest sn
683         */
684        aimbs_put8(&fr->data, strlen(sn));
685        aimbs_putraw(&fr->data, (guint8 *)sn, strlen(sn));
686
687        /*
688         * TLV t(0005)
689         *
690         * Encompasses everything below. Gee.
691         */
692        aimbs_put16(&fr->data, 0x0005);
693        aimbs_put16(&fr->data, 2+8+16+6+8+6+4+2+2+2+2+4+strlen(filename)+4);
694
695        aimbs_put16(&fr->data, 0x0000);
696        aimbs_putraw(&fr->data, ck, 8);
697        aim_putcap(&fr->data, AIM_CAPS_SENDFILE);
698
699        /* TLV t(000a) */
700        aimbs_put16(&fr->data, 0x000a);
701        aimbs_put16(&fr->data, 0x0002);
702        aimbs_put16(&fr->data, 0x0001);
703
704        /* TLV t(0003) (IP) */
705        aimbs_put16(&fr->data, 0x0003);
706        aimbs_put16(&fr->data, 0x0004);
707        aimbs_putraw(&fr->data, ip, 4);
708
709        /* TLV t(0005) (port) */
710        aimbs_put16(&fr->data, 0x0005);
711        aimbs_put16(&fr->data, 0x0002);
712        aimbs_put16(&fr->data, port);
713
714        /* TLV t(000f) */
715        aimbs_put16(&fr->data, 0x000f);
716        aimbs_put16(&fr->data, 0x0000);
717
718        /* TLV t(2711) */
719        aimbs_put16(&fr->data, 0x2711);
720        aimbs_put16(&fr->data, 2+2+4+strlen(filename)+4);
721
722        /* ? */
723        aimbs_put16(&fr->data, 0x0001);
724        aimbs_put16(&fr->data, numfiles);
725        aimbs_put32(&fr->data, totsize);
726        aimbs_putraw(&fr->data, (guint8 *)filename, strlen(filename));
727
728        /* ? */
729        aimbs_put32(&fr->data, 0x00000000);
730
731        aim_tx_enqueue(sess, fr);
732
733        return 0;
734}
735
736/**
737 * Request the status message of the given ICQ user.
738 *
739 * @param sess The oscar session.
740 * @param sn The UIN of the user of whom you wish to request info.
741 * @param type The type of info you wish to request.  This should be the current
742 *        state of the user, as one of the AIM_ICQ_STATE_* defines.
743 * @return Return 0 if no errors, otherwise return the error number.
744 */
745int aim_send_im_ch2_geticqmessage(aim_session_t *sess, const char *sn, int type)
746{
747        aim_conn_t *conn;
748        int i;
749        guint8 ck[8];
750        aim_frame_t *fr;
751        aim_snacid_t snacid;
752
753        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)) || !sn)
754                return -EINVAL;
755
756        for (i = 0; i < 8; i++)
757                aimutil_put8(ck+i, (guint8) rand());
758
759        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sn) + 4+0x5e + 4)))
760                return -ENOMEM;
761
762        snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
763        aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
764
765        /* Cookie */
766        aimbs_putraw(&fr->data, ck, 8);
767
768        /* Channel (2) */
769        aimbs_put16(&fr->data, 0x0002);
770
771        /* Dest sn */
772        aimbs_put8(&fr->data, strlen(sn));
773        aimbs_putraw(&fr->data, (guint8 *)sn, strlen(sn));
774
775        /* TLV t(0005) - Encompasses almost everything below. */
776        aimbs_put16(&fr->data, 0x0005); /* T */
777        aimbs_put16(&fr->data, 0x005e); /* L */
778        { /* V */
779                aimbs_put16(&fr->data, 0x0000);
780
781                /* Cookie */
782                aimbs_putraw(&fr->data, ck, 8);
783
784                /* Put the 16 byte server relay capability */
785                aim_putcap(&fr->data, AIM_CAPS_ICQSERVERRELAY);
786
787                /* TLV t(000a) */
788                aimbs_put16(&fr->data, 0x000a);
789                aimbs_put16(&fr->data, 0x0002);
790                aimbs_put16(&fr->data, 0x0001);
791
792                /* TLV t(000f) */
793                aimbs_put16(&fr->data, 0x000f);
794                aimbs_put16(&fr->data, 0x0000);
795
796                /* TLV t(2711) */
797                aimbs_put16(&fr->data, 0x2711);
798                aimbs_put16(&fr->data, 0x0036);
799                { /* V */
800                        aimbs_putle16(&fr->data, 0x001b); /* L */
801                        aimbs_putle16(&fr->data, 0x0008); /* AAA - Protocol version */
802                        aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
803                        aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
804                        aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
805                        aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
806                        aimbs_putle16(&fr->data, 0x0000); /* Unknown */
807                        aimbs_putle16(&fr->data, 0x0003); /* Client features? */
808                        aimbs_putle16(&fr->data, 0x0000); /* Unknown */
809                        aimbs_putle8(&fr->data, 0x00); /* Unkizown */
810                        aimbs_putle16(&fr->data, 0xffff); /* Sequence number?  XXX - This should decrement by 1 with each request */
811
812                        aimbs_putle16(&fr->data, 0x000e); /* L */
813                        aimbs_putle16(&fr->data, 0xffff); /* Sequence number?  XXX - This should decrement by 1 with each request */
814                        aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
815                        aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
816                        aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
817
818                        /* The type of status message being requested */
819                        if (type & AIM_ICQ_STATE_CHAT)
820                                aimbs_putle16(&fr->data, 0x03ec);
821                        else if(type & AIM_ICQ_STATE_DND)
822                                aimbs_putle16(&fr->data, 0x03eb);
823                        else if(type & AIM_ICQ_STATE_OUT)
824                                aimbs_putle16(&fr->data, 0x03ea);
825                        else if(type & AIM_ICQ_STATE_BUSY)
826                                aimbs_putle16(&fr->data, 0x03e9);
827                        else if(type & AIM_ICQ_STATE_AWAY)
828                                aimbs_putle16(&fr->data, 0x03e8);
829
830                        aimbs_putle16(&fr->data, 0x0000); /* Status? */
831                        aimbs_putle16(&fr->data, 0x0001); /* Priority of this message? */
832                        aimbs_putle16(&fr->data, 0x0001); /* L? */
833                        aimbs_putle8(&fr->data, 0x00); /* Null termination? */
834                } /* End TLV t(2711) */
835        } /* End TLV t(0005) */
836
837        /* TLV t(0003) */
838        aimbs_put16(&fr->data, 0x0003);
839        aimbs_put16(&fr->data, 0x0000);
840
841        aim_tx_enqueue(sess, fr);
842
843        return 0;
844}
845
846/**
847 * answers status message requests
848 * @param sess the oscar session
849 * @param sender the guy whos asking
850 * @param cookie message id which we are answering for
851 * @param message away message
852 * @param state our current away state the way icq requests it (0xE8 for away, 0xE9 occupied, ...)
853 * @return 0 if no error
854 */
855int aim_send_im_ch2_statusmessage(aim_session_t *sess, const char *sender, const guint8 *cookie,
856        const char *message, const guint8 state, const guint16 dc)
857{
858    aim_conn_t *conn;
859    aim_frame_t *fr;
860    aim_snacid_t snacid;
861
862    if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
863                return -EINVAL;
864       
865    if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 
866                                        10+8+2+1+strlen(sender)+2+0x1d+0x10+9+strlen(message)+1)))
867                return -ENOMEM;
868
869    snacid = aim_cachesnac(sess, 0x0004, 0x000b, 0x0000, NULL, 0);
870    aim_putsnac(&fr->data, 0x0004, 0x000b, 0x0000, snacid);
871   
872    aimbs_putraw(&fr->data, cookie, 8);
873   
874    aimbs_put16(&fr->data, 0x0002); /* channel */
875    aimbs_put8(&fr->data, strlen(sender));
876    aimbs_putraw(&fr->data, (guint8 *)sender, strlen(sender));
877
878    aimbs_put16(&fr->data, 0x0003); /* reason: channel specific */
879
880    aimbs_putle16(&fr->data, 0x001b); /* length of data SEQ1 */
881    aimbs_putle16(&fr->data, 0x0008); /* protocol version */
882
883    aimbs_putle32(&fr->data, 0x0000); /* no plugin -> 16 times 0x00 */ 
884    aimbs_putle32(&fr->data, 0x0000); 
885    aimbs_putle32(&fr->data, 0x0000); 
886    aimbs_putle32(&fr->data, 0x0000);
887
888    aimbs_putle16(&fr->data, 0x0000); /* unknown */
889    aimbs_putle32(&fr->data, 0x0003); /* client features */
890    aimbs_putle8(&fr->data, 0x00); /* unknown */
891    aimbs_putle16(&fr->data, dc); /* Sequence number?  XXX - This should decrement by 1 with each request */
892    /* end of SEQ1 */
893
894    aimbs_putle16(&fr->data, 0x000e); /* Length of SEQ2 */
895    aimbs_putle16(&fr->data, dc); /* Sequence number? same as above
896                                       * XXX - This should decrement by 1 with each request */
897    aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
898    aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
899    aimbs_putle32(&fr->data, 0x00000000); /* Unknown */
900    /* end of SEQ2 */
901
902    /* now for the real fun */
903    aimbs_putle8(&fr->data, state); /* away state */
904    aimbs_putle8(&fr->data, 0x03); /* msg-flag: 03 for states */
905    aimbs_putle16(&fr->data, 0x0000); /* status code ? */
906    aimbs_putle16(&fr->data, 0x0000); /* priority code */
907    aimbs_putle16(&fr->data, strlen(message) + 1); /* message length + termination */
908    aimbs_putraw(&fr->data, (guint8 *) message, strlen(message) + 1); /* null terminated string */
909   
910    aim_tx_enqueue(sess, fr);
911
912
913    return 0;
914}
915
916
917static int outgoingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
918{
919        int i, ret = 0;
920        aim_rxcallback_t userfunc;
921        guint8 cookie[8];
922        guint16 channel;
923        aim_tlvlist_t *tlvlist;
924        char *sn;
925        int snlen;
926        guint16 icbmflags = 0;
927        guint8 flag1 = 0, flag2 = 0;
928        char *msg = NULL;
929        aim_tlv_t *msgblock;
930
931        /* ICBM Cookie. */
932        for (i = 0; i < 8; i++)
933                cookie[i] = aimbs_get8(bs);
934
935        /* Channel ID */
936        channel = aimbs_get16(bs);
937
938        if (channel != 0x01) {
939                do_error_dialog(sess->aux_data, "icbm: ICBM recieved on unsupported channel.  Ignoring.", "Gaim");
940                return 0;
941        }
942
943        snlen = aimbs_get8(bs);
944        sn = aimbs_getstr(bs, snlen);
945
946        tlvlist = aim_readtlvchain(bs);
947
948        if (aim_gettlv(tlvlist, 0x0003, 1))
949                icbmflags |= AIM_IMFLAGS_ACK;
950        if (aim_gettlv(tlvlist, 0x0004, 1))
951                icbmflags |= AIM_IMFLAGS_AWAY;
952
953        if ((msgblock = aim_gettlv(tlvlist, 0x0002, 1))) {
954                aim_bstream_t mbs;
955                int featurelen, msglen;
956
957                aim_bstream_init(&mbs, msgblock->value, msgblock->length);
958
959                aimbs_get8(&mbs);
960                aimbs_get8(&mbs);
961                for (featurelen = aimbs_get16(&mbs); featurelen; featurelen--)
962                        aimbs_get8(&mbs);
963                aimbs_get8(&mbs);
964                aimbs_get8(&mbs);
965
966                msglen = aimbs_get16(&mbs) - 4; /* final block length */
967
968                flag1 = aimbs_get16(&mbs);
969                flag2 = aimbs_get16(&mbs);
970
971                msg = aimbs_getstr(&mbs, msglen);
972        }
973
974        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
975                ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2);
976
977        g_free(sn);
978        aim_freetlvchain(&tlvlist);
979
980        return ret;
981}
982
983/*
984 * Ahh, the joys of nearly ridiculous over-engineering.
985 *
986 * Not only do AIM ICBM's support multiple channels.  Not only do they
987 * support multiple character sets.  But they support multiple character
988 * sets / encodings within the same ICBM.
989 *
990 * These multipart messages allow for complex space savings techniques, which
991 * seem utterly unnecessary by today's standards.  In fact, there is only
992 * one client still in popular use that still uses this method: AOL for the
993 * Macintosh, Version 5.0.  Obscure, yes, I know. 
994 *
995 * In modern (non-"legacy") clients, if the user tries to send a character
996 * that is not ISO-8859-1 or ASCII, the client will send the entire message
997 * as UNICODE, meaning that every character in the message will occupy the
998 * full 16 bit UNICODE field, even if the high order byte would be zero.
999 * Multipart messages prevent this wasted space by allowing the client to
1000 * only send the characters in UNICODE that need to be sent that way, and
1001 * the rest of the message can be sent in whatever the native character
1002 * set is (probably ASCII).
1003 *
1004 * An important note is that sections will be displayed in the order that
1005 * they appear in the ICBM.  There is no facility for merging or rearranging
1006 * sections at run time.  So if you have, say, ASCII then UNICODE then ASCII,
1007 * you must supply two ASCII sections with a UNICODE in the middle, and incur
1008 * the associated overhead.
1009 *
1010 * Normally I would have laughed and given a firm 'no' to supporting this
1011 * seldom-used feature, but something is attracting me to it.  In the future,
1012 * it may be possible to abuse this to send mixed-media messages to other
1013 * open source clients (like encryption or something) -- see faimtest for
1014 * examples of how to do this.
1015 *
1016 * I would definitly recommend avoiding this feature unless you really
1017 * know what you are doing, and/or you have something neat to do with it.
1018 *
1019 */
1020int aim_mpmsg_init(aim_session_t *sess, aim_mpmsg_t *mpm)
1021{
1022
1023        memset(mpm, 0, sizeof(aim_mpmsg_t));
1024
1025        return 0;
1026}
1027
1028static int mpmsg_addsection(aim_session_t *sess, aim_mpmsg_t *mpm, guint16 charset, guint16 charsubset, guint8 *data, guint16 datalen)
1029{
1030        aim_mpmsg_section_t *sec; 
1031       
1032        if (!(sec = g_new0(aim_mpmsg_section_t,1)))
1033                return -1;
1034
1035        sec->charset = charset;
1036        sec->charsubset = charsubset;
1037        sec->data = data;
1038        sec->datalen = datalen;
1039        sec->next = NULL;
1040
1041        if (!mpm->parts)
1042                mpm->parts = sec;
1043        else {
1044                aim_mpmsg_section_t *cur;
1045
1046                for (cur = mpm->parts; cur->next; cur = cur->next)
1047                        ;
1048                cur->next = sec;
1049        }
1050
1051        mpm->numparts++;
1052
1053        return 0;
1054}
1055
1056int aim_mpmsg_addraw(aim_session_t *sess, aim_mpmsg_t *mpm, guint16 charset, guint16 charsubset, const guint8 *data, guint16 datalen)
1057{
1058        guint8 *dup;
1059
1060        if (!(dup = g_malloc(datalen)))
1061                return -1;
1062        memcpy(dup, data, datalen);
1063
1064        if (mpmsg_addsection(sess, mpm, charset, charsubset, dup, datalen) == -1) {
1065                g_free(dup);
1066                return -1;
1067        }
1068
1069        return 0;
1070}
1071
1072/* XXX should provide a way of saying ISO-8859-1 specifically */
1073int aim_mpmsg_addascii(aim_session_t *sess, aim_mpmsg_t *mpm, const char *ascii)
1074{
1075        char *dup;
1076
1077        if (!(dup = g_strdup(ascii))) 
1078                return -1;
1079
1080        if (mpmsg_addsection(sess, mpm, 0x0000, 0x0000, (guint8 *)dup, (guint16) strlen(ascii)) == -1) {
1081                g_free(dup);
1082                return -1;
1083        }
1084
1085        return 0;
1086}
1087
1088int aim_mpmsg_addunicode(aim_session_t *sess, aim_mpmsg_t *mpm, const guint16 *unicode, guint16 unicodelen)
1089{
1090        guint8 *buf;
1091        aim_bstream_t bs;
1092        int i;
1093
1094        if (!(buf = g_malloc(unicodelen * 2)))
1095                return -1;
1096
1097        aim_bstream_init(&bs, buf, unicodelen * 2);
1098
1099        /* We assume unicode is in /host/ byte order -- convert to network */
1100        for (i = 0; i < unicodelen; i++)
1101                aimbs_put16(&bs, unicode[i]);
1102
1103        if (mpmsg_addsection(sess, mpm, 0x0002, 0x0000, buf, aim_bstream_curpos(&bs)) == -1) {
1104                g_free(buf);
1105                return -1;
1106        }
1107       
1108        return 0;
1109}
1110
1111void aim_mpmsg_free(aim_session_t *sess, aim_mpmsg_t *mpm)
1112{
1113        aim_mpmsg_section_t *cur;
1114
1115        for (cur = mpm->parts; cur; ) {
1116                aim_mpmsg_section_t *tmp;
1117               
1118                tmp = cur->next;
1119                g_free(cur->data);
1120                g_free(cur);
1121                cur = tmp;
1122        }
1123       
1124        mpm->numparts = 0;
1125        mpm->parts = NULL;
1126
1127        return;
1128}
1129
1130/*
1131 * Start by building the multipart structures, then pick the first
1132 * human-readable section and stuff it into args->msg so no one gets
1133 * suspicious.
1134 *
1135 */
1136static int incomingim_ch1_parsemsgs(aim_session_t *sess, guint8 *data, int len, struct aim_incomingim_ch1_args *args)
1137{
1138        static const guint16 charsetpri[] = {
1139                0x0000, /* ASCII first */
1140                0x0003, /* then ISO-8859-1 */
1141                0x0002, /* UNICODE as last resort */
1142        };
1143        static const int charsetpricount = 3;
1144        int i;
1145        aim_bstream_t mbs;
1146        aim_mpmsg_section_t *sec;
1147
1148        aim_bstream_init(&mbs, data, len);
1149
1150        while (aim_bstream_empty(&mbs)) {
1151                guint16 msglen, flag1, flag2;
1152                char *msgbuf;
1153
1154                aimbs_get8(&mbs); /* 01 */
1155                aimbs_get8(&mbs); /* 01 */
1156
1157                /* Message string length, including character set info. */
1158                msglen = aimbs_get16(&mbs);
1159
1160                /* Character set info */
1161                flag1 = aimbs_get16(&mbs);
1162                flag2 = aimbs_get16(&mbs);
1163
1164                /* Message. */
1165                msglen -= 4;
1166
1167                /*
1168                 * For now, we don't care what the encoding is.  Just copy
1169                 * it into a multipart struct and deal with it later. However,
1170                 * always pad the ending with a NULL.  This makes it easier
1171                 * to treat ASCII sections as strings.  It won't matter for
1172                 * UNICODE or binary data, as you should never read past
1173                 * the specified data length, which will not include the pad.
1174                 *
1175                 * XXX There's an API bug here.  For sending, the UNICODE is
1176                 * given in host byte order (aim_mpmsg_addunicode), but here
1177                 * the received messages are given in network byte order.
1178                 *
1179                 */
1180                msgbuf = aimbs_getstr(&mbs, msglen);
1181                mpmsg_addsection(sess, &args->mpmsg, flag1, flag2, (guint8 *)msgbuf, (guint16) msglen);
1182
1183        } /* while */
1184
1185        args->icbmflags |= AIM_IMFLAGS_MULTIPART; /* always set */
1186
1187        /*
1188         * Clients that support multiparts should never use args->msg, as it
1189         * will point to an arbitrary section.
1190         *
1191         * Here, we attempt to provide clients that do not support multipart
1192         * messages with something to look at -- hopefully a human-readable
1193         * string.  But, failing that, a UNICODE message, or nothing at all.
1194         *
1195         * Which means that even if args->msg is NULL, it does not mean the
1196         * message was blank.
1197         *
1198         */
1199        for (i = 0; i < charsetpricount; i++) {
1200                for (sec = args->mpmsg.parts; sec; sec = sec->next) {
1201
1202                        if (sec->charset != charsetpri[i])
1203                                continue;
1204
1205                        /* Great. We found one.  Fill it in. */
1206                        args->charset = sec->charset;
1207                        args->charsubset = sec->charsubset;
1208                        args->icbmflags |= AIM_IMFLAGS_CUSTOMCHARSET;
1209
1210                        /* Set up the simple flags */
1211                        if (args->charset == 0x0000)
1212                                ; /* ASCII */
1213                        else if (args->charset == 0x0002)
1214                                args->icbmflags |= AIM_IMFLAGS_UNICODE;
1215                        else if (args->charset == 0x0003)
1216                                args->icbmflags |= AIM_IMFLAGS_ISO_8859_1;
1217                        else if (args->charset == 0xffff)
1218                                ; /* no encoding (yeep!) */
1219
1220                        if (args->charsubset == 0x0000)
1221                                ; /* standard subencoding? */
1222                        else if (args->charsubset == 0x000b)
1223                                args->icbmflags |= AIM_IMFLAGS_SUBENC_MACINTOSH;
1224                        else if (args->charsubset == 0xffff)
1225                                ; /* no subencoding */
1226#if 0
1227                        /* XXX this isn't really necesary... */
1228                        if (    ((args.flag1 != 0x0000) &&
1229                                 (args.flag1 != 0x0002) &&
1230                                 (args.flag1 != 0x0003) &&
1231                                 (args.flag1 != 0xffff)) ||
1232                                ((args.flag2 != 0x0000) &&
1233                                 (args.flag2 != 0x000b) &&
1234                                 (args.flag2 != 0xffff))) {
1235                                faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", args.flag1, args.flag2);
1236                        }
1237#endif
1238
1239                        args->msg = (char *)sec->data;
1240                        args->msglen = sec->datalen;
1241
1242                        return 0;
1243                }
1244        }
1245
1246        /* No human-readable sections found.  Oh well. */
1247        args->charset = args->charsubset = 0xffff;
1248        args->msg = NULL;
1249        args->msglen = 0;
1250
1251        return 0;
1252}
1253
1254static int incomingim_ch1(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, guint16 channel, aim_userinfo_t *userinfo, aim_bstream_t *bs, guint8 *cookie)
1255{
1256        guint16 type, length;
1257        aim_rxcallback_t userfunc;
1258        int ret = 0;
1259        struct aim_incomingim_ch1_args args;
1260        int endpos;
1261
1262        memset(&args, 0, sizeof(args));
1263
1264        aim_mpmsg_init(sess, &args.mpmsg);
1265
1266        /*
1267         * This used to be done using tlvchains.  For performance reasons,
1268         * I've changed it to process the TLVs in-place.  This avoids lots
1269         * of per-IM memory allocations.
1270         */
1271        while (aim_bstream_empty(bs)) {
1272
1273                type = aimbs_get16(bs);
1274                length = aimbs_get16(bs);
1275
1276                endpos = aim_bstream_curpos(bs) + length;
1277
1278                if (type == 0x0002) { /* Message Block */
1279
1280                        /*
1281                         * This TLV consists of the following:
1282                         *   - 0501 -- Unknown
1283                         *   - Features: Don't know how to interpret these
1284                         *   - 0101 -- Unknown
1285                         *   - Message
1286                         *
1287                         */
1288
1289                        aimbs_get8(bs); /* 05 */
1290                        aimbs_get8(bs); /* 01 */
1291
1292                        args.featureslen = aimbs_get16(bs);
1293                        /* XXX XXX this is all evil! */
1294                        args.features = bs->data + bs->offset;
1295                        aim_bstream_advance(bs, args.featureslen);
1296                        args.icbmflags |= AIM_IMFLAGS_CUSTOMFEATURES;
1297
1298                        /*
1299                         * The rest of the TLV contains one or more message
1300                         * blocks...
1301                         */
1302                        incomingim_ch1_parsemsgs(sess, bs->data + bs->offset /* XXX evil!!! */, length - 2 - 2 - args.featureslen, &args);
1303
1304                } else if (type == 0x0003) { /* Server Ack Requested */
1305
1306                        args.icbmflags |= AIM_IMFLAGS_ACK;
1307
1308                } else if (type == 0x0004) { /* Message is Auto Response */
1309
1310                        args.icbmflags |= AIM_IMFLAGS_AWAY;
1311
1312                } else if (type == 0x0006) { /* Message was received offline. */
1313
1314                        /* XXX not sure if this actually gets sent. */
1315                        args.icbmflags |= AIM_IMFLAGS_OFFLINE;
1316
1317                } else if (type == 0x0008) { /* I-HAVE-A-REALLY-PURTY-ICON Flag */
1318
1319                        args.iconlen = aimbs_get32(bs);
1320                        aimbs_get16(bs); /* 0x0001 */
1321                        args.iconsum = aimbs_get16(bs);
1322                        args.iconstamp = aimbs_get32(bs);
1323
1324                        /*
1325                         * This looks to be a client bug.  MacAIM 4.3 will
1326                         * send this tag, but with all zero values, in the
1327                         * first message of a conversation. This makes no
1328                         * sense whatsoever, so I'm going to say its a bug.
1329                         *
1330                         * You really shouldn't advertise a zero-length icon
1331                         * anyway.
1332                         *
1333                         */
1334                        if (args.iconlen)
1335                                args.icbmflags |= AIM_IMFLAGS_HASICON;
1336
1337                } else if (type == 0x0009) {
1338
1339                        args.icbmflags |= AIM_IMFLAGS_BUDDYREQ;
1340
1341                } else if (type == 0x0017) {
1342
1343                        args.extdatalen = length;
1344                        args.extdata = aimbs_getraw(bs, args.extdatalen);
1345
1346                } else {
1347                        // do_error_dialog(sess->aux_data, "Unknown TLV encountered", "Gaim");
1348                }
1349
1350                /*
1351                 * This is here to protect ourselves from ourselves.  That
1352                 * is, if something above doesn't completly parse its value
1353                 * section, or, worse, overparses it, this will set the
1354                 * stream where it needs to be in order to land on the next
1355                 * TLV when the loop continues.
1356                 *
1357                 */
1358                aim_bstream_setpos(bs, endpos);
1359        }
1360
1361
1362        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1363                ret = userfunc(sess, rx, channel, userinfo, &args);
1364
1365        aim_mpmsg_free(sess, &args.mpmsg);
1366        g_free(args.extdata);
1367
1368        return ret;
1369}
1370
1371static void incomingim_ch2_icqserverrelay_free(aim_session_t *sess, struct aim_incomingim_ch2_args *args)
1372{
1373
1374        g_free((char *)args->info.rtfmsg.rtfmsg);
1375
1376        return;
1377}
1378
1379/*
1380 * The relationship between AIM_CAPS_ICQSERVERRELAY and AIM_CAPS_ICQRTF is
1381 * kind of odd. This sends the client ICQRTF since that is all that I've seen
1382 * SERVERRELAY used for.
1383 *
1384 * Note that this is all little-endian.  Cringe.
1385 *
1386 * This cap is used for auto status message replies, too [ft]
1387 *
1388 */
1389static void incomingim_ch2_icqserverrelay(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_bstream_t *servdata)
1390{
1391        guint16 hdrlen, msglen, dc;
1392        guint8 msgtype, msgflags;
1393    guint8 *plugin;
1394    int i = 0, tmp = 0;
1395    struct gaim_connection *gc = sess->aux_data;
1396
1397    /* at the moment we just can deal with requests, not with cancel or accept */
1398    if (args->status != 0) return;
1399
1400        hdrlen = aimbs_getle16(servdata);
1401
1402    aim_bstream_advance(servdata, 0x02); /* protocol version */
1403    plugin = aimbs_getraw(servdata, 0x10); /* following data is a message or
1404                                              something plugin specific */
1405    /* as there is no plugin handling, just skip the rest */
1406    aim_bstream_advance(servdata, hdrlen - 0x12);
1407
1408        hdrlen = aimbs_getle16(servdata);
1409    dc = aimbs_getle16(servdata); /* save the sequence number */
1410        aim_bstream_advance(servdata, hdrlen - 0x02);
1411
1412    /* TODO is it a message or something for a plugin? */
1413    for (i = 0; i < 0x10; i++) {
1414        tmp |= plugin[i];
1415    }
1416
1417    if (!tmp) { /* message follows */
1418
1419        msgtype = aimbs_getle8(servdata);
1420        msgflags = aimbs_getle8(servdata);
1421
1422        aim_bstream_advance(servdata, 0x04); /* status code and priority code */
1423
1424        msglen = aimbs_getle16(servdata); /* message string length */
1425        args->info.rtfmsg.rtfmsg = aimbs_getstr(servdata, msglen);
1426
1427        switch(msgtype) {
1428            case AIM_MTYPE_PLAIN:
1429
1430                args->info.rtfmsg.fgcolor = aimbs_getle32(servdata);
1431                args->info.rtfmsg.bgcolor = aimbs_getle32(servdata);
1432
1433                hdrlen = aimbs_getle32(servdata);
1434                aim_bstream_advance(servdata, hdrlen);
1435
1436                /* XXX This is such a hack. */
1437                args->reqclass = AIM_CAPS_ICQRTF;
1438                break;
1439
1440            case AIM_MTYPE_AUTOAWAY: 
1441            case AIM_MTYPE_AUTOBUSY:
1442            case AIM_MTYPE_AUTONA:
1443            case AIM_MTYPE_AUTODND:
1444            case AIM_MTYPE_AUTOFFC:
1445            case 0x9c:  /* ICQ 5 seems to send this */
1446                aim_send_im_ch2_statusmessage(sess, userinfo->sn, args->cookie,
1447                        gc->away, sess->aim_icq_state, dc);
1448                break;
1449
1450        }
1451    } /* message or plugin specific */
1452
1453    g_free(plugin);
1454        args->destructor = (void *)incomingim_ch2_icqserverrelay_free;
1455
1456        return;
1457}
1458
1459typedef void (*ch2_args_destructor_t)(aim_session_t *sess, struct aim_incomingim_ch2_args *args);
1460
1461static int incomingim_ch2(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, guint16 channel, aim_userinfo_t *userinfo, aim_tlvlist_t *tlvlist, guint8 *cookie)
1462{
1463        aim_rxcallback_t userfunc;
1464        aim_tlv_t *block1, *servdatatlv;
1465        aim_tlvlist_t *list2;
1466        struct aim_incomingim_ch2_args args;
1467        aim_bstream_t bbs, sdbs, *sdbsptr = NULL;
1468        guint8 *cookie2;
1469        int ret = 0;
1470
1471        char clientip1[30] = {""};
1472        char clientip2[30] = {""};
1473        char verifiedip[30] = {""};
1474
1475        memset(&args, 0, sizeof(args));
1476
1477        /*
1478         * There's another block of TLVs embedded in the type 5 here.
1479         */
1480        block1 = aim_gettlv(tlvlist, 0x0005, 1);
1481        aim_bstream_init(&bbs, block1->value, block1->length);
1482
1483        /*
1484         * First two bytes represent the status of the connection.
1485         *
1486         * 0 is a request, 1 is a deny (?), 2 is an accept
1487         */ 
1488        args.status = aimbs_get16(&bbs);
1489
1490        /*
1491         * Next comes the cookie.  Should match the ICBM cookie.
1492         */
1493        cookie2 = aimbs_getraw(&bbs, 8);
1494        if (memcmp(cookie, cookie2, 8) != 0) 
1495                do_error_dialog(sess->aux_data, "rend: warning cookies don't match!", "Gaim");
1496        memcpy(args.cookie, cookie2, 8);
1497        g_free(cookie2);
1498
1499        /*
1500         * The next 16bytes are a capability block so we can
1501         * identify what type of rendezvous this is.
1502         */
1503        args.reqclass = aim_getcap(sess, &bbs, 0x10);
1504
1505        /*
1506         * What follows may be TLVs or nothing, depending on the
1507         * purpose of the message.
1508         *
1509         * Ack packets for instance have nothing more to them.
1510         */
1511        list2 = aim_readtlvchain(&bbs);
1512
1513        /*
1514         * IP address from the perspective of the client.
1515         */
1516        if (aim_gettlv(list2, 0x0002, 1)) {
1517                aim_tlv_t *iptlv;
1518
1519                iptlv = aim_gettlv(list2, 0x0002, 1);
1520
1521                g_snprintf(clientip1, sizeof(clientip1), "%d.%d.%d.%d",
1522                                aimutil_get8(iptlv->value+0),
1523                                aimutil_get8(iptlv->value+1),
1524                                aimutil_get8(iptlv->value+2),
1525                                aimutil_get8(iptlv->value+3));
1526        }
1527
1528        /*
1529         * Secondary IP address from the perspective of the client.
1530         */
1531        if (aim_gettlv(list2, 0x0003, 1)) {
1532                aim_tlv_t *iptlv;
1533
1534                iptlv = aim_gettlv(list2, 0x0003, 1);
1535
1536                g_snprintf(clientip2, sizeof(clientip2), "%d.%d.%d.%d",
1537                                aimutil_get8(iptlv->value+0),
1538                                aimutil_get8(iptlv->value+1),
1539                                aimutil_get8(iptlv->value+2),
1540                                aimutil_get8(iptlv->value+3));
1541        }
1542
1543        /*
1544         * Verified IP address (from the perspective of Oscar).
1545         *
1546         * This is added by the server.
1547         */
1548        if (aim_gettlv(list2, 0x0004, 1)) {
1549                aim_tlv_t *iptlv;
1550
1551                iptlv = aim_gettlv(list2, 0x0004, 1);
1552
1553                g_snprintf(verifiedip, sizeof(verifiedip), "%d.%d.%d.%d",
1554                                aimutil_get8(iptlv->value+0),
1555                                aimutil_get8(iptlv->value+1),
1556                                aimutil_get8(iptlv->value+2),
1557                                aimutil_get8(iptlv->value+3));
1558        }
1559
1560        /*
1561         * Port number for something.
1562         */
1563        if (aim_gettlv(list2, 0x0005, 1))
1564                args.port = aim_gettlv16(list2, 0x0005, 1);
1565
1566        /*
1567         * Error code.
1568         */
1569        if (aim_gettlv(list2, 0x000b, 1))
1570                args.errorcode = aim_gettlv16(list2, 0x000b, 1);
1571
1572        /*
1573         * Invitation message / chat description.
1574         */
1575        if (aim_gettlv(list2, 0x000c, 1))
1576                args.msg = aim_gettlv_str(list2, 0x000c, 1);
1577
1578        /*
1579         * Character set.
1580         */
1581        if (aim_gettlv(list2, 0x000d, 1))
1582                args.encoding = aim_gettlv_str(list2, 0x000d, 1);
1583       
1584        /*
1585         * Language.
1586         */
1587        if (aim_gettlv(list2, 0x000e, 1))
1588                args.language = aim_gettlv_str(list2, 0x000e, 1);
1589
1590        /* Unknown -- two bytes = 0x0001 */
1591        if (aim_gettlv(list2, 0x000a, 1))
1592                ;
1593
1594        /* Unknown -- no value */
1595        if (aim_gettlv(list2, 0x000f, 1))
1596                ;
1597
1598        if (strlen(clientip1))
1599                args.clientip = (char *)clientip1;
1600        if (strlen(clientip2))
1601                args.clientip2 = (char *)clientip2;
1602        if (strlen(verifiedip))
1603                args.verifiedip = (char *)verifiedip;
1604
1605        /*
1606         * This is must be present in PROPOSALs, but will probably not
1607         * exist in CANCELs and ACCEPTs.
1608         *
1609         * Service Data blocks are module-specific in format.
1610         */
1611        if ((servdatatlv = aim_gettlv(list2, 0x2711 /* 10001 */, 1))) {
1612
1613                aim_bstream_init(&sdbs, servdatatlv->value, servdatatlv->length);
1614                sdbsptr = &sdbs;
1615        }
1616
1617        if (args.reqclass & AIM_CAPS_ICQSERVERRELAY)
1618                incomingim_ch2_icqserverrelay(sess, mod, rx, snac, userinfo, &args, sdbsptr);
1619
1620
1621        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1622                ret = userfunc(sess, rx, channel, userinfo, &args);
1623
1624
1625        if (args.destructor)
1626                ((ch2_args_destructor_t)args.destructor)(sess, &args);
1627
1628        g_free((char *)args.msg);
1629        g_free((char *)args.encoding);
1630        g_free((char *)args.language);
1631
1632        aim_freetlvchain(&list2);
1633
1634        return ret;
1635}
1636
1637static int incomingim_ch4(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, guint16 channel, aim_userinfo_t *userinfo, aim_tlvlist_t *tlvlist, guint8 *cookie)
1638{
1639        aim_bstream_t meat;
1640        aim_rxcallback_t userfunc;
1641        aim_tlv_t *block;
1642        struct aim_incomingim_ch4_args args;
1643        int ret = 0;
1644
1645        /*
1646         * Make a bstream for the meaty part.  Yum.  Meat.
1647         */
1648        if (!(block = aim_gettlv(tlvlist, 0x0005, 1)))
1649                return -1;
1650        aim_bstream_init(&meat, block->value, block->length);
1651
1652        args.uin = aimbs_getle32(&meat);
1653        args.type = aimbs_getle16(&meat);
1654        args.msg = (char *)aimbs_getraw(&meat, aimbs_getle16(&meat));
1655
1656        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1657                ret = userfunc(sess, rx, channel, userinfo, &args);
1658
1659        g_free(args.msg);
1660
1661        return ret;
1662}
1663
1664/*
1665 * It can easily be said that parsing ICBMs is THE single
1666 * most difficult thing to do in the in AIM protocol.  In
1667 * fact, I think I just did say that.
1668 *
1669 * Below is the best damned solution I've come up with
1670 * over the past sixteen months of battling with it. This
1671 * can parse both away and normal messages from every client
1672 * I have access to.  Its not fast, its not clean.  But it works.
1673 *
1674 */
1675static int incomingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1676{
1677        int i, ret = 0;
1678        guint8 cookie[8];
1679        guint16 channel;
1680        aim_userinfo_t userinfo;
1681
1682        memset(&userinfo, 0x00, sizeof(aim_userinfo_t));
1683
1684        /*
1685         * Read ICBM Cookie.  And throw away.
1686         */
1687        for (i = 0; i < 8; i++)
1688                cookie[i] = aimbs_get8(bs);
1689
1690        /*
1691         * Channel ID.
1692         *
1693         * Channel 0x0001 is the message channel.  There are
1694         * other channels for things called "rendevous"
1695         * which represent chat and some of the other new
1696         * features of AIM2/3/3.5.
1697         *
1698         * Channel 0x0002 is the Rendevous channel, which
1699         * is where Chat Invitiations and various client-client
1700         * connection negotiations come from.
1701         *
1702         * Channel 0x0004 is used for ICQ authorization, or
1703         * possibly any system notice.
1704         *
1705         */
1706        channel = aimbs_get16(bs);
1707
1708        /*
1709         * Extract the standard user info block.
1710         *
1711         * Note that although this contains TLVs that appear contiguous
1712         * with the TLVs read below, they are two different pieces.  The
1713         * userinfo block contains the number of TLVs that contain user
1714         * information, the rest are not even though there is no seperation.
1715         * aim_extractuserinfo() returns the number of bytes used by the
1716         * userinfo tlvs, so you can start reading the rest of them right
1717         * afterward. 
1718         *
1719         * That also means that TLV types can be duplicated between the
1720         * userinfo block and the rest of the message, however there should
1721         * never be two TLVs of the same type in one block.
1722         *
1723         */
1724        aim_extractuserinfo(sess, bs, &userinfo);
1725
1726        /*
1727         * From here on, its depends on what channel we're on.
1728         *
1729         * Technically all channels have a TLV list have this, however,
1730         * for the common channel 1 case, in-place parsing is used for
1731         * performance reasons (less memory allocation).
1732         */
1733        if (channel == 1) {
1734
1735                ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, bs, cookie);
1736
1737        } else if (channel == 2) {
1738                aim_tlvlist_t *tlvlist;
1739
1740                /*
1741                 * Read block of TLVs (not including the userinfo data).  All
1742                 * further data is derived from what is parsed here.
1743                 */
1744                tlvlist = aim_readtlvchain(bs);
1745
1746                ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
1747
1748                aim_freetlvchain(&tlvlist);
1749
1750        } else if (channel == 4) {
1751                aim_tlvlist_t *tlvlist;
1752
1753                tlvlist = aim_readtlvchain(bs);
1754                ret = incomingim_ch4(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
1755                aim_freetlvchain(&tlvlist);
1756
1757        } else {
1758
1759                do_error_dialog(sess->aux_data, "ICBM received on an unsupported channel.  Ignoring.", "Gaim");
1760
1761                return 0;
1762        }
1763
1764        return ret;
1765}
1766
1767/*
1768 * Possible codes:
1769 *    AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
1770 *    AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
1771 *    AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
1772 *
1773 */
1774int aim_denytransfer(aim_session_t *sess, const char *sender, const guint8 *cookie, guint16 code)
1775{
1776        aim_conn_t *conn;
1777        aim_frame_t *fr;
1778        aim_snacid_t snacid;
1779        aim_tlvlist_t *tl = NULL;
1780       
1781        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
1782                return -EINVAL;
1783
1784        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sender)+6)))
1785                return -ENOMEM;
1786
1787        snacid = aim_cachesnac(sess, 0x0004, 0x000b, 0x0000, NULL, 0);
1788        aim_putsnac(&fr->data, 0x0004, 0x000b, 0x0000, snacid);
1789       
1790        aimbs_putraw(&fr->data, cookie, 8);
1791
1792        aimbs_put16(&fr->data, 0x0002); /* channel */
1793        aimbs_put8(&fr->data, strlen(sender));
1794        aimbs_putraw(&fr->data, (guint8 *)sender, strlen(sender));
1795
1796        aim_addtlvtochain16(&tl, 0x0003, code);
1797        aim_writetlvchain(&fr->data, &tl);
1798        aim_freetlvchain(&tl);
1799
1800        aim_tx_enqueue(sess, fr);
1801
1802        return 0;
1803}
1804
1805/*
1806 * aim_reqicbmparaminfo()
1807 *
1808 * Request ICBM parameter information.
1809 *
1810 */
1811int aim_reqicbmparams(aim_session_t *sess)
1812{
1813        aim_conn_t *conn;
1814
1815        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
1816                return -EINVAL;
1817
1818        return aim_genericreq_n(sess, conn, 0x0004, 0x0004);
1819}
1820
1821/*
1822 *
1823 * I definitly recommend sending this.  If you don't, you'll be stuck
1824 * with the rather unreasonable defaults.  You don't want those.  Send this.
1825 *
1826 */
1827int aim_seticbmparam(aim_session_t *sess, struct aim_icbmparameters *params)
1828{
1829        aim_conn_t *conn;
1830        aim_frame_t *fr;
1831        aim_snacid_t snacid;
1832
1833        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
1834                return -EINVAL;
1835
1836        if (!params)
1837                return -EINVAL;
1838
1839        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+16)))
1840                return -ENOMEM;
1841
1842        snacid = aim_cachesnac(sess, 0x0004, 0x0002, 0x0000, NULL, 0);
1843        aim_putsnac(&fr->data, 0x0004, 0x0002, 0x0000, snacid);
1844
1845        /* This is read-only (see Parameter Reply). Must be set to zero here. */
1846        aimbs_put16(&fr->data, 0x0000);
1847
1848        /* These are all read-write */
1849        aimbs_put32(&fr->data, params->flags); 
1850        aimbs_put16(&fr->data, params->maxmsglen);
1851        aimbs_put16(&fr->data, params->maxsenderwarn); 
1852        aimbs_put16(&fr->data, params->maxrecverwarn); 
1853        aimbs_put32(&fr->data, params->minmsginterval);
1854
1855        aim_tx_enqueue(sess, fr);
1856
1857        return 0;
1858}
1859
1860static int paraminfo(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1861{
1862        struct aim_icbmparameters params;
1863        aim_rxcallback_t userfunc;
1864
1865        params.maxchan = aimbs_get16(bs);
1866        params.flags = aimbs_get32(bs);
1867        params.maxmsglen = aimbs_get16(bs);
1868        params.maxsenderwarn = aimbs_get16(bs);
1869        params.maxrecverwarn = aimbs_get16(bs);
1870        params.minmsginterval = aimbs_get32(bs);
1871       
1872        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1873                return userfunc(sess, rx, &params);
1874
1875        return 0;
1876}
1877
1878static int missedcall(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1879{
1880        int ret = 0;
1881        aim_rxcallback_t userfunc;
1882        guint16 channel, nummissed, reason;
1883        aim_userinfo_t userinfo;
1884
1885        while (aim_bstream_empty(bs)) { 
1886
1887                channel = aimbs_get16(bs);
1888                aim_extractuserinfo(sess, bs, &userinfo);
1889                nummissed = aimbs_get16(bs);
1890                reason = aimbs_get16(bs);
1891
1892                if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1893                         ret = userfunc(sess, rx, channel, &userinfo, nummissed, reason);
1894        }
1895
1896        return ret;
1897}
1898
1899/*
1900 * Receive the response from an ICQ status message request.  This contains the
1901 * ICQ status message.  Go figure.
1902 */
1903static int clientautoresp(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1904{
1905        int ret = 0;
1906        aim_rxcallback_t userfunc;
1907        guint16 channel, reason;
1908        char *sn;
1909        guint8 *ck, snlen;
1910
1911        ck = aimbs_getraw(bs, 8);
1912        channel = aimbs_get16(bs);
1913        snlen = aimbs_get8(bs);
1914        sn = aimbs_getstr(bs, snlen);
1915        reason = aimbs_get16(bs);
1916
1917        switch (reason) {
1918                case 0x0003: { /* ICQ status message.  Maybe other stuff too, you never know with these people. */
1919                        guint8 statusmsgtype, *msg;
1920                        guint16 len;
1921                        guint32 state;
1922
1923                        len = aimbs_getle16(bs); /* Should be 0x001b */
1924                        aim_bstream_advance(bs, len); /* Unknown */
1925
1926                        len = aimbs_getle16(bs); /* Should be 0x000e */
1927                        aim_bstream_advance(bs, len); /* Unknown */
1928
1929                        statusmsgtype = aimbs_getle8(bs);
1930                        switch (statusmsgtype) {
1931                                case 0xe8:
1932                                        state = AIM_ICQ_STATE_AWAY;
1933                                        break;
1934                                case 0xe9:
1935                                        state = AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_BUSY;
1936                                        break;
1937                                case 0xea:
1938                                        state = AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_OUT;
1939                                        break;
1940                                case 0xeb:
1941                                        state = AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_DND | AIM_ICQ_STATE_BUSY;
1942                                        break;
1943                                case 0xec:
1944                                        state = AIM_ICQ_STATE_CHAT;
1945                                        break;
1946                                default:
1947                                        state = 0;
1948                                        break;
1949                        }
1950
1951                        aimbs_getle8(bs); /* Unknown - 0x03 Maybe this means this is an auto-reply */
1952                        aimbs_getle16(bs); /* Unknown - 0x0000 */
1953                        aimbs_getle16(bs); /* Unknown - 0x0000 */
1954
1955                        len = aimbs_getle16(bs);
1956                        msg = aimbs_getraw(bs, len);
1957
1958                        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1959                                ret = userfunc(sess, rx, channel, sn, reason, state, msg);
1960
1961                        g_free(msg);
1962                } break;
1963
1964                default: {
1965                        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1966                                ret = userfunc(sess, rx, channel, sn, reason);
1967                } break;
1968        } /* end switch */
1969
1970        g_free(ck);
1971        g_free(sn);
1972
1973        return ret;
1974}
1975
1976static int msgack(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1977{
1978        aim_rxcallback_t userfunc;
1979        guint16 type;
1980        guint8 snlen, *ck;
1981        char *sn;
1982        int ret = 0;
1983
1984        ck = aimbs_getraw(bs, 8);
1985        type = aimbs_get16(bs);
1986        snlen = aimbs_get8(bs);
1987        sn = aimbs_getstr(bs, snlen);
1988
1989        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1990                ret = userfunc(sess, rx, type, sn);
1991
1992        g_free(sn);
1993        g_free(ck);
1994
1995        return ret;
1996}
1997
1998static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1999{
2000
2001        if (snac->subtype == 0x0005)
2002                return paraminfo(sess, mod, rx, snac, bs);
2003        else if (snac->subtype == 0x0006)
2004                return outgoingim(sess, mod, rx, snac, bs);
2005        else if (snac->subtype == 0x0007)
2006                return incomingim(sess, mod, rx, snac, bs);
2007        else if (snac->subtype == 0x000a)
2008                return missedcall(sess, mod, rx, snac, bs);
2009        else if (snac->subtype == 0x000b)
2010                return clientautoresp(sess, mod, rx, snac, bs);
2011        else if (snac->subtype == 0x000c)
2012                return msgack(sess, mod, rx, snac, bs);
2013
2014        return 0;
2015}
2016
2017int msg_modfirst(aim_session_t *sess, aim_module_t *mod)
2018{
2019
2020        mod->family = 0x0004;
2021        mod->version = 0x0001;
2022        mod->toolid = 0x0110;
2023        mod->toolversion = 0x0629;
2024        mod->flags = 0;
2025        strncpy(mod->name, "messaging", sizeof(mod->name));
2026        mod->snachandler = snachandler;
2027
2028        return 0;
2029}
Note: See TracBrowser for help on using the repository browser.