diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bb3bf13 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CFLAGS?=-s -O0 +CC?=cc + +all: + $(CC) $(CFLAGS) -omain *.c + +clean: + rm main diff --git a/cfg.h b/cfg.h new file mode 100644 index 0000000..bbb490a --- /dev/null +++ b/cfg.h @@ -0,0 +1,6 @@ +//Settings +#define NICK "gamepadbot" +#define HOST "localhost" +#define PORT 6667 + +char *channels[] = {"#channel"}; diff --git a/main.c b/main.c new file mode 100644 index 0000000..55afbfe --- /dev/null +++ b/main.c @@ -0,0 +1,147 @@ +#include "irc.h" +#include "cfg.h" +#include +#include +#include +IRCC_client client; + +void die(const char *msg) { + puts(msg); + exit(1); +} + + +void emit(int fd, int type, int code, int val) { + struct input_event ie; + + ie.type = type; + ie.code = code; + ie.value = val; + + ie.time.tv_sec = 0; + ie.time.tv_usec = 0; + + write(fd, &ie, sizeof(ie)); +} + +void press(int fd, int event) { + emit(fd, EV_KEY, event, 1); + emit(fd, EV_SYN, SYN_REPORT, 0); + + usleep(200000); + + emit(fd, EV_KEY, event, 0); + emit(fd, EV_SYN, SYN_REPORT, 0); +} + +void recvinfo(int fd) { + while (1) { + unsigned int irc_status = IRCC_recv(&client); + if (irc_status == IRCC_DISCONNECTED) + die("Disconnected"); + + else if (client.msg != NULL && irc_status == IRCC_PRIVMSG) { + for (int i = 0; i < strlen(client.msg); i++) { + usleep(20000); + switch (client.msg[i]) { + case 'w': + press(fd, KEY_UP); + break; + + case 's': + press(fd, KEY_DOWN); + break; + + case 'a': + press(fd, KEY_LEFT); + break; + + case 'd': + press(fd, KEY_RIGHT); + break; + + case 'z': + press(fd, KEY_Z); + break; + + case 'x': + press(fd, KEY_X); + break; + + case 'c': + press(fd, KEY_C); + break; + + case 'e': + press(fd, KEY_ENTER); + break; + + default: + break; + } + } + } + + else if (irc_status == IRCC_CONNECTED) + return; + } +} + +int main(void) { + + //Init uinput + int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); + if (fd < 0) + die("WTF! Were uinput? Try modprobe uinput"); + + ioctl(fd, UI_SET_EVBIT, EV_KEY); + ioctl(fd, UI_SET_KEYBIT, KEY_RIGHT); + ioctl(fd, UI_SET_KEYBIT, KEY_UP); + ioctl(fd, UI_SET_KEYBIT, KEY_DOWN); + ioctl(fd, UI_SET_KEYBIT, KEY_LEFT); + ioctl(fd, UI_SET_KEYBIT, KEY_ENTER); + ioctl(fd, UI_SET_KEYBIT, KEY_C); + ioctl(fd, UI_SET_KEYBIT, KEY_Z); + ioctl(fd, UI_SET_KEYBIT, KEY_X); + + struct uinput_setup usetup; + memset(&usetup, 0, sizeof(usetup)); + + usetup.id.bustype = BUS_USB; + usetup.id.vendor = 0x1234; + usetup.id.product = 0x5678; + strcpy(usetup.name, "IrcKeyboard"); + + ioctl(fd, UI_DEV_SETUP, &usetup); + ioctl(fd, UI_DEV_CREATE); + sleep(2); + + //512 - size of raw buffer (max irc) + IRCC_init(&client); + int status = IRCC_connect(&client, HOST, PORT); + if (status == IRCC_ERROR) + die("Conn refused"); + + //Register + IRCC_register(&client, NICK); + + //Recv motd and join in channel + recvinfo(fd); + sleep(5); + for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) { + if (channels[i] == NULL) + break; + + size_t join_size = strlen("JOIN \r\n") + strlen(channels[i]) + 1; + char *tmp = malloc(join_size); + if (tmp == NULL) + die("malloc retured NULL"); + + snprintf(tmp, join_size, "JOIN %s\r\n", channels[i]); + send(client.socket, tmp, join_size, 0); + free(tmp); + } + + recvinfo(fd); +} +