Added API data definitions.
This commit is contained in:
@@ -1,10 +1,20 @@
|
||||
#include "include/data.h"
|
||||
#include "include/api.h"
|
||||
|
||||
PacketType packet_type_parse(const char* type_str) {
|
||||
if (!type_str) return PACKET_TYPE_INVALID;
|
||||
if (!strcmp(type_str, "join")) return PACKET_TYPE_JOIN;
|
||||
if (!strcmp(type_str, "JOIN_EV")) return PACKET_TYPE_JOIN_EV;
|
||||
return PACKET_TYPE_INVALID;
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char* packet_type_strings[] = {
|
||||
[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) {
|
||||
@@ -17,12 +27,11 @@ Packet* packet_init(PacketType type, void* data) {
|
||||
return packet;
|
||||
}
|
||||
|
||||
Packet* packet_init_untrusted(PacketType type, void* data) {
|
||||
Packet* packet = malloc(sizeof(Packet));
|
||||
if (!packet) return NULL;
|
||||
Packet* packet_init_safe(char* type, void* data) {
|
||||
PacketType t = packet_type_parse(type);
|
||||
if (t == PACKET_TYPE_BAD) return NULL;
|
||||
|
||||
packet->type = type;
|
||||
packet->data = data;
|
||||
if (!data) return NULL;
|
||||
|
||||
return packet;
|
||||
return packet_init(t, data);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
// TODO: Make types for allt he proto events. This is quite messy without that.
|
||||
|
||||
#include "include/chat.h"
|
||||
#include "include/session.h"
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "include/data.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int name_verify(const char* name) {
|
||||
if (!name) return 0;
|
||||
if (strlen(name) > NAME_MAX_LENGTH) return 0;
|
||||
|
||||
@@ -4,38 +4,50 @@
|
||||
#include "data.h"
|
||||
|
||||
typedef enum {
|
||||
PACKET_TYPE_JOIN,
|
||||
PACKET_TYPE_WELCOME,
|
||||
PACKET_TYPE_JOIN_EV,
|
||||
PACKET_TYPE_JOIN, // C -> S.
|
||||
PACKET_TYPE_WELCOME, // S -> C.
|
||||
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;
|
||||
|
||||
static const char* packet_type_strings[] = {
|
||||
[PACKET_TYPE_JOIN]= "join",
|
||||
[PACKET_TYPE_WELCOME]= "welcome",
|
||||
[PACKET_TYPE_JOIN_EV]= "join_ev",
|
||||
};
|
||||
|
||||
PacketType packet_type_parse(const char* type_str);
|
||||
// Create a packet type from untrusted data.
|
||||
PacketType packet_type_parse(const char* type);
|
||||
|
||||
typedef struct {
|
||||
PacketType type;
|
||||
void* data;
|
||||
} Packet;
|
||||
|
||||
// Create a new packet.
|
||||
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 {
|
||||
Name name;
|
||||
Name name; // Your selected name.
|
||||
} PacketJoin;
|
||||
|
||||
typedef struct {
|
||||
UserData* online;
|
||||
MsgData* history;
|
||||
UserID id; // Their ID.
|
||||
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;
|
||||
|
||||
typedef struct {
|
||||
UserData newguy;
|
||||
UserData newguy; // Who just joined.
|
||||
} 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
|
||||
|
||||
Reference in New Issue
Block a user