4 * The functions here are responsible for requesting and parsing information-
5 * gathering SNACs. Or something like that.
12 struct aim_priv_inforeq {
17 faim_export int aim_getinfo(aim_session_t *sess, aim_conn_t *conn, const char *sn, fu16_t infotype)
19 struct aim_priv_inforeq privdata;
23 if (!sess || !conn || !sn)
26 if ((infotype != AIM_GETINFO_GENERALINFO) && (infotype != AIM_GETINFO_AWAYMESSAGE))
29 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 12+1+strlen(sn))))
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));
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));
41 aim_tx_enqueue(sess, fr);
46 faim_export const char *aim_userinfo_sn(aim_userinfo_t *ui)
55 faim_export fu16_t aim_userinfo_flags(aim_userinfo_t *ui)
64 faim_export fu16_t aim_userinfo_idle(aim_userinfo_t *ui)
73 faim_export float aim_userinfo_warnlevel(aim_userinfo_t *ui)
79 return (ui->warnlevel / 10);
82 faim_export time_t aim_userinfo_membersince(aim_userinfo_t *ui)
88 return (time_t)ui->membersince;
91 faim_export time_t aim_userinfo_onlinesince(aim_userinfo_t *ui)
97 return (time_t)ui->onlinesince;
100 faim_export fu32_t aim_userinfo_sessionlen(aim_userinfo_t *ui)
106 return ui->sessionlen;
109 faim_export int aim_userinfo_hascap(aim_userinfo_t *ui, fu16_t cap)
112 if (!ui || !ui->capspresent)
115 return !!(ui->capabilities & cap);
122 static const struct {
124 unsigned char data[16];
128 {0x09, 0x46, 0x13, 0x46, 0x4c, 0x7f, 0x11, 0xd1,
129 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
132 {0x09, 0x46, 0x13, 0x41, 0x4c, 0x7f, 0x11, 0xd1,
133 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
136 {0x09, 0x46, 0x13, 0x45, 0x4c, 0x7f, 0x11, 0xd1,
137 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
140 {0x74, 0x8f, 0x24, 0x20, 0x62, 0x87, 0x11, 0xd1,
141 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
144 {0x09, 0x46, 0x13, 0x48, 0x4c, 0x7f, 0x11, 0xd1,
145 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
148 {0x09, 0x46, 0x13, 0x43, 0x4c, 0x7f, 0x11, 0xd1,
149 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
151 {AIM_CAPS_SAVESTOCKS,
152 {0x09, 0x46, 0x13, 0x47, 0x4c, 0x7f, 0x11, 0xd1,
153 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
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
162 {0x09, 0x46, 0x13, 0x4a, 0x4c, 0x7f, 0x11, 0xd1,
163 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
165 {0x09, 0x46, 0x13, 0x4a, 0x4c, 0x7f, 0x11, 0xd1,
166 0x22, 0x82, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
168 {AIM_CAPS_SENDBUDDYLIST,
169 {0x09, 0x46, 0x13, 0x4b, 0x4c, 0x7f, 0x11, 0xd1,
170 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}},
176 * This still takes a length parameter even with a bstream because capabilities
177 * are not naturally bounded.
180 faim_internal fu16_t aim_getcap(aim_session_t *sess, aim_bstream_t *bs, int len)
185 for (offset = 0; aim_bstream_empty(bs) && (offset < len); offset += 0x10) {
189 cap = aimbs_getraw(bs, 0x10);
191 for (i = 0, identified = 0; !(aim_caps[i].flag & AIM_CAPS_LAST); i++) {
193 if (memcmp(&aim_caps[i].data, cap, 0x10) == 0) {
194 flags |= aim_caps[i].flag;
196 break; /* should only match once... */
202 faimdprintf(sess, 0, "unknown capability!\n");
210 faim_internal int aim_putcap(aim_bstream_t *bs, fu16_t caps)
217 for (i = 0; aim_bstream_empty(bs); i++) {
219 if (aim_caps[i].flag == AIM_CAPS_LAST)
222 if (caps & aim_caps[i].flag)
223 aimbs_putraw(bs, aim_caps[i].data, 0x10);
231 * AIM is fairly regular about providing user info. This is a generic
232 * routine to extract it in its standard form.
234 faim_internal int aim_extractuserinfo(aim_session_t *sess, aim_bstream_t *bs, aim_userinfo_t *outinfo)
242 /* Clear out old data first */
243 memset(outinfo, 0x00, sizeof(aim_userinfo_t));
246 * Screen name. Stored as an unterminated string prepended with a
247 * byte containing its length.
249 snlen = aimbs_get8(bs);
250 aimbs_getrawbuf(bs, outinfo->sn, snlen);
253 * Warning Level. Stored as an unsigned short.
255 outinfo->warnlevel = aimbs_get16(bs);
258 * TLV Count. Unsigned short representing the number of
259 * Type-Length-Value triples that follow.
261 tlvcnt = aimbs_get16(bs);
264 * Parse out the Type-Length-Value triples as they're found.
266 for (curtlv = 0; curtlv < tlvcnt; curtlv++) {
270 type = aimbs_get16(bs);
271 length = aimbs_get16(bs);
273 endpos = aim_bstream_curpos(bs) + length;
275 if (type == 0x0001) {
277 * Type = 0x0001: User flags
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
289 outinfo->flags = aimbs_get16(bs);
291 } else if (type == 0x0002) {
293 * Type = 0x0002: Member-Since date.
295 * The time/date that the user originally registered for
296 * the service, stored in time_t format.
298 outinfo->membersince = aimbs_get32(bs);
300 } else if (type == 0x0003) {
302 * Type = 0x0003: On-Since date.
304 * The time/date that the user started their current
305 * session, stored in time_t format.
307 outinfo->onlinesince = aimbs_get32(bs);
309 } else if (type == 0x0004) {
311 * Type = 0x0004: Idle time.
313 * Number of seconds since the user actively used the
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.
320 outinfo->idletime = aimbs_get16(bs);
322 } else if (type == 0x0006) {
324 * Type = 0x0006: ICQ Online Status
326 * ICQ's Away/DND/etc "enriched" status. Some decoding
327 * of values done by Scott <darkagl@pcnet.com>
330 outinfo->icqinfo.status = aimbs_get16(bs);
332 } else if (type == 0x000a) {
336 * ICQ User IP Address.
337 * Ahh, the joy of ICQ security.
339 outinfo->icqinfo.ipaddr = aimbs_get32(bs);
341 } else if (type == 0x000c) {
345 * random crap containing the IP address,
346 * apparently a port number, and some Other Stuff.
349 aimbs_getrawbuf(bs, outinfo->icqinfo.crap, 0x25);
351 } else if (type == 0x000d) {
355 * Capability information.
358 outinfo->capabilities = aim_getcap(sess, bs, length);
359 outinfo->capspresent = 1;
361 } else if (type == 0x000e) {
365 * Unknown. Always of zero length, and always only
372 } else if ((type == 0x000f) || (type == 0x0010)) {
374 * Type = 0x000f: Session Length. (AIM)
375 * Type = 0x0010: Session Length. (AOL)
377 * The duration, in seconds, of the user's current
380 * Which TLV type this comes in depends on the
381 * service the user is using (AIM or AOL).
384 outinfo->sessionlen = aimbs_get32(bs);
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.
393 * Either way, inform the owner and attempt
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);
404 /* Save ourselves. */
405 aim_bstream_setpos(bs, endpos);
412 * Inverse of aim_extractuserinfo()
414 faim_internal int aim_putuserinfo(aim_bstream_t *bs, aim_userinfo_t *info)
416 aim_tlvlist_t *tlvlist = NULL;
421 aimbs_put8(bs, strlen(info->sn));
422 aimbs_putraw(bs, info->sn, strlen(info->sn));
424 aimbs_put16(bs, info->warnlevel);
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);
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);
439 aim_addtlvtochain_caps(&tlvlist, 0x000d, info->capabilities);
441 aim_addtlvtochain32(&tlvlist, (fu16_t)((info->flags & AIM_FLAG_AOL) ? 0x0010 : 0x000f), info->sessionlen);
443 aimbs_put16(bs, aim_counttlvchain(&tlvlist));
444 aim_writetlvchain(bs, &tlvlist);
445 aim_freetlvchain(&tlvlist);
450 faim_export int aim_sendbuddyoncoming(aim_session_t *sess, aim_conn_t *conn, aim_userinfo_t *info)
455 if (!sess || !conn || !info)
458 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 1152)))
461 snacid = aim_cachesnac(sess, 0x0003, 0x000b, 0x0000, NULL, 0);
463 aim_putsnac(&fr->data, 0x0003, 0x000b, 0x0000, snacid);
464 aim_putuserinfo(&fr->data, info);
466 aim_tx_enqueue(sess, fr);
471 faim_export int aim_sendbuddyoffgoing(aim_session_t *sess, aim_conn_t *conn, const char *sn)
476 if (!sess || !conn || !sn)
479 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+1+strlen(sn))))
482 snacid = aim_cachesnac(sess, 0x0003, 0x000c, 0x0000, NULL, 0);
484 aim_putsnac(&fr->data, 0x0003, 0x000c, 0x0000, snacid);
485 aimbs_put8(&fr->data, strlen(sn));
486 aimbs_putraw(&fr->data, sn, strlen(sn));
488 aim_tx_enqueue(sess, fr);
496 faim_export int aim_0002_000b(aim_session_t *sess, aim_conn_t *conn, const char *sn)
501 if (!sess || !conn || !sn)
504 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+1+strlen(sn))))
507 snacid = aim_cachesnac(sess, 0x0002, 0x000b, 0x0000, NULL, 0);
509 aim_putsnac(&fr->data, 0x0002, 0x000b, 0x0000, snacid);
510 aimbs_put8(&fr->data, strlen(sn));
511 aimbs_putraw(&fr->data, sn, strlen(sn));
513 aim_tx_enqueue(sess, fr);
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)
524 static int rights(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
526 aim_tlvlist_t *tlvlist;
527 aim_rxcallback_t userfunc;
529 fu16_t maxsiglen = 0;
531 tlvlist = aim_readtlvchain(bs);
533 if (aim_gettlv(tlvlist, 0x0001, 1))
534 maxsiglen = aim_gettlv16(tlvlist, 0x0001, 1);
536 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
537 ret = userfunc(sess, rx, maxsiglen);
539 aim_freetlvchain(&tlvlist);
544 static int userinfo(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
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;
554 origsnac = aim_remsnac(sess, snac->id);
556 if (!origsnac || !origsnac->data) {
557 faimdprintf(sess, 0, "parse_userinfo_middle: major problem: no snac stored!\n");
561 inforeq = (struct aim_priv_inforeq *)origsnac->data;
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);
569 aim_extractuserinfo(sess, bs, &userinfo);
571 tlvlist = aim_readtlvchain(bs);
574 * Depending on what informational text was requested, different
575 * TLVs will appear here.
577 * Profile will be 1 and 2, away message will be 3 and 4.
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);
587 if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
588 ret = userfunc(sess, rx, &userinfo, text_encoding, text, inforeq->infotype);
593 aim_freetlvchain(&tlvlist);
596 free(origsnac->data);
602 static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
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);
613 faim_internal int locate_modfirst(aim_session_t *sess, aim_module_t *mod)
616 mod->family = 0x0002;
617 mod->version = 0x0000;
619 strncpy(mod->name, "locate", sizeof(mod->name));
620 mod->snachandler = snachandler;