WIP commit
This commit is contained in:
144
src/IRC.c
144
src/IRC.c
@@ -5,7 +5,7 @@
|
||||
#include <string.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);
|
||||
char *ptr = msg->line;
|
||||
@@ -63,22 +63,110 @@ int IRC_ParseMessage(char *line, irc_message_t *msg)
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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]);
|
||||
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)
|
||||
{
|
||||
command_t commands[] =
|
||||
{
|
||||
{"PING", handlePing},
|
||||
{"CAP", handleCapabilities},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -89,4 +177,56 @@ void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc)
|
||||
commands[i].handler(msg, 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_ParseMessage(linebuf, &msg);
|
||||
|
||||
IRC_ProcessMessage(&msg, &irc);
|
||||
|
||||
// try to print it
|
||||
|
||||
if(msg.source != NULL)
|
||||
@@ -101,5 +103,7 @@ int main(int argc, char **argv)
|
||||
|
||||
printf("Connection closed by foreign host.\n");
|
||||
|
||||
NET_Close(sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user