X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2Fhash.c;h=a55ad9ba92568bc06f6e3b031953a41bce38f273;hb=80d9ed728be4b02ac483f3339cbb184f6602d15b;hp=c60ec57bdd4e4eb8eff65f181a08014235f4c6a6;hpb=b9b6869615fb2f06800b365dad8566286960a805;p=srvx.git diff --git a/src/hash.c b/src/hash.c index c60ec57..a55ad9b 100644 --- a/src/hash.c +++ b/src/hash.c @@ -28,7 +28,7 @@ dict_t channels; dict_t clients; dict_t servers; unsigned int max_clients, invis_clients; -time_t max_clients_time; +unsigned long max_clients_time; struct userList curr_opers; static void hash_cleanup(void); @@ -42,6 +42,18 @@ void init_structs(void) reg_exit_func(hash_cleanup); } +int userList_contains(struct userList *list, struct userNode *user) +{ + unsigned int ii; + + for (ii = 0; ii < list->used; ++ii) { + if (user == list->list[ii]) { + return 1; + } + } + return 0; +} + server_link_func_t *slf_list; unsigned int slf_size = 0, slf_used = 0; @@ -49,13 +61,13 @@ void reg_server_link_func(server_link_func_t handler) { if (slf_used == slf_size) { - if (slf_size) { - slf_size <<= 1; - slf_list = realloc(slf_list, slf_size*sizeof(server_link_func_t)); - } else { - slf_size = 8; - slf_list = malloc(slf_size*sizeof(server_link_func_t)); - } + if (slf_size) { + slf_size <<= 1; + slf_list = realloc(slf_list, slf_size*sizeof(server_link_func_t)); + } else { + slf_size = 8; + slf_list = malloc(slf_size*sizeof(server_link_func_t)); + } } slf_list[slf_used++] = handler; } @@ -73,13 +85,13 @@ void reg_new_user_func(new_user_func_t handler) { if (nuf_used == nuf_size) { - if (nuf_size) { - nuf_size <<= 1; - nuf_list = realloc(nuf_list, nuf_size*sizeof(new_user_func_t)); - } else { - nuf_size = 8; - nuf_list = malloc(nuf_size*sizeof(new_user_func_t)); - } + if (nuf_size) { + nuf_size <<= 1; + nuf_list = realloc(nuf_list, nuf_size*sizeof(new_user_func_t)); + } else { + nuf_size = 8; + nuf_list = malloc(nuf_size*sizeof(new_user_func_t)); + } } nuf_list[nuf_used++] = handler; } @@ -110,13 +122,13 @@ void reg_del_user_func(del_user_func_t handler) { if (duf_used == duf_size) { - if (duf_size) { - duf_size <<= 1; - duf_list = realloc(duf_list, duf_size*sizeof(del_user_func_t)); - } else { - duf_size = 8; - duf_list = malloc(duf_size*sizeof(del_user_func_t)); - } + if (duf_size) { + duf_size <<= 1; + duf_list = realloc(duf_list, duf_size*sizeof(del_user_func_t)); + } else { + duf_size = 8; + duf_list = malloc(duf_size*sizeof(del_user_func_t)); + } } duf_list[duf_used++] = handler; } @@ -139,13 +151,13 @@ ReintroduceUser(struct userNode *user) { struct mod_chanmode change; unsigned int n; - + irc_user(user); mod_chanmode_init(&change); change.argc = 1; for (n = 0; n < user->channels.used; n++) { struct modeNode *mn = user->channels.list[n]; - irc_join(user, mn->channel); + irc_join(user, mn->channel); if (mn->modes) { change.args[0].mode = mn->modes; change.args[0].u.member = mn; @@ -179,9 +191,11 @@ NickChange(struct userNode* user, const char *new_nick, int no_announce) #endif /* Make callbacks for nick changes. Do this with new nick in - * place because that is slightly more useful. + * place because that is slightly more useful. Stop if the user + * gets killed by any of the hooks, so that later hooks do not get + * confused by the user having disappeared. */ - for (nn=0; nndead; nn++) ncf2_list[nn](user, old_nick); user->timestamp = now; if (IsLocal(user) && !no_announce) @@ -207,7 +221,7 @@ reg_account_func(account_func_t handler) } void -call_account_func(struct userNode *user, const char *stamp) +call_account_func(struct userNode *user, const char *stamp, unsigned long timestamp, unsigned long serial) { /* We've received an account stamp for a user; notify NickServ, which registers the sole account_func @@ -216,7 +230,7 @@ call_account_func(struct userNode *user, const char *stamp) P10 Protocol violation if (user->modes & FLAGS_STAMPED) here. */ if (account_func) - account_func(user, stamp); + account_func(user, stamp, timestamp, serial); #ifdef WITH_PROTOCOL_P10 /* Mark the user so we don't stamp it again. */ @@ -225,7 +239,7 @@ call_account_func(struct userNode *user, const char *stamp) } void -StampUser(struct userNode *user, const char *stamp) +StampUser(struct userNode *user, const char *stamp, unsigned long timestamp, unsigned long serial) { #ifdef WITH_PROTOCOL_P10 /* The P10 protocol says we can't stamp users who already @@ -234,16 +248,19 @@ StampUser(struct userNode *user, const char *stamp) return; #endif - irc_account(user, stamp); + irc_account(user, stamp, timestamp, serial); user->modes |= FLAGS_STAMPED; } void -assign_fakehost(struct userNode *user, const char *host, int announce) +assign_fakehost(struct userNode *user, const char *host, const char *ident, int force, int announce) { - safestrncpy(user->fakehost, host, sizeof(user->fakehost)); + if (host) + safestrncpy(user->fakehost, host, sizeof(user->fakehost)); + if (ident) + safestrncpy(user->fakeident, ident, sizeof(user->ident)); if (announce) - irc_fakehost(user, host); + irc_fakehost(user, host, ident, force); } static new_channel_func_t *ncf_list; @@ -253,13 +270,13 @@ void reg_new_channel_func(new_channel_func_t handler) { if (ncf_used == ncf_size) { - if (ncf_size) { - ncf_size <<= 1; - ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0])); - } else { - ncf_size = 8; - ncf_list = malloc(ncf_size*sizeof(ncf_list[0])); - } + if (ncf_size) { + ncf_size <<= 1; + ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0])); + } else { + ncf_size = 8; + ncf_list = malloc(ncf_size*sizeof(ncf_list[0])); + } } ncf_list[ncf_used++] = handler; } @@ -271,13 +288,13 @@ void reg_join_func(join_func_t handler) { if (jf_used == jf_size) { - if (jf_size) { - jf_size <<= 1; - jf_list = realloc(jf_list, jf_size*sizeof(join_func_t)); - } else { - jf_size = 8; - jf_list = malloc(jf_size*sizeof(join_func_t)); - } + if (jf_size) { + jf_size <<= 1; + jf_list = realloc(jf_list, jf_size*sizeof(join_func_t)); + } else { + jf_size = 8; + jf_list = malloc(jf_size*sizeof(join_func_t)); + } } jf_list[jf_used++] = handler; } @@ -285,10 +302,12 @@ reg_join_func(join_func_t handler) int rel_age; static void -wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned int modec) { +wipeout_channel(struct chanNode *cNode, unsigned long new_time, char **modes, unsigned int modec) { unsigned int orig_limit; chan_mode_t orig_modes; char orig_key[KEYLEN+1]; + char orig_apass[KEYLEN+1]; + char orig_upass[KEYLEN+1]; unsigned int nn, argc; /* nuke old topic */ @@ -300,6 +319,8 @@ wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned orig_modes = cNode->modes; orig_limit = cNode->limit; strcpy(orig_key, cNode->key); + strcpy(orig_upass, cNode->upass); + strcpy(orig_apass, cNode->apass); cNode->modes = 0; mod_chanmode(NULL, cNode, modes, modec, 0); cNode->timestamp = new_time; @@ -323,6 +344,8 @@ wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned change->modes_set = orig_modes; change->new_limit = orig_limit; strcpy(change->new_key, orig_key); + strcpy(change->new_upass, orig_upass); + strcpy(change->new_apass, orig_apass); for (nn = argc = 0; nn < cNode->members.used; ++nn) { struct modeNode *mn = cNode->members.list[nn]; if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) { @@ -339,7 +362,7 @@ wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned } struct chanNode * -AddChannel(const char *name, time_t time_, const char *modes, char *banlist) +AddChannel(const char *name, unsigned long time_, const char *modes, char *banlist) { struct chanNode *cNode; char new_modes[MAXLEN], *argv[MAXNUMPARAMS]; @@ -359,7 +382,8 @@ AddChannel(const char *name, time_t time_, const char *modes, char *banlist) strcpy(cNode->name, name); banList_init(&cNode->banlist); modeList_init(&cNode->members); - mod_chanmode(NULL, cNode, argv, nn, 0); + userList_init(&cNode->invited); + mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER); dict_insert(channels, cNode->name, cNode); cNode->timestamp = time_; rel_age = 1; @@ -367,7 +391,7 @@ AddChannel(const char *name, time_t time_, const char *modes, char *banlist) wipeout_channel(cNode, time_, argv, nn); rel_age = 1; } else if (cNode->timestamp == time_) { - mod_chanmode(NULL, cNode, argv, nn, 0); + mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER); rel_age = 0; } else { rel_age = -1; @@ -409,13 +433,13 @@ void reg_del_channel_func(del_channel_func_t handler) { if (dcf_used == dcf_size) { - if (dcf_size) { - dcf_size <<= 1; - dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0])); - } else { - dcf_size = 8; - dcf_list = malloc(dcf_size*sizeof(dcf_list[0])); - } + if (dcf_size) { + dcf_size <<= 1; + dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0])); + } else { + dcf_size = 8; + dcf_list = malloc(dcf_size*sizeof(dcf_list[0])); + } } dcf_list[dcf_used++] = handler; } @@ -434,7 +458,7 @@ DelChannel(struct chanNode *channel) /* go through all channel members and delete them from the channel */ for (n=channel->members.used; n>0; ) - DelChannelUser(channel->members.list[--n]->user, channel, false, 1); + DelChannelUser(channel->members.list[--n]->user, channel, NULL, 1); /* delete all channel bans */ for (n=channel->banlist.used; n>0; ) @@ -446,49 +470,80 @@ DelChannel(struct chanNode *channel) modeList_clean(&channel->members); banList_clean(&channel->banlist); + userList_clean(&channel->invited); free(channel); } struct modeNode * AddChannelUser(struct userNode *user, struct chanNode* channel) { - struct modeNode *mNode; - unsigned int n; + struct modeNode *mNode; + unsigned int n; - mNode = GetUserMode(channel, user); - if (mNode) + mNode = GetUserMode(channel, user); + if (mNode) return mNode; - mNode = malloc(sizeof(*mNode)); + mNode = malloc(sizeof(*mNode)); - /* set up modeNode */ - mNode->channel = channel; - mNode->user = user; - mNode->modes = 0; + /* set up modeNode */ + mNode->channel = channel; + mNode->user = user; + mNode->modes = 0; + mNode->oplevel = MAXOPLEVEL; mNode->idle_since = now; - /* Add modeNode to channel and to user. + /* Add modeNode to channel and to user. * We have to do this before calling join funcs in case the * modeNode is manipulated (e.g. chanserv ops the user). */ - modeList_append(&channel->members, mNode); - modeList_append(&user->channels, mNode); + modeList_append(&channel->members, mNode); + modeList_append(&user->channels, mNode); if (channel->members.used == 1 - && !(channel->modes & MODE_REGISTERED)) + && !(channel->modes & MODE_REGISTERED) + && !(channel->modes & MODE_APASS)) mNode->modes |= MODE_CHANOP; - for (n=0; ndead; n++) { /* Callbacks return true if they kick or kill the user, * and we can continue without removing mNode. */ if (jf_list[n](mNode)) return NULL; } - if (IsLocal(user)) - irc_join(user, channel); + return mNode; +} - return mNode; +/* Return negative if *(struct modeNode**)pa is "less than" pb, + * positive if pa is "larger than" pb. Comparison is based on sorting + * so that non-voiced/non-opped users are first, voiced-only users are + * next, and the "strongest" oplevels are before "weaker" oplevels. + * Within those sets, ordering is arbitrary. + */ +int +modeNode_sort(const void *pa, const void *pb) +{ + struct modeNode *a = *(struct modeNode**)pa; + struct modeNode *b = *(struct modeNode**)pb; + + if (a->modes & MODE_CHANOP) { + if (!(b->modes & MODE_CHANOP)) + return -1; + else if ((b->modes & MODE_VOICE) != (a->modes & MODE_VOICE)) + return (b->modes & MODE_VOICE) - (a->modes & MODE_VOICE); + else if (a->oplevel != b->oplevel) + return a->oplevel - b->oplevel; + } else if (b->modes & MODE_CHANOP) + return 1; + else if ((b->modes & MODE_VOICE) != (a->modes & MODE_VOICE)) + return (b->modes & MODE_VOICE) - (a->modes & MODE_VOICE); + + return irccasecmp(a->user->nick, b->user->nick); } static part_func_t *pf_list; @@ -498,13 +553,13 @@ void reg_part_func(part_func_t handler) { if (pf_used == pf_size) { - if (pf_size) { - pf_size <<= 1; - pf_list = realloc(pf_list, pf_size*sizeof(part_func_t)); - } else { - pf_size = 8; - pf_list = malloc(pf_size*sizeof(part_func_t)); - } + if (pf_size) { + pf_size <<= 1; + pf_list = realloc(pf_list, pf_size*sizeof(part_func_t)); + } else { + pf_size = 8; + pf_list = malloc(pf_size*sizeof(part_func_t)); + } } pf_list[pf_used++] = handler; } @@ -542,7 +597,7 @@ DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reas struct modeNode* mNode; unsigned int n; - if (reason) + if (IsLocal(user) && reason) irc_part(user, channel, reason); mNode = GetUserMode(channel, user); @@ -559,12 +614,14 @@ DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reas /* make callbacks */ for (n=0; nmembers.used && !channel->locks && !(channel->modes & MODE_REGISTERED)) + /* A single check for APASS only should be enough here */ + if (!deleting && !channel->members.used && !channel->locks + && !(channel->modes & MODE_REGISTERED) && !(channel->modes & MODE_APASS)) DelChannel(channel); } @@ -578,9 +635,9 @@ KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNo if (IsLocal(target)) { - /* NULL reason because we don't want a PART message to be - sent by DelChannelUser. */ - DelChannelUser(target, channel, NULL, 0); + /* NULL reason because we don't want a PART message to be + sent by DelChannelUser. */ + DelChannelUser(target, channel, NULL, 0); } } @@ -591,13 +648,13 @@ void reg_kick_func(kick_func_t handler) { if (kf_used == kf_size) { - if (kf_size) { - kf_size <<= 1; - kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t)); - } else { - kf_size = 8; - kf_list = malloc(kf_size*sizeof(kick_func_t)); - } + if (kf_size) { + kf_size <<= 1; + kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t)); + } else { + kf_size = 8; + kf_list = malloc(kf_size*sizeof(kick_func_t)); + } } kf_list[kf_used++] = handler; } @@ -608,20 +665,20 @@ ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanN unsigned int n; struct modeNode *mn; - if (!victim || !channel || IsService(victim) || !GetUserMode(channel, victim)) + if (!victim || !channel || !GetUserMode(channel, victim)) return; - + /* Update the kicker's idle time (kicker may be null if it was a server) */ if (kicker && (mn = GetUserMode(channel, kicker))) mn->idle_since = now; for (n=0; nbanlist.used; n++) - if (match_ircglobs(channel->banlist.list[n]->ban, ban)) - return 1; + if (match_ircglobs(channel->banlist.list[n]->ban, ban)) + return 1; return 0; } @@ -641,13 +698,13 @@ void reg_topic_func(topic_func_t handler) { if (tf_used == tf_size) { - if (tf_size) { - tf_size <<= 1; - tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t)); - } else { - tf_size = 8; - tf_list = malloc(tf_size*sizeof(topic_func_t)); - } + if (tf_size) { + tf_size <<= 1; + tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t)); + } else { + tf_size = 8; + tf_list = malloc(tf_size*sizeof(topic_func_t)); + } } tf_list[tf_used++] = handler; } @@ -672,12 +729,16 @@ SetChannelTopic(struct chanNode *channel, struct userNode *user, const char *top } if (announce) { - /* We don't really care if a local user messes with the topic, + /* We don't really care if a local user messes with the topic, * so don't call the tf_list functions. */ - irc_topic(user, channel, topic); + irc_topic(user, channel, topic); } else { - for (n=0; nchannels.list); if (channel->members.used < user->channels.used) { - for (n=0; nmembers.used; n++) { + for (n=0; nmembers.used; n++) { verify(channel->members.list[n]); - if (user == channel->members.list[n]->user) { - mn = channel->members.list[n]; - break; - } - } + if (user == channel->members.list[n]->user) { + mn = channel->members.list[n]; + break; + } + } } else { - for (n=0; nchannels.used; n++) { + for (n=0; nchannels.used; n++) { verify(user->channels.list[n]); - if (channel == user->channels.list[n]->channel) { - mn = user->channels.list[n]; - break; - } - } + if (channel == user->channels.list[n]->channel) { + mn = user->channels.list[n]; + break; + } + } } return mn; }