-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d77f4bf
Showing
10 changed files
with
1,485 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
LIBS = libtoxcore libtoxav | ||
CFLAGS = -std=gnu99 -Wall -Werror -ggdb -D_XOPEN_SOURCE_EXTENDED -D_XOPEN_SOURCE -D_FILE_OFFSET_BITS=64 | ||
OBJ = toxcs.o misc.o commands.o | ||
LDFLAGS = $(shell pkg-config --libs $(LIBS)) | ||
SRC_DIR = ./src | ||
|
||
all: $(OBJ) | ||
@echo " LD $@" | ||
@$(CC) $(CFLAGS) -o toxcs $(OBJ) $(LDFLAGS) | ||
|
||
%.o: $(SRC_DIR)/%.c | ||
@echo " CC $@" | ||
@$(CC) $(CFLAGS) -o $*.o -c $(SRC_DIR)/$*.c | ||
@$(CC) -MM $(CFLAGS) $(SRC_DIR)/$*.c > $*.d | ||
|
||
clean: | ||
rm -f *.d *.o toxcs | ||
|
||
.PHONY: clean all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# ToxCS | ||
This is a present for my friend Stal. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* commands.c | ||
* | ||
* | ||
* Copyright (C) 2016 toxcs All Rights Reserved. | ||
* | ||
* This file is part of toxcs. | ||
* | ||
* toxcs is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* toxcs is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with toxcs. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
#include <strings.h> | ||
#include <stdbool.h> | ||
#include <time.h> | ||
#include <inttypes.h> | ||
|
||
#include <tox/tox.h> | ||
|
||
#include "toxcs.h" | ||
#include "misc.h" | ||
|
||
#define MAX_COMMAND_LENGTH TOX_MAX_MESSAGE_LENGTH | ||
#define MAX_NUM_ARGS 4 | ||
|
||
extern struct Tox_Bot Tox_Bot; | ||
|
||
static void authent_failed(Tox *m, int friendnum) | ||
{ | ||
const char *outmsg = "Invalid command."; | ||
tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); | ||
} | ||
|
||
static void cmd_start(Tox *m, int friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) | ||
{ | ||
const char *outmsg = NULL; | ||
|
||
if (!friend_is_master(m, friendnum)) { | ||
authent_failed(m, friendnum); | ||
return; | ||
} | ||
|
||
if (Tox_Bot.is_running) { | ||
outmsg = "Server is already running"; | ||
tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); | ||
return; | ||
} | ||
|
||
if (system("./hlds_run -game cstrike +maxplayers 10 +map <user string> +exec server.cfg") == -1) { | ||
outmsg = "Failed to execute system command"; | ||
} else { | ||
outmsg = "Started server"; | ||
Tox_Bot.is_running = true; | ||
} | ||
|
||
tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); | ||
} | ||
|
||
static void cmd_stop(Tox *m, int friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) | ||
{ | ||
const char *outmsg = NULL; | ||
|
||
if (!friend_is_master(m, friendnum)) { | ||
authent_failed(m, friendnum); | ||
return; | ||
} | ||
|
||
// TODO: you need to make this do something | ||
|
||
outmsg = Tox_Bot.is_running ? "Shutting down server" : "Server is not running"; | ||
tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); | ||
Tox_Bot.is_running = false; | ||
} | ||
|
||
static void cmd_status(Tox *m, int friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) | ||
{ | ||
const char *outmsg = Tox_Bot.is_running ? "Server is running" : "Server is not running"; | ||
tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); | ||
} | ||
|
||
/* Parses input command and puts args into arg array. | ||
Returns number of arguments on success, -1 on failure. */ | ||
static int parse_command(const char *input, char (*args)[MAX_COMMAND_LENGTH]) | ||
{ | ||
char *cmd = strdup(input); | ||
|
||
if (cmd == NULL) | ||
exit(EXIT_FAILURE); | ||
|
||
int num_args = 0; | ||
int i = 0; /* index of last char in an argument */ | ||
|
||
/* characters wrapped in double quotes count as one arg */ | ||
while (num_args < MAX_NUM_ARGS) { | ||
int qt_ofst = 0; /* set to 1 to offset index for quote char at end of arg */ | ||
|
||
if (*cmd == '\"') { | ||
qt_ofst = 1; | ||
i = char_find(1, cmd, '\"'); | ||
|
||
if (cmd[i] == '\0') { | ||
free(cmd); | ||
return -1; | ||
} | ||
} else { | ||
i = char_find(0, cmd, ' '); | ||
} | ||
|
||
memcpy(args[num_args], cmd, i + qt_ofst); | ||
args[num_args++][i + qt_ofst] = '\0'; | ||
|
||
if (cmd[i] == '\0') /* no more args */ | ||
break; | ||
|
||
char tmp[MAX_COMMAND_LENGTH]; | ||
snprintf(tmp, sizeof(tmp), "%s", &cmd[i + 1]); | ||
strcpy(cmd, tmp); /* tmp will always fit inside cmd */ | ||
} | ||
|
||
free(cmd); | ||
return num_args; | ||
} | ||
|
||
static struct { | ||
const char *name; | ||
void (*func)(Tox *m, int friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]); | ||
} commands[] = { | ||
{ "start", cmd_start }, | ||
{ "stop", cmd_stop }, | ||
{ "status", cmd_status }, | ||
{ NULL, NULL }, | ||
}; | ||
|
||
static int do_command(Tox *m, int friendnum, int num_args, char (*args)[MAX_COMMAND_LENGTH]) | ||
{ | ||
int i; | ||
|
||
for (i = 0; commands[i].name; ++i) { | ||
if (strcmp(args[0], commands[i].name) == 0) { | ||
(commands[i].func)(m, friendnum, num_args - 1, args); | ||
return 0; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
int execute(Tox *m, int friendnum, const char *input, int length) | ||
{ | ||
if (length >= MAX_COMMAND_LENGTH) | ||
return -1; | ||
|
||
char args[MAX_NUM_ARGS][MAX_COMMAND_LENGTH]; | ||
int num_args = parse_command(input, args); | ||
|
||
if (num_args == -1) | ||
return -1; | ||
|
||
return do_command(m, friendnum, num_args, args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* commands.h | ||
* | ||
* | ||
* Copyright (C) 2016 toxcs All Rights Reserved. | ||
* | ||
* This file is part of toxcs. | ||
* | ||
* toxcs is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* toxcs is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with toxcs. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#ifndef COMMANDS_H | ||
#define COMMANDS_H | ||
|
||
int execute(Tox *m, int friendnumber, const char *input, int length); | ||
|
||
#endif /* COMMANDS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* misc.c | ||
* | ||
* | ||
* Copyright (C) 2016 toxcs All Rights Reserved. | ||
* | ||
* This file is part of toxcs. | ||
* | ||
* toxcs is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* toxcs is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with toxcs. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
#include <unistd.h> | ||
|
||
#include "misc.h" | ||
|
||
char *hex_string_to_bin(const char *hex_string) | ||
{ | ||
size_t len = strlen(hex_string); | ||
char *val = malloc(len); | ||
|
||
if (val == NULL) | ||
exit(EXIT_FAILURE); | ||
|
||
size_t i; | ||
|
||
for (i = 0; i < len; ++i, hex_string += 2) | ||
sscanf(hex_string, "%2hhx", &val[i]); | ||
|
||
return val; | ||
} | ||
|
||
bool file_exists(const char *path) | ||
{ | ||
struct stat s; | ||
return stat(path, &s) == 0; | ||
} | ||
|
||
off_t file_size(const char *path) | ||
{ | ||
struct stat st; | ||
|
||
if (stat(path, &st) == -1) | ||
return 0; | ||
|
||
return st.st_size; | ||
} | ||
|
||
uint16_t copy_tox_str(char *msg, size_t size, const char *data, uint16_t length) | ||
{ | ||
int len = MIN(length, size - 1); | ||
memcpy(msg, data, len); | ||
msg[len] = '\0'; | ||
return len; | ||
} | ||
|
||
int char_find(int idx, const char *s, char ch) | ||
{ | ||
int i = idx; | ||
|
||
for (i = idx; s[i]; ++i) { | ||
if (s[i] == ch) | ||
break; | ||
} | ||
|
||
return i; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* misc.h | ||
* | ||
* | ||
* Copyright (C) 2016 toxcs All Rights Reserved. | ||
* | ||
* This file is part of toxcs. | ||
* | ||
* toxcs is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* toxcs is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with toxcs. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#ifndef MISC_H | ||
#define MISC_H | ||
|
||
#ifndef MIN | ||
#define MIN(x, y) (((x) < (y)) ? (x) : (y)) | ||
#endif | ||
|
||
#ifndef MAX | ||
#define MAX(x, y) (((x) > (y)) ? (x) : (y)) | ||
#endif | ||
|
||
bool timed_out(uint64_t timestamp, uint64_t curtime, uint64_t timeout); | ||
|
||
/* converts hexidecimal string to binary */ | ||
char *hex_string_to_bin(const char *hex_string); | ||
|
||
/* checks if a file exists. Returns true or false */ | ||
bool file_exists(const char *path); | ||
|
||
/* returns file size or 0 on error */ | ||
off_t file_size(const char *path); | ||
|
||
/* copies data to msg buffer. | ||
returns length of msg, which will be no larger than size-1 */ | ||
uint16_t copy_tox_str(char *msg, size_t size, const char *data, uint16_t length); | ||
|
||
/* returns index of the first instance of ch in s starting at idx. | ||
returns length of s if char not found */ | ||
int char_find(int idx, const char *s, char ch); | ||
|
||
/* Converts seconds to string in format days hours minutes */ | ||
void get_elapsed_time_str(char *buf, int bufsize, uint64_t secs); | ||
|
||
#endif /* MISC_H */ |
Oops, something went wrong.