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