]>
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 | ||
37ee990e | 16 | #define FAIM_INTERNAL |
dd60ff8b | 17 | #include <aim.h> |
040457cc | 18 | |
7392c79f | 19 | /* |
20 | * aim_cachecookie: | |
21 | * appends a cookie to the cookie list for sess. | |
37ee990e | 22 | * - if cookie->cookie for type cookie->type is found, -1 is returned |
7392c79f | 23 | * - copies cookie struct; you need to free() it afterwards; |
24 | * - cookie->data is not copied, but passed along. don't free it. | |
7392c79f | 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 | ||
37ee990e | 34 | if (!sess || !cookie) |
040457cc | 35 | return -1; |
36 | ||
37ee990e | 37 | printf("\t\tCC cache %d %s", cookie->type, cookie->cookie); |
38 | if(cookie->type == AIM_COOKIETYPE_OFTGET) { | |
39 | struct aim_filetransfer_priv *priv; | |
40 | priv = cookie->data; | |
41 | printf("%s\n", priv->sn); | |
42 | } else | |
43 | printf("\n"); | |
fd0b7da6 | 44 | |
37ee990e | 45 | if( (newcook = aim_checkcookie(sess, cookie->cookie, cookie->type)) ) { |
46 | printf("aim_cachecookie: cookie already cached\n"); | |
47 | return -1; | |
7392c79f | 48 | } |
49 | ||
040457cc | 50 | if (!(newcook = malloc(sizeof(struct aim_msgcookie_t)))) |
51 | return -1; | |
52 | memcpy(newcook, cookie, sizeof(struct aim_msgcookie_t)); | |
040457cc | 53 | |
fd0b7da6 | 54 | newcook->next = sess->msgcookies; |
55 | sess->msgcookies = newcook; | |
7392c79f | 56 | |
040457cc | 57 | return 0; |
58 | } | |
59 | ||
7392c79f | 60 | /* |
61 | * aim_uncachecookie: | |
62 | * takes a cookie string and grabs the cookie struct associated with | |
63 | * it. removes struct from chain. returns the struct if found, or | |
64 | * NULL on not found. | |
65 | */ | |
78b3fb13 | 66 | faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type) |
040457cc | 67 | { |
37ee990e | 68 | struct aim_msgcookie_t *cur, **prev; |
040457cc | 69 | |
7392c79f | 70 | if (!cookie || !sess->msgcookies) |
040457cc | 71 | return NULL; |
72 | ||
37ee990e | 73 | printf("\t\tCC uncache %d %s\n", type, cookie); |
040457cc | 74 | |
37ee990e | 75 | for (prev = &sess->msgcookies; (cur = *prev); ) { |
76 | if ((cur->type == type) && | |
77 | (memcmp(cur->cookie, cookie, 8) == 0)) { | |
78 | *prev = cur->next; | |
79 | return cur; | |
040457cc | 80 | } |
37ee990e | 81 | prev = &cur->next; |
040457cc | 82 | } |
fd0b7da6 | 83 | |
040457cc | 84 | return NULL; |
85 | } | |
86 | ||
78b3fb13 | 87 | faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data) |
7392c79f | 88 | { |
89 | struct aim_msgcookie_t *cookie; | |
90 | ||
fd0b7da6 | 91 | if (!c) |
92 | return NULL; | |
7392c79f | 93 | |
fd0b7da6 | 94 | if (!(cookie = calloc(1, sizeof(struct aim_msgcookie_t)))) |
95 | return NULL; | |
7392c79f | 96 | |
97 | cookie->data = data; | |
7392c79f | 98 | cookie->type = type; |
7392c79f | 99 | memcpy(cookie->cookie, c, 8); |
100 | ||
fd0b7da6 | 101 | return cookie; |
7392c79f | 102 | } |
103 | ||
37ee990e | 104 | faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, const unsigned char *cookie, const int type) |
7392c79f | 105 | { |
106 | struct aim_msgcookie_t *cur; | |
37ee990e | 107 | |
108 | printf("\t\tCC check %d %s\n", type, cookie); | |
109 | ||
fd0b7da6 | 110 | for (cur = sess->msgcookies; cur; cur = cur->next) { |
111 | if ((cur->type == type) && | |
112 | (memcmp(cur->cookie, cookie, 8) == 0)) | |
37ee990e | 113 | return cur; |
fd0b7da6 | 114 | } |
7392c79f | 115 | |
fd0b7da6 | 116 | return NULL; |
7392c79f | 117 | } |
118 | ||
37ee990e | 119 | faim_internal int aim_freecookie(struct aim_session_t *sess, struct aim_msgcookie_t *cookie) { |
120 | struct aim_msgcookie_t *cur, **prev; | |
121 | ||
122 | if (!sess || !cookie) | |
123 | return -1; | |
124 | ||
125 | /* | |
126 | * Make sure its not in the list somewhere still. | |
127 | * | |
128 | * If this actually happens, theres been a major coding failure | |
129 | * on my part. However, that does not reduce its occurance likelyhood. | |
130 | */ | |
131 | for (prev = &sess->msgcookies; (cur = *prev); ) { | |
132 | if (cur == cookie) { | |
133 | *prev = cur->next; | |
134 | } else | |
135 | prev = &cur->next; | |
136 | } | |
137 | ||
138 | free(cookie); | |
139 | ||
fd0b7da6 | 140 | return 0; |
7392c79f | 141 | } |
142 | ||
78b3fb13 | 143 | faim_internal int aim_msgcookie_gettype(int reqclass) { |
7392c79f | 144 | /* XXX: hokey-assed. needs fixed. */ |
145 | switch(reqclass) { | |
146 | case AIM_CAPS_BUDDYICON: | |
147 | return AIM_COOKIETYPE_OFTICON; | |
148 | break; | |
149 | case AIM_CAPS_VOICE: | |
150 | return AIM_COOKIETYPE_OFTVOICE; | |
151 | break; | |
152 | case AIM_CAPS_IMIMAGE: | |
153 | return AIM_COOKIETYPE_OFTIMAGE; | |
154 | break; | |
155 | case AIM_CAPS_CHAT: | |
156 | return AIM_COOKIETYPE_CHAT; | |
157 | break; | |
158 | case AIM_CAPS_GETFILE: | |
159 | return AIM_COOKIETYPE_OFTGET; | |
160 | break; | |
161 | case AIM_CAPS_SENDFILE: | |
162 | return AIM_COOKIETYPE_OFTSEND; | |
163 | break; | |
164 | default: | |
165 | return AIM_COOKIETYPE_UNKNOWN; | |
166 | break; | |
167 | } | |
168 | } |