70 lines
1.8 KiB
C
70 lines
1.8 KiB
C
/*
|
|
* This file is part of uIRC. (https://git.redxen.eu/caskd/uIRC)
|
|
* Copyright (c) 2019-2021 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 "uirc/assembler.h" // Assm_user()
|
|
#include "uirc/error.h" // uirc_errno
|
|
#include "uirc/type.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) {
|
|
int res;
|
|
if ((res = snprintf(buf, len, "%s", u->nick)) >= 0) {
|
|
buf += (size_t) res;
|
|
len -= (size_t) res;
|
|
} else {
|
|
uirc_errno = UIRC_ERR_BUFSIZ;
|
|
return -1;
|
|
}
|
|
}
|
|
if (u->user != NULL) {
|
|
int res;
|
|
if ((res = snprintf(buf, len, "!%s", u->user)) >= 0) {
|
|
buf += (size_t) res;
|
|
len -= (size_t) res;
|
|
} else {
|
|
uirc_errno = UIRC_ERR_BUFSIZ;
|
|
return -1;
|
|
}
|
|
}
|
|
if (u->host != NULL) {
|
|
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 {
|
|
uirc_errno = UIRC_ERR_BUFSIZ;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
return buf - sv;
|
|
}
|
|
|