Mercurial > pidgin
annotate src/protocols/oscar/util.c @ 10747:0e6b33463a3a
[gaim-migrate @ 12349]
This gets rid of that "you tried to set your away message too early"
error message for oscar.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Sun, 27 Mar 2005 04:52:55 +0000 |
parents | 9f358a718f38 |
children | 7d31d61e6438 |
rev | line source |
---|---|
2086 | 1 /* |
4246 | 2 * A little bit of this |
3 * A little bit of that | |
4 * It started with a kiss | |
5 * Now we're up to bat | |
2086 | 6 */ |
7 | |
2672 | 8 #define FAIM_INTERNAL |
2086 | 9 #include <aim.h> |
10 #include <ctype.h> | |
11 | |
4246 | 12 #ifdef _WIN32 |
13 #include "win32dep.h" | |
14 #endif | |
15 | |
16 faim_internal void faimdprintf(aim_session_t *sess, int dlevel, const char *format, ...) | |
17 { | |
18 if (!sess) { | |
19 fprintf(stderr, "faimdprintf: no session! boo! (%d, %s)\n", dlevel, format); | |
20 return; | |
21 } | |
22 if ((dlevel <= sess->debug) && sess->debugcb) { | |
23 va_list ap; | |
24 va_start(ap, format); | |
25 sess->debugcb(sess, dlevel, format, ap); | |
26 va_end(ap); | |
27 } | |
28 | |
29 return; | |
30 } | |
31 | |
5927 | 32 faim_export int aimutil_putstr(char *dest, const char *src, int len) |
2086 | 33 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
34 memcpy(dest, src, len); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
35 return len; |
2086 | 36 } |
37 | |
38 /* | |
39 * Tokenizing functions. Used to portably replace strtok/sep. | |
40 * -- DMP. | |
41 * | |
42 */ | |
6323 | 43 faim_export int aimutil_tokslen(char *toSearch, int theindex, char dl) |
2086 | 44 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
45 int curCount = 1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
46 char *next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
47 char *last; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
48 int toReturn; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
49 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
50 last = toSearch; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
51 next = strchr(toSearch, dl); |
2086 | 52 |
6323 | 53 while(curCount < theindex && next != NULL) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
54 curCount++; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
55 last = next + 1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
56 next = strchr(last, dl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
57 } |
2086 | 58 |
6323 | 59 if ((curCount < theindex) || (next == NULL)) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
60 toReturn = strlen(toSearch) - (curCount - 1); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
61 else |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
62 toReturn = next - toSearch - (curCount - 1); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
63 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
64 return toReturn; |
2086 | 65 } |
66 | |
67 faim_export int aimutil_itemcnt(char *toSearch, char dl) | |
68 { | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
69 int curCount; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
70 char *next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
71 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
72 curCount = 1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
73 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
74 next = strchr(toSearch, dl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
75 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
76 while(next != NULL) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
77 curCount++; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
78 next = strchr(next + 1, dl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
79 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
80 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
81 return curCount; |
2086 | 82 } |
83 | |
6323 | 84 faim_export char *aimutil_itemindex(char *toSearch, int theindex, char dl) |
2086 | 85 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
86 int curCount; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
87 char *next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
88 char *last; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
89 char *toReturn; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
90 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
91 curCount = 0; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
92 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
93 last = toSearch; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
94 next = strchr(toSearch, dl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
95 |
6323 | 96 while (curCount < theindex && next != NULL) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
97 curCount++; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
98 last = next + 1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
99 next = strchr(last, dl); |
2086 | 100 } |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
101 |
6323 | 102 if (curCount < theindex) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
103 toReturn = malloc(sizeof(char)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
104 *toReturn = '\0'; |
2086 | 105 } |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
106 next = strchr(last, dl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
107 |
6323 | 108 if (curCount < theindex) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
109 toReturn = malloc(sizeof(char)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
110 *toReturn = '\0'; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
111 } else { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
112 if (next == NULL) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
113 toReturn = malloc((strlen(last) + 1) * sizeof(char)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
114 strcpy(toReturn, last); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
115 } else { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
116 toReturn = malloc((next - last + 1) * sizeof(char)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
117 memcpy(toReturn, last, (next - last)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
118 toReturn[next - last] = '\0'; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
119 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
120 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
121 return toReturn; |
2086 | 122 } |
123 | |
4617 | 124 /** |
125 * Calculate the checksum of a given icon. | |
126 * | |
127 */ | |
128 faim_export fu16_t aimutil_iconsum(const fu8_t *buf, int buflen) | |
129 { | |
130 fu32_t sum; | |
131 int i; | |
132 | |
133 for (i=0, sum=0; i+1<buflen; i+=2) | |
134 sum += (buf[i+1] << 8) + buf[i]; | |
135 if (i < buflen) | |
136 sum += buf[i]; | |
137 sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff); | |
138 | |
139 return sum; | |
140 } | |
141 | |
8092 | 142 /** |
8972 | 143 * Check if the given screen name is a valid AIM screen name. |
144 * Example: BobDole | |
9681 | 145 * Example: Henry_Ford@mac.com |
8972 | 146 * |
147 * @return 1 if the screen name is valid, 0 if not. | |
148 */ | |
149 static int aim_snvalid_aim(const char *sn) | |
150 { | |
151 int i; | |
152 | |
153 for (i = 0; sn[i] != '\0'; i++) { | |
9681 | 154 if (!isalnum(sn[i]) && (sn[i] != ' ') && |
155 (sn[i] != '@') && (sn[i] != '.') && | |
156 (sn[i] != '_') && (sn[i] != '-')) | |
8972 | 157 return 0; |
158 } | |
159 | |
160 return 1; | |
161 } | |
162 | |
163 /** | |
164 * Check if the given screen name is a valid ICQ screen name. | |
165 * Example: 1234567 | |
166 * | |
167 * @return 1 if the screen name is valid, 0 if not. | |
168 */ | |
169 static int aim_snvalid_icq(const char *sn) | |
170 { | |
171 int i; | |
172 | |
173 for (i = 0; sn[i] != '\0'; i++) { | |
174 if (!isdigit(sn[i])) | |
175 return 0; | |
176 } | |
177 | |
178 return 1; | |
179 } | |
180 | |
181 /** | |
182 * Check if the given screen name is a valid SMS screen name. | |
183 * Example: +19195551234 | |
184 * | |
185 * @return 1 if the screen name is valid, 0 if not. | |
186 */ | |
187 static int aim_snvalid_sms(const char *sn) | |
188 { | |
189 int i; | |
190 | |
191 if (sn[0] != '+') | |
192 return 0; | |
193 | |
194 for (i = 1; sn[i] != '\0'; i++) { | |
195 if (!isdigit(sn[i])) | |
196 return 0; | |
197 } | |
198 | |
199 return 1; | |
200 } | |
201 | |
202 /** | |
203 * Check if the given screen name is a valid oscar screen name. | |
8092 | 204 * |
205 * @return 1 if the screen name is valid, 0 if not. | |
206 */ | |
207 faim_export int aim_snvalid(const char *sn) | |
208 { | |
8972 | 209 if ((sn == NULL) || (*sn == '\0')) |
8092 | 210 return 0; |
211 | |
8972 | 212 if (isalpha(sn[0])) |
213 return aim_snvalid_aim(sn); | |
214 else if (isdigit(sn[0])) | |
215 return aim_snvalid_icq(sn); | |
216 else if (sn[0] == '+') | |
217 return aim_snvalid_sms(sn); | |
8092 | 218 |
8972 | 219 return 0; |
8092 | 220 } |
221 | |
9975 | 222 /** |
223 * Determine if a given screen name is an ICQ screen name | |
224 * (i.e. it begins with a number). | |
225 * | |
226 * @sn A valid AIM or ICQ screen name. | |
227 * @return 1 if the screen name is an ICQ screen name. Otherwise 0 | |
228 * is returned. | |
229 */ | |
230 faim_export int aim_sn_is_icq(const char *sn) | |
231 { | |
232 if (isalpha(sn[0])) | |
233 return 0; | |
234 return 1; | |
235 } | |
236 | |
2086 | 237 /* |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
238 * int snlen(const char *) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
239 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
240 * This takes a screen name and returns its length without |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
241 * spaces. If there are no spaces in the SN, then the |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
242 * return is equal to that of strlen(). |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
243 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
244 */ |
2086 | 245 faim_export int aim_snlen(const char *sn) |
246 { | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
247 int i = 0; |
2086 | 248 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
249 if (!sn) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
250 return 0; |
2086 | 251 |
5556 | 252 while (*sn != '\0') { |
253 if (*sn != ' ') | |
254 i++; | |
255 sn++; | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
256 } |
2086 | 257 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
258 return i; |
2086 | 259 } |
260 | |
261 /* | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
262 * int sncmp(const char *, const char *) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
263 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
264 * This takes two screen names and compares them using the rules |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
265 * on screen names for AIM/AOL. Mainly, this means case and space |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
266 * insensitivity (all case differences and spacing differences are |
5628 | 267 * ignored, with the exception that screen names can not start with |
268 * a space). | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
269 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
270 * Return: 0 if equal |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
271 * non-0 if different |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
272 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
273 */ |
2086 | 274 faim_export int aim_sncmp(const char *sn1, const char *sn2) |
275 { | |
276 | |
10116 | 277 if ((sn1 == NULL) || (sn2 == NULL)) |
278 return -1; | |
279 | |
5928 | 280 do { |
5556 | 281 while (*sn2 == ' ') |
5724 | 282 sn2++; |
5556 | 283 while (*sn1 == ' ') |
5724 | 284 sn1++; |
5928 | 285 if (toupper(*sn1) != toupper(*sn2)) |
286 return 1; | |
287 } while ((*sn1 != '\0') && sn1++ && sn2++); | |
2086 | 288 |
5928 | 289 return 0; |
2086 | 290 } |