]>
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 | * | |
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 | { |
32 | struct aim_msgcookie_t *newcook = NULL, *cur = NULL; | |
33 | ||
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 | } | |
49 | return(0); | |
50 | } | |
51 | ||
040457cc | 52 | if (!(newcook = malloc(sizeof(struct aim_msgcookie_t)))) |
53 | return -1; | |
54 | memcpy(newcook, cookie, sizeof(struct aim_msgcookie_t)); | |
55 | newcook->addtime = time(NULL); | |
040457cc | 56 | |
7392c79f | 57 | if(newcook->next) |
58 | printf("faim: cachecookie: newcook->next isn't NULL ???\n"); | |
59 | ||
60 | newcook->next = NULL; | |
61 | ||
040457cc | 62 | cur = sess->msgcookies; |
63 | ||
64 | if (cur == NULL) { | |
65 | sess->msgcookies = newcook; | |
66 | return 0; | |
67 | } | |
7392c79f | 68 | |
040457cc | 69 | while (cur->next != NULL) |
70 | cur = cur->next; | |
71 | cur->next = newcook; | |
7392c79f | 72 | |
040457cc | 73 | return 0; |
74 | } | |
75 | ||
7392c79f | 76 | /* |
77 | * aim_uncachecookie: | |
78 | * takes a cookie string and grabs the cookie struct associated with | |
79 | * it. removes struct from chain. returns the struct if found, or | |
80 | * NULL on not found. | |
81 | */ | |
82 | ||
78b3fb13 | 83 | faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type) |
040457cc | 84 | { |
85 | struct aim_msgcookie_t *cur; | |
86 | ||
7392c79f | 87 | if (!cookie || !sess->msgcookies) |
040457cc | 88 | return NULL; |
89 | ||
7392c79f | 90 | cur = sess->msgcookies; |
040457cc | 91 | |
7392c79f | 92 | if ( (memcmp(cur->cookie, cookie, 8) == 0) && (cur->type == type) ) { |
040457cc | 93 | sess->msgcookies = cur->next; |
94 | return cur; | |
95 | } | |
96 | ||
040457cc | 97 | while (cur->next) { |
7392c79f | 98 | if ( (memcmp(cur->next->cookie, cookie, 8) == 0) && (cur->next->type == type) ) { |
040457cc | 99 | struct aim_msgcookie_t *tmp; |
100 | ||
101 | tmp = cur->next; | |
102 | cur->next = cur->next->next; | |
103 | return tmp; | |
104 | } | |
105 | cur = cur->next; | |
106 | } | |
107 | return NULL; | |
108 | } | |
109 | ||
110 | /* | |
7392c79f | 111 | * aim_purgecookies: |
112 | * purge out old cookies | |
113 | * | |
114 | * finds old cookies, calls uncache on them. | |
115 | * | |
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. | |
119 | * | |
120 | * i'll avoid the puns. | |
040457cc | 121 | */ |
7392c79f | 122 | |
78b3fb13 | 123 | faim_export int aim_purgecookies(struct aim_session_t *sess, int maxage) |
040457cc | 124 | { |
040457cc | 125 | struct aim_msgcookie_t *cur; |
126 | struct aim_msgcookie_t *remed = NULL; | |
127 | time_t curtime; | |
128 | ||
129 | cur = sess->msgcookies; | |
130 | ||
131 | curtime = time(&curtime); | |
132 | ||
133 | while (cur) { | |
7392c79f | 134 | if ( (cur->addtime) > (curtime - maxage) ) { |
040457cc | 135 | #if DEBUG > 1 |
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]); | |
139 | #endif | |
7392c79f | 140 | |
141 | remed = aim_uncachecookie(sess, cur->cookie, cur->type); | |
040457cc | 142 | if (remed) { |
143 | if (remed->data) | |
144 | free(remed->data); | |
145 | free(remed); | |
146 | } | |
147 | } | |
7392c79f | 148 | |
040457cc | 149 | cur = cur->next; |
7392c79f | 150 | |
040457cc | 151 | } |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
78b3fb13 | 156 | faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data) |
7392c79f | 157 | { |
158 | struct aim_msgcookie_t *cookie; | |
159 | ||
160 | if(!c) | |
161 | return(NULL); | |
162 | ||
163 | if( (cookie = calloc(1, sizeof(struct aim_msgcookie_t))) == NULL) | |
164 | return(NULL); | |
165 | ||
166 | cookie->data = data; | |
167 | ||
168 | cookie->type = type; | |
169 | ||
170 | memcpy(cookie->cookie, c, 8); | |
171 | ||
172 | return(cookie); | |
173 | } | |
174 | ||
78b3fb13 | 175 | faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, unsigned char *cookie, int type) |
7392c79f | 176 | { |
177 | struct aim_msgcookie_t *cur; | |
178 | ||
179 | if(!sess->msgcookies) | |
180 | return NULL; | |
181 | ||
182 | cur = sess->msgcookies; | |
183 | ||
184 | if( (memcmp(cur->cookie, cookie, 8) == 0) && (cur->type == type)) | |
185 | return(cur); | |
186 | ||
187 | while( (cur = cur->next) ) | |
188 | if( (memcmp(cur->cookie, cookie, 8) == 0) && (cur->type == type)) | |
189 | return(cur); | |
190 | ||
191 | return(NULL); | |
192 | } | |
193 | ||
78b3fb13 | 194 | static int aim_freecookie(struct aim_msgcookie_t *cookie) { |
7392c79f | 195 | return(0); |
196 | } | |
197 | ||
78b3fb13 | 198 | faim_internal int aim_msgcookie_gettype(int reqclass) { |
7392c79f | 199 | /* XXX: hokey-assed. needs fixed. */ |
200 | switch(reqclass) { | |
201 | case AIM_CAPS_BUDDYICON: | |
202 | return AIM_COOKIETYPE_OFTICON; | |
203 | break; | |
204 | case AIM_CAPS_VOICE: | |
205 | return AIM_COOKIETYPE_OFTVOICE; | |
206 | break; | |
207 | case AIM_CAPS_IMIMAGE: | |
208 | return AIM_COOKIETYPE_OFTIMAGE; | |
209 | break; | |
210 | case AIM_CAPS_CHAT: | |
211 | return AIM_COOKIETYPE_CHAT; | |
212 | break; | |
213 | case AIM_CAPS_GETFILE: | |
214 | return AIM_COOKIETYPE_OFTGET; | |
215 | break; | |
216 | case AIM_CAPS_SENDFILE: | |
217 | return AIM_COOKIETYPE_OFTSEND; | |
218 | break; | |
219 | default: | |
220 | return AIM_COOKIETYPE_UNKNOWN; | |
221 | break; | |
222 | } | |
223 | } |