Fri, 31 Jan 2003 13:03:47 +0000
[gaim-migrate @ 4766]
aim_user is dead. long live gaim_account.
| 3630 | 1 | #ifdef HAVE_CONFIG_H |
| 2 | #include <config.h> | |
| 3 | #endif /* HAVE_CONFIG_H */ | |
| 4 | ||
| 3664 | 5 | #include <sys/types.h> |
| 6 | /*this must happen before sys/socket.h or freebsd won't compile*/ | |
| 7 | ||
| 3630 | 8 | #ifndef _WIN32 |
| 9 | #include <syslog.h> | |
| 10 | #include <strings.h> | |
| 11 | #include <sys/param.h> | |
| 12 | #include <sys/socket.h> | |
| 13 | #include <netinet/in.h> | |
| 14 | #include <netdb.h> | |
| 15 | #include <arpa/inet.h> | |
| 16 | #include <unistd.h> | |
| 17 | #else | |
| 18 | #include <winsock.h> | |
| 19 | #endif | |
| 3127 | 20 | |
| 21 | #include <string.h> | |
| 22 | #include <stdlib.h> | |
| 23 | #include <stdio.h> | |
| 24 | #include <setjmp.h> | |
| 25 | #include <sys/stat.h> | |
| 26 | #include <fcntl.h> | |
| 27 | #include <errno.h> | |
| 28 | #include <signal.h> | |
| 29 | #include <sys/time.h> | |
| 30 | #include <stdarg.h> | |
| 31 | #include <ctype.h> | |
| 32 | #include <time.h> | |
| 33 | ||
| 34 | #include "xmlparse.h" | |
| 35 | ||
| 36 | /* | |
| 37 | ** Arrange to use either varargs or stdargs | |
| 38 | */ | |
| 39 | ||
| 40 | #define MAXSHORTSTR 203 /* max short string length */ | |
| 41 | #define QUAD_T unsigned long long | |
| 42 | ||
| 43 | #ifdef __STDC__ | |
| 44 | ||
| 45 | #include <stdarg.h> | |
| 46 | ||
| 47 | # define VA_LOCAL_DECL va_list ap; | |
| 48 | # define VA_START(f) va_start(ap, f) | |
| 49 | # define VA_END va_end(ap) | |
| 50 | ||
| 51 | #else /* __STDC__ */ | |
| 52 | ||
| 53 | # include <varargs.h> | |
| 54 | ||
| 55 | # define VA_LOCAL_DECL va_list ap; | |
| 56 | # define VA_START(f) va_start(ap) | |
| 57 | # define VA_END va_end(ap) | |
| 58 | ||
| 59 | #endif /* __STDC__ */ | |
| 60 | ||
| 61 | ||
| 62 | #ifndef INCL_LIB_H | |
| 63 | #define INCL_LIB_H | |
| 64 | ||
| 65 | #ifdef __cplusplus | |
| 66 | extern "C" { | |
| 67 | #endif | |
| 68 | ||
| 69 | ||
| 70 | #ifndef HAVE_SNPRINTF | |
| 71 | extern int ap_snprintf(char *, size_t, const char *, ...); | |
| 72 | #define snprintf ap_snprintf | |
| 73 | #endif | |
| 74 | ||
| 75 | #ifndef HAVE_VSNPRINTF | |
| 76 | extern int ap_vsnprintf(char *, size_t, const char *, va_list ap); | |
| 77 | #define vsnprintf ap_vsnprintf | |
| 78 | #endif | |
| 79 | ||
| 80 | #define ZONE zonestr(__FILE__,__LINE__) | |
| 81 | char *zonestr(char *file, int line); | |
| 82 | ||
| 83 | /* --------------------------------------------------------- */ | |
| 84 | /* */ | |
| 85 | /* Pool-based memory management routines */ | |
| 86 | /* */ | |
| 87 | /* --------------------------------------------------------- */ | |
| 88 | ||
| 89 | #undef POOL_DEBUG | |
| 90 | /* | |
| 91 | flip these, this should be a prime number for top # of pools debugging | |
| 92 | #define POOL_DEBUG 40009 | |
| 93 | */ | |
| 94 | ||
| 95 | /* pheap - singular allocation of memory */ | |
| 96 | struct pheap | |
| 97 | { | |
| 98 | void *block; | |
| 99 | int size, used; | |
| 100 | }; | |
| 101 | ||
| 102 | /* pool_cleaner - callback type which is associated | |
| 103 | with a pool entry; invoked when the pool entry is | |
| 104 | free'd */ | |
| 105 | typedef void (*pool_cleaner)(void *arg); | |
| 106 | ||
| 107 | /* pfree - a linked list node which stores an | |
| 108 | allocation chunk, plus a callback */ | |
| 109 | struct pfree | |
| 110 | { | |
| 111 | pool_cleaner f; | |
| 112 | void *arg; | |
| 113 | struct pheap *heap; | |
| 114 | struct pfree *next; | |
| 115 | }; | |
| 116 | ||
| 117 | /* pool - base node for a pool. Maintains a linked list | |
| 118 | of pool entries (pfree) */ | |
| 119 | typedef struct pool_struct | |
| 120 | { | |
| 121 | int size; | |
| 122 | struct pfree *cleanup; | |
| 123 | struct pheap *heap; | |
| 124 | #ifdef POOL_DEBUG | |
| 125 | char name[8], zone[32]; | |
| 126 | int lsize; | |
| 127 | } _pool, *pool; | |
| 128 | #define pool_new() _pool_new(ZONE) | |
| 129 | #define pool_heap(i) _pool_new_heap(i,ZONE) | |
| 130 | #else | |
| 131 | } _pool, *pool; | |
| 132 | #define pool_heap(i) _pool_new_heap(i,NULL) | |
| 133 | #define pool_new() _pool_new(NULL) | |
| 134 | #endif | |
| 135 | ||
| 136 | pool _pool_new(char *zone); /* new pool :) */ | |
| 137 | pool _pool_new_heap(int size, char *zone); /* creates a new memory pool with an initial heap size */ | |
| 138 | void *pmalloc(pool p, int size); /* wrapper around malloc, takes from the pool, cleaned up automatically */ | |
| 139 | void *pmalloc_x(pool p, int size, char c); /* Wrapper around pmalloc which prefils buffer with c */ | |
| 140 | void *pmalloco(pool p, int size); /* YAPW for zeroing the block */ | |
| 141 | char *pstrdup(pool p, const char *src); /* wrapper around strdup, gains mem from pool */ | |
| 142 | void pool_stat(int full); /* print to stderr the changed pools and reset */ | |
| 143 | char *pstrdupx(pool p, const char *src); /* temp stub */ | |
| 144 | void pool_cleanup(pool p, pool_cleaner f, void *arg); /* calls f(arg) before the pool is freed during cleanup */ | |
| 145 | void pool_free(pool p); /* calls the cleanup functions, frees all the data on the pool, and deletes the pool itself */ | |
| 146 | int pool_size(pool p); /* returns total bytes allocated in this pool */ | |
| 147 | ||
| 148 | ||
| 149 | ||
| 150 | ||
| 151 | /* --------------------------------------------------------- */ | |
| 152 | /* */ | |
| 153 | /* Socket helper stuff */ | |
| 154 | /* */ | |
| 155 | /* --------------------------------------------------------- */ | |
| 156 | #ifndef MAXHOSTNAMELEN | |
| 157 | #define MAXHOSTNAMELEN 64 | |
| 158 | #endif | |
| 159 | ||
| 160 | #define NETSOCKET_SERVER 0 | |
| 161 | #define NETSOCKET_CLIENT 1 | |
| 162 | #define NETSOCKET_UDP 2 | |
| 163 | ||
| 164 | int make_netsocket(u_short port, char *host, int type); | |
| 165 | struct in_addr *make_addr(char *host); | |
| 166 | int set_fd_close_on_exec(int fd, int flag); | |
| 167 | ||
| 168 | ||
| 169 | /* --------------------------------------------------------- */ | |
| 170 | /* */ | |
| 171 | /* String management routines */ | |
| 172 | /* */ | |
| 173 | /* --------------------------------------------------------- */ | |
| 174 | char *j_strdup(const char *str); /* provides NULL safe strdup wrapper */ | |
| 175 | char *j_strcat(char *dest, char *txt); /* strcpy() clone */ | |
| 176 | int j_strcmp(const char *a, const char *b); /* provides NULL safe strcmp wrapper */ | |
| 177 | int j_strcasecmp(const char *a, const char *b); /* provides NULL safe strcasecmp wrapper */ | |
| 178 | int j_strncmp(const char *a, const char *b, int i); /* provides NULL safe strncmp wrapper */ | |
| 179 | int j_strncasecmp(const char *a, const char *b, int i); /* provides NULL safe strncasecmp wrapper */ | |
| 180 | int j_strlen(const char *a); /* provides NULL safe strlen wrapper */ | |
| 181 | int j_atoi(const char *a, int def); /* checks for NULL and uses default instead, convienence */ | |
| 182 | void str_b64decode(char *str); /* what it says */ | |
| 183 | ||
| 184 | ||
| 185 | /* --------------------------------------------------------- */ | |
| 186 | /* */ | |
| 187 | /* SHA calculations */ | |
| 188 | /* */ | |
| 189 | /* --------------------------------------------------------- */ | |
| 190 | #if (SIZEOF_INT == 4) | |
| 191 | typedef unsigned int uint32; | |
| 192 | #elif (SIZEOF_SHORT == 4) | |
| 193 | typedef unsigned short uint32; | |
| 194 | #else | |
| 195 | typedef unsigned int uint32; | |
| 196 | #endif /* HAVEUINT32 */ | |
| 197 | ||
| 198 | char *shahash(char *str); /* NOT THREAD SAFE */ | |
| 199 | void shahash_r(const char* str, char hashbuf[40]); /* USE ME */ | |
| 200 | ||
| 201 | int strprintsha(char *dest, int *hashval); | |
| 202 | ||
| 203 | ||
| 204 | /* --------------------------------------------------------- */ | |
| 205 | /* */ | |
| 206 | /* Hashtable functions */ | |
| 207 | /* */ | |
| 208 | /* --------------------------------------------------------- */ | |
| 209 | typedef struct xhn_struct | |
| 210 | { | |
| 211 | struct xhn_struct *next; | |
| 212 | const char *key; | |
| 213 | void *val; | |
| 214 | } *xhn, _xhn; | |
| 215 | ||
| 216 | typedef struct xht_struct | |
| 217 | { | |
| 218 | pool p; | |
| 219 | int prime; | |
| 220 | struct xhn_struct *zen; | |
| 221 | } *xht, _xht; | |
| 222 | ||
| 223 | xht xhash_new(int prime); | |
| 224 | void xhash_put(xht h, const char *key, void *val); | |
| 225 | void *xhash_get(xht h, const char *key); | |
| 226 | void xhash_zap(xht h, const char *key); | |
| 227 | void xhash_free(xht h); | |
| 228 | typedef void (*xhash_walker)(xht h, const char *key, void *val, void *arg); | |
| 229 | void xhash_walk(xht h, xhash_walker w, void *arg); | |
| 230 | ||
| 231 | /* --------------------------------------------------------- */ | |
| 232 | /* */ | |
| 233 | /* DEPRECIATED Hashtable functions */ | |
| 234 | /* */ | |
| 235 | /* --------------------------------------------------------- */ | |
| 236 | typedef int (*KEYHASHFUNC)(const void *key); | |
| 237 | typedef int (*KEYCOMPAREFUNC)(const void *key1, const void *key2); | |
| 238 | typedef int (*TABLEWALKFUNC)(void *user_data, const void *key, void *data); | |
| 239 | ||
| 240 | typedef void *HASHTABLE; | |
| 241 | ||
| 242 | HASHTABLE ghash_create(int buckets, KEYHASHFUNC hash, KEYCOMPAREFUNC cmp); | |
| 243 | HASHTABLE ghash_create_pool(pool p, int buckets, KEYHASHFUNC hash, KEYCOMPAREFUNC cmp); | |
| 244 | void ghash_destroy(HASHTABLE tbl); | |
| 245 | void *ghash_get(HASHTABLE tbl, const void *key); | |
| 246 | int ghash_put(HASHTABLE tbl, const void *key, void *value); | |
| 247 | int ghash_remove(HASHTABLE tbl, const void *key); | |
| 248 | int ghash_walk(HASHTABLE tbl, TABLEWALKFUNC func, void *user_data); | |
| 249 | int str_hash_code(const char *s); | |
| 250 | ||
| 251 | ||
| 252 | /* --------------------------------------------------------- */ | |
| 253 | /* */ | |
| 254 | /* XML escaping utils */ | |
| 255 | /* */ | |
| 256 | /* --------------------------------------------------------- */ | |
| 257 | char *strescape(pool p, char *buf); /* Escape <>&'" chars */ | |
| 258 | char *strunescape(pool p, char *buf); | |
| 259 | ||
| 260 | ||
| 261 | /* --------------------------------------------------------- */ | |
| 262 | /* */ | |
| 263 | /* String pools (spool) functions */ | |
| 264 | /* */ | |
| 265 | /* --------------------------------------------------------- */ | |
| 266 | struct spool_node | |
| 267 | { | |
| 268 | char *c; | |
| 269 | struct spool_node *next; | |
| 270 | }; | |
| 271 | ||
| 272 | typedef struct spool_struct | |
| 273 | { | |
| 274 | pool p; | |
| 275 | int len; | |
| 276 | struct spool_node *last; | |
| 277 | struct spool_node *first; | |
| 278 | } *spool; | |
| 279 | ||
| 280 | spool spool_new(pool p); /* create a string pool */ | |
| 281 | void spooler(spool s, ...); /* append all the char * args to the pool, terminate args with s again */ | |
| 282 | char *spool_print(spool s); /* return a big string */ | |
| 283 | void spool_add(spool s, char *str); /* add a single char to the pool */ | |
| 284 | char *spools(pool p, ...); /* wrap all the spooler stuff in one function, the happy fun ball! */ | |
| 285 | ||
| 286 | ||
| 287 | /* --------------------------------------------------------- */ | |
| 288 | /* */ | |
| 289 | /* xmlnodes - Document Object Model */ | |
| 290 | /* */ | |
| 291 | /* --------------------------------------------------------- */ | |
| 292 | #define NTYPE_TAG 0 | |
| 293 | #define NTYPE_ATTRIB 1 | |
| 294 | #define NTYPE_CDATA 2 | |
| 295 | ||
| 296 | #define NTYPE_LAST 2 | |
| 297 | #define NTYPE_UNDEF -1 | |
| 298 | ||
| 299 | /* -------------------------------------------------------------------------- | |
| 300 | Node structure. Do not use directly! Always use accessor macros | |
| 301 | and methods! | |
| 302 | -------------------------------------------------------------------------- */ | |
| 303 | typedef struct xmlnode_t | |
| 304 | { | |
| 305 | char* name; | |
| 306 | unsigned short type; | |
| 307 | char* data; | |
| 308 | int data_sz; | |
| 309 | int complete; | |
| 310 | pool p; | |
| 311 | struct xmlnode_t* parent; | |
| 312 | struct xmlnode_t* firstchild; | |
| 313 | struct xmlnode_t* lastchild; | |
| 314 | struct xmlnode_t* prev; | |
| 315 | struct xmlnode_t* next; | |
| 316 | struct xmlnode_t* firstattrib; | |
| 317 | struct xmlnode_t* lastattrib; | |
| 318 | } _xmlnode, *xmlnode; | |
| 319 | ||
| 320 | /* Node creation routines */ | |
| 321 | xmlnode xmlnode_wrap(xmlnode x,const char* wrapper); | |
| 322 | xmlnode xmlnode_new_tag(const char* name); | |
| 323 | xmlnode xmlnode_new_tag_pool(pool p, const char* name); | |
| 324 | xmlnode xmlnode_insert_tag(xmlnode parent, const char* name); | |
| 325 | xmlnode xmlnode_insert_cdata(xmlnode parent, const char* CDATA, unsigned int size); | |
| 326 | xmlnode xmlnode_insert_tag_node(xmlnode parent, xmlnode node); | |
| 327 | void xmlnode_insert_node(xmlnode parent, xmlnode node); | |
| 328 | xmlnode xmlnode_str(char *str, int len); | |
| 329 | xmlnode xmlnode_file(char *file); | |
| 330 | char* xmlnode_file_borked(char *file); /* same as _file but returns the parsing error */ | |
| 331 | xmlnode xmlnode_dup(xmlnode x); /* duplicate x */ | |
| 332 | xmlnode xmlnode_dup_pool(pool p, xmlnode x); | |
| 333 | ||
| 334 | /* Node Memory Pool */ | |
| 335 | pool xmlnode_pool(xmlnode node); | |
| 336 | xmlnode _xmlnode_new(pool p, const char *name, unsigned int type); | |
| 337 | ||
| 338 | /* Node editing */ | |
| 339 | void xmlnode_hide(xmlnode child); | |
| 340 | void xmlnode_hide_attrib(xmlnode parent, const char *name); | |
| 341 | ||
| 342 | /* Node deletion routine, also frees the node pool! */ | |
| 343 | void xmlnode_free(xmlnode node); | |
| 344 | ||
| 345 | /* Locates a child tag by name and returns it */ | |
| 346 | xmlnode xmlnode_get_tag(xmlnode parent, const char* name); | |
| 347 | char* xmlnode_get_tag_data(xmlnode parent, const char* name); | |
| 348 | ||
| 349 | /* Attribute accessors */ | |
| 350 | void xmlnode_put_attrib(xmlnode owner, const char* name, const char* value); | |
| 351 | char* xmlnode_get_attrib(xmlnode owner, const char* name); | |
| 352 | void xmlnode_put_expat_attribs(xmlnode owner, const char** atts); | |
| 353 | ||
| 354 | /* Bastard am I, but these are fun for internal use ;-) */ | |
| 355 | void xmlnode_put_vattrib(xmlnode owner, const char* name, void *value); | |
| 356 | void* xmlnode_get_vattrib(xmlnode owner, const char* name); | |
| 357 | ||
| 358 | /* Node traversal routines */ | |
| 359 | xmlnode xmlnode_get_firstattrib(xmlnode parent); | |
| 360 | xmlnode xmlnode_get_firstchild(xmlnode parent); | |
| 361 | xmlnode xmlnode_get_lastchild(xmlnode parent); | |
| 362 | xmlnode xmlnode_get_nextsibling(xmlnode sibling); | |
| 363 | xmlnode xmlnode_get_prevsibling(xmlnode sibling); | |
| 364 | xmlnode xmlnode_get_parent(xmlnode node); | |
| 365 | ||
| 366 | /* Node information routines */ | |
| 367 | char* xmlnode_get_name(xmlnode node); | |
| 368 | char* xmlnode_get_data(xmlnode node); | |
| 369 | int xmlnode_get_datasz(xmlnode node); | |
| 370 | int xmlnode_get_type(xmlnode node); | |
| 371 | ||
| 372 | int xmlnode_has_children(xmlnode node); | |
| 373 | int xmlnode_has_attribs(xmlnode node); | |
| 374 | ||
| 375 | /* Node-to-string translation */ | |
| 376 | char* xmlnode2str(xmlnode node); | |
| 377 | ||
| 378 | /* Node-to-terminated-string translation | |
| 379 | -- useful for interfacing w/ scripting langs */ | |
| 380 | char* xmlnode2tstr(xmlnode node); | |
| 381 | ||
| 382 | int xmlnode_cmp(xmlnode a, xmlnode b); /* compares a and b for equality */ | |
| 383 | ||
| 384 | int xmlnode2file(char *file, xmlnode node); /* writes node to file */ | |
| 385 | ||
| 386 | /* Expat callbacks */ | |
| 387 | void expat_startElement(void* userdata, const char* name, const char** atts); | |
| 388 | void expat_endElement(void* userdata, const char* name); | |
| 389 | void expat_charData(void* userdata, const char* s, int len); | |
| 390 | ||
| 391 | /*********************** | |
| 392 | * XSTREAM Section | |
| 393 | ***********************/ | |
| 394 | ||
| 395 | #define XSTREAM_MAXNODE 1000000 | |
| 396 | #define XSTREAM_MAXDEPTH 100 | |
| 397 | ||
| 398 | #define XSTREAM_ROOT 0 /* root element */ | |
| 399 | #define XSTREAM_NODE 1 /* normal node */ | |
| 400 | #define XSTREAM_CLOSE 2 /* closed </stream:stream> */ | |
| 401 | #define XSTREAM_ERR 4 /* parser error */ | |
| 402 | ||
| 403 | typedef void (*xstream_onNode)(int type, xmlnode x, void *arg); /* xstream event handler */ | |
| 404 | ||
| 405 | typedef struct xstream_struct | |
| 406 | { | |
| 407 | XML_Parser parser; | |
| 408 | xmlnode node; | |
| 409 | char *cdata; | |
| 410 | int cdata_len; | |
| 411 | pool p; | |
| 412 | xstream_onNode f; | |
| 413 | void *arg; | |
| 414 | int status; | |
| 415 | int depth; | |
| 416 | } *xstream, _xstream; | |
| 417 | ||
| 418 | xstream xstream_new(pool p, xstream_onNode f, void *arg); /* create a new xstream */ | |
| 419 | int xstream_eat(xstream xs, char *buff, int len); /* parse new data for this xstream, returns last XSTREAM_* status */ | |
| 420 | ||
| 421 | /* convience functions */ | |
| 422 | xmlnode xstream_header(char *namespace, char *to, char *from); | |
| 423 | char *xstream_header_char(xmlnode x); | |
| 424 | ||
| 425 | ||
| 426 | typedef struct { | |
| 427 | unsigned long H[5]; | |
| 428 | unsigned long W[80]; | |
| 429 | int lenW; | |
| 430 | unsigned long sizeHi,sizeLo; | |
| 431 | } j_SHA_CTX; | |
| 432 | ||
| 433 | ||
| 434 | void shaInit(j_SHA_CTX *ctx); | |
| 435 | void shaUpdate(j_SHA_CTX *ctx, unsigned char *dataIn, int len); | |
| 436 | void shaFinal(j_SHA_CTX *ctx, unsigned char hashout[20]); | |
| 437 | void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]); | |
| 438 | ||
| 439 | /********** END OLD libxode.h BEGIN OLD jabber.h *************/ | |
| 440 | ||
| 441 | /* --------------------------------------------------------- */ | |
| 442 | /* */ | |
| 443 | /* JID structures & constants */ | |
| 444 | /* */ | |
| 445 | /* --------------------------------------------------------- */ | |
| 446 | #define JID_RESOURCE 1 | |
| 447 | #define JID_USER 2 | |
| 448 | #define JID_SERVER 4 | |
| 449 | ||
| 450 | typedef struct jid_struct | |
| 451 | { | |
| 452 | pool p; | |
| 453 | char* resource; | |
| 454 | char* user; | |
| 455 | char* server; | |
| 456 | char* full; | |
| 457 | struct jid_struct *next; /* for lists of jids */ | |
| 458 | } *jid; | |
| 459 | ||
| 460 | jid jid_new(pool p, char *idstr); /* Creates a jabber id from the idstr */ | |
| 461 | void jid_set(jid id, char *str, int item); /* Individually sets jid components */ | |
| 462 | char* jid_full(jid id); /* Builds a string type=user/resource@server from the jid data */ | |
| 463 | int jid_cmp(jid a, jid b); /* Compares two jid's, returns 0 for perfect match */ | |
| 464 | int jid_cmpx(jid a, jid b, int parts); /* Compares just the parts specified as JID_|JID_ */ | |
| 465 | jid jid_append(jid a, jid b); /* Appending b to a (list), no dups */ | |
| 466 | xmlnode jid_xres(jid id); /* Returns xmlnode representation of the resource?query=string */ | |
| 467 | xmlnode jid_nodescan(jid id, xmlnode x); /* Scans the children of the node for a matching jid attribute */ | |
| 468 | jid jid_user(jid a); /* returns the same jid but just of the user@host part */ | |
| 469 | ||
| 470 | ||
| 471 | /* --------------------------------------------------------- */ | |
| 472 | /* */ | |
| 473 | /* JPacket structures & constants */ | |
| 474 | /* */ | |
| 475 | /* --------------------------------------------------------- */ | |
| 476 | #define JPACKET_UNKNOWN 0x00 | |
| 477 | #define JPACKET_MESSAGE 0x01 | |
| 478 | #define JPACKET_PRESENCE 0x02 | |
| 479 | #define JPACKET_IQ 0x04 | |
| 480 | #define JPACKET_S10N 0x08 | |
| 481 | ||
| 482 | #define JPACKET__UNKNOWN 0 | |
| 483 | #define JPACKET__NONE 1 | |
| 484 | #define JPACKET__ERROR 2 | |
| 485 | #define JPACKET__CHAT 3 | |
| 486 | #define JPACKET__GROUPCHAT 4 | |
| 487 | #define JPACKET__GET 5 | |
| 488 | #define JPACKET__SET 6 | |
| 489 | #define JPACKET__RESULT 7 | |
| 490 | #define JPACKET__SUBSCRIBE 8 | |
| 491 | #define JPACKET__SUBSCRIBED 9 | |
| 492 | #define JPACKET__UNSUBSCRIBE 10 | |
| 493 | #define JPACKET__UNSUBSCRIBED 11 | |
| 494 | #define JPACKET__AVAILABLE 12 | |
| 495 | #define JPACKET__UNAVAILABLE 13 | |
| 496 | #define JPACKET__PROBE 14 | |
| 497 | #define JPACKET__HEADLINE 15 | |
| 498 | #define JPACKET__INVISIBLE 16 | |
| 499 | ||
| 500 | typedef struct jpacket_struct | |
| 501 | { | |
| 502 | unsigned char type; | |
| 503 | int subtype; | |
| 504 | int flag; | |
| 505 | void* aux1; | |
| 506 | xmlnode x; | |
| 507 | jid to; | |
| 508 | jid from; | |
| 509 | char* iqns; | |
| 510 | xmlnode iq; | |
| 511 | pool p; | |
| 512 | } *jpacket, _jpacket; | |
| 513 | ||
| 514 | jpacket jpacket_new(xmlnode x); /* Creates a jabber packet from the xmlnode */ | |
| 515 | jpacket jpacket_reset(jpacket p); /* Resets the jpacket values based on the xmlnode */ | |
| 516 | int jpacket_subtype(jpacket p); /* Returns the subtype value (looks at xmlnode for it) */ | |
| 517 | ||
| 518 | ||
| 519 | /* --------------------------------------------------------- */ | |
| 520 | /* */ | |
| 521 | /* Presence Proxy DB structures & constants */ | |
| 522 | /* */ | |
| 523 | /* --------------------------------------------------------- */ | |
| 524 | typedef struct ppdb_struct | |
| 525 | { | |
| 526 | jid id; /* entry data */ | |
| 527 | int pri; | |
| 528 | xmlnode x; | |
| 529 | struct ppdb_struct* user; /* linked list for user@server */ | |
| 530 | pool p; /* db-level data */ | |
| 531 | struct ppdb_struct* next; | |
| 532 | } _ppdb, *ppdb; | |
| 533 | ||
| 534 | ppdb ppdb_insert(ppdb db, jid id, xmlnode x); /* Inserts presence into the proxy */ | |
| 535 | xmlnode ppdb_primary(ppdb db, jid id); /* Fetches the matching primary presence for the id */ | |
| 536 | void ppdb_free(ppdb db); /* Frees the db and all entries */ | |
| 537 | xmlnode ppdb_get(ppdb db, jid id); /* Called successively to return each presence xmlnode */ | |
| 538 | /* for the id and children, returns NULL at the end */ | |
| 539 | ||
| 540 | ||
| 541 | /* --------------------------------------------------------- */ | |
| 542 | /* */ | |
| 543 | /* Simple Jabber Rate limit functions */ | |
| 544 | /* */ | |
| 545 | /* --------------------------------------------------------- */ | |
| 546 | typedef struct jlimit_struct | |
| 547 | { | |
| 548 | char *key; | |
| 549 | int start; | |
| 550 | int points; | |
| 551 | int maxt, maxp; | |
| 552 | pool p; | |
| 553 | } *jlimit, _jlimit; | |
| 554 | ||
| 555 | jlimit jlimit_new(int maxt, int maxp); | |
| 556 | void jlimit_free(jlimit r); | |
| 557 | int jlimit_check(jlimit r, char *key, int points); | |
| 558 | ||
| 559 | ||
| 560 | // #define KARMA_DEBUG | |
| 561 | // default to disable karma | |
| 562 | #define KARMA_READ_MAX(k) (abs(k)*100) /* how much you are allowed to read off the sock */ | |
| 563 | #define KARMA_INIT 5 /* internal "init" value */ | |
| 564 | #define KARMA_HEARTBEAT 2 /* seconds to register for heartbeat */ | |
| 565 | #define KARMA_MAX 10 /* total max karma you can have */ | |
| 566 | #define KARMA_INC 1 /* how much to increment every KARMA_HEARTBEAT seconds */ | |
| 567 | #define KARMA_DEC 0 /* how much to penalize for reading KARMA_READ_MAX in | |
| 568 | KARMA_HEARTBEAT seconds */ | |
| 569 | #define KARMA_PENALTY -5 /* where you go when you hit 0 karma */ | |
| 570 | #define KARMA_RESTORE 5 /* where you go when you payed your penelty or INIT */ | |
| 571 | #define KARMA_RESETMETER 0 /* Reset byte meter on restore default is falst */ | |
| 572 | ||
| 573 | struct karma | |
| 574 | { | |
| 575 | int init; /* struct initialized */ | |
| 576 | int reset_meter; /* reset the byte meter on restore */ | |
| 577 | int val; /* current karma value */ | |
| 578 | long bytes; /* total bytes read (in that time period) */ | |
| 579 | int max; /* max karma you can have */ | |
| 580 | int inc,dec; /* how much to increment/decrement */ | |
| 581 | int penalty,restore; /* what penalty (<0) or restore (>0) */ | |
| 582 | time_t last_update; /* time this was last incremented */ | |
| 583 | }; | |
| 584 | ||
| 585 | struct karma *karma_new(pool p); /* creates a new karma object, with default values */ | |
| 586 | void karma_copy(struct karma *new, struct karma *old); /* makes a copy of old in new */ | |
| 587 | void karma_increment(struct karma *k); /* inteligently increments karma */ | |
| 588 | void karma_decrement(struct karma *k, long bytes_read); /* inteligently decrements karma */ | |
| 589 | int karma_check(struct karma *k,long bytes_read); /* checks to see if we have good karma */ | |
| 590 | ||
| 591 | ||
| 592 | ||
| 593 | /* --------------------------------------------------------- */ | |
| 594 | /* */ | |
| 595 | /* Error structures & constants */ | |
| 596 | /* */ | |
| 597 | /* --------------------------------------------------------- */ | |
| 598 | typedef struct terror_struct | |
| 599 | { | |
| 600 | int code; | |
| 601 | char msg[64]; | |
| 602 | } terror; | |
| 603 | ||
| 604 | #define TERROR_BAD (terror){400,"Bad Request"} | |
| 605 | #define TERROR_AUTH (terror){401,"Unauthorized"} | |
| 606 | #define TERROR_PAY (terror){402,"Payment Required"} | |
| 607 | #define TERROR_FORBIDDEN (terror){403,"Forbidden"} | |
| 608 | #define TERROR_NOTFOUND (terror){404,"Not Found"} | |
| 609 | #define TERROR_NOTALLOWED (terror){405,"Not Allowed"} | |
| 610 | #define TERROR_NOTACCEPTABLE (terror){406,"Not Acceptable"} | |
| 611 | #define TERROR_REGISTER (terror){407,"Registration Required"} | |
| 612 | #define TERROR_REQTIMEOUT (terror){408,"Request Timeout"} | |
| 613 | #define TERROR_CONFLICT (terror){409,"Conflict"} | |
| 614 | ||
| 615 | #define TERROR_INTERNAL (terror){500,"Internal Server Error"} | |
| 616 | #define TERROR_NOTIMPL (terror){501,"Not Implemented"} | |
| 617 | #define TERROR_EXTERNAL (terror){502,"Remote Server Error"} | |
| 618 | #define TERROR_UNAVAIL (terror){503,"Service Unavailable"} | |
| 619 | #define TERROR_EXTTIMEOUT (terror){504,"Remote Server Timeout"} | |
| 620 | #define TERROR_DISCONNECTED (terror){510,"Disconnected"} | |
| 621 | ||
| 622 | /* --------------------------------------------------------- */ | |
| 623 | /* */ | |
| 624 | /* Namespace constants */ | |
| 625 | /* */ | |
| 626 | /* --------------------------------------------------------- */ | |
| 627 | #define NSCHECK(x,n) (j_strcmp(xmlnode_get_attrib(x,"xmlns"),n) == 0) | |
| 628 | ||
| 629 | #define NS_CLIENT "jabber:client" | |
| 630 | #define NS_SERVER "jabber:server" | |
| 631 | #define NS_AUTH "jabber:iq:auth" | |
| 632 | #define NS_REGISTER "jabber:iq:register" | |
| 633 | #define NS_ROSTER "jabber:iq:roster" | |
| 634 | #define NS_OFFLINE "jabber:x:offline" | |
| 635 | #define NS_AGENT "jabber:iq:agent" | |
| 636 | #define NS_AGENTS "jabber:iq:agents" | |
| 637 | #define NS_DELAY "jabber:x:delay" | |
| 638 | #define NS_VERSION "jabber:iq:version" | |
| 639 | #define NS_TIME "jabber:iq:time" | |
| 640 | #define NS_VCARD "vcard-temp" | |
| 641 | #define NS_PRIVATE "jabber:iq:private" | |
| 642 | #define NS_SEARCH "jabber:iq:search" | |
| 643 | #define NS_OOB "jabber:iq:oob" | |
| 644 | #define NS_XOOB "jabber:x:oob" | |
| 645 | #define NS_ADMIN "jabber:iq:admin" | |
| 646 | #define NS_FILTER "jabber:iq:filter" | |
| 647 | #define NS_AUTH_0K "jabber:iq:auth:0k" | |
| 648 | #define NS_BROWSE "jabber:iq:browse" | |
| 649 | #define NS_EVENT "jabber:x:event" | |
| 650 | #define NS_CONFERENCE "jabber:iq:conference" | |
| 651 | #define NS_SIGNED "jabber:x:signed" | |
| 652 | #define NS_ENCRYPTED "jabber:x:encrypted" | |
| 653 | #define NS_GATEWAY "jabber:iq:gateway" | |
| 654 | #define NS_LAST "jabber:iq:last" | |
| 655 | #define NS_ENVELOPE "jabber:x:envelope" | |
| 656 | #define NS_EXPIRE "jabber:x:expire" | |
| 657 | #define NS_XHTML "http://www.w3.org/1999/xhtml" | |
| 658 | ||
| 659 | #define NS_XDBGINSERT "jabber:xdb:ginsert" | |
| 660 | #define NS_XDBNSLIST "jabber:xdb:nslist" | |
| 661 | ||
| 662 | ||
| 663 | /* --------------------------------------------------------- */ | |
| 664 | /* */ | |
| 665 | /* JUtil functions */ | |
| 666 | /* */ | |
| 667 | /* --------------------------------------------------------- */ | |
| 668 | xmlnode jutil_presnew(int type, char *to, char *status); /* Create a skeleton presence packet */ | |
| 669 | xmlnode jutil_iqnew(int type, char *ns); /* Create a skeleton iq packet */ | |
| 670 | xmlnode jutil_msgnew(char *type, char *to, char *subj, char *body); | |
| 671 | /* Create a skeleton message packet */ | |
| 672 | xmlnode jutil_header(char* xmlns, char* server); /* Create a skeleton stream packet */ | |
| 673 | int jutil_priority(xmlnode x); /* Determine priority of this packet */ | |
| 674 | void jutil_tofrom(xmlnode x); /* Swaps to/from fields on a packet */ | |
| 675 | xmlnode jutil_iqresult(xmlnode x); /* Generate a skeleton iq/result, given a iq/query */ | |
| 676 | char* jutil_timestamp(void); /* Get stringified timestamp */ | |
| 677 | void jutil_error(xmlnode x, terror E); /* Append an <error> node to x */ | |
| 678 | void jutil_delay(xmlnode msg, char *reason); /* Append a delay packet to msg */ | |
| 679 | char* jutil_regkey(char *key, char *seed); /* pass a seed to generate a key, pass the key again to validate (returns it) */ | |
| 680 | ||
| 681 | ||
| 682 | #ifdef __cplusplus | |
| 683 | } | |
| 684 | #endif | |
| 685 | ||
| 686 | #endif /* INCL_LIB_H */ |