2 * Cookie Caching stuff. Adam wrote this, apparently just some
3 * derivatives of n's SNAC work. I cleaned it up, added comments.
5 * I'm going to rewrite this stuff eventually, honest. -jbm
10 * I'm assuming that cookies are type-specific. that is, we can have
11 * "1234578" for type 1 and type 2 concurrently. if i'm wrong, then we
12 * lose some error checking. if we assume cookies are not type-specific and are
13 * wrong, we get quirky behavior when cookies step on each others' toes.
20 * appends a cookie to the cookie list for sess.
21 * - if cookie->cookie for type cookie->type is found, addtime is updated.
22 * - copies cookie struct; you need to free() it afterwards;
23 * - cookie->data is not copied, but passed along. don't free it.
24 * - newcook->addtime is updated accordingly;
25 * - cookie->type is just passed across.
27 * returns -1 on error, 0 on success. */
29 faim_internal int aim_cachecookie(struct aim_session_t *sess,
30 struct aim_msgcookie_t *cookie)
32 struct aim_msgcookie_t *newcook = NULL, *cur = NULL;
37 if( (newcook = aim_checkcookie(sess, cookie->cookie, cookie->type)) ) {
38 newcook->addtime = time(NULL);
39 if(cookie->data != newcook->data) {
41 printf("faim: cachecookie: matching cookie/type pair "
42 "%x%x%x%x%x%x%x%x/%x has different *data. free()ing cookie copy..\n",
43 cookie->cookie[0], cookie->cookie[1], cookie->cookie[2],
44 cookie->cookie[3], cookie->cookie[4], cookie->cookie[5],
45 cookie->cookie[6], cookie->cookie[7], cookie->type);
52 if (!(newcook = malloc(sizeof(struct aim_msgcookie_t))))
54 memcpy(newcook, cookie, sizeof(struct aim_msgcookie_t));
55 newcook->addtime = time(NULL);
58 printf("faim: cachecookie: newcook->next isn't NULL ???\n");
62 cur = sess->msgcookies;
65 sess->msgcookies = newcook;
69 while (cur->next != NULL)
78 * takes a cookie string and grabs the cookie struct associated with
79 * it. removes struct from chain. returns the struct if found, or
83 faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type)
85 struct aim_msgcookie_t *cur;
87 if (!cookie || !sess->msgcookies)
90 cur = sess->msgcookies;
92 if ( (memcmp(cur->cookie, cookie, 8) == 0) && (cur->type == type) ) {
93 sess->msgcookies = cur->next;
98 if ( (memcmp(cur->next->cookie, cookie, 8) == 0) && (cur->next->type == type) ) {
99 struct aim_msgcookie_t *tmp;
102 cur->next = cur->next->next;
112 * purge out old cookies
114 * finds old cookies, calls uncache on them.
116 * this is highly inefficient, but It Works. and i don't feel like
117 * totally rewriting this. it might have some concurrency issues as
118 * well, if i rewrite it.
120 * i'll avoid the puns.
123 faim_export int aim_purgecookies(struct aim_session_t *sess, int maxage)
125 struct aim_msgcookie_t *cur;
126 struct aim_msgcookie_t *remed = NULL;
129 cur = sess->msgcookies;
131 curtime = time(&curtime);
134 if ( (cur->addtime) > (curtime - maxage) ) {
136 printf("aimmsgcookie: WARNING purged obsolete message cookie %x%x%x%x %x%x%x%x\n",
137 cur->cookie[0], cur->cookie[1], cur->cookie[2], cur->cookie[3],
138 cur->cookie[4], cur->cookie[5], cur->cookie[6], cur->cookie[7]);
141 remed = aim_uncachecookie(sess, cur->cookie, cur->type);
156 faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data)
158 struct aim_msgcookie_t *cookie;
163 if( (cookie = calloc(1, sizeof(struct aim_msgcookie_t))) == NULL)
170 memcpy(cookie->cookie, c, 8);
175 faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, unsigned char *cookie, int type)
177 struct aim_msgcookie_t *cur;
179 if(!sess->msgcookies)
182 cur = sess->msgcookies;
184 if( (memcmp(cur->cookie, cookie, 8) == 0) && (cur->type == type))
187 while( (cur = cur->next) )
188 if( (memcmp(cur->cookie, cookie, 8) == 0) && (cur->type == type))
194 static int aim_freecookie(struct aim_msgcookie_t *cookie) {
198 faim_internal int aim_msgcookie_gettype(int reqclass) {
199 /* XXX: hokey-assed. needs fixed. */
201 case AIM_CAPS_BUDDYICON:
202 return AIM_COOKIETYPE_OFTICON;
205 return AIM_COOKIETYPE_OFTVOICE;
207 case AIM_CAPS_IMIMAGE:
208 return AIM_COOKIETYPE_OFTIMAGE;
211 return AIM_COOKIETYPE_CHAT;
213 case AIM_CAPS_GETFILE:
214 return AIM_COOKIETYPE_OFTGET;
216 case AIM_CAPS_SENDFILE:
217 return AIM_COOKIETYPE_OFTSEND;
220 return AIM_COOKIETYPE_UNKNOWN;