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/assemblers/user.c

66 lines
1.7 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 "assemblers.h" // Assm_user()
#include "types.h" // IRC_User
#include <assert.h> // assert()
#include <stdbool.h> // bool
#include <stdio.h> // NULL snprintf()
#include <sys/types.h> // size_t
ssize_t
uirc_assembler_user(char* buf, const IRC_User* u, size_t len)
{
assert(buf != NULL);
assert(u != NULL);
char* const sv = buf;
if (u->nick != NULL) {
if (len < 1) return -1;
int res;
if ((res = snprintf(buf, len, "%s", u->nick)) >= 0) {
buf += (size_t) res;
len -= (size_t) res;
} else
return -1;
}
if (u->user != NULL) {
if (len < 1) return -1;
int res;
if ((res = snprintf(buf, len, "!%s", u->user)) >= 0) {
buf += (size_t) res;
len -= (size_t) res;
} else
return -1;
}
if (u->host != NULL) {
if (len < 1) return -1;
int res;
if ((res = snprintf(buf, len, (u->nick != NULL || u->user != NULL) ? "@%s" : "%s", u->host)) >= 0) {
buf += (size_t) res;
len -= (size_t) res;
} else
return -1;
}
return buf - sv;
}