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