Commit a73c01f2 authored by Dustin L. Howett's avatar Dustin L. Howett

Pass command state in a command_state structure instead of adding more...

Pass command state in a command_state structure instead of adding more arguments to command handlers.
parent 2be79ff1
...@@ -10,8 +10,9 @@ ...@@ -10,8 +10,9 @@
extern void *app_handle; extern void *app_handle;
#include "commands.h" #include "commands.h"
#define _COMMAND(name) extern "C" int cmd_ ## name (int argc, char **argv, struct command_state *state)
int dispatch_command(const char *cmdname, int argc, char **argv, char *sender, char *target, bool privmsg) { int dispatch_command(const char *cmdname, int argc, char **argv, struct command_state *state) {
char *namecmp = (char *)malloc(strlen(cmdname)); char *namecmp = (char *)malloc(strlen(cmdname));
char *namep = namecmp; char *namep = namecmp;
while(*cmdname) { while(*cmdname) {
...@@ -35,31 +36,31 @@ int dispatch_command(const char *cmdname, int argc, char **argv, char *sender, c ...@@ -35,31 +36,31 @@ int dispatch_command(const char *cmdname, int argc, char **argv, char *sender, c
* } * }
*/ */
if(!handler) return -1; if(!handler) return -1;
return handler(argc, argv, sender, target, privmsg); return handler(argc, argv, state);
} }
void do_copyover(char *); void do_copyover(char *);
void ircprintf(const char *format, ...); void ircprintf(const char *format, ...);
extern "C" int cmd_copyover(int argc, char **argv, char *sender, char *target, bool privmsg) { _COMMAND(copyover) {
do_copyover(sender); do_copyover(state->sender->nick);
return 0; return 0;
} }
extern "C" int cmd_ping(int argc, char **argv, char *sender, char *target, bool privmsg) { _COMMAND(ping) {
ircprintf("PRIVMSG %s :PONG", privmsg?sender:target); ircprintf("PRIVMSG %s :PONG", state->channel);
return 0; return 0;
} }
extern "C" int cmd_report(int argc, char **argv, char *sender, char *target, bool privmsg) { _COMMAND(report) {
ircprintf("PRIVMSG %s :Compile date: %s %s", privmsg?sender:target, __DATE__, __TIME__); ircprintf("PRIVMSG %s :Compile date: %s %s", state->channel, __DATE__, __TIME__);
return 0; return 0;
} }
extern "C" int cmd_arguments(int argc, char **argv, char *sender, char *target, bool privmsg) { _COMMAND(arguments) {
char **argvp = argv; char **argvp = argv;
ircprintf("PRIVMSG %s :argc: %d", privmsg?sender:target, argc); ircprintf("PRIVMSG %s :argc: %d", state->channel, argc);
while(*argvp) { while(*argvp) {
ircprintf("PRIVMSG %s :Argument: %s", privmsg?sender:target, *argvp); ircprintf("PRIVMSG %s :Argument: %s", state->channel, *argvp);
argvp++; argvp++;
} }
return 0; return 0;
......
#ifndef __COMMANDS_H #ifndef __COMMANDS_H
#define __COMMANDS_H #define __COMMANDS_H
#include "oligos.h"
typedef int (*command_handler)(int argc, char **argv, char *sender, char *target, bool privmsg); typedef int (*command_handler)(int argc, char **argv, struct command_state *state);
typedef struct { typedef struct {
const char *name; const char *name;
command_handler handler; command_handler handler;
} command; } command;
int dispatch_command(const char *command, int argc, char **argv, char *sender, char *target, bool privmsg); int dispatch_command(const char *command, int argc, char **argv, struct command_state *state);
#endif //__COMMANDS_H #endif //__COMMANDS_H
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
void *app_handle; void *app_handle;
#include "dady/dady.h" #include "dady/dady.h"
#include "oligos.h"
#include "commands.h" #include "commands.h"
static char *progname; static char *progname;
...@@ -34,11 +35,6 @@ static unsigned long long int nbytes_write = 0; ...@@ -34,11 +35,6 @@ static unsigned long long int nbytes_write = 0;
static char *_nick; static char *_nick;
struct sender {
char *nick;
char *username;
char *host;
};
void ircprintf(const char *format, ...); void ircprintf(const char *format, ...);
#define MAX(a,b) (a>b?a:b) #define MAX(a,b) (a>b?a:b)
...@@ -245,7 +241,12 @@ void handle_irc(char *msgbuf) { ...@@ -245,7 +241,12 @@ void handle_irc(char *msgbuf) {
*argv = NULL; *argv = NULL;
} }
if(dispatch_command(commandname, argc, argv, sender.nick, recipient, priv) == -1) { struct command_state state;
state.sender = &sender;
state.message_type = (priv ? MESSAGE_TYPE_PRIVATE : MESSAGE_TYPE_PUBLIC);
state.channel = (priv ? sender.nick : recipient);
if(dispatch_command(commandname, argc, argv, &state) == -1) {
ircprintf("PRIVMSG %s :Unknown command: %s", priv?sender.nick:recipient, commandname); ircprintf("PRIVMSG %s :Unknown command: %s", priv?sender.nick:recipient, commandname);
} }
......
#ifndef OLIGOS_H
#define OLIGOS_H
struct sender {
char *nick;
char *username;
char *host;
};
enum _MessageType {
MESSAGE_TYPE_PUBLIC = 1,
MESSAGE_TYPE_PRIVATE,
MESSAGE_TYPE_NOTICE,
};
struct command_state {
struct sender *sender;
int message_type;
char *channel;
};
#endif /* end of include guard: OLIGOS_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment