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/helpers.c

474 lines
12 KiB
C
Raw Normal View History

2020-07-17 13:49:59 +00:00
/*
* 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/>.
*/
2020-07-22 16:57:59 +00:00
#include "helpers.h"
static IRC_Message imassm_mesg;
char* RESERVED = "*";
void clear_assm(void)
{
memset((void*)&imassm_mesg, '\0', sizeof(IRC_Message));
}
IRC_Message* Assm_cmd_NICK(char* nick)
{
if (nick == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = nick;
imassm_mesg.cmd = NICK;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_USER(char* user, char* realname, int modes)
{
if (user == NULL || modes < 0 || modes > (MBMASK_INVIS | MBMASK_WALLOPS))
return NULL;
clear_assm();
static char local_mode[2];
sprintf(local_mode, "%i", modes);
imassm_mesg.args[0] = user;
imassm_mesg.args[1] = local_mode;
imassm_mesg.args[2] = RESERVED;
imassm_mesg.trailing = realname;
imassm_mesg.cmd = USER;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_PASS(char* password)
{
if (password == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = password;
imassm_mesg.cmd = PASS;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_OPER(char* name, char* password)
{
if (name == NULL || password == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = name;
imassm_mesg.args[1] = password;
imassm_mesg.cmd = OPER;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_MODE(char* nick, char* modes, char* modeparams)
{
if (nick == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = nick;
imassm_mesg.args[1] = modes;
imassm_mesg.args[2] = modeparams;
imassm_mesg.cmd = MODE;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_SERVICE(char* nickname, char* distribution, char* type, char* info)
{
if (nickname == NULL || distribution == NULL || type == NULL || info == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = nickname;
imassm_mesg.args[1] = RESERVED;
imassm_mesg.args[2] = distribution;
imassm_mesg.args[3] = "0";
imassm_mesg.args[4] = RESERVED;
imassm_mesg.trailing = info;
imassm_mesg.cmd = SERVICE;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_QUIT(char* mesg)
{
clear_assm();
imassm_mesg.trailing = mesg;
imassm_mesg.cmd = QUIT;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_SQUIT(char* server, char* comment)
{
if (server == NULL || comment == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = server;
imassm_mesg.trailing = comment;
imassm_mesg.cmd = SQUIT;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_JOIN(char* channels, char* keys)
{
if (channels == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = channels;
imassm_mesg.args[1] = keys;
imassm_mesg.cmd = JOIN;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_PART(char* channel, char* message)
{
if (channel == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = channel;
imassm_mesg.trailing = message;
imassm_mesg.cmd = PART;
return &imassm_mesg;
}
/* 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
*/
IRC_Message* Assm_cmd_TOPIC(char* channel, char* topic)
{
if (channel == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = channel;
imassm_mesg.trailing = topic;
imassm_mesg.cmd = TOPIC;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_NAMES(char* channels, char* target)
{
if (channels == NULL && target != NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = channels;
imassm_mesg.args[1] = target;
imassm_mesg.cmd = NAMES;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_LIST(char* channels, char* target)
{
if (channels == NULL && target != NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = channels;
imassm_mesg.args[1] = target;
imassm_mesg.cmd = LIST;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_INVITE(char* nick, char* channel)
{
if (nick == NULL || channel == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = nick;
imassm_mesg.args[1] = channel;
imassm_mesg.cmd = INVITE;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_KICK(char* channels, char* users, char* comment)
{
if (channels == NULL || users == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = channels;
imassm_mesg.args[1] = users;
imassm_mesg.trailing = comment;
imassm_mesg.cmd = KICK;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_PRIVMSG(char* target, char* message)
{
if (target == NULL || message == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.trailing = message;
imassm_mesg.cmd = PRIVMSG;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_NOTICE(char* target, char* text)
{
if (target == NULL || text == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.args[1] = text;
imassm_mesg.cmd = NOTICE;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_MOTD(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = MOTD;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_LUSERS(char* mask, char* target)
{
if (mask == NULL && target != NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = mask;
imassm_mesg.args[1] = target;
imassm_mesg.cmd = LUSERS;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_VERSION(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = VERSION;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_STATS(char* query, char* target)
{
if (query == NULL && target != NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = query;
imassm_mesg.args[1] = target;
imassm_mesg.cmd = STATS;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_LINKS(char* remoteserv, char* servmask)
{
if (remoteserv != NULL && servmask == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = (remoteserv == NULL) ? servmask : remoteserv;
imassm_mesg.args[1] = (remoteserv == NULL) ? NULL : servmask;
imassm_mesg.cmd = LINKS;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_TIME(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = TIME;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_CONNECT(char* target, char* port, char* remote)
{
if (target == NULL || port == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.args[1] = port;
imassm_mesg.args[2] = remote;
imassm_mesg.cmd = CONNECT;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_TRACE(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = TRACE;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_ADMIN(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = ADMIN;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_INFO(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = INFO;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_SERVLIST(char* mask, char* type)
{
if (type != NULL && mask == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = mask;
imassm_mesg.args[1] = type;
imassm_mesg.cmd = SERVLIST;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_SQUERY(char* servicename, char* text)
{
if (servicename == NULL || text == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = servicename;
imassm_mesg.trailing = text;
imassm_mesg.cmd = SQUERY;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_WHO(char* mask, bool oper)
{
static char* operator= "o";
if (oper && mask == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = mask;
imassm_mesg.args[1] = (oper) ? operator: NULL;
imassm_mesg.cmd = WHO;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_WHOIS(char* target, char* mask)
{
if (mask == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = (target == NULL) ? mask : target;
imassm_mesg.args[1] = (target == NULL) ? NULL : mask;
imassm_mesg.cmd = WHOIS;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_WHOWAS(char* nick, char* count, char* target)
{
if (nick == NULL || (target != NULL && count == NULL))
return NULL;
clear_assm();
imassm_mesg.args[0] = nick;
imassm_mesg.args[1] = count;
imassm_mesg.args[2] = target;
imassm_mesg.cmd = WHOWAS;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_KILL(char* nick, char* comment)
{
if (nick == NULL || comment == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = nick;
imassm_mesg.trailing = comment;
imassm_mesg.cmd = KILL;
return &imassm_mesg;
}
/* NOTE: This is what implementation you have to live with
* I would've just used the prefix to set the source but whatever
*/
IRC_Message* Assm_cmd_PING(char* source, char* target)
{
if (source == NULL && target == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = (source != NULL && target != NULL) ? source : (target != NULL) ? target : NULL;
imassm_mesg.args[1] = (source != NULL && target != NULL) ? target : NULL;
imassm_mesg.trailing = (source != NULL && target == NULL) ? source : NULL;
imassm_mesg.cmd = PING;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_PONG(char* source, char* target)
{
if (source == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = source;
imassm_mesg.args[1] = target;
imassm_mesg.cmd = PONG;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_ERROR(char* message)
{
if (message == NULL)
return NULL;
clear_assm();
imassm_mesg.trailing = message;
imassm_mesg.cmd = ERROR;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_AWAY(char* mesg)
{
clear_assm();
imassm_mesg.trailing = mesg;
imassm_mesg.cmd = AWAY;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_REHASH(void)
{
clear_assm();
imassm_mesg.cmd = REHASH;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_DIE(void)
{
clear_assm();
imassm_mesg.cmd = DIE;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_RESTART(void)
{
clear_assm();
imassm_mesg.cmd = RESTART;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_SUMMON(char* user, char* target, char* channel)
{
if (user == NULL || (channel != NULL && target == NULL))
return NULL;
clear_assm();
imassm_mesg.args[0] = user;
imassm_mesg.args[1] = target;
imassm_mesg.args[2] = channel;
imassm_mesg.cmd = SUMMON;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_USERS(char* target)
{
if (target == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = USERS;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_WALLOPS(char* text)
{
if (text == NULL)
return NULL;
clear_assm();
imassm_mesg.trailing = text;
imassm_mesg.cmd = WALLOPS;
return &imassm_mesg;
}
IRC_Message* Assm_cmd_USERHOST(char* users[])
{
if (users[0] == NULL)
return NULL;
clear_assm();
for (unsigned int i = 0; i < 5 && users[i] != NULL; i++) {
imassm_mesg.args[i] = users[i];
}
imassm_mesg.cmd = USERHOST;
return &imassm_mesg;
}
/* NOTE: Limited to 14 nicks per command */
IRC_Message* Assm_cmd_ISON(char* users[])
{
if (users[0] == NULL)
return NULL;
clear_assm();
for (unsigned int i = 0; i < 14 && users[i] != NULL; i++) {
imassm_mesg.args[i] = users[i];
}
imassm_mesg.cmd = ISON;
return &imassm_mesg;
}
void Tok_cmd_PING(IRC_Message* mesg, char** source, char** target)
{
*source = (mesg->args[0] == NULL) ? mesg->trailing : (mesg->args[1] != NULL) ? mesg->args[0] : NULL;
*target = (mesg->args[1] == NULL) ? mesg->args[0] : mesg->args[1];
}
/* Use with WHOIS/LINKS
* (stands for first argument optional)
* [ <optarg> ] <reqarg>
*/
void Tok_FArgOpt(IRC_Message* mesg, char** optarg, char** reqarg)
{
*optarg = (mesg->args[1] != NULL) ? mesg->args[0] : NULL;
*reqarg = (mesg->args[1] != NULL) ? mesg->args[1] : mesg->args[0];
}