libfaim/aim_msgcookie.c

changeset 1184
e475cddf904d
parent 840
b80a628d935f
child 1186
17204492d19d
equal deleted inserted replaced
1183:9bd1d70b5352 1184:e475cddf904d
22 * - copies cookie struct; you need to free() it afterwards; 22 * - copies cookie struct; you need to free() it afterwards;
23 * - cookie->data is not copied, but passed along. don't free it. 23 * - cookie->data is not copied, but passed along. don't free it.
24 * - newcook->addtime is updated accordingly; 24 * - newcook->addtime is updated accordingly;
25 * - cookie->type is just passed across. 25 * - cookie->type is just passed across.
26 * 26 *
27 * returns -1 on error, 0 on success. */ 27 * returns -1 on error, 0 on success.
28 28 */
29 faim_internal int aim_cachecookie(struct aim_session_t *sess, 29 faim_internal int aim_cachecookie(struct aim_session_t *sess,
30 struct aim_msgcookie_t *cookie) 30 struct aim_msgcookie_t *cookie)
31 { 31 {
32 struct aim_msgcookie_t *newcook = NULL, *cur = NULL; 32 struct aim_msgcookie_t *newcook;
33 33
34 if (!cookie) 34 if (!cookie)
35 return -1; 35 return -1;
36 36
37 if( (newcook = aim_checkcookie(sess, cookie->cookie, cookie->type)) ) { 37 if( (newcook = aim_checkcookie(sess, cookie->cookie, cookie->type)) ) {
38 newcook->addtime = time(NULL); 38 newcook->addtime = time(NULL);
44 cookie->cookie[3], cookie->cookie[4], cookie->cookie[5], 44 cookie->cookie[3], cookie->cookie[4], cookie->cookie[5],
45 cookie->cookie[6], cookie->cookie[7], cookie->type); 45 cookie->cookie[6], cookie->cookie[7], cookie->type);
46 46
47 free(cookie->data); 47 free(cookie->data);
48 } 48 }
49 return(0); 49
50 return 0;
50 } 51 }
51 52
52 if (!(newcook = malloc(sizeof(struct aim_msgcookie_t)))) 53 if (!(newcook = malloc(sizeof(struct aim_msgcookie_t))))
53 return -1; 54 return -1;
54 memcpy(newcook, cookie, sizeof(struct aim_msgcookie_t)); 55 memcpy(newcook, cookie, sizeof(struct aim_msgcookie_t));
55 newcook->addtime = time(NULL); 56 newcook->addtime = time(NULL);
56 57
57 if(newcook->next) 58 newcook->next = sess->msgcookies;
58 printf("faim: cachecookie: newcook->next isn't NULL ???\n"); 59 sess->msgcookies = newcook;
59 60
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; 61 return 0;
74 } 62 }
75 63
76 /* 64 /*
77 * aim_uncachecookie: 65 * aim_uncachecookie:
78 * takes a cookie string and grabs the cookie struct associated with 66 * takes a cookie string and grabs the cookie struct associated with
79 * it. removes struct from chain. returns the struct if found, or 67 * it. removes struct from chain. returns the struct if found, or
80 * NULL on not found. 68 * NULL on not found.
81 */ 69 */
82
83 faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type) 70 faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type)
84 { 71 {
85 struct aim_msgcookie_t *cur; 72 struct aim_msgcookie_t *cur;
86 73
87 if (!cookie || !sess->msgcookies) 74 if (!cookie || !sess->msgcookies)
88 return NULL; 75 return NULL;
89 76
90 cur = sess->msgcookies; 77 if ((sess->msgcookies->type == type) &&
91 78 (memcmp(sess->msgcookies->cookie, cookie, 8) == 0)) {
92 if ( (memcmp(cur->cookie, cookie, 8) == 0) && (cur->type == type) ) { 79 struct aim_msgcookie_t *tmp;
93 sess->msgcookies = cur->next; 80
94 return cur; 81 tmp = sess->msgcookies;
82 sess->msgcookies = sess->msgcookies->next;
83
84 return tmp;
95 } 85 }
96 86
97 while (cur->next) { 87 for (cur = sess->msgcookies; cur->next; cur = cur->next) {
98 if ( (memcmp(cur->next->cookie, cookie, 8) == 0) && (cur->next->type == type) ) { 88 if ((cur->next->type == type) &&
89 (memcmp(cur->next->cookie, cookie, 8) == 0)) {
99 struct aim_msgcookie_t *tmp; 90 struct aim_msgcookie_t *tmp;
100 91
101 tmp = cur->next; 92 tmp = cur->next;
102 cur->next = cur->next->next; 93 cur->next = cur->next->next;
94
103 return tmp; 95 return tmp;
104 } 96 }
105 cur = cur->next; 97 }
106 } 98
107 return NULL; 99 return NULL;
108 } 100 }
109 101
110 /* 102 /*
111 * aim_purgecookies: 103 * aim_purgecookies:
121 */ 113 */
122 114
123 faim_export int aim_purgecookies(struct aim_session_t *sess, int maxage) 115 faim_export int aim_purgecookies(struct aim_session_t *sess, int maxage)
124 { 116 {
125 struct aim_msgcookie_t *cur; 117 struct aim_msgcookie_t *cur;
126 struct aim_msgcookie_t *remed = NULL;
127 time_t curtime; 118 time_t curtime;
128 119
129 cur = sess->msgcookies; 120 curtime = time(NULL);
130 121
131 curtime = time(&curtime); 122 for (cur = sess->msgcookies; cur; cur = cur->next) {
132 123 if (cur->addtime > (time(NULL) - maxage)) {
133 while (cur) { 124 struct aim_msgcookie_t *remed = NULL;
134 if ( (cur->addtime) > (curtime - maxage) ) { 125
135 #if DEBUG > 1 126 #if DEBUG > 1
136 printf("aimmsgcookie: WARNING purged obsolete message cookie %x%x%x%x %x%x%x%x\n", 127 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], 128 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]); 129 cur->cookie[4], cur->cookie[5], cur->cookie[6], cur->cookie[7]);
139 #endif 130 #endif
143 if (remed->data) 134 if (remed->data)
144 free(remed->data); 135 free(remed->data);
145 free(remed); 136 free(remed);
146 } 137 }
147 } 138 }
148
149 cur = cur->next;
150
151 } 139 }
152 140
153 return 0; 141 return 0;
154 } 142 }
155 143
156 faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data) 144 faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data)
157 { 145 {
158 struct aim_msgcookie_t *cookie; 146 struct aim_msgcookie_t *cookie;
159 147
160 if(!c) 148 if (!c)
161 return(NULL); 149 return NULL;
162 150
163 if( (cookie = calloc(1, sizeof(struct aim_msgcookie_t))) == NULL) 151 if (!(cookie = calloc(1, sizeof(struct aim_msgcookie_t))))
164 return(NULL); 152 return NULL;
165 153
166 cookie->data = data; 154 cookie->data = data;
167
168 cookie->type = type; 155 cookie->type = type;
169
170 memcpy(cookie->cookie, c, 8); 156 memcpy(cookie->cookie, c, 8);
171 157
172 return(cookie); 158 return cookie;
173 } 159 }
174 160
175 faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, unsigned char *cookie, int type) 161 faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, unsigned char *cookie, int type)
176 { 162 {
177 struct aim_msgcookie_t *cur; 163 struct aim_msgcookie_t *cur;
178 164
179 if(!sess->msgcookies) 165 for (cur = sess->msgcookies; cur; cur = cur->next) {
180 return NULL; 166 if ((cur->type == type) &&
181 167 (memcmp(cur->cookie, cookie, 8) == 0))
182 cur = sess->msgcookies; 168 return cur;
183 169 }
184 if( (memcmp(cur->cookie, cookie, 8) == 0) && (cur->type == type)) 170
185 return(cur); 171 return NULL;
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 } 172 }
193 173
194 static int aim_freecookie(struct aim_msgcookie_t *cookie) { 174 static int aim_freecookie(struct aim_msgcookie_t *cookie) {
195 return(0); 175 return 0;
196 } 176 }
197 177
198 faim_internal int aim_msgcookie_gettype(int reqclass) { 178 faim_internal int aim_msgcookie_gettype(int reqclass) {
199 /* XXX: hokey-assed. needs fixed. */ 179 /* XXX: hokey-assed. needs fixed. */
200 switch(reqclass) { 180 switch(reqclass) {

mercurial