]> andersk Git - libfaim.git/blob - aim_msgcookie.c
- Sat Dec 2 02:14:45 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;
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
50     return 0;
51   }
52   
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);
57   
58   newcook->next = sess->msgcookies;
59   sess->msgcookies = newcook;
60
61   return 0;
62 }
63
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  */
70 faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type)
71 {
72   struct aim_msgcookie_t *cur;
73
74   if (!cookie || !sess->msgcookies)
75     return NULL;
76
77   if ((sess->msgcookies->type == type) && 
78       (memcmp(sess->msgcookies->cookie, cookie, 8) == 0)) {
79     struct aim_msgcookie_t *tmp;
80
81     tmp = sess->msgcookies;
82     sess->msgcookies = sess->msgcookies->next;
83
84     return tmp;
85   } 
86
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;
91       
92       tmp = cur->next;
93       cur->next = cur->next->next;
94
95       return tmp;
96     }
97   }
98
99   return NULL;
100 }
101
102 /*
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.  
113  */
114
115 faim_export int aim_purgecookies(struct aim_session_t *sess, int maxage)
116 {
117   struct aim_msgcookie_t *cur;
118   time_t curtime;
119  
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
126 #if 1
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
131
132       remed = aim_uncachecookie(sess, cur->cookie, cur->type);
133       if (remed) {
134         if (remed->data)
135           free(remed->data);
136         free(remed);
137       }
138     }
139   }
140   
141   return 0;
142 }
143
144 faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data) 
145 {
146   struct aim_msgcookie_t *cookie;
147
148   if (!c)
149     return NULL;
150
151   if (!(cookie = calloc(1, sizeof(struct aim_msgcookie_t))))
152     return NULL;
153   
154   cookie->data = data;
155   cookie->type = type;
156   memcpy(cookie->cookie, c, 8);
157   
158   return cookie;
159 }
160   
161 faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, unsigned char *cookie, int type)
162 {
163   struct aim_msgcookie_t *cur;
164   
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   }
170
171   return NULL;
172 }
173
174 static int aim_freecookie(struct aim_msgcookie_t *cookie) {
175   return 0;
176
177
178 faim_internal int aim_msgcookie_gettype(int reqclass) {
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 }
This page took 0.079474 seconds and 5 git commands to generate.