]> andersk Git - libfaim.git/blob - aim_msgcookie.c
- Fri Nov 10 08:24:34 UTC 2000
[libfaim.git] / aim_msgcookie.c
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  */
8
9 /*
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.
14  */
15
16 #include <faim/aim.h>
17
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
29 faim_internal int aim_cachecookie(struct aim_session_t *sess,
30                                   struct aim_msgcookie_t *cookie)
31 {
32   struct aim_msgcookie_t *newcook = NULL, *cur = NULL;
33   
34   if (!cookie)
35     return -1;
36
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   
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);
56
57   if(newcook->next)
58     printf("faim: cachecookie: newcook->next isn't NULL ???\n");
59
60   newcook->next = NULL;
61   
62   cur = sess->msgcookies;
63   
64   if (cur == NULL) {
65     sess->msgcookies = newcook;
66     return 0;
67   }
68
69   while (cur->next != NULL)
70     cur = cur->next;
71   cur->next = newcook;
72   
73   return 0;
74 }
75
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
83 faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type)
84 {
85   struct aim_msgcookie_t *cur;
86
87   if (!cookie || !sess->msgcookies)
88     return NULL;
89
90   cur = sess->msgcookies;
91
92   if ( (memcmp(cur->cookie, cookie, 8) == 0) && (cur->type == type) ) {
93     sess->msgcookies = cur->next;
94     return cur;
95   } 
96
97   while (cur->next) {
98     if ( (memcmp(cur->next->cookie, cookie, 8) == 0) && (cur->next->type == type) ) {
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 /*
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.  
121  */
122
123 faim_export int aim_purgecookies(struct aim_session_t *sess, int maxage)
124 {
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) {
134     if ( (cur->addtime) > (curtime - maxage) ) {
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
140
141       remed = aim_uncachecookie(sess, cur->cookie, cur->type);
142       if (remed) {
143         if (remed->data)
144           free(remed->data);
145         free(remed);
146       }
147     }
148
149     cur = cur->next;
150
151   }
152   
153   return 0;
154 }
155
156 faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data) 
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   
175 faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, unsigned char *cookie, int type)
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
194 static int aim_freecookie(struct aim_msgcookie_t *cookie) {
195   return(0);
196
197
198 faim_internal int aim_msgcookie_gettype(int reqclass) {
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 }
This page took 0.762431 seconds and 5 git commands to generate.