diff --git a/include/mappings.h b/include/mappings.h index b00bd75..6e344ca 100644 --- a/include/mappings.h +++ b/include/mappings.h @@ -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, diff --git a/include/uirc.h b/include/uirc.h index b6d5207..44e090a 100644 --- a/include/uirc.h +++ b/include/uirc.h @@ -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); diff --git a/src/uirc.c b/src/uirc.c index c3897f5..ef2f648 100644 --- a/src/uirc.c +++ b/src/uirc.c @@ -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) {