Mercurial > pidgin
annotate src/protocols/oscar/msgcookie.c @ 13239:f260d319bbbc
[gaim-migrate @ 15605]
Renaming a bunch of structs and typedefs to use the same naming
scheme as the rest of Gaim
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sun, 12 Feb 2006 16:02:05 +0000 |
parents | f2431a7e33aa |
children | 6519aeb66b31 |
rev | line source |
---|---|
13234 | 1 /* |
2 * Gaim's oscar protocol plugin | |
3 * This file is the legal property of its developers. | |
4 * Please see the AUTHORS file distributed alongside this file. | |
5 * | |
6 * This library is free software; you can redistribute it and/or | |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 2 of the License, or (at your option) any later version. | |
10 * | |
11 * This library is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Lesser General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Lesser General Public | |
17 * License along with this library; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 */ | |
20 | |
2086 | 21 /* |
22 * Cookie Caching stuff. Adam wrote this, apparently just some | |
23 * derivatives of n's SNAC work. I cleaned it up, added comments. | |
11399 | 24 * |
2086 | 25 */ |
26 | |
27 /* | |
28 * I'm assuming that cookies are type-specific. that is, we can have | |
29 * "1234578" for type 1 and type 2 concurrently. if i'm wrong, then we | |
30 * lose some error checking. if we assume cookies are not type-specific and are | |
31 * wrong, we get quirky behavior when cookies step on each others' toes. | |
32 */ | |
33 | |
13234 | 34 #include "oscar.h" |
2086 | 35 |
36 /** | |
37 * aim_cachecookie - appends a cookie to the cookie list | |
38 * | |
39 * if cookie->cookie for type cookie->type is found, updates the | |
40 * ->addtime of the found structure; otherwise adds the given cookie | |
41 * to the cache | |
42 * | |
8866 | 43 * @param sess session to add to |
44 * @param cookie pointer to struct to append | |
45 * @return returns -1 on error, 0 on append, 1 on update. the cookie you pass | |
46 * in may be free'd, so don't count on its value after calling this! | |
2086 | 47 */ |
13239 | 48 faim_internal int aim_cachecookie(OscarSession *sess, IcbmCookie *cookie) |
2086 | 49 { |
13239 | 50 IcbmCookie *newcook; |
2086 | 51 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
52 if (!sess || !cookie) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
53 return -EINVAL; |
2086 | 54 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
55 newcook = aim_checkcookie(sess, cookie->cookie, cookie->type); |
11399 | 56 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
57 if (newcook == cookie) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
58 newcook->addtime = time(NULL); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
59 return 1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
60 } else if (newcook) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
61 aim_cookie_free(sess, newcook); |
2086 | 62 |
11399 | 63 cookie->addtime = time(NULL); |
2086 | 64 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
65 cookie->next = sess->msgcookies; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
66 sess->msgcookies = cookie; |
2086 | 67 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
68 return 0; |
2086 | 69 } |
70 | |
71 /** | |
72 * aim_uncachecookie - grabs a cookie from the cookie cache (removes it from the list) | |
73 * | |
74 * 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. | |
75 * | |
8866 | 76 * @param sess session to grab cookie from |
77 * @param cookie cookie string to look for | |
78 * @param type cookie type to look for | |
79 * @return if found, returns the struct; if none found (or on error), returns NULL: | |
2086 | 80 */ |
13239 | 81 faim_internal IcbmCookie *aim_uncachecookie(OscarSession *sess, guint8 *cookie, int type) |
2086 | 82 { |
13239 | 83 IcbmCookie *cur, **prev; |
2086 | 84 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
85 if (!cookie || !sess->msgcookies) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
86 return NULL; |
2086 | 87 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
88 for (prev = &sess->msgcookies; (cur = *prev); ) { |
11399 | 89 if ((cur->type == type) && |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
90 (memcmp(cur->cookie, cookie, 8) == 0)) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
91 *prev = cur->next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
92 return cur; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
93 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
94 prev = &cur->next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
95 } |
2086 | 96 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
97 return NULL; |
2086 | 98 } |
99 | |
100 /** | |
13239 | 101 * aim_mkcookie - generate an IcbmCookie *struct from a cookie string, a type, and a data pointer. |
2086 | 102 * |
8866 | 103 * @param c pointer to the cookie string array |
104 * @param type cookie type to use | |
105 * @param data data to be cached with the cookie | |
106 * @return returns NULL on error, a pointer to the newly-allocated | |
107 * cookie on success. | |
2086 | 108 */ |
13239 | 109 faim_internal IcbmCookie *aim_mkcookie(guint8 *c, int type, void *data) |
2086 | 110 { |
13239 | 111 IcbmCookie *cookie; |
2086 | 112 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
113 if (!c) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
114 return NULL; |
2086 | 115 |
13239 | 116 if (!(cookie = calloc(1, sizeof(IcbmCookie)))) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
117 return NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
118 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
119 cookie->data = data; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
120 cookie->type = type; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
121 memcpy(cookie->cookie, c, 8); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
122 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
123 return cookie; |
2086 | 124 } |
125 | |
126 /** | |
127 * aim_checkcookie - check to see if a cookietuple has been cached | |
128 * | |
8866 | 129 * @param sess session to check for the cookie in |
130 * @param cookie pointer to the cookie string array | |
131 * @param type type of the cookie to look for | |
132 * @return returns a pointer to the cookie struct (still in the list) | |
133 * on success; returns NULL on error/not found | |
2086 | 134 */ |
135 | |
13239 | 136 faim_internal IcbmCookie *aim_checkcookie(OscarSession *sess, const guint8 *cookie, int type) |
2086 | 137 { |
13239 | 138 IcbmCookie *cur; |
2086 | 139 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
140 for (cur = sess->msgcookies; cur; cur = cur->next) { |
11399 | 141 if ((cur->type == type) && |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
142 (memcmp(cur->cookie, cookie, 8) == 0)) |
11399 | 143 return cur; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
144 } |
2086 | 145 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
146 return NULL; |
2086 | 147 } |
148 | |
149 /** | |
13239 | 150 * aim_cookie_free - free an IcbmCookie struct |
2086 | 151 * |
10504 | 152 * this function removes the cookie *cookie from the list of cookies |
2086 | 153 * in sess, and then frees all memory associated with it. including |
154 * its data! if you want to use the private data after calling this, | |
155 * make sure you copy it first. | |
156 * | |
8866 | 157 * @param sess session to remove the cookie from |
158 * @param cookie the address of a pointer to the cookie struct to remove | |
159 * @return returns -1 on error, 0 on success. | |
2086 | 160 * |
161 */ | |
13239 | 162 faim_internal int aim_cookie_free(OscarSession *sess, IcbmCookie *cookie) |
2086 | 163 { |
13239 | 164 IcbmCookie *cur, **prev; |
2086 | 165 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
166 if (!sess || !cookie) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
167 return -EINVAL; |
2086 | 168 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
169 for (prev = &sess->msgcookies; (cur = *prev); ) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
170 if (cur == cookie) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
171 *prev = cur->next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
172 else |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
173 prev = &cur->next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
174 } |
2086 | 175 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
176 free(cookie->data); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
177 free(cookie); |
2086 | 178 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
179 return 0; |
11399 | 180 } |
2086 | 181 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
182 /* XXX I hate switch */ |
11399 | 183 faim_internal int aim_msgcookie_gettype(int reqclass) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
184 { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
185 /* XXX: hokey-assed. needs fixed. */ |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
186 switch(reqclass) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
187 case AIM_CAPS_BUDDYICON: return AIM_COOKIETYPE_OFTICON; |
8092 | 188 case AIM_CAPS_TALK: return AIM_COOKIETYPE_OFTVOICE; |
6871 | 189 case AIM_CAPS_DIRECTIM: return AIM_COOKIETYPE_OFTIMAGE; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
190 case AIM_CAPS_CHAT: return AIM_COOKIETYPE_CHAT; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
191 case AIM_CAPS_GETFILE: return AIM_COOKIETYPE_OFTGET; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
192 case AIM_CAPS_SENDFILE: return AIM_COOKIETYPE_OFTSEND; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
193 default: return AIM_COOKIETYPE_UNKNOWN; |
11399 | 194 } |
2086 | 195 } |