]> jfr.im git - irc/evilnet/x3.git/blobdiff - src/hash.c
Minor typo in previous commit where returning 0 when it should have been 1 from opser...
[irc/evilnet/x3.git] / src / hash.c
index f1a043d74ad5c410625999ec17cff215435a5a5d..80ac00223cb2c1e491b96778370796f121df844a 100644 (file)
@@ -39,7 +39,7 @@ unsigned int max_clients, invis_clients;
 time_t max_clients_time;
 struct userList curr_opers;
 
-static void hash_cleanup(void);
+static void hash_cleanup(void *extra);
 
 void init_structs(void)
 {
@@ -47,7 +47,7 @@ void init_structs(void)
     clients = dict_new();
     servers = dict_new();
     userList_init(&curr_opers);
-    reg_exit_func(hash_cleanup);
+    reg_exit_func(hash_cleanup, NULL);
 }
 
 int userList_contains(struct userList *list, struct userNode *user)
@@ -63,21 +63,36 @@ int userList_contains(struct userList *list, struct userNode *user)
 }
 
 server_link_func_t *slf_list;
+void **slf_list_extra;
 unsigned int slf_size = 0, slf_used = 0;
 
 void
-reg_server_link_func(server_link_func_t handler)
+reg_server_link_func(server_link_func_t handler, void *extra)
 {
     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));
+            slf_list_extra = realloc(slf_list_extra, slf_size*sizeof(void*));
+        } else {
+            slf_size = 8;
+            slf_list = malloc(slf_size*sizeof(server_link_func_t));
+            slf_list_extra = malloc(slf_size*sizeof(void*));
+        }
+    }
+    slf_list[slf_used] = handler;
+    slf_list_extra[slf_used++] = extra;
+}
+
+void
+call_server_link_funcs(struct server *server)
+{
+    unsigned int i;
+
+    for (i = 0; i < slf_used; ++i)
+    {
+        slf_list[i](server, slf_list_extra[i]);
     }
-    slf_list[slf_used++] = handler;
 }
 
 struct server*
@@ -87,69 +102,104 @@ GetServerH(const char *name)
 }
 
 new_user_func_t *nuf_list;
+void **nuf_list_extra;
 unsigned int nuf_size = 0, nuf_used = 0;
 
 void
-reg_new_user_func(new_user_func_t handler)
+reg_new_user_func(new_user_func_t handler, void *extra)
 {
     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));
+            nuf_list_extra = realloc(nuf_list_extra, nuf_size*sizeof(void*));
+        } else {
+            nuf_size = 8;
+            nuf_list = malloc(nuf_size*sizeof(new_user_func_t));
+            nuf_list_extra = malloc(nuf_size*sizeof(void*));
+        }
+    }
+    nuf_list[nuf_used] = handler;
+    nuf_list_extra[nuf_used++] = extra;
+}
+
+void
+call_new_user_funcs(struct userNode* user)
+{
+    unsigned int i;
+
+    for (i = 0; i < nuf_used && !(user->dead); ++i)
+    {
+        nuf_list[i](user, nuf_list_extra[i]);
     }
-    nuf_list[nuf_used++] = handler;
 }
 
 static nick_change_func_t *ncf2_list;
+static void **ncf2_list_extra;
 static unsigned int ncf2_size = 0, ncf2_used = 0;
 
 void
-reg_nick_change_func(nick_change_func_t handler)
+reg_nick_change_func(nick_change_func_t handler, void *extra)
 {
     if (ncf2_used == ncf2_size) {
         if (ncf2_size) {
             ncf2_size <<= 1;
             ncf2_list = realloc(ncf2_list, ncf2_size*sizeof(nick_change_func_t));
+            ncf2_list_extra = realloc(ncf2_list_extra, ncf2_size*sizeof(void*));
         } else {
             ncf2_size = 8;
             ncf2_list = malloc(ncf2_size*sizeof(nick_change_func_t));
+            ncf2_list_extra = malloc(ncf2_size*sizeof(void*));
         }
     }
-    ncf2_list[ncf2_used++] = handler;
+    ncf2_list[ncf2_used] = handler;
+    ncf2_list_extra[ncf2_used++] = extra;
 }
 
 
 del_user_func_t *duf_list;
+void **duf_list_extra;
 unsigned int duf_size = 0, duf_used = 0;
 
 void
-reg_del_user_func(del_user_func_t handler)
+reg_del_user_func(del_user_func_t handler, void *extra)
 {
     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));
+            duf_list_extra = realloc(duf_list_extra, duf_size*sizeof(void*));
+        } else {
+            duf_size = 8;
+            duf_list = malloc(duf_size*sizeof(del_user_func_t));
+            duf_list_extra = malloc(duf_size*sizeof(void*));
+        }
     }
-    duf_list[duf_used++] = handler;
+    duf_list[duf_used] = handler;
+    duf_list_extra[duf_used++] = extra;
 }
 
 void
-unreg_del_user_func(del_user_func_t handler)
+call_del_user_funcs(struct userNode *user, struct userNode *killer, const char *why)
+{
+    unsigned int i;
+
+    for (i = 0; i < duf_used; ++i)
+    {
+        duf_list[i](user, killer, why, duf_list_extra[i]);
+    }
+}
+
+void
+unreg_del_user_func(del_user_func_t handler, void *extra)
 {
     unsigned int i;
     for (i=0; i<duf_used; i++) {
-        if (duf_list[i] == handler) break;
+        if (duf_list[i] == handler && duf_list_extra[i] == extra) break;
     }
     if (i == duf_used) return;
     memmove(duf_list+i, duf_list+i+1, (duf_used-i-1)*sizeof(duf_list[0]));
+    memmove(duf_list_extra+i, duf_list_extra+i+1, (duf_used-i-1)*sizeof(duf_list_extra[0]));
     duf_used--;
 }
 
@@ -202,7 +252,7 @@ NickChange(struct userNode* user, const char *new_nick, int no_announce)
      * place because that is slightly more useful.
      */
     for (nn=0; (nn<ncf2_used) && !user->dead; nn++)
-        ncf2_list[nn](user, old_nick);
+        ncf2_list[nn](user, old_nick, ncf2_list_extra[nn]);
     user->timestamp = now;
     if (IsLocal(user) && !no_announce)
         irc_nick(user, old_nick);
@@ -237,7 +287,7 @@ SVSNickChange(struct userNode* user, const char *new_nick)
      * place because that is slightly more useful.
      */
     for (nn=0; (nn<ncf2_used) && !user->dead; nn++)
-        ncf2_list[nn](user, old_nick);
+        ncf2_list[nn](user, old_nick, ncf2_list_extra[nn]);
     user->timestamp = now;
 
     free(old_nick);
@@ -353,39 +403,47 @@ set_geoip_info(struct userNode *user)
 }
 
 static new_channel_func_t *ncf_list;
+static void **ncf_list_extra;
 static unsigned int ncf_size = 0, ncf_used = 0;
 
 void
-reg_new_channel_func(new_channel_func_t handler)
+reg_new_channel_func(new_channel_func_t handler, void *extra)
 {
     if (ncf_used == ncf_size) {
        if (ncf_size) {
            ncf_size <<= 1;
            ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0]));
+        ncf_list_extra = realloc(ncf_list_extra, ncf_size*sizeof(void*));
        } else {
            ncf_size = 8;
            ncf_list = malloc(ncf_size*sizeof(ncf_list[0]));
+        ncf_list_extra = malloc(ncf_size*sizeof(void*));
        }
     }
-    ncf_list[ncf_used++] = handler;
+    ncf_list[ncf_used] = handler;
+    ncf_list_extra[ncf_used++] = extra;
 }
 
 static join_func_t *jf_list;
+static void **jf_list_extra;
 static unsigned int jf_size = 0, jf_used = 0;
 
 void
-reg_join_func(join_func_t handler)
+reg_join_func(join_func_t handler, void *extra)
 {
     if (jf_used == jf_size) {
        if (jf_size) {
            jf_size <<= 1;
            jf_list = realloc(jf_list, jf_size*sizeof(join_func_t));
+        jf_list_extra = realloc(jf_list_extra, jf_size*sizeof(void*));
        } else {
            jf_size = 8;
            jf_list = malloc(jf_size*sizeof(join_func_t));
+        jf_list_extra = malloc(jf_size*sizeof(void*));
        }
     }
-    jf_list[jf_used++] = handler;
+    jf_list[jf_used] = handler;
+    jf_list_extra[jf_used++] = extra;
 }
 
 int rel_age;
@@ -499,7 +557,7 @@ AddChannel(const char *name, time_t time_, const char *modes, char *banlist, cha
     /* if it's a new or updated channel, make callbacks */
     if (rel_age > 0)
         for (nn=0; nn<ncf_used; nn++)
-            ncf_list[nn](cNode);
+            ncf_list[nn](cNode, ncf_list_extra[nn]);
 
     /* go through list of bans and add each one */
     if (banlist && (rel_age >= 0)) {
@@ -539,21 +597,25 @@ AddChannel(const char *name, time_t time_, const char *modes, char *banlist, cha
 }
 
 static del_channel_func_t *dcf_list;
+static void **dcf_list_extra;
 static unsigned int dcf_size = 0, dcf_used = 0;
 
 void
-reg_del_channel_func(del_channel_func_t handler)
+reg_del_channel_func(del_channel_func_t handler, void *extra)
 {
     if (dcf_used == dcf_size) {
        if (dcf_size) {
            dcf_size <<= 1;
            dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0]));
+        dcf_list_extra = realloc(dcf_list_extra, dcf_size*sizeof(void*));
        } else {
            dcf_size = 8;
            dcf_list = malloc(dcf_size*sizeof(dcf_list[0]));
+        dcf_list_extra = malloc(dcf_size*sizeof(dcf_list_extra[0]));
        }
     }
-    dcf_list[dcf_used++] = handler;
+    dcf_list[dcf_used] = handler;
+    dcf_list_extra[dcf_used++] = extra;
 }
 
 static void
@@ -583,7 +645,7 @@ DelChannel(struct chanNode *channel)
     channel->exemptlist.used = 0;
 
     for (n=0; n<dcf_used; n++)
-        dcf_list[n](channel);
+        dcf_list[n](channel, dcf_list_extra[n]);
 
     modeList_clean(&channel->members);
     banList_clean(&channel->banlist);
@@ -619,17 +681,19 @@ AddChannelUser(struct userNode *user, struct chanNode* channel)
 
         if (channel->members.used == 1
             && !(channel->modes & MODE_REGISTERED)
-            && !(channel->modes & MODE_APASS))
+            && !(channel->modes & MODE_APASS)) {
             mNode->modes |= MODE_CHANOP;
+            log_module(MAIN_LOG, LOG_DEBUG, "setting op");
+        }
 
         if (IsLocal(user)) {
             irc_join(user, channel);
         }
 
-        for (n=0; n<jf_used; n++) {
+        for (n=0; (n<jf_used) && !user->dead; n++) {
             /* Callbacks return true if they kick or kill the user,
              * and we can continue without removing mNode. */
-            if (jf_list[n](mNode))
+            if (jf_list[n](mNode, jf_list_extra[n]))
                 return NULL;
         }
 
@@ -637,33 +701,38 @@ AddChannelUser(struct userNode *user, struct chanNode* channel)
 }
 
 static part_func_t *pf_list;
+static void **pf_list_extra;
 static unsigned int pf_size = 0, pf_used = 0;
 
 void
-reg_part_func(part_func_t handler)
+reg_part_func(part_func_t handler, void *extra)
 {
     if (pf_used == pf_size) {
        if (pf_size) {
            pf_size <<= 1;
            pf_list = realloc(pf_list, pf_size*sizeof(part_func_t));
+        pf_list_extra = realloc(pf_list_extra, pf_size*sizeof(void*));
        } else {
            pf_size = 8;
            pf_list = malloc(pf_size*sizeof(part_func_t));
+        pf_list_extra = malloc(pf_size*sizeof(void*));
        }
     }
-    pf_list[pf_used++] = handler;
+    pf_list[pf_used] = handler;
+    pf_list_extra[pf_used++] = extra;
 }
 
 void
-unreg_part_func(part_func_t handler)
+unreg_part_func(part_func_t handler, void *extra)
 {
     unsigned int i;
     for (i=0; i<pf_used; i++)
-        if (pf_list[i] == handler)
+        if (pf_list[i] == handler && pf_list_extra[i] == extra)
             break;
     if (i == pf_used)
         return;
     memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0]));
+    memmove(pf_list_extra+i, pf_list_extra+i+1, (pf_used-i-1)*sizeof(pf_list_extra[0]));
     pf_used--;
 }
 
@@ -704,7 +773,7 @@ DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reas
 
     /* make callbacks */
     for (n=0; n<pf_used; n++)
-       pf_list[n](mNode, reason);
+       pf_list[n](mNode, reason, pf_list_extra[n]);
 
     /* free memory */
     free(mNode);
@@ -716,6 +785,7 @@ DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reas
 }
 
 static kick_func_t *kf_list;
+static void **kf_list_extra;
 static unsigned int kf_size = 0, kf_used = 0;
 
 void
@@ -728,7 +798,7 @@ KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNo
 
     /* This may break things, but lets see.. -Rubin */
     for (n=0; n<kf_used; n++)
-        kf_list[n](kicker, target, channel);
+        kf_list[n](kicker, target, channel, kf_list_extra[n]);
 
     /* don't remove them from the channel, since the server will send a PART */
     irc_kick(kicker, target, channel, why);
@@ -742,18 +812,21 @@ KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNo
 }
 
 void
-reg_kick_func(kick_func_t handler)
+reg_kick_func(kick_func_t handler, void *extra)
 {
     if (kf_used == kf_size) {
        if (kf_size) {
            kf_size <<= 1;
            kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t));
+        kf_list_extra = realloc(kf_list_extra, kf_size*sizeof(void*));
        } else {
            kf_size = 8;
            kf_list = malloc(kf_size*sizeof(kick_func_t));
+        kf_list_extra = malloc(kf_size*sizeof(void*));
        }
     }
-    kf_list[kf_used++] = handler;
+    kf_list[kf_used] = handler;
+    kf_list_extra[kf_used++] = extra;
 }
 
 void
@@ -770,7 +843,7 @@ ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanN
         mn->idle_since = now;
 
     for (n=0; n<kf_used; n++)
-       kf_list[n](kicker, victim, channel);
+       kf_list[n](kicker, victim, channel, kf_list_extra[n]);
 
     DelChannelUser(victim, channel, 0, 0);
 
@@ -799,21 +872,25 @@ int ChannelExemptExists(struct chanNode *channel, const char *exempt)
 }
 
 static topic_func_t *tf_list;
+static void **tf_list_extra;
 static unsigned int tf_size = 0, tf_used = 0;
 
 void
-reg_topic_func(topic_func_t handler)
+reg_topic_func(topic_func_t handler, void *extra)
 {
     if (tf_used == tf_size) {
        if (tf_size) {
            tf_size <<= 1;
            tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t));
+        tf_list_extra = realloc(tf_list_extra, tf_size*sizeof(void*));
        } else {
            tf_size = 8;
            tf_list = malloc(tf_size*sizeof(topic_func_t));
+        tf_list_extra = malloc(tf_size*sizeof(void*));
        }
     }
-    tf_list[tf_used++] = handler;
+    tf_list[tf_used] = handler;
+    tf_list_extra[tf_used++] = extra;
 }
 
 void
@@ -845,7 +922,7 @@ SetChannelTopic(struct chanNode *channel, struct userNode *service, struct userN
              * that it has reverted the topic change, and that further
              * hooks should not be called.
              */
-           if (tf_list[n](user, channel, old_topic))
+           if (tf_list[n](user, channel, old_topic, tf_list_extra[n]))
                 break;
     }
 }
@@ -920,7 +997,7 @@ DEFINE_LIST(channelList, struct chanNode*)
 DEFINE_LIST(serverList, struct server*)
 
 static void
-hash_cleanup(void)
+hash_cleanup(UNUSED_ARG(void *extra))
 {
     dict_iterator_t it, next;
 
@@ -935,13 +1012,23 @@ hash_cleanup(void)
     userList_clean(&curr_opers);
 
     free(slf_list);
+    free(slf_list_extra);
     free(nuf_list);
+    free(nuf_list_extra);
     free(ncf2_list);
+    free(ncf2_list_extra);
     free(duf_list);
+    free(duf_list_extra);
     free(ncf_list);
+    free(ncf_list_extra);
     free(jf_list);
+    free(jf_list_extra);
     free(dcf_list);
+    free(dcf_list_extra);
     free(pf_list);
+    free(pf_list_extra);
     free(kf_list);
+    free(kf_list_extra);
     free(tf_list);
+    free(tf_list_extra);
 }