]>
Commit | Line | Data |
---|---|---|
7392c79f | 1 | /* |
2 | * Cookie Caching stuff. Adam wrote this, apparently just some | |
3 | * derivatives of n's SNAC work. I cleaned it up, added comments. | |
4 | * | |
7392c79f | 5 | */ |
040457cc | 6 | |
7 | /* | |
7392c79f | 8 | * I'm assuming that cookies are type-specific. that is, we can have |
9 | * "1234578" for type 1 and type 2 concurrently. if i'm wrong, then we | |
10 | * lose some error checking. if we assume cookies are not type-specific and are | |
11 | * wrong, we get quirky behavior when cookies step on each others' toes. | |
040457cc | 12 | */ |
13 | ||
37ee990e | 14 | #define FAIM_INTERNAL |
dd60ff8b | 15 | #include <aim.h> |
040457cc | 16 | |
646c6b52 | 17 | /** |
18 | * aim_cachecookie - appends a cookie to the cookie list | |
19 | * @sess: session to add to | |
20 | * @cookie: pointer to struct to append | |
21 | * | |
22 | * if cookie->cookie for type cookie->type is found, updates the | |
23 | * ->addtime of the found structure; otherwise adds the given cookie | |
24 | * to the cache | |
25 | * | |
26 | * returns -1 on error, 0 on append, 1 on update. the cookie you pass | |
27 | * in may be free'd, so don't count on its value after calling this! | |
7392c79f | 28 | * |
fd0b7da6 | 29 | */ |
d410cf58 | 30 | faim_internal int aim_cachecookie(aim_session_t *sess, aim_msgcookie_t *cookie) |
040457cc | 31 | { |
d410cf58 | 32 | aim_msgcookie_t *newcook; |
fd0b7da6 | 33 | |
d410cf58 | 34 | if (!sess || !cookie) |
35 | return -EINVAL; | |
040457cc | 36 | |
d410cf58 | 37 | newcook = aim_checkcookie(sess, cookie->cookie, cookie->type); |
38 | ||
39 | if (newcook == cookie) { | |
40 | newcook->addtime = time(NULL); | |
41 | return 1; | |
42 | } else if (newcook) | |
43 | aim_cookie_free(sess, newcook); | |
646c6b52 | 44 | |
d410cf58 | 45 | cookie->addtime = time(NULL); |
646c6b52 | 46 | |
d410cf58 | 47 | cookie->next = sess->msgcookies; |
48 | sess->msgcookies = cookie; | |
7392c79f | 49 | |
d410cf58 | 50 | return 0; |
040457cc | 51 | } |
52 | ||
646c6b52 | 53 | /** |
54 | * aim_uncachecookie - grabs a cookie from the cookie cache (removes it from the list) | |
55 | * @sess: session to grab cookie from | |
56 | * @cookie: cookie string to look for | |
57 | * @type: cookie type to look for | |
58 | * | |
59 | * takes a cookie string and a cookie type and finds the cookie struct associated with that duple, removing it from the cookie list ikn the process. | |
60 | * | |
61 | * if found, returns the struct; if none found (or on error), returns NULL: | |
7392c79f | 62 | */ |
d410cf58 | 63 | faim_internal aim_msgcookie_t *aim_uncachecookie(aim_session_t *sess, fu8_t *cookie, int type) |
040457cc | 64 | { |
d410cf58 | 65 | aim_msgcookie_t *cur, **prev; |
040457cc | 66 | |
d410cf58 | 67 | if (!cookie || !sess->msgcookies) |
68 | return NULL; | |
040457cc | 69 | |
d410cf58 | 70 | for (prev = &sess->msgcookies; (cur = *prev); ) { |
71 | if ((cur->type == type) && | |
72 | (memcmp(cur->cookie, cookie, 8) == 0)) { | |
73 | *prev = cur->next; | |
74 | return cur; | |
75 | } | |
76 | prev = &cur->next; | |
77 | } | |
fd0b7da6 | 78 | |
d410cf58 | 79 | return NULL; |
040457cc | 80 | } |
81 | ||
646c6b52 | 82 | /** |
83 | * aim_mkcookie - generate an aim_msgcookie_t *struct from a cookie string, a type, and a data pointer. | |
84 | * @c: pointer to the cookie string array | |
85 | * @type: cookie type to use | |
86 | * @data: data to be cached with the cookie | |
87 | * | |
88 | * returns NULL on error, a pointer to the newly-allocated cookie on | |
89 | * success. | |
90 | * | |
91 | */ | |
d410cf58 | 92 | faim_internal aim_msgcookie_t *aim_mkcookie(fu8_t *c, int type, void *data) |
7392c79f | 93 | { |
d410cf58 | 94 | aim_msgcookie_t *cookie; |
95 | ||
96 | if (!c) | |
97 | return NULL; | |
98 | ||
99 | if (!(cookie = calloc(1, sizeof(aim_msgcookie_t)))) | |
100 | return NULL; | |
101 | ||
102 | cookie->data = data; | |
103 | cookie->type = type; | |
104 | memcpy(cookie->cookie, c, 8); | |
105 | ||
106 | return cookie; | |
7392c79f | 107 | } |
646c6b52 | 108 | |
109 | /** | |
110 | * aim_checkcookie - check to see if a cookietuple has been cached | |
111 | * @sess: session to check for the cookie in | |
112 | * @cookie: pointer to the cookie string array | |
113 | * @type: type of the cookie to look for | |
114 | * | |
115 | * this returns a pointer to the cookie struct (still in the list) on | |
116 | * success; returns NULL on error/not found | |
117 | * | |
118 | */ | |
119 | ||
d410cf58 | 120 | faim_internal aim_msgcookie_t *aim_checkcookie(aim_session_t *sess, const fu8_t *cookie, int type) |
7392c79f | 121 | { |
d410cf58 | 122 | aim_msgcookie_t *cur; |
37ee990e | 123 | |
d410cf58 | 124 | for (cur = sess->msgcookies; cur; cur = cur->next) { |
125 | if ((cur->type == type) && | |
126 | (memcmp(cur->cookie, cookie, 8) == 0)) | |
127 | return cur; | |
128 | } | |
7392c79f | 129 | |
d410cf58 | 130 | return NULL; |
7392c79f | 131 | } |
132 | ||
646c6b52 | 133 | #if 0 /* debugging feature */ |
d410cf58 | 134 | faim_internal int aim_dumpcookie(aim_msgcookie_t *cookie) |
646c6b52 | 135 | { |
d410cf58 | 136 | |
137 | if (!cookie) | |
138 | return -EINVAL; | |
139 | ||
140 | printf("\tCookie at %p: %d/%s with %p, next %p\n", | |
141 | cookie, cookie->type, cookie->cookie, | |
142 | cookie->data, cookie->next); | |
143 | ||
144 | return 0; | |
646c6b52 | 145 | } |
146 | #endif | |
147 | ||
148 | /** | |
149 | * aim_cookie_free - free an aim_msgcookie_t struct | |
150 | * @sess: session to remove the cookie from | |
151 | * @cookiep: the address of a pointer to the cookie struct to remove | |
152 | * | |
153 | * this function removes the cookie *cookie from teh list of cookies | |
154 | * in sess, and then frees all memory associated with it. including | |
155 | * its data! if you want to use the private data after calling this, | |
156 | * make sure you copy it first. | |
157 | * | |
158 | * returns -1 on error, 0 on success. | |
159 | * | |
160 | */ | |
d410cf58 | 161 | faim_internal int aim_cookie_free(aim_session_t *sess, aim_msgcookie_t *cookie) |
646c6b52 | 162 | { |
d410cf58 | 163 | aim_msgcookie_t *cur, **prev; |
646c6b52 | 164 | |
d410cf58 | 165 | if (!sess || !cookie) |
166 | return -EINVAL; | |
37ee990e | 167 | |
d410cf58 | 168 | for (prev = &sess->msgcookies; (cur = *prev); ) { |
169 | if (cur == cookie) | |
170 | *prev = cur->next; | |
171 | else | |
172 | prev = &cur->next; | |
173 | } | |
646c6b52 | 174 | |
d410cf58 | 175 | free(cookie->data); |
176 | free(cookie); | |
37ee990e | 177 | |
d410cf58 | 178 | return 0; |
7392c79f | 179 | } |
180 | ||
d410cf58 | 181 | /* XXX I hate switch */ |
182 | faim_internal int aim_msgcookie_gettype(int reqclass) | |
183 | { | |
184 | /* XXX: hokey-assed. needs fixed. */ | |
185 | switch(reqclass) { | |
186 | case AIM_CAPS_BUDDYICON: return AIM_COOKIETYPE_OFTICON; | |
187 | case AIM_CAPS_VOICE: return AIM_COOKIETYPE_OFTVOICE; | |
188 | case AIM_CAPS_IMIMAGE: return AIM_COOKIETYPE_OFTIMAGE; | |
189 | case AIM_CAPS_CHAT: return AIM_COOKIETYPE_CHAT; | |
190 | case AIM_CAPS_GETFILE: return AIM_COOKIETYPE_OFTGET; | |
191 | case AIM_CAPS_SENDFILE: return AIM_COOKIETYPE_OFTSEND; | |
192 | default: return AIM_COOKIETYPE_UNKNOWN; | |
193 | } | |
7392c79f | 194 | } |
d410cf58 | 195 | |
196 |