added submodules, minor updates
This commit is contained in:
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "libircc"]
|
||||||
|
path = libircc
|
||||||
|
url = https://gitea.codersquack.nl/thorium1256/libircc.git
|
||||||
26
Makefile
26
Makefile
@@ -3,12 +3,15 @@ SRC_DIR := src
|
|||||||
INCLUDE_DIR := include
|
INCLUDE_DIR := include
|
||||||
PRIVATE_INCLUDE_DIR := privinclude
|
PRIVATE_INCLUDE_DIR := privinclude
|
||||||
|
|
||||||
|
LIBIRCC_DIR := libircc
|
||||||
|
LIBIRCC_LIB := $(LIBIRCC_DIR)/libircc.so
|
||||||
|
|
||||||
CC := clang
|
CC := clang
|
||||||
CFLAGS := -I $(INCLUDE_DIR) -I $(PRIVATE_INCLUDE_DIR) -Wall -Wextra
|
CFLAGS := -I$(INCLUDE_DIR) -I$(PRIVATE_INCLUDE_DIR) -I$(LIBIRCC_DIR)/include -Wall -Wextra
|
||||||
CFLAGS_DBG := $(CFLAGS) -g -O0
|
CFLAGS_DBG := $(CFLAGS) -g -O0
|
||||||
CFLAGS_REL := $(CFLAGS) -O2
|
CFLAGS_REL := $(CFLAGS) -O2
|
||||||
|
|
||||||
LDFLAGS := -lssl -lcrypto
|
LDFLAGS := -L./libircc -Wl,-rpath,'$$ORIGIN/libircc' -lircc -lssl -lcrypto
|
||||||
LDFLAGS_REL := $(LDFLAGS) -s
|
LDFLAGS_REL := $(LDFLAGS) -s
|
||||||
|
|
||||||
APPLICATION := bbirc
|
APPLICATION := bbirc
|
||||||
@@ -16,30 +19,37 @@ APPLICATION_DBG := bbirc-debug
|
|||||||
|
|
||||||
$(shell mkdir -p $(BUILD_DIR))
|
$(shell mkdir -p $(BUILD_DIR))
|
||||||
SRCS := $(wildcard $(SRC_DIR)/*.c)
|
SRCS := $(wildcard $(SRC_DIR)/*.c)
|
||||||
|
LIBIRCC_SRCS := $(shell find $(LIBIRCC_DIR)/$(SRC_DIR) -name '*.c')
|
||||||
DBG_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/dbg_%.o,$(SRCS))
|
DBG_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/dbg_%.o,$(SRCS))
|
||||||
REL_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/rel_%.o,$(SRCS))
|
REL_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/rel_%.o,$(SRCS))
|
||||||
|
|
||||||
.PHONY: all debug release both clean
|
.PHONY: all debug release both clean ircc
|
||||||
|
|
||||||
all: release
|
all: release
|
||||||
|
|
||||||
debug: $(APPLICATION_DBG)
|
debug: ircc $(APPLICATION_DBG)
|
||||||
release: $(APPLICATION)
|
release: ircc $(APPLICATION)
|
||||||
both: debug release
|
both: debug release
|
||||||
|
ircc: $(LIBIRCC_LIB)
|
||||||
|
|
||||||
$(APPLICATION): $(REL_OBJS)
|
$(APPLICATION): $(REL_OBJS)
|
||||||
$(CC) -o $@ $^ $(LDFLAGS_REL)
|
$(CC) -o $@ $^ $(LDFLAGS_REL)
|
||||||
|
|
||||||
$(APPLICATION_DBG): $(DBG_OBJS)
|
$(APPLICATION_DBG): $(DBG_OBJS)
|
||||||
|
+$(MAKE) -C $(LIBIRCC_DIR)
|
||||||
$(CC) -o $@ $^ $(LDFLAGS)
|
$(CC) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
$(BUILD_DIR)/dbg_%.o: $(SRC_DIR)/%.c
|
$(BUILD_DIR)/dbg_%.o: $(SRC_DIR)/%.c
|
||||||
$(CC) -c -o $@ $^ $(CFLAGS_DBG)
|
$(CC) -c -o $@ $< $(CFLAGS_DBG)
|
||||||
|
|
||||||
$(BUILD_DIR)/rel_%.o: $(SRC_DIR)/%.c
|
$(BUILD_DIR)/rel_%.o: $(SRC_DIR)/%.c
|
||||||
$(CC) -c -o $@ $^ $(CFLAGS_REL)
|
$(CC) -c -o $@ $< $(CFLAGS_REL)
|
||||||
|
|
||||||
|
$(LIBIRCC_LIB): $(LIBIRCC_SRCS)
|
||||||
|
+$(MAKE) -C libircc
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BUILD_DIR)
|
rm -rf $(BUILD_DIR)
|
||||||
rm -rf $(APPLICATION)
|
rm -rf $(APPLICATION)
|
||||||
rm -rf $(APPLICATION_DBG)
|
rm -rf $(APPLICATION_DBG)
|
||||||
|
+$(MAKE) -C $(LIBIRCC_DIR) clean
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"file": "src/IRC.c",
|
|
||||||
"arguments": [
|
|
||||||
"/usr/bin/clang",
|
|
||||||
"-c",
|
|
||||||
"-o",
|
|
||||||
"build/rel_IRC.o",
|
|
||||||
"src/IRC.c",
|
|
||||||
"-I",
|
|
||||||
"include",
|
|
||||||
"-I",
|
|
||||||
"privinclude",
|
|
||||||
"-Wall",
|
|
||||||
"-Wextra",
|
|
||||||
"-O2"
|
|
||||||
],
|
|
||||||
"directory": "/home/neon1246/Documents/stuff/code/c/ircclient",
|
|
||||||
"output": "build/rel_IRC.o"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"file": "src/base64.c",
|
|
||||||
"arguments": [
|
|
||||||
"/usr/bin/clang",
|
|
||||||
"-c",
|
|
||||||
"-o",
|
|
||||||
"build/rel_base64.o",
|
|
||||||
"src/base64.c",
|
|
||||||
"-I",
|
|
||||||
"include",
|
|
||||||
"-I",
|
|
||||||
"privinclude",
|
|
||||||
"-Wall",
|
|
||||||
"-Wextra",
|
|
||||||
"-O2"
|
|
||||||
],
|
|
||||||
"directory": "/home/neon1246/Documents/stuff/code/c/ircclient",
|
|
||||||
"output": "build/rel_base64.o"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"file": "src/main.c",
|
|
||||||
"arguments": [
|
|
||||||
"/usr/bin/clang",
|
|
||||||
"-c",
|
|
||||||
"-o",
|
|
||||||
"build/rel_main.o",
|
|
||||||
"src/main.c",
|
|
||||||
"-I",
|
|
||||||
"include",
|
|
||||||
"-I",
|
|
||||||
"privinclude",
|
|
||||||
"-Wall",
|
|
||||||
"-Wextra",
|
|
||||||
"-O2"
|
|
||||||
],
|
|
||||||
"directory": "/home/neon1246/Documents/stuff/code/c/ircclient",
|
|
||||||
"output": "build/rel_main.o"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"file": "src/netcode.c",
|
|
||||||
"arguments": [
|
|
||||||
"/usr/bin/clang",
|
|
||||||
"-c",
|
|
||||||
"-o",
|
|
||||||
"build/rel_netcode.o",
|
|
||||||
"src/netcode.c",
|
|
||||||
"-I",
|
|
||||||
"include",
|
|
||||||
"-I",
|
|
||||||
"privinclude",
|
|
||||||
"-Wall",
|
|
||||||
"-Wextra",
|
|
||||||
"-O2"
|
|
||||||
],
|
|
||||||
"directory": "/home/neon1246/Documents/stuff/code/c/ircclient",
|
|
||||||
"output": "build/rel_netcode.o"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
@@ -38,4 +38,10 @@ ssize_t IRC_Receive(irc_client_t *irc, char* toBuf, size_t bufSize);
|
|||||||
void IRC_Send(irc_client_t *irc, char* toSend);
|
void IRC_Send(irc_client_t *irc, char* toSend);
|
||||||
void IRC_Close(irc_client_t *irc);
|
void IRC_Close(irc_client_t *irc);
|
||||||
|
|
||||||
|
// init
|
||||||
|
int IRC_Init(irc_client_t *irc);
|
||||||
|
void IRC_Free(irc_client_t *irc);
|
||||||
|
|
||||||
|
int IRC_PollEvent(irc_event_t *event, irc_client_t *irc);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
17
include/IRC/IRC_events.h
Normal file
17
include/IRC/IRC_events.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef IRC_EVENTS_H
|
||||||
|
#define IRC_EVENTS_H
|
||||||
|
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
IRC_EVENT_PRIVMSG,
|
||||||
|
IRC_EVENT_NOTICE,
|
||||||
|
IRC_EVENT_JOIN,
|
||||||
|
IRC_EVENT_PART,
|
||||||
|
IRC_EVENT_QUIT,
|
||||||
|
IRC_EVENT_MODESET,
|
||||||
|
IRC_EVENT_AWAY,
|
||||||
|
IRC_EVENT_KICK
|
||||||
|
} irc_event_type_t;
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef IRC_STRUCTS_H
|
#ifndef IRC_STRUCTS_H
|
||||||
#define IRC_STRUCTS_H
|
#define IRC_STRUCTS_H
|
||||||
|
|
||||||
|
#include "IRC/IRC_events.h"
|
||||||
#include "netcode.h"
|
#include "netcode.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <openssl/ssl.h>
|
#include <openssl/ssl.h>
|
||||||
@@ -27,6 +28,12 @@ typedef struct {
|
|||||||
int argc;
|
int argc;
|
||||||
} irc_message_t;
|
} irc_message_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
irc_event_type_t type;
|
||||||
|
irc_message_t orig_msg;
|
||||||
|
} irc_event_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
irc_hostmask_t hostmask;
|
irc_hostmask_t hostmask;
|
||||||
@@ -92,17 +99,20 @@ typedef struct
|
|||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
irc_connection_info_t connectionInfo;
|
irc_connection_info_t connectionInfo;
|
||||||
sslSockfd_t connection;
|
sslSockfd_t connection;
|
||||||
irc_hostmask_t ownHostmask;
|
irc_hostmask_t ownHostmask;
|
||||||
char supportedCapabilities[513];
|
char supportedCapabilities[513];
|
||||||
char requestedCapabilities[513];
|
char requestedCapabilities[513];
|
||||||
irc_capability_t acked[24];
|
irc_capability_t acked[16];
|
||||||
|
|
||||||
/* Authentication parameters */
|
/* Authentication parameters */
|
||||||
irc_auth_t saslAuth;
|
irc_auth_t saslAuth;
|
||||||
|
|
||||||
/* Identity */
|
/* Identity */
|
||||||
irc_identity_t identity;
|
irc_identity_t identity;
|
||||||
|
|
||||||
|
irc_event_t* eventQueue; // lives on the heap
|
||||||
|
int eventQueuePtr;
|
||||||
} irc_client_t;
|
} irc_client_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
1
libircc
Submodule
1
libircc
Submodule
Submodule libircc added at 7be1ef312e
455
src/IRC.c
455
src/IRC.c
@@ -1,455 +0,0 @@
|
|||||||
#include "IRC/IRC_numerics.h"
|
|
||||||
#include "IRC/IRC_structs.h"
|
|
||||||
#include "defines.h"
|
|
||||||
#include "netcode.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
|
|
||||||
#include "IRC/IRC.h"
|
|
||||||
#include "base64.h"
|
|
||||||
|
|
||||||
void IRC_ParseMessage(char *line, irc_message_t *msg)
|
|
||||||
{
|
|
||||||
strncpy(msg->line, line, sizeof(msg->line) - 1);
|
|
||||||
char *ptr = msg->line;
|
|
||||||
ptr[sizeof(msg->line)] = NUL;
|
|
||||||
|
|
||||||
char *source = NULL;
|
|
||||||
char *command = NULL;
|
|
||||||
|
|
||||||
// get the source, if it exists (the : at the beginning of an IRC message)
|
|
||||||
if(*ptr == ':')
|
|
||||||
{
|
|
||||||
ptr++;
|
|
||||||
source = ptr;
|
|
||||||
|
|
||||||
while (*ptr && *ptr != ' ') ptr++;
|
|
||||||
if (*ptr) *ptr++ = NUL;
|
|
||||||
|
|
||||||
while (*ptr == ' ') ptr++; // skip any extra spaces
|
|
||||||
}
|
|
||||||
|
|
||||||
msg->source = source;
|
|
||||||
|
|
||||||
// read the command
|
|
||||||
command = ptr;
|
|
||||||
|
|
||||||
while(*ptr && *ptr != ' ') ptr++;
|
|
||||||
if (*ptr) *ptr++ = NUL;
|
|
||||||
|
|
||||||
msg->command = command;
|
|
||||||
|
|
||||||
while (*ptr == ' ') ptr++;
|
|
||||||
|
|
||||||
// read its arguments (until we encounter a colon/nullbyte)
|
|
||||||
msg->argc = 0;
|
|
||||||
|
|
||||||
while(*ptr && msg->argc <= 15)
|
|
||||||
{
|
|
||||||
if(*ptr == ':') // if it's a trailing arg
|
|
||||||
{
|
|
||||||
ptr++;
|
|
||||||
msg->argv[msg->argc] = ptr;
|
|
||||||
msg->argc++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
msg->argv[msg->argc] = ptr;
|
|
||||||
|
|
||||||
while(*ptr && *ptr != ' ') ptr++;
|
|
||||||
|
|
||||||
if (*ptr)
|
|
||||||
{
|
|
||||||
*ptr++ = NUL;
|
|
||||||
while (*ptr == ' ') ptr++; // skip spaces for next arg
|
|
||||||
}
|
|
||||||
|
|
||||||
msg->argc++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg->argc > 0) {
|
|
||||||
char *last = msg->argv[msg->argc - 1];
|
|
||||||
size_t len = strlen(last);
|
|
||||||
while (len > 0 && (last[len-1] == '\r' || last[len-1] == '\n')) {
|
|
||||||
last[len-1] = '\0';
|
|
||||||
len--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the number of capabilities found.
|
|
||||||
int IRC_ParseCapabilities(char *caps, irc_capability_t *capabilities, int capsLength)
|
|
||||||
{
|
|
||||||
while (*caps == ' ') caps++;
|
|
||||||
|
|
||||||
size_t len = strlen(caps);
|
|
||||||
while (len > 0 && (caps[len-1] == '\n' || caps[len-1] == '\r' || caps[len-1] == ' ')) {
|
|
||||||
caps[len-1] = '\0';
|
|
||||||
len--;
|
|
||||||
}
|
|
||||||
char capBak[513];
|
|
||||||
strncpy(capBak, caps, sizeof(capBak) - 1); // copy the capline so we don't destroy our parameter
|
|
||||||
capBak[sizeof(capBak) - 1] = 0;
|
|
||||||
|
|
||||||
char *ptr = capBak;
|
|
||||||
char *tmp = ptr;
|
|
||||||
|
|
||||||
int argc = 0;
|
|
||||||
|
|
||||||
while(*ptr && argc < capsLength)
|
|
||||||
{
|
|
||||||
while(*ptr == ' ') ptr++; // skip any spaces before a capability
|
|
||||||
if(!*ptr) break;
|
|
||||||
|
|
||||||
tmp = ptr;
|
|
||||||
|
|
||||||
while(*tmp && *tmp != ' ') tmp++;
|
|
||||||
|
|
||||||
*tmp++ = 0;
|
|
||||||
|
|
||||||
// copy the cap line while it's untampered with
|
|
||||||
strncpy(capabilities[argc].capLine, ptr, sizeof(capabilities[argc].capLine) - 1);
|
|
||||||
capabilities[argc].capLine[sizeof(capabilities[argc].capLine) - 1] = 0;
|
|
||||||
|
|
||||||
// ptr now points to the start of the next cap
|
|
||||||
ptr = tmp;
|
|
||||||
|
|
||||||
// set the name pointer there
|
|
||||||
char* walker = capabilities[argc].capLine;
|
|
||||||
capabilities[argc].name = walker;
|
|
||||||
|
|
||||||
while(*walker && *walker != '=') walker++;
|
|
||||||
|
|
||||||
if(*walker == '=')
|
|
||||||
{
|
|
||||||
*walker++ = 0;
|
|
||||||
|
|
||||||
// we don't need to move walker since it will be reinitialized in the next `while` iteration, and capLine already ends with a nullbyte
|
|
||||||
capabilities[argc].args = walker;
|
|
||||||
argc++;
|
|
||||||
}
|
|
||||||
else if (*walker == 0)
|
|
||||||
{
|
|
||||||
// the cap has no args
|
|
||||||
capabilities[argc].args = NULL;
|
|
||||||
argc++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return argc;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handlePing(irc_message_t *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
char buf[128]; // most pingpongs are usually short, with the servername being short too
|
|
||||||
snprintf(buf, sizeof buf, "PONG :%s\r\n", msg->argv[0]);
|
|
||||||
IRC_Send(irc, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handleCapabilities(irc_message_t *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
// we are willing to tolerate some spaghetti...
|
|
||||||
if(strcmp(msg->argv[1], "LS") == 0)
|
|
||||||
{
|
|
||||||
snprintf(irc->supportedCapabilities, sizeof irc->supportedCapabilities, "%s", msg->argv[2]);
|
|
||||||
|
|
||||||
// parse the caps
|
|
||||||
irc_capability_t caps[24];
|
|
||||||
int capCount = IRC_ParseCapabilities(irc->supportedCapabilities, caps, sizeof(caps) / sizeof(irc_capability_t));
|
|
||||||
|
|
||||||
irc_capability_t reqCaps[24];
|
|
||||||
|
|
||||||
int reqCapCount = IRC_ParseCapabilities(irc->requestedCapabilities, reqCaps, sizeof(reqCaps) / sizeof(irc_capability_t));
|
|
||||||
|
|
||||||
char buf[513];
|
|
||||||
int bytesWritten = 0;
|
|
||||||
|
|
||||||
for(int i = 0; i < capCount; i++)
|
|
||||||
{
|
|
||||||
for(int j = 0; j < reqCapCount; j++)
|
|
||||||
{
|
|
||||||
if(strcmp(caps[i].name, reqCaps[j].name) == 0)
|
|
||||||
{
|
|
||||||
bytesWritten += snprintf(buf + bytesWritten, sizeof(buf) - bytesWritten, "%s ", reqCaps[j].name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
IRC_RequestCapabilities(buf, irc);
|
|
||||||
}
|
|
||||||
else if (strcmp(msg->argv[1], "ACK") == 0)
|
|
||||||
{
|
|
||||||
int ackedCount = IRC_ParseCapabilities(msg->argv[2], irc->acked, sizeof(irc->acked) / sizeof(irc_capability_t));
|
|
||||||
|
|
||||||
for(int i = 0; i < ackedCount; i++)
|
|
||||||
{
|
|
||||||
if(strcmp(irc->acked[i].name, "sasl") == 0)
|
|
||||||
{
|
|
||||||
IRC_RequestSASLAuthentication(irc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// NAK is a no-op anyway, if it isn't in our irc->acked we just won't use it then.
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handleAuthentication(irc_message_t *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
if(strcmp(msg->argv[0], "+") == 0)
|
|
||||||
{
|
|
||||||
// send our base64 sasl username and password
|
|
||||||
switch(irc->saslAuth.mechanism)
|
|
||||||
{
|
|
||||||
case SASL_MECHANISM_NONE:
|
|
||||||
break;
|
|
||||||
case SASL_MECHANISM_PLAIN:
|
|
||||||
{
|
|
||||||
const char *user = irc->saslAuth.plain_credentials.username;
|
|
||||||
const char *pass = irc->saslAuth.plain_credentials.password;
|
|
||||||
size_t user_len = strlen(user);
|
|
||||||
size_t pass_len = strlen(pass);
|
|
||||||
|
|
||||||
uint8_t sasl_data[user_len + pass_len + 3];
|
|
||||||
size_t idx = 0;
|
|
||||||
|
|
||||||
sasl_data[idx++] = 0;
|
|
||||||
memcpy(sasl_data + idx, user, user_len);
|
|
||||||
idx += user_len;
|
|
||||||
sasl_data[idx++] = 0;
|
|
||||||
memcpy(sasl_data + idx, pass, pass_len);
|
|
||||||
idx += pass_len;
|
|
||||||
|
|
||||||
char base64Out[513];
|
|
||||||
base64_encode(sasl_data, idx, base64Out, sizeof(base64Out));
|
|
||||||
char buf[513];
|
|
||||||
snprintf(buf, sizeof buf, "AUTHENTICATE %s\r\n", base64Out);
|
|
||||||
IRC_Send(irc, buf);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case SASL_MECHANISM_EXTERNAL:
|
|
||||||
{
|
|
||||||
IRC_Send(irc, "AUTHENTICATE +\r\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handleSaslSuccess(irc_message_t *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
// silence msg warning
|
|
||||||
(void)msg;
|
|
||||||
|
|
||||||
IRC_EndCapabilityNegotiation(irc);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
IRC_ProcessMessage is only used for commands that have no need to be a user-listenable event.
|
|
||||||
For pollable events, use IRC_PollEvent(&event).
|
|
||||||
*/
|
|
||||||
void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
command_t commands[] =
|
|
||||||
{
|
|
||||||
{"PING", handlePing},
|
|
||||||
{"CAP", handleCapabilities},
|
|
||||||
{"AUTHENTICATE", handleAuthentication},
|
|
||||||
{RPL_SASLSUCCESS, handleSaslSuccess},
|
|
||||||
{NULL, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
for(int i = 0; commands[i].name; i++)
|
|
||||||
{
|
|
||||||
if(strcmp(msg->command, commands[i].name) == 0)
|
|
||||||
{
|
|
||||||
commands[i].handler(msg, irc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_PRIVMSG(char *to, char *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
char buf[513];
|
|
||||||
snprintf(buf, sizeof buf, "PRIVMSG %s :%s\r\n", to, msg);
|
|
||||||
IRC_Send(irc, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_NOTICE(char *to, char *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
char buf[513];
|
|
||||||
snprintf(buf, sizeof buf, "NOTICE %s :%s\r\n", to, msg);
|
|
||||||
IRC_Send(irc, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_NICK(char *newNick, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
char buf[513];
|
|
||||||
snprintf(buf, sizeof buf, "NICK %s\r\n", newNick);
|
|
||||||
IRC_Send(irc, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_USER(char *ident, char *realname, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
char buf[513];
|
|
||||||
snprintf(buf, sizeof buf, "USER %s 0 * :%s\r\n", ident, realname);
|
|
||||||
IRC_Send(irc, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_Register(irc_client_t *irc)
|
|
||||||
{
|
|
||||||
if(irc->saslAuth.mechanism != SASL_MECHANISM_NONE)
|
|
||||||
{
|
|
||||||
IRC_NeedCapabilities("sasl", irc);
|
|
||||||
IRC_StartCapabilityNegotiation(irc);
|
|
||||||
}
|
|
||||||
IRC_NICK(irc->identity.nick, irc);
|
|
||||||
IRC_USER(irc->identity.username, irc->identity.realname, irc);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_SetSASLParameters(irc_sasl_mechanism_t mechanism, char *username, char *password, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
switch(mechanism)
|
|
||||||
{
|
|
||||||
case SASL_MECHANISM_NONE:
|
|
||||||
break;
|
|
||||||
case SASL_MECHANISM_EXTERNAL:
|
|
||||||
// unimplemented
|
|
||||||
break;
|
|
||||||
case SASL_MECHANISM_PLAIN:
|
|
||||||
irc->saslAuth.mechanism = mechanism;
|
|
||||||
strncpy(irc->saslAuth.plain_credentials.username, username, sizeof(irc->saslAuth.plain_credentials.username) - 1);
|
|
||||||
strncpy(irc->saslAuth.plain_credentials.password, password, sizeof(irc->saslAuth.plain_credentials.password) - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_StartCapabilityNegotiation(irc_client_t *irc)
|
|
||||||
{
|
|
||||||
IRC_Send(irc, "CAP LS 302\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_RequestCapabilities(char *caps, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
char buf[513];
|
|
||||||
snprintf(buf, sizeof buf, "CAP REQ :%s\r\n", caps);
|
|
||||||
IRC_Send(irc, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_NeedCapabilities(char *caps, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
snprintf(irc->requestedCapabilities, sizeof irc->requestedCapabilities, "%s", caps);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_EndCapabilityNegotiation(irc_client_t *irc)
|
|
||||||
{
|
|
||||||
IRC_Send(irc, "CAP END\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_RequestSASLAuthentication(irc_client_t *irc)
|
|
||||||
{
|
|
||||||
switch(irc->saslAuth.mechanism)
|
|
||||||
{
|
|
||||||
case SASL_MECHANISM_NONE:
|
|
||||||
// why?
|
|
||||||
break;
|
|
||||||
case SASL_MECHANISM_PLAIN:
|
|
||||||
IRC_Send(irc, "AUTHENTICATE PLAIN\r\n");
|
|
||||||
break;
|
|
||||||
case SASL_MECHANISM_EXTERNAL:
|
|
||||||
IRC_Send(irc, "AUTHENTICATE EXTERNAL\r\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_CTCPSend(char *to, char *cmd, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
char buf[513];
|
|
||||||
snprintf(buf, sizeof buf, "\x01%s\x01", cmd);
|
|
||||||
IRC_PRIVMSG(to, buf, irc);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_Connect(irc_client_t *irc)
|
|
||||||
{
|
|
||||||
switch(irc->connectionInfo.connType)
|
|
||||||
{
|
|
||||||
case IRC_CONNECTION_PLAINTEXT:
|
|
||||||
irc->connection = NET_Connect(irc->connectionInfo.host, irc->connectionInfo.port);
|
|
||||||
break;
|
|
||||||
case IRC_CONNECTION_TLS:
|
|
||||||
irc->connection = NET_TLS_Connect(irc->connectionInfo.host, irc->connectionInfo.port);
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (irc->connection.sockfd < 0) {
|
|
||||||
fprintf(stderr, "Connection failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t IRC_Receive(irc_client_t *irc, char *toBuf, size_t bufSize)
|
|
||||||
{
|
|
||||||
switch(irc->connectionInfo.connType)
|
|
||||||
{
|
|
||||||
case IRC_CONNECTION_PLAINTEXT:
|
|
||||||
{
|
|
||||||
return recv(irc->connection.sockfd, toBuf, bufSize, 0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case IRC_CONNECTION_TLS:
|
|
||||||
{
|
|
||||||
ssize_t n = SSL_read(irc->connection.ssl, toBuf, bufSize);
|
|
||||||
if (n <= 0) {
|
|
||||||
int err = SSL_get_error(irc->connection.ssl, n);
|
|
||||||
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_Send(irc_client_t *irc, char *toSend)
|
|
||||||
{
|
|
||||||
switch(irc->connectionInfo.connType)
|
|
||||||
{
|
|
||||||
case IRC_CONNECTION_PLAINTEXT:
|
|
||||||
NET_Send(irc->connection, toSend);
|
|
||||||
break;
|
|
||||||
case IRC_CONNECTION_TLS:
|
|
||||||
NET_TLS_Send(irc->connection, toSend);
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_Close(irc_client_t *irc)
|
|
||||||
{
|
|
||||||
switch(irc->connectionInfo.connType)
|
|
||||||
{
|
|
||||||
case IRC_CONNECTION_PLAINTEXT:
|
|
||||||
NET_Close(irc->connection);
|
|
||||||
break;
|
|
||||||
case IRC_CONNECTION_TLS:
|
|
||||||
NET_TLS_Close(irc->connection);
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_SetConnectionParameters(char *host, int port, irc_connection_type_t conntype, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
snprintf(irc->connectionInfo.host, sizeof irc->connectionInfo.host, "%s", host);
|
|
||||||
irc->connectionInfo.port = port;
|
|
||||||
irc->connectionInfo.connType = conntype;
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_SetIdentityParameters(char *nickname, char *username, char *realname, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
snprintf(irc->identity.nick, sizeof irc->identity.nick, "%s", nickname);
|
|
||||||
snprintf(irc->identity.username, sizeof irc->identity.username, "%s", username);
|
|
||||||
snprintf(irc->identity.realname, sizeof irc->identity.realname, "%s", realname);
|
|
||||||
}
|
|
||||||
34
src/base64.c
34
src/base64.c
@@ -1,34 +0,0 @@
|
|||||||
#include <openssl/bio.h>
|
|
||||||
#include <openssl/evp.h>
|
|
||||||
#include <openssl/buffer.h>
|
|
||||||
#include "base64.h"
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
||||||
int base64_encode(const uint8_t *in, size_t in_len, char *out, size_t out_size)
|
|
||||||
{
|
|
||||||
size_t needed = ((in_len + 2) / 3) * 4 + 1;
|
|
||||||
|
|
||||||
if(out_size < needed) return -1;
|
|
||||||
|
|
||||||
size_t i = 0, o = 0;
|
|
||||||
while(i < in_len)
|
|
||||||
{
|
|
||||||
uint32_t n = 0;
|
|
||||||
int remaining = 3;
|
|
||||||
|
|
||||||
n |= (i < in_len ? in[i++] : 0) << 16;
|
|
||||||
n |= (i < in_len ? in[i++] : 0) << 8;
|
|
||||||
n |= (i < in_len ? in[i++] : 0);
|
|
||||||
|
|
||||||
if(i > in_len) remaining = in_len % 3;
|
|
||||||
|
|
||||||
out[o++] = b64[(n >> 18) & 63];
|
|
||||||
out[o++] = b64[(n >> 12) & 63];
|
|
||||||
out[o++] = remaining > 1 ? b64[(n >> 6) & 63] : '=';
|
|
||||||
out[o++] = remaining > 2 ? b64[n & 63] : '=';
|
|
||||||
}
|
|
||||||
|
|
||||||
out[o] = 0;
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
@@ -24,14 +24,16 @@ int main(int argc, char **argv)
|
|||||||
SSL_library_init();
|
SSL_library_init();
|
||||||
OpenSSL_add_ssl_algorithms();
|
OpenSSL_add_ssl_algorithms();
|
||||||
SSL_load_error_strings();
|
SSL_load_error_strings();
|
||||||
|
|
||||||
fd_set readfds;
|
fd_set readfds;
|
||||||
static char linebuf[1026];
|
static char linebuf[1026];
|
||||||
static size_t lineLen = 0;
|
static size_t lineLen = 0;
|
||||||
char recvline[513];
|
char recvline[513];
|
||||||
char sendline[513];
|
char sendline[513];
|
||||||
|
|
||||||
irc_client_t irc;
|
irc_client_t irc;
|
||||||
|
IRC_Init(&irc);
|
||||||
|
|
||||||
|
|
||||||
IRC_SetConnectionParameters("irc.libera.chat", 6697, IRC_CONNECTION_TLS, &irc);
|
IRC_SetConnectionParameters("irc.libera.chat", 6697, IRC_CONNECTION_TLS, &irc);
|
||||||
IRC_Connect(&irc);
|
IRC_Connect(&irc);
|
||||||
@@ -121,6 +123,7 @@ int main(int argc, char **argv)
|
|||||||
printf("Connection closed by foreign host.\n");
|
printf("Connection closed by foreign host.\n");
|
||||||
|
|
||||||
IRC_Close(&irc);
|
IRC_Close(&irc);
|
||||||
|
IRC_Free(&irc);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -33,10 +33,15 @@ sslSockfd_t NET_Connect(const char *host, int port)
|
|||||||
char portNumber[6];
|
char portNumber[6];
|
||||||
snprintf(portNumber, 6, "%d", port);
|
snprintf(portNumber, 6, "%d", port);
|
||||||
|
|
||||||
|
DEFRAWT();
|
||||||
|
|
||||||
|
PRINT_INFO(timeinfo);
|
||||||
|
|
||||||
printf("Looking up %s...\n", host);
|
printf("Looking up %s...\n", host);
|
||||||
|
|
||||||
if ((status = getaddrinfo(host, portNumber, &hints, &res)) == -1)
|
if ((status = getaddrinfo(host, portNumber, &hints, &res)) == -1)
|
||||||
{
|
{
|
||||||
|
PRINT_INFO(timeinfo);
|
||||||
fprintf(stderr, "Unable to lookup %s: %s\n", host, gai_strerror(status));
|
fprintf(stderr, "Unable to lookup %s: %s\n", host, gai_strerror(status));
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
@@ -61,6 +66,8 @@ sslSockfd_t NET_Connect(const char *host, int port)
|
|||||||
}
|
}
|
||||||
|
|
||||||
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
|
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
|
||||||
|
REDEFRAWT();
|
||||||
|
PRINT_INFO(timeinfo);
|
||||||
printf("Connecting to %s:%d (%s:%d)...\n", host, port, ipstr, port);
|
printf("Connecting to %s:%d (%s:%d)...\n", host, port, ipstr, port);
|
||||||
|
|
||||||
if((sock.sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
|
if((sock.sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
|
||||||
@@ -80,6 +87,8 @@ sslSockfd_t NET_Connect(const char *host, int port)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
REDEFRAWT();
|
||||||
|
PRINT_INFO(timeinfo);
|
||||||
printf("Connected!\n");
|
printf("Connected!\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -92,7 +101,7 @@ void NET_Send(sslSockfd_t info, const char *toSend)
|
|||||||
size_t bytesSent = 0;
|
size_t bytesSent = 0;
|
||||||
size_t sendLen = strlen(toSend);
|
size_t sendLen = strlen(toSend);
|
||||||
|
|
||||||
printf("> %s", toSend);
|
// printf("> %s", toSend);
|
||||||
|
|
||||||
while(bytesSent < sendLen)
|
while(bytesSent < sendLen)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user