]>
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 | * | |
5 | * I'm going to rewrite this stuff eventually, honest. -jbm | |
6 | * | |
7 | */ | |
040457cc | 8 | |
9 | /* | |
7392c79f | 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. | |
040457cc | 14 | */ |
15 | ||
16 | #include <faim/aim.h> | |
17 | ||
7392c79f | 18 | /* |
19 | * aim_cachecookie: | |
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. | |
26 | * | |
fd0b7da6 | 27 | * returns -1 on error, 0 on success. |
28 | */ | |
78b3fb13 | 29 | faim_internal int aim_cachecookie(struct aim_session_t *sess, |
30 | struct aim_msgcookie_t *cookie) | |
040457cc | 31 | { |
fd0b7da6 | 32 | struct aim_msgcookie_t *newcook; |
33 | ||
040457cc | 34 | if (!cookie) |
35 | return -1; | |
36 | ||
7392c79f | 37 | if( (newcook = aim_checkcookie(sess, cookie->cookie, cookie->type)) ) { |
38 | newcook->addtime = time(NULL); | |
39 | if(cookie->data != newcook->data) { | |
40 | ||
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); | |
46 | ||
47 | free(cookie->data); | |
48 | } | |
fd0b7da6 | 49 | |
50 | return 0; | |
7392c79f | 51 | } |
52 | ||
040457cc | 53 | if (!(newcook = malloc(sizeof(struct aim_msgcookie_t)))) |
54 | return -1; | |
55 | memcpy(newcook, cookie, sizeof(struct aim_msgcookie_t)); | |
56 | newcook->addtime = time(NULL); | |
040457cc | 57 | |
fd0b7da6 | 58 | newcook->next = sess->msgcookies; |
59 | sess->msgcookies = newcook; | |
7392c79f | 60 | |
040457cc | 61 | return 0; |
62 | } | |
63 | ||
7392c79f | 64 | /* |
65 | * aim_uncachecookie: | |
66 | * takes a cookie string and grabs the cookie struct associated with | |
67 | * it. removes struct from chain. returns the struct if found, or | |
68 | * NULL on not found. | |
69 | */ | |
78b3fb13 | 70 | faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type) |
040457cc | 71 | { |
72 | struct aim_msgcookie_t *cur; | |
73 | ||
7392c79f | 74 | if (!cookie || !sess->msgcookies) |
040457cc | 75 | return NULL; |
76 | ||
fd0b7da6 | 77 | if ((sess->msgcookies->type == type) && |
78 | (memcmp(sess->msgcookies->cookie, cookie, 8) == 0)) { | |
79 | struct aim_msgcookie_t *tmp; | |
040457cc | 80 | |
fd0b7da6 | 81 | tmp = sess->msgcookies; |
82 | sess->msgcookies = sess->msgcookies->next; | |
83 | ||
84 | return tmp; | |
040457cc | 85 | } |
86 | ||
fd0b7da6 | 87 | for (cur = sess->msgcookies; cur->next; cur = cur->next) { |
88 | if ((cur->next->type == type) && | |
89 | (memcmp(cur->next->cookie, cookie, 8) == 0)) { | |
040457cc | 90 | struct aim_msgcookie_t *tmp; |
91 | ||
92 | tmp = cur->next; | |
93 | cur->next = cur->next->next; | |
fd0b7da6 | 94 | |
040457cc | 95 | return tmp; |
96 | } | |
040457cc | 97 | } |
fd0b7da6 | 98 | |
040457cc | 99 | return NULL; |
100 | } | |
101 | ||
102 | /* | |
7392c79f | 103 | * aim_purgecookies: |
104 | * purge out old cookies | |
105 | * | |
106 | * finds old cookies, calls uncache on them. | |
107 | * | |
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. | |
111 | * | |
112 | * i'll avoid the puns. | |
040457cc | 113 | */ |
7392c79f | 114 | |
78b3fb13 | 115 | faim_export int aim_purgecookies(struct aim_session_t *sess, int maxage) |
040457cc | 116 | { |
040457cc | 117 | struct aim_msgcookie_t *cur; |
040457cc | 118 | time_t curtime; |
119 | ||
fd0b7da6 | 120 | curtime = time(NULL); |
121 | ||
122 | for (cur = sess->msgcookies; cur; cur = cur->next) { | |
123 | if (cur->addtime > (time(NULL) - maxage)) { | |
124 | struct aim_msgcookie_t *remed = NULL; | |
125 | ||
9f20a4e3 | 126 | #if 1 |
040457cc | 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]); | |
130 | #endif | |
7392c79f | 131 | |
132 | remed = aim_uncachecookie(sess, cur->cookie, cur->type); | |
040457cc | 133 | if (remed) { |
134 | if (remed->data) | |
135 | free(remed->data); | |
136 | free(remed); | |
137 | } | |
138 | } | |
040457cc | 139 | } |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
78b3fb13 | 144 | faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data) |
7392c79f | 145 | { |
146 | struct aim_msgcookie_t *cookie; | |
147 | ||
fd0b7da6 | 148 | if (!c) |
149 | return NULL; | |
7392c79f | 150 | |
fd0b7da6 | 151 | if (!(cookie = calloc(1, sizeof(struct aim_msgcookie_t)))) |
152 | return NULL; | |
7392c79f | 153 | |
154 | cookie->data = data; | |
7392c79f | 155 | cookie->type = type; |
7392c79f | 156 | memcpy(cookie->cookie, c, 8); |
157 | ||
fd0b7da6 | 158 | return cookie; |
7392c79f | 159 | } |
160 | ||
78b3fb13 | 161 | faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, unsigned char *cookie, int type) |
7392c79f | 162 | { |
163 | struct aim_msgcookie_t *cur; | |
164 | ||
fd0b7da6 | 165 | for (cur = sess->msgcookies; cur; cur = cur->next) { |
166 | if ((cur->type == type) && | |
167 | (memcmp(cur->cookie, cookie, 8) == 0)) | |
168 | return cur; | |
169 | } | |
7392c79f | 170 | |
fd0b7da6 | 171 | return NULL; |
7392c79f | 172 | } |
173 | ||
78b3fb13 | 174 | static int aim_freecookie(struct aim_msgcookie_t *cookie) { |
fd0b7da6 | 175 | return 0; |
7392c79f | 176 | } |
177 | ||
78b3fb13 | 178 | faim_internal int aim_msgcookie_gettype(int reqclass) { |
7392c79f | 179 | /* XXX: hokey-assed. needs fixed. */ |
180 | switch(reqclass) { | |
181 | case AIM_CAPS_BUDDYICON: | |
182 | return AIM_COOKIETYPE_OFTICON; | |
183 | break; | |
184 | case AIM_CAPS_VOICE: | |
185 | return AIM_COOKIETYPE_OFTVOICE; | |
186 | break; | |
187 | case AIM_CAPS_IMIMAGE: | |
188 | return AIM_COOKIETYPE_OFTIMAGE; | |
189 | break; | |
190 | case AIM_CAPS_CHAT: | |
191 | return AIM_COOKIETYPE_CHAT; | |
192 | break; | |
193 | case AIM_CAPS_GETFILE: | |
194 | return AIM_COOKIETYPE_OFTGET; | |
195 | break; | |
196 | case AIM_CAPS_SENDFILE: | |
197 | return AIM_COOKIETYPE_OFTSEND; | |
198 | break; | |
199 | default: | |
200 | return AIM_COOKIETYPE_UNKNOWN; | |
201 | break; | |
202 | } | |
203 | } |