]> andersk Git - libfaim.git/blob - src/im.c
d44e46efefceef04d429a13a4a245fb122b40daa
[libfaim.git] / src / im.c
1 /*
2  *  aim_im.c
3  *
4  *  The routines for sending/receiving Instant Messages.
5  *
6  */
7
8 #define FAIM_INTERNAL
9 #include <aim.h>
10
11 /*
12  * Takes a msghdr (and a length) and returns a client type
13  * code.  Note that this is *only a guess* and has a low likelihood
14  * of actually being accurate.
15  *
16  * Its based on experimental data, with the help of Eric Warmenhoven
17  * who seems to have collected a wide variety of different AIM clients.
18  *
19  *
20  * Heres the current collection:
21  *  0501 0003 0101 0101 01       AOL Mobile Communicator, WinAIM 1.0.414
22  *  0501 0003 0101 0201 01       WinAIM 2.0.847, 2.1.1187, 3.0.1464, 
23  *                                      4.3.2229, 4.4.2286
24  *  0501 0004 0101 0102 0101     WinAIM 4.1.2010, libfaim (right here)
25  *  0501 0001 0101 01            AOL v6.0, CompuServe 2000 v6.0, any
26  *                                      TOC client
27  */
28 faim_export unsigned short aim_fingerprintclient(unsigned char *msghdr, int len)
29 {
30   static const struct {
31     unsigned short clientid;
32     int len;
33     unsigned char data[10];
34   } fingerprints[] = {
35     /* AOL Mobile Communicator, WinAIM 1.0.414 */
36     { AIM_CLIENTTYPE_MC, 
37       9, {0x05, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01}},
38
39     /* WinAIM 2.0.847, 2.1.1187, 3.0.1464, 4.3.2229, 4.4.2286 */
40     { AIM_CLIENTTYPE_WINAIM, 
41       9, {0x05, 0x01, 0x00, 0x03, 0x01, 0x01, 0x02, 0x01, 0x01}},
42
43     /* WinAIM 4.1.2010, libfaim */
44     { AIM_CLIENTTYPE_WINAIM41,
45       10, {0x05, 0x01, 0x00, 0x04, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01}},
46
47     /* AOL v6.0, CompuServe 2000 v6.0, any TOC client */
48     { AIM_CLIENTTYPE_AOL_TOC,
49       7, {0x05, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01}},
50
51     { 0, 0}
52   };
53   int i;
54
55   if (!msghdr || (len <= 0))
56     return 0;
57
58   for (i = 0; fingerprints[i].len; i++) {
59     if (fingerprints[i].len != len)
60       continue;
61     if (memcmp(fingerprints[i].data, msghdr, fingerprints[i].len) == 0)
62       return fingerprints[i].clientid;
63   }
64
65   return AIM_CLIENTTYPE_UNKNOWN;
66 }
67
68 /* This should be endian-safe now... but who knows... */
69 faim_export unsigned short aim_iconsum(const unsigned char *buf, int buflen)
70 {
71   unsigned long sum;
72   int i;
73
74   for (i = 0, sum = 0; i < buflen; i += 2)
75     sum += (buf[i+1] << 8) + buf[i];
76
77   sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff);
78
79   return sum & 0xffff;
80 }
81
82 /*
83  * Send an ICBM (instant message).  
84  *
85  *
86  * Possible flags:
87  *   AIM_IMFLAGS_AWAY  -- Marks the message as an autoresponse
88  *   AIM_IMFLAGS_ACK   -- Requests that the server send an ack
89  *                        when the message is received (of type 0x0004/0x000c)
90  *   AIM_IMFLAGS_UNICODE--Instead of ASCII7, the passed message is
91  *                        made up of UNICODE duples.  If you set
92  *                        this, you'd better be damn sure you know
93  *                        what you're doing.
94  *   AIM_IMFLAGS_ISO_8859_1 -- The message contains the ASCII8 subset
95  *                        known as ISO-8859-1.  
96  *
97  * Generally, you should use the lowest encoding possible to send
98  * your message.  If you only use basic punctuation and the generic
99  * Latin alphabet, use ASCII7 (no flags).  If you happen to use non-ASCII7
100  * characters, but they are all clearly defined in ISO-8859-1, then 
101  * use that.  Keep in mind that not all characters in the PC ASCII8
102  * character set are defined in the ISO standard. For those cases (most
103  * notably when the (r) symbol is used), you must use the full UNICODE
104  * encoding for your message.  In UNICODE mode, _all_ characters must
105  * occupy 16bits, including ones that are not special.  (Remember that
106  * the first 128 UNICODE symbols are equivelent to ASCII7, however they
107  * must be prefixed with a zero high order byte.)
108  *
109  * I strongly discourage the use of UNICODE mode, mainly because none
110  * of the clients I use can parse those messages (and besides that,
111  * wchars are difficult and non-portable to handle in most UNIX environments).
112  * If you really need to include special characters, use the HTML UNICODE 
113  * entities.  These are of the form &#2026; where 2026 is the hex 
114  * representation of the UNICODE index (in this case, UNICODE 
115  * "Horizontal Ellipsis", or 133 in in ASCII8).
116  *
117  */
118 faim_export unsigned long aim_send_im_ext(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_sendimext_args *args)
119 {
120   int curbyte,i;
121   struct command_tx_struct *newpacket;
122
123   if (!sess || !conn || !args)
124     return -1;
125
126   if (!args->msg || (args->msglen <= 0))
127     return -1;
128
129   if (args->msglen >= MAXMSGLEN)
130     return -1;
131
132   if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, args->msglen+512)))
133     return -1;
134
135   newpacket->lock = 1; /* lock struct */
136
137   curbyte  = 0;
138   curbyte += aim_putsnac(newpacket->data+curbyte, 
139                          0x0004, 0x0006, 0x0000, sess->snac_nextid);
140
141   /* 
142    * Generate a random message cookie 
143    *
144    * We could cache these like we do SNAC IDs.  (In fact, it 
145    * might be a good idea.)  In the message error functions, 
146    * the 8byte message cookie is returned as well as the 
147    * SNAC ID.
148    *
149    */
150   for (i = 0; i < 8; i++)
151     curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) rand());
152
153   /*
154    * Channel ID
155    */
156   curbyte += aimutil_put16(newpacket->data+curbyte,0x0001);
157
158   /* 
159    * Destination SN (prepended with byte length)
160    */
161   curbyte += aimutil_put8(newpacket->data+curbyte, strlen(args->destsn));
162   curbyte += aimutil_putstr(newpacket->data+curbyte, args->destsn, strlen(args->destsn));
163
164   /*
165    * metaTLV start.
166    */
167   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
168   curbyte += aimutil_put16(newpacket->data+curbyte, args->msglen + 0x10);
169
170   /*
171    * Flag data / ICBM Parameters?
172    *
173    * I don't know what these are...
174    *
175    */
176   curbyte += aimutil_put8(newpacket->data+curbyte, 0x05);
177   curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
178
179   /* number of bytes to follow */
180   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
181   curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
182   curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
183   curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
184   curbyte += aimutil_put8(newpacket->data+curbyte, 0x02);
185
186   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0101);
187
188   /* 
189    * Message block length.
190    */
191   curbyte += aimutil_put16(newpacket->data+curbyte, args->msglen + 0x04);
192
193   /*
194    * Character set.
195    */
196   if (args->flags & AIM_IMFLAGS_UNICODE)
197     curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
198   else if (args->flags & AIM_IMFLAGS_ISO_8859_1)
199     curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
200   else
201     curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
202
203   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
204
205   /*
206    * Message.  Not terminated.
207    */
208   curbyte += aimutil_putstr(newpacket->data+curbyte, args->msg, args->msglen);
209
210   /*
211    * Set the Request Acknowledge flag.  
212    */
213   if (args->flags & AIM_IMFLAGS_ACK) {
214     curbyte += aimutil_put16(newpacket->data+curbyte,0x0003);
215     curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
216   }
217   
218   /*
219    * Set the Autoresponse flag.
220    */
221   if (args->flags & AIM_IMFLAGS_AWAY) {
222     curbyte += aimutil_put16(newpacket->data+curbyte,0x0004);
223     curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
224   }
225   
226   /*
227    * Set the Buddy Icon Requested flag.
228    */
229   if (args->flags & AIM_IMFLAGS_BUDDYREQ) {
230     curbyte += aimutil_put16(newpacket->data+curbyte,0x0009);
231     curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
232   }
233
234   /*
235    * Set the I HAVE A REALLY PURTY ICON flag (with timestamp).
236    */
237   if (args->flags & AIM_IMFLAGS_HASICON) {
238     curbyte += aimutil_put16(newpacket->data+curbyte, 0x0008);
239     curbyte += aimutil_put16(newpacket->data+curbyte, 0x000c);
240     curbyte += aimutil_put32(newpacket->data+curbyte, args->iconlen);
241     curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
242     curbyte += aimutil_put16(newpacket->data+curbyte, args->iconsum);
243     curbyte += aimutil_put32(newpacket->data+curbyte, args->iconstamp);
244   }
245
246   newpacket->commandlen = curbyte;
247   newpacket->lock = 0;
248
249   aim_tx_enqueue(sess, newpacket);
250
251 #if 1 /* XXX do this with autoconf or something... */
252   aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, args->destsn, strlen(args->destsn)+1);
253   aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
254 #endif
255
256   return sess->snac_nextid;
257 }
258
259 /*
260  * Simple wrapper for aim_send_im_ext() 
261  *
262  * You cannot use aim_send_im if you need the HASICON flag.  You must
263  * use aim_send_im_ext directly for that.
264  *
265  * aim_send_im also cannot be used if you require UNICODE messages, because
266  * that requires an explicit message length.  Use aim_send_im_ext().
267  *
268  */
269 faim_export unsigned long aim_send_im(struct aim_session_t *sess, struct aim_conn_t *conn, const char *destsn, unsigned short flags, const char *msg)
270 {
271   struct aim_sendimext_args args;
272
273   args.destsn = destsn;
274   args.flags = flags;
275   args.msg = msg;
276   args.msglen = strlen(msg);
277
278   return aim_send_im_ext(sess, conn, &args);
279 }
280
281 faim_export int aim_send_icon(struct aim_session_t *sess, struct aim_conn_t *conn, const char *sn, const unsigned char *icon, int iconlen, time_t stamp, unsigned short iconsum)
282 {
283   struct command_tx_struct *np;
284   int i,curbyte = 0;
285   unsigned char ck[8];
286
287   if (!sess || !conn || !sn || !icon || (iconlen <= 0) || (iconlen >= MAXICONLEN))
288     return -1;
289
290   if (conn->type != AIM_CONN_TYPE_BOS)
291     return -1;
292
293   for (i = 0, curbyte = 0; i < 8; i++)
294     curbyte += aimutil_put8(ck+curbyte, (u_char)rand());
295
296   if (!(np = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 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)))
297     return -1;
298
299   np->lock = 1;
300
301   curbyte = aim_putsnac(np->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
302
303   /*
304    * Cookie
305    */
306   memcpy(np->data+curbyte, ck, 8);
307   curbyte += 8;
308
309   /*
310    * Channel (2)
311    */
312   curbyte += aimutil_put16(np->data+curbyte, 0x0002);
313
314   /*
315    * Dest sn
316    */
317   curbyte += aimutil_put8(np->data+curbyte, strlen(sn));
318   curbyte += aimutil_putstr(np->data+curbyte, sn, strlen(sn));
319
320   /*
321    * TLV t(0005)
322    */
323   curbyte += aimutil_put16(np->data+curbyte, 0x0005);
324   curbyte += aimutil_put16(np->data+curbyte, 2+8+16+6+4+4+iconlen+4+4+4+strlen(AIM_ICONIDENT));
325
326   curbyte += aimutil_put16(np->data+curbyte, 0x0000);
327
328   memcpy(np->data+curbyte, ck, 8);
329   curbyte += 8;
330
331   curbyte += aim_putcap(np->data+curbyte, 16, AIM_CAPS_BUDDYICON);
332
333   /* TLV t(000a) */
334   curbyte += aimutil_put16(np->data+curbyte, 0x000a);
335   curbyte += aimutil_put16(np->data+curbyte, 0x0002);
336   curbyte += aimutil_put16(np->data+curbyte, 0x0001);
337
338   /* TLV t(000f) */
339   curbyte += aimutil_put16(np->data+curbyte, 0x000f);
340   curbyte += aimutil_put16(np->data+curbyte, 0x0000);
341
342   /* TLV t(2711) */
343   curbyte += aimutil_put16(np->data+curbyte, 0x2711);
344   curbyte += aimutil_put16(np->data+curbyte, 4+4+4+iconlen+strlen(AIM_ICONIDENT));
345   curbyte += aimutil_put16(np->data+curbyte, 0x0000);
346   curbyte += aimutil_put16(np->data+curbyte, iconsum);
347   curbyte += aimutil_put32(np->data+curbyte, iconlen);
348   curbyte += aimutil_put32(np->data+curbyte, stamp);
349   memcpy(np->data+curbyte, icon, iconlen);
350   curbyte += iconlen;
351   memcpy(np->data+curbyte, AIM_ICONIDENT, strlen(AIM_ICONIDENT));
352   curbyte += strlen(AIM_ICONIDENT);
353
354   /* TLV t(0003) */
355   curbyte += aimutil_put16(np->data+curbyte, 0x0003);
356   curbyte += aimutil_put16(np->data+curbyte, 0x0000);
357   
358   np->commandlen = curbyte;
359   np->lock = 0;
360   aim_tx_enqueue(sess, np);
361
362   return 0;
363 }
364
365 static int outgoingim(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
366 {
367   unsigned int i, ret = 0;
368   aim_rxcallback_t userfunc;
369   unsigned char cookie[8];
370   int channel;
371   struct aim_tlvlist_t *tlvlist;
372   char sn[MAXSNLEN];
373   unsigned short icbmflags = 0;
374   unsigned char flag1 = 0, flag2 = 0;
375   unsigned char *msgblock = NULL, *msg = NULL;
376
377   /* ICBM Cookie. */
378   for (i = 0; i < 8; i++)
379     cookie[i] = aimutil_get8(data+i);
380
381   /* Channel ID */
382   channel = aimutil_get16(data+i);
383   i += 2;
384
385   if (channel != 0x01) {
386     faimdprintf(sess, 0, "icbm: ICBM recieved on unsupported channel.  Ignoring. (chan = %04x)\n", channel);
387     return 1;
388   }
389
390   strncpy(sn, (char *) data+i+1, (int) *(data+i));
391   i += 1 + (int) *(data+i);
392
393   tlvlist = aim_readtlvchain(data+i, datalen-i);
394
395   if (aim_gettlv(tlvlist, 0x0003, 1))
396     icbmflags |= AIM_IMFLAGS_ACK;
397   if (aim_gettlv(tlvlist, 0x0004, 1))
398     icbmflags |= AIM_IMFLAGS_AWAY;
399
400   if (aim_gettlv(tlvlist, 0x0002, 1)) {
401     int j = 0;
402
403     msgblock = (unsigned char *)aim_gettlv_str(tlvlist, 0x0002, 1);
404
405     /* no, this really is correct.  I'm not high or anything either. */
406     j += 2;
407     j += 2 + aimutil_get16(msgblock+j);
408     j += 2;
409     
410     j += 2; /* final block length */
411
412     flag1 = aimutil_get16(msgblock);
413     j += 2;
414     flag2 = aimutil_get16(msgblock);
415     j += 2;
416     
417     msg = msgblock+j;
418   }
419
420   if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
421     ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2);
422   
423   if (msgblock)
424     free(msgblock);
425   aim_freetlvchain(&tlvlist);
426
427   return ret;
428 }
429
430 static int incomingim_ch1(struct aim_session_t *sess, aim_module_t *mod,  struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned short channel, struct aim_userinfo_s *userinfo, unsigned char *data, int datalen, unsigned char *cookie)
431 {
432   unsigned short type, length;
433   aim_rxcallback_t userfunc;
434   int i, ret = 0;
435   struct aim_incomingim_ch1_args args;
436
437   memset(&args, 0, sizeof(args));
438
439   /*
440    * This used to be done using tlvchains.  For performance reasons,
441    * I've changed it to process the TLVs in-place.  This avoids lots
442    * of per-IM memory allocations.
443    */
444   for (i = 0; i < datalen; ) {
445       
446     type = aimutil_get16(data+i);
447     i += 2;
448       
449     length = aimutil_get16(data+i);
450     i += 2;
451
452     if (type == 0x0002) { /* Message Block */
453       unsigned short wastebits;
454       unsigned char *msgblock;
455       int j = 0, y = 0, z = 0;
456
457       msgblock = data+i;
458       
459       /*
460        * Extracting the message from the unknown cruft.
461        * 
462        * This is a bit messy, and I'm not really qualified,
463        * even as the author, to comment on it.  At least
464        * its not as bad as a while loop shooting into infinity.
465        *
466        * "Do you believe in magic?"
467        *
468        */
469
470       wastebits = aimutil_get8(msgblock+j++);
471       wastebits = aimutil_get8(msgblock+j++);
472       
473       y = aimutil_get16(msgblock+j);
474       j += 2;
475       for (z = 0; z < y; z++)
476         wastebits = aimutil_get8(msgblock+j++);
477       wastebits = aimutil_get8(msgblock+j++);
478       wastebits = aimutil_get8(msgblock+j++);
479
480       args.finlen = j;
481       if (args.finlen > sizeof(args.fingerprint))
482         args.finlen = sizeof(args.fingerprint);
483       memcpy(args.fingerprint, msgblock, args.finlen);
484
485       /* Message string length, including flag words. */
486       args.msglen = aimutil_get16(msgblock+j);
487       j += 2;
488
489       /* Flag words. */
490       args.flag1 = aimutil_get16(msgblock+j);
491       if (args.flag1 == 0x0002)
492         args.icbmflags |= AIM_IMFLAGS_UNICODE;
493       else if (args.flag1 == 0x0003)
494         args.icbmflags |= AIM_IMFLAGS_ISO_8859_1;
495       j += 2;
496
497       args.flag2 = aimutil_get16(msgblock+j);
498       j += 2;
499       
500       if ((args.flag1 && (args.flag1 != 0x0002) && (args.flag1 != 0x0003)) || args.flag2)
501         faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", args.flag1, args.flag2);
502
503       /* Message string. */
504       args.msglen -= 4;
505       if (args.icbmflags & AIM_IMFLAGS_UNICODE) {
506         args.msg = malloc(args.msglen+2);
507         memcpy(args.msg, msgblock+j, args.msglen);
508         args.msg[args.msglen] = '\0'; /* wide NULL */
509         args.msg[args.msglen+1] = '\0';
510       } else {
511         args.msg = malloc(args.msglen+1);
512         memcpy(args.msg, msgblock+j, args.msglen);
513         args.msg[args.msglen] = '\0';
514       }
515
516     } else if (type == 0x0003) { /* Server Ack Requested */
517
518       args.icbmflags |= AIM_IMFLAGS_ACK;
519
520     } else if (type == 0x0004) { /* Message is Auto Response */
521
522       args.icbmflags |= AIM_IMFLAGS_AWAY;
523
524     } else if ((type == 0x0008) && 
525                (length == 0x000c)) { /* I-HAVE-A-REALLY-PURTY-ICON Flag */
526
527       args.iconstamp = aimutil_get32(data+i+8);
528       args.icbmflags |= AIM_IMFLAGS_HASICON;
529
530     } else if (type == 0x0009) {
531
532       args.icbmflags |= AIM_IMFLAGS_BUDDYREQ;
533
534     } else {
535       fprintf(stderr, "incomingim_ch1: unknown TLV 0x%04x (len %d)\n", type, length);
536     }
537
538     i += length;
539   }
540
541
542   if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
543     ret = userfunc(sess, rx, channel, userinfo, &args);
544
545   free(args.msg);
546
547   return ret;
548 }
549
550 static int incomingim_ch2(struct aim_session_t *sess, aim_module_t *mod,  struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned short channel, struct aim_userinfo_s *userinfo, struct aim_tlvlist_t *tlvlist, unsigned char *cookie)
551 {
552   aim_rxcallback_t userfunc;
553   struct aim_tlv_t *block1;
554   struct aim_tlvlist_t *list2;
555   int ret = 0;
556   struct aim_incomingim_ch2_args args;
557
558   memset(&args, 0, sizeof(args));
559       
560   /*
561    * There's another block of TLVs embedded in the type 5 here. 
562    */
563   block1 = aim_gettlv(tlvlist, 0x0005, 1);
564   if (!block1 || !block1->value) {
565     faimdprintf(sess, 0, "no tlv 0x0005 in rendezvous transaction!\n");
566     return 0;
567   }
568
569   /*
570    * First two bytes represent the status of the connection.
571    *
572    * 0 is a request, 2 is an accept
573    */ 
574   args.status = aimutil_get16(block1->value+0);
575       
576   /*
577    * Next comes the cookie.  Should match the ICBM cookie.
578    */
579   if (memcmp(block1->value+2, cookie, 8) != 0) 
580     faimdprintf(sess, 0, "rend: warning cookies don't match!\n");
581
582   /*
583    * The next 16bytes are a capability block so we can
584    * identify what type of rendezvous this is.
585    *
586    * Thanks to Eric Warmenhoven <warmenhoven@linux.com> (of GAIM)
587    * for pointing some of this out to me.  In fact, a lot of 
588    * the client-to-client info comes from the work of the GAIM 
589    * developers. Thanks!
590    *
591    * Read off one capability string and we should have it ID'd.
592    * 
593    */
594   if ((args.reqclass = aim_getcap(sess, block1->value+2+8, 0x10)) == 0x0000) {
595     faimdprintf(sess, 0, "rend: no ID block\n");
596     return 0;
597   }
598
599   /* 
600    * What follows may be TLVs or nothing, depending on the
601    * purpose of the message.
602    *
603    * Ack packets for instance have nothing more to them.
604    */
605   list2 = aim_readtlvchain(block1->value+2+8+16, block1->length-2-8-16);
606       
607   if (!list2 || ((args.reqclass != AIM_CAPS_IMIMAGE) && !(aim_gettlv(list2, 0x2711, 1)))) {
608     struct aim_msgcookie_t *cook;
609     int type;
610         
611     type = aim_msgcookie_gettype(args.reqclass); /* XXX: fix this shitty code */
612
613     if ((cook = aim_checkcookie(sess, cookie, type)) == NULL) {
614       faimdprintf(sess, 0, "non-data rendezvous thats not in cache %d/%s!\n", type, cookie);
615       aim_freetlvchain(&list2);
616       return 0;
617     }
618
619     if (cook->type == AIM_COOKIETYPE_OFTGET) {
620       struct aim_filetransfer_priv *ft;
621
622       if (cook->data) {
623         int errorcode = -1; /* XXX shouldnt this be 0? */
624
625         ft = (struct aim_filetransfer_priv *)cook->data;
626
627         if (args.status != 0x0002) {
628
629           if (aim_gettlv(list2, 0x000b, 1))
630             errorcode = aim_gettlv16(list2, 0x000b, 1);
631
632           /* XXX this should make it up to the client, you know.. */
633           if (errorcode)
634             faimdprintf(sess, 0, "transfer from %s (%s) for %s cancelled (error code %d)\n", ft->sn, ft->ip, ft->fh.name, errorcode);
635         }
636       } else {
637         faimdprintf(sess, 0, "no data attached to file transfer\n");
638       }
639     } else if (cook->type == AIM_CAPS_VOICE) {
640       faimdprintf(sess, 0, "voice request cancelled\n");
641     } else {
642       faimdprintf(sess, 0, "unknown cookie cache type %d\n", cook->type);
643     }
644         
645     aim_freetlvchain(&list2);
646
647     return 1;
648   }
649
650   /*
651    * The rest of the handling depends on what type it is.
652    */
653   if (args.reqclass & AIM_CAPS_BUDDYICON) {
654     struct aim_tlv_t *miscinfo;
655     int curpos = 0;
656
657     miscinfo = aim_gettlv(list2, 0x2711, 1);
658
659     /* aimutil_get32(miscinfo->value+curpos); i don't know what this is */
660     curpos += 4;
661     args.info.icon.length = aimutil_get32(miscinfo->value+curpos);
662     curpos += 4;
663     args.info.icon.timestamp = aimutil_get32(miscinfo->value+curpos);
664     curpos += 4;
665     args.info.icon.icon = malloc(args.info.icon.length);
666     memcpy(args.info.icon.icon, miscinfo->value+curpos, args.info.icon.length);
667
668     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
669       ret = userfunc(sess, rx, channel, userinfo, &args);
670
671     free(args.info.icon.icon);
672
673   } else if (args.reqclass & AIM_CAPS_VOICE) {
674     struct aim_msgcookie_t *cachedcook;
675
676     faimdprintf(sess, 0, "rend: voice!\n");
677
678     if(!(cachedcook = (struct aim_msgcookie_t*)calloc(1, sizeof(struct aim_msgcookie_t)))) {
679       aim_freetlvchain(&list2);
680       return 0;
681     }
682
683     memcpy(cachedcook->cookie, cookie, 8);
684     cachedcook->type = AIM_COOKIETYPE_OFTVOICE;
685     cachedcook->data = NULL;
686
687     if (aim_cachecookie(sess, cachedcook) == -1)
688       faimdprintf(sess, 0, "ERROR caching message cookie\n");
689
690     /* XXX: implement all this */
691
692     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) 
693       ret = userfunc(sess, rx, channel, userinfo, &args);
694
695   } else if (args.reqclass & AIM_CAPS_IMIMAGE) {
696     char ip[30];
697     struct aim_directim_priv *priv;
698
699     memset(ip, 0, sizeof(ip));
700         
701     if (aim_gettlv(list2, 0x0003, 1) && aim_gettlv(list2, 0x0005, 1)) {
702       struct aim_tlv_t *iptlv, *porttlv;
703           
704       iptlv = aim_gettlv(list2, 0x0003, 1);
705       porttlv = aim_gettlv(list2, 0x0005, 1);
706
707       snprintf(ip, 30, "%d.%d.%d.%d:%d", 
708                aimutil_get8(iptlv->value+0),
709                aimutil_get8(iptlv->value+1),
710                aimutil_get8(iptlv->value+2),
711                aimutil_get8(iptlv->value+3),
712                4443 /*aimutil_get16(porttlv->value)*/);
713     }
714
715     faimdprintf(sess, 0, "rend: directIM request from %s (%s)\n",
716                 userinfo->sn, ip);
717
718     /* 
719      * XXX: there are a couple of different request packets for
720      *          different things 
721      */
722
723     args.info.directim = priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
724     memcpy(priv->ip, ip, sizeof(priv->ip));
725     memcpy(priv->sn, userinfo->sn, sizeof(priv->sn));
726     memcpy(priv->cookie, cookie, sizeof(priv->cookie));
727
728     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
729       ret = userfunc(sess, rx, channel, userinfo, &args);
730
731   } else if (args.reqclass & AIM_CAPS_CHAT) {
732     struct aim_tlv_t *miscinfo;
733
734     miscinfo = aim_gettlv(list2, 0x2711, 1);
735     aim_chat_readroominfo(miscinfo->value, &args.info.chat.roominfo);
736                   
737     if (aim_gettlv(list2, 0x000c, 1))
738       args.info.chat.msg = aim_gettlv_str(list2, 0x000c, 1);
739           
740     if (aim_gettlv(list2, 0x000d, 1))
741       args.info.chat.encoding = aim_gettlv_str(list2, 0x000d, 1);
742           
743     if (aim_gettlv(list2, 0x000e, 1))
744       args.info.chat.lang = aim_gettlv_str(list2, 0x000e, 1);
745       
746     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
747       ret = userfunc(sess, rx, channel, userinfo, &args);
748
749     free(args.info.chat.roominfo.name);
750     free(args.info.chat.msg);
751     free(args.info.chat.encoding);
752     free(args.info.chat.lang);
753
754   } else if (args.reqclass & AIM_CAPS_GETFILE) {
755     char ip[30];
756     struct aim_msgcookie_t *cachedcook;
757     struct aim_tlv_t *miscinfo;
758     struct aim_tlv_t *iptlv, *porttlv;
759
760     memset(ip, 0, 30);
761
762     if (!(cachedcook = calloc(1, sizeof(struct aim_msgcookie_t)))) {
763       aim_freetlvchain(&list2);
764       return 0;
765     }
766
767     if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) || 
768         !(iptlv = aim_gettlv(list2, 0x0003, 1)) || 
769         !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
770       faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
771       aim_cookie_free(sess, cachedcook);
772       aim_freetlvchain(&list2);
773       return 0;
774     }
775
776     snprintf(ip, 30, "%d.%d.%d.%d:%d",
777              aimutil_get8(iptlv->value+0),
778              aimutil_get8(iptlv->value+1),
779              aimutil_get8(iptlv->value+2),
780              aimutil_get8(iptlv->value+3),
781              aimutil_get16(porttlv->value));
782
783     faimdprintf(sess, 0, "rend: file get request from %s (%s)\n", userinfo->sn, ip);
784
785     args.info.getfile.ip = ip;
786     args.info.getfile.cookie = cookie;
787
788     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
789       ret = userfunc(sess, rx, channel, userinfo, &args);
790
791   } else if (args.reqclass & AIM_CAPS_SENDFILE) {
792 #if 0
793     char ip[30];
794     struct aim_msgcookie_t *cachedcook;
795     struct aim_tlv_t *miscinfo;
796     struct aim_tlv_t *iptlv, *porttlv;
797
798     memset(ip, 0, 30);
799
800     if (!(cachedcook = calloc(1, sizeof(struct aim_msgcookie_t)))) {
801       aim_freetlvchain(&list2);
802       return 0;
803     }
804
805     if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) || 
806         !(iptlv = aim_gettlv(list2, 0x0003, 1)) || 
807         !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
808       faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
809       aim_cookie_free(sess, cachedcook);
810       aim_freetlvchain(&list2);
811       return 0;
812     }
813
814     snprintf(ip, 30, "%d.%d.%d.%d:%d",
815              aimutil_get8(iptlv->value+0),
816              aimutil_get8(iptlv->value+1),
817              aimutil_get8(iptlv->value+2),
818              aimutil_get8(iptlv->value+3),
819              aimutil_get16(porttlv->value));
820
821     if (aim_gettlv(list2, 0x000c, 1))
822       desc = aim_gettlv_str(list2, 0x000c, 1);
823
824     faimdprintf(sess, 0, "rend: file transfer request from %s for %s: %s (%s)\n",
825                 userinfo->sn, miscinfo->value+8,
826                 desc, ip);
827         
828     memcpy(cachedcook->cookie, cookie, 8);
829         
830     ft = malloc(sizeof(struct aim_filetransfer_priv));
831     strncpy(ft->sn, userinfo.sn, sizeof(ft->sn));
832     strncpy(ft->ip, ip, sizeof(ft->ip));
833     strncpy(ft->fh.name, miscinfo->value+8, sizeof(ft->fh.name));
834     cachedcook->type = AIM_COOKIETYPE_OFTSEND;
835     cachedcook->data = ft;
836
837     if (aim_cachecookie(sess, cachedcook) == -1)
838       faimdprintf(sess, 0, "ERROR caching message cookie\n");
839
840     aim_accepttransfer(sess, rx->conn, ft->sn, cookie, AIM_CAPS_SENDFILE);
841         
842     if (desc)
843       free(desc);
844
845     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
846       ret = userfunc(sess, rx, channel, userinfo, &args);
847
848 #endif  
849   } else
850     faimdprintf(sess, 0, "rend: unknown rendezvous 0x%04x\n", args.reqclass);
851
852   aim_freetlvchain(&list2);
853
854   return ret;
855 }
856
857 /*
858  * It can easily be said that parsing ICBMs is THE single
859  * most difficult thing to do in the in AIM protocol.  In
860  * fact, I think I just did say that.
861  *
862  * Below is the best damned solution I've come up with
863  * over the past sixteen months of battling with it. This
864  * can parse both away and normal messages from every client
865  * I have access to.  Its not fast, its not clean.  But it works.
866  *
867  */
868 static int incomingim(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
869 {
870   int i, ret = 0;
871   unsigned char cookie[8];
872   int channel;
873   struct aim_userinfo_s userinfo;
874
875   memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
876  
877   /*
878    * Read ICBM Cookie.  And throw away.
879    */
880   for (i = 0; i < 8; i++)
881     cookie[i] = aimutil_get8(data+i);
882   
883   /*
884    * Channel ID.
885    *
886    * Channel 0x0001 is the message channel.  There are 
887    * other channels for things called "rendevous"
888    * which represent chat and some of the other new
889    * features of AIM2/3/3.5. 
890    *
891    * Channel 0x0002 is the Rendevous channel, which
892    * is where Chat Invitiations and various client-client
893    * connection negotiations come from.
894    * 
895    */
896   channel = aimutil_get16(data+i);
897   i += 2;
898   
899   /*
900    *
901    */
902   if ((channel != 0x01) && (channel != 0x02)) {
903     faimdprintf(sess, 0, "icbm: ICBM received on an unsupported channel.  Ignoring.\n (chan = %04x)", channel);
904     return 1;
905   }
906
907   /*
908    * Extract the standard user info block.
909    *
910    * Note that although this contains TLVs that appear contiguous
911    * with the TLVs read below, they are two different pieces.  The
912    * userinfo block contains the number of TLVs that contain user
913    * information, the rest are not even though there is no seperation.
914    * aim_extractuserinfo() returns the number of bytes used by the
915    * userinfo tlvs, so you can start reading the rest of them right
916    * afterward.  
917    *
918    * That also means that TLV types can be duplicated between the
919    * userinfo block and the rest of the message, however there should
920    * never be two TLVs of the same type in one block.
921    * 
922    */
923   i += aim_extractuserinfo(sess, data+i, &userinfo);
924   
925   /*
926    * From here on, its depends on what channel we're on.
927    *
928    * Technically all channels have a TLV list have this, however,
929    * for the common channel 1 case, in-place parsing is used for
930    * performance reasons (less memory allocation).
931    */
932   if (channel == 1) {
933
934     ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, data+i, datalen-i, cookie);
935
936   } else if (channel == 0x0002) {
937     struct aim_tlvlist_t *tlvlist;
938
939     /*
940      * Read block of TLVs (not including the userinfo data).  All 
941      * further data is derived from what is parsed here.
942      */
943     tlvlist = aim_readtlvchain(data+i, datalen-i);
944
945     ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
946
947     /*
948      * Free up the TLV chain.
949      */
950     aim_freetlvchain(&tlvlist);
951   }
952
953   return ret;
954 }
955
956 /*
957  * Possible codes:
958  *    AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
959  *    AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
960  *    AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
961  * 
962  */
963 faim_export unsigned long aim_denytransfer(struct aim_session_t *sess,
964                                            struct aim_conn_t *conn, 
965                                            char *sender,
966                                            char *cookie, 
967                                            unsigned short code)
968 {
969   struct command_tx_struct *newpacket;
970   int curbyte, i;
971
972   if(!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(sender)+6)))
973     return -1;
974
975   newpacket->lock = 1;
976
977   curbyte = aim_putsnac(newpacket->data, 0x0004, 0x000b, 0x0000, sess->snac_nextid);
978   for (i = 0; i < 8; i++)
979     curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
980   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
981   curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sender));
982   curbyte += aimutil_putstr(newpacket->data+curbyte, sender, strlen(sender));
983   curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0003, code);
984
985   newpacket->lock = 0;
986   aim_tx_enqueue(sess, newpacket);
987
988   return (sess->snac_nextid++);
989 }
990
991 /*
992  * Not real sure what this does, nor does anyone I've talk to.
993  *
994  * Didn't use to send it.  But now I think it might be a good
995  * idea. 
996  *
997  */
998 faim_export unsigned long aim_seticbmparam(struct aim_session_t *sess,
999                                            struct aim_conn_t *conn)
1000 {
1001   struct command_tx_struct *newpacket;
1002   int curbyte;
1003
1004   if(!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+16)))
1005     return -1;
1006
1007   newpacket->lock = 1;
1008
1009   curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0002, 0x0000, sess->snac_nextid);
1010   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1011   curbyte += aimutil_put32(newpacket->data+curbyte, 0x00000003);
1012   curbyte += aimutil_put16(newpacket->data+curbyte,  0x1f40);
1013   curbyte += aimutil_put16(newpacket->data+curbyte,  0x03e7);
1014   curbyte += aimutil_put16(newpacket->data+curbyte,  0x03e7);
1015   curbyte += aimutil_put32(newpacket->data+curbyte, 0x00000000);
1016
1017   newpacket->lock = 0;
1018   aim_tx_enqueue(sess, newpacket);
1019
1020   return (sess->snac_nextid++);
1021 }
1022
1023 static int paraminfo(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
1024 {
1025   unsigned long defflags, minmsginterval;
1026   unsigned short maxicbmlen, maxsenderwarn, maxrecverwarn, maxchannel;
1027   aim_rxcallback_t userfunc;
1028   int i = 0;
1029
1030   maxchannel = aimutil_get16(data+i);
1031   i += 2;
1032
1033   defflags = aimutil_get32(data+i);
1034   i += 4;
1035
1036   maxicbmlen = aimutil_get16(data+i);
1037   i += 2;
1038
1039   maxsenderwarn = aimutil_get16(data+i);
1040   i += 2;
1041
1042   maxrecverwarn = aimutil_get16(data+i);
1043   i += 2;
1044
1045   minmsginterval = aimutil_get32(data+i);
1046   i += 4;
1047
1048   if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1049     return userfunc(sess, rx, maxchannel, defflags, maxicbmlen, maxsenderwarn, maxrecverwarn, minmsginterval);
1050
1051   return 0;
1052 }
1053
1054 static int missedcall(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
1055 {
1056   int i = 0;
1057   aim_rxcallback_t userfunc;
1058   unsigned short channel, nummissed, reason;
1059   struct aim_userinfo_s userinfo;
1060  
1061   /*
1062    * XXX: supposedly, this entire packet can repeat as many times
1063    * as necessary. Should implement that.
1064    */
1065
1066   /*
1067    * Channel ID.
1068    */
1069   channel = aimutil_get16(data+i);
1070   i += 2;
1071   
1072   /*
1073    * Extract the standard user info block.
1074    */
1075   i += aim_extractuserinfo(sess, data+i, &userinfo);
1076   
1077   nummissed = aimutil_get16(data+i);
1078   i += 2;
1079   
1080   reason = aimutil_get16(data+i);
1081   i += 2;
1082
1083   if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1084     return userfunc(sess, rx, channel, &userinfo, nummissed, reason);
1085   
1086   return 0;
1087 }
1088
1089 static int msgack(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
1090 {
1091   aim_rxcallback_t userfunc;
1092   char sn[MAXSNLEN];
1093   unsigned char ck[8];
1094   unsigned short type;
1095   int i = 0;
1096   unsigned char snlen;
1097
1098   memcpy(ck, data, 8);
1099   i += 8;
1100
1101   type = aimutil_get16(data+i);
1102   i += 2;
1103
1104   snlen = aimutil_get8(data+i);
1105   i++;
1106
1107   memset(sn, 0, sizeof(sn));
1108   strncpy(sn, (char *)data+i, snlen);
1109
1110   if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
1111     return userfunc(sess, rx, type, sn);
1112
1113   return 0;
1114 }
1115
1116 static int snachandler(struct aim_session_t *sess, aim_module_t *mod, struct command_rx_struct *rx, aim_modsnac_t *snac, unsigned char *data, int datalen)
1117 {
1118
1119   if (snac->subtype == 0x0005)
1120     return paraminfo(sess, mod, rx, snac, data, datalen);
1121   else if (snac->subtype == 0x0006)
1122     return outgoingim(sess, mod, rx, snac, data, datalen);
1123   else if (snac->subtype == 0x0007)
1124     return incomingim(sess, mod, rx, snac, data, datalen);
1125   else if (snac->subtype == 0x000a)
1126     return missedcall(sess, mod, rx, snac, data, datalen);
1127   else if (snac->subtype == 0x000c)
1128     return msgack(sess, mod, rx, snac, data, datalen);
1129
1130   return 0;
1131 }
1132
1133 faim_internal int msg_modfirst(struct aim_session_t *sess, aim_module_t *mod)
1134 {
1135
1136   mod->family = 0x0004;
1137   mod->version = 0x0000;
1138   mod->flags = 0;
1139   strncpy(mod->name, "messaging", sizeof(mod->name));
1140   mod->snachandler = snachandler;
1141
1142   return 0;
1143 }
This page took 0.1245 seconds and 3 git commands to generate.