1 | |
---|
2 | /* |
---|
3 | * aim_search.c |
---|
4 | * |
---|
5 | * TODO: Add aim_usersearch_name() |
---|
6 | * |
---|
7 | */ |
---|
8 | |
---|
9 | #include <aim.h> |
---|
10 | |
---|
11 | int aim_usersearch_address(aim_session_t *sess, aim_conn_t *conn, const char *address) |
---|
12 | { |
---|
13 | aim_frame_t *fr; |
---|
14 | aim_snacid_t snacid; |
---|
15 | |
---|
16 | if (!sess || !conn || !address) |
---|
17 | return -EINVAL; |
---|
18 | |
---|
19 | if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+strlen(address)))) |
---|
20 | return -ENOMEM; |
---|
21 | |
---|
22 | snacid = aim_cachesnac(sess, 0x000a, 0x0002, 0x0000, g_strdup(address), strlen(address)+1); |
---|
23 | aim_putsnac(&fr->data, 0x000a, 0x0002, 0x0000, snacid); |
---|
24 | |
---|
25 | aimbs_putraw(&fr->data, (guint8 *)address, strlen(address)); |
---|
26 | |
---|
27 | aim_tx_enqueue(sess, fr); |
---|
28 | |
---|
29 | return 0; |
---|
30 | } |
---|
31 | |
---|
32 | /* XXX can this be integrated with the rest of the error handling? */ |
---|
33 | static int error(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs) |
---|
34 | { |
---|
35 | int ret = 0; |
---|
36 | aim_rxcallback_t userfunc; |
---|
37 | aim_snac_t *snac2; |
---|
38 | |
---|
39 | /* XXX the modules interface should have already retrieved this for us */ |
---|
40 | if (!(snac2 = aim_remsnac(sess, snac->id))) { |
---|
41 | imcb_error(sess->aux_data, "couldn't get snac"); |
---|
42 | return 0; |
---|
43 | } |
---|
44 | |
---|
45 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) |
---|
46 | ret = userfunc(sess, rx, snac2->data /* address */); |
---|
47 | |
---|
48 | /* XXX freesnac()? */ |
---|
49 | if (snac2) |
---|
50 | g_free(snac2->data); |
---|
51 | g_free(snac2); |
---|
52 | |
---|
53 | return ret; |
---|
54 | } |
---|
55 | |
---|
56 | static int reply(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs) |
---|
57 | { |
---|
58 | int j = 0, m, ret = 0; |
---|
59 | aim_tlvlist_t *tlvlist; |
---|
60 | char *cur = NULL, *buf = NULL; |
---|
61 | aim_rxcallback_t userfunc; |
---|
62 | aim_snac_t *snac2; |
---|
63 | char *searchaddr = NULL; |
---|
64 | |
---|
65 | if ((snac2 = aim_remsnac(sess, snac->id))) |
---|
66 | searchaddr = (char *)snac2->data; |
---|
67 | |
---|
68 | tlvlist = aim_readtlvchain(bs); |
---|
69 | m = aim_counttlvchain(&tlvlist); |
---|
70 | |
---|
71 | /* XXX uhm. */ |
---|
72 | while ((cur = aim_gettlv_str(tlvlist, 0x0001, j+1)) && j < m) { |
---|
73 | buf = g_realloc(buf, (j+1) * (MAXSNLEN+1)); |
---|
74 | |
---|
75 | strncpy(&buf[j * (MAXSNLEN+1)], cur, MAXSNLEN); |
---|
76 | g_free(cur); |
---|
77 | |
---|
78 | j++; |
---|
79 | } |
---|
80 | |
---|
81 | aim_freetlvchain(&tlvlist); |
---|
82 | |
---|
83 | if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) |
---|
84 | ret = userfunc(sess, rx, searchaddr, j, buf); |
---|
85 | |
---|
86 | /* XXX freesnac()? */ |
---|
87 | if (snac2) |
---|
88 | g_free(snac2->data); |
---|
89 | g_free(snac2); |
---|
90 | |
---|
91 | g_free(buf); |
---|
92 | |
---|
93 | return ret; |
---|
94 | } |
---|
95 | |
---|
96 | static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs) |
---|
97 | { |
---|
98 | |
---|
99 | if (snac->subtype == 0x0001) |
---|
100 | return error(sess, mod, rx, snac, bs); |
---|
101 | else if (snac->subtype == 0x0003) |
---|
102 | return reply(sess, mod, rx, snac, bs); |
---|
103 | |
---|
104 | return 0; |
---|
105 | } |
---|
106 | |
---|
107 | int search_modfirst(aim_session_t *sess, aim_module_t *mod) |
---|
108 | { |
---|
109 | |
---|
110 | mod->family = 0x000a; |
---|
111 | mod->version = 0x0001; |
---|
112 | mod->toolid = 0x0110; |
---|
113 | mod->toolversion = 0x0629; |
---|
114 | mod->flags = 0; |
---|
115 | strncpy(mod->name, "search", sizeof(mod->name)); |
---|
116 | mod->snachandler = snachandler; |
---|
117 | |
---|
118 | return 0; |
---|
119 | } |
---|
120 | |
---|
121 | |
---|