Add validators and name command enum

This commit is contained in:
Alex 2020-07-17 13:47:24 +02:00
parent 2c543d6d03
commit 08b6b0f187
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
3 changed files with 75 additions and 1 deletions

View File

@ -6,13 +6,14 @@
#define ERR_UIRC_INVALID_FORMAT -3
#define ERR_UIRC_BUFFER_ERR -4
#define ERR_UIRC_UNKNOWN_TOKEN -5
#define ERR_UIRC_VAL_FAILED -10
/* Mode bitmask values */
#define MBMASK_WALLOPS 2 /* 010 */
#define MBMASK_INVIS 4 /* 100 */
#define UIRC_FCMD ADMIN
enum {
enum commands {
ADMIN,
AWAY,
CONNECT,

View File

@ -31,6 +31,13 @@ extern signed int Tok_user(char* str, IRC_User* out, bool useorig);
extern signed int Assm_mesg(char* buf, IRC_Message* in);
extern signed int Assm_user(char* buf, IRC_User* in, bool useorig);
/* Validators: They check that the parsed message is valid and follows the standard */
extern signed int Val_mesg(IRC_Message* mesg);
extern signed int Val_channame(char* chan);
extern signed int Val_type_nocrlf(char* str);
extern signed int Val_type_nospcl(char* str);
extern signed int Val_type_noblcm(char* str);
#ifdef UIRC_IRCV3
extern signed int Tok_tags(char* str, IRC_Tags* out);
extern signed int Assm_tags(char* buf, IRC_Tags* in);

View File

@ -184,6 +184,72 @@ signed int Assm_mesg(char* buf, IRC_Message* in)
return ERR_UIRC_BUFFER_ERR;
return pos - buf;
}
signed int Val_mesg(IRC_Message* mesg)
{
if (mesg == NULL)
return ERR_UIRC_NULL_ARGS;
for (unsigned int i = 0; i < 14 && mesg->args[i] != NULL; i++) {
if (Val_type_nocrlf(mesg->args[i]) != 1)
return ERR_UIRC_VAL_FAILED;
if (Val_type_nospcl(mesg->args[i]) != 1)
return ERR_UIRC_VAL_FAILED;
}
if (mesg->trailing != NULL) {
if (Val_type_nocrlf(mesg->trailing) != 1)
return ERR_UIRC_VAL_FAILED;
}
return 1;
}
signed int Val_type_nocrlf(char* str)
{
if (str == NULL)
return ERR_UIRC_NULL_ARGS;
for (; *str; str++) {
if (*str == '\r' || *str == '\n')
return 0;
}
return 1;
}
signed int Val_type_nospcl(char* str)
{
if (str == NULL)
return ERR_UIRC_NULL_ARGS;
for (; *str; str++) {
if (*str == ' ' || *str == ':')
return 0;
}
return 1;
}
signed int Val_type_noblcm(char* str)
{
if (str == NULL)
return ERR_UIRC_NULL_ARGS;
for (; *str; str++) {
if (*str == '\a' || *str == ',')
return 0;
}
return 1;
}
signed int Val_channame(char* chan)
{
if (chan == NULL)
return ERR_UIRC_NULL_ARGS;
if (*chan != '#' || *chan != '+' || *chan != '!' || *chan != '&')
return 0;
char* clps = ++chan;
if ((clps = strchr(chan, ':')) != NULL) {
*clps = '\0';
if (Val_type_nospcl(chan) != 1 || Val_type_nocrlf(chan) != 1 || Val_type_noblcm(chan) != 1) {
*clps = ':';
return 0;
}
*clps = ':';
chan = ++clps;
}
if (Val_type_nospcl(chan) != 1 || Val_type_nocrlf(chan) != 1 || Val_type_noblcm(chan) != 1)
return 0;
return 1;
}
#ifdef UIRC_IRCV3
signed int Assm_tags(char* buf, IRC_Tags* in)
{