Cleanup code and fix a few bugs

This commit is contained in:
Alex D. 2020-12-22 11:47:44 +00:00
parent cb7a0a35d8
commit 10c5dfb400
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
5 changed files with 50 additions and 68 deletions

View File

@ -63,7 +63,7 @@ set_channel(Channel* chan, const char* name, const char* key, bool joined)
}
if (key != NULL) {
if (!allocate_copy(&chan->key, key)) {
LOG(LOG_WARN, "Couldn't allocate memory for the channel %s key. " ERRNOFMT, name, strerror(errno), errno);
LOG(LOG_WARN, "Couldn't allocate memory for the channel %s key. " ERRNOFMT, (name == NULL) ? chan->name : name, strerror(errno), errno);
return 0;
}
}

View File

@ -25,23 +25,15 @@ init_connection(Connection* conn)
struct addrinfo* addr_info;
if ((getaddrres = getaddrinfo(conn->data.address, conn->data.service, NULL, &addr_info)) != 0) {
freeaddrinfo(addr_info);
if (getaddrres != EAI_AGAIN && getaddrres != EAI_NONAME) {
LOG(LOG_ERROR,
"Failed to get address info for " ADDRFMT ". " ERRNOFMT,
conn->data.address,
conn->data.service,
gai_strerror(getaddrres),
getaddrres);
return INIT_HARDFAIL;
} else {
LOG(LOG_WARN,
"Failed to get address info for " ADDRFMT ". " ERRNOFMT,
conn->data.address,
conn->data.service,
gai_strerror(getaddrres),
getaddrres);
LOG(LOG_WARN,
"Failed to get address info for " ADDRFMT ". " ERRNOFMT,
conn->data.address,
conn->data.service,
gai_strerror(getaddrres),
getaddrres);
if (getaddrres != EAI_AGAIN && getaddrres != EAI_NONAME) return INIT_HARDFAIL;
else
return INIT_SOFTFAIL;
}
}
if ((sockfd = socket(addr_info->ai_family, addr_info->ai_socktype, addr_info->ai_protocol)) < 0) {
LOG(LOG_ERROR, "Failed to open a socket for " ADDRFMT ". " ERRNOFMT, conn->data.address, conn->data.service, strerror(errno), errno);
@ -51,23 +43,10 @@ init_connection(Connection* conn)
if ((connectres = connect(sockfd, addr_info->ai_addr, addr_info->ai_addrlen)) == -1) {
close(sockfd);
freeaddrinfo(addr_info);
if (errno != EADDRNOTAVAIL && errno != ETIMEDOUT && errno != ECONNRESET && errno != ECONNREFUSED) {
LOG(LOG_ERROR,
"Failed to connect to host " ADDRFMT ". " ERRNOFMT,
conn->data.address,
conn->data.service,
strerror(errno),
errno);
return INIT_HARDFAIL;
} else {
LOG(LOG_WARN,
"Failed to connect to host " ADDRFMT ". " ERRNOFMT,
conn->data.address,
conn->data.service,
strerror(errno),
errno);
LOG(LOG_ERROR, "Failed to connect to host " ADDRFMT ". " ERRNOFMT, conn->data.address, conn->data.service, strerror(errno), errno);
if (errno != EADDRNOTAVAIL && errno != ETIMEDOUT && errno != ECONNRESET && errno != ECONNREFUSED) return INIT_HARDFAIL;
else
return INIT_SOFTFAIL;
}
}
freeaddrinfo(addr_info);
return sockfd;

View File

@ -93,13 +93,11 @@ add_socket_flags(int fd, int flags)
if ((cflgs = fcntl(fd, F_GETFL)) != -1) {
if (fcntl(fd, F_SETFL, cflgs | flags) == -1) {
LOG(LOG_WARN, "Couldn't add socket flags. " ERRNOFMT, strerror(errno), errno);
return 0;
}
} else {
} else
return 1;
} else
LOG(LOG_WARN, "Failed to get socket flags. " ERRNOFMT, strerror(errno), errno);
return 0;
}
return 1;
return 0;
}
ssize_t
@ -108,39 +106,46 @@ get_path(char* buf, size_t lim, Connection* conn, IRC_Message* msg, bool global,
if (buf == NULL || conn == NULL) return -1;
ssize_t res;
const char* save = buf;
int ci = -1;
int ci = -1;
const char *dir = NULL, *subdir = NULL;
if (global) {
if ((res = snprintf(buf, lim, "global")) == -1) return -1;
dir = "global";
} else if (msg != NULL && (ci = get_msgchannel(msg)) != -1 && msg->args[ci] != NULL) {
if ((res = snprintf(buf, lim, "channel")) == -1) return -1;
clean_and_push(&lim, (size_t) res, &buf);
if ((res = snprintf(buf, lim, "%s", msg->args[ci])) == -1) return -1;
dir = "channel";
subdir = msg->args[ci];
} else if (msg != NULL && msg->name.nick != NULL) {
if ((res = snprintf(buf, lim, "user")) == -1) return -1;
clean_and_push(&lim, (size_t) res, &buf);
if ((res = snprintf(buf, lim, "%s", msg->name.nick)) == -1) return -1;
dir = "user";
subdir = msg->name.nick;
} else
return -1;
clean_and_push(&lim, (size_t) res, &buf);
if ((res = snprintf(buf, lim, "%s", dir)) == -1) return -1;
clean_and_push(&lim, (size_t) res, &buf, true);
if (subdir != NULL) {
if ((res = snprintf(buf, lim, "%s", subdir)) == -1) return -1;
clean_and_push(&lim, (size_t) res, &buf, true);
}
const char* category = NULL;
if (input) {
if ((res = snprintf(buf, lim, "in")) == -1) return -1;
category = "in";
} else if (output) {
if ((res = snprintf(buf, lim, "out")) == -1) return -1;
} else if ((category = get_categ(msg)) != NULL) {
if ((res = snprintf(buf, lim, "%s", category)) == -1) return -1;
} else
return -1;
category = "out";
} else {
if ((category = get_categ(msg)) == NULL) return -1;
}
if ((res = snprintf(buf, lim, "%s", category)) == -1) return -1;
clean_and_push(&lim, (size_t) res, &buf, false);
return (buf - save);
}
void
clean_and_push(size_t* lim, size_t res, char** buf)
clean_and_push(size_t* lim, size_t res, char** buf, bool dir)
{
cleanup_path_names(*buf);
*(*(buf) + res) = '/';
*lim -= (size_t) res + 1;
*buf += (size_t) res + 1;
if (dir) *(*(buf) + res) = '/';
*lim -= (size_t) res + dir;
*buf += (size_t) res + dir;
}

View File

@ -39,7 +39,7 @@ bool write_log(const char* path, const char* message);
bool cleanup_path_names(char* name);
bool add_socket_flags(int fd, int flags);
ssize_t get_path(char* buf, size_t lim, Connection* conn, IRC_Message* msg, bool global, bool input, bool output);
void clean_and_push(size_t* lim, size_t res, char** buf);
void clean_and_push(size_t* lim, size_t res, char** buf, bool dir);
#endif /* UIRCD_GUARD_FILESYSTEM */

View File

@ -84,7 +84,7 @@ print_help(void)
{ 'c', "str", "Channel", NULL },
{ 'k', "str", "Channel key", NULL },
#ifdef UIRCD_FEATURE_LIBCONFIG
{ 'C', "path", "Configuration path", "${XDG_CONFIG_HOME:-~/.config}/uircd/main.conf" },
{ 'C', "path", "Configuration path", NULL },
#endif /* UIRCD_FEATURE_LIBCONFIG */
{ 'v', NULL, "Print version information", NULL },
{ 'h', NULL, "Print this help message", NULL } };
@ -366,14 +366,12 @@ main(int argc, char* argv[])
char logpath[UIRCD_LIMITS_PATH];
if (Assm_mesg(buf.log.buffer, &buffer, sizeof(buf.log.buffer)) > 0) {
printf("%s\r\n", buf.log.buffer);
if (get_path(logpath, sizeof(logpath), &connection, &buffer, false, false, false) >= 0) {
if (mkdir_bottomup(logpath)) write_log(logpath, buf.log.buffer);
}
if (get_path(logpath, sizeof(logpath), &connection, &buffer, true, false, false) >= 0) {
if (mkdir_bottomup(logpath)) write_log(logpath, buf.log.buffer);
}
if (get_path(logpath, sizeof(logpath), &connection, &buffer, true, false, true) >= 0) {
if (mkdir_bottomup(logpath)) write_log(logpath, buf.log.buffer);
bool match[][3] = { { false, false, false }, { true, false, false }, { true, false, true } };
for (unsigned long i = 0; i < sizeof(match) / sizeof(*match); i++) {
if (get_path(logpath, sizeof(logpath), &connection, &buffer, match[i][0], match[i][1], match[i][2])
>= 0) {
if (mkdir_bottomup(logpath)) write_log(logpath, buf.log.buffer);
}
}
}
if (!auto_msg_actions(&buffer, &connection, &buf.send)) continue;