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

466 lines
11 KiB
C
Raw Normal View History

#include "../include/uirc.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static IRC_Message imassm_mesg;
2020-07-05 10:48:24 +00:00
const char* const RESERVED = "*";
void clear_assm(void)
{
memset((void*)&imassm_mesg, '\0', sizeof(IRC_Message));
}
IRC_Message* Assemble_NICK(char* nick)
{
if (nick == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = nick;
imassm_mesg.cmd = NICK;
return &imassm_mesg;
}
IRC_Message* Assemble_USER(char* user, char* realname, int modes)
{
if (user == NULL || modes < 0 || modes > (MBMASK_INVIS | MBMASK_WALLOPS))
return NULL;
clear_assm();
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* Assemble_PASS(char* password)
{
if (password == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = password;
imassm_mesg.cmd = PASS;
return &imassm_mesg;
}
IRC_Message* Assemble_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;
}
2020-07-05 10:48:24 +00:00
IRC_Message* Assemble_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* Assemble_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* Assemble_QUIT(char* mesg, IRC_User* user)
{
clear_assm();
imassm_mesg.trailing = mesg;
if (user != NULL)
imassm_mesg.name = *user;
imassm_mesg.cmd = QUIT;
return &imassm_mesg;
}
IRC_Message* Assemble_SQUIT(char* server, char* comment, IRC_User* user)
{
if (server == NULL || comment == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = server;
imassm_mesg.trailing = comment;
if (user != NULL)
imassm_mesg.name = *user;
imassm_mesg.cmd = SQUIT;
return &imassm_mesg;
}
IRC_Message* Assemble_JOIN(char* channels, char* keys, IRC_User* user)
{
if (channels == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = channels;
imassm_mesg.args[1] = keys;
if (user != NULL)
imassm_mesg.name = *user;
imassm_mesg.cmd = JOIN;
return &imassm_mesg;
}
IRC_Message* Assemble_PART(char* channel, char* message, IRC_User* user)
{
if (channel == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = channel;
imassm_mesg.trailing = message;
if (user != NULL)
imassm_mesg.name = *user;
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* Assemble_TOPIC(char* channel, char* topic, IRC_User* user)
{
if (channel == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = channel;
imassm_mesg.trailing = topic;
if (user != NULL)
imassm_mesg.name = *user;
imassm_mesg.cmd = TOPIC;
return &imassm_mesg;
}
IRC_Message* Assemble_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* Assemble_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* Assemble_INVITE(char* nick, char* channel, IRC_User* user)
{
if (nick == NULL || channel == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = nick;
imassm_mesg.args[1] = channel;
if (user != NULL)
imassm_mesg.name = *user;
imassm_mesg.cmd = INVITE;
return &imassm_mesg;
}
IRC_Message* Assemble_KICK(char* channels, char* users, char* comment, IRC_User* user)
{
if (channels == NULL || users == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = channels;
imassm_mesg.args[1] = users;
imassm_mesg.trailing = comment;
if (user != NULL)
imassm_mesg.name = *user;
imassm_mesg.cmd = KICK;
return &imassm_mesg;
}
IRC_Message* Assemble_PRIVMSG(char* target, char* message, IRC_User* source)
{
if (target == NULL || message == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.trailing = message;
if (source != NULL)
imassm_mesg.name = *source;
imassm_mesg.cmd = PRIVMSG;
return &imassm_mesg;
}
IRC_Message* Assemble_NOTICE(char* target, char* text, IRC_User* user)
{
if (target == NULL || text == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.args[1] = text;
if (user != NULL)
imassm_mesg.name = *user;
imassm_mesg.cmd = NOTICE;
return &imassm_mesg;
}
IRC_Message* Assemble_MOTD(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = MOTD;
return &imassm_mesg;
}
IRC_Message* Assemble_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* Assemble_VERSION(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = VERSION;
return &imassm_mesg;
}
IRC_Message* Assemble_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* Assemble_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* Assemble_TIME(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = TIME;
return &imassm_mesg;
}
IRC_Message* Assemble_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* Assemble_TRACE(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = TRACE;
return &imassm_mesg;
}
IRC_Message* Assemble_ADMIN(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = ADMIN;
return &imassm_mesg;
}
IRC_Message* Assemble_INFO(char* target)
{
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = INFO;
return &imassm_mesg;
}
IRC_Message* Assemble_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* Assemble_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* Assemble_WHO(char* mask, bool oper)
{
static const char* const 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* Assemble_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* Assemble_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* Assemble_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* Assemble_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* Assemble_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* Assemble_ERROR(char* message)
{
if (message == NULL)
return NULL;
clear_assm();
imassm_mesg.trailing = message;
imassm_mesg.cmd = ERROR;
return &imassm_mesg;
}
IRC_Message* Assemble_AWAY(char* mesg)
{
clear_assm();
imassm_mesg.trailing = mesg;
imassm_mesg.cmd = AWAY;
return &imassm_mesg;
}
IRC_Message* Assemble_REHASH(void)
{
clear_assm();
imassm_mesg.cmd = REHASH;
return &imassm_mesg;
}
IRC_Message* Assemble_DIE(void)
{
clear_assm();
imassm_mesg.cmd = DIE;
return &imassm_mesg;
}
IRC_Message* Assemble_RESTART(void)
{
clear_assm();
imassm_mesg.cmd = RESTART;
return &imassm_mesg;
}
IRC_Message* Assemble_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* Assemble_USERS(char* target)
{
if (target == NULL)
return NULL;
clear_assm();
imassm_mesg.args[0] = target;
imassm_mesg.cmd = USERS;
return &imassm_mesg;
}
IRC_Message* Assemble_WALLOPS(char* text, IRC_User* source)
{
if (text == NULL)
return NULL;
clear_assm();
imassm_mesg.trailing = text;
if (source != NULL)
imassm_mesg.name = *source;
imassm_mesg.cmd = WALLOPS;
return &imassm_mesg;
}
IRC_Message* Assemble_USERHOST(char* users[], IRC_User* source)
{
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];
}
if (source != NULL)
imassm_mesg.name = *source;
imassm_mesg.cmd = USERHOST;
return &imassm_mesg;
}
/* NOTE: Limited to 14 nicks per command */
IRC_Message* Assemble_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;
}