diff --git a/include/h.h b/include/h.h index c4e1da556..accdedb5b 100644 --- a/include/h.h +++ b/include/h.h @@ -129,6 +129,7 @@ extern int match_ipv6(struct IN_ADDR *addr, struct IN_ADDR *mask, int bits); #endif extern ConfigItem_ban *Find_ban_ip(aClient *sptr); extern void add_ListItem(ListStruct *, ListStruct **); +extern void add_ListItemPrio(ListStructPrio *, ListStructPrio **, int); extern ListStruct *del_ListItem(ListStruct *, ListStruct **); extern aClient *find_match_server(char *mask); extern MODVAR LoopStruct loop; diff --git a/include/struct.h b/include/struct.h index 7afda3893..c383bbaa1 100644 --- a/include/struct.h +++ b/include/struct.h @@ -118,6 +118,7 @@ typedef struct _configitem_include ConfigItem_include; typedef struct _configitem_help ConfigItem_help; typedef struct _configitem_offchans ConfigItem_offchans; typedef struct liststruct ListStruct; +typedef struct liststructprio ListStructPrio; #define CFG_TIME 0x0001 #define CFG_SIZE 0x0002 @@ -1509,14 +1510,19 @@ struct DSlink { }; #define AddListItem(item,list) add_ListItem((ListStruct *)item, (ListStruct **)&list) #define DelListItem(item,list) del_ListItem((ListStruct *)item, (ListStruct **)&list) -/* Backwards compatibility */ -#define add_ConfigItem(item,list) add_ListItem((ListStruct *)item, (ListStruct **)&list) -#define del_ConfigItem(item,list) del_ListItem((ListStruct *)item, (ListStruct **)&list) + +#define AddListItemPrio(item,list,prio) add_ListItemPrio((ListStructPrio *)item, (ListStructPrio **)&list, prio) +#define DelListItemPrio(item,list,prio) del_ListItem((ListStruct *)item, (ListStruct **)&list) struct liststruct { ListStruct *prev, *next; }; +struct liststructprio { + ListStructPrio *prev, *next; + int priority; +}; + /* channel structure */ diff --git a/src/list.c b/src/list.c index 4ab1b0ed5..e30a1fc8f 100644 --- a/src/list.c +++ b/src/list.c @@ -574,3 +574,49 @@ ListStruct *del_ListItem(ListStruct *item, ListStruct **list) { } return NULL; } + +/** Add item to list with a 'priority'. + * If there are multiple items with the same priority then it will be + * added as the last item within. + */ +void add_ListItemPrio(ListStructPrio *new, ListStructPrio **list, int priority) +{ + ListStructPrio *x, *last = NULL; + + if (!*list) + { + /* We are the only item. Easy. */ + *list = new; + return; + } + + for (x = *list; x; x = x->next) + { + last = x; + if (x->priority >= priority) + break; + } + + if (x) + { + if (x->prev) + { + /* We will insert ourselves just before this item */ + new->prev = x->prev; + new->next = x; + x->prev->next = new; + x->prev = new; + } else { + /* We are the new head */ + *list = new; + new->next = x; + x->prev = new; + } + } else + { + /* We are the last item */ + last->next = new; + new->prev = last; + } +} + diff --git a/src/modules.c b/src/modules.c index cc81e9af4..39b5e5d18 100644 --- a/src/modules.c +++ b/src/modules.c @@ -1244,42 +1244,8 @@ Hook *HookAddMain(Module *module, int hooktype, int priority, int (*func)(), voi AddListItem(hookobj, module->objects); module->errorcode = MODERR_NOERROR; } - - if (!Hooks[hooktype]) - { - /* Nobody else using this hook. Easy. */ - Hooks[hooktype] = p; - } else { - /* There are existing entries. Put our entry in the list, taking into account 'priority'. */ - Hook *h, *last = NULL; - for (h = Hooks[hooktype]; h; h = h->next) - { - last = h; - if (h->priority >= priority) - break; - } - if (h) - { - if (h->prev) - { - /* We will insert ourselves just before this item */ - p->prev = h->prev; - p->next = h; - h->prev->next = p; - h->prev = p; - } else { - /* We are the new head */ - Hooks[hooktype] = p; - p->next = h; - h->prev = p; - } - } else - { - /* We are the last item */ - last->next = p; - p->prev = last; - } - } + + AddListItemPrio(p, Hooks[hooktype], p->priority); return p; }