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

167 lines
4.6 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 "helpers.h"
static IRC_Message imassm_mesg;
char* RESERVED = "*";
void 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();
int i;
for (i = 0; args[i] != NULL && i < 15; i++) {
imassm_mesg.args[i] = args[i];
}
if (i < req)
return NULL;
imassm_mesg.trailing = trailing;
imassm_mesg.cmd = cmd;
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];
snprintf(local_mode, 2, "%i", modes);
imassm_mesg.args[0] = user;
imassm_mesg.args[1] = local_mode;
imassm_mesg.args[2] = RESERVED;
imassm_mesg.args[3] = realname;
imassm_mesg.trailing = true;
imassm_mesg.cmd = USER;
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_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;
}
/* 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) ? source : target;
imassm_mesg.args[1] = (source != NULL && target != NULL) ? target : NULL;
imassm_mesg.trailing = (source != NULL && target == NULL) ? true : false;
imassm_mesg.cmd = PING;
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_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[2] : (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];
}