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