Remove tokspace, improve logic and fix some bugs

This commit is contained in:
Alex 2020-06-30 23:39:56 +02:00
parent b14a8a64fd
commit 2fbcadfaf3
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
3 changed files with 32 additions and 44 deletions

View File

@ -22,6 +22,7 @@
typedef struct uirc_tag {
char* value;
bool clientbound;
bool present;
} IRC_Tag;
typedef struct name {
char* nick;

View File

@ -70,7 +70,7 @@ int uirc_tokenize_message(IRC_Message* irc_msg, char** line)
if (*progr == '@') {
char* tags;
if ((tags = tokspace(progr, &progr)) != NULL) {
if ((tags = strtok_r(progr, " ", &progr)) != NULL) {
if (!tagmgr(&tags, irc_msg, true))
return -1;
} else
@ -78,29 +78,30 @@ int uirc_tokenize_message(IRC_Message* irc_msg, char** line)
}
if (*progr == ':') {
char* prefix;
if ((prefix = tokspace(progr, &progr)) != NULL) {
if ((prefix = strtok_r(progr, " ", &progr)) != NULL) {
if (!tok_prefix(prefix, &irc_msg->name))
return 0;
} else
return 0;
}
command = tokspace(progr, &progr);
if (isalpha(*command)) {
if (!(irc_msg->cmd = uirc_ircmd_stoi(command)))
return 0;
} else if (isdigit(*command)) {
if ((irc_msg->cmd = atoi(command)) == 0)
if ((command = strtok_r(NULL, " ", &progr)) != NULL) {
if (isalpha(*command)) {
if (!(irc_msg->cmd = uirc_ircmd_stoi(command)))
return 0;
} else if (isdigit(*command)) {
if ((irc_msg->cmd = atoi(command)) == 0)
return 0;
} else {
return 0;
}
} else {
return 0;
}
if (progr == NULL)
return 1;
int i;
for (i = 0; *progr != ':' && i < 14; i++) {
if ((irc_msg->args[i] = strtok_r(NULL, " :", &progr)) == NULL)
if ((irc_msg->args[i] = strtok_r(NULL, " ", &progr)) == NULL)
return 0;
}
irc_msg->args[i] = NULL;
@ -153,7 +154,7 @@ int uirc_assm_mesg(char* buf, IRC_Message* mesg)
bool tagmgr(char** pos, IRC_Message* mesg, bool tagged)
{
const char* origpos = *pos;
char *cval, *cpos = *pos;
char *cval, *cpos = *pos, *ctag = NULL;
int cnt;
bool clientbound;
const struct tagmapping tags[] = {
@ -169,25 +170,24 @@ bool tagmgr(char** pos, IRC_Message* mesg, bool tagged)
if (tagged) {
if (*cpos == '@')
cpos++;
do {
while ((ctag = strtok_r(NULL, "; ", &cpos)) != NULL) {
clientbound = false;
if (*cpos == '+') {
cpos++;
if (*ctag == '+') {
ctag++;
clientbound = true;
}
if ((cval = strchr(cpos, '=')) != NULL) {
cval = strchr(ctag, '=');
if (cval != NULL)
*(cval++) = '\0';
for (unsigned int i = 0; i < sizeof(tags) / sizeof(struct tagmapping); i++) {
if (!strcmp(cpos, tags[i].name)) {
for (unsigned int i = 0; i < sizeof(tags) / sizeof(struct tagmapping); i++) {
if (!strcmp(ctag, tags[i].name)) {
if (cval != NULL)
(*tags[i].assg).value = cval;
(*tags[i].assg).clientbound = clientbound;
}
(*tags[i].assg).clientbound = clientbound;
(*tags[i].assg).present = true;
}
if ((cpos = strchr(cval, ';')) != NULL)
*(cpos++) = '\0';
} else
return false;
} while (cpos != NULL);
}
}
} else {
for (unsigned int i = 0; i < sizeof(tags) / sizeof(struct tagmapping); i++) {
if ((*tags[i].assg).value != NULL) {
@ -216,23 +216,12 @@ bool tagmgr(char** pos, IRC_Message* mesg, bool tagged)
}
return true;
}
char* tokspace(char* pos, char** save)
{
char* delimpos = NULL;
if (pos == NULL || (delimpos = strchr(pos, ' ')) == NULL)
return NULL;
while (*delimpos == ' ') {
*(delimpos++) = '\0';
}
*save = delimpos;
return pos;
}
bool assm_prefix(char** str, IRC_User* usr)
{
int cnt;
char* pos = *str;
*(pos++) = ':';
if (!usr->nick && !usr->user) {
if (usr->nick == NULL && usr->user == NULL) {
if ((cnt = sprintf(pos, "%s", usr->host)) > 0)
pos += cnt;
else
@ -242,13 +231,13 @@ bool assm_prefix(char** str, IRC_User* usr)
pos += cnt;
else
return false;
if (usr->user) {
if (usr->user != NULL) {
if ((cnt = sprintf(pos, "!%s", usr->user)) > 0)
pos += cnt;
else
return false;
}
if (usr->host) {
if (usr->host != NULL) {
if ((cnt = sprintf(pos, "@%s", usr->host)) > 0)
pos += cnt;
else
@ -262,11 +251,11 @@ bool assm_prefix(char** str, IRC_User* usr)
bool tok_prefix(char* prefix, IRC_User* user)
{
char* pos = (*prefix == ':') ? prefix + 1 : prefix;
if ((user->host = strchr(prefix, '@')) != NULL)
if ((user->host = strchr(pos, '@')) != NULL)
*(user->host++) = '\0';
if ((user->user = strchr(prefix, '!')) != NULL) /* RFC2812 says this cannot be here without the host but RFC1459 says it can, we accept both options */
if ((user->user = strchr(pos, '!')) != NULL) /* RFC2812 says this cannot be here without the host but RFC1459 says it can, we accept both options */
*(user->user++) = '\0';
if (*(user->nick = prefix) == '\0') /* NOTE: This may be the server instead of a nick according to RFC2812, it is left to the library user to decide how to interpret this based on the context. */
if (*(user->nick = pos) == '\0') /* NOTE: This may be the server instead of a nick according to RFC2812, it is left to the library user to decide how to interpret this based on the context. */
return false;
return true;
}

View File

@ -21,8 +21,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char* tokspace(char* pos, char** save);
char* strtok_rs(char* pos, char* sin, char* mul, char** save);
bool tagmgr(char** pos, IRC_Message* mesg, bool tagged);
bool tok_prefix(char* prefix, IRC_User* user);
bool assm_prefix(char** str, IRC_User* usr);