Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Dustin L. Howett
oligos
Commits
b1e2691b
Commit
b1e2691b
authored
Apr 25, 2012
by
Dustin L. Howett
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Commands (rough)
parent
d05be75f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
137 additions
and
12 deletions
+137
-12
Makefile
Makefile
+1
-1
commands.cc
commands.cc
+75
-0
commands.h
commands.h
+15
-0
main.cc
main.cc
+46
-11
No files found.
Makefile
View file @
b1e2691b
...
...
@@ -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
commands.cc
0 → 100644
View file @
b1e2691b
#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, },
*};
*/
commands.h
0 → 100644
View file @
b1e2691b
#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
;
extern
command
command_list
[];
int
dispatch_command
(
const
char
*
command
,
int
argc
,
char
**
argv
,
char
*
sender
,
char
*
target
,
bool
privmsg
);
#endif //__COMMANDS_H
main.cc
View file @
b1e2691b
...
...
@@ -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,55 @@ 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
(
!
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
(
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
(
dispatch_command
(
commandname
,
argc
,
argv
,
sender
.
nick
,
recipient
,
priv
)
==
-
1
)
{
ircprintf
(
"PRIVMSG %s :Unknown command: %s"
,
priv
?
sender
.
nick
:
recipient
,
commandname
);
}
free
(
argv
);
/*
*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__);
*}
*/
}
return
;
}
...
...
@@ -325,6 +359,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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment