]> andersk Git - libfaim.git/blob - aim_info.c
close->closesocket for win32, and socket() stupidity in aim_ft.
[libfaim.git] / aim_info.c
1 /*
2  * aim_info.c
3  *
4  * The functions here are responsible for requesting and parsing information-
5  * gathering SNACs.  
6  *
7  */
8
9
10 #include <faim/aim.h>
11
12 struct aim_priv_inforeq {
13   char sn[MAXSNLEN];
14   unsigned short infotype;
15 };
16
17 faim_export unsigned long aim_getinfo(struct aim_session_t *sess,
18                                       struct aim_conn_t *conn, 
19                                       const char *sn,
20                                       unsigned short infotype)
21 {
22   struct command_tx_struct *newpacket;
23   int i = 0;
24
25   if (!sess || !conn || !sn)
26     return 0;
27
28   if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 12+1+strlen(sn))))
29     return -1;
30
31   newpacket->lock = 1;
32
33   i = aim_putsnac(newpacket->data, 0x0002, 0x0005, 0x0000, sess->snac_nextid);
34
35   i += aimutil_put16(newpacket->data+i, infotype);
36   i += aimutil_put8(newpacket->data+i, strlen(sn));
37   i += aimutil_putstr(newpacket->data+i, sn, strlen(sn));
38
39   newpacket->lock = 0;
40   aim_tx_enqueue(sess, newpacket);
41
42   {
43     struct aim_snac_t snac;
44     
45     snac.id = sess->snac_nextid;
46     snac.family = 0x0002;
47     snac.type = 0x0005;
48     snac.flags = 0x0000;
49
50     snac.data = malloc(sizeof(struct aim_priv_inforeq));
51     strcpy(((struct aim_priv_inforeq *)snac.data)->sn, sn);
52     ((struct aim_priv_inforeq *)snac.data)->infotype = infotype;
53
54     aim_newsnac(sess, &snac);
55   }
56
57   return (sess->snac_nextid++);
58 }
59
60 faim_internal int aim_parse_locateerr(struct aim_session_t *sess,
61                                       struct command_rx_struct *command)
62 {
63   u_long snacid = 0x000000000;
64   struct aim_snac_t *snac = NULL;
65   int ret = 0;
66   rxcallback_t userfunc = NULL;
67   char *dest;
68   unsigned short reason = 0;
69
70   /*
71    * Get SNAC from packet and look it up 
72    * the list of unrepliedto/outstanding
73    * SNACs.
74    *
75    */
76   snacid = aimutil_get32(command->data+6);
77   snac = aim_remsnac(sess, snacid);
78
79   if (!snac) {
80     printf("faim: locerr: got an locate-failed error on an unknown SNAC ID! (%08lx)\n", snacid);
81     dest = NULL;
82   } else
83     dest = snac->data;
84
85   reason = aimutil_get16(command->data+10);
86
87   /*
88    * Call client.
89    */
90   userfunc = aim_callhandler(command->conn, 0x0002, 0x0001);
91   if (userfunc)
92     ret =  userfunc(sess, command, dest, reason);
93   else
94     ret = 0;
95   
96   if (snac) {
97     free(snac->data);
98     free(snac);
99   }
100
101   return ret;
102 }
103
104 /*
105  * Capability blocks.  
106  */
107 u_char aim_caps[6][16] = {
108   
109   /* Buddy icon */
110   {0x09, 0x46, 0x13, 0x46, 0x4c, 0x7f, 0x11, 0xd1, 
111    0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00},
112   
113   /* Voice */
114   {0x09, 0x46, 0x13, 0x41, 0x4c, 0x7f, 0x11, 0xd1, 
115    0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00},
116   
117   /* IM image */
118   {0x09, 0x46, 0x13, 0x45, 0x4c, 0x7f, 0x11, 0xd1, 
119    0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00},
120   
121   /* Chat */
122   {0x74, 0x8f, 0x24, 0x20, 0x62, 0x87, 0x11, 0xd1, 
123    0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00},
124   
125   /* Get file */
126   {0x09, 0x46, 0x13, 0x48, 0x4c, 0x7f, 0x11, 0xd1,
127    0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00},
128   
129   /* Send file */
130   {0x09, 0x46, 0x13, 0x43, 0x4c, 0x7f, 0x11, 0xd1, 
131    0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}
132 };
133
134 faim_internal unsigned short aim_getcap(unsigned char *capblock, int buflen)
135 {
136   u_short ret = 0;
137   int y;
138   int offset = 0;
139
140   while (offset < buflen) {
141     for(y=0; y < (sizeof(aim_caps)/0x10); y++) {
142       if (memcmp(&aim_caps[y], capblock+offset, 0x10) == 0) {
143         switch(y) {
144         case 0: ret |= AIM_CAPS_BUDDYICON; break;
145         case 1: ret |= AIM_CAPS_VOICE; break;
146         case 2: ret |= AIM_CAPS_IMIMAGE; break;
147         case 3: ret |= AIM_CAPS_CHAT; break;
148         case 4: ret |= AIM_CAPS_GETFILE; break;
149         case 5: ret |= AIM_CAPS_SENDFILE; break;
150         default: ret |= 0xff00; break;
151         }
152       }
153     }
154     offset += 0x10;
155   } 
156   return ret;
157 }
158
159 faim_internal int aim_putcap(unsigned char *capblock, int buflen, u_short caps)
160 {
161   int offset = 0;
162
163   if (!capblock)
164     return -1;
165
166   if ((caps & AIM_CAPS_BUDDYICON) && (offset < buflen)) {
167     memcpy(capblock+offset, aim_caps[0], sizeof(aim_caps[0]));
168     offset += sizeof(aim_caps[1]);
169   }
170   if ((caps & AIM_CAPS_VOICE) && (offset < buflen)) {
171     memcpy(capblock+offset, aim_caps[1], sizeof(aim_caps[1]));
172     offset += sizeof(aim_caps[1]);
173   }
174   if ((caps & AIM_CAPS_IMIMAGE) && (offset < buflen)) {
175     memcpy(capblock+offset, aim_caps[2], sizeof(aim_caps[2]));
176     offset += sizeof(aim_caps[2]);
177   }
178   if ((caps & AIM_CAPS_CHAT) && (offset < buflen)) {
179     memcpy(capblock+offset, aim_caps[3], sizeof(aim_caps[3]));
180     offset += sizeof(aim_caps[3]);
181   }
182   if ((caps & AIM_CAPS_GETFILE) && (offset < buflen)) {
183     memcpy(capblock+offset, aim_caps[4], sizeof(aim_caps[4]));
184     offset += sizeof(aim_caps[4]);
185   }
186   if ((caps & AIM_CAPS_SENDFILE) && (offset < buflen)) {
187     memcpy(capblock+offset, aim_caps[5], sizeof(aim_caps[5]));
188     offset += sizeof(aim_caps[5]);
189   }
190
191   return offset;
192 }
193
194 /*
195  * AIM is fairly regular about providing user info.  This
196  * is a generic routine to extract it in its standard form.
197  */
198 faim_internal int aim_extractuserinfo(u_char *buf, struct aim_userinfo_s *outinfo)
199 {
200   int i = 0;
201   int tlvcnt = 0;
202   int curtlv = 0;
203   int tlv1 = 0;
204   u_short curtype;
205   int lastvalid;
206
207
208   if (!buf || !outinfo)
209     return -1;
210
211   /* Clear out old data first */
212   memset(outinfo, 0x00, sizeof(struct aim_userinfo_s));
213
214   /*
215    * Screen name.    Stored as an unterminated string prepended
216    *                 with an unsigned byte containing its length.
217    */
218   if (buf[i] < MAXSNLEN) {
219     memcpy(outinfo->sn, &(buf[i+1]), buf[i]);
220     outinfo->sn[(int)buf[i]] = '\0';
221   } else {
222     memcpy(outinfo->sn, &(buf[i+1]), MAXSNLEN-1);
223     outinfo->sn[MAXSNLEN] = '\0';
224   }
225   i = 1 + (int)buf[i];
226
227   /*
228    * Warning Level.  Stored as an unsigned short.
229    */
230   outinfo->warnlevel = aimutil_get16(&buf[i]);
231   i += 2;
232
233   /*
234    * TLV Count.      Unsigned short representing the number of 
235    *                 Type-Length-Value triples that follow.
236    */
237   tlvcnt = aimutil_get16(&buf[i]);
238   i += 2;
239
240   /* 
241    * Parse out the Type-Length-Value triples as they're found.
242    */
243   while (curtlv < tlvcnt) {
244     lastvalid = 1;
245     curtype = aimutil_get16(&buf[i]);
246     switch (curtype) {
247       /*
248        * Type = 0x0000: Invalid
249        *
250        * AOL has been trying to throw these in just to break us.
251        * They're real nice guys over there at AOL.  
252        *
253        * Just skip the two zero bytes and continue on. (This doesn't
254        * count towards tlvcnt!)
255        */
256     case 0x0000:
257       lastvalid = 0;
258       i += 2;
259       break;
260
261       /*
262        * Type = 0x0001: Member Class.   
263        * 
264        * Specified as any of the following bitwise ORed together:
265        *      0x0001  Trial (user less than 60days)
266        *      0x0002  Unknown bit 2
267        *      0x0004  AOL Main Service user
268        *      0x0008  Unknown bit 4
269        *      0x0010  Free (AIM) user 
270        *      0x0020  Away
271        *
272        * In some odd cases, we can end up with more
273        * than one of these.  We only want the first,
274        * as the others may not be something we want.
275        *
276        */
277     case 0x0001:
278       if (tlv1) /* use only the first */
279         break;
280       outinfo->class = aimutil_get16(&buf[i+4]);
281       tlv1++;
282       break;
283       
284       /*
285        * Type = 0x0002: Member-Since date. 
286        *
287        * The time/date that the user originally
288        * registered for the service, stored in 
289        * time_t format
290        */
291     case 0x0002: 
292       outinfo->membersince = aimutil_get32(&buf[i+4]);
293       break;
294       
295       /*
296        * Type = 0x0003: On-Since date.
297        *
298        * The time/date that the user started 
299        * their current session, stored in time_t
300        * format.
301        */
302     case 0x0003:
303       outinfo->onlinesince = aimutil_get32(&buf[i+4]);
304       break;
305       
306       /*
307        * Type = 0x0004: Idle time.
308        *
309        * Number of seconds since the user
310        * actively used the service.
311        */
312     case 0x0004:
313       outinfo->idletime = aimutil_get16(&buf[i+4]);
314       break;
315       
316       /*
317        * Type = 0x000d
318        *
319        * Capability information.  Not real sure of
320        * actual decoding.  See comment on aim_bos_setprofile()
321        * in aim_misc.c about the capability block, its the same.
322        *
323        */
324     case 0x000d:
325       {
326         int len;
327         len = aimutil_get16(buf+i+2);
328         if (!len)
329           break;
330         
331         outinfo->capabilities = aim_getcap(buf+i+4, len);
332       }
333       break;
334       
335       /*
336        * Type = 0x000e
337        *
338        * Unknown.  Always of zero length, and always only
339        * on AOL users.
340        *
341        * Ignore.
342        *
343        */
344     case 0x000e:
345       break;
346       
347       /*
348        * Type = 0x000f: Session Length. (AIM)
349        * Type = 0x0010: Session Length. (AOL)
350        *
351        * The duration, in seconds, of the user's
352        * current session.
353        *
354        * Which TLV type this comes in depends
355        * on the service the user is using (AIM or AOL).
356        *
357        */
358     case 0x000f:
359     case 0x0010:
360       outinfo->sessionlen = aimutil_get32(&buf[i+4]);
361       break;
362       
363       /*
364        * Reaching here indicates that either AOL has
365        * added yet another TLV for us to deal with, 
366        * or the parsing has gone Terribly Wrong.
367        *
368        * Either way, inform the owner and attempt
369        * recovery.
370        *
371        */
372     default:
373       {
374         int len,z = 0, y = 0, x = 0;
375         char tmpstr[80];
376         printf("faim: userinfo: **warning: unexpected TLV:\n");
377         printf("faim: userinfo:   sn    =%s\n", outinfo->sn);
378         printf("faim: userinfo:   curtlv=0x%04x\n", curtlv);
379         printf("faim: userinfo:   type  =0x%04x\n",aimutil_get16(&buf[i]));
380         printf("faim: userinfo:   length=0x%04x\n", len = aimutil_get16(&buf[i+2]));
381         printf("faim: userinfo:   data: \n");
382         while (z<len)
383           {
384             x = sprintf(tmpstr, "faim: userinfo:      ");
385             for (y = 0; y < 8; y++)
386               {
387                 if (z<len)
388                   {
389                     sprintf(tmpstr+x, "%02x ", buf[i+4+z]);
390                     z++;
391                     x += 3;
392                   }
393                 else
394                   break;
395               }
396             printf("%s\n", tmpstr);
397           }
398       }
399       break;
400     }  
401     /*
402      * No matter what, TLV triplets should always look like this:
403      *
404      *   u_short type;
405      *   u_short length;
406      *   u_char  data[length];
407      *
408      */
409     if (lastvalid) {
410       i += (2 + 2 + aimutil_get16(&buf[i+2]));     
411       curtlv++;
412     }
413   }
414   
415   return i;
416 }
417
418 /*
419  * Oncoming Buddy notifications contain a subset of the
420  * user information structure.  Its close enough to run
421  * through aim_extractuserinfo() however.
422  *
423  */
424 faim_internal int aim_parse_oncoming_middle(struct aim_session_t *sess,
425                                             struct command_rx_struct *command)
426 {
427   struct aim_userinfo_s userinfo;
428   u_int i = 0;
429   rxcallback_t userfunc=NULL;
430
431   i = 10;
432   i += aim_extractuserinfo(command->data+i, &userinfo);
433
434   userfunc = aim_callhandler(command->conn, AIM_CB_FAM_BUD, AIM_CB_BUD_ONCOMING);
435   if (userfunc)
436     i = userfunc(sess, command, &userinfo);
437
438   return 1;
439 }
440
441 /*
442  * Offgoing Buddy notifications contain no useful
443  * information other than the name it applies to.
444  *
445  */
446 faim_internal int aim_parse_offgoing_middle(struct aim_session_t *sess,
447                                             struct command_rx_struct *command)
448 {
449   char sn[MAXSNLEN+1];
450   u_int i = 0;
451   rxcallback_t userfunc=NULL;
452
453   strncpy(sn, (char *)command->data+11, (int)command->data[10]);
454   sn[(int)command->data[10]] = '\0';
455
456   userfunc = aim_callhandler(command->conn, AIM_CB_FAM_BUD, AIM_CB_BUD_OFFGOING);
457   if (userfunc)
458     i = userfunc(sess, command, sn);
459
460   return 1;
461 }
462
463 /*
464  * This parses the user info stuff out all nice and pretty then calls 
465  * the higher-level callback (in the user app).
466  *
467  */
468 faim_internal int aim_parse_userinfo_middle(struct aim_session_t *sess,
469                                             struct command_rx_struct *command)
470 {
471   struct aim_userinfo_s userinfo;
472   char *text_encoding = NULL;
473   char *text = NULL;
474   u_int i = 0;
475   rxcallback_t userfunc=NULL;
476   struct aim_tlvlist_t *tlvlist;
477   struct aim_snac_t *origsnac = NULL;
478   u_long snacid;
479   struct aim_priv_inforeq *inforeq;
480   
481   snacid = aimutil_get32(&command->data[6]);
482   origsnac = aim_remsnac(sess, snacid);
483
484   if (!origsnac || !origsnac->data) {
485     printf("faim: parse_userinfo_middle: major problem: no snac stored!\n");
486     return 1;
487   }
488
489   inforeq = (struct aim_priv_inforeq *)origsnac->data;
490
491   switch (inforeq->infotype) {
492   case AIM_GETINFO_GENERALINFO:
493   case AIM_GETINFO_AWAYMESSAGE:
494     i = 10;
495
496     /*
497      * extractuserinfo will give us the basic metaTLV information
498      */
499     i += aim_extractuserinfo(command->data+i, &userinfo);
500   
501     /*
502      * However, in this command, there's usually more TLVs following...
503      */ 
504     tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
505
506     /* 
507      * Depending on what informational text was requested, different
508      * TLVs will appear here.
509      *
510      * Profile will be 1 and 2, away message will be 3 and 4.
511      */
512     if (aim_gettlv(tlvlist, 0x0001, 1)) {
513       text_encoding = aim_gettlv_str(tlvlist, 0x0001, 1);
514       text = aim_gettlv_str(tlvlist, 0x0002, 1);
515     } else if (aim_gettlv(tlvlist, 0x0003, 1)) {
516       text_encoding = aim_gettlv_str(tlvlist, 0x0003, 1);
517       text = aim_gettlv_str(tlvlist, 0x0004, 1);
518     }
519
520     userfunc = aim_callhandler(command->conn, AIM_CB_FAM_LOC, AIM_CB_LOC_USERINFO);
521     if (userfunc) {
522       i = userfunc(sess,
523                    command, 
524                    &userinfo, 
525                    text_encoding, 
526                    text,
527                    inforeq->infotype); 
528     }
529   
530     free(text_encoding);
531     free(text);
532     aim_freetlvchain(&tlvlist);
533     break;
534   default:
535     printf("faim: parse_userinfo_middle: unknown infotype in request! (0x%04x)\n", inforeq->infotype);
536     break;
537   }
538
539   if (origsnac) {
540     if (origsnac->data)
541       free(origsnac->data);
542     free(origsnac);
543   }
544
545   return 1;
546 }
547
548 /*
549  * Inverse of aim_extractuserinfo()
550  */
551 faim_internal int aim_putuserinfo(u_char *buf, int buflen, struct aim_userinfo_s *info)
552 {
553   int i = 0;
554   struct aim_tlvlist_t *tlvlist = NULL;
555
556   if (!buf || !info)
557     return 0;
558
559   i += aimutil_put8(buf+i, strlen(info->sn));
560   i += aimutil_putstr(buf+i, info->sn, strlen(info->sn));
561
562   i += aimutil_put16(buf+i, info->warnlevel);
563
564   /* XXX: we only put down five */
565   i += aimutil_put16(buf+i, 5);
566   aim_addtlvtochain16(&tlvlist, 0x0001, info->class);
567   aim_addtlvtochain32(&tlvlist, 0x0002, info->membersince);
568   aim_addtlvtochain32(&tlvlist, 0x0003, info->onlinesince);
569   aim_addtlvtochain16(&tlvlist, 0x0004, info->idletime);
570   /* XXX: should put caps here */
571   aim_addtlvtochain32(&tlvlist, (unsigned short)((info->class)&AIM_CLASS_AOL?0x0010:0x000f), info->sessionlen);
572   
573   i += aim_writetlvchain(buf+i, buflen-i, &tlvlist);
574   aim_freetlvchain(&tlvlist);
575   
576   return i;
577 }
578
579 faim_export int aim_sendbuddyoncoming(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_userinfo_s *info)
580 {
581   struct command_tx_struct *tx;
582   int i = 0;
583
584   if (!sess || !conn || !info)
585     return 0;
586
587   if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
588     return -1;
589
590   tx->lock = 1;
591
592   i += aimutil_put16(tx->data+i, 0x0003);
593   i += aimutil_put16(tx->data+i, 0x000b);
594   i += aimutil_put16(tx->data+i, 0x0000);
595   i += aimutil_put16(tx->data+i, 0x0000);
596   i += aimutil_put16(tx->data+i, 0x0000);
597
598   i += aim_putuserinfo(tx->data+i, tx->commandlen-i, info);
599
600   tx->commandlen = i;
601   tx->lock = 0;
602   aim_tx_enqueue(sess, tx);
603
604   return 0;
605 }
606
607 faim_export int aim_sendbuddyoffgoing(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn)
608 {
609   struct command_tx_struct *tx;
610   int i = 0;
611
612   if (!sess || !conn || !sn)
613     return 0;
614
615   if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+1+strlen(sn))))
616     return -1;
617
618   tx->lock = 1;
619
620   i += aimutil_put16(tx->data+i, 0x0003);
621   i += aimutil_put16(tx->data+i, 0x000c);
622   i += aimutil_put16(tx->data+i, 0x0000);
623   i += aimutil_put16(tx->data+i, 0x0000);
624   i += aimutil_put16(tx->data+i, 0x0000);
625
626   i += aimutil_put8(tx->data+i, strlen(sn));
627   i += aimutil_putstr(tx->data+i, sn, strlen(sn));
628   
629   tx->lock = 0;
630   aim_tx_enqueue(sess, tx);
631
632   return 0;
633 }
634
This page took 0.233823 seconds and 5 git commands to generate.