Commit 2be79ff1 authored by Dustin L. Howett's avatar Dustin L. Howett

Add dynamic commands (resolve cmd_COMMANDNAME at runtime!)

parent d05be75f
......@@ -3,7 +3,7 @@ export GO_EASY_ON_ME=1
include theos/makefiles/common.mk
TOOL_NAME = oligos
oligos_FILES = main.cc
oligos_FILES = main.cc commands.cc
oligos_SUBPROJECTS = dady
include $(THEOS_MAKE_PATH)/tool.mk
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dlfcn.h>
extern void *app_handle;
#include "commands.h"
int dispatch_command(const char *cmdname, int argc, char **argv, char *sender, char *target, bool privmsg) {
char *namecmp = (char *)malloc(strlen(cmdname));
char *namep = namecmp;
while(*cmdname) {
*namep = tolower(*cmdname);
++namep;
++cmdname;
}
*namep = '\0';
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, sender, target, privmsg);
}
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);
return 0;
}
extern "C" int cmd_ping(int argc, char **argv, char *sender, char *target, bool privmsg) {
ircprintf("PRIVMSG %s :PONG", privmsg?sender:target);
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__);
return 0;
}
extern "C" int cmd_arguments(int argc, char **argv, char *sender, char *target, bool privmsg) {
char **argvp = argv;
ircprintf("PRIVMSG %s :argc: %d", privmsg?sender:target, argc);
while(*argvp) {
ircprintf("PRIVMSG %s :Argument: %s", privmsg?sender:target, *argvp);
argvp++;
}
return 0;
}
/*
*command command_list[] = {
* { "copyover", &cmd_copyover, },
* { "ping", &cmd_ping, },
* { "report", &cmd_report, },
* { NULL, NULL, },
*};
*/
#ifndef __COMMANDS_H
#define __COMMANDS_H
typedef int (*command_handler)(int argc, char **argv, char *sender, char *target, bool privmsg);
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);
#endif //__COMMANDS_H
......@@ -14,7 +14,11 @@
#include <stdarg.h>
#include <dlfcn.h>
void *app_handle;
#include "dady/dady.h"
#include "commands.h"
static char *progname;
static int irc_fd = -1;
......@@ -210,25 +214,42 @@ void handle_irc(char *msgbuf) {
ircprintf("PONG %s", msgbuf);
} else if(strcmp(cmd, "PRIVMSG") == 0) {
char *recipient = strsep(&msgbuf, " ");
msgbuf++; // Lop off the errant : at the beginning.
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 && !priv) msgbuf += 3;
if(!addressed) return;
if(addressed) {
if(strcmp(msgbuf, "copyover") == 0)
do_copyover(sender.nick);
else if(strcmp(msgbuf, "quit") == 0)
exit(0);
else if(strcmp(msgbuf, "ping") == 0)
ircprintf("PRIVMSG %s :PONG (%s!%s@%s)", priv?sender.nick:recipient, sender.nick, sender.username, sender.host);
else if(strcmp(msgbuf, "report") == 0)
ircprintf("PRIVMSG %s :Compile date: %s %s", priv?sender.nick:recipient, __DATE__, __TIME__);
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;
}
if(dispatch_command(commandname, argc, argv, sender.nick, recipient, priv) == -1) {
ircprintf("PRIVMSG %s :Unknown command: %s", priv?sender.nick:recipient, commandname);
}
free(argv);
}
return;
}
......@@ -325,6 +346,7 @@ void _init_buffers() {
int main(int argc, char **argv, char **envp) {
progname = strdup(argv[0]);
app_handle = dlopen(NULL, RTLD_NOW|RTLD_LOCAL|RTLD_FIRST);
_init_buffers();
......
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