1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
4 * $Id: util.c 2096 2001-07-31 01:00:39Z warmenhoven $
6 * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and
7 * Bill Soudan <soudan@kde.org>
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.
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.
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.
29 #include "stdpackets.h"
32 * This list of countries should be sorted according to country code.
33 * When adding new country, please preserve the order!
35 icq_ArrayType icq_Countries
[] = {
54 {"Czech Republic",42},
64 {"Guantanomo Bay",53},
129 {"French Antilles",596},
132 {"Netherlands Antilles",599},
136 {"Papua New Guinea",675},
138 {"American Samoa",684},
139 {"New Caledonia",687},
140 {"French Polynesia",689},
142 {"Trinidad and Tobago",868},
148 {"Saudia Arabia",966},
151 {"United Arab Emirates",971},
156 {"Slovak Republic",4201},
157 {"Not entered",0xffff},
160 icq_ArrayType icq_MetaOccupation
[] = {
162 {"Administrative", 2},
163 {"Art/Entertainment", 3},
164 {"College Student", 4},
166 {"Community & Social", 6},
169 {"Financial Services", 9},
171 {"High School Student", 11},
173 {"ICQ - Providing Help", 13},
176 {"Manufacturing", 16},
177 {"Medical/Health", 17},
179 {"Non-Government Organization", 19},
180 {"Professional", 20},
183 {"Science & Research", 23},
186 {"University Student", 26},
187 {"Web Building", 27},
188 {"Other Services", 99},
191 icq_ArrayType icq_MetaPastBackgrounds
[] = {
192 {"Elementary School", 300},
193 {"High School", 301},
197 {"Past Work Place", 305},
198 {"Past Organization", 306},
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},
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},
221 {"Voluntary Org.", 218},
225 icq_ArrayType icq_MetaLanguages
[] = {
296 icq_ArrayType icq_Genders
[] = {
297 {"Not Specified", 0},
302 void hex_dump(char *data
, long size
)
308 unsigned char bfr
[64];
314 printf("%04lx: ", i
);
316 sprintf(d
, "%08x", data
[i
]);
318 snprintf(d
, 9, "%08x", data
[i
]);
320 printf("%c%c ", d
[6], d
[7]);
350 /* simple mapping for udp status->tcp status translation function */
351 struct icq_TCPStatusRec
353 unsigned long udp_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
},
366 long icq_TCPXlateStatus(unsigned long udp_status
)
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 */
376 void icq_FmtLog(icq_Link
*icqlink
, int level
, const char *fmt
, ...)
385 _vsnprintf(buffer
, 1024, fmt
, ap
);
387 (void)vsnprintf(buffer
, 1024, fmt
, ap
);
391 if(icqlink
->icq_Log
&& icq_LogLevel
>=level
)
392 (*icqlink
->icq_Log
)(icqlink
, time(0L), level
, buffer
);
396 * Following functions used by qsort and bsearch to
397 * compare 2 pointers to icq_ArrayType object.
399 int array_code_compare(const void *x
, const void *y
)
401 return ((icq_ArrayType
*)x
)->code
- ((icq_ArrayType
*)y
)->code
;
404 const char *icq_GetCountryName(unsigned short code
)
406 icq_ArrayType
*res
,key
;
409 res
=bsearch(&key
, icq_Countries
, sizeof(icq_Countries
)/sizeof(icq_ArrayType
),
410 sizeof(icq_ArrayType
), array_code_compare
);
412 return res
?res
->name
:"Unknown";
415 const char *icq_GetMetaOccupationName(unsigned short code
)
417 icq_ArrayType
*res
,key
;
420 res
=bsearch(&key
, icq_MetaOccupation
, sizeof(icq_MetaOccupation
)/sizeof(icq_ArrayType
),
421 sizeof(icq_ArrayType
), array_code_compare
);
423 return res
?res
->name
:"Unknown";
426 const char *icq_GetMetaBackgroundName(unsigned short code
)
428 icq_ArrayType
*res
,key
;
431 res
=bsearch(&key
, icq_MetaPastBackgrounds
, sizeof(icq_MetaPastBackgrounds
)/sizeof(icq_ArrayType
),
432 sizeof(icq_ArrayType
), array_code_compare
);
434 return res
?res
->name
:"Unknown";
437 const char *icq_GetMetaAffiliationName(unsigned short code
)
439 icq_ArrayType
*res
,key
;
442 res
=bsearch(&key
, icq_MetaAffiliations
, sizeof(icq_MetaAffiliations
)/sizeof(icq_ArrayType
),
443 sizeof(icq_ArrayType
), array_code_compare
);
445 return res
?res
->name
:"Unknown";
448 const char *icq_GetMetaLanguageName(unsigned short code
)
450 icq_ArrayType
*res
,key
;
453 res
=bsearch(&key
, icq_MetaLanguages
, sizeof(icq_MetaLanguages
)/sizeof(icq_ArrayType
),
454 sizeof(icq_ArrayType
), array_code_compare
);
456 return res
?res
->name
:"Unknown";
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
)
465 if((unsigned long)STATUS_OFFLINE
== status
) /* this because -1 & 0x01FF is not -1 */
468 if((status
& STATUS_INVISIBLE
) == STATUS_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
)
476 else if((status
& STATUS_NA
) == STATUS_NA
)
477 return "Not available";
478 else if((status
& STATUS_AWAY
) == STATUS_AWAY
)
480 else if(!(status
& 0x01FF))
486 int icq_SplitFields(icq_List
*strList
, const char *str
)
488 char *tmpBuf
, *tmp
, *ptr
;
491 tmpBuf
= (char*)malloc(strlen(str
)+1);
498 tmp
= strchr(ptr
, 0xFE);
505 p
= (char *)malloc(strlen(ptr
)+1);
507 icq_ListEnqueue(strList
, p
);