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 @@
extern void *app_handle;
#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 *namep = namecmp;
while(*cmdname) {
......@@ -35,31 +36,31 @@ int dispatch_command(const char *cmdname, int argc, char **argv, char *sender, c
* }
*/
if(!handler) return -1;
return handler(argc, argv, sender, target, privmsg);
return handler(argc, argv, state);
}
void do_copyover(char *);
void ircprintf(const char *format, ...);
extern "C" int cmd_copyover(int argc, char **argv, char *sender, char *target, bool privmsg) {
do_copyover(sender);
_COMMAND(copyover) {
do_copyover(state->sender->nick);
return 0;
}
extern "C" int cmd_ping(int argc, char **argv, char *sender, char *target, bool privmsg) {
ircprintf("PRIVMSG %s :PONG", privmsg?sender:target);
_COMMAND(ping) {
ircprintf("PRIVMSG %s :PONG", state->channel);
return 0;
}
extern "C" int cmd_report(int argc, char **argv, char *sender, char *target, bool privmsg) {
ircprintf("PRIVMSG %s :Compile date: %s %s", privmsg?sender:target, __DATE__, __TIME__);
_COMMAND(report) {
ircprintf("PRIVMSG %s :Compile date: %s %s", state->channel, __DATE__, __TIME__);
return 0;
}
extern "C" int cmd_arguments(int argc, char **argv, char *sender, char *target, bool privmsg) {
_COMMAND(arguments) {
char **argvp = argv;
ircprintf("PRIVMSG %s :argc: %d", privmsg?sender:target, argc);
ircprintf("PRIVMSG %s :argc: %d", state->channel, argc);
while(*argvp) {
ircprintf("PRIVMSG %s :Argument: %s", privmsg?sender:target, *argvp);
ircprintf("PRIVMSG %s :Argument: %s", state->channel, *argvp);
argvp++;
}
return 0;
......
#ifndef __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 {
const char *name;
command_handler handler;
} 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
......@@ -18,6 +18,7 @@
void *app_handle;
#include "dady/dady.h"
#include "oligos.h"
#include "commands.h"
static char *progname;
......@@ -34,11 +35,6 @@ static unsigned long long int nbytes_write = 0;
static char *_nick;
struct sender {
char *nick;
char *username;
char *host;
};
void ircprintf(const char *format, ...);
#define MAX(a,b) (a>b?a:b)
......@@ -245,7 +241,12 @@ void handle_irc(char *msgbuf) {
*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);
}
......
#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