unrealircd- supernets unrealircd source & configuration |
git clone git://git.acid.vegas/unrealircd.git |
Log | Files | Refs | Archive | README | LICENSE |
whowas.c (5156B)
1 /************************************************************************ 2 * IRC - Internet Relay Chat, src/whowas.c 3 * Copyright (C) 1990 Markku Savela 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 1, or (at your option) 8 * any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 */ 19 20 #include "unrealircd.h" 21 22 // FIXME: move this to cmd_whowas, 23 // Consider making add_history an efunc? Or via a hook? 24 // Some users may not want to load cmd_whowas at all. 25 26 void add_whowas_to_clist(WhoWas **, WhoWas *); 27 void del_whowas_from_clist(WhoWas **, WhoWas *); 28 void add_whowas_to_list(WhoWas **, WhoWas *); 29 void del_whowas_from_list(WhoWas **, WhoWas *); 30 31 WhoWas MODVAR WHOWAS[NICKNAMEHISTORYLENGTH]; 32 WhoWas MODVAR *WHOWASHASH[WHOWAS_HASH_TABLE_SIZE]; 33 34 MODVAR int whowas_next = 0; 35 36 void free_whowas_fields(WhoWas *e) 37 { 38 safe_free(e->name); 39 safe_free(e->hostname); 40 safe_free(e->virthost); 41 safe_free(e->realname); 42 safe_free(e->username); 43 safe_free(e->account); 44 safe_free(e->ip); 45 e->servername = NULL; 46 e->event = 0; 47 e->logon = 0; 48 e->logoff = 0; 49 e->connected_since = 0; 50 51 /* Remove from lists and reset hashv */ 52 if (e->online) 53 del_whowas_from_clist(&(e->online->user->whowas), e); 54 del_whowas_from_list(&WHOWASHASH[e->hashv], e); 55 e->hashv = -1; 56 } 57 58 void create_whowas_entry(Client *client, WhoWas *e, WhoWasEvent event) 59 { 60 e->hashv = hash_whowas_name(client->name); 61 e->event = event; 62 e->connected_since = get_creationtime(client); 63 e->logon = client->lastnick; 64 e->logoff = TStime(); 65 e->umodes = client->umodes; 66 safe_strdup(e->name, client->name); 67 safe_strdup(e->username, client->user->username); 68 safe_strdup(e->hostname, client->user->realhost); 69 safe_strdup(e->ip, client->ip); 70 if (client->user->virthost) 71 safe_strdup(e->virthost, client->user->virthost); 72 else 73 safe_strdup(e->virthost, ""); 74 e->servername = client->user->server; 75 safe_strdup(e->realname, client->info); 76 if (strcmp(client->user->account, "0")) 77 safe_strdup(e->account, client->user->account); 78 79 /* Its not string copied, a pointer to the scache hash is copied 80 -Dianora 81 */ 82 /* strlcpy(e->servername, client->user->server,HOSTLEN); */ 83 e->servername = client->user->server; 84 } 85 86 void add_history(Client *client, int online, WhoWasEvent event) 87 { 88 WhoWas *new; 89 90 new = &WHOWAS[whowas_next]; 91 92 if (new->hashv != -1) 93 free_whowas_fields(new); 94 95 create_whowas_entry(client, new, event); 96 97 if (online) 98 { 99 new->online = client; 100 add_whowas_to_clist(&(client->user->whowas), new); 101 } else { 102 new->online = NULL; 103 } 104 add_whowas_to_list(&WHOWASHASH[new->hashv], new); 105 whowas_next++; 106 if (whowas_next == NICKNAMEHISTORYLENGTH) 107 whowas_next = 0; 108 } 109 110 void off_history(Client *client) 111 { 112 WhoWas *temp, *next; 113 114 for (temp = client->user->whowas; temp; temp = next) 115 { 116 next = temp->cnext; 117 temp->online = NULL; 118 del_whowas_from_clist(&(client->user->whowas), temp); 119 } 120 } 121 122 Client *get_history(const char *nick, time_t timelimit) 123 { 124 WhoWas *temp; 125 int blah; 126 127 timelimit = TStime() - timelimit; 128 blah = hash_whowas_name(nick); 129 temp = WHOWASHASH[blah]; 130 for (; temp; temp = temp->next) 131 { 132 if (mycmp(nick, temp->name)) 133 continue; 134 if (temp->logoff < timelimit) 135 continue; 136 return temp->online; 137 } 138 return NULL; 139 } 140 141 void count_whowas_memory(int *wwu, u_long *wwum) 142 { 143 WhoWas *tmp; 144 int i; 145 int u = 0; 146 u_long um = 0; 147 /* count the number of used whowas structs in 'u' */ 148 /* count up the memory used of whowas structs in um */ 149 150 for (i = 0, tmp = &WHOWAS[0]; i < NICKNAMEHISTORYLENGTH; i++, tmp++) 151 if (tmp->hashv != -1) 152 { 153 u++; 154 um += sizeof(WhoWas); 155 } 156 *wwu = u; 157 *wwum = um; 158 return; 159 } 160 161 void initwhowas() 162 { 163 int i; 164 165 for (i = 0; i < NICKNAMEHISTORYLENGTH; i++) 166 { 167 memset(&WHOWAS[i], 0, sizeof(WhoWas)); 168 WHOWAS[i].hashv = -1; 169 } 170 for (i = 0; i < WHOWAS_HASH_TABLE_SIZE; i++) 171 WHOWASHASH[i] = NULL; 172 } 173 174 void add_whowas_to_clist(WhoWas ** bucket, WhoWas * whowas) 175 { 176 whowas->cprev = NULL; 177 if ((whowas->cnext = *bucket) != NULL) 178 whowas->cnext->cprev = whowas; 179 *bucket = whowas; 180 } 181 182 void del_whowas_from_clist(WhoWas ** bucket, WhoWas * whowas) 183 { 184 if (whowas->cprev) 185 whowas->cprev->cnext = whowas->cnext; 186 else 187 *bucket = whowas->cnext; 188 if (whowas->cnext) 189 whowas->cnext->cprev = whowas->cprev; 190 } 191 192 void add_whowas_to_list(WhoWas ** bucket, WhoWas * whowas) 193 { 194 whowas->prev = NULL; 195 if ((whowas->next = *bucket) != NULL) 196 whowas->next->prev = whowas; 197 *bucket = whowas; 198 } 199 200 void del_whowas_from_list(WhoWas ** bucket, WhoWas * whowas) 201 { 202 if (whowas->prev) 203 whowas->prev->next = whowas->next; 204 else 205 *bucket = whowas->next; 206 if (whowas->next) 207 whowas->next->prev = whowas->prev; 208 }