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