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