00001 #include "server.h"
00002
00003 #define UHOST_CACHE_SIZE 47
00004
00005 static hash_table_t *uhost_cache_ht = NULL;
00006
00007
00008 static int uhost_cache_delete(const void *key, void *data, void *param);
00009
00010 void uhost_cache_init()
00011 {
00012 uhost_cache_ht = hash_table_create(NULL, NULL, UHOST_CACHE_SIZE, HASH_TABLE_STRINGS);
00013 }
00014
00015
00016 void uhost_cache_reset()
00017 {
00018 uhost_cache_destroy();
00019 uhost_cache_init();
00020 }
00021
00022 void uhost_cache_destroy()
00023 {
00024 hash_table_walk(uhost_cache_ht, uhost_cache_delete, NULL);
00025 hash_table_delete(uhost_cache_ht);
00026 }
00027
00028 static int uhost_cache_delete(const void *key, void *dataptr, void *param)
00029 {
00030 uhost_cache_entry_t *cache = *(void **)dataptr;
00031
00032 if (cache->nick) free(cache->nick);
00033 if (cache->uhost) free(cache->uhost);
00034 free(cache);
00035 return(0);
00036 }
00037
00038 static uhost_cache_entry_t *cache_lookup(const char *nick)
00039 {
00040 char buf[64], *lnick;
00041 uhost_cache_entry_t *cache = NULL;
00042
00043 lnick = egg_msprintf(buf, sizeof(buf), NULL, "%s", nick);
00044 str_tolower(lnick);
00045 hash_table_find(uhost_cache_ht, lnick, &cache);
00046 if (lnick != buf) free(lnick);
00047 return(cache);
00048 }
00049
00050 char *uhost_cache_lookup(const char *nick)
00051 {
00052 uhost_cache_entry_t *cache;
00053
00054 cache = cache_lookup(nick);
00055 if (cache) return(cache->uhost);
00056 return(NULL);
00057 }
00058
00059 void uhost_cache_addref(const char *nick, const char *uhost)
00060 {
00061 char buf[64], *lnick;
00062 uhost_cache_entry_t *cache = NULL;
00063
00064 lnick = egg_msprintf(buf, sizeof(buf), NULL, "%s", nick);
00065 str_tolower(lnick);
00066 hash_table_find(uhost_cache_ht, lnick, &cache);
00067 if (!cache) {
00068 cache = calloc(1, sizeof(*cache));
00069 cache->nick = strdup(nick);
00070 str_tolower(cache->nick);
00071 if (uhost) cache->uhost = strdup(uhost);
00072 hash_table_insert(uhost_cache_ht, cache->nick, cache);
00073 }
00074 cache->ref_count++;
00075 if (lnick != buf) free(lnick);
00076 }
00077
00078 void uhost_cache_decref(const char *nick)
00079 {
00080 uhost_cache_entry_t *cache;
00081
00082
00083 if (match_my_nick(nick)) return;
00084
00085 cache = cache_lookup(nick);
00086 if (!cache) return;
00087
00088 cache->ref_count--;
00089 if (cache->ref_count <= 0) {
00090 hash_table_remove(uhost_cache_ht, cache->nick, NULL);
00091 uhost_cache_delete(NULL, &cache, NULL);
00092 }
00093 }
00094
00095 void uhost_cache_swap(const char *old_nick, const char *new_nick)
00096 {
00097 uhost_cache_entry_t *cache;
00098 char buf[64], *lnick;
00099
00100 lnick = egg_msprintf(buf, sizeof(buf), NULL, "%s", old_nick);
00101 str_tolower(lnick);
00102 hash_table_remove(uhost_cache_ht, lnick, &cache);
00103 if (lnick != buf) free(lnick);
00104
00105 if (cache) {
00106 str_redup(&cache->nick, new_nick);
00107 str_tolower(cache->nick);
00108 hash_table_insert(uhost_cache_ht, cache->nick, cache);
00109 }
00110 }