WIP commit
This commit is contained in:
@@ -1,6 +1,13 @@
|
|||||||
#ifndef IRC_H
|
#ifndef IRC_H
|
||||||
#define IRC_H
|
#define IRC_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
PLAIN,
|
||||||
|
EXTERNAL
|
||||||
|
} irc_sasl_method_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char *nickname;
|
char *nickname;
|
||||||
@@ -9,7 +16,6 @@ typedef struct
|
|||||||
int identlen;
|
int identlen;
|
||||||
char *hostname;
|
char *hostname;
|
||||||
int hostlen;
|
int hostlen;
|
||||||
|
|
||||||
} irc_hostmask_t;
|
} irc_hostmask_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -20,10 +26,32 @@ typedef struct {
|
|||||||
int argc;
|
int argc;
|
||||||
} irc_message_t;
|
} irc_message_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
irc_hostmask_t hostmask;
|
||||||
|
char prefix;
|
||||||
|
} irc_chanuser_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char name[64];
|
||||||
|
char topic[512];
|
||||||
|
irc_chanuser_t* users;
|
||||||
|
} irc_channel_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char name[32];
|
||||||
|
char args[64];
|
||||||
|
} irc_capability_t;
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int sockfd;
|
int sockfd;
|
||||||
irc_hostmask_t ownHostmask;
|
irc_hostmask_t ownHostmask;
|
||||||
|
char supportedCapabilities[512];
|
||||||
|
char* requestedCapabilities;
|
||||||
} irc_client_t;
|
} irc_client_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -31,9 +59,23 @@ typedef struct {
|
|||||||
void (*handler)(irc_message_t *msg, irc_client_t *irc);
|
void (*handler)(irc_message_t *msg, irc_client_t *irc);
|
||||||
} command_t;
|
} command_t;
|
||||||
|
|
||||||
int IRC_ParseMessage(char* line, irc_message_t *msg);
|
void IRC_ParseMessage(char* line, irc_message_t *msg);
|
||||||
void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc);
|
void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc);
|
||||||
|
|
||||||
void IRC_PRIVMSG(char* msg, irc_client_t irc);
|
// various IRC commands
|
||||||
|
void IRC_PRIVMSG(char* to, char* msg, irc_client_t *irc);
|
||||||
|
void IRC_NOTICE(char* to, char* msg, irc_client_t *irc);
|
||||||
|
void IRC_NICK(char *nick, irc_client_t *irc);
|
||||||
|
void IRC_USER(char *ident, char* realname, irc_client_t *irc);
|
||||||
|
|
||||||
|
// registration
|
||||||
|
void IRC_Register(char *nick, char* ident, char *realname, bool saslEnabled, irc_sasl_method_t saslMethod, char *saslUsername, char *saslPassword, irc_client_t *irc);
|
||||||
|
|
||||||
|
// Capability negotiation block
|
||||||
|
void IRC_StartCapabilityNegotiation(irc_client_t *irc);
|
||||||
|
void IRC_RequestCapabilities(char* caps, irc_client_t *irc);
|
||||||
|
void IRC_EndCapabilityNegotiation(irc_client_t *irc);
|
||||||
|
|
||||||
|
int IRC_ParseCapabilities(char* caps, irc_capability_t *capabilities, int capsLength);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
144
src/IRC.c
144
src/IRC.c
@@ -5,7 +5,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
|
||||||
int IRC_ParseMessage(char *line, irc_message_t *msg)
|
void IRC_ParseMessage(char *line, irc_message_t *msg)
|
||||||
{
|
{
|
||||||
strncpy(msg->line, line, sizeof(msg->line) - 1);
|
strncpy(msg->line, line, sizeof(msg->line) - 1);
|
||||||
char *ptr = msg->line;
|
char *ptr = msg->line;
|
||||||
@@ -63,22 +63,110 @@ int IRC_ParseMessage(char *line, irc_message_t *msg)
|
|||||||
|
|
||||||
msg->argc++;
|
msg->argc++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WIP
|
||||||
|
int IRC_ParseCapabilities(char *caps, irc_capability_t *capabilities, int capsLength)
|
||||||
|
{
|
||||||
|
char capBak[512];
|
||||||
|
strncpy(capBak, caps, sizeof(capBak) - 1);
|
||||||
|
capBak[sizeof(capBak) - 1] = 0;
|
||||||
|
|
||||||
|
memset(capabilities, 0, sizeof(irc_capability_t) * capsLength);
|
||||||
|
|
||||||
|
char *ptr = capBak;
|
||||||
|
|
||||||
|
int argc = 0;
|
||||||
|
|
||||||
|
while(*ptr && argc <= capsLength)
|
||||||
|
{
|
||||||
|
// copy the cap name
|
||||||
|
strncpy(capabilities[argc].name, ptr, sizeof(capabilities[argc].name) - 1);
|
||||||
|
capabilities[argc].name[sizeof(capabilities[argc].name) - 1] = 0;
|
||||||
|
while(*ptr != ' ' && *ptr != '=') ptr++;
|
||||||
|
|
||||||
|
if(*ptr == '=')
|
||||||
|
{
|
||||||
|
*ptr++ = 0;
|
||||||
|
}
|
||||||
|
else if (*ptr == ' ')
|
||||||
|
{
|
||||||
|
if(strlen(capabilities[argc].name) > 0)
|
||||||
|
{
|
||||||
|
// the cap has no args
|
||||||
|
|
||||||
|
}
|
||||||
|
argc++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handlePing(irc_message_t *msg, irc_client_t *irc)
|
static void handlePing(irc_message_t *msg, irc_client_t *irc)
|
||||||
{
|
{
|
||||||
char buf[128];
|
char buf[128]; // most pingpongs are usually short, with the servername being short too
|
||||||
snprintf(buf, sizeof buf, "PONG :%s", msg->argv[0]);
|
snprintf(buf, sizeof buf, "PONG :%s", msg->argv[0]);
|
||||||
NET_Send(irc->sockfd, buf);
|
NET_Send(irc->sockfd, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void handleCapabilities(irc_message_t *msg, irc_client_t *irc)
|
||||||
|
{
|
||||||
|
// we are willing to tolerate some spaghetti...
|
||||||
|
if(strncmp(msg->argv[1], "LS", 4) == 0)
|
||||||
|
{
|
||||||
|
// add the supported capabilities to our client info
|
||||||
|
strncpy(irc->supportedCapabilities, msg->argv[2], sizeof irc->supportedCapabilities);
|
||||||
|
irc->supportedCapabilities[sizeof(irc->supportedCapabilities) - 1] = 0;
|
||||||
|
// try to find specific requested capabilities in there
|
||||||
|
|
||||||
|
char* caps[64];
|
||||||
|
int capCount = 0;
|
||||||
|
|
||||||
|
char* ptr = irc->requestedCapabilities;
|
||||||
|
|
||||||
|
// split the requested caps
|
||||||
|
while(*ptr && capCount < 64)
|
||||||
|
{
|
||||||
|
caps[capCount] = ptr;
|
||||||
|
|
||||||
|
while(*ptr && *ptr != ' ') ptr++;
|
||||||
|
|
||||||
|
if(*ptr)
|
||||||
|
{
|
||||||
|
*ptr++ = NUL;
|
||||||
|
while (*ptr == ' ') ptr++;
|
||||||
|
}
|
||||||
|
|
||||||
|
capCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
char buf[512];
|
||||||
|
int bytesWritten = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i < capCount; i++)
|
||||||
|
{
|
||||||
|
if(strstr(irc->supportedCapabilities, caps[i]) != NULL)
|
||||||
|
{
|
||||||
|
// queue that up for one CAP REQ
|
||||||
|
bytesWritten += snprintf(buf + bytesWritten, sizeof(buf) - bytesWritten, "%s ", caps[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IRC_RequestCapabilities(buf, 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)
|
void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc)
|
||||||
{
|
{
|
||||||
command_t commands[] =
|
command_t commands[] =
|
||||||
{
|
{
|
||||||
{"PING", handlePing},
|
{"PING", handlePing},
|
||||||
|
{"CAP", handleCapabilities},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -90,3 +178,55 @@ void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IRC_PRIVMSG(char *to, char *msg, irc_client_t *irc)
|
||||||
|
{
|
||||||
|
char buf[512];
|
||||||
|
snprintf(buf, sizeof buf, "PRIVMSG %s :%s", to, msg);
|
||||||
|
NET_Send(irc->sockfd, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRC_NOTICE(char *to, char *msg, irc_client_t *irc)
|
||||||
|
{
|
||||||
|
char buf[512];
|
||||||
|
snprintf(buf, sizeof buf, "NOTICE %s :%s", to, msg);
|
||||||
|
NET_Send(irc->sockfd, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRC_NICK(char *newNick, irc_client_t *irc)
|
||||||
|
{
|
||||||
|
char buf[512];
|
||||||
|
snprintf(buf, sizeof buf, "NICK %s", newNick);
|
||||||
|
NET_Send(irc->sockfd, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRC_USER(char *ident, char *realname, irc_client_t *irc)
|
||||||
|
{
|
||||||
|
char buf[512];
|
||||||
|
snprintf(buf, sizeof buf, "USER %s 0 * :%s", ident, realname);
|
||||||
|
NET_Send(irc->sockfd, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRC_REGISTER(char *nick, char *ident, char *realname, bool saslEnabled, irc_sasl_method_t saslMethod, char *saslUsername, char *saslPassword, irc_client_t *irc)
|
||||||
|
{
|
||||||
|
IRC_StartCapabilityNegotiation(irc);
|
||||||
|
IRC_NICK(nick, irc);
|
||||||
|
IRC_USER(ident, realname, irc);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRC_StartCapabilityNegotiation(irc_client_t *irc)
|
||||||
|
{
|
||||||
|
NET_Send(irc->sockfd, "CAP LS 302");
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRC_RequestCapabilities(char *caps, irc_client_t *irc)
|
||||||
|
{
|
||||||
|
char buf[512];
|
||||||
|
snprintf(buf, sizeof buf, "CAP REQ :%s", caps);
|
||||||
|
NET_Send(irc->sockfd, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRC_EndCapabilityNegotiation(irc_client_t *irc)
|
||||||
|
{
|
||||||
|
NET_Send(irc->sockfd, "CAP END");
|
||||||
|
}
|
||||||
@@ -72,6 +72,8 @@ int main(int argc, char **argv)
|
|||||||
irc_message_t msg;
|
irc_message_t msg;
|
||||||
IRC_ParseMessage(linebuf, &msg);
|
IRC_ParseMessage(linebuf, &msg);
|
||||||
|
|
||||||
|
IRC_ProcessMessage(&msg, &irc);
|
||||||
|
|
||||||
// try to print it
|
// try to print it
|
||||||
|
|
||||||
if(msg.source != NULL)
|
if(msg.source != NULL)
|
||||||
@@ -101,5 +103,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
printf("Connection closed by foreign host.\n");
|
printf("Connection closed by foreign host.\n");
|
||||||
|
|
||||||
|
NET_Close(sockfd);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user