]> andersk Git - libfaim.git/blame - aim_info.c
- Fri Sep 22 22:47:49 UTC 2000
[libfaim.git] / aim_info.c
CommitLineData
9de3ca7e 1/*
2 * aim_info.c
3 *
4 * The functions here are responsible for requesting and parsing information-
5 * gathering SNACs.
6 *
7 */
8
9
a25832e6 10#include <faim/aim.h>
9de3ca7e 11
262272cc 12struct aim_priv_inforeq {
13 char sn[MAXSNLEN];
14 unsigned short infotype;
15};
16
78b3fb13 17faim_export unsigned long aim_getinfo(struct aim_session_t *sess,
18 struct aim_conn_t *conn,
19 const char *sn,
20 unsigned short infotype)
9de3ca7e 21{
5b79dc93 22 struct command_tx_struct *newpacket;
276495a3 23 int i = 0;
24
25 if (!sess || !conn || !sn)
26 return 0;
9de3ca7e 27
b69540e3 28 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 12+1+strlen(sn))))
5b79dc93 29 return -1;
9de3ca7e 30
5b79dc93 31 newpacket->lock = 1;
9de3ca7e 32
5b79dc93 33 i = aim_putsnac(newpacket->data, 0x0002, 0x0005, 0x0000, sess->snac_nextid);
9de3ca7e 34
262272cc 35 i += aimutil_put16(newpacket->data+i, infotype);
5b79dc93 36 i += aimutil_put8(newpacket->data+i, strlen(sn));
37 i += aimutil_putstr(newpacket->data+i, sn, strlen(sn));
9de3ca7e 38
5b79dc93 39 newpacket->lock = 0;
40 aim_tx_enqueue(sess, newpacket);
9de3ca7e 41
42 {
43 struct aim_snac_t snac;
44
a25832e6 45 snac.id = sess->snac_nextid;
9de3ca7e 46 snac.family = 0x0002;
47 snac.type = 0x0005;
48 snac.flags = 0x0000;
49
262272cc 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;
9de3ca7e 53
a25832e6 54 aim_newsnac(sess, &snac);
9de3ca7e 55 }
56
a25832e6 57 return (sess->snac_nextid++);
9de3ca7e 58}
59
78b3fb13 60faim_internal int aim_parse_locateerr(struct aim_session_t *sess,
61 struct command_rx_struct *command)
96f8b1ed 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}
5b79dc93 103
104/*
105 * Capability blocks.
106 */
107u_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
78b3fb13 134faim_internal unsigned short aim_getcap(unsigned char *capblock, int buflen)
b69540e3 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
78b3fb13 159faim_internal int aim_putcap(unsigned char *capblock, int buflen, u_short caps)
b69540e3 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
9de3ca7e 194/*
195 * AIM is fairly regular about providing user info. This
196 * is a generic routine to extract it in its standard form.
197 */
78b3fb13 198faim_internal int aim_extractuserinfo(u_char *buf, struct aim_userinfo_s *outinfo)
9de3ca7e 199{
200 int i = 0;
201 int tlvcnt = 0;
202 int curtlv = 0;
203 int tlv1 = 0;
204 u_short curtype;
68ac63c2 205 int lastvalid;
9de3ca7e 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 */
68ac63c2 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 }
9de3ca7e 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 */
68ac63c2 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;
0b4acf97 260
68ac63c2 261 /*
a15d82b1 262 * Type = 0x0001: User flags
68ac63c2 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;
a15d82b1 280 outinfo->flags = aimutil_get16(&buf[i+4]);
68ac63c2 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
f2d214f9 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
68ac63c2 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)
9de3ca7e 341 break;
68ac63c2 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)
9de3ca7e 395 {
68ac63c2 396 x = sprintf(tmpstr, "faim: userinfo: ");
397 for (y = 0; y < 8; y++)
9de3ca7e 398 {
68ac63c2 399 if (z<len)
9de3ca7e 400 {
68ac63c2 401 sprintf(tmpstr+x, "%02x ", buf[i+4+z]);
402 z++;
403 x += 3;
9de3ca7e 404 }
68ac63c2 405 else
406 break;
9de3ca7e 407 }
68ac63c2 408 printf("%s\n", tmpstr);
9de3ca7e 409 }
68ac63c2 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]));
9de3ca7e 423 curtlv++;
424 }
68ac63c2 425 }
9de3ca7e 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 */
78b3fb13 436faim_internal int aim_parse_oncoming_middle(struct aim_session_t *sess,
437 struct command_rx_struct *command)
9de3ca7e 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)
a25832e6 448 i = userfunc(sess, command, &userinfo);
9de3ca7e 449
450 return 1;
451}
452
a25832e6 453/*
454 * Offgoing Buddy notifications contain no useful
455 * information other than the name it applies to.
456 *
457 */
78b3fb13 458faim_internal int aim_parse_offgoing_middle(struct aim_session_t *sess,
459 struct command_rx_struct *command)
a25832e6 460{
461 char sn[MAXSNLEN+1];
462 u_int i = 0;
463 rxcallback_t userfunc=NULL;
464
78b3fb13 465 strncpy(sn, (char *)command->data+11, (int)command->data[10]);
1a7c2214 466 sn[(int)command->data[10]] = '\0';
a25832e6 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}
9de3ca7e 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 */
78b3fb13 480faim_internal int aim_parse_userinfo_middle(struct aim_session_t *sess,
481 struct command_rx_struct *command)
9de3ca7e 482{
483 struct aim_userinfo_s userinfo;
262272cc 484 char *text_encoding = NULL;
485 char *text = NULL;
9de3ca7e 486 u_int i = 0;
487 rxcallback_t userfunc=NULL;
0c20631f 488 struct aim_tlvlist_t *tlvlist;
262272cc 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);
9de3ca7e 495
262272cc 496 if (!origsnac || !origsnac->data) {
497 printf("faim: parse_userinfo_middle: major problem: no snac stored!\n");
498 return 1;
9de3ca7e 499 }
9de3ca7e 500
262272cc 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);
0c20631f 512
262272cc 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 }
9de3ca7e 531
262272cc 532 userfunc = aim_callhandler(command->conn, AIM_CB_FAM_LOC, AIM_CB_LOC_USERINFO);
533 if (userfunc) {
a25832e6 534 i = userfunc(sess,
535 command,
9de3ca7e 536 &userinfo,
262272cc 537 text_encoding,
538 text,
539 inforeq->infotype);
9de3ca7e 540 }
0c20631f 541
262272cc 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 }
9de3ca7e 556
557 return 1;
558}
50443ea0 559
560/*
561 * Inverse of aim_extractuserinfo()
562 */
78b3fb13 563faim_internal int aim_putuserinfo(u_char *buf, int buflen, struct aim_userinfo_s *info)
50443ea0 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);
a15d82b1 578 aim_addtlvtochain16(&tlvlist, 0x0001, info->flags);
50443ea0 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 */
a15d82b1 583 aim_addtlvtochain32(&tlvlist, (unsigned short)((info->flags)&AIM_FLAG_AOL?0x0010:0x000f), info->sessionlen);
50443ea0 584
585 i += aim_writetlvchain(buf+i, buflen-i, &tlvlist);
586 aim_freetlvchain(&tlvlist);
587
588 return i;
589}
590
78b3fb13 591faim_export int aim_sendbuddyoncoming(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_userinfo_s *info)
50443ea0 592{
5b79dc93 593 struct command_tx_struct *tx;
50443ea0 594 int i = 0;
595
596 if (!sess || !conn || !info)
597 return 0;
598
b69540e3 599 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 1152)))
5b79dc93 600 return -1;
50443ea0 601
5b79dc93 602 tx->lock = 1;
50443ea0 603
5b79dc93 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);
50443ea0 609
5b79dc93 610 i += aim_putuserinfo(tx->data+i, tx->commandlen-i, info);
50443ea0 611
5b79dc93 612 tx->commandlen = i;
613 tx->lock = 0;
614 aim_tx_enqueue(sess, tx);
50443ea0 615
616 return 0;
617}
618
78b3fb13 619faim_export int aim_sendbuddyoffgoing(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn)
50443ea0 620{
5b79dc93 621 struct command_tx_struct *tx;
50443ea0 622 int i = 0;
623
624 if (!sess || !conn || !sn)
625 return 0;
626
b69540e3 627 if (!(tx = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+1+strlen(sn))))
5b79dc93 628 return -1;
50443ea0 629
5b79dc93 630 tx->lock = 1;
50443ea0 631
5b79dc93 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);
50443ea0 637
5b79dc93 638 i += aimutil_put8(tx->data+i, strlen(sn));
639 i += aimutil_putstr(tx->data+i, sn, strlen(sn));
50443ea0 640
5b79dc93 641 tx->lock = 0;
642 aim_tx_enqueue(sess, tx);
50443ea0 643
644 return 0;
645}
262272cc 646
This page took 0.140393 seconds and 5 git commands to generate.