Mercurial > pidgin.yaz
annotate src/protocols/oscar/msgcookie.c @ 11162:ccb38cf22483
[gaim-migrate @ 13263]
Get rid of the last of the errors in oscar.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Fri, 29 Jul 2005 01:14:23 +0000 |
parents | 1a97d5e88d12 |
children | 7d31d61e6438 |
rev | line source |
---|---|
2086 | 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 * | |
5 */ | |
6 | |
7 /* | |
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. | |
12 */ | |
13 | |
14 #define FAIM_INTERNAL | |
15 #include <aim.h> | |
16 | |
17 /** | |
18 * aim_cachecookie - appends a cookie to the cookie list | |
19 * | |
20 * if cookie->cookie for type cookie->type is found, updates the | |
21 * ->addtime of the found structure; otherwise adds the given cookie | |
22 * to the cache | |
23 * | |
8866 | 24 * @param sess session to add to |
25 * @param cookie pointer to struct to append | |
26 * @return 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! | |
2086 | 28 */ |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
29 faim_internal int aim_cachecookie(aim_session_t *sess, aim_msgcookie_t *cookie) |
2086 | 30 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
31 aim_msgcookie_t *newcook; |
2086 | 32 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
33 if (!sess || !cookie) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
34 return -EINVAL; |
2086 | 35 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
36 newcook = aim_checkcookie(sess, cookie->cookie, cookie->type); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
37 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
38 if (newcook == cookie) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
39 newcook->addtime = time(NULL); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
40 return 1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
41 } else if (newcook) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
42 aim_cookie_free(sess, newcook); |
2086 | 43 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
44 cookie->addtime = time(NULL); |
2086 | 45 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
46 cookie->next = sess->msgcookies; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
47 sess->msgcookies = cookie; |
2086 | 48 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
49 return 0; |
2086 | 50 } |
51 | |
52 /** | |
53 * aim_uncachecookie - grabs a cookie from the cookie cache (removes it from the list) | |
54 * | |
55 * 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. | |
56 * | |
8866 | 57 * @param sess session to grab cookie from |
58 * @param cookie cookie string to look for | |
59 * @param type cookie type to look for | |
60 * @return if found, returns the struct; if none found (or on error), returns NULL: | |
2086 | 61 */ |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
62 faim_internal aim_msgcookie_t *aim_uncachecookie(aim_session_t *sess, fu8_t *cookie, int type) |
2086 | 63 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
64 aim_msgcookie_t *cur, **prev; |
2086 | 65 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
66 if (!cookie || !sess->msgcookies) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
67 return NULL; |
2086 | 68 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
69 for (prev = &sess->msgcookies; (cur = *prev); ) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
70 if ((cur->type == type) && |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
71 (memcmp(cur->cookie, cookie, 8) == 0)) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
72 *prev = cur->next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
73 return cur; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
74 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
75 prev = &cur->next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
76 } |
2086 | 77 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
78 return NULL; |
2086 | 79 } |
80 | |
81 /** | |
82 * aim_mkcookie - generate an aim_msgcookie_t *struct from a cookie string, a type, and a data pointer. | |
83 * | |
8866 | 84 * @param c pointer to the cookie string array |
85 * @param type cookie type to use | |
86 * @param data data to be cached with the cookie | |
87 * @return returns NULL on error, a pointer to the newly-allocated | |
88 * cookie on success. | |
2086 | 89 */ |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
90 faim_internal aim_msgcookie_t *aim_mkcookie(fu8_t *c, int type, void *data) |
2086 | 91 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
92 aim_msgcookie_t *cookie; |
2086 | 93 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
94 if (!c) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
95 return NULL; |
2086 | 96 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
97 if (!(cookie = calloc(1, sizeof(aim_msgcookie_t)))) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
98 return NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
99 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
100 cookie->data = data; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
101 cookie->type = type; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
102 memcpy(cookie->cookie, c, 8); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
103 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
104 return cookie; |
2086 | 105 } |
106 | |
107 /** | |
108 * aim_checkcookie - check to see if a cookietuple has been cached | |
109 * | |
8866 | 110 * @param sess session to check for the cookie in |
111 * @param cookie pointer to the cookie string array | |
112 * @param type type of the cookie to look for | |
113 * @return returns a pointer to the cookie struct (still in the list) | |
114 * on success; returns NULL on error/not found | |
2086 | 115 */ |
116 | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
117 faim_internal aim_msgcookie_t *aim_checkcookie(aim_session_t *sess, const fu8_t *cookie, int type) |
2086 | 118 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
119 aim_msgcookie_t *cur; |
2086 | 120 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
121 for (cur = sess->msgcookies; cur; cur = cur->next) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
122 if ((cur->type == type) && |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
123 (memcmp(cur->cookie, cookie, 8) == 0)) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
124 return cur; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
125 } |
2086 | 126 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
127 return NULL; |
2086 | 128 } |
129 | |
130 #if 0 /* debugging feature */ | |
4159 | 131 faim_internal int aim_dumpcookie(aim_session_t *sess, aim_msgcookie_t *cookie) |
2086 | 132 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
133 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
134 if (!cookie) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
135 return -EINVAL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
136 |
4159 | 137 faimdprintf(sess, 0, "\tCookie at %p: %d/%s with %p, next %p\n", cookie, |
138 cookie->type, cookie->cookie, cookie->data, cookie->next); | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
139 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
140 return 0; |
2086 | 141 } |
142 #endif | |
143 | |
144 /** | |
145 * aim_cookie_free - free an aim_msgcookie_t struct | |
146 * | |
10504 | 147 * this function removes the cookie *cookie from the list of cookies |
2086 | 148 * in sess, and then frees all memory associated with it. including |
149 * its data! if you want to use the private data after calling this, | |
150 * make sure you copy it first. | |
151 * | |
8866 | 152 * @param sess session to remove the cookie from |
153 * @param cookie the address of a pointer to the cookie struct to remove | |
154 * @return returns -1 on error, 0 on success. | |
2086 | 155 * |
156 */ | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
157 faim_internal int aim_cookie_free(aim_session_t *sess, aim_msgcookie_t *cookie) |
2086 | 158 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
159 aim_msgcookie_t *cur, **prev; |
2086 | 160 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
161 if (!sess || !cookie) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
162 return -EINVAL; |
2086 | 163 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
164 for (prev = &sess->msgcookies; (cur = *prev); ) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
165 if (cur == cookie) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
166 *prev = cur->next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
167 else |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
168 prev = &cur->next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
169 } |
2086 | 170 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
171 free(cookie->data); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
172 free(cookie); |
2086 | 173 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
174 return 0; |
2086 | 175 } |
176 | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
177 /* XXX I hate switch */ |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
178 faim_internal int aim_msgcookie_gettype(int reqclass) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
179 { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
180 /* XXX: hokey-assed. needs fixed. */ |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
181 switch(reqclass) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
182 case AIM_CAPS_BUDDYICON: return AIM_COOKIETYPE_OFTICON; |
8092 | 183 case AIM_CAPS_TALK: return AIM_COOKIETYPE_OFTVOICE; |
6871 | 184 case AIM_CAPS_DIRECTIM: return AIM_COOKIETYPE_OFTIMAGE; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
185 case AIM_CAPS_CHAT: return AIM_COOKIETYPE_CHAT; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
186 case AIM_CAPS_GETFILE: return AIM_COOKIETYPE_OFTGET; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
187 case AIM_CAPS_SENDFILE: return AIM_COOKIETYPE_OFTSEND; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
188 default: return AIM_COOKIETYPE_UNKNOWN; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
189 } |
2086 | 190 } |