]> jfr.im git - solanum.git/commitdiff
Implement hook priorities
authorEd Kellett <redacted>
Sun, 26 Apr 2020 23:14:56 +0000 (00:14 +0100)
committerEd Kellett <redacted>
Fri, 1 May 2020 16:44:15 +0000 (17:44 +0100)
include/hook.h
include/modules.h
ircd/hook.c
ircd/modules.c

index 099eeec7e37e93870230049a1b0d4a2ca0422989..e185d46b12426efc5df3c1363de8b7f951270ae7 100644 (file)
@@ -11,6 +11,16 @@ typedef struct
        rb_dlink_list hooks;
 } hook;
 
+enum hook_priority
+{
+       HOOK_LOWEST = 10,
+       HOOK_LOW = 20,
+       HOOK_NORMAL = 30,
+       HOOK_HIGH = 40,
+       HOOK_HIGHEST = 50,
+       HOOK_MONITOR = 100
+};
+
 typedef void (*hookfn) (void *data);
 
 extern int h_iosend_id;
@@ -39,6 +49,7 @@ extern int h_rehash;
 void init_hook(void);
 int register_hook(const char *name);
 void add_hook(const char *name, hookfn fn);
+void add_hook_prio(const char *name, hookfn fn, enum hook_priority priority);
 void remove_hook(const char *name, hookfn fn);
 void call_hook(int id, void *arg);
 
index 4953986597c6c1b00b162e2964e813009bdcb08e..9a362c117e56a04fbdce7c715fde2054c126cd55 100644 (file)
@@ -70,9 +70,9 @@ typedef struct
 {
        const char *hapi_name;
        hookfn fn;
+       enum hook_priority priority;
 } mapi_hfn_list_av1;
 
-
 #define MAPI_CAP_CLIENT                1
 #define MAPI_CAP_SERVER                2
 
index 70defd1c3dbfc9f831dfacc0aad94a8fa8a65b10..bda9fc0cce8a5172b047c2bf86fcb9e4346aad64 100644 (file)
@@ -42,6 +42,13 @@ hook *hooks;
 
 #define HOOK_INCREMENT 1000
 
+struct hook_entry
+{
+       rb_dlink_node node;
+       hookfn fn;
+       enum hook_priority priority;
+};
+
 int num_hooks = 0;
 int last_hook = 0;
 int max_hooks = HOOK_INCREMENT;
@@ -174,11 +181,34 @@ register_hook(const char *name)
 void
 add_hook(const char *name, hookfn fn)
 {
+       add_hook_prio(name, fn, HOOK_NORMAL);
+}
+
+/* add_hook_prio()
+ *   Adds a hook with the specified priority
+ */
+void
+add_hook_prio(const char *name, hookfn fn, enum hook_priority priority)
+{
+       rb_dlink_node *ptr;
+       struct hook_entry *entry = rb_malloc(sizeof *entry);
        int i;
 
        i = register_hook(name);
+       entry->fn = fn;
+       entry->priority = priority;
+
+       RB_DLINK_FOREACH(ptr, &hooks[i].hooks.head)
+       {
+               struct hook_entry *o = ptr->data;
+               if (entry->priority < o->priority)
+               {
+                       rb_dlinkAddBefore(ptr, entry, &entry->node, &hooks[i].hooks);
+                       return;
+               }
+       }
 
-       rb_dlinkAddAlloc(fn, &hooks[i].hooks);
+       rb_dlinkAddTail(entry, &entry->node, &hooks[i].hooks);
 }
 
 /* remove_hook()
@@ -187,12 +217,21 @@ add_hook(const char *name, hookfn fn)
 void
 remove_hook(const char *name, hookfn fn)
 {
+       rb_dlink_node *ptr, *scratch;
        int i;
 
        if((i = find_hook(name)) < 0)
                return;
 
-       rb_dlinkFindDestroy(fn, &hooks[i].hooks);
+       RB_DLINK_FOREACH_SAFE(ptr, scratch, &hooks[i].hooks.head)
+       {
+               struct hook_entry *entry = ptr->data;
+               if (entry->fn == fn)
+               {
+                       rb_dlinkDelete(ptr, &hooks[i].hooks);
+                       return;
+               }
+       }
 }
 
 /* call_hook()
@@ -201,7 +240,6 @@ remove_hook(const char *name, hookfn fn)
 void
 call_hook(int id, void *arg)
 {
-       hookfn fn;
        rb_dlink_node *ptr;
 
        /* The ID we were passed is the position in the hook table of this
@@ -209,8 +247,8 @@ call_hook(int id, void *arg)
         */
        RB_DLINK_FOREACH(ptr, hooks[id].hooks.head)
        {
-               fn = ptr->data;
-               fn(arg);
+               struct hook_entry *entry = ptr->data;
+               entry->fn(arg);
        }
 }
 
index 7a246cbe989b9fd09b200790caebbc3a7e6fb8d2..6602a21d8c7c28660b9e71438abb7efee6b491f0 100644 (file)
@@ -586,7 +586,12 @@ load_a_module(const char *path, bool warn, int origin, bool core)
                        {
                                mapi_hfn_list_av1 *m;
                                for (m = mheader->mapi_hfn_list; m->hapi_name; ++m)
+                               {
+                                       int priority = m->priority;
+                                       if (priority == 0)
+                                               priority = HOOK_NORMAL;
                                        add_hook(m->hapi_name, m->fn);
+                               }
                        }
 
                        /* New in MAPI v2 - version replacement */