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