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;
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);
53 if (!(newcook = malloc(sizeof(struct aim_msgcookie_t))))
55 memcpy(newcook, cookie, sizeof(struct aim_msgcookie_t));
56 newcook->addtime = time(NULL);
58 newcook->next = sess->msgcookies;
59 sess->msgcookies = newcook;
66 * takes a cookie string and grabs the cookie struct associated with
67 * it. removes struct from chain. returns the struct if found, or
70 faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type)
72 struct aim_msgcookie_t *cur;
74 if (!cookie || !sess->msgcookies)
77 if ((sess->msgcookies->type == type) &&
78 (memcmp(sess->msgcookies->cookie, cookie, 8) == 0)) {
79 struct aim_msgcookie_t *tmp;
81 tmp = sess->msgcookies;
82 sess->msgcookies = sess->msgcookies->next;
87 for (cur = sess->msgcookies; cur->next; cur = cur->next) {
88 if ((cur->next->type == type) &&
89 (memcmp(cur->next->cookie, cookie, 8) == 0)) {
90 struct aim_msgcookie_t *tmp;
93 cur->next = cur->next->next;
104 * purge out old cookies
106 * finds old cookies, calls uncache on them.
108 * this is highly inefficient, but It Works. and i don't feel like
109 * totally rewriting this. it might have some concurrency issues as
110 * well, if i rewrite it.
112 * i'll avoid the puns.
115 faim_export int aim_purgecookies(struct aim_session_t *sess, int maxage)
117 struct aim_msgcookie_t *cur;
120 curtime = time(NULL);
122 for (cur = sess->msgcookies; cur; cur = cur->next) {
123 if (cur->addtime > (time(NULL) - maxage)) {
124 struct aim_msgcookie_t *remed = NULL;
127 printf("aimmsgcookie: WARNING purged obsolete message cookie %x%x%x%x %x%x%x%x\n",
128 cur->cookie[0], cur->cookie[1], cur->cookie[2], cur->cookie[3],
129 cur->cookie[4], cur->cookie[5], cur->cookie[6], cur->cookie[7]);
132 remed = aim_uncachecookie(sess, cur->cookie, cur->type);
144 faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data)
146 struct aim_msgcookie_t *cookie;
151 if (!(cookie = calloc(1, sizeof(struct aim_msgcookie_t))))
156 memcpy(cookie->cookie, c, 8);
161 faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, unsigned char *cookie, int type)
163 struct aim_msgcookie_t *cur;
165 for (cur = sess->msgcookies; cur; cur = cur->next) {
166 if ((cur->type == type) &&
167 (memcmp(cur->cookie, cookie, 8) == 0))
174 static int aim_freecookie(struct aim_msgcookie_t *cookie) {
178 faim_internal int aim_msgcookie_gettype(int reqclass) {
179 /* XXX: hokey-assed. needs fixed. */
181 case AIM_CAPS_BUDDYICON:
182 return AIM_COOKIETYPE_OFTICON;
185 return AIM_COOKIETYPE_OFTVOICE;
187 case AIM_CAPS_IMIMAGE:
188 return AIM_COOKIETYPE_OFTIMAGE;
191 return AIM_COOKIETYPE_CHAT;
193 case AIM_CAPS_GETFILE:
194 return AIM_COOKIETYPE_OFTGET;
196 case AIM_CAPS_SENDFILE:
197 return AIM_COOKIETYPE_OFTSEND;
200 return AIM_COOKIETYPE_UNKNOWN;