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

Break the event handler out of the message parser and make it extensible. Uses...

Break the event handler out of the message parser and make it extensible. Uses the old remnants of the original command dispatcher.
parent 6a8ae4dd
......@@ -3,7 +3,7 @@ export GO_EASY_ON_ME=1
include theos/makefiles/common.mk
TOOL_NAME = oligos
oligos_FILES = main.cc commands.cc
oligos_FILES = main.cc commands.cc event.cc
oligos_SUBPROJECTS = dady
include $(THEOS_MAKE_PATH)/tool.mk
......@@ -24,17 +24,7 @@ int dispatch_command(const char *cmdname, int argc, char **argv, struct command_
char *symname = NULL;
asprintf(&symname, "cmd_%s", namecmp);
command_handler handler = (command_handler)dlsym(app_handle, symname);
/*
*
* command_handler handler = NULL;
* command *c;
* for(c = &command_list[0]; c->name; ++c) {
* if(strcmp(c->name, namecmp) == 0) {
* handler = c->handler;
* break;
* }
* }
*/
if(!handler) return -1;
return handler(argc, argv, state);
}
......@@ -65,12 +55,3 @@ _COMMAND(arguments) {
}
return 0;
}
/*
*command command_list[] = {
* { "copyover", &cmd_copyover, },
* { "ping", &cmd_ping, },
* { "report", &cmd_report, },
* { NULL, NULL, },
*};
*/
......@@ -3,12 +3,6 @@
#include "oligos.h"
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, struct command_state *state);
#endif //__COMMANDS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "oligos.h"
#include "event.h"
#include "commands.h"
void dispatch_event(char *name, struct sender *sender, char *msgbuf) {
event_handler_func handler = NULL;
eventhandler *c;
for(c = &event_handlers[0]; c->name; ++c) {
if(strcmp(c->name, name) == 0) {
handler = c->handler;
break;
}
}
if(handler) {
handler(sender, msgbuf);
}
}
#define _EVENT(name) void evt_ ## name (struct sender *sender, char *msgbuf)
_EVENT(001) {
char *nick = strsep(&msgbuf, " ");
//_nick = strdup(nick);
ircprintf("JOIN #bacon");
}
_EVENT(ping) {
ircprintf("PONG %s", msgbuf);
}
_EVENT(privmsg) {
char *recipient = strsep(&msgbuf, " ");
bool priv = false;
if(strcmp(recipient, "oligosII") == 0) {
priv = true;
}
msgbuf++; // Lop off the errant : at the beginning.
bool addressed = priv || strncmp(msgbuf, "!o ", 3) == 0;
if(!addressed) return;
if(!priv) msgbuf += 3;
char *commandname = strsep(&msgbuf, " ");
int argc = 0;
char **argv;
if(msgbuf) {
for(unsigned int i = 0; i <= strlen(msgbuf); ++i) {
if(msgbuf[i] == ' ' || msgbuf[i] == '\0') {
++argc;
}
}
argv = (char**)malloc(argc+1 * sizeof(char*));
int i = 0;
for(i = 0; i < argc; ++i) {
argv[i] = strsep(&msgbuf, " ");
}
argv[i] = NULL;
} else {
argv = (char**)malloc(sizeof(char*));
*argv = NULL;
}
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);
}
free(argv);
}
eventhandler event_handlers[] = {
{ "001", &evt_001, },
{ "PING", &evt_ping, },
{ "PRIVMSG", &evt_privmsg, },
{ NULL, NULL, },
};
#ifndef EVENT_H
#define EVENT_H
typedef void (*event_handler_func)(struct sender *sender, char *msgbuf);
typedef struct {
const char *name;
event_handler_func handler;
} eventhandler;
extern eventhandler event_handlers[];
void dispatch_event(char *name, struct sender *sender, char *msgbuf);
#endif /* end of include guard: EVENT_H */
......@@ -19,6 +19,7 @@ void *app_handle;
#include "dady/dady.h"
#include "oligos.h"
#include "event.h"
#include "commands.h"
static char *progname;
......@@ -33,7 +34,6 @@ static unsigned long long int nbytes_write = 0;
static char *_nick;
void ircprintf(const char *format, ...);
#define MAX(a,b) (a>b?a:b)
void do_copyover(char *requester) {
......@@ -179,8 +179,6 @@ void ircprintf(const char *format, ...) {
}
void handle_irc(char *msgbuf) {
static bool seen001;
struct sender sender;
memset(&sender, 0, sizeof(sender));
char *cmd = NULL;
......@@ -196,60 +194,8 @@ void handle_irc(char *msgbuf) {
cmd = strsep(&msgbuf, " ");
_D("%s from %s (%s) on %s.\n", cmd, sender.nick, sender.username, sender.host);
_D("<- \"%s\"\n", msgbuf);
if(!seen001 && strcmp(cmd, "001") == 0) {
seen001 = true;
char *nick = strsep(&msgbuf, " ");
_nick = strdup(nick);
ircprintf("JOIN #bacon");
} else if(strcmp(cmd, "PING") == 0) {
ircprintf("PONG %s", msgbuf);
} else if(strcmp(cmd, "PRIVMSG") == 0) {
char *recipient = strsep(&msgbuf, " ");
bool priv = false;
if(strcmp(recipient, "oligosII") == 0) {
priv = true;
}
msgbuf++; // Lop off the errant : at the beginning.
bool addressed = priv || strncmp(msgbuf, "!o ", 3) == 0;
if(!addressed) return;
if(!priv) msgbuf += 3;
char *commandname = strsep(&msgbuf, " ");
int argc = 0;
char **argv;
if(msgbuf) {
for(unsigned int i = 0; i <= strlen(msgbuf); ++i) {
if(msgbuf[i] == ' ' || msgbuf[i] == '\0') {
++argc;
}
}
argv = (char**)malloc(argc+1 * sizeof(char*));
int i = 0;
for(i = 0; i < argc; ++i) {
argv[i] = strsep(&msgbuf, " ");
}
argv[i] = NULL;
} else {
argv = (char**)malloc(sizeof(char*));
*argv = NULL;
}
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);
}
free(argv);
}
_D("[%s!%s@%s] %s %s\n", sender.nick, sender.username, sender.host, cmd, msgbuf);
dispatch_event(cmd, &sender, msgbuf);
return;
}
......
......@@ -22,4 +22,6 @@ struct command_state {
#define READ_BUF_LEN 4096
#define WRITE_BUF_LEN (200*1024)
void ircprintf(const char *format, ...);
#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