source: protocols/oscar/im.c @ d7edadf

Last change on this file since d7edadf was d18db32f, checked in by Miklos Vajna <vmiklos@…>, at 2011-12-06T00:53:26Z

oscar: unused-but-set-variables

  • Property mode set to 100644
File size: 57.7 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        guint16 channel;
922        aim_tlvlist_t *tlvlist;
923        char *sn;
924        int snlen;
925        guint16 icbmflags = 0;
926        guint8 flag1 = 0, flag2 = 0;
927        char *msg = NULL;
928        aim_tlv_t *msgblock;
929
930        /* ICBM Cookie. */
931        for (i = 0; i < 8; i++)
932                aimbs_get8(bs);
933
934        /* Channel ID */
935        channel = aimbs_get16(bs);
936
937        if (channel != 0x01) {
938                imcb_error(sess->aux_data, "icbm: ICBM received on unsupported channel.  Ignoring.");
939                return 0;
940        }
941
942        snlen = aimbs_get8(bs);
943        sn = aimbs_getstr(bs, snlen);
944
945        tlvlist = aim_readtlvchain(bs);
946
947        if (aim_gettlv(tlvlist, 0x0003, 1))
948                icbmflags |= AIM_IMFLAGS_ACK;
949        if (aim_gettlv(tlvlist, 0x0004, 1))
950                icbmflags |= AIM_IMFLAGS_AWAY;
951
952        if ((msgblock = aim_gettlv(tlvlist, 0x0002, 1))) {
953                aim_bstream_t mbs;
954                int featurelen, msglen;
955
956                aim_bstream_init(&mbs, msgblock->value, msgblock->length);
957
958                aimbs_get8(&mbs);
959                aimbs_get8(&mbs);
960                for (featurelen = aimbs_get16(&mbs); featurelen; featurelen--)
961                        aimbs_get8(&mbs);
962                aimbs_get8(&mbs);
963                aimbs_get8(&mbs);
964
965                msglen = aimbs_get16(&mbs) - 4; /* final block length */
966
967                flag1 = aimbs_get16(&mbs);
968                flag2 = aimbs_get16(&mbs);
969
970                msg = aimbs_getstr(&mbs, msglen);
971        }
972
973        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
974                ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2);
975
976        g_free(sn);
977        aim_freetlvchain(&tlvlist);
978
979        return ret;
980}
981
982/*
983 * Ahh, the joys of nearly ridiculous over-engineering.
984 *
985 * Not only do AIM ICBM's support multiple channels.  Not only do they
986 * support multiple character sets.  But they support multiple character
987 * sets / encodings within the same ICBM.
988 *
989 * These multipart messages allow for complex space savings techniques, which
990 * seem utterly unnecessary by today's standards.  In fact, there is only
991 * one client still in popular use that still uses this method: AOL for the
992 * Macintosh, Version 5.0.  Obscure, yes, I know. 
993 *
994 * In modern (non-"legacy") clients, if the user tries to send a character
995 * that is not ISO-8859-1 or ASCII, the client will send the entire message
996 * as UNICODE, meaning that every character in the message will occupy the
997 * full 16 bit UNICODE field, even if the high order byte would be zero.
998 * Multipart messages prevent this wasted space by allowing the client to
999 * only send the characters in UNICODE that need to be sent that way, and
1000 * the rest of the message can be sent in whatever the native character
1001 * set is (probably ASCII).
1002 *
1003 * An important note is that sections will be displayed in the order that
1004 * they appear in the ICBM.  There is no facility for merging or rearranging
1005 * sections at run time.  So if you have, say, ASCII then UNICODE then ASCII,
1006 * you must supply two ASCII sections with a UNICODE in the middle, and incur
1007 * the associated overhead.
1008 *
1009 * Normally I would have laughed and given a firm 'no' to supporting this
1010 * seldom-used feature, but something is attracting me to it.  In the future,
1011 * it may be possible to abuse this to send mixed-media messages to other
1012 * open source clients (like encryption or something) -- see faimtest for
1013 * examples of how to do this.
1014 *
1015 * I would definitly recommend avoiding this feature unless you really
1016 * know what you are doing, and/or you have something neat to do with it.
1017 *
1018 */
1019int aim_mpmsg_init(aim_session_t *sess, aim_mpmsg_t *mpm)
1020{
1021
1022        memset(mpm, 0, sizeof(aim_mpmsg_t));
1023
1024        return 0;
1025}
1026
1027static int mpmsg_addsection(aim_session_t *sess, aim_mpmsg_t *mpm, guint16 charset, guint16 charsubset, guint8 *data, guint16 datalen)
1028{
1029        aim_mpmsg_section_t *sec; 
1030       
1031        if (!(sec = g_new0(aim_mpmsg_section_t,1)))
1032                return -1;
1033
1034        sec->charset = charset;
1035        sec->charsubset = charsubset;
1036        sec->data = data;
1037        sec->datalen = datalen;
1038        sec->next = NULL;
1039
1040        if (!mpm->parts)
1041                mpm->parts = sec;
1042        else {
1043                aim_mpmsg_section_t *cur;
1044
1045                for (cur = mpm->parts; cur->next; cur = cur->next)
1046                        ;
1047                cur->next = sec;
1048        }
1049
1050        mpm->numparts++;
1051
1052        return 0;
1053}
1054
1055int aim_mpmsg_addraw(aim_session_t *sess, aim_mpmsg_t *mpm, guint16 charset, guint16 charsubset, const guint8 *data, guint16 datalen)
1056{
1057        guint8 *dup;
1058
1059        if (!(dup = g_malloc(datalen)))
1060                return -1;
1061        memcpy(dup, data, datalen);
1062
1063        if (mpmsg_addsection(sess, mpm, charset, charsubset, dup, datalen) == -1) {
1064                g_free(dup);
1065                return -1;
1066        }
1067
1068        return 0;
1069}
1070
1071/* XXX should provide a way of saying ISO-8859-1 specifically */
1072int aim_mpmsg_addascii(aim_session_t *sess, aim_mpmsg_t *mpm, const char *ascii)
1073{
1074        char *dup;
1075
1076        if (!(dup = g_strdup(ascii))) 
1077                return -1;
1078
1079        if (mpmsg_addsection(sess, mpm, 0x0000, 0x0000, (guint8 *)dup, (guint16) strlen(ascii)) == -1) {
1080                g_free(dup);
1081                return -1;
1082        }
1083
1084        return 0;
1085}
1086
1087int aim_mpmsg_addunicode(aim_session_t *sess, aim_mpmsg_t *mpm, const guint16 *unicode, guint16 unicodelen)
1088{
1089        guint8 *buf;
1090        aim_bstream_t bs;
1091        int i;
1092
1093        if (!(buf = g_malloc(unicodelen * 2)))
1094                return -1;
1095
1096        aim_bstream_init(&bs, buf, unicodelen * 2);
1097
1098        /* We assume unicode is in /host/ byte order -- convert to network */
1099        for (i = 0; i < unicodelen; i++)
1100                aimbs_put16(&bs, unicode[i]);
1101
1102        if (mpmsg_addsection(sess, mpm, 0x0002, 0x0000, buf, aim_bstream_curpos(&bs)) == -1) {
1103                g_free(buf);
1104                return -1;
1105        }
1106       
1107        return 0;
1108}
1109
1110void aim_mpmsg_free(aim_session_t *sess, aim_mpmsg_t *mpm)
1111{
1112        aim_mpmsg_section_t *cur;
1113
1114        for (cur = mpm->parts; cur; ) {
1115                aim_mpmsg_section_t *tmp;
1116               
1117                tmp = cur->next;
1118                g_free(cur->data);
1119                g_free(cur);
1120                cur = tmp;
1121        }
1122       
1123        mpm->numparts = 0;
1124        mpm->parts = NULL;
1125
1126        return;
1127}
1128
1129/*
1130 * Start by building the multipart structures, then pick the first
1131 * human-readable section and stuff it into args->msg so no one gets
1132 * suspicious.
1133 *
1134 */
1135static int incomingim_ch1_parsemsgs(aim_session_t *sess, guint8 *data, int len, struct aim_incomingim_ch1_args *args)
1136{
1137        static const guint16 charsetpri[] = {
1138                0x0000, /* ASCII first */
1139                0x0003, /* then ISO-8859-1 */
1140                0x0002, /* UNICODE as last resort */
1141        };
1142        static const int charsetpricount = 3;
1143        int i;
1144        aim_bstream_t mbs;
1145        aim_mpmsg_section_t *sec;
1146
1147        aim_bstream_init(&mbs, data, len);
1148
1149        while (aim_bstream_empty(&mbs)) {
1150                guint16 msglen, flag1, flag2;
1151                char *msgbuf;
1152
1153                aimbs_get8(&mbs); /* 01 */
1154                aimbs_get8(&mbs); /* 01 */
1155
1156                /* Message string length, including character set info. */
1157                msglen = aimbs_get16(&mbs);
1158
1159                /* Character set info */
1160                flag1 = aimbs_get16(&mbs);
1161                flag2 = aimbs_get16(&mbs);
1162
1163                /* Message. */
1164                msglen -= 4;
1165
1166                /*
1167                 * For now, we don't care what the encoding is.  Just copy
1168                 * it into a multipart struct and deal with it later. However,
1169                 * always pad the ending with a NULL.  This makes it easier
1170                 * to treat ASCII sections as strings.  It won't matter for
1171                 * UNICODE or binary data, as you should never read past
1172                 * the specified data length, which will not include the pad.
1173                 *
1174                 * XXX There's an API bug here.  For sending, the UNICODE is
1175                 * given in host byte order (aim_mpmsg_addunicode), but here
1176                 * the received messages are given in network byte order.
1177                 *
1178                 */
1179                msgbuf = aimbs_getstr(&mbs, msglen);
1180                mpmsg_addsection(sess, &args->mpmsg, flag1, flag2, (guint8 *)msgbuf, (guint16) msglen);
1181
1182        } /* while */
1183
1184        args->icbmflags |= AIM_IMFLAGS_MULTIPART; /* always set */
1185
1186        /*
1187         * Clients that support multiparts should never use args->msg, as it
1188         * will point to an arbitrary section.
1189         *
1190         * Here, we attempt to provide clients that do not support multipart
1191         * messages with something to look at -- hopefully a human-readable
1192         * string.  But, failing that, a UNICODE message, or nothing at all.
1193         *
1194         * Which means that even if args->msg is NULL, it does not mean the
1195         * message was blank.
1196         *
1197         */
1198        for (i = 0; i < charsetpricount; i++) {
1199                for (sec = args->mpmsg.parts; sec; sec = sec->next) {
1200
1201                        if (sec->charset != charsetpri[i])
1202                                continue;
1203
1204                        /* Great. We found one.  Fill it in. */
1205                        args->charset = sec->charset;
1206                        args->charsubset = sec->charsubset;
1207                        args->icbmflags |= AIM_IMFLAGS_CUSTOMCHARSET;
1208
1209                        /* Set up the simple flags */
1210                        if (args->charset == 0x0000)
1211                                ; /* ASCII */
1212                        else if (args->charset == 0x0002)
1213                                args->icbmflags |= AIM_IMFLAGS_UNICODE;
1214                        else if (args->charset == 0x0003)
1215                                args->icbmflags |= AIM_IMFLAGS_ISO_8859_1;
1216                        else if (args->charset == 0xffff)
1217                                ; /* no encoding (yeep!) */
1218
1219                        if (args->charsubset == 0x0000)
1220                                ; /* standard subencoding? */
1221                        else if (args->charsubset == 0x000b)
1222                                args->icbmflags |= AIM_IMFLAGS_SUBENC_MACINTOSH;
1223                        else if (args->charsubset == 0xffff)
1224                                ; /* no subencoding */
1225#if 0
1226                        /* XXX this isn't really necesary... */
1227                        if (    ((args.flag1 != 0x0000) &&
1228                                 (args.flag1 != 0x0002) &&
1229                                 (args.flag1 != 0x0003) &&
1230                                 (args.flag1 != 0xffff)) ||
1231                                ((args.flag2 != 0x0000) &&
1232                                 (args.flag2 != 0x000b) &&
1233                                 (args.flag2 != 0xffff))) {
1234                                faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", args.flag1, args.flag2);
1235                        }
1236#endif
1237
1238                        args->msg = (char *)sec->data;
1239                        args->msglen = sec->datalen;
1240
1241                        return 0;
1242                }
1243        }
1244
1245        /* No human-readable sections found.  Oh well. */
1246        args->charset = args->charsubset = 0xffff;
1247        args->msg = NULL;
1248        args->msglen = 0;
1249
1250        return 0;
1251}
1252
1253static 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)
1254{
1255        guint16 type, length;
1256        aim_rxcallback_t userfunc;
1257        int ret = 0;
1258        struct aim_incomingim_ch1_args args;
1259        int endpos;
1260
1261        memset(&args, 0, sizeof(args));
1262
1263        aim_mpmsg_init(sess, &args.mpmsg);
1264
1265        /*
1266         * This used to be done using tlvchains.  For performance reasons,
1267         * I've changed it to process the TLVs in-place.  This avoids lots
1268         * of per-IM memory allocations.
1269         */
1270        while (aim_bstream_empty(bs)) {
1271
1272                type = aimbs_get16(bs);
1273                length = aimbs_get16(bs);
1274
1275                endpos = aim_bstream_curpos(bs) + length;
1276
1277                if (type == 0x0002) { /* Message Block */
1278
1279                        /*
1280                         * This TLV consists of the following:
1281                         *   - 0501 -- Unknown
1282                         *   - Features: Don't know how to interpret these
1283                         *   - 0101 -- Unknown
1284                         *   - Message
1285                         *
1286                         */
1287
1288                        aimbs_get8(bs); /* 05 */
1289                        aimbs_get8(bs); /* 01 */
1290
1291                        args.featureslen = aimbs_get16(bs);
1292                        /* XXX XXX this is all evil! */
1293                        args.features = bs->data + bs->offset;
1294                        aim_bstream_advance(bs, args.featureslen);
1295                        args.icbmflags |= AIM_IMFLAGS_CUSTOMFEATURES;
1296
1297                        /*
1298                         * The rest of the TLV contains one or more message
1299                         * blocks...
1300                         */
1301                        incomingim_ch1_parsemsgs(sess, bs->data + bs->offset /* XXX evil!!! */, length - 2 - 2 - args.featureslen, &args);
1302
1303                } else if (type == 0x0003) { /* Server Ack Requested */
1304
1305                        args.icbmflags |= AIM_IMFLAGS_ACK;
1306
1307                } else if (type == 0x0004) { /* Message is Auto Response */
1308
1309                        args.icbmflags |= AIM_IMFLAGS_AWAY;
1310
1311                } else if (type == 0x0006) { /* Message was received offline. */
1312
1313                        /* XXX not sure if this actually gets sent. */
1314                        args.icbmflags |= AIM_IMFLAGS_OFFLINE;
1315
1316                } else if (type == 0x0008) { /* I-HAVE-A-REALLY-PURTY-ICON Flag */
1317
1318                        args.iconlen = aimbs_get32(bs);
1319                        aimbs_get16(bs); /* 0x0001 */
1320                        args.iconsum = aimbs_get16(bs);
1321                        args.iconstamp = aimbs_get32(bs);
1322
1323                        /*
1324                         * This looks to be a client bug.  MacAIM 4.3 will
1325                         * send this tag, but with all zero values, in the
1326                         * first message of a conversation. This makes no
1327                         * sense whatsoever, so I'm going to say its a bug.
1328                         *
1329                         * You really shouldn't advertise a zero-length icon
1330                         * anyway.
1331                         *
1332                         */
1333                        if (args.iconlen)
1334                                args.icbmflags |= AIM_IMFLAGS_HASICON;
1335
1336                } else if (type == 0x0009) {
1337
1338                        args.icbmflags |= AIM_IMFLAGS_BUDDYREQ;
1339
1340                } else if (type == 0x0017) {
1341
1342                        args.extdatalen = length;
1343                        args.extdata = aimbs_getraw(bs, args.extdatalen);
1344
1345                } else {
1346                        // imcb_error(sess->aux_data, "Unknown TLV encountered");
1347                }
1348
1349                /*
1350                 * This is here to protect ourselves from ourselves.  That
1351                 * is, if something above doesn't completly parse its value
1352                 * section, or, worse, overparses it, this will set the
1353                 * stream where it needs to be in order to land on the next
1354                 * TLV when the loop continues.
1355                 *
1356                 */
1357                aim_bstream_setpos(bs, endpos);
1358        }
1359
1360
1361        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1362                ret = userfunc(sess, rx, channel, userinfo, &args);
1363
1364        aim_mpmsg_free(sess, &args.mpmsg);
1365        g_free(args.extdata);
1366
1367        return ret;
1368}
1369
1370
1371static void incomingim_ch2_chat_free(aim_session_t *sess, struct aim_incomingim_ch2_args *args)
1372{
1373
1374        /* XXX aim_chat_roominfo_free() */
1375        g_free(args->info.chat.roominfo.name);
1376
1377        return;
1378}
1379
1380static void incomingim_ch2_chat(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_userinfo_t *userinfo, struct aim_incomingim_ch2_args *args, aim_bstream_t *servdata)
1381{
1382
1383        /*
1384         * Chat room info.
1385         */
1386        if (servdata)
1387                aim_chat_readroominfo(servdata, &args->info.chat.roominfo);
1388
1389        args->destructor = (void *)incomingim_ch2_chat_free;
1390
1391        return;
1392}
1393
1394static void incomingim_ch2_icqserverrelay_free(aim_session_t *sess, struct aim_incomingim_ch2_args *args)
1395{
1396
1397        g_free((char *)args->info.rtfmsg.rtfmsg);
1398
1399        return;
1400}
1401
1402/*
1403 * The relationship between AIM_CAPS_ICQSERVERRELAY and AIM_CAPS_ICQRTF is
1404 * kind of odd. This sends the client ICQRTF since that is all that I've seen
1405 * SERVERRELAY used for.
1406 *
1407 * Note that this is all little-endian.  Cringe.
1408 *
1409 * This cap is used for auto status message replies, too [ft]
1410 *
1411 */
1412static 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)
1413{
1414        guint16 hdrlen, msglen, dc;
1415        guint8 msgtype;
1416    guint8 *plugin;
1417    int i = 0, tmp = 0;
1418    struct im_connection *ic = sess->aux_data;
1419
1420    /* at the moment we just can deal with requests, not with cancel or accept */
1421    if (args->status != 0) return;
1422
1423        hdrlen = aimbs_getle16(servdata);
1424
1425    aim_bstream_advance(servdata, 0x02); /* protocol version */
1426    plugin = aimbs_getraw(servdata, 0x10); /* following data is a message or
1427                                              something plugin specific */
1428    /* as there is no plugin handling, just skip the rest */
1429    aim_bstream_advance(servdata, hdrlen - 0x12);
1430
1431        hdrlen = aimbs_getle16(servdata);
1432    dc = aimbs_getle16(servdata); /* save the sequence number */
1433        aim_bstream_advance(servdata, hdrlen - 0x02);
1434
1435    /* TODO is it a message or something for a plugin? */
1436    for (i = 0; i < 0x10; i++) {
1437        tmp |= plugin[i];
1438    }
1439
1440    if (!tmp) { /* message follows */
1441
1442        msgtype = aimbs_getle8(servdata);
1443        aimbs_getle8(servdata); /* msgflags */
1444
1445        aim_bstream_advance(servdata, 0x04); /* status code and priority code */
1446
1447        msglen = aimbs_getle16(servdata); /* message string length */
1448        args->info.rtfmsg.rtfmsg = aimbs_getstr(servdata, msglen);
1449
1450        switch(msgtype) {
1451            case AIM_MTYPE_PLAIN:
1452
1453                args->info.rtfmsg.fgcolor = aimbs_getle32(servdata);
1454                args->info.rtfmsg.bgcolor = aimbs_getle32(servdata);
1455
1456                hdrlen = aimbs_getle32(servdata);
1457                aim_bstream_advance(servdata, hdrlen);
1458
1459                /* XXX This is such a hack. */
1460                args->reqclass = AIM_CAPS_ICQRTF;
1461                break;
1462
1463            case AIM_MTYPE_AUTOAWAY: 
1464            case AIM_MTYPE_AUTOBUSY:
1465            case AIM_MTYPE_AUTONA:
1466            case AIM_MTYPE_AUTODND:
1467            case AIM_MTYPE_AUTOFFC:
1468            case 0x9c:  /* ICQ 5 seems to send this */
1469                aim_send_im_ch2_statusmessage(sess, userinfo->sn, args->cookie,
1470                        ic->away ? ic->away : "", sess->aim_icq_state, dc);
1471                break;
1472
1473        }
1474    } /* message or plugin specific */
1475
1476    g_free(plugin);
1477        args->destructor = (void *)incomingim_ch2_icqserverrelay_free;
1478
1479        return;
1480}
1481
1482typedef void (*ch2_args_destructor_t)(aim_session_t *sess, struct aim_incomingim_ch2_args *args);
1483
1484static 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)
1485{
1486        aim_rxcallback_t userfunc;
1487        aim_tlv_t *block1, *servdatatlv;
1488        aim_tlvlist_t *list2;
1489        struct aim_incomingim_ch2_args args;
1490        aim_bstream_t bbs, sdbs, *sdbsptr = NULL;
1491        guint8 *cookie2;
1492        int ret = 0;
1493
1494        char clientip1[30] = {""};
1495        char clientip2[30] = {""};
1496        char verifiedip[30] = {""};
1497
1498        memset(&args, 0, sizeof(args));
1499
1500        /*
1501         * There's another block of TLVs embedded in the type 5 here.
1502         */
1503        block1 = aim_gettlv(tlvlist, 0x0005, 1);
1504        aim_bstream_init(&bbs, block1->value, block1->length);
1505
1506        /*
1507         * First two bytes represent the status of the connection.
1508         *
1509         * 0 is a request, 1 is a deny (?), 2 is an accept
1510         */ 
1511        args.status = aimbs_get16(&bbs);
1512
1513        /*
1514         * Next comes the cookie.  Should match the ICBM cookie.
1515         */
1516        cookie2 = aimbs_getraw(&bbs, 8);
1517        if (memcmp(cookie, cookie2, 8) != 0) 
1518                imcb_error(sess->aux_data, "rend: warning cookies don't match!");
1519        memcpy(args.cookie, cookie2, 8);
1520        g_free(cookie2);
1521
1522        /*
1523         * The next 16bytes are a capability block so we can
1524         * identify what type of rendezvous this is.
1525         */
1526        args.reqclass = aim_getcap(sess, &bbs, 0x10);
1527
1528        /*
1529         * What follows may be TLVs or nothing, depending on the
1530         * purpose of the message.
1531         *
1532         * Ack packets for instance have nothing more to them.
1533         */
1534        list2 = aim_readtlvchain(&bbs);
1535
1536        /*
1537         * IP address from the perspective of the client.
1538         */
1539        if (aim_gettlv(list2, 0x0002, 1)) {
1540                aim_tlv_t *iptlv;
1541
1542                iptlv = aim_gettlv(list2, 0x0002, 1);
1543
1544                g_snprintf(clientip1, sizeof(clientip1), "%d.%d.%d.%d",
1545                                aimutil_get8(iptlv->value+0),
1546                                aimutil_get8(iptlv->value+1),
1547                                aimutil_get8(iptlv->value+2),
1548                                aimutil_get8(iptlv->value+3));
1549        }
1550
1551        /*
1552         * Secondary IP address from the perspective of the client.
1553         */
1554        if (aim_gettlv(list2, 0x0003, 1)) {
1555                aim_tlv_t *iptlv;
1556
1557                iptlv = aim_gettlv(list2, 0x0003, 1);
1558
1559                g_snprintf(clientip2, sizeof(clientip2), "%d.%d.%d.%d",
1560                                aimutil_get8(iptlv->value+0),
1561                                aimutil_get8(iptlv->value+1),
1562                                aimutil_get8(iptlv->value+2),
1563                                aimutil_get8(iptlv->value+3));
1564        }
1565
1566        /*
1567         * Verified IP address (from the perspective of Oscar).
1568         *
1569         * This is added by the server.
1570         */
1571        if (aim_gettlv(list2, 0x0004, 1)) {
1572                aim_tlv_t *iptlv;
1573
1574                iptlv = aim_gettlv(list2, 0x0004, 1);
1575
1576                g_snprintf(verifiedip, sizeof(verifiedip), "%d.%d.%d.%d",
1577                                aimutil_get8(iptlv->value+0),
1578                                aimutil_get8(iptlv->value+1),
1579                                aimutil_get8(iptlv->value+2),
1580                                aimutil_get8(iptlv->value+3));
1581        }
1582
1583        /*
1584         * Port number for something.
1585         */
1586        if (aim_gettlv(list2, 0x0005, 1))
1587                args.port = aim_gettlv16(list2, 0x0005, 1);
1588
1589        /*
1590         * Error code.
1591         */
1592        if (aim_gettlv(list2, 0x000b, 1))
1593                args.errorcode = aim_gettlv16(list2, 0x000b, 1);
1594
1595        /*
1596         * Invitation message / chat description.
1597         */
1598        if (aim_gettlv(list2, 0x000c, 1))
1599                args.msg = aim_gettlv_str(list2, 0x000c, 1);
1600
1601        /*
1602         * Character set.
1603         */
1604        if (aim_gettlv(list2, 0x000d, 1))
1605                args.encoding = aim_gettlv_str(list2, 0x000d, 1);
1606       
1607        /*
1608         * Language.
1609         */
1610        if (aim_gettlv(list2, 0x000e, 1))
1611                args.language = aim_gettlv_str(list2, 0x000e, 1);
1612
1613        /* Unknown -- two bytes = 0x0001 */
1614        if (aim_gettlv(list2, 0x000a, 1))
1615                ;
1616
1617        /* Unknown -- no value */
1618        if (aim_gettlv(list2, 0x000f, 1))
1619                ;
1620
1621        if (strlen(clientip1))
1622                args.clientip = (char *)clientip1;
1623        if (strlen(clientip2))
1624                args.clientip2 = (char *)clientip2;
1625        if (strlen(verifiedip))
1626                args.verifiedip = (char *)verifiedip;
1627
1628        /*
1629         * This is must be present in PROPOSALs, but will probably not
1630         * exist in CANCELs and ACCEPTs.
1631         *
1632         * Service Data blocks are module-specific in format.
1633         */
1634        if ((servdatatlv = aim_gettlv(list2, 0x2711 /* 10001 */, 1))) {
1635
1636                aim_bstream_init(&sdbs, servdatatlv->value, servdatatlv->length);
1637                sdbsptr = &sdbs;
1638        }
1639
1640        if (args.reqclass & AIM_CAPS_ICQSERVERRELAY)
1641                incomingim_ch2_icqserverrelay(sess, mod, rx, snac, userinfo, &args, sdbsptr);
1642        else if (args.reqclass & AIM_CAPS_CHAT)
1643                incomingim_ch2_chat(sess, mod, rx, snac, userinfo, &args, sdbsptr);
1644
1645
1646        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1647                ret = userfunc(sess, rx, channel, userinfo, &args);
1648
1649
1650        if (args.destructor)
1651                ((ch2_args_destructor_t)args.destructor)(sess, &args);
1652
1653        g_free((char *)args.msg);
1654        g_free((char *)args.encoding);
1655        g_free((char *)args.language);
1656
1657        aim_freetlvchain(&list2);
1658
1659        return ret;
1660}
1661
1662static 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)
1663{
1664        aim_bstream_t meat;
1665        aim_rxcallback_t userfunc;
1666        aim_tlv_t *block;
1667        struct aim_incomingim_ch4_args args;
1668        int ret = 0;
1669
1670        /*
1671         * Make a bstream for the meaty part.  Yum.  Meat.
1672         */
1673        if (!(block = aim_gettlv(tlvlist, 0x0005, 1)))
1674                return -1;
1675        aim_bstream_init(&meat, block->value, block->length);
1676
1677        args.uin = aimbs_getle32(&meat);
1678        args.type = aimbs_getle16(&meat);
1679        args.msg = (char *)aimbs_getraw(&meat, aimbs_getle16(&meat));
1680
1681        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1682                ret = userfunc(sess, rx, channel, userinfo, &args);
1683
1684        g_free(args.msg);
1685
1686        return ret;
1687}
1688
1689/*
1690 * It can easily be said that parsing ICBMs is THE single
1691 * most difficult thing to do in the in AIM protocol.  In
1692 * fact, I think I just did say that.
1693 *
1694 * Below is the best damned solution I've come up with
1695 * over the past sixteen months of battling with it. This
1696 * can parse both away and normal messages from every client
1697 * I have access to.  Its not fast, its not clean.  But it works.
1698 *
1699 */
1700static int incomingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1701{
1702        int i, ret = 0;
1703        guint8 cookie[8];
1704        guint16 channel;
1705        aim_userinfo_t userinfo;
1706
1707        memset(&userinfo, 0x00, sizeof(aim_userinfo_t));
1708
1709        /*
1710         * Read ICBM Cookie.  And throw away.
1711         */
1712        for (i = 0; i < 8; i++)
1713                cookie[i] = aimbs_get8(bs);
1714
1715        /*
1716         * Channel ID.
1717         *
1718         * Channel 0x0001 is the message channel.  There are
1719         * other channels for things called "rendevous"
1720         * which represent chat and some of the other new
1721         * features of AIM2/3/3.5.
1722         *
1723         * Channel 0x0002 is the Rendevous channel, which
1724         * is where Chat Invitiations and various client-client
1725         * connection negotiations come from.
1726         *
1727         * Channel 0x0004 is used for ICQ authorization, or
1728         * possibly any system notice.
1729         *
1730         */
1731        channel = aimbs_get16(bs);
1732
1733        /*
1734         * Extract the standard user info block.
1735         *
1736         * Note that although this contains TLVs that appear contiguous
1737         * with the TLVs read below, they are two different pieces.  The
1738         * userinfo block contains the number of TLVs that contain user
1739         * information, the rest are not even though there is no seperation.
1740         * aim_extractuserinfo() returns the number of bytes used by the
1741         * userinfo tlvs, so you can start reading the rest of them right
1742         * afterward. 
1743         *
1744         * That also means that TLV types can be duplicated between the
1745         * userinfo block and the rest of the message, however there should
1746         * never be two TLVs of the same type in one block.
1747         *
1748         */
1749        aim_extractuserinfo(sess, bs, &userinfo);
1750
1751        /*
1752         * From here on, its depends on what channel we're on.
1753         *
1754         * Technically all channels have a TLV list have this, however,
1755         * for the common channel 1 case, in-place parsing is used for
1756         * performance reasons (less memory allocation).
1757         */
1758        if (channel == 1) {
1759
1760                ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, bs, cookie);
1761
1762        } else if (channel == 2) {
1763                aim_tlvlist_t *tlvlist;
1764
1765                /*
1766                 * Read block of TLVs (not including the userinfo data).  All
1767                 * further data is derived from what is parsed here.
1768                 */
1769                tlvlist = aim_readtlvchain(bs);
1770
1771                ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
1772
1773                aim_freetlvchain(&tlvlist);
1774
1775        } else if (channel == 4) {
1776                aim_tlvlist_t *tlvlist;
1777
1778                tlvlist = aim_readtlvchain(bs);
1779                ret = incomingim_ch4(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
1780                aim_freetlvchain(&tlvlist);
1781
1782        } else {
1783
1784                imcb_error(sess->aux_data, "ICBM received on an unsupported channel.  Ignoring.");
1785
1786                return 0;
1787        }
1788
1789        return ret;
1790}
1791
1792/*
1793 * Possible codes:
1794 *    AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
1795 *    AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
1796 *    AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
1797 *
1798 */
1799int aim_denytransfer(aim_session_t *sess, const char *sender, const guint8 *cookie, guint16 code)
1800{
1801        aim_conn_t *conn;
1802        aim_frame_t *fr;
1803        aim_snacid_t snacid;
1804        aim_tlvlist_t *tl = NULL;
1805       
1806        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
1807                return -EINVAL;
1808
1809        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sender)+6)))
1810                return -ENOMEM;
1811
1812        snacid = aim_cachesnac(sess, 0x0004, 0x000b, 0x0000, NULL, 0);
1813        aim_putsnac(&fr->data, 0x0004, 0x000b, 0x0000, snacid);
1814       
1815        aimbs_putraw(&fr->data, cookie, 8);
1816
1817        aimbs_put16(&fr->data, 0x0002); /* channel */
1818        aimbs_put8(&fr->data, strlen(sender));
1819        aimbs_putraw(&fr->data, (guint8 *)sender, strlen(sender));
1820
1821        aim_addtlvtochain16(&tl, 0x0003, code);
1822        aim_writetlvchain(&fr->data, &tl);
1823        aim_freetlvchain(&tl);
1824
1825        aim_tx_enqueue(sess, fr);
1826
1827        return 0;
1828}
1829
1830/*
1831 * aim_reqicbmparaminfo()
1832 *
1833 * Request ICBM parameter information.
1834 *
1835 */
1836int aim_reqicbmparams(aim_session_t *sess)
1837{
1838        aim_conn_t *conn;
1839
1840        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
1841                return -EINVAL;
1842
1843        return aim_genericreq_n(sess, conn, 0x0004, 0x0004);
1844}
1845
1846/*
1847 *
1848 * I definitly recommend sending this.  If you don't, you'll be stuck
1849 * with the rather unreasonable defaults.  You don't want those.  Send this.
1850 *
1851 */
1852int aim_seticbmparam(aim_session_t *sess, struct aim_icbmparameters *params)
1853{
1854        aim_conn_t *conn;
1855        aim_frame_t *fr;
1856        aim_snacid_t snacid;
1857
1858        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004)))
1859                return -EINVAL;
1860
1861        if (!params)
1862                return -EINVAL;
1863
1864        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+16)))
1865                return -ENOMEM;
1866
1867        snacid = aim_cachesnac(sess, 0x0004, 0x0002, 0x0000, NULL, 0);
1868        aim_putsnac(&fr->data, 0x0004, 0x0002, 0x0000, snacid);
1869
1870        /* This is read-only (see Parameter Reply). Must be set to zero here. */
1871        aimbs_put16(&fr->data, 0x0000);
1872
1873        /* These are all read-write */
1874        aimbs_put32(&fr->data, params->flags); 
1875        aimbs_put16(&fr->data, params->maxmsglen);
1876        aimbs_put16(&fr->data, params->maxsenderwarn); 
1877        aimbs_put16(&fr->data, params->maxrecverwarn); 
1878        aimbs_put32(&fr->data, params->minmsginterval);
1879
1880        aim_tx_enqueue(sess, fr);
1881
1882        return 0;
1883}
1884
1885static int paraminfo(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1886{
1887        struct aim_icbmparameters params;
1888        aim_rxcallback_t userfunc;
1889
1890        params.maxchan = aimbs_get16(bs);
1891        params.flags = aimbs_get32(bs);
1892        params.maxmsglen = aimbs_get16(bs);
1893        params.maxsenderwarn = aimbs_get16(bs);
1894        params.maxrecverwarn = aimbs_get16(bs);
1895        params.minmsginterval = aimbs_get32(bs);
1896       
1897        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1898                return userfunc(sess, rx, &params);
1899
1900        return 0;
1901}
1902
1903static int missedcall(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, nummissed, reason;
1908        aim_userinfo_t userinfo;
1909
1910        while (aim_bstream_empty(bs)) { 
1911
1912                channel = aimbs_get16(bs);
1913                aim_extractuserinfo(sess, bs, &userinfo);
1914                nummissed = aimbs_get16(bs);
1915                reason = aimbs_get16(bs);
1916
1917                if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1918                         ret = userfunc(sess, rx, channel, &userinfo, nummissed, reason);
1919        }
1920
1921        return ret;
1922}
1923
1924/*
1925 * Receive the response from an ICQ status message request.  This contains the
1926 * ICQ status message.  Go figure.
1927 */
1928static int clientautoresp(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
1929{
1930        int ret = 0;
1931        aim_rxcallback_t userfunc;
1932        guint16 channel, reason;
1933        char *sn;
1934        guint8 *ck, snlen;
1935
1936        ck = aimbs_getraw(bs, 8);
1937        channel = aimbs_get16(bs);
1938        snlen = aimbs_get8(bs);
1939        sn = aimbs_getstr(bs, snlen);
1940        reason = aimbs_get16(bs);
1941
1942        switch (reason) {
1943                case 0x0003: { /* ICQ status message.  Maybe other stuff too, you never know with these people. */
1944                        guint8 statusmsgtype, *msg;
1945                        guint16 len;
1946                        guint32 state;
1947
1948                        len = aimbs_getle16(bs); /* Should be 0x001b */
1949                        aim_bstream_advance(bs, len); /* Unknown */
1950
1951                        len = aimbs_getle16(bs); /* Should be 0x000e */
1952                        aim_bstream_advance(bs, len); /* Unknown */
1953
1954                        statusmsgtype = aimbs_getle8(bs);
1955                        switch (statusmsgtype) {
1956                                case 0xe8:
1957                                        state = AIM_ICQ_STATE_AWAY;
1958                                        break;
1959                                case 0xe9:
1960                                        state = AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_BUSY;
1961                                        break;
1962                                case 0xea:
1963                                        state = AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_OUT;
1964                                        break;
1965                                case 0xeb:
1966                                        state = AIM_ICQ_STATE_AWAY | AIM_ICQ_STATE_DND | AIM_ICQ_STATE_BUSY;
1967                                        break;
1968                                case 0xec:
1969                                        state = AIM_ICQ_STATE_CHAT;
1970                                        break;
1971                                default:
1972                                        state = 0;
1973                                        break;
1974                        }
1975
1976                        aimbs_getle8(bs); /* Unknown - 0x03 Maybe this means this is an auto-reply */
1977                        aimbs_getle16(bs); /* Unknown - 0x0000 */
1978                        aimbs_getle16(bs); /* Unknown - 0x0000 */
1979
1980                        len = aimbs_getle16(bs);
1981                        msg = aimbs_getraw(bs, len);
1982
1983                        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1984                                ret = userfunc(sess, rx, channel, sn, reason, state, msg);
1985
1986                        g_free(msg);
1987                } break;
1988
1989                default: {
1990                        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1991                                ret = userfunc(sess, rx, channel, sn, reason);
1992                } break;
1993        } /* end switch */
1994
1995        g_free(ck);
1996        g_free(sn);
1997
1998        return ret;
1999}
2000
2001static int msgack(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
2002{
2003        aim_rxcallback_t userfunc;
2004        guint16 type;
2005        guint8 snlen, *ck;
2006        char *sn;
2007        int ret = 0;
2008
2009        ck = aimbs_getraw(bs, 8);
2010        type = aimbs_get16(bs);
2011        snlen = aimbs_get8(bs);
2012        sn = aimbs_getstr(bs, snlen);
2013
2014        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
2015                ret = userfunc(sess, rx, type, sn);
2016
2017        g_free(sn);
2018        g_free(ck);
2019
2020        return ret;
2021}
2022
2023/*
2024 * Subtype 0x0014 - Send a mini typing notification (mtn) packet.
2025 *
2026 * This is supported by winaim5 and newer, MacAIM bleh and newer, iChat bleh and newer,
2027 * and Gaim 0.60 and newer.
2028 *
2029 */
2030int aim_im_sendmtn(aim_session_t *sess, guint16 type1, const char *sn, guint16 type2)
2031{
2032        aim_conn_t *conn;
2033        aim_frame_t *fr;
2034        aim_snacid_t snacid;
2035
2036        if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0002)))
2037                return -EINVAL;
2038
2039        if (!sn)
2040                return -EINVAL;
2041
2042        if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+11+strlen(sn)+2)))
2043                return -ENOMEM;
2044
2045        snacid = aim_cachesnac(sess, 0x0004, 0x0014, 0x0000, NULL, 0);
2046        aim_putsnac(&fr->data, 0x0004, 0x0014, 0x0000, snacid);
2047
2048        /*
2049         * 8 days of light
2050         * Er, that is to say, 8 bytes of 0's
2051         */
2052        aimbs_put16(&fr->data, 0x0000);
2053        aimbs_put16(&fr->data, 0x0000);
2054        aimbs_put16(&fr->data, 0x0000);
2055        aimbs_put16(&fr->data, 0x0000);
2056
2057        /*
2058         * Type 1 (should be 0x0001 for mtn)
2059         */
2060        aimbs_put16(&fr->data, type1);
2061
2062        /*
2063         * Dest sn
2064         */
2065        aimbs_put8(&fr->data, strlen(sn));
2066        aimbs_putraw(&fr->data, (const guint8*)sn, strlen(sn));
2067
2068        /*
2069         * Type 2 (should be 0x0000, 0x0001, or 0x0002 for mtn)
2070         */
2071        aimbs_put16(&fr->data, type2);
2072
2073        aim_tx_enqueue(sess, fr);
2074
2075        return 0;
2076}
2077
2078/*
2079 * Subtype 0x0014 - Receive a mini typing notification (mtn) packet.
2080 *
2081 * This is supported by winaim5 and newer, MacAIM bleh and newer, iChat bleh and newer,
2082 * and Gaim 0.60 and newer.
2083 *
2084 */
2085static int mtn_receive(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
2086{
2087        int ret = 0;
2088        aim_rxcallback_t userfunc;
2089        char *sn;
2090        guint8 snlen;
2091        guint16 type1, type2;
2092
2093        aim_bstream_advance(bs, 8); /* Unknown - All 0's */
2094        type1 = aimbs_get16(bs);
2095        snlen = aimbs_get8(bs);
2096        sn = aimbs_getstr(bs, snlen);
2097        type2 = aimbs_get16(bs);
2098
2099        if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
2100                ret = userfunc(sess, rx, type1, sn, type2);
2101
2102        g_free(sn);
2103
2104        return ret;
2105}
2106
2107static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
2108{
2109
2110        if (snac->subtype == 0x0005)
2111                return paraminfo(sess, mod, rx, snac, bs);
2112        else if (snac->subtype == 0x0006)
2113                return outgoingim(sess, mod, rx, snac, bs);
2114        else if (snac->subtype == 0x0007)
2115                return incomingim(sess, mod, rx, snac, bs);
2116        else if (snac->subtype == 0x000a)
2117                return missedcall(sess, mod, rx, snac, bs);
2118        else if (snac->subtype == 0x000b)
2119                return clientautoresp(sess, mod, rx, snac, bs);
2120        else if (snac->subtype == 0x000c)
2121                return msgack(sess, mod, rx, snac, bs);
2122        else if (snac->subtype == 0x0014)
2123                return mtn_receive(sess, mod, rx, snac, bs);
2124
2125        return 0;
2126}
2127
2128int msg_modfirst(aim_session_t *sess, aim_module_t *mod)
2129{
2130
2131        mod->family = 0x0004;
2132        mod->version = 0x0001;
2133        mod->toolid = 0x0110;
2134        mod->toolversion = 0x0629;
2135        mod->flags = 0;
2136        strncpy(mod->name, "messaging", sizeof(mod->name));
2137        mod->snachandler = snachandler;
2138
2139        return 0;
2140}
Note: See TracBrowser for help on using the repository browser.