From 0c540e883ffed8d9803bb565aafd0aa721ead3ab Mon Sep 17 00:00:00 2001 From: codemastr Date: Fri, 15 Mar 2002 23:35:55 +0000 Subject: [PATCH] Converted commands to use module objects --- Changes | 2 +- include/h.h | 2 +- include/modules.h | 10 +++++++++- include/struct.h | 29 +++++++++++++++-------------- src/modules.c | 3 +++ src/packet.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 6 files changed, 72 insertions(+), 18 deletions(-) diff --git a/Changes b/Changes index ca4558bde..20102cd9a 100644 --- a/Changes +++ b/Changes @@ -1234,4 +1234,4 @@ seen. gmtime warning still there various other people .. - Fixed a /mkpasswd typo and added the updated /mkpasswd syntax to doc/unrealircd.doc, both reported by peck - +- Made the command system use module objects (haven't yet converted modules to use it) diff --git a/include/h.h b/include/h.h index e20adbe93..66f0a35dc 100644 --- a/include/h.h +++ b/include/h.h @@ -440,7 +440,7 @@ char *Inet_ia2p(struct IN_ADDR *ia); */ extern aCommand *CommandHash[256]; extern void init_CommandHash(void); -extern void add_Command_backend(char *cmd, int (*func)(), unsigned char parameters, unsigned char token, int flags); +extern aCommand *add_Command_backend(char *cmd, int (*func)(), unsigned char parameters, unsigned char token, int flags); extern void add_Command(char *cmd, char *token, int (*func)(), unsigned char parameters); extern void add_Command_to_list(aCommand *item, aCommand **list); extern aCommand *del_Command_from_list(aCommand *item, aCommand **list); diff --git a/include/modules.h b/include/modules.h index 12711b5a7..cd7a82d62 100644 --- a/include/modules.h +++ b/include/modules.h @@ -91,13 +91,18 @@ typedef struct { #define MOBJ_HOOK 0x0002 #define MOBJ_COMMAND 0x0004 +typedef struct _command { + struct _command *prev, *next; + aCommand *cmd, *tok; +} Command; + typedef struct _ModuleObject { struct _ModuleObject *prev, *next; short type; union { Event *event; Hook *hook; - aCommand *command; + Command *command; } object; } ModuleObject; @@ -222,6 +227,9 @@ Hook *HookDel(Hook *hook); #define RunHook2(hooktype,x,y) for (global_i = Hooks[hooktype]; global_i; global_i = global_i->next) (*(global_i->func.intfunc))(x,y) +Command *CommandAdd(Module *module, char *cmd, char *tok, int (*func)(), unsigned char params, int flags); +void CommandDel(Command *command); + /* Hook types */ #define HOOKTYPE_LOCAL_QUIT 1 #define HOOKTYPE_LOCAL_NICKCHANGE 2 diff --git a/include/struct.h b/include/struct.h index 65c25c2fb..29471f989 100644 --- a/include/struct.h +++ b/include/struct.h @@ -655,20 +655,6 @@ struct Server { #define M_ALIAS 0x0020 #define M_RESETIDLE 0x0040 -struct Command { - aCommand *prev, *next; - char *cmd; - int (*func) (); - int flags; - unsigned int count; - unsigned parameters : 5; - unsigned token : 1; - unsigned long bytes; -#ifdef DEBUGMODE - unsigned long lticks; - unsigned long rticks; -#endif -}; /* tkl: @@ -1401,6 +1387,21 @@ extern char *gnulicense[]; #define EVENT_HASHES EVENT_DRUGS #include "modules.h" #include "events.h" +struct Command { + aCommand *prev, *next; + char *cmd; + int (*func) (); + int flags; + unsigned int count; + unsigned parameters : 5; + unsigned token : 1; + unsigned long bytes; + Module *owner; +#ifdef DEBUGMODE + unsigned long lticks; + unsigned long rticks; +#endif +}; #endif /* __struct_include__ */ diff --git a/src/modules.c b/src/modules.c index efa50212e..8e0df1970 100644 --- a/src/modules.c +++ b/src/modules.c @@ -293,6 +293,9 @@ int Module_free(Module *mod) else if (objs->type == MOBJ_HOOK) { HookDel(objs->object.hook); } + else if (objs->type == MOBJ_COMMAND) { + CommandDel(objs->object.command); + } } for (p = Modules; p; p = p->next) { diff --git a/src/packet.c b/src/packet.c index ffbee6d6e..c79b1903d 100644 --- a/src/packet.c +++ b/src/packet.c @@ -218,7 +218,7 @@ void init_CommandHash(void) #endif } -void add_Command_backend(char *cmd, int (*func)(), unsigned char parameters, unsigned char token, int flags) +aCommand *add_Command_backend(char *cmd, int (*func)(), unsigned char parameters, unsigned char token, int flags) { aCommand *newcmd = (aCommand *) MyMalloc(sizeof(aCommand)); @@ -232,6 +232,48 @@ void add_Command_backend(char *cmd, int (*func)(), unsigned char parameters, uns /* Add in hash with hash value = first byte */ AddListItem(newcmd, CommandHash[toupper(*cmd)]); + return newcmd; +} + +Command *CommandAdd(Module *module, char *cmd, char *tok, int (*func)(), unsigned char params, int flags) { + Command *command = MyMallocEx(sizeof(Command)); + command->cmd = add_Command_backend(cmd,func,params, 0, flags); + command->tok = NULL; + command->cmd->owner = module; + if (tok) { + command->tok = add_Command_backend(tok,func,params,1,flags); + command->tok->owner = module; + } + if (module) { + ModuleObject *cmdobj = (ModuleObject *)MyMallocEx(sizeof(ModuleObject)); + cmdobj->object.command = command; + cmdobj->type = MOBJ_COMMAND; + AddListItem(cmdobj, module->objects); + } + return command; +} + + void CommandDel(Command *command) { + DelListItem(command->cmd, CommandHash[toupper(*command->cmd->cmd)]); + if (command->tok) + DelListItem(command->tok, CommandHash[toupper(*command->tok->cmd)]); + if (command->cmd->owner) { + ModuleObject *cmdobj; + for (cmdobj = command->cmd->owner->objects; cmdobj; cmdobj = (ModuleObject *)cmdobj->next) { + if (cmdobj->type == MOBJ_COMMAND && cmdobj->object.command == command) { + DelListItem(cmdobj,command->cmd->owner->objects); + MyFree(cmdobj); + break; + } + } + } + MyFree(command->cmd->cmd); + MyFree(command->cmd); + if (command->tok) { + MyFree(command->tok->cmd); + MyFree(command->tok); + } + MyFree(command); } void add_Command(char *cmd, char *token, int (*func)(), unsigned char parameters)