Reformat code, add if preprocessor comments, shorten structrs, remove analyzer

This commit is contained in:
Alex D. 2020-12-16 14:22:25 +00:00
parent 124da1eb77
commit 0d019b02dc
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
19 changed files with 502 additions and 446 deletions

View File

@ -1,20 +1,41 @@
--- ---
Language: Cpp
Standard: Cpp11
BasedOnStyle: LLVM BasedOnStyle: LLVM
TabWidth: 8 AlignConsecutiveMacros: 'true'
IndentWidth: 8 AlignConsecutiveAssignments: 'true'
UseTab: ForIndentation AlignConsecutiveDeclarations: 'true'
AllowShortBlocksOnASingleLine: true AlignEscapedNewlines: Left
AllowShortCaseLabelsOnASingleLine: true AllowShortBlocksOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: true AllowShortCaseLabelsOnASingleLine: 'true'
AllowShortIfStatementsOnASingleLine: true AllowShortFunctionsOnASingleLine: All
AllowShortLoopsOnASingleLine: true AllowShortIfStatementsOnASingleLine: Always
AllowAllParametersOfDeclarationOnNextLine: true AllowShortLoopsOnASingleLine: 'true'
AlwaysBreakAfterDefinitionReturnType: TopLevel
AlwaysBreakAfterReturnType: AllDefinitions
AlwaysBreakBeforeMultilineStrings: 'false'
BinPackArguments: 'false'
BinPackParameters: 'false'
BreakBeforeBinaryOperators: NonAssignment BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Linux BreakBeforeBraces: Linux
IndentCaseLabels: true BreakStringLiterals: 'false'
ColumnLimit: '150'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
Cpp11BracedListStyle: 'false'
IncludeBlocks: Regroup
IndentCaseLabels: 'true'
IndentPPDirectives: None
IndentWidth: '8'
Language: Cpp
PointerAlignment: Left PointerAlignment: Left
ColumnLimit: 150 ReflowComments: 'true'
... SortIncludes: 'true'
SpaceAfterCStyleCast: 'true'
SpaceAfterLogicalNot: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeCpp11BracedList: 'true'
SpaceBeforeParens: ControlStatements
SpacesInCStyleCastParentheses: 'false'
Standard: Cpp11
TabWidth: '8'
UseTab: ForIndentation
...

View File

@ -1,9 +1,9 @@
--- ---
Checks: 'clang-diagnostic-*,clang-analyzer-*,clang-diagnostic-*,clang-analyzer-*,-*,clang-analyzer-core.*,clang-analyzer-optin.performance.*,clang-analyzer-optin.portability.*,clang-analyzer-nullability.*,clang-analyzer-security.*,clang-analyzer-unix.*,bugprone-*,misc-*,performance-*,readability-*,-*,clang-analyzer-core.*,clang-analyzer-optin.performance.*,clang-analyzer-optin.portability.*,clang-analyzer-nullability.*,clang-analyzer-security.*,clang-analyzer-unix.*,bugprone-*,misc-*,performance-*,readability-*,-readability-isolate-declaration,-readability-else-after-return,-readability-braces-around-statements' Checks: 'clang-diagnostic-*,clang-analyzer-*,clang-diagnostic-*,clang-analyzer-*,clang-diagnostic-*,clang-analyzer-*,-*,clang-analyzer-core.*,clang-analyzer-optin.performance.*,clang-analyzer-optin.portability.*,clang-analyzer-nullability.*,clang-analyzer-security.*,clang-analyzer-unix.*,bugprone-*,misc-*,performance-*,readability-*,-*,clang-analyzer-core.*,clang-analyzer-optin.performance.*,clang-analyzer-optin.portability.*,clang-analyzer-nullability.*,clang-analyzer-security.*,clang-analyzer-unix.*,bugprone-*,misc-*,performance-*,readability-*,-readability-isolate-declaration,-readability-else-after-return,-readability-braces-around-statements,-*,clang-analyzer-core.*,clang-analyzer-optin.performance.*,clang-analyzer-optin.portability.*,clang-analyzer-nullability.*,clang-analyzer-security.*,clang-analyzer-unix.*,bugprone-*,misc-*,performance-*,readability-*,-readability-isolate-declaration,-readability-else-after-return,-readability-braces-around-statements,-readability-magic-numbers'
WarningsAsErrors: '' WarningsAsErrors: ''
HeaderFilterRegex: '' HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false AnalyzeTemporaryDtors: false
FormatStyle: none FormatStyle: file
User: caskd User: caskd
CheckOptions: CheckOptions:
- key: bugprone-argument-comment.CommentBoolLiterals - key: bugprone-argument-comment.CommentBoolLiterals
@ -158,10 +158,6 @@ CheckOptions:
value: '1' value: '1'
- key: readability-inconsistent-declaration-parameter-name.Strict - key: readability-inconsistent-declaration-parameter-name.Strict
value: '0' value: '0'
- key: readability-magic-numbers.IgnoredFloatingPointValues
value: '1.0;100.0;'
- key: readability-magic-numbers.IgnoredIntegerValues
value: '1;2;3;4;'
- key: readability-redundant-member-init.IgnoreBaseInCopyConstructors - key: readability-redundant-member-init.IgnoreBaseInCopyConstructors
value: '0' value: '0'
- key: readability-redundant-smartptr-get.IgnoreMacros - key: readability-redundant-smartptr-get.IgnoreMacros

View File

@ -1,19 +1,26 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(microirc LANGUAGES C) project(microirc LANGUAGES C)
# NOTE: Do these seem too annoying? Try writing good code then.
# Code that triggers these warnings will not be accepted unless it has a good reason to trigger them.
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wall -Wextra -Werror -Wformat-overflow=2 -Wformat-security -Winit-self -Wstrict-overflow=2 -Wstringop-overflow=2 -Walloc-zero -Wduplicated-branches -Wduplicated-cond -Wtrampolines -Wfloat-equal -Wshadow -Wunsafe-loop-optimizations -Wparentheses -pedantic -fanalyzer -fstack-check)
elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang")
add_compile_options(-Weverything -Wno-padded -Wno-disabled-macro-expansion -pedantic)
endif()
OPTION(CMAKE_BUILD_TYPE "Debug")
OPTION(BUILD_TESTS "Build tests for ctest" OFF) OPTION(BUILD_TESTS "Build tests for ctest" OFF)
OPTION(BUILD_IRCV3 "Build IRCv3 components" ON) OPTION(BUILD_IRCV3 "Build IRCv3 components" ON)
OPTION(BUILD_HELPERS "Build message helpers" ON) OPTION(BUILD_HELPERS "Build message helpers" ON)
OPTION(BUILD_VALIDATORS "Build message validators" ON) OPTION(BUILD_VALIDATORS "Build message validators" ON)
OPTION(CODE_ANALYZER "Analyze the code statically" ON)
OPTION(CODE_COVERAGE "Build with coverage tools" OFF)
# NOTE: Do these seem too annoying? Try writing good code then.
# Code that triggers these warnings will not be accepted unless it has a good reason to trigger them.
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wall -Wextra -Werror -Wformat-overflow=2 -Wformat-security -Winit-self -Wstrict-overflow=2 -Wstringop-overflow=2 -Walloc-zero -Wduplicated-branches -Wduplicated-cond -Wtrampolines -Wfloat-equal -Wshadow -Wunsafe-loop-optimizations -Wparentheses -pedantic -fstack-check)
if ( CODE_ANALYZER )
add_compile_options(-fanalyzer)
endif()
elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang")
add_compile_options(-Weverything -Wno-padded -Wno-disabled-macro-expansion -pedantic)
if ( CODE_COVERAGE )
add_compile_options(-fprofile-instr-generate -fcoverage-mapping)
endif()
endif()
if ( BUILD_IRCV3 ) if ( BUILD_IRCV3 )
message(STATUS "IRCv3 capabilities are going to be built.") message(STATUS "IRCv3 capabilities are going to be built.")

View File

@ -15,25 +15,27 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with uIRC. If not, see <https://www.gnu.org/licenses/>. * along with uIRC. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "types.h" #include "types.h"
#include <stdbool.h> #include <stdbool.h>
#include <sys/types.h> #include <sys/types.h>
#ifndef UIRC_INCLUDED_FUNCT #ifndef UIRC_GUARD_FUNCTIONS
#define UIRC_INCLUDED_FUNCT #define UIRC_GUARD_FUNCTIONS
/* Tokenizers: They take a string in and point their struct element pointers at tokens and end tokens with '\0' */ /* Tokenizers: They take a string in and point their struct element pointers at tokens and end tokens with '\0' */
extern signed int Tok_mesg(char* str, IRC_Message* out); extern signed int Tok_mesg(char* str, IRC_Message* out);
extern signed int Tok_user(char* str, IRC_User* out, bool useorig); extern signed int Tok_user(char* str, IRC_User* out, bool useorig);
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
extern signed int Tok_tags(char* str, IRC_Tags* out); extern signed int Tok_tags(char* str, IRC_Tags* out);
#endif #endif /* UIRC_IRCV3 */
/* Assemblers: They return the amount of bytes written and write directly at buf */ /* Assemblers: They return the amount of bytes written and write directly at buf */
extern signed long Assm_mesg(char* buf, IRC_Message* in, size_t len); extern signed long Assm_mesg(char* buf, IRC_Message* in, size_t len);
extern signed long Assm_user(char* buf, IRC_User* in, size_t len, bool useorig); extern signed long Assm_user(char* buf, IRC_User* in, size_t len, bool useorig);
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
extern signed long Assm_tags(char* buf, IRC_Tags* in, size_t len); extern signed long Assm_tags(char* buf, IRC_Tags* in, size_t len);
#endif #endif /* UIRC_IRCV3 */
/* Validators: They check that the parsed message is valid and follows the standard */ /* Validators: They check that the parsed message is valid and follows the standard */
#ifdef UIRC_VALIDATORS #ifdef UIRC_VALIDATORS
@ -42,9 +44,9 @@ extern signed int Val_channame(char* chan);
extern signed int Val_type_nocrlf(char* str); extern signed int Val_type_nocrlf(char* str);
extern signed int Val_type_nospcl(char* str); extern signed int Val_type_nospcl(char* str);
extern signed int Val_type_noblcm(char* str); extern signed int Val_type_noblcm(char* str);
#endif #endif /* UIRC_VALIDATORS */
/* Converters: They convert from one format to another */ /* Converters: They convert from one format to another */
extern signed int Ircmd_stoi(char* str); extern signed int Ircmd_stoi(char* str);
#endif #endif /* UIRC_GUARD_FUNCTIONS */

View File

@ -15,63 +15,66 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with uIRC. If not, see <https://www.gnu.org/licenses/>. * along with uIRC. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "types.h" #include "types.h"
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
#ifdef UIRC_HELPERS #ifdef UIRC_HELPERS
#ifndef UIRC_INCLUDED_HELPERS #ifndef UIRC_GUARD_HELPERS
#define UIRC_INCLUDED_HELPERS #define UIRC_GUARD_HELPERS
extern IRC_Message* Assm_AUTO(int cmd, bool trailing, char** args, int req); extern IRC_Message* Assm_AUTO(int cmd, bool trailing, char** args, int req);
#define Assm_cmd_REHASH() Assm_AUTO(REHASH, false, (char*[]){NULL}, 0) #define Assm_cmd_REHASH() Assm_AUTO(REHASH, false, (char*[]) { NULL }, 0)
#define Assm_cmd_DIE() Assm_AUTO(DIE, false, (char*[]){NULL}, 0) #define Assm_cmd_DIE() Assm_AUTO(DIE, false, (char*[]) { NULL }, 0)
#define Assm_cmd_RESTART() Assm_AUTO(RESTART, false, (char*[]){NULL}, 0) #define Assm_cmd_RESTART() Assm_AUTO(RESTART, false, (char*[]) { NULL }, 0)
#define Assm_cmd_QUIT(message) Assm_AUTO(QUIT, true, (char*[]){message, NULL}, 0) #define Assm_cmd_QUIT(message) Assm_AUTO(QUIT, true, (char*[]) { message, NULL }, 0)
#define Assm_cmd_MOTD(target) Assm_AUTO(MOTD, false, (char*[]){target, NULL}, 0) #define Assm_cmd_MOTD(target) Assm_AUTO(MOTD, false, (char*[]) { target, NULL }, 0)
#define Assm_cmd_VERSION(target) Assm_AUTO(VERSION, false, (char*[]){target, NULL}, 0) #define Assm_cmd_VERSION(target) Assm_AUTO(VERSION, false, (char*[]) { target, NULL }, 0)
#define Assm_cmd_TIME(target) Assm_AUTO(TIME, false, (char*[]){target, NULL}, 0) #define Assm_cmd_TIME(target) Assm_AUTO(TIME, false, (char*[]) { target, NULL }, 0)
#define Assm_cmd_TRACE(target) Assm_AUTO(TRACE, false, (char*[]){target, NULL}, 0) #define Assm_cmd_TRACE(target) Assm_AUTO(TRACE, false, (char*[]) { target, NULL }, 0)
#define Assm_cmd_ADMIN(target) Assm_AUTO(ADMIN, false, (char*[]){target, NULL}, 0) #define Assm_cmd_ADMIN(target) Assm_AUTO(ADMIN, false, (char*[]) { target, NULL }, 0)
#define Assm_cmd_INFO(target) Assm_AUTO(INFO, false, (char*[]){target, NULL}, 0) #define Assm_cmd_INFO(target) Assm_AUTO(INFO, false, (char*[]) { target, NULL }, 0)
#define Assm_cmd_AWAY(message) Assm_AUTO(AWAY, false, (char*[]){message, NULL}, 0) #define Assm_cmd_AWAY(message) Assm_AUTO(AWAY, false, (char*[]) { message, NULL }, 0)
#define Assm_cmd_PASS(password) Assm_AUTO(PASS, true, (char*[]){password, NULL}, 1) #define Assm_cmd_PASS(password) Assm_AUTO(PASS, true, (char*[]) { password, NULL }, 1)
#define Assm_cmd_ERROR(message) Assm_AUTO(ERROR, true, (char*[]){message, NULL}, 1) #define Assm_cmd_ERROR(message) Assm_AUTO(ERROR, true, (char*[]) { message, NULL }, 1)
#define Assm_cmd_WALLOPS(text) Assm_AUTO(WALLOPS, true, (char*[]){text, NULL}, 1) #define Assm_cmd_WALLOPS(text) Assm_AUTO(WALLOPS, true, (char*[]) { text, NULL }, 1)
#define Assm_cmd_NICK(nickname) Assm_AUTO(NICK, false, (char*[]){nickname, NULL}, 1) #define Assm_cmd_NICK(nickname) Assm_AUTO(NICK, false, (char*[]) { nickname, NULL }, 1)
#define Assm_cmd_USERS(target) Assm_AUTO(USERS, false, (char*[]){target, NULL}, 1) #define Assm_cmd_USERS(target) Assm_AUTO(USERS, false, (char*[]) { target, NULL }, 1)
#define Assm_cmd_NAMES(channels, target) Assm_AUTO(NAMES, false, (char*[]){channels, target, NULL}, 0) #define Assm_cmd_NAMES(channels, target) Assm_AUTO(NAMES, false, (char*[]) { channels, target, NULL }, 0)
#define Assm_cmd_LIST(channels, target) Assm_AUTO(LIST, false, (char*[]){channels, target, NULL}, 0) #define Assm_cmd_LIST(channels, target) Assm_AUTO(LIST, false, (char*[]) { channels, target, NULL }, 0)
#define Assm_cmd_LUSERS(mask, target) Assm_AUTO(LUSERS, false, (char*[]){mask, target, NULL}, 0) #define Assm_cmd_LUSERS(mask, target) Assm_AUTO(LUSERS, false, (char*[]) { mask, target, NULL }, 0)
#define Assm_cmd_STATS(query, target) Assm_AUTO(STATS, false, (char*[]){query, target, NULL}, 0) #define Assm_cmd_STATS(query, target) Assm_AUTO(STATS, false, (char*[]) { query, target, NULL }, 0)
#define Assm_cmd_SERVLIST(mask, type) Assm_AUTO(SERVLIST, false, (char*[]){mask, type, NULL}, 0) #define Assm_cmd_SERVLIST(mask, type) Assm_AUTO(SERVLIST, false, (char*[]) { mask, type, NULL }, 0)
#define Assm_cmd_JOIN(channels, keys) Assm_AUTO(JOIN, false, (char*[]){channels, keys, NULL}, 1) #define Assm_cmd_JOIN(channels, keys) Assm_AUTO(JOIN, false, (char*[]) { channels, keys, NULL }, 1)
#define Assm_cmd_PART(channel, message) Assm_AUTO(PART, false, (char*[]){channel, message, NULL}, 1) #define Assm_cmd_PART(channel, message) Assm_AUTO(PART, false, (char*[]) { channel, message, NULL }, 1)
/* NOTE: Use a non-NULL address (pointing at a "\0") as the topic to clear it and use a NULL address to check it /* NOTE: Use a non-NULL address (pointing at a "\0") as the topic to clear it and use a NULL address to check it
* Blame the protocol, not this >:C */ * Blame the protocol, not this >:C */
#define Assm_cmd_TOPIC(channel, topic) Assm_AUTO(TOPIC, true, (char*[]){channel, topic, NULL}, 1) #define Assm_cmd_TOPIC(channel, topic) Assm_AUTO(TOPIC, true, (char*[]) { channel, topic, NULL }, 1)
#define Assm_cmd_PONG(source, target) Assm_AUTO(PONG, true, (char*[]){source, target, NULL}, 1) #define Assm_cmd_PONG(source, target) Assm_AUTO(PONG, true, (char*[]) { source, target, NULL }, 1)
#define Assm_cmd_OPER(name, password) Assm_AUTO(OPER, true, (char*[]){name, password, NULL}, 2) #define Assm_cmd_OPER(name, password) Assm_AUTO(OPER, true, (char*[]) { name, password, NULL }, 2)
#define Assm_cmd_SQUIT(server, comment) Assm_AUTO(SQUIT, true, (char*[]){server, comment, NULL}, 2) #define Assm_cmd_SQUIT(server, comment) Assm_AUTO(SQUIT, true, (char*[]) { server, comment, NULL }, 2)
#define Assm_cmd_PRIVMSG(target, message) Assm_AUTO(PRIVMSG, true, (char*[]){target, message, NULL}, 2) #define Assm_cmd_PRIVMSG(target, message) Assm_AUTO(PRIVMSG, true, (char*[]) { target, message, NULL }, 2)
#define Assm_cmd_NOTICE(target, text) Assm_AUTO(NOTICE, true, (char*[]){target, text, NULL}, 2) #define Assm_cmd_NOTICE(target, text) Assm_AUTO(NOTICE, true, (char*[]) { target, text, NULL }, 2)
#define Assm_cmd_SQUERY(servicename, text) Assm_AUTO(SQUERY, true, (char*[]){servicename, text, NULL}, 2) #define Assm_cmd_SQUERY(servicename, text) Assm_AUTO(SQUERY, true, (char*[]) { servicename, text, NULL }, 2)
#define Assm_cmd_KILL(nick, comment) Assm_AUTO(KILL, true, (char*[]){nick, comment, NULL}, 2) #define Assm_cmd_KILL(nick, comment) Assm_AUTO(KILL, true, (char*[]) { nick, comment, NULL }, 2)
#define Assm_cmd_INVITE(nick, channel) Assm_AUTO(INVITE, false, (char*[]){nick, channel, NULL}, 2) #define Assm_cmd_INVITE(nick, channel) Assm_AUTO(INVITE, false, (char*[]) { nick, channel, NULL }, 2)
#define Assm_cmd_MODE(nickname, modes, modeparams) Assm_AUTO(MODE, false, (char*[]){nickname, modes, modeparams, NULL}, 1) #define Assm_cmd_MODE(nickname, modes, modeparams) Assm_AUTO(MODE, false, (char*[]) { nickname, modes, modeparams, NULL }, 1)
#define Assm_cmd_KICK(channels, users, comment) Assm_AUTO(KICK, true, (char*[]){channels, users, comment, NULL}, 2) #define Assm_cmd_KICK(channels, users, comment) Assm_AUTO(KICK, true, (char*[]) { channels, users, comment, NULL }, 2)
#define Assm_cmd_CONNECT(target, port, remote) Assm_AUTO(CONNECT, false, (char*[]){target, port, remote, NULL}, 2) #define Assm_cmd_CONNECT(target, port, remote) Assm_AUTO(CONNECT, false, (char*[]) { target, port, remote, NULL }, 2)
#define Assm_cmd_SERVICE(nickname, distribution, type, info) \ #define Assm_cmd_SERVICE(nickname, distribution, type, info) \
Assm_AUTO(SERVICE, true, (char*[]){nickname, RESERVED, distribution, "0", RESERVED, info, NULL}, 6) Assm_AUTO(SERVICE, true, (char*[]) { nickname, RESERVED, distribution, "0", RESERVED, info, NULL }, 6)
extern IRC_Message* Assm_cmd_USER(char* user, char* realname, int modes); extern IRC_Message* Assm_cmd_USER(char* user, char* realname, int modes);
extern IRC_Message* Assm_cmd_LINKS(char* remoteserv, char* servmask); extern IRC_Message* Assm_cmd_LINKS(char* remoteserv, char* servmask);
@ -88,17 +91,18 @@ extern void Tok_FArgOpt(IRC_Message* mesg, char** optarg, char** reqarg);
extern int Tok_CAPS(char* caps); extern int Tok_CAPS(char* caps);
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
#define Assm_cmd_CAP_END() Assm_AUTO(CAP, false, (char*[]){"END", NULL}, 0) #define Assm_cmd_CAP_END() Assm_AUTO(CAP, false, (char*[]) { "END", NULL }, 0)
#define Assm_cmd_CAP_LIST() Assm_AUTO(CAP, false, (char*[]){"LIST", NULL}, 0) #define Assm_cmd_CAP_LIST() Assm_AUTO(CAP, false, (char*[]) { "LIST", NULL }, 0)
#define Assm_cmd_CAP_LS(version) Assm_AUTO(CAP, false, (char*[]){"LS", version, NULL}, 0) #define Assm_cmd_CAP_LS(version) Assm_AUTO(CAP, false, (char*[]) { "LS", version, NULL }, 0)
#define Assm_cmd_CAP_REQ(caps) Assm_AUTO(CAP, true, (char*[]){"REQ", caps, NULL}, 1) #define Assm_cmd_CAP_REQ(caps) Assm_AUTO(CAP, true, (char*[]) { "REQ", caps, NULL }, 1)
#define Assm_cmd_CAP_NEW(nick, caps) Assm_AUTO(CAP, true, (char*[]){"NEW", nick, caps, NULL}, 2) #define Assm_cmd_CAP_NEW(nick, caps) Assm_AUTO(CAP, true, (char*[]) { "NEW", nick, caps, NULL }, 2)
#define Assm_cmd_CAP_DEL(nick, caps) Assm_AUTO(CAP, true, (char*[]){"DEL", nick, caps, NULL}, 2) #define Assm_cmd_CAP_DEL(nick, caps) Assm_AUTO(CAP, true, (char*[]) { "DEL", nick, caps, NULL }, 2)
#endif #endif /* UIRC_IRCV3 */
extern size_t Assm_tag_timestamp(char* buf, size_t len, time_t time); extern size_t Assm_tag_timestamp(char* buf, size_t len, time_t time);
#endif
#endif #endif /* UIRC_GUARD_HELPERS */
#endif /* UIRC_HELPERS */

View File

@ -16,8 +16,8 @@
* along with uIRC. If not, see <https://www.gnu.org/licenses/>. * along with uIRC. If not, see <https://www.gnu.org/licenses/>.
*/ */
#ifndef UIRC_INCLUDED_MAPS #ifndef UIRC_GUARD_MAPS
#define UIRC_INCLUDED_MAPS #define UIRC_GUARD_MAPS
#define ERR_UIRC_GENERIC -1 #define ERR_UIRC_GENERIC -1
#define ERR_UIRC_NULL_ARGS -2 #define ERR_UIRC_NULL_ARGS -2
@ -53,7 +53,7 @@
#define CAP_TLS 19 #define CAP_TLS 19
#define CAP_USERHOST_IN_NAMES 20 #define CAP_USERHOST_IN_NAMES 20
#define CAPBIT(cap) (1 << cap) #define CAPBIT(cap) (1 << cap)
#endif #endif /* UIRC_IRCV3 */
#define UIRC_FCMD ADMIN #define UIRC_FCMD ADMIN
enum commands { enum commands {
@ -121,10 +121,10 @@ enum commands {
WEBIRC, /* https://ircv3.net/specs/extensions/webirc */ WEBIRC, /* https://ircv3.net/specs/extensions/webirc */
#define UIRC_LCMD WEBIRC #define UIRC_LCMD WEBIRC
#else #else /* UIRC_IRCV3 */
#define UIRC_LCMD WHOWAS #define UIRC_LCMD WHOWAS
#endif #endif /* UIRC_IRCV3 */
}; };
#define RPL_WELCOME 1 #define RPL_WELCOME 1
#define RPL_YOURHOST 2 #define RPL_YOURHOST 2
@ -141,7 +141,7 @@ enum commands {
* Read the above and go speak with them, or just don't use IRCv3 :) * Read the above and go speak with them, or just don't use IRCv3 :)
*/ */
#define RPL_ISUPPORT 5 #define RPL_ISUPPORT 5
#endif #endif /* UIRC_IRCV3 */
#define RPL_TRACELINK 200 #define RPL_TRACELINK 200
#define RPL_TRACECONNECTING 201 #define RPL_TRACECONNECTING 201
@ -283,12 +283,12 @@ enum commands {
#define ERR_SASLABORTED 906 #define ERR_SASLABORTED 906
#define ERR_SASLALREADY 907 #define ERR_SASLALREADY 907
#define ERR_SASLMECHS 908 #define ERR_SASLMECHS 908
#endif #endif /* UIRC_IRCV3 */
#endif
extern const char* const IRC_Cmds[UIRC_LCMD + UIRC_FCMD]; extern const char* const IRC_Cmds[UIRC_LCMD + UIRC_FCMD];
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
extern const char* const IRC_v3_Caps[CAP_USERHOST_IN_NAMES + 1]; extern const char* const IRC_v3_Caps[CAP_USERHOST_IN_NAMES + 1];
#endif #endif /* UIRC_IRCV3 */
#endif /* UIRC_GUARD_MAPS */

View File

@ -17,8 +17,8 @@
*/ */
#include <stdbool.h> #include <stdbool.h>
#ifndef UIRC_INCLUDED_TYPES #ifndef UIRC_GUARD_TYPES
#define UIRC_INCLUDED_TYPES #define UIRC_GUARD_TYPES
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
typedef struct { typedef struct {
@ -28,24 +28,12 @@ typedef struct {
typedef struct { typedef struct {
/* See https://ircv3.net/registry#tags for more information */ /* See https://ircv3.net/registry#tags for more information */
IRC_Tag account; IRC_Tag account, batch, label, msgid, multiline_concat, time, typing, react, reply;
IRC_Tag batch;
IRC_Tag label;
IRC_Tag msgid;
IRC_Tag multiline_concat;
IRC_Tag time;
IRC_Tag typing;
IRC_Tag react;
IRC_Tag reply;
} IRC_Tags; } IRC_Tags;
#endif #endif /* UIRC_IRCV3 */
typedef struct { typedef struct {
char* nick; char* nick, *user, *host, *orig, *real;
char* user;
char* host;
char* orig;
char* real;
} IRC_User; } IRC_User;
/* This is how a full user source would look like /* This is how a full user source would look like
* NOTE: 'real (Real name)' may only be used in special contexts other than communication. * NOTE: 'real (Real name)' may only be used in special contexts other than communication.
@ -56,11 +44,12 @@ typedef unsigned short IRC_Command;
typedef struct { typedef struct {
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
IRC_Tags tags; IRC_Tags tags;
#endif #endif /* UIRC_IRCV3 */
IRC_User name; IRC_User name;
char* args[16]; /* 0-13 + trailing + NULL */ char* args[16]; /* 0-13 + trailing + NULL */
bool trailing; /* Tells if the last argument is trailing */ bool trailing; /* Tells if the last argument is trailing */
IRC_Command cmd; IRC_Command cmd;
} IRC_Message; } IRC_Message;
#endif
#endif /* UIRC_GUARD_TYPES */

View File

@ -20,9 +20,11 @@
#include "helpers.h" #include "helpers.h"
#include "mappings.h" #include "mappings.h"
#include "types.h" #include "types.h"
#ifndef UIRC_INCLUDED
#define UIRC_INCLUDED
#define UIRC_VERSION 20203110 #ifndef UIRC_GUARD_UIRC
#endif #define UIRC_GUARD_UIRC
#define UIRC_VERSION 20201216
#endif /* UIRC_GUARD_UIRC */

View File

@ -17,104 +17,103 @@
*/ */
#include "assemblers.h" #include "assemblers.h"
signed long Assm_mesg(char* buf, IRC_Message* in, size_t len) signed long
Assm_mesg(char* buf, IRC_Message* in, size_t len)
{ {
if (buf == NULL || in == NULL) return ERR_UIRC_BUFFER_ERR; if (buf == NULL || in == NULL) return ERR_UIRC_BUFFER_ERR;
char* pos = buf; char* pos = buf;
signed long cnt, ret; signed long cnt, ret;
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
if ((ret = Assm_tags(pos, &in->tags, len - (unsigned long)(pos - buf))) < 0) if ((ret = Assm_tags(pos, &in->tags, len - (unsigned long) (pos - buf))) < 0) return ret;
return ret;
else if (ret != 0) { else if (ret != 0) {
pos += ret; pos += ret;
if (!safe_charcpy(&pos, ' ', len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_charcpy(&pos, ' ', len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
} }
#endif #endif /* UIRC_IRCV3 */
if (in->name.nick != NULL || in->name.host != NULL) { if (in->name.nick != NULL || in->name.host != NULL) {
if (!safe_charcpy(&pos, ':', len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_charcpy(&pos, ':', len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
if ((ret = Assm_user(pos, &in->name, len - (unsigned long)(pos - buf), false)) <= 0) if ((ret = Assm_user(pos, &in->name, len - (unsigned long) (pos - buf), false)) <= 0) return ret;
return ret;
else else
pos += ret; pos += ret;
if (!safe_charcpy(&pos, ' ', len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_charcpy(&pos, ' ', len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
} }
if (in->cmd < UIRC_FCMD || in->cmd > UIRC_LCMD) { if (in->cmd < UIRC_FCMD || in->cmd > UIRC_LCMD) {
if ((cnt = snprintf(pos, 4, "%.3i", in->cmd)) == 3) if ((cnt = snprintf(pos, 4, "%.3i", in->cmd)) == 3) pos += cnt;
pos += cnt;
else else
return ERR_UIRC_UNKNOWN_TOKEN; return ERR_UIRC_UNKNOWN_TOKEN;
} else { } else {
if (IRC_Cmds[in->cmd] != NULL) { if (IRC_Cmds[in->cmd] != NULL) {
size_t cmdlen = strlen(IRC_Cmds[in->cmd]); size_t cmdlen = strlen(IRC_Cmds[in->cmd]);
if (len - (unsigned long)(pos - buf) > cmdlen && strcpy(pos, IRC_Cmds[in->cmd]) != NULL) if (len - (unsigned long) (pos - buf) > cmdlen && strcpy(pos, IRC_Cmds[in->cmd]) != NULL) pos += cmdlen;
pos += cmdlen;
else else
return ERR_UIRC_UNKNOWN_TOKEN; return ERR_UIRC_UNKNOWN_TOKEN;
} }
} }
for (unsigned int i = 0; in->args[i] != NULL; i++) { for (unsigned int i = 0; in->args[i] != NULL; i++) {
if (len - (unsigned long)(pos - buf) > strlen(in->args[i]) + 2 if (len - (unsigned long) (pos - buf) > strlen(in->args[i]) + 2
&& (cnt = && (cnt =
snprintf(pos, len - (unsigned long)(pos - buf), (in->args[i + 1] == NULL && in->trailing) ? " :%s" : " %s", in->args[i])) snprintf(pos, len - (unsigned long) (pos - buf), (in->args[i + 1] == NULL && in->trailing) ? " :%s" : " %s", in->args[i]))
> 0) > 0)
pos += cnt; pos += cnt;
else else
return ERR_UIRC_BUFFER_ERR; return ERR_UIRC_BUFFER_ERR;
} }
if (!safe_strcpy(&pos, "\r\n", len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_strcpy(&pos, "\r\n", len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
return pos - buf; return pos - buf;
} }
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
signed long Assm_tags(char* buf, IRC_Tags* in, size_t len) signed long
Assm_tags(char* buf, IRC_Tags* in, size_t len)
{ {
if (buf == NULL || in == NULL) return ERR_UIRC_NULL_ARGS; if (buf == NULL || in == NULL) return ERR_UIRC_NULL_ARGS;
char* pos = buf; char* pos = buf;
struct tagmapping tagmps[] = {{.name = "time", .assg = &in->time}, {.name = "account", .assg = &in->account}, struct tagmapping tagmps[] = { { .name = "time", .assg = &in->time }, { .name = "account", .assg = &in->account },
{.name = "batch", .assg = &in->batch}, {.name = "label", .assg = &in->label}, { .name = "batch", .assg = &in->batch }, { .name = "label", .assg = &in->label },
{.name = "msgid", .assg = &in->msgid}, {.name = "multiline-concat", .assg = &in->multiline_concat}, { .name = "msgid", .assg = &in->msgid }, { .name = "multiline-concat", .assg = &in->multiline_concat },
{.name = "typing", .assg = &in->typing}, {.name = "react", .assg = &in->react}, { .name = "typing", .assg = &in->typing }, { .name = "react", .assg = &in->react },
{.name = "reply", .assg = &in->reply}}; { .name = "reply", .assg = &in->reply } };
for (unsigned int i = 0; i < sizeof(tagmps) / sizeof(struct tagmapping); i++) { for (unsigned int i = 0; i < sizeof(tagmps) / sizeof(struct tagmapping); i++) {
if ((*tagmps[i].assg).value != NULL) { if ((*tagmps[i].assg).value != NULL) {
if (pos == buf) { if (pos == buf) {
if (!safe_charcpy(&pos, '@', len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_charcpy(&pos, '@', len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
} else { } else {
if (!safe_charcpy(&pos, ';', len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_charcpy(&pos, ';', len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
} }
if ((*tagmps[i].assg).clientbound) { if ((*tagmps[i].assg).clientbound) {
if (!safe_charcpy(&pos, '+', len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_charcpy(&pos, '+', len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
} }
if (!safe_strcpy(&pos, tagmps[i].name, len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_strcpy(&pos, tagmps[i].name, len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
if (*(*tagmps[i].assg).value != '\0') { if (*(*tagmps[i].assg).value != '\0') {
if (!safe_charcpy(&pos, '=', len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_charcpy(&pos, '=', len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
if (!safe_strcpy(&pos, (*tagmps[i].assg).value, len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_strcpy(&pos, (*tagmps[i].assg).value, len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
} }
} }
} }
return pos - buf; return pos - buf;
} }
#endif #endif /* UIRC_IRCV3*/
signed long Assm_user(char* buf, IRC_User* in, size_t len, bool useorig) signed long
Assm_user(char* buf, IRC_User* in, size_t len, bool useorig)
{ {
if (buf == NULL || in == NULL) return ERR_UIRC_NULL_ARGS; if (buf == NULL || in == NULL) return ERR_UIRC_NULL_ARGS;
char* pos = buf; char* pos = buf;
if (in->nick == NULL && in->host != NULL) { if (in->nick == NULL && in->host != NULL) {
if (!safe_strcpy(&pos, in->host, len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_strcpy(&pos, in->host, len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
} else if (in->nick != NULL) { } else if (in->nick != NULL) {
if (!safe_strcpy(&pos, in->nick, len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_strcpy(&pos, in->nick, len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
if (in->user != NULL) { if (in->user != NULL) {
if (!safe_charcpy(&pos, '!', len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_charcpy(&pos, '!', len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
if (!safe_strcpy(&pos, in->user, len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_strcpy(&pos, in->user, len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
} }
if (useorig && in->orig != NULL) { if (useorig && in->orig != NULL) {
if (!safe_charcpy(&pos, '%', len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_charcpy(&pos, '%', len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
if (!safe_strcpy(&pos, in->orig, len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_strcpy(&pos, in->orig, len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
} }
if (in->host != NULL) { if (in->host != NULL) {
if (!safe_charcpy(&pos, '@', len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_charcpy(&pos, '@', len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
if (!safe_strcpy(&pos, in->host, len - (unsigned long)(pos - buf))) return ERR_UIRC_BUFFER_ERR; if (!safe_strcpy(&pos, in->host, len - (unsigned long) (pos - buf))) return ERR_UIRC_BUFFER_ERR;
} }
} else } else
return ERR_UIRC_NULL_ARGS; return ERR_UIRC_NULL_ARGS;

View File

@ -19,18 +19,19 @@
#include "../include/mappings.h" #include "../include/mappings.h"
#include "../include/types.h" #include "../include/types.h"
#include "misc.h" #include "misc.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#ifndef UIRC_INCLUDED_PRIVATE_ASSM #ifndef UIRC_GUARD_PRIVATE_ASSEMBLERS
#define UIRC_INCLUDED_PRIVATE_ASSM #define UIRC_GUARD_PRIVATE_ASSEMBLERS
signed long Assm_mesg(char* buf, IRC_Message* in, size_t len); signed long Assm_mesg(char* buf, IRC_Message* in, size_t len);
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
signed long Assm_tags(char* buf, IRC_Tags* in, size_t len); signed long Assm_tags(char* buf, IRC_Tags* in, size_t len);
#endif #endif /* UIRC_IRCV3 */
signed long Assm_user(char* buf, IRC_User* in, size_t len, bool useorig); signed long Assm_user(char* buf, IRC_User* in, size_t len, bool useorig);
#endif #endif /* UIRC_GUARD_PRIVATE_ASSEMBLERS */

View File

@ -20,8 +20,13 @@
static IRC_Message imassm_mesg; static IRC_Message imassm_mesg;
char* RESERVED = "*"; char* RESERVED = "*";
void clear_assm(void) { memset((void*)&imassm_mesg, '\0', sizeof(IRC_Message)); } void
IRC_Message* Assm_AUTO(IRC_Command cmd, bool trailing, char** args, int req) clear_assm(void)
{
memset((void*) &imassm_mesg, '\0', sizeof(IRC_Message));
}
IRC_Message*
Assm_AUTO(IRC_Command cmd, bool trailing, char** args, int req)
{ {
clear_assm(); clear_assm();
int i; int i;
@ -32,7 +37,8 @@ IRC_Message* Assm_AUTO(IRC_Command cmd, bool trailing, char** args, int req)
return &imassm_mesg; return &imassm_mesg;
} }
IRC_Message* Assm_cmd_USER(char* user, char* realname, int modes) IRC_Message*
Assm_cmd_USER(char* user, char* realname, int modes)
{ {
if (user == NULL || modes < 0 || modes > (MBMASK_INVIS | MBMASK_WALLOPS)) return NULL; if (user == NULL || modes < 0 || modes > (MBMASK_INVIS | MBMASK_WALLOPS)) return NULL;
clear_assm(); clear_assm();
@ -47,7 +53,8 @@ IRC_Message* Assm_cmd_USER(char* user, char* realname, int modes)
return &imassm_mesg; return &imassm_mesg;
} }
IRC_Message* Assm_cmd_LINKS(char* remoteserv, char* servmask) IRC_Message*
Assm_cmd_LINKS(char* remoteserv, char* servmask)
{ {
if (remoteserv != NULL && servmask == NULL) return NULL; if (remoteserv != NULL && servmask == NULL) return NULL;
clear_assm(); clear_assm();
@ -57,7 +64,8 @@ IRC_Message* Assm_cmd_LINKS(char* remoteserv, char* servmask)
return &imassm_mesg; return &imassm_mesg;
} }
IRC_Message* Assm_cmd_WHO(char* mask, bool oper) IRC_Message*
Assm_cmd_WHO(char* mask, bool oper)
{ {
static char* operator= "o"; static char* operator= "o";
if (oper && mask == NULL) return NULL; if (oper && mask == NULL) return NULL;
@ -68,7 +76,8 @@ IRC_Message* Assm_cmd_WHO(char* mask, bool oper)
return &imassm_mesg; return &imassm_mesg;
} }
IRC_Message* Assm_cmd_WHOIS(char* target, char* mask) IRC_Message*
Assm_cmd_WHOIS(char* target, char* mask)
{ {
if (mask == NULL) return NULL; if (mask == NULL) return NULL;
clear_assm(); clear_assm();
@ -78,7 +87,8 @@ IRC_Message* Assm_cmd_WHOIS(char* target, char* mask)
return &imassm_mesg; return &imassm_mesg;
} }
IRC_Message* Assm_cmd_WHOWAS(char* nick, char* count, char* target) IRC_Message*
Assm_cmd_WHOWAS(char* nick, char* count, char* target)
{ {
if (nick == NULL || (target != NULL && count == NULL)) return NULL; if (nick == NULL || (target != NULL && count == NULL)) return NULL;
clear_assm(); clear_assm();
@ -92,7 +102,8 @@ IRC_Message* Assm_cmd_WHOWAS(char* nick, char* count, char* target)
/* NOTE: This is what implementation you have to live with /* NOTE: This is what implementation you have to live with
* I would've just used the prefix to set the source but whatever * I would've just used the prefix to set the source but whatever
*/ */
IRC_Message* Assm_cmd_PING(char* source, char* target) IRC_Message*
Assm_cmd_PING(char* source, char* target)
{ {
if (source == NULL && target == NULL) return NULL; if (source == NULL && target == NULL) return NULL;
clear_assm(); clear_assm();
@ -103,7 +114,8 @@ IRC_Message* Assm_cmd_PING(char* source, char* target)
return &imassm_mesg; return &imassm_mesg;
} }
IRC_Message* Assm_cmd_SUMMON(char* user, char* target, char* channel) IRC_Message*
Assm_cmd_SUMMON(char* user, char* target, char* channel)
{ {
if (user == NULL || (channel != NULL && target == NULL)) return NULL; if (user == NULL || (channel != NULL && target == NULL)) return NULL;
clear_assm(); clear_assm();
@ -113,7 +125,8 @@ IRC_Message* Assm_cmd_SUMMON(char* user, char* target, char* channel)
imassm_mesg.cmd = SUMMON; imassm_mesg.cmd = SUMMON;
return &imassm_mesg; return &imassm_mesg;
} }
IRC_Message* Assm_cmd_USERHOST(char* users[]) IRC_Message*
Assm_cmd_USERHOST(char* users[])
{ {
if (users[0] == NULL) return NULL; if (users[0] == NULL) return NULL;
clear_assm(); clear_assm();
@ -122,7 +135,8 @@ IRC_Message* Assm_cmd_USERHOST(char* users[])
return &imassm_mesg; return &imassm_mesg;
} }
/* NOTE: Limited to 14 nicks per command */ /* NOTE: Limited to 14 nicks per command */
IRC_Message* Assm_cmd_ISON(char* users[]) IRC_Message*
Assm_cmd_ISON(char* users[])
{ {
if (users[0] == NULL) return NULL; if (users[0] == NULL) return NULL;
clear_assm(); clear_assm();
@ -131,7 +145,8 @@ IRC_Message* Assm_cmd_ISON(char* users[])
return &imassm_mesg; return &imassm_mesg;
} }
void Tok_cmd_PING(IRC_Message* mesg, char** source, char** target) void
Tok_cmd_PING(IRC_Message* mesg, char** source, char** target)
{ {
*source = (mesg->args[0] == NULL && mesg->trailing) ? mesg->args[2] : (mesg->args[1] != NULL) ? mesg->args[0] : NULL; *source = (mesg->args[0] == NULL && mesg->trailing) ? mesg->args[2] : (mesg->args[1] != NULL) ? mesg->args[0] : NULL;
*target = (mesg->args[1] == NULL) ? mesg->args[0] : mesg->args[1]; *target = (mesg->args[1] == NULL) ? mesg->args[0] : mesg->args[1];
@ -140,19 +155,21 @@ void Tok_cmd_PING(IRC_Message* mesg, char** source, char** target)
* (stands for first argument optional) * (stands for first argument optional)
* [ <optarg> ] <reqarg> * [ <optarg> ] <reqarg>
*/ */
void Tok_FArgOpt(IRC_Message* mesg, char** optarg, char** reqarg) void
Tok_FArgOpt(IRC_Message* mesg, char** optarg, char** reqarg)
{ {
*optarg = (mesg->args[1] != NULL) ? mesg->args[0] : NULL; *optarg = (mesg->args[1] != NULL) ? mesg->args[0] : NULL;
*reqarg = (mesg->args[1] != NULL) ? mesg->args[1] : mesg->args[0]; *reqarg = (mesg->args[1] != NULL) ? mesg->args[1] : mesg->args[0];
} }
int Tok_CAPS(char* caps) int
Tok_CAPS(char* caps)
{ {
int temp = 0; int temp = 0;
char* cur = NULL; char* cur = NULL;
if ((cur = strtok(caps, " ")) != NULL) { if ((cur = strtok(caps, " ")) != NULL) {
do { do {
for (int i = 1; (unsigned long)i < (sizeof(IRC_v3_Caps) / sizeof(*IRC_v3_Caps)); i++) { for (int i = 1; (unsigned long) i < (sizeof(IRC_v3_Caps) / sizeof(*IRC_v3_Caps)); i++) {
if (strcmp(IRC_v3_Caps[i], cur) == 0) { if (strcmp(IRC_v3_Caps[i], cur) == 0) {
temp |= CAPBIT(i); temp |= CAPBIT(i);
break; break;

View File

@ -18,14 +18,15 @@
#include "../include/mappings.h" #include "../include/mappings.h"
#include "../include/types.h" #include "../include/types.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#ifndef UIRC_INCLUDED_PRIVATE_HELPERS #ifndef UIRC_GUARD_PRIVATE_HELPERS
#define UIRC_INCLUDED_PRIVATE_HELPERS #define UIRC_GUARD_PRIVATE_HELPERS
extern char* RESERVED; extern char* RESERVED;
void clear_assm(void); void clear_assm(void);
@ -44,5 +45,5 @@ IRC_Message* Assm_cmd_ISON(char* users[]);
void Tok_cmd_PING(IRC_Message* mesg, char** source, char** target); void Tok_cmd_PING(IRC_Message* mesg, char** source, char** target);
void Tok_FArgOpt(IRC_Message* mesg, char** optarg, char** reqarg); void Tok_FArgOpt(IRC_Message* mesg, char** optarg, char** reqarg);
int Tok_CAPS(char* caps); int Tok_CAPS(char* caps);
#endif #endif /* UIRC_GUARD_PRIVATE_HELPERS */

View File

@ -17,7 +17,7 @@
*/ */
#include "misc.h" #include "misc.h"
const char* const IRC_Cmds[] = {[ADMIN] = "ADMIN", [AWAY] = "AWAY", [CONNECT] = "CONNECT", [DIE] = "DIE", const char* const IRC_Cmds[] = { [ADMIN] = "ADMIN", [AWAY] = "AWAY", [CONNECT] = "CONNECT", [DIE] = "DIE",
[ERROR] = "ERROR", [INFO] = "INFO", [INVITE] = "INVITE", [ISON] = "ISON", [ERROR] = "ERROR", [INFO] = "INFO", [INVITE] = "INVITE", [ISON] = "ISON",
[JOIN] = "JOIN", [KICK] = "KICK", [KILL] = "KILL", [LINKS] = "LINKS", [JOIN] = "JOIN", [KICK] = "KICK", [KILL] = "KILL", [LINKS] = "LINKS",
[LIST] = "LIST", [LUSERS] = "LUSERS", [MODE] = "MODE", [MOTD] = "MOTD", [LIST] = "LIST", [LUSERS] = "LUSERS", [MODE] = "MODE", [MOTD] = "MOTD",
@ -34,7 +34,7 @@ const char* const IRC_Cmds[] = {[ADMIN] = "ADMIN", [AWAY] = "AWAY", [C
[BATCH] = "BATCH", [CAP] = "CAP", [CHGHOST] = "CHGHOST", [FAIL] = "FAIL", [BATCH] = "BATCH", [CAP] = "CAP", [CHGHOST] = "CHGHOST", [FAIL] = "FAIL",
[MONITOR] = "MONITOR", [NOTE] = "NOTE", [RENAME] = "RENAME", [RESUME] = "RESUME", [MONITOR] = "MONITOR", [NOTE] = "NOTE", [RENAME] = "RENAME", [RESUME] = "RESUME",
[SETNAME] = "SETNAME", [WARN] = "WARN", [WEBIRC] = "WEBIRC" [SETNAME] = "SETNAME", [WARN] = "WARN", [WEBIRC] = "WEBIRC"
#endif #endif /* UIRC_IRCV3 */
}; };
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
@ -60,18 +60,20 @@ const char* const IRC_v3_Caps[] = {
[CAP_TLS] = "tls", [CAP_TLS] = "tls",
[CAP_USERHOST_IN_NAMES] = "userhost-in-names", [CAP_USERHOST_IN_NAMES] = "userhost-in-names",
}; };
#endif #endif /* UIRC_IRCV3 */
signed short Ircmd_stoi(char* str) signed short
Ircmd_stoi(char* str)
{ {
if (str == NULL) return ERR_UIRC_NULL_ARGS; if (str == NULL) return ERR_UIRC_NULL_ARGS;
for (signed short i = UIRC_FCMD; i <= (signed short)UIRC_LCMD; i++) { for (signed short i = UIRC_FCMD; i <= (signed short) UIRC_LCMD; i++) {
if (IRC_Cmds[i] != NULL && strcmp(IRC_Cmds[i], str) == 0) return i; if (IRC_Cmds[i] != NULL && strcmp(IRC_Cmds[i], str) == 0) return i;
} }
return ERR_UIRC_UNKNOWN_TOKEN; return ERR_UIRC_UNKNOWN_TOKEN;
} }
size_t safe_strcpy(char** dest, const char* src, size_t lef) size_t
safe_strcpy(char** dest, const char* src, size_t lef)
{ {
size_t cnt; size_t cnt;
if (lef > (cnt = strlen(src)) + 1) { if (lef > (cnt = strlen(src)) + 1) {
@ -82,7 +84,8 @@ size_t safe_strcpy(char** dest, const char* src, size_t lef)
return 0; return 0;
} }
bool safe_charcpy(char** dest, const char c, size_t lef) bool
safe_charcpy(char** dest, const char c, size_t lef)
{ {
if (lef > 1) { if (lef > 1) {
*(*dest)++ = c; *(*dest)++ = c;
@ -92,13 +95,15 @@ bool safe_charcpy(char** dest, const char c, size_t lef)
return 0; return 0;
} }
void skip_spaces(char** addr) void
skip_spaces(char** addr)
{ {
for (; **addr == ' '; (*addr)++) for (; **addr == ' '; (*addr)++)
; ;
} }
char* strtok_mr(char** addr, const char* tokens) char*
strtok_mr(char** addr, const char* tokens)
{ {
if (addr == NULL || *addr == NULL || !**addr || tokens == NULL) return NULL; if (addr == NULL || *addr == NULL || !**addr || tokens == NULL) return NULL;
char* save = *addr; char* save = *addr;

View File

@ -18,23 +18,24 @@
#include "../include/mappings.h" #include "../include/mappings.h"
#include "../include/types.h" #include "../include/types.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#ifndef UIRC_INCLUDED_PRIVATE_MISC #ifndef UIRC_GUARD_PRIVATE_MISC
#define UIRC_INCLUDED_PRIVATE_MISC #define UIRC_GUARD_PRIVATE_MISC
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
struct tagmapping { struct tagmapping {
const char* const name; const char* const name;
IRC_Tag* assg; IRC_Tag* assg;
}; };
#endif #endif /* UIRC_IRCV3 */
signed short Ircmd_stoi(char* str); signed short Ircmd_stoi(char* str);
size_t safe_strcpy(char** dest, const char* src, size_t lef); size_t safe_strcpy(char** dest, const char* src, size_t lef);
bool safe_charcpy(char** dest, char c, size_t lef); bool safe_charcpy(char** dest, char c, size_t lef);
void skip_spaces(char** addr); void skip_spaces(char** addr);
char* strtok_mr(char** addr, const char* tokens); char* strtok_mr(char** addr, const char* tokens);
#endif #endif /* UIRC_GUARD_PRIVATE_MISC */

View File

@ -22,8 +22,8 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#ifndef UIRC_INCLUDED_PRIVATE_TAGHELPERS #ifndef UIRC_GUARD_PRIVATE_TAGHELPERS
#define UIRC_INCLUDED_PRIVATE_TAGHELPERS #define UIRC_GUARD_PRIVATE_TAGHELPERS
size_t Assm_tag_timestamp(char* buf, size_t len, time_t time); size_t Assm_tag_timestamp(char* buf, size_t len, time_t time);
#endif #endif /* UIRC_GUARD_PRIVATE_TAGHELPERS */

View File

@ -17,7 +17,8 @@
*/ */
#include "tokenizers.h" #include "tokenizers.h"
signed int Tok_mesg(char* str, IRC_Message* out) signed int
Tok_mesg(char* str, IRC_Message* out)
{ {
if (str == NULL || out == NULL) return ERR_UIRC_NULL_ARGS; if (str == NULL || out == NULL) return ERR_UIRC_NULL_ARGS;
int ret; int ret;
@ -31,7 +32,7 @@ signed int Tok_mesg(char* str, IRC_Message* out)
return ERR_UIRC_INVALID_FORMAT; return ERR_UIRC_INVALID_FORMAT;
} }
skip_spaces(&progr); skip_spaces(&progr);
#endif #endif /* UIRC_IRCV3 */
if (*progr == ':') { if (*progr == ':') {
char* prefix; char* prefix;
if ((prefix = strtok_mr(&progr, " ")) != NULL) { if ((prefix = strtok_mr(&progr, " ")) != NULL) {
@ -46,9 +47,9 @@ signed int Tok_mesg(char* str, IRC_Message* out)
signed short temp; signed short temp;
if (isalpha(*command)) { if (isalpha(*command)) {
if ((temp = Ircmd_stoi(command)) < UIRC_FCMD || temp > UIRC_LCMD) return ERR_UIRC_UNKNOWN_TOKEN; if ((temp = Ircmd_stoi(command)) < UIRC_FCMD || temp > UIRC_LCMD) return ERR_UIRC_UNKNOWN_TOKEN;
out->cmd = (IRC_Command)temp; out->cmd = (IRC_Command) temp;
} else { } else {
out->cmd = (IRC_Command)atoi(command); out->cmd = (IRC_Command) atoi(command);
} }
} else } else
return ERR_UIRC_INVALID_FORMAT; return ERR_UIRC_INVALID_FORMAT;
@ -71,16 +72,19 @@ signed int Tok_mesg(char* str, IRC_Message* out)
} }
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
signed int Tok_tags(char* str, IRC_Tags* out) signed int
Tok_tags(char* str, IRC_Tags* out)
{ {
if (str == NULL || out == NULL) return ERR_UIRC_NULL_ARGS; if (str == NULL || out == NULL) return ERR_UIRC_NULL_ARGS;
char *cval, *cpos = str, *ctag = NULL; char * cval, *cpos = str, *ctag = NULL;
bool clientbound; bool clientbound;
const struct tagmapping tagmps[] = {{.name = "time", .assg = &out->time}, {.name = "account", .assg = &out->account}, const struct tagmapping tagmps[] = {
{.name = "batch", .assg = &out->batch}, {.name = "label", .assg = &out->label}, { .name = "time", .assg = &out->time }, { .name = "account", .assg = &out->account },
{.name = "msgid", .assg = &out->msgid}, {.name = "multiline-concat", .assg = &out->multiline_concat}, { .name = "batch", .assg = &out->batch }, { .name = "label", .assg = &out->label },
{.name = "typing", .assg = &out->typing}, {.name = "react", .assg = &out->react}, { .name = "msgid", .assg = &out->msgid }, { .name = "multiline-concat", .assg = &out->multiline_concat },
{.name = "reply", .assg = &out->reply}}; { .name = "typing", .assg = &out->typing }, { .name = "react", .assg = &out->react },
{ .name = "reply", .assg = &out->reply }
};
if (*cpos == '@') cpos++; if (*cpos == '@') cpos++;
while ((ctag = strtok_mr(&cpos, "; ")) != NULL) { while ((ctag = strtok_mr(&cpos, "; ")) != NULL) {
clientbound = false; clientbound = false;
@ -102,16 +106,17 @@ signed int Tok_tags(char* str, IRC_Tags* out)
} }
return 1; return 1;
} }
#endif #endif /* UIRC_IRCV3 */
signed int Tok_user(char* str, IRC_User* out, bool useorig) signed int
Tok_user(char* str, IRC_User* out, bool useorig)
{ {
char* pos = (*str == ':') ? str + 1 : str; char* pos = (*str == ':') ? str + 1 : str;
if ((out->host = strchr(pos, '@')) != NULL) *(out->host++) = '\0'; if ((out->host = strchr(pos, '@')) != NULL) *(out->host++) = '\0';
if (useorig && (out->orig = strchr(pos, '%')) != NULL) *(out->orig++) = '\0'; if (useorig && (out->orig = strchr(pos, '%')) != NULL) *(out->orig++) = '\0';
if ((out->user = strchr(pos, '!')) if ((out->user = strchr(pos, '!'))
!= NULL) /* RFC2812 says this cannot be here without the host but RFC1459 says it can, we accept both options */ != NULL) /* RFC2812 says this cannot be here without the host but RFC1459 says it can, we accept both options */
*((char*)out->user++) = '\0'; *((char*) out->user++) = '\0';
if (!*(out->nick = pos)) return ERR_UIRC_INVALID_FORMAT; if (!*(out->nick = pos)) return ERR_UIRC_INVALID_FORMAT;
/* NOTE: De-facto standard below /* NOTE: De-facto standard below
* This assumes that every prefix without a '@host' and '!user' is itself a host prefix only * This assumes that every prefix without a '@host' and '!user' is itself a host prefix only

View File

@ -19,19 +19,20 @@
#include "../include/mappings.h" #include "../include/mappings.h"
#include "../include/types.h" #include "../include/types.h"
#include "misc.h" #include "misc.h"
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifndef UIRC_INCLUDED_PRIVATE_TKNIZ #ifndef UIRC_GUARD_PRIVATE_TOKENIZERS
#define UIRC_INCLUDED_PRIVATE_TKNIZ #define UIRC_GUARD_PRIVATE_TOKENIZERS
signed int Tok_mesg(char* str, IRC_Message* out); signed int Tok_mesg(char* str, IRC_Message* out);
#ifdef UIRC_IRCV3 #ifdef UIRC_IRCV3
signed int Tok_tags(char* str, IRC_Tags* out); signed int Tok_tags(char* str, IRC_Tags* out);
#endif #endif /* UIRC_IRCV3 */
signed int Tok_user(char* str, IRC_User* out, bool useorig); signed int Tok_user(char* str, IRC_User* out, bool useorig);
#endif #endif /* UIRC_GUARD_PRIVATE_TOKENIZERS */

View File

@ -1,4 +1,3 @@
/* /*
* This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC) * This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC)
* Copyright (c) 2019, 2020 Alex-David Denes * Copyright (c) 2019, 2020 Alex-David Denes
@ -18,7 +17,8 @@
*/ */
#include "validators.h" #include "validators.h"
signed int Val_mesg(IRC_Message* mesg) signed int
Val_mesg(IRC_Message* mesg)
{ {
if (mesg == NULL) return ERR_UIRC_NULL_ARGS; if (mesg == NULL) return ERR_UIRC_NULL_ARGS;
for (unsigned int i = 0; mesg->args[i] != NULL; i++) { for (unsigned int i = 0; mesg->args[i] != NULL; i++) {
@ -30,7 +30,8 @@ signed int Val_mesg(IRC_Message* mesg)
return 1; return 1;
} }
signed int Val_type_nocrlf(char* str) signed int
Val_type_nocrlf(char* str)
{ {
if (str == NULL) return ERR_UIRC_NULL_ARGS; if (str == NULL) return ERR_UIRC_NULL_ARGS;
for (; *str; str++) { for (; *str; str++) {
@ -39,7 +40,8 @@ signed int Val_type_nocrlf(char* str)
return 1; return 1;
} }
signed int Val_type_nospcl(char* str) signed int
Val_type_nospcl(char* str)
{ {
if (str == NULL) return ERR_UIRC_NULL_ARGS; if (str == NULL) return ERR_UIRC_NULL_ARGS;
for (; *str; str++) { for (; *str; str++) {
@ -48,7 +50,8 @@ signed int Val_type_nospcl(char* str)
return 1; return 1;
} }
signed int Val_type_noblcm(char* str) signed int
Val_type_noblcm(char* str)
{ {
if (str == NULL) return ERR_UIRC_NULL_ARGS; if (str == NULL) return ERR_UIRC_NULL_ARGS;
for (; *str; str++) { for (; *str; str++) {
@ -57,7 +60,8 @@ signed int Val_type_noblcm(char* str)
return 1; return 1;
} }
signed int Val_channame(char* chan) signed int
Val_channame(char* chan)
{ {
if (chan == NULL) return ERR_UIRC_NULL_ARGS; if (chan == NULL) return ERR_UIRC_NULL_ARGS;
if (*chan != '#' && *chan != '+' && *chan != '!' && *chan != '&') return 0; if (*chan != '#' && *chan != '+' && *chan != '!' && *chan != '&') return 0;

View File

@ -18,15 +18,16 @@
#include "../include/mappings.h" #include "../include/mappings.h"
#include "../include/types.h" #include "../include/types.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#ifndef UIRC_INCLUDED_PRIVATE_VALIDATORS #ifndef UIRC_GUARD_PRIVATE_VALIDATORS
#define UIRC_INCLUDED_PRIVATE_VALIDATORS #define UIRC_GUARD_PRIVATE_VALIDATORS
signed int Val_mesg(IRC_Message* mesg); signed int Val_mesg(IRC_Message* mesg);
signed int Val_type_nocrlf(char* str); signed int Val_type_nocrlf(char* str);
signed int Val_type_nospcl(char* str); signed int Val_type_nospcl(char* str);
signed int Val_type_noblcm(char* str); signed int Val_type_noblcm(char* str);
signed int Val_channame(char* chan); signed int Val_channame(char* chan);
#endif #endif /* UIRC_GUARD_PRIVATE_VALIDATORS */