| |
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| |
2 |
| |
3 /* |
| |
4 * $Id: util.c 2096 2001-07-31 01:00:39Z warmenhoven $ |
| |
5 * |
| |
6 * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and |
| |
7 * Bill Soudan <soudan@kde.org> |
| |
8 * |
| |
9 * This program is free software; you can redistribute it and/or modify |
| |
10 * it under the terms of the GNU General Public License as published by |
| |
11 * the Free Software Foundation; either version 2 of the License, or |
| |
12 * (at your option) any later version. |
| |
13 * |
| |
14 * This program is distributed in the hope that it will be useful, |
| |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
17 * GNU General Public License for more details. |
| |
18 * |
| |
19 * You should have received a copy of the GNU General Public License |
| |
20 * along with this program; if not, write to the Free Software |
| |
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| |
22 * |
| |
23 */ |
| |
24 |
| |
25 #include <stdlib.h> |
| |
26 #include <ctype.h> |
| |
27 |
| |
28 #include "icqlib.h" |
| |
29 #include "stdpackets.h" |
| |
30 |
| |
31 /* |
| |
32 * This list of countries should be sorted according to country code. |
| |
33 * When adding new country, please preserve the order! |
| |
34 */ |
| |
35 icq_ArrayType icq_Countries[] = { |
| |
36 {"USA",1}, |
| |
37 {"Russia",7}, |
| |
38 {"Egypt",20}, |
| |
39 {"South Africa",27}, |
| |
40 {"Greece",30}, |
| |
41 {"Netherlands",31}, |
| |
42 {"Belgium",32}, |
| |
43 {"France",33}, |
| |
44 {"Monaco",33}, |
| |
45 {"Spain",34}, |
| |
46 {"Hungary",36}, |
| |
47 {"Yugoslavia",38}, |
| |
48 {"Italy",39}, |
| |
49 {"San Marino",39}, |
| |
50 {"Vatican City",39}, |
| |
51 {"Romania",40}, |
| |
52 {"Liechtenstein",41}, |
| |
53 {"Switzerland",41}, |
| |
54 {"Czech Republic",42}, |
| |
55 {"Austria",43}, |
| |
56 {"UK",44}, |
| |
57 {"Denmark",45}, |
| |
58 {"Sweden",46}, |
| |
59 {"Norway",47}, |
| |
60 {"Poland",48}, |
| |
61 {"Germany",49}, |
| |
62 {"Peru",51}, |
| |
63 {"Mexico",52}, |
| |
64 {"Guantanomo Bay",53}, |
| |
65 {"Argentina",54}, |
| |
66 {"Brazil",55}, |
| |
67 {"Chile",56}, |
| |
68 {"Columbia",57}, |
| |
69 {"Venezuela",58}, |
| |
70 {"Malaysia",60}, |
| |
71 {"Australia",61}, |
| |
72 {"Indonesia",62}, |
| |
73 {"Philippines",63}, |
| |
74 {"New Zealand",64}, |
| |
75 {"Singapore",65}, |
| |
76 {"Thailand",66}, |
| |
77 {"Japan",81}, |
| |
78 {"South Korea",82}, |
| |
79 {"Vietnam",84}, |
| |
80 {"China",86}, |
| |
81 {"Turkey",90}, |
| |
82 {"India",91}, |
| |
83 {"Pakistan",92}, |
| |
84 {"Sri Lanka",94}, |
| |
85 {"Iran",98}, |
| |
86 {"Canada",107}, |
| |
87 {"Morocco",212}, |
| |
88 {"Algeria",213}, |
| |
89 {"Tunisia",216}, |
| |
90 {"Libya",218}, |
| |
91 {"Senegal",221}, |
| |
92 {"Mali",223}, |
| |
93 {"Ivory Coast",225}, |
| |
94 {"Liberia",231}, |
| |
95 {"Ghana",233}, |
| |
96 {"Nigeria",234}, |
| |
97 {"Cameroon",237}, |
| |
98 {"Gabon",241}, |
| |
99 {"Zaire",243}, |
| |
100 {"Ethiopia",251}, |
| |
101 {"Kenya",254}, |
| |
102 {"Tanzania",255}, |
| |
103 {"Zimbabwe",263}, |
| |
104 {"Namibia",264}, |
| |
105 {"Malawi",265}, |
| |
106 {"Aruba",297}, |
| |
107 {"Portugal",351}, |
| |
108 {"Luxembourg",352}, |
| |
109 {"Ireland",353}, |
| |
110 {"Iceland",354}, |
| |
111 {"Malta",356}, |
| |
112 {"Cyprus",357}, |
| |
113 {"Finland",358}, |
| |
114 {"Bulgaria",359}, |
| |
115 {"Ukraine",380}, |
| |
116 {"Belize",501}, |
| |
117 {"Guatemala",502}, |
| |
118 {"El Salvador",503}, |
| |
119 {"Honduras",504}, |
| |
120 {"Nicaragua",505}, |
| |
121 {"Costa Rice",506}, |
| |
122 {"Panama",507}, |
| |
123 {"Haiti",509}, |
| |
124 {"Guadeloupe",590}, |
| |
125 {"Bolivia",591}, |
| |
126 {"Guyana",592}, |
| |
127 {"Ecuador",593}, |
| |
128 {"Paraguay",595}, |
| |
129 {"French Antilles",596}, |
| |
130 {"Suriname",597}, |
| |
131 {"Uruguay",598}, |
| |
132 {"Netherlands Antilles",599}, |
| |
133 {"Saipan",670}, |
| |
134 {"Saipan",670}, |
| |
135 {"Guam",671}, |
| |
136 {"Papua New Guinea",675}, |
| |
137 {"Fiji",679}, |
| |
138 {"American Samoa",684}, |
| |
139 {"New Caledonia",687}, |
| |
140 {"French Polynesia",689}, |
| |
141 {"Hong Kong",852}, |
| |
142 {"Trinidad and Tobago",868}, |
| |
143 {"Bangladesh",880}, |
| |
144 {"Taiwan",886}, |
| |
145 {"Jordan",962}, |
| |
146 {"Iraq",964}, |
| |
147 {"Kuwait",965}, |
| |
148 {"Saudia Arabia",966}, |
| |
149 {"Yemen",967}, |
| |
150 {"Oman",968}, |
| |
151 {"United Arab Emirates",971}, |
| |
152 {"Israel",972}, |
| |
153 {"Bahrain",973}, |
| |
154 {"Qatar",974}, |
| |
155 {"Nepal",977}, |
| |
156 {"Slovak Republic",4201}, |
| |
157 {"Not entered",0xffff}, |
| |
158 }; |
| |
159 |
| |
160 icq_ArrayType icq_MetaOccupation[] = { |
| |
161 {"Academic", 1}, |
| |
162 {"Administrative", 2}, |
| |
163 {"Art/Entertainment", 3}, |
| |
164 {"College Student", 4}, |
| |
165 {"Computers", 5}, |
| |
166 {"Community & Social", 6}, |
| |
167 {"Education", 7}, |
| |
168 {"Engineering", 8}, |
| |
169 {"Financial Services", 9}, |
| |
170 {"Government", 10}, |
| |
171 {"High School Student", 11}, |
| |
172 {"Home", 12}, |
| |
173 {"ICQ - Providing Help", 13}, |
| |
174 {"Law", 14}, |
| |
175 {"Managerial", 15}, |
| |
176 {"Manufacturing", 16}, |
| |
177 {"Medical/Health", 17}, |
| |
178 {"Military", 18}, |
| |
179 {"Non-Government Organization", 19}, |
| |
180 {"Professional", 20}, |
| |
181 {"Retail", 21}, |
| |
182 {"Retired", 22}, |
| |
183 {"Science & Research", 23}, |
| |
184 {"Sports", 24}, |
| |
185 {"Technical", 25}, |
| |
186 {"University Student", 26}, |
| |
187 {"Web Building", 27}, |
| |
188 {"Other Services", 99}, |
| |
189 }; |
| |
190 |
| |
191 icq_ArrayType icq_MetaPastBackgrounds[] = { |
| |
192 {"Elementary School", 300}, |
| |
193 {"High School", 301}, |
| |
194 {"College", 302}, |
| |
195 {"University", 303}, |
| |
196 {"Military", 304}, |
| |
197 {"Past Work Place", 305}, |
| |
198 {"Past Organization", 306}, |
| |
199 {"Other", 399}, |
| |
200 }; |
| |
201 |
| |
202 icq_ArrayType icq_MetaAffiliations[] = { |
| |
203 {"Alumni Org.", 200}, |
| |
204 {"Charity Org.", 201}, |
| |
205 {"Club/Social Org.", 202}, |
| |
206 {"Community Org.", 203}, |
| |
207 {"Cultural Org.", 204}, |
| |
208 {"Fan Clubs", 205}, |
| |
209 {"Fraternity/Sorority", 206}, |
| |
210 {"Hobbyists Org.", 207}, |
| |
211 {"International Org.", 208}, |
| |
212 {"Nature and Environment Org.", 209}, |
| |
213 {"Professional Org.", 210}, |
| |
214 {"Scientific/Technical Org.", 211}, |
| |
215 {"Self Improvement Group", 212}, |
| |
216 {"Spiritual/Religious Org.", 213}, |
| |
217 {"Sports Org.", 214}, |
| |
218 {"Support Org.", 215}, |
| |
219 {"Trade and Business Org.", 216}, |
| |
220 {"Union", 217}, |
| |
221 {"Voluntary Org.", 218}, |
| |
222 {"Other", 299}, |
| |
223 }; |
| |
224 |
| |
225 icq_ArrayType icq_MetaLanguages[] = { |
| |
226 {"Not Entered", 0}, |
| |
227 {"Arabic", 1}, |
| |
228 {"Bhojpuri", 2}, |
| |
229 {"Bulgarian", 3}, |
| |
230 {"Burmese", 4}, |
| |
231 {"Cantonese", 5}, |
| |
232 {"Catalan", 6}, |
| |
233 {"Chinese", 7}, |
| |
234 {"Croatian", 8}, |
| |
235 {"Czech", 9}, |
| |
236 {"Danish", 10}, |
| |
237 {"Dutch", 11}, |
| |
238 {"English", 12}, |
| |
239 {"Esperanto", 13}, |
| |
240 {"Estonian", 14}, |
| |
241 {"Farsi", 15}, |
| |
242 {"Finnish", 16}, |
| |
243 {"French", 17}, |
| |
244 {"Gaelic", 18}, |
| |
245 {"German", 19}, |
| |
246 {"Greek", 20}, |
| |
247 {"Hebrew", 21}, |
| |
248 {"Hindi", 22}, |
| |
249 {"Hungarian", 23}, |
| |
250 {"Icelandic", 24}, |
| |
251 {"Indonesian", 25}, |
| |
252 {"Italian", 26}, |
| |
253 {"Japanese", 27}, |
| |
254 {"Khmer", 28}, |
| |
255 {"Korean", 29}, |
| |
256 {"Lao", 30}, |
| |
257 {"Latvian", 31}, |
| |
258 {"Lithuanian", 32}, |
| |
259 {"Malay", 33}, |
| |
260 {"Norwegian", 34}, |
| |
261 {"Polish", 35}, |
| |
262 {"Portuguese", 36}, |
| |
263 {"Romanian", 37}, |
| |
264 {"Russian", 38}, |
| |
265 {"Serbian", 39}, |
| |
266 {"Slovak", 40}, |
| |
267 {"Slovenian", 41}, |
| |
268 {"Somali", 42}, |
| |
269 {"Spanish", 43}, |
| |
270 {"Swahili", 44}, |
| |
271 {"Swedish", 45}, |
| |
272 {"Tagalog", 46}, |
| |
273 {"Tatar", 47}, |
| |
274 {"Thai", 48}, |
| |
275 {"Turkish", 49}, |
| |
276 {"Ukrainian", 50}, |
| |
277 {"Urdu", 51}, |
| |
278 {"Vietnamese", 52}, |
| |
279 {"Yiddish", 53}, |
| |
280 {"Yoruba", 54}, |
| |
281 {"Afrikaans", 55}, |
| |
282 {"Bosnian", 56}, |
| |
283 {"Persian", 57}, |
| |
284 {"Albanian", 58}, |
| |
285 {"Armenian", 59}, |
| |
286 {"Punjabi", 60}, |
| |
287 {"Chamorro", 61}, |
| |
288 {"Mongolian", 62}, |
| |
289 {"Mandarin", 63}, |
| |
290 {"Taiwaness", 64}, |
| |
291 {"Macedonian", 65}, |
| |
292 {"Sindhi", 66}, |
| |
293 {"Welsh", 67}, |
| |
294 }; |
| |
295 |
| |
296 icq_ArrayType icq_Genders[] = { |
| |
297 {"Not Specified", 0}, |
| |
298 {"Female", 1}, |
| |
299 {"Male", 2}, |
| |
300 }; |
| |
301 |
| |
302 void hex_dump(char *data, long size) |
| |
303 { |
| |
304 long i; |
| |
305 long x; |
| |
306 int r=1; |
| |
307 char d[32]; |
| |
308 unsigned char bfr[64]; |
| |
309 for(i=0, x=0;;i++) |
| |
310 { |
| |
311 if(i<size) |
| |
312 { |
| |
313 if(x==0) |
| |
314 printf("%04lx: ", i); |
| |
315 #ifdef _WIN32 |
| |
316 sprintf(d, "%08x", data[i]); |
| |
317 #else |
| |
318 snprintf(d, 9, "%08x", data[i]); |
| |
319 #endif |
| |
320 printf("%c%c ", d[6], d[7]); |
| |
321 bfr[x]=data[i]; |
| |
322 if(bfr[x]<0x20) |
| |
323 bfr[x]='.'; |
| |
324 if(bfr[x]>0x7f) |
| |
325 bfr[x]='.'; |
| |
326 } |
| |
327 else |
| |
328 { |
| |
329 if(x==0) |
| |
330 break; |
| |
331 else |
| |
332 { |
| |
333 printf(" "); |
| |
334 bfr[x]=' '; |
| |
335 r=0; |
| |
336 } |
| |
337 } |
| |
338 x++; |
| |
339 if(!(x<16)) |
| |
340 { |
| |
341 bfr[x]=0; |
| |
342 printf("%s\n", bfr); |
| |
343 x=0; |
| |
344 if(!r) |
| |
345 break; |
| |
346 } |
| |
347 } |
| |
348 } |
| |
349 |
| |
350 /* simple mapping for udp status->tcp status translation function */ |
| |
351 struct icq_TCPStatusRec |
| |
352 { |
| |
353 unsigned long udp_status; |
| |
354 long tcp_status; |
| |
355 } icq_TCPStatusMap[] = { |
| |
356 { STATUS_OFFLINE , 0 }, |
| |
357 { STATUS_ONLINE , ICQ_TCP_STATUS_ONLINE }, |
| |
358 { STATUS_INVISIBLE , ICQ_TCP_STATUS_INVISIBLE }, |
| |
359 { STATUS_NA , ICQ_TCP_STATUS_NA }, |
| |
360 { STATUS_FREE_CHAT , ICQ_TCP_STATUS_FREE_CHAT }, |
| |
361 { STATUS_OCCUPIED , ICQ_TCP_STATUS_OCCUPIED }, |
| |
362 { STATUS_AWAY , ICQ_TCP_STATUS_AWAY }, |
| |
363 { STATUS_DND , ICQ_TCP_STATUS_DND }, |
| |
364 }; |
| |
365 |
| |
366 long icq_TCPXlateStatus(unsigned long udp_status) |
| |
367 { |
| |
368 unsigned int i; |
| |
369 for(i=0;i<sizeof(icq_TCPStatusMap)/sizeof(struct icq_TCPStatusRec);i++) |
| |
370 if(icq_TCPStatusMap[i].udp_status==udp_status) |
| |
371 return icq_TCPStatusMap[i].tcp_status; |
| |
372 /* warning: "couldn't find tcp status for %d, assuming 0", udp_status */ |
| |
373 return 0; |
| |
374 } |
| |
375 |
| |
376 void icq_FmtLog(icq_Link *icqlink, int level, const char *fmt, ...) |
| |
377 { |
| |
378 char buffer[2048]; |
| |
379 va_list ap; |
| |
380 va_start(ap, fmt); |
| |
381 |
| |
382 if(!icqlink) |
| |
383 return; |
| |
384 #ifdef _MSVC_ |
| |
385 _vsnprintf(buffer, 1024, fmt, ap); |
| |
386 #else |
| |
387 (void)vsnprintf(buffer, 1024, fmt, ap); |
| |
388 #endif |
| |
389 va_end(ap); |
| |
390 |
| |
391 if(icqlink->icq_Log && icq_LogLevel>=level) |
| |
392 (*icqlink->icq_Log)(icqlink, time(0L), level, buffer); |
| |
393 } |
| |
394 |
| |
395 /** |
| |
396 * Following functions used by qsort and bsearch to |
| |
397 * compare 2 pointers to icq_ArrayType object. |
| |
398 */ |
| |
399 int array_code_compare(const void *x, const void *y) |
| |
400 { |
| |
401 return ((icq_ArrayType*)x)->code - ((icq_ArrayType*)y)->code; |
| |
402 } |
| |
403 |
| |
404 const char *icq_GetCountryName(unsigned short code) |
| |
405 { |
| |
406 icq_ArrayType *res,key; |
| |
407 |
| |
408 key.code=code; |
| |
409 res=bsearch(&key, icq_Countries, sizeof(icq_Countries)/sizeof(icq_ArrayType), |
| |
410 sizeof(icq_ArrayType), array_code_compare); |
| |
411 |
| |
412 return res?res->name:"Unknown"; |
| |
413 } |
| |
414 |
| |
415 const char *icq_GetMetaOccupationName(unsigned short code) |
| |
416 { |
| |
417 icq_ArrayType *res,key; |
| |
418 |
| |
419 key.code=code; |
| |
420 res=bsearch(&key, icq_MetaOccupation, sizeof(icq_MetaOccupation)/sizeof(icq_ArrayType), |
| |
421 sizeof(icq_ArrayType), array_code_compare); |
| |
422 |
| |
423 return res?res->name:"Unknown"; |
| |
424 } |
| |
425 |
| |
426 const char *icq_GetMetaBackgroundName(unsigned short code) |
| |
427 { |
| |
428 icq_ArrayType *res,key; |
| |
429 |
| |
430 key.code=code; |
| |
431 res=bsearch(&key, icq_MetaPastBackgrounds, sizeof(icq_MetaPastBackgrounds)/sizeof(icq_ArrayType), |
| |
432 sizeof(icq_ArrayType), array_code_compare); |
| |
433 |
| |
434 return res?res->name:"Unknown"; |
| |
435 } |
| |
436 |
| |
437 const char *icq_GetMetaAffiliationName(unsigned short code) |
| |
438 { |
| |
439 icq_ArrayType *res,key; |
| |
440 |
| |
441 key.code=code; |
| |
442 res=bsearch(&key, icq_MetaAffiliations, sizeof(icq_MetaAffiliations)/sizeof(icq_ArrayType), |
| |
443 sizeof(icq_ArrayType), array_code_compare); |
| |
444 |
| |
445 return res?res->name:"Unknown"; |
| |
446 } |
| |
447 |
| |
448 const char *icq_GetMetaLanguageName(unsigned short code) |
| |
449 { |
| |
450 icq_ArrayType *res,key; |
| |
451 |
| |
452 key.code=code; |
| |
453 res=bsearch(&key, icq_MetaLanguages, sizeof(icq_MetaLanguages)/sizeof(icq_ArrayType), |
| |
454 sizeof(icq_ArrayType), array_code_compare); |
| |
455 |
| |
456 return res?res->name:"Unknown"; |
| |
457 } |
| |
458 |
| |
459 /******************************************** |
| |
460 returns a string describing the status or |
| |
461 a "Error" if no such string exists |
| |
462 *********************************************/ |
| |
463 const char *icq_ConvertStatus2Str(unsigned long status) |
| |
464 { |
| |
465 if((unsigned long)STATUS_OFFLINE == status) /* this because -1 & 0x01FF is not -1 */ |
| |
466 return "Offline"; |
| |
467 |
| |
468 if((status & STATUS_INVISIBLE) == STATUS_INVISIBLE) |
| |
469 return "Invisible"; |
| |
470 else if((status & STATUS_FREE_CHAT) == STATUS_FREE_CHAT) |
| |
471 return "Free for chat"; |
| |
472 else if((status & STATUS_DND) == STATUS_DND) |
| |
473 return "Do not disturb"; |
| |
474 else if((status & STATUS_OCCUPIED) == STATUS_OCCUPIED) |
| |
475 return "Occupied"; |
| |
476 else if((status & STATUS_NA) == STATUS_NA) |
| |
477 return "Not available"; |
| |
478 else if((status & STATUS_AWAY) == STATUS_AWAY) |
| |
479 return "Away"; |
| |
480 else if(!(status & 0x01FF)) |
| |
481 return "Online"; |
| |
482 else |
| |
483 return "Error"; |
| |
484 } |
| |
485 |
| |
486 int icq_SplitFields(icq_List *strList, const char *str) |
| |
487 { |
| |
488 char *tmpBuf, *tmp, *ptr; |
| |
489 int count = 0; |
| |
490 |
| |
491 tmpBuf = (char*)malloc(strlen(str)+1); |
| |
492 strcpy(tmpBuf, str); |
| |
493 ptr = tmpBuf; |
| |
494 |
| |
495 while(ptr) |
| |
496 { |
| |
497 char *p; |
| |
498 tmp = strchr(ptr, 0xFE); |
| |
499 if(tmp != 0L) |
| |
500 { |
| |
501 *tmp = 0; |
| |
502 tmp++; |
| |
503 } |
| |
504 count++; |
| |
505 p = (char *)malloc(strlen(ptr)+1); |
| |
506 strcpy(p, ptr); |
| |
507 icq_ListEnqueue(strList, p); |
| |
508 ptr = tmp; |
| |
509 } |
| |
510 |
| |
511 free(tmpBuf); |
| |
512 return count; |
| |
513 } |