]> andersk Git - libfaim.git/blob - aim_info.c
Oops.
[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: User flags
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->flags = 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 = 0x0006: ICQ Online Status
318        *
319        * ICQ's Away/DND/etc "enriched" status
320        * Some decoding of values done by Scott <darkagl@pcnet.com>
321        */
322     case 0x0006:
323       outinfo->icqinfo.status = aimutil_get16(buf+i+2+2+2);
324       break;
325
326
327       /*
328        * Type = 0x000a
329        *
330        * ICQ User IP Address.
331        * Ahh, the joy of ICQ security.
332        */
333     case 0x000a:
334       outinfo->icqinfo.ipaddr = aimutil_get32(&buf[i+4]);
335       break;
336
337       /* Type = 0x000c
338        *
339        * random crap containing the IP address,
340        * apparently a port number, and some Other Stuff.
341        *
342        */
343     case 0x000c:
344       memcpy(outinfo->icqinfo.crap, &buf[i+4], 0x25);
345       break;
346
347       /*
348        * Type = 0x000d
349        *
350        * Capability information.  Not real sure of
351        * actual decoding.  See comment on aim_bos_setprofile()
352        * in aim_misc.c about the capability block, its the same.
353        *
354        */
355     case 0x000d:
356       {
357         int len;
358         len = aimutil_get16(buf+i+2);
359         if (!len)
360           break;
361         
362         outinfo->capabilities = aim_getcap(buf+i+4, len);
363       }
364       break;
365       
366       /*
367        * Type = 0x000e
368        *
369        * Unknown.  Always of zero length, and always only
370        * on AOL users.
371        *
372        * Ignore.
373        *
374        */
375     case 0x000e:
376       break;
377       
378       /*
379        * Type = 0x000f: Session Length. (AIM)
380        * Type = 0x0010: Session Length. (AOL)
381        *
382        * The duration, in seconds, of the user's
383        * current session.
384        *
385        * Which TLV type this comes in depends
386        * on the service the user is using (AIM or AOL).
387        *
388        */
389     case 0x000f:
390     case 0x0010:
391       outinfo->sessionlen = aimutil_get32(&buf[i+4]);
392       break;
393       
394       /*
395        * Reaching here indicates that either AOL has
396        * added yet another TLV for us to deal with, 
397        * or the parsing has gone Terribly Wrong.
398        *
399        * Either way, inform the owner and attempt
400        * recovery.
401        *
402        */
403     default:
404       {
405         int len,z = 0, y = 0, x = 0;
406         char tmpstr[80];
407         printf("faim: userinfo: **warning: unexpected TLV:\n");
408         printf("faim: userinfo:   sn    =%s\n", outinfo->sn);
409         printf("faim: userinfo:   curtlv=0x%04x\n", curtlv);
410         printf("faim: userinfo:   type  =0x%04x\n",aimutil_get16(&buf[i]));
411         printf("faim: userinfo:   length=0x%04x\n", len = aimutil_get16(&buf[i+2]));
412         printf("faim: userinfo:   data: \n");
413         while (z<len)
414           {
415             x = sprintf(tmpstr, "faim: userinfo:      ");
416             for (y = 0; y < 8; y++)
417               {
418                 if (z<len)
419                   {
420                     sprintf(tmpstr+x, "%02x ", buf[i+4+z]);
421                     z++;
422                     x += 3;
423                   }
424                 else
425                   break;
426               }
427             printf("%s\n", tmpstr);
428           }
429       }
430       break;
431     }  
432     /*
433      * No matter what, TLV triplets should always look like this:
434      *
435      *   u_short type;
436      *   u_short length;
437      *   u_char  data[length];
438      *
439      */
440     if (lastvalid) {
441       i += (2 + 2 + aimutil_get16(&buf[i+2]));     
442       curtlv++;
443     }
444   }
445   
446   return i;
447 }
448
449 /*
450  * Oncoming Buddy notifications contain a subset of the
451  * user information structure.  Its close enough to run
452  * through aim_extractuserinfo() however.
453  *
454  */
455 faim_internal int aim_parse_oncoming_middle(struct aim_session_t *sess,
456                                             struct command_rx_struct *command)
457 {
458   struct aim_userinfo_s userinfo;
459   u_int i = 0;
460   rxcallback_t userfunc=NULL;
461
462   i = 10;
463   i += aim_extractuserinfo(command->data+i, &userinfo);
464
465   userfunc = aim_callhandler(command->conn, AIM_CB_FAM_BUD, AIM_CB_BUD_ONCOMING);
466   if (userfunc)
467     i = userfunc(sess, command, &userinfo);
468
469   return 1;
470 }
471
472 /*
473  * Offgoing Buddy notifications contain no useful
474  * information other than the name it applies to.
475  *
476  */
477 faim_internal int aim_parse_offgoing_middle(struct aim_session_t *sess,
478                                             struct command_rx_struct *command)
479 {
480   char sn[MAXSNLEN+1];
481   u_int i = 0;
482   rxcallback_t userfunc=NULL;
483
484   strncpy(sn, (char *)command->data+11, (int)command->data[10]);
485   sn[(int)command->data[10]] = '\0';
486
487   userfunc = aim_callhandler(command->conn, AIM_CB_FAM_BUD, AIM_CB_BUD_OFFGOING);
488   if (userfunc)
489     i = userfunc(sess, command, sn);
490
491   return 1;
492 }
493
494 /*
495  * This parses the user info stuff out all nice and pretty then calls 
496  * the higher-level callback (in the user app).
497  *
498  */
499 faim_internal int aim_parse_userinfo_middle(struct aim_session_t *sess,
500                                             struct command_rx_struct *command)
501 {
502   struct aim_userinfo_s userinfo;
503   char *text_encoding = NULL;
504   char *text = NULL;
505   u_int i = 0;
506   rxcallback_t userfunc=NULL;
507   struct aim_tlvlist_t *tlvlist;
508   struct aim_snac_t *origsnac = NULL;
509   u_long snacid;
510   struct aim_priv_inforeq *inforeq;
511   
512   snacid = aimutil_get32(&command->data[6]);
513   origsnac = aim_remsnac(sess, snacid);
514
515   if (!origsnac || !origsnac->data) {
516     printf("faim: parse_userinfo_middle: major problem: no snac stored!\n");
517     return 1;
518   }
519
520   inforeq = (struct aim_priv_inforeq *)origsnac->data;
521
522   switch (inforeq->infotype) {
523   case AIM_GETINFO_GENERALINFO:
524   case AIM_GETINFO_AWAYMESSAGE:
525     i = 10;
526
527     /*
528      * extractuserinfo will give us the basic metaTLV information
529      */
530     i += aim_extractuserinfo(command->data+i, &userinfo);
531   
532     /*
533      * However, in this command, there's usually more TLVs following...
534      */ 
535     tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i);
536
537     /* 
538      * Depending on what informational text was requested, different
539      * TLVs will appear here.
540      *
541      * Profile will be 1 and 2, away message will be 3 and 4.
542      */
543     if (aim_gettlv(tlvlist, 0x0001, 1)) {
544       text_encoding = aim_gettlv_str(tlvlist, 0x0001, 1);
545       text = aim_gettlv_str(tlvlist, 0x0002, 1);
546     } else if (aim_gettlv(tlvlist, 0x0003, 1)) {
547       text_encoding = aim_gettlv_str(tlvlist, 0x0003, 1);
548       text = aim_gettlv_str(tlvlist, 0x0004, 1);
549     }
550
551     userfunc = aim_callhandler(command->conn, AIM_CB_FAM_LOC, AIM_CB_LOC_USERINFO);
552     if (userfunc) {
553       i = userfunc(sess,
554                    command, 
555                    &userinfo, 
556                    text_encoding, 
557                    text,
558                    inforeq->infotype); 
559     }
560   
561     free(text_encoding);
562     free(text);
563     aim_freetlvchain(&tlvlist);
564     break;
565   default:
566     printf("faim: parse_userinfo_middle: unknown infotype in request! (0x%04x)\n", inforeq->infotype);
567     break;
568   }
569
570   if (origsnac) {
571     if (origsnac->data)
572       free(origsnac->data);
573     free(origsnac);
574   }
575
576   return 1;
577 }
578
579 /*
580  * Inverse of aim_extractuserinfo()
581  */
582 faim_internal int aim_putuserinfo(u_char *buf, int buflen, struct aim_userinfo_s *info)
583 {
584   int i = 0, numtlv = 0;
585   struct aim_tlvlist_t *tlvlist = NULL;
586
587   if (!buf || !info)
588     return 0;
589
590   i += aimutil_put8(buf+i, strlen(info->sn));
591   i += aimutil_putstr(buf+i, info->sn, strlen(info->sn));
592
593   i += aimutil_put16(buf+i, info->warnlevel);
594
595
596   aim_addtlvtochain16(&tlvlist, 0x0001, info->flags);
597   numtlv++;
598
599   aim_addtlvtochain32(&tlvlist, 0x0002, info->membersince);
600   numtlv++;
601
602   aim_addtlvtochain32(&tlvlist, 0x0003, info->onlinesince);
603   numtlv++;
604
605   aim_addtlvtochain16(&tlvlist, 0x0004, info->idletime);
606   numtlv++;
607
608 #if ICQ_OSCAR_SUPPORT
609   if(atoi(info->sn) != 0) {
610     aim_addtlvtochain16(&tlvlist, 0x0006, info->icqinfo.status);
611     aim_addtlvtochain32(&tlvlist, 0x000a, info->icqinfo.ipaddr);
612   }
613 #endif
614
615   aim_addtlvtochain_caps(&tlvlist, 0x000d, info->capabilities);
616   numtlv++;
617
618   aim_addtlvtochain32(&tlvlist, (unsigned short)((info->flags)&AIM_FLAG_AOL?0x0010:0x000f), info->sessionlen);
619   numtlv++;
620
621   i += aimutil_put16(buf+i, numtlv); /* tlvcount */
622   i += aim_writetlvchain(buf+i, buflen-i, &tlvlist); /* tlvs */
623   aim_freetlvchain(&tlvlist);
624
625   return i;
626 }
627
628 faim_export int aim_sendbuddyoncoming(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_userinfo_s *info)
629 {
630   struct command_tx_struct *tx;
631   int i = 0;
632
633   if (!sess || !conn || !info)
634     return 0;
635
636   if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
637     return -1;
638
639   tx->lock = 1;
640
641   i += aimutil_put16(tx->data+i, 0x0003);
642   i += aimutil_put16(tx->data+i, 0x000b);
643   i += aimutil_put16(tx->data+i, 0x0000);
644   i += aimutil_put16(tx->data+i, 0x0000);
645   i += aimutil_put16(tx->data+i, 0x0000);
646
647   i += aim_putuserinfo(tx->data+i, tx->commandlen-i, info);
648
649   tx->commandlen = i;
650   tx->lock = 0;
651   aim_tx_enqueue(sess, tx);
652
653   return 0;
654 }
655
656 faim_export int aim_sendbuddyoffgoing(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn)
657 {
658   struct command_tx_struct *tx;
659   int i = 0;
660
661   if (!sess || !conn || !sn)
662     return 0;
663
664   if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+1+strlen(sn))))
665     return -1;
666
667   tx->lock = 1;
668
669   i += aimutil_put16(tx->data+i, 0x0003);
670   i += aimutil_put16(tx->data+i, 0x000c);
671   i += aimutil_put16(tx->data+i, 0x0000);
672   i += aimutil_put16(tx->data+i, 0x0000);
673   i += aimutil_put16(tx->data+i, 0x0000);
674
675   i += aimutil_put8(tx->data+i, strlen(sn));
676   i += aimutil_putstr(tx->data+i, sn, strlen(sn));
677   
678   tx->lock = 0;
679   aim_tx_enqueue(sess, tx);
680
681   return 0;
682 }
683
This page took 0.086506 seconds and 5 git commands to generate.