]> andersk Git - libfaim.git/blob - src/info.c
- Wed Oct 3 11:07:22 PDT 2001
[libfaim.git] / src / info.c
1 /*
2  * aim_info.c
3  *
4  * The functions here are responsible for requesting and parsing information-
5  * gathering SNACs.  Or something like that. 
6  *
7  */
8
9 #define FAIM_INTERNAL
10 #include <aim.h>
11
12 struct aim_priv_inforeq {
13         char sn[MAXSNLEN+1];
14         fu16_t infotype;
15 };
16
17 faim_export int aim_getinfo(aim_session_t *sess, aim_conn_t *conn, const char *sn, fu16_t infotype)
18 {
19         struct aim_priv_inforeq privdata;
20         aim_frame_t *fr;
21         aim_snacid_t snacid;
22
23         if (!sess || !conn || !sn)
24                 return -EINVAL;
25
26         if ((infotype != AIM_GETINFO_GENERALINFO) && (infotype != AIM_GETINFO_AWAYMESSAGE))
27                 return -EINVAL;
28
29         if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 12+1+strlen(sn))))
30                 return -ENOMEM;
31
32         strncpy(privdata.sn, sn, sizeof(privdata.sn));
33         privdata.infotype = infotype;
34         snacid = aim_cachesnac(sess, 0x0002, 0x0005, 0x0000, &privdata, sizeof(struct aim_priv_inforeq));
35         
36         aim_putsnac(&fr->data, 0x0002, 0x0005, 0x0000, snacid);
37         aimbs_put16(&fr->data, infotype);
38         aimbs_put8(&fr->data, strlen(sn));
39         aimbs_putraw(&fr->data, sn, strlen(sn));
40
41         aim_tx_enqueue(sess, fr);
42
43         return 0;
44 }
45
46 faim_export const char *aim_userinfo_sn(aim_userinfo_t *ui)
47 {
48
49         if (!ui)
50                 return NULL;
51
52         return ui->sn;
53 }
54
55 faim_export fu16_t aim_userinfo_flags(aim_userinfo_t *ui)
56 {
57
58         if (!ui)
59                 return 0;
60
61         return ui->flags;
62 }
63
64 faim_export fu16_t aim_userinfo_idle(aim_userinfo_t *ui)
65 {
66
67         if (!ui)
68                 return 0;
69
70         return ui->idletime;
71 }
72
73 faim_export float aim_userinfo_warnlevel(aim_userinfo_t *ui)
74 {
75
76         if (!ui)
77                 return 0.00;
78
79         return (ui->warnlevel / 10);
80 }
81
82 faim_export time_t aim_userinfo_membersince(aim_userinfo_t *ui)
83 {
84
85         if (!ui)
86                 return 0;
87
88         return (time_t)ui->membersince;
89 }
90
91 faim_export time_t aim_userinfo_onlinesince(aim_userinfo_t *ui)
92 {
93
94         if (!ui)
95                 return 0;
96
97         return (time_t)ui->onlinesince;
98 }
99
100 faim_export fu32_t aim_userinfo_sessionlen(aim_userinfo_t *ui)
101 {
102
103         if (!ui)
104                 return 0;
105
106         return ui->sessionlen;
107 }
108
109 faim_export int aim_userinfo_hascap(aim_userinfo_t *ui, fu16_t cap)
110 {
111
112         if (!ui || !ui->capspresent)
113                 return -1;
114
115         return !!(ui->capabilities & cap);
116 }
117
118
119 /*
120  * Capability blocks.  
121  */
122 static const struct {
123         unsigned short flag;
124         unsigned char data[16];
125 } aim_caps[] = {
126
127         {AIM_CAPS_BUDDYICON,
128          {0x09, 0x46, 0x13, 0x46, 0x4c, 0x7f, 0x11, 0xd1, 
129           0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
130
131         {AIM_CAPS_VOICE,
132          {0x09, 0x46, 0x13, 0x41, 0x4c, 0x7f, 0x11, 0xd1, 
133           0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
134
135         {AIM_CAPS_IMIMAGE,
136          {0x09, 0x46, 0x13, 0x45, 0x4c, 0x7f, 0x11, 0xd1, 
137           0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
138
139         {AIM_CAPS_CHAT,
140          {0x74, 0x8f, 0x24, 0x20, 0x62, 0x87, 0x11, 0xd1, 
141           0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
142
143         {AIM_CAPS_GETFILE,
144          {0x09, 0x46, 0x13, 0x48, 0x4c, 0x7f, 0x11, 0xd1,
145           0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
146
147         {AIM_CAPS_SENDFILE,
148          {0x09, 0x46, 0x13, 0x43, 0x4c, 0x7f, 0x11, 0xd1, 
149           0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
150
151         {AIM_CAPS_SAVESTOCKS,
152          {0x09, 0x46, 0x13, 0x47, 0x4c, 0x7f, 0x11, 0xd1,
153           0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
154
155         /*
156          * Indeed, there are two of these.  The former appears to be correct, 
157          * but in some versions of winaim, the second one is set.  Either they 
158          * forgot to fix endianness, or they made a typo. It really doesn't 
159          * matter which.
160          */
161         {AIM_CAPS_GAMES,
162          {0x09, 0x46, 0x13, 0x4a, 0x4c, 0x7f, 0x11, 0xd1,
163           0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
164         {AIM_CAPS_GAMES2,
165          {0x09, 0x46, 0x13, 0x4a, 0x4c, 0x7f, 0x11, 0xd1,
166           0x22, 0x82, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
167
168         {AIM_CAPS_SENDBUDDYLIST,
169          {0x09, 0x46, 0x13, 0x4b, 0x4c, 0x7f, 0x11, 0xd1,
170           0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
171
172         {AIM_CAPS_LAST}
173 };
174
175 /*
176  * This still takes a length parameter even with a bstream because capabilities
177  * are not naturally bounded.
178  * 
179  */
180 faim_internal fu16_t aim_getcap(aim_session_t *sess, aim_bstream_t *bs, int len)
181 {
182         fu16_t flags = 0;
183         int offset;
184
185         for (offset = 0; aim_bstream_empty(bs) && (offset < len); offset += 0x10) {
186                 fu8_t *cap;
187                 int i, identified;
188
189                 cap = aimbs_getraw(bs, 0x10);
190
191                 for (i = 0, identified = 0; !(aim_caps[i].flag & AIM_CAPS_LAST); i++) {
192
193                         if (memcmp(&aim_caps[i].data, cap, 0x10) == 0) {
194                                 flags |= aim_caps[i].flag;
195                                 identified++;
196                                 break; /* should only match once... */
197
198                         }
199                 }
200
201                 if (!identified)
202                         faimdprintf(sess, 0, "unknown capability!\n");
203
204                 free(cap);
205         }
206
207         return flags;
208 }
209
210 faim_internal int aim_putcap(aim_bstream_t *bs, fu16_t caps)
211 {
212         int i;
213
214         if (!bs)
215                 return -EINVAL;
216
217         for (i = 0; aim_bstream_empty(bs); i++) {
218
219                 if (aim_caps[i].flag == AIM_CAPS_LAST)
220                         break;
221
222                 if (caps & aim_caps[i].flag)
223                         aimbs_putraw(bs, aim_caps[i].data, 0x10);
224
225         }
226
227         return 0;
228 }
229
230 /*
231  * AIM is fairly regular about providing user info.  This is a generic 
232  * routine to extract it in its standard form.
233  */
234 faim_internal int aim_extractuserinfo(aim_session_t *sess, aim_bstream_t *bs, aim_userinfo_t *outinfo)
235 {
236         int curtlv, tlvcnt;
237         fu8_t snlen;
238
239         if (!bs || !outinfo)
240                 return -EINVAL;
241
242         /* Clear out old data first */
243         memset(outinfo, 0x00, sizeof(aim_userinfo_t));
244
245         /*
246          * Screen name.  Stored as an unterminated string prepended with a 
247          * byte containing its length.
248          */
249         snlen = aimbs_get8(bs);
250         aimbs_getrawbuf(bs, outinfo->sn, snlen);
251
252         /*
253          * Warning Level.  Stored as an unsigned short.
254          */
255         outinfo->warnlevel = aimbs_get16(bs);
256
257         /*
258          * TLV Count. Unsigned short representing the number of 
259          * Type-Length-Value triples that follow.
260          */
261         tlvcnt = aimbs_get16(bs);
262
263         /* 
264          * Parse out the Type-Length-Value triples as they're found.
265          */
266         for (curtlv = 0; curtlv < tlvcnt; curtlv++) {
267                 int endpos;
268                 fu16_t type, length;
269
270                 type = aimbs_get16(bs);
271                 length = aimbs_get16(bs);
272
273                 endpos = aim_bstream_curpos(bs) + length;
274
275                 if (type == 0x0001) {
276                         /*
277                          * Type = 0x0001: User flags
278                          * 
279                          * Specified as any of the following ORed together:
280                          *      0x0001  Trial (user less than 60days)
281                          *      0x0002  Unknown bit 2
282                          *      0x0004  AOL Main Service user
283                          *      0x0008  Unknown bit 4
284                          *      0x0010  Free (AIM) user 
285                          *      0x0020  Away
286                          *      0x0400  ActiveBuddy
287                          *
288                          */
289                         outinfo->flags = aimbs_get16(bs);
290
291                 } else if (type == 0x0002) {
292                         /*
293                          * Type = 0x0002: Member-Since date. 
294                          *
295                          * The time/date that the user originally registered for
296                          * the service, stored in time_t format.
297                          */
298                         outinfo->membersince = aimbs_get32(bs);
299
300                 } else if (type == 0x0003) {
301                         /*
302                          * Type = 0x0003: On-Since date.
303                          *
304                          * The time/date that the user started their current 
305                          * session, stored in time_t format.
306                          */
307                         outinfo->onlinesince = aimbs_get32(bs);
308
309                 } else if (type == 0x0004) {
310                         /*
311                          * Type = 0x0004: Idle time.
312                          *
313                          * Number of seconds since the user actively used the 
314                          * service.
315                          *
316                          * Note that the client tells the server when to start
317                          * counting idle times, so this may or may not be 
318                          * related to reality.
319                          */
320                         outinfo->idletime = aimbs_get16(bs);
321
322                 } else if (type == 0x0006) {
323                         /*
324                          * Type = 0x0006: ICQ Online Status
325                          *
326                          * ICQ's Away/DND/etc "enriched" status. Some decoding 
327                          * of values done by Scott <darkagl@pcnet.com>
328                          */
329                         aimbs_get16(bs);
330                         outinfo->icqinfo.status = aimbs_get16(bs);
331
332                 } else if (type == 0x000a) {
333                         /*
334                          * Type = 0x000a
335                          *
336                          * ICQ User IP Address.
337                          * Ahh, the joy of ICQ security.
338                          */
339                         outinfo->icqinfo.ipaddr = aimbs_get32(bs);
340
341                 } else if (type == 0x000c) {
342                         /* 
343                          * Type = 0x000c
344                          *
345                          * random crap containing the IP address,
346                          * apparently a port number, and some Other Stuff.
347                          *
348                          */
349                         aimbs_getrawbuf(bs, outinfo->icqinfo.crap, 0x25);
350
351                 } else if (type == 0x000d) {
352                         /*
353                          * Type = 0x000d
354                          *
355                          * Capability information.
356                          *
357                          */
358                         outinfo->capabilities = aim_getcap(sess, bs, length);
359                         outinfo->capspresent = 1;
360
361                 } else if (type == 0x000e) {
362                         /*
363                          * Type = 0x000e
364                          *
365                          * Unknown.  Always of zero length, and always only
366                          * on AOL users.
367                          *
368                          * Ignore.
369                          *
370                          */
371
372                 } else if ((type == 0x000f) || (type == 0x0010)) {
373                         /*
374                          * Type = 0x000f: Session Length. (AIM)
375                          * Type = 0x0010: Session Length. (AOL)
376                          *
377                          * The duration, in seconds, of the user's current 
378                          * session.
379                          *
380                          * Which TLV type this comes in depends on the
381                          * service the user is using (AIM or AOL).
382                          *
383                          */
384                         outinfo->sessionlen = aimbs_get32(bs);
385
386                 } else {
387
388                         /*
389                          * Reaching here indicates that either AOL has
390                          * added yet another TLV for us to deal with, 
391                          * or the parsing has gone Terribly Wrong.
392                          *
393                          * Either way, inform the owner and attempt
394                          * recovery.
395                          *
396                          */
397                         faimdprintf(sess, 0, "userinfo: **warning: unexpected TLV:\n");
398                         faimdprintf(sess, 0, "userinfo:   sn    =%s\n", outinfo->sn);
399                         faimdprintf(sess, 0, "userinfo:   type  =0x%04x\n",type);
400                         faimdprintf(sess, 0, "userinfo:   length=0x%04x\n", length);
401
402                 }
403
404                 /* Save ourselves. */
405                 aim_bstream_setpos(bs, endpos);
406         }
407
408         return 0;
409 }
410
411 /*
412  * Inverse of aim_extractuserinfo()
413  */
414 faim_internal int aim_putuserinfo(aim_bstream_t *bs, aim_userinfo_t *info)
415 {
416         aim_tlvlist_t *tlvlist = NULL;
417
418         if (!bs || !info)
419                 return -EINVAL;
420
421         aimbs_put8(bs, strlen(info->sn));
422         aimbs_putraw(bs, info->sn, strlen(info->sn));
423
424         aimbs_put16(bs, info->warnlevel);
425
426
427         aim_addtlvtochain16(&tlvlist, 0x0001, info->flags);
428         aim_addtlvtochain32(&tlvlist, 0x0002, info->membersince);
429         aim_addtlvtochain32(&tlvlist, 0x0003, info->onlinesince);
430         aim_addtlvtochain16(&tlvlist, 0x0004, info->idletime);
431
432 #if ICQ_OSCAR_SUPPORT
433         if (atoi(info->sn) != 0) {
434                 aim_addtlvtochain16(&tlvlist, 0x0006, info->icqinfo.status);
435                 aim_addtlvtochain32(&tlvlist, 0x000a, info->icqinfo.ipaddr);
436         }
437 #endif
438
439         aim_addtlvtochain_caps(&tlvlist, 0x000d, info->capabilities);
440
441         aim_addtlvtochain32(&tlvlist, (fu16_t)((info->flags & AIM_FLAG_AOL) ? 0x0010 : 0x000f), info->sessionlen);
442
443         aimbs_put16(bs, aim_counttlvchain(&tlvlist));
444         aim_writetlvchain(bs, &tlvlist);
445         aim_freetlvchain(&tlvlist);
446
447         return 0;
448 }
449
450 faim_export int aim_sendbuddyoncoming(aim_session_t *sess, aim_conn_t *conn, aim_userinfo_t *info)
451 {
452         aim_frame_t *fr;
453         aim_snacid_t snacid;
454
455         if (!sess || !conn || !info)
456                 return -EINVAL;
457
458         if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 1152)))
459                 return -ENOMEM;
460
461         snacid = aim_cachesnac(sess, 0x0003, 0x000b, 0x0000, NULL, 0);
462         
463         aim_putsnac(&fr->data, 0x0003, 0x000b, 0x0000, snacid);
464         aim_putuserinfo(&fr->data, info);
465
466         aim_tx_enqueue(sess, fr);
467
468         return 0;
469 }
470
471 faim_export int aim_sendbuddyoffgoing(aim_session_t *sess, aim_conn_t *conn, const char *sn)
472 {
473         aim_frame_t *fr;
474         aim_snacid_t snacid;
475
476         if (!sess || !conn || !sn)
477                 return -EINVAL;
478
479         if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+1+strlen(sn))))
480                 return -ENOMEM;
481
482         snacid = aim_cachesnac(sess, 0x0003, 0x000c, 0x0000, NULL, 0);
483         
484         aim_putsnac(&fr->data, 0x0003, 0x000c, 0x0000, snacid);
485         aimbs_put8(&fr->data, strlen(sn));
486         aimbs_putraw(&fr->data, sn, strlen(sn));
487
488         aim_tx_enqueue(sess, fr);
489
490         return 0;
491 }
492
493 /*
494  * Huh? What is this?
495  */
496 faim_export int aim_0002_000b(aim_session_t *sess, aim_conn_t *conn, const char *sn)
497 {
498         aim_frame_t *fr;
499         aim_snacid_t snacid;
500
501         if (!sess || !conn || !sn)
502                 return -EINVAL;
503
504         if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+1+strlen(sn))))
505                 return -ENOMEM;
506
507         snacid = aim_cachesnac(sess, 0x0002, 0x000b, 0x0000, NULL, 0);
508         
509         aim_putsnac(&fr->data, 0x0002, 0x000b, 0x0000, snacid);
510         aimbs_put8(&fr->data, strlen(sn));
511         aimbs_putraw(&fr->data, sn, strlen(sn));
512
513         aim_tx_enqueue(sess, fr);
514
515         return 0;
516 }
517
518 /*
519  * Normally contains:
520  *   t(0001)  - short containing max profile length (value = 1024)
521  *   t(0002)  - short - unknown (value = 16) [max MIME type length?]
522  *   t(0003)  - short - unknown (value = 10)
523  */
524 static int rights(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
525 {
526         aim_tlvlist_t *tlvlist;
527         aim_rxcallback_t userfunc;
528         int ret = 0;
529         fu16_t maxsiglen = 0;
530
531         tlvlist = aim_readtlvchain(bs);
532
533         if (aim_gettlv(tlvlist, 0x0001, 1))
534                 maxsiglen = aim_gettlv16(tlvlist, 0x0001, 1);
535
536         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
537                 ret = userfunc(sess, rx, maxsiglen);
538
539         aim_freetlvchain(&tlvlist);
540
541         return ret;
542 }
543
544 static int userinfo(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
545 {
546         aim_userinfo_t userinfo;
547         char *text_encoding = NULL, *text = NULL;
548         aim_rxcallback_t userfunc;
549         aim_tlvlist_t *tlvlist;
550         aim_snac_t *origsnac = NULL;
551         struct aim_priv_inforeq *inforeq;
552         int ret = 0;
553
554         origsnac = aim_remsnac(sess, snac->id);
555
556         if (!origsnac || !origsnac->data) {
557                 faimdprintf(sess, 0, "parse_userinfo_middle: major problem: no snac stored!\n");
558                 return 0;
559         }
560
561         inforeq = (struct aim_priv_inforeq *)origsnac->data;
562
563         if ((inforeq->infotype != AIM_GETINFO_GENERALINFO) &&
564                         (inforeq->infotype != AIM_GETINFO_AWAYMESSAGE)) {
565                 faimdprintf(sess, 0, "parse_userinfo_middle: unknown infotype in request! (0x%04x)\n", inforeq->infotype);
566                 return 0;
567         }
568
569         aim_extractuserinfo(sess, bs, &userinfo);
570
571         tlvlist = aim_readtlvchain(bs);
572
573         /* 
574          * Depending on what informational text was requested, different
575          * TLVs will appear here.
576          *
577          * Profile will be 1 and 2, away message will be 3 and 4.
578          */
579         if (aim_gettlv(tlvlist, 0x0001, 1)) {
580                 text_encoding = aim_gettlv_str(tlvlist, 0x0001, 1);
581                 text = aim_gettlv_str(tlvlist, 0x0002, 1);
582         } else if (aim_gettlv(tlvlist, 0x0003, 1)) {
583                 text_encoding = aim_gettlv_str(tlvlist, 0x0003, 1);
584                 text = aim_gettlv_str(tlvlist, 0x0004, 1);
585         }
586
587         if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
588                 ret = userfunc(sess, rx, &userinfo, text_encoding, text, inforeq->infotype);
589
590         free(text_encoding);
591         free(text);
592
593         aim_freetlvchain(&tlvlist);
594
595         if (origsnac)
596                 free(origsnac->data);
597         free(origsnac);
598
599         return ret;
600 }
601
602 static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
603 {
604
605         if (snac->subtype == 0x0003)
606                 return rights(sess, mod, rx, snac, bs);
607         else if (snac->subtype == 0x0006)
608                 return userinfo(sess, mod, rx, snac, bs);
609
610         return 0;
611 }
612
613 faim_internal int locate_modfirst(aim_session_t *sess, aim_module_t *mod)
614 {
615
616         mod->family = 0x0002;
617         mod->version = 0x0000;
618         mod->flags = 0;
619         strncpy(mod->name, "locate", sizeof(mod->name));
620         mod->snachandler = snachandler;
621
622         return 0;
623 }
This page took 0.293071 seconds and 5 git commands to generate.