This repository has been archived on 2021-04-17. You can view files and clone it, but cannot push or open issues or pull requests.
uIRC/src/misc.c

123 lines
4.4 KiB
C

/*
* This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC)
* Copyright (c) 2019, 2020 Alex-David Denes
*
* uIRC is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* uIRC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with uIRC. If not, see <https://www.gnu.org/licenses/>.
*/
#include "misc.h"
const char* const IRC_Cmds[] = { [ADMIN] = "ADMIN", [AWAY] = "AWAY", [CONNECT] = "CONNECT", [DIE] = "DIE",
[ERROR] = "ERROR", [INFO] = "INFO", [INVITE] = "INVITE", [ISON] = "ISON",
[JOIN] = "JOIN", [KICK] = "KICK", [KILL] = "KILL", [LINKS] = "LINKS",
[LIST] = "LIST", [LUSERS] = "LUSERS", [MODE] = "MODE", [MOTD] = "MOTD",
[NAMES] = "NAMES", [NICK] = "NICK", [NOTICE] = "NOTICE", [OPER] = "OPER",
[PART] = "PART", [PASS] = "PASS", [PING] = "PING", [PONG] = "PONG",
[PRIVMSG] = "PRIVMSG", [QUIT] = "QUIT", [REHASH] = "REHASH", [RESTART] = "RESTART",
[SERVER] = "SERVER", [SERVICE] = "SERVICE", [SERVLIST] = "SERVLIST", [SQUERY] = "SQUERY",
[SQUIT] = "SQUIT", [STATS] = "STATS", [SUMMON] = "SUMMON", [TIME] = "TIME",
[TOPIC] = "TOPIC", [TRACE] = "TRACE", [USERHOST] = "USERHOST", [USERS] = "USERS",
[USER] = "USER", [VERSION] = "VERSION", [WALLOPS] = "WALLOPS", [WHOIS] = "WHOIS",
[WHOWAS] = "WHOWAS", [WHO] = "WHO",
#ifdef UIRC_IRCV3
[ACCOUNT] = "ACCOUNT", [ACC] = "ACC", [ACK] = "ACK", [AUTHENTICATE] = "AUTHENTICATE",
[BATCH] = "BATCH", [CAP] = "CAP", [CHGHOST] = "CHGHOST", [FAIL] = "FAIL",
[MONITOR] = "MONITOR", [NOTE] = "NOTE", [RENAME] = "RENAME", [RESUME] = "RESUME",
[SETNAME] = "SETNAME", [WARN] = "WARN", [WEBIRC] = "WEBIRC"
#endif /* UIRC_IRCV3 */
};
#ifdef UIRC_IRCV3
const char* const IRC_v3_Caps[] = {
[CAP_ACCOUNT_NOTIFY] = "account-notify",
[CAP_ACCOUNT_TAG] = "account-tag",
[CAP_AWAY_NOTIFY] = "away-notify",
[CAP_BATCH] = "batch",
[CAP_CAP_NOTIFY] = "cap-notify",
[CAP_CHANNEL_RENAME] = "channel-rename",
[CAP_CHGHOST] = "chghost",
[CAP_ECHO_MESSAGE] = "echo-message",
[CAP_EXTENDED_JOIN] = "extended-join",
[CAP_INVITE_NOTIFY] = "invite-notify",
[CAP_LABELED_RESPONSE] = "labeled-response",
[CAP_MESSAGE_TAGS] = "message-tags",
[CAP_MONITOR] = "monitor",
[CAP_MULTI_PREFIX] = "multi-prefix",
[CAP_MULTILINE] = "multiline",
[CAP_SASL] = "sasl",
[CAP_SERVER_TIME] = "server-time",
[CAP_SETNAME] = "setname",
[CAP_TLS] = "tls",
[CAP_USERHOST_IN_NAMES] = "userhost-in-names",
};
#endif /* UIRC_IRCV3 */
signed short
Ircmd_stoi(char* str)
{
if (str == NULL) return ERR_UIRC_NULL_ARGS;
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;
}
return ERR_UIRC_UNKNOWN_TOKEN;
}
size_t
safe_strcpy(char** dest, const char* src, size_t lef)
{
size_t cnt;
if (lef > (cnt = strlen(src)) + 1) {
strcpy(*dest, src);
*dest += cnt;
return cnt;
} else
return 0;
}
bool
safe_charcpy(char** dest, const char c, size_t lef)
{
if (lef > 1) {
*(*dest)++ = c;
**dest = '\0';
return 1;
} else
return 0;
}
void
skip_spaces(char** addr)
{
for (; **addr == ' '; (*addr)++)
;
}
char*
strtok_mr(char** addr, const char* tokens)
{
if (addr == NULL || *addr == NULL || !**addr || tokens == NULL) return NULL;
char* save = *addr;
const char* tok = NULL;
do {
if (!**addr) return save;
for (tok = tokens; *tok; tok++) {
if (**addr == *tok) {
**addr = '\0';
break;
}
}
} while (*(*addr)++);
return save;
}