Added API data definitions.

This commit is contained in:
2026-02-21 10:03:04 -05:00
parent c21c96046a
commit 504c78feba
4 changed files with 50 additions and 29 deletions

View File

@@ -1,10 +1,20 @@
#include "include/data.h" #include "include/api.h"
PacketType packet_type_parse(const char* type_str) { #include <stdlib.h>
if (!type_str) return PACKET_TYPE_INVALID; #include <string.h>
if (!strcmp(type_str, "join")) return PACKET_TYPE_JOIN;
if (!strcmp(type_str, "JOIN_EV")) return PACKET_TYPE_JOIN_EV; static const char* packet_type_strings[] = {
return PACKET_TYPE_INVALID; [PACKET_TYPE_JOIN] = "join", [PACKET_TYPE_WELCOME] = "welcome",
[PACKET_TYPE_JOIN_EVT] = "join_evt", [PACKET_TYPE_MSG] = "msg",
[PACKET_TYPE_MSG_EVT] = "msg_evt",
};
PacketType packet_type_parse(const char* type) {
for (int i = 0; i < PACKET_TYPE_MAX; i++)
if (!strcmp(type, packet_type_strings[i])) return (PacketType)i;
return PACKET_TYPE_BAD;
} }
Packet* packet_init(PacketType type, void* data) { Packet* packet_init(PacketType type, void* data) {
@@ -17,12 +27,11 @@ Packet* packet_init(PacketType type, void* data) {
return packet; return packet;
} }
Packet* packet_init_untrusted(PacketType type, void* data) { Packet* packet_init_safe(char* type, void* data) {
Packet* packet = malloc(sizeof(Packet)); PacketType t = packet_type_parse(type);
if (!packet) return NULL; if (t == PACKET_TYPE_BAD) return NULL;
packet->type = type; if (!data) return NULL;
packet->data = data;
return packet; return packet_init(t, data);
} }

View File

@@ -1,5 +1,3 @@
// TODO: Make types for allt he proto events. This is quite messy without that.
#include "include/chat.h" #include "include/chat.h"
#include "include/session.h" #include "include/session.h"

View File

@@ -1,5 +1,7 @@
#include "include/data.h" #include "include/data.h"
#include <string.h>
int name_verify(const char* name) { int name_verify(const char* name) {
if (!name) return 0; if (!name) return 0;
if (strlen(name) > NAME_MAX_LENGTH) return 0; if (strlen(name) > NAME_MAX_LENGTH) return 0;

View File

@@ -4,38 +4,50 @@
#include "data.h" #include "data.h"
typedef enum { typedef enum {
PACKET_TYPE_JOIN, PACKET_TYPE_JOIN, // C -> S.
PACKET_TYPE_WELCOME, PACKET_TYPE_WELCOME, // S -> C.
PACKET_TYPE_JOIN_EV, PACKET_TYPE_JOIN_EVT, // S->A.
PACKET_TYPE_MSG, // C->S.
PACKET_TYPE_MSG_EVT, // S->A.
PACKET_TYPE_MAX = PACKET_TYPE_MSG_EVT,
PACKET_TYPE_BAD,
} PacketType; } PacketType;
static const char* packet_type_strings[] = { // Create a packet type from untrusted data.
[PACKET_TYPE_JOIN]= "join", PacketType packet_type_parse(const char* type);
[PACKET_TYPE_WELCOME]= "welcome",
[PACKET_TYPE_JOIN_EV]= "join_ev",
};
PacketType packet_type_parse(const char* type_str);
typedef struct { typedef struct {
PacketType type; PacketType type;
void* data; void* data;
} Packet; } Packet;
// Create a new packet.
Packet* packet_init(PacketType type, void* data); Packet* packet_init(PacketType type, void* data);
Packet* packet_init_untrusted(PacketType type, void* data);
// Create a packet from untrusted data.
Packet* packet_init_safe(char* type, void* data);
typedef struct { typedef struct {
Name name; Name name; // Your selected name.
} PacketJoin; } PacketJoin;
typedef struct { typedef struct {
UserData* online; UserID id; // Their ID.
MsgData* history; size_t onlinec; // The number of users online.
UserData* online; // The list of online users.
size_t historyc; // The number of messages in history.
MsgData* history; // The list of historical messages.
} PacketWelcome; } PacketWelcome;
typedef struct { typedef struct {
UserData newguy; UserData newguy; // Who just joined.
} PacketJoinEvent; } PacketJoinEvent;
typedef struct {
MsgContent content; // The content of the message.
MsgID parent; // The ID of the message being replied to.
} PacketMsg;
typedef MsgData PacketMsgEvt;
#endif #endif