]> andersk Git - libfaim.git/blob - src/im.c
0f2fbe7443887ea7ec06e4ff0410a370042e0ed5
[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 /*
69  * Send an ICBM (instant message).  
70  *
71  *
72  * Possible flags:
73  *   AIM_IMFLAGS_AWAY  -- Marks the message as an autoresponse
74  *   AIM_IMFLAGS_ACK   -- Requests that the server send an ack
75  *                        when the message is received (of type 0x0004/0x000c)
76  *
77  */
78 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, int msglen)
79 {
80   int curbyte,i;
81   struct command_tx_struct *newpacket;
82
83   if (!msg || (msglen <= 0))
84     return -1;
85
86   if (msglen >= MAXMSGLEN)
87     return -1;
88
89   if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, msglen+256)))
90     return -1;
91
92   newpacket->lock = 1; /* lock struct */
93
94   curbyte  = 0;
95   curbyte += aim_putsnac(newpacket->data+curbyte, 
96                          0x0004, 0x0006, 0x0000, sess->snac_nextid);
97
98   /* 
99    * Generate a random message cookie 
100    *
101    * We could cache these like we do SNAC IDs.  (In fact, it 
102    * might be a good idea.)  In the message error functions, 
103    * the 8byte message cookie is returned as well as the 
104    * SNAC ID.
105    *
106    */
107   for (i=0;i<8;i++)
108     curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) rand());
109
110   /*
111    * Channel ID
112    */
113   curbyte += aimutil_put16(newpacket->data+curbyte,0x0001);
114
115   /* 
116    * Destination SN (prepended with byte length)
117    */
118   curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
119   curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
120
121   /*
122    * metaTLV start.
123    */
124   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
125   curbyte += aimutil_put16(newpacket->data+curbyte, msglen + 0x10);
126
127   /*
128    * Flag data / ICBM Parameters?
129    *
130    * I don't know what these are...
131    *
132    */
133   curbyte += aimutil_put8(newpacket->data+curbyte, 0x05);
134   curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
135
136   /* number of bytes to follow */
137   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
138   curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
139   curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
140   curbyte += aimutil_put8(newpacket->data+curbyte, 0x01);
141   curbyte += aimutil_put8(newpacket->data+curbyte, 0x02);
142
143   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0101);
144
145   /* 
146    * Message block length.
147    */
148   curbyte += aimutil_put16(newpacket->data+curbyte, msglen + 0x04);
149
150   /*
151    * Character set data? 
152    */
153   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
154   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
155
156   /*
157    * Message.  Not terminated.
158    */
159   curbyte += aimutil_putstr(newpacket->data+curbyte,msg, msglen);
160
161   /*
162    * Set the Request Acknowledge flag.  
163    */
164   if (flags & AIM_IMFLAGS_ACK) {
165     curbyte += aimutil_put16(newpacket->data+curbyte,0x0003);
166     curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
167   }
168   
169   /*
170    * Set the Autoresponse flag.
171    */
172   if (flags & AIM_IMFLAGS_AWAY) {
173     curbyte += aimutil_put16(newpacket->data+curbyte,0x0004);
174     curbyte += aimutil_put16(newpacket->data+curbyte,0x0000);
175   }
176   
177   newpacket->commandlen = curbyte;
178   newpacket->lock = 0;
179
180   aim_tx_enqueue(sess, newpacket);
181
182   aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, destsn, strlen(destsn)+1);
183   aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
184
185   return sess->snac_nextid;
186 }
187
188 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)
189 {
190   unsigned int i, ret = 0;
191   aim_rxcallback_t userfunc;
192   unsigned char cookie[8];
193   int channel;
194   struct aim_tlvlist_t *tlvlist;
195   char sn[MAXSNLEN];
196   unsigned short icbmflags = 0;
197   unsigned char flag1 = 0, flag2 = 0;
198   unsigned char *msgblock = NULL, *msg = NULL;
199
200   /* ICBM Cookie. */
201   for (i = 0; i < 8; i++)
202     cookie[i] = aimutil_get8(data+i);
203
204   /* Channel ID */
205   channel = aimutil_get16(data+i);
206   i += 2;
207
208   if (channel != 0x01) {
209     faimdprintf(sess, 0, "icbm: ICBM recieved on unsupported channel.  Ignoring. (chan = %04x)\n", channel);
210     return 1;
211   }
212
213   strncpy(sn, (char *) data+i+1, (int) *(data+i));
214   i += 1 + (int) *(data+i);
215
216   tlvlist = aim_readtlvchain(data+i, datalen-i);
217
218   if (aim_gettlv(tlvlist, 0x0003, 1))
219     icbmflags |= AIM_IMFLAGS_ACK;
220   if (aim_gettlv(tlvlist, 0x0004, 1))
221     icbmflags |= AIM_IMFLAGS_AWAY;
222
223   if (aim_gettlv(tlvlist, 0x0002, 1)) {
224     int j = 0;
225
226     msgblock = (unsigned char *)aim_gettlv_str(tlvlist, 0x0002, 1);
227
228     /* no, this really is correct.  I'm not high or anything either. */
229     j += 2;
230     j += 2 + aimutil_get16(msgblock+j);
231     j += 2;
232     
233     j += 2; /* final block length */
234
235     flag1 = aimutil_get16(msgblock);
236     j += 2;
237     flag2 = aimutil_get16(msgblock);
238     j += 2;
239     
240     msg = msgblock+j;
241   }
242
243   if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
244     ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2);
245   
246   if (msgblock)
247     free(msgblock);
248   aim_freetlvchain(&tlvlist);
249
250   return ret;
251 }
252
253 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, struct aim_tlvlist_t *tlvlist, unsigned char *cookie)
254 {
255   aim_rxcallback_t userfunc;
256   int i, j = 0, y = 0, z = 0, ret = 0;
257   char *msg = NULL;
258   unsigned long icbmflags = 0;
259   struct aim_tlv_t *msgblocktlv;
260   unsigned char *msgblock;
261   unsigned short flag1, flag2;
262   int finlen = 0;
263   unsigned char fingerprint[10];
264   unsigned short wastebits;
265            
266   /*
267    * Check Autoresponse status.  If it is an autoresponse,
268    * it will contain a type 0x0004 TLV, with zero length.
269    */
270   if (aim_gettlv(tlvlist, 0x0004, 1))
271     icbmflags |= AIM_IMFLAGS_AWAY;
272       
273   /*
274    * Check Ack Request status.
275    */
276   if (aim_gettlv(tlvlist, 0x0003, 1))
277     icbmflags |= AIM_IMFLAGS_ACK;
278       
279   /*
280    * Message block.
281    */
282   msgblocktlv = aim_gettlv(tlvlist, 0x0002, 1);
283   if (!msgblocktlv || !(msgblock = msgblocktlv->value)) {
284     faimdprintf(sess, 0, "icbm: major error! no message block TLV found!\n");
285     return 0;
286   }
287       
288   /*
289    * Extracting the message from the unknown cruft.
290    * 
291    * This is a bit messy, and I'm not really qualified,
292    * even as the author, to comment on it.  At least
293    * its not as bad as a while loop shooting into infinity.
294    *
295    * "Do you believe in magic?"
296    *
297    */
298
299   wastebits = aimutil_get8(msgblock+j++);
300   wastebits = aimutil_get8(msgblock+j++);
301       
302   y = aimutil_get16(msgblock+j);
303   j += 2;
304   for (z = 0; z < y; z++)
305     wastebits = aimutil_get8(msgblock+j++);
306   wastebits = aimutil_get8(msgblock+j++);
307   wastebits = aimutil_get8(msgblock+j++);
308
309   finlen = j;
310   if (finlen > sizeof(fingerprint))
311     finlen = sizeof(fingerprint);
312   memcpy(fingerprint, msgblocktlv->value, finlen);
313
314   /* 
315    * Message string length, including flag words.
316    */
317   i = aimutil_get16(msgblock+j);
318   j += 2;
319
320   /*
321    * Flag words.
322    *
323    * Its rumored that these can kick in some funky
324    * 16bit-wide char stuff that used to really kill
325    * libfaim.  Hopefully the latter is no longer true.
326    *
327    * Though someone should investiagte the former.
328    *
329    */
330   flag1 = aimutil_get16(msgblock+j);
331   j += 2;
332   flag2 = aimutil_get16(msgblock+j);
333   j += 2;
334       
335   if (flag1 || flag2)
336     faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", flag1, flag2);
337
338   /* 
339    * Message string. 
340    */
341   i -= 4;
342   msg = (char *)malloc(i+1);
343   memcpy(msg, msgblock+j, i);
344   msg[i] = '\0';
345
346   if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
347     ret = userfunc(sess, rx, channel, userinfo, msg, icbmflags, flag1, flag2, finlen, fingerprint);
348
349   free(msg);
350
351   return ret;
352 }
353
354 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)
355 {
356   aim_rxcallback_t userfunc;
357   struct aim_tlv_t *block1;
358   struct aim_tlvlist_t *list2;
359   unsigned short reqclass = 0;
360   unsigned short status = 0;
361   int ret = 0;
362       
363   /*
364    * There's another block of TLVs embedded in the type 5 here. 
365    */
366   block1 = aim_gettlv(tlvlist, 0x0005, 1);
367   if (!block1 || !block1->value) {
368     faimdprintf(sess, 0, "no tlv 0x0005 in rendezvous transaction!\n");
369     return 0;
370   }
371
372   /*
373    * First two bytes represent the status of the connection.
374    *
375    * 0 is a request, 2 is an accept
376    */ 
377   status = aimutil_get16(block1->value+0);
378       
379   /*
380    * Next comes the cookie.  Should match the ICBM cookie.
381    */
382   if (memcmp(block1->value+2, cookie, 8) != 0) 
383     faimdprintf(sess, 0, "rend: warning cookies don't match!\n");
384
385   /*
386    * The next 16bytes are a capability block so we can
387    * identify what type of rendezvous this is.
388    *
389    * Thanks to Eric Warmenhoven <warmenhoven@linux.com> (of GAIM)
390    * for pointing some of this out to me.  In fact, a lot of 
391    * the client-to-client info comes from the work of the GAIM 
392    * developers. Thanks!
393    *
394    * Read off one capability string and we should have it ID'd.
395    * 
396    */
397   reqclass = aim_getcap(sess, block1->value+2+8, 0x10);
398   if (reqclass == 0x0000) {
399     faimdprintf(sess, 0, "rend: no ID block\n");
400     return 0;
401   }
402
403   /* 
404    * What follows may be TLVs or nothing, depending on the
405    * purpose of the message.
406    *
407    * Ack packets for instance have nothing more to them.
408    */
409   list2 = aim_readtlvchain(block1->value+2+8+16, block1->length-2-8-16);
410       
411   if (!list2 || ((reqclass != AIM_CAPS_IMIMAGE) && !(aim_gettlv(list2, 0x2711, 1)))) {
412     struct aim_msgcookie_t *cook;
413     int type;
414         
415     type = aim_msgcookie_gettype(reqclass); /* XXX: fix this shitty code */
416
417     if ((cook = aim_checkcookie(sess, cookie, type)) == NULL) {
418       faimdprintf(sess, 0, "non-data rendezvous thats not in cache %d/%s!\n", type, cookie);
419       aim_freetlvchain(&list2);
420       return 0;
421     }
422
423     if (cook->type == AIM_COOKIETYPE_OFTGET) {
424       struct aim_filetransfer_priv *ft;
425
426       if (cook->data) {
427         int errorcode = -1; /* XXX shouldnt this be 0? */
428
429         ft = (struct aim_filetransfer_priv *)cook->data;
430
431         if(status != 0x0002) {
432           if (aim_gettlv(list2, 0x000b, 1))
433             errorcode = aim_gettlv16(list2, 0x000b, 1);
434
435           /* XXX this should make it up to the client, you know.. */
436           if (errorcode)
437             faimdprintf(sess, 0, "transfer from %s (%s) for %s cancelled (error code %d)\n", ft->sn, ft->ip, ft->fh.name, errorcode);
438         }
439       } else {
440         faimdprintf(sess, 0, "no data attached to file transfer\n");
441       }
442     } else if (cook->type == AIM_CAPS_VOICE) {
443       faimdprintf(sess, 0, "voice request cancelled\n");
444     } else {
445       faimdprintf(sess, 0, "unknown cookie cache type %d\n", cook->type);
446     }
447         
448     aim_freetlvchain(&list2);
449
450     return 1;
451   }
452
453   /*
454    * The rest of the handling depends on what type it is.
455    */
456   if (reqclass & AIM_CAPS_BUDDYICON) {
457
458     /* XXX implement this (its in ActiveBuddy...) */
459     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
460       ret = userfunc(sess, rx, channel, reqclass, userinfo);
461
462   } else if (reqclass & AIM_CAPS_VOICE) {
463     struct aim_msgcookie_t *cachedcook;
464
465     faimdprintf(sess, 0, "rend: voice!\n");
466
467     if(!(cachedcook = (struct aim_msgcookie_t*)calloc(1, sizeof(struct aim_msgcookie_t)))) {
468       aim_freetlvchain(&list2);
469       return 0;
470     }
471
472     memcpy(cachedcook->cookie, cookie, 8);
473     cachedcook->type = AIM_COOKIETYPE_OFTVOICE;
474     cachedcook->data = NULL;
475
476     if (aim_cachecookie(sess, cachedcook) == -1)
477       faimdprintf(sess, 0, "ERROR caching message cookie\n");
478
479     /* XXX: implement all this */
480
481     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) 
482       ret = userfunc(sess, rx, channel, reqclass, &userinfo);
483
484   } else if ((reqclass & AIM_CAPS_IMIMAGE) || 
485              (reqclass & AIM_CAPS_BUDDYICON)) {
486     char ip[30];
487     struct aim_directim_priv *priv;
488
489     memset(ip, 0, sizeof(ip));
490         
491     if (aim_gettlv(list2, 0x0003, 1) && aim_gettlv(list2, 0x0005, 1)) {
492       struct aim_tlv_t *iptlv, *porttlv;
493           
494       iptlv = aim_gettlv(list2, 0x0003, 1);
495       porttlv = aim_gettlv(list2, 0x0005, 1);
496
497       snprintf(ip, 30, "%d.%d.%d.%d:%d", 
498                aimutil_get8(iptlv->value+0),
499                aimutil_get8(iptlv->value+1),
500                aimutil_get8(iptlv->value+2),
501                aimutil_get8(iptlv->value+3),
502                4443 /*aimutil_get16(porttlv->value)*/);
503     }
504
505     faimdprintf(sess, 0, "rend: directIM request from %s (%s)\n",
506                 userinfo->sn, ip);
507
508     /* 
509      * XXX: there are a couple of different request packets for
510      *          different things 
511      */
512
513     priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
514     memcpy(priv->ip, ip, sizeof(priv->ip));
515     memcpy(priv->sn, userinfo->sn, sizeof(priv->sn));
516     memcpy(priv->cookie, cookie, sizeof(priv->cookie));
517
518     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
519       ret = userfunc(sess, rx, channel, reqclass, userinfo, priv);
520
521   } else if (reqclass & AIM_CAPS_CHAT) {
522     struct aim_tlv_t *miscinfo;
523     struct aim_chat_roominfo roominfo;
524     char *msg=NULL,*encoding=NULL,*lang=NULL;
525
526     miscinfo = aim_gettlv(list2, 0x2711, 1);
527     aim_chat_readroominfo(miscinfo->value, &roominfo);
528                   
529     if (aim_gettlv(list2, 0x000c, 1))
530       msg = aim_gettlv_str(list2, 0x000c, 1);
531           
532     if (aim_gettlv(list2, 0x000d, 1))
533       encoding = aim_gettlv_str(list2, 0x000d, 1);
534           
535     if (aim_gettlv(list2, 0x000e, 1))
536       lang = aim_gettlv_str(list2, 0x000e, 1);
537       
538     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
539       ret = userfunc(sess, rx, channel, reqclass, userinfo, &roominfo, msg, encoding?encoding+1:NULL, lang?lang+1:NULL);
540
541     free(roominfo.name);
542     free(msg);
543     free(encoding);
544     free(lang);
545
546   } else if (reqclass & AIM_CAPS_GETFILE) {
547     char ip[30];
548     struct aim_msgcookie_t *cachedcook;
549     struct aim_tlv_t *miscinfo;
550     struct aim_tlv_t *iptlv, *porttlv;
551
552     memset(ip, 0, 30);
553
554     if (!(cachedcook = calloc(1, sizeof(struct aim_msgcookie_t)))) {
555       aim_freetlvchain(&list2);
556       return 0;
557     }
558
559     if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) || 
560         !(iptlv = aim_gettlv(list2, 0x0003, 1)) || 
561         !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
562       faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
563       aim_cookie_free(sess, cachedcook);
564       aim_freetlvchain(&list2);
565       return 0;
566     }
567
568     snprintf(ip, 30, "%d.%d.%d.%d:%d",
569              aimutil_get8(iptlv->value+0),
570              aimutil_get8(iptlv->value+1),
571              aimutil_get8(iptlv->value+2),
572              aimutil_get8(iptlv->value+3),
573              aimutil_get16(porttlv->value));
574
575     faimdprintf(sess, 0, "rend: file get request from %s (%s)\n", userinfo->sn, ip);
576     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
577       ret = userfunc(sess, rx, channel, reqclass, userinfo, ip, cookie);
578
579   } else if (reqclass & AIM_CAPS_SENDFILE) {
580 #if 0
581     char ip[30];
582     struct aim_msgcookie_t *cachedcook;
583     struct aim_tlv_t *miscinfo;
584     struct aim_tlv_t *iptlv, *porttlv;
585
586     memset(ip, 0, 30);
587
588     if (!(cachedcook = calloc(1, sizeof(struct aim_msgcookie_t)))) {
589       aim_freetlvchain(&list2);
590       return 0;
591     }
592
593     if (!(miscinfo = aim_gettlv(list2, 0x2711, 1)) || 
594         !(iptlv = aim_gettlv(list2, 0x0003, 1)) || 
595         !(porttlv = aim_gettlv(list2, 0x0005, 1))) {
596       faimdprintf(sess, 0, "rend: badly damaged file get request from %s...\n", userinfo->sn);
597       aim_cookie_free(sess, cachedcook);
598       aim_freetlvchain(&list2);
599       return 0;
600     }
601
602     snprintf(ip, 30, "%d.%d.%d.%d:%d",
603              aimutil_get8(iptlv->value+0),
604              aimutil_get8(iptlv->value+1),
605              aimutil_get8(iptlv->value+2),
606              aimutil_get8(iptlv->value+3),
607              aimutil_get16(porttlv->value));
608
609     if (aim_gettlv(list2, 0x000c, 1))
610       desc = aim_gettlv_str(list2, 0x000c, 1);
611
612     faimdprintf(sess, 0, "rend: file transfer request from %s for %s: %s (%s)\n",
613                 userinfo->sn, miscinfo->value+8,
614                 desc, ip);
615         
616     memcpy(cachedcook->cookie, cookie, 8);
617         
618     ft = malloc(sizeof(struct aim_filetransfer_priv));
619     strncpy(ft->sn, userinfo.sn, sizeof(ft->sn));
620     strncpy(ft->ip, ip, sizeof(ft->ip));
621     strncpy(ft->fh.name, miscinfo->value+8, sizeof(ft->fh.name));
622     cachedcook->type = AIM_COOKIETYPE_OFTSEND;
623     cachedcook->data = ft;
624
625     if (aim_cachecookie(sess, cachedcook) == -1)
626       faimdprintf(sess, 0, "ERROR caching message cookie\n");
627
628     aim_accepttransfer(sess, rx->conn, ft->sn, cookie, AIM_CAPS_SENDFILE);
629         
630     if (desc)
631       free(desc);
632
633     if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
634       ret = userfunc(sess, rx, channel, reqclass, userinfo);
635
636 #endif  
637   } else
638     faimdprintf(sess, 0, "rend: unknown rendezvous 0x%04x\n", reqclass);
639
640   aim_freetlvchain(&list2);
641
642   return ret;
643 }
644
645 /*
646  * It can easily be said that parsing ICBMs is THE single
647  * most difficult thing to do in the in AIM protocol.  In
648  * fact, I think I just did say that.
649  *
650  * Below is the best damned solution I've come up with
651  * over the past sixteen months of battling with it. This
652  * can parse both away and normal messages from every client
653  * I have access to.  Its not fast, its not clean.  But it works.
654  *
655  */
656 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)
657 {
658   int i, ret = 0;
659   unsigned char cookie[8];
660   int channel;
661   struct aim_tlvlist_t *tlvlist;
662   struct aim_userinfo_s userinfo;
663
664   memset(&userinfo, 0x00, sizeof(struct aim_userinfo_s));
665  
666   /*
667    * Read ICBM Cookie.  And throw away.
668    */
669   for (i = 0; i < 8; i++)
670     cookie[i] = aimutil_get8(data+i);
671   
672   /*
673    * Channel ID.
674    *
675    * Channel 0x0001 is the message channel.  There are 
676    * other channels for things called "rendevous"
677    * which represent chat and some of the other new
678    * features of AIM2/3/3.5. 
679    *
680    * Channel 0x0002 is the Rendevous channel, which
681    * is where Chat Invitiations and various client-client
682    * connection negotiations come from.
683    * 
684    */
685   channel = aimutil_get16(data+i);
686   i += 2;
687   
688   /*
689    *
690    */
691   if ((channel != 0x01) && (channel != 0x02)) {
692     faimdprintf(sess, 0, "icbm: ICBM received on an unsupported channel.  Ignoring.\n (chan = %04x)", channel);
693     return 1;
694   }
695
696   /*
697    * Extract the standard user info block.
698    *
699    * Note that although this contains TLVs that appear contiguous
700    * with the TLVs read below, they are two different pieces.  The
701    * userinfo block contains the number of TLVs that contain user
702    * information, the rest are not even though there is no seperation.
703    * aim_extractuserinfo() returns the number of bytes used by the
704    * userinfo tlvs, so you can start reading the rest of them right
705    * afterward.  
706    *
707    * That also means that TLV types can be duplicated between the
708    * userinfo block and the rest of the message, however there should
709    * never be two TLVs of the same type in one block.
710    * 
711    */
712   i += aim_extractuserinfo(sess, data+i, &userinfo);
713   
714   /*
715    * Read block of TLVs (not including the userinfo data).  All 
716    * further data is derived from what is parsed here.
717    */
718   tlvlist = aim_readtlvchain(data+i, datalen-i);
719
720   /*
721    * From here on, its depends on what channel we're on.
722    */
723   if (channel == 1)
724     ret = incomingim_ch1(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
725   else if (channel == 0x0002)
726     ret = incomingim_ch2(sess, mod, rx, snac, channel, &userinfo, tlvlist, cookie);
727
728   /*
729    * Free up the TLV chain.
730    */
731   aim_freetlvchain(&tlvlist);
732
733   return ret;
734 }
735
736 /*
737  * Possible codes:
738  *    AIM_TRANSFER_DENY_NOTSUPPORTED -- "client does not support"
739  *    AIM_TRANSFER_DENY_DECLINE -- "client has declined transfer"
740  *    AIM_TRANSFER_DENY_NOTACCEPTING -- "client is not accepting transfers"
741  * 
742  */
743 faim_export unsigned long aim_denytransfer(struct aim_session_t *sess,
744                                            struct aim_conn_t *conn, 
745                                            char *sender,
746                                            char *cookie, 
747                                            unsigned short code)
748 {
749   struct command_tx_struct *newpacket;
750   int curbyte, i;
751
752   if(!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(sender)+6)))
753     return -1;
754
755   newpacket->lock = 1;
756
757   curbyte = aim_putsnac(newpacket->data, 0x0004, 0x000b, 0x0000, sess->snac_nextid);
758   for (i = 0; i < 8; i++)
759     curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
760   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
761   curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sender));
762   curbyte += aimutil_putstr(newpacket->data+curbyte, sender, strlen(sender));
763   curbyte += aim_puttlv_16(newpacket->data+curbyte, 0x0003, code);
764
765   newpacket->lock = 0;
766   aim_tx_enqueue(sess, newpacket);
767
768   return (sess->snac_nextid++);
769 }
770
771 /*
772  * Not real sure what this does, nor does anyone I've talk to.
773  *
774  * Didn't use to send it.  But now I think it might be a good
775  * idea. 
776  *
777  */
778 faim_export unsigned long aim_seticbmparam(struct aim_session_t *sess,
779                                            struct aim_conn_t *conn)
780 {
781   struct command_tx_struct *newpacket;
782   int curbyte;
783
784   if(!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+16)))
785     return -1;
786
787   newpacket->lock = 1;
788
789   curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0002, 0x0000, sess->snac_nextid);
790   curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
791   curbyte += aimutil_put32(newpacket->data+curbyte, 0x00000003);
792   curbyte += aimutil_put16(newpacket->data+curbyte,  0x1f40);
793   curbyte += aimutil_put16(newpacket->data+curbyte,  0x03e7);
794   curbyte += aimutil_put16(newpacket->data+curbyte,  0x03e7);
795   curbyte += aimutil_put32(newpacket->data+curbyte, 0x00000000);
796
797   newpacket->lock = 0;
798   aim_tx_enqueue(sess, newpacket);
799
800   return (sess->snac_nextid++);
801 }
802
803 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)
804 {
805   unsigned long defflags, minmsginterval;
806   unsigned short maxicbmlen, maxsenderwarn, maxrecverwarn, maxchannel;
807   aim_rxcallback_t userfunc;
808   int i = 0;
809
810   maxchannel = aimutil_get16(data+i);
811   i += 2;
812
813   defflags = aimutil_get32(data+i);
814   i += 4;
815
816   maxicbmlen = aimutil_get16(data+i);
817   i += 2;
818
819   maxsenderwarn = aimutil_get16(data+i);
820   i += 2;
821
822   maxrecverwarn = aimutil_get16(data+i);
823   i += 2;
824
825   minmsginterval = aimutil_get32(data+i);
826   i += 4;
827
828   if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
829     return userfunc(sess, rx, maxchannel, defflags, maxicbmlen, maxsenderwarn, maxrecverwarn, minmsginterval);
830
831   return 0;
832 }
833
834 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)
835 {
836   int i = 0;
837   aim_rxcallback_t userfunc;
838   unsigned short channel, nummissed, reason;
839   struct aim_userinfo_s userinfo;
840  
841   /*
842    * XXX: supposedly, this entire packet can repeat as many times
843    * as necessary. Should implement that.
844    */
845
846   /*
847    * Channel ID.
848    */
849   channel = aimutil_get16(data+i);
850   i += 2;
851   
852   /*
853    * Extract the standard user info block.
854    */
855   i += aim_extractuserinfo(sess, data+i, &userinfo);
856   
857   nummissed = aimutil_get16(data+i);
858   i += 2;
859   
860   reason = aimutil_get16(data+i);
861   i += 2;
862
863   if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
864     return userfunc(sess, rx, channel, &userinfo, nummissed, reason);
865   
866   return 0;
867 }
868
869 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)
870 {
871   aim_rxcallback_t userfunc;
872   char sn[MAXSNLEN];
873   unsigned char ck[8];
874   unsigned short type;
875   int i = 0;
876   unsigned char snlen;
877
878   memcpy(ck, data, 8);
879   i += 8;
880
881   type = aimutil_get16(data+i);
882   i += 2;
883
884   snlen = aimutil_get8(data+i);
885   i++;
886
887   memset(sn, 0, sizeof(sn));
888   strncpy(sn, (char *)data+i, snlen);
889
890   if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
891     return userfunc(sess, rx, type, sn);
892
893   return 0;
894 }
895
896 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)
897 {
898
899   if (snac->subtype == 0x0005)
900     return paraminfo(sess, mod, rx, snac, data, datalen);
901   else if (snac->subtype == 0x0006)
902     return outgoingim(sess, mod, rx, snac, data, datalen);
903   else if (snac->subtype == 0x0007)
904     return incomingim(sess, mod, rx, snac, data, datalen);
905   else if (snac->subtype == 0x000a)
906     return missedcall(sess, mod, rx, snac, data, datalen);
907   else if (snac->subtype == 0x000c)
908     return msgack(sess, mod, rx, snac, data, datalen);
909
910   return 0;
911 }
912
913 faim_internal int msg_modfirst(struct aim_session_t *sess, aim_module_t *mod)
914 {
915
916   mod->family = 0x0004;
917   mod->version = 0x0000;
918   mod->flags = 0;
919   strncpy(mod->name, "messaging", sizeof(mod->name));
920   mod->snachandler = snachandler;
921
922   return 0;
923 }
This page took 0.09652 seconds and 3 git commands to generate.