WIP: reformat code and remove some stuff that needs to be changed
This commit is contained in:
parent
fc99d4e88f
commit
008afed09f
|
@ -1,20 +1,41 @@
|
|||
---
|
||||
Language: Cpp
|
||||
Standard: Cpp11
|
||||
BasedOnStyle: LLVM
|
||||
TabWidth: 8
|
||||
IndentWidth: 8
|
||||
UseTab: ForIndentation
|
||||
AllowShortBlocksOnASingleLine: true
|
||||
AllowShortCaseLabelsOnASingleLine: true
|
||||
AllowShortFunctionsOnASingleLine: true
|
||||
AllowShortIfStatementsOnASingleLine: true
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
BreakBeforeBinaryOperators: NonAssignment
|
||||
BreakBeforeBraces: Linux
|
||||
IndentCaseLabels: true
|
||||
PointerAlignment: Left
|
||||
ColumnLimit: 150
|
||||
...
|
||||
BasedOnStyle: LLVM
|
||||
AlignConsecutiveMacros: 'true'
|
||||
AlignConsecutiveAssignments: 'true'
|
||||
AlignConsecutiveDeclarations: 'true'
|
||||
AlignEscapedNewlines: Left
|
||||
AllowShortBlocksOnASingleLine: 'true'
|
||||
AllowShortCaseLabelsOnASingleLine: 'true'
|
||||
AllowShortFunctionsOnASingleLine: All
|
||||
AllowShortIfStatementsOnASingleLine: Always
|
||||
AllowShortLoopsOnASingleLine: 'true'
|
||||
AlwaysBreakAfterDefinitionReturnType: TopLevel
|
||||
AlwaysBreakAfterReturnType: AllDefinitions
|
||||
AlwaysBreakBeforeMultilineStrings: 'false'
|
||||
BinPackArguments: 'false'
|
||||
BinPackParameters: 'false'
|
||||
BreakBeforeBinaryOperators: NonAssignment
|
||||
BreakBeforeBraces: Linux
|
||||
BreakStringLiterals: 'false'
|
||||
ColumnLimit: '150'
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
|
||||
Cpp11BracedListStyle: 'false'
|
||||
IncludeBlocks: Regroup
|
||||
IndentCaseLabels: 'true'
|
||||
IndentPPDirectives: None
|
||||
IndentWidth: '8'
|
||||
Language: Cpp
|
||||
PointerAlignment: Left
|
||||
ReflowComments: 'true'
|
||||
SortIncludes: 'true'
|
||||
SpaceAfterCStyleCast: 'true'
|
||||
SpaceAfterLogicalNot: 'false'
|
||||
SpaceBeforeAssignmentOperators: 'true'
|
||||
SpaceBeforeCpp11BracedList: 'true'
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpacesInCStyleCastParentheses: 'false'
|
||||
Standard: Cpp11
|
||||
TabWidth: '8'
|
||||
UseTab: ForIndentation
|
||||
|
||||
...
|
||||
|
|
|
@ -3,7 +3,7 @@ Checks: 'clang-diagnostic-*,clang-analyzer-*,clang-diagnostic-*,clang-a
|
|||
WarningsAsErrors: ''
|
||||
HeaderFilterRegex: ''
|
||||
AnalyzeTemporaryDtors: false
|
||||
FormatStyle: none
|
||||
FormatStyle: file
|
||||
User: caskd
|
||||
CheckOptions:
|
||||
- key: bugprone-argument-comment.CommentBoolLiterals
|
||||
|
|
11
src/buffer.c
11
src/buffer.c
|
@ -18,7 +18,8 @@
|
|||
|
||||
#include "buffer.h"
|
||||
|
||||
ssize_t get_buffer_line(char* buf, IRC_Message* parsed)
|
||||
ssize_t
|
||||
get_buffer_line(char* buf, IRC_Message* parsed)
|
||||
{
|
||||
if (buf == NULL || parsed == NULL) return -1;
|
||||
char* ppoi;
|
||||
|
@ -33,14 +34,14 @@ ssize_t get_buffer_line(char* buf, IRC_Message* parsed)
|
|||
return 0;
|
||||
}
|
||||
|
||||
ssize_t flush_buffer(char* buf, size_t buflen, int fd)
|
||||
ssize_t
|
||||
flush_buffer(char* buf, size_t buflen, int fd)
|
||||
{
|
||||
ssize_t res;
|
||||
char* pos = buf;
|
||||
char* pos = buf;
|
||||
for (;;) {
|
||||
if ((size_t)(res = write(fd, pos, buflen - (size_t)(pos - buf))) != buflen - (size_t)(pos - buf)) {
|
||||
if (res == -1 && errno != EINTR)
|
||||
return -1;
|
||||
if (res == -1 && errno != EINTR) return -1;
|
||||
else
|
||||
pos += res;
|
||||
} else
|
||||
|
|
10
src/buffer.h
10
src/buffer.h
|
@ -18,14 +18,14 @@
|
|||
|
||||
#include "logging.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h> // errno
|
||||
#include <string.h> // strerror()
|
||||
#include <sys/types.h> // size_t ssize_t
|
||||
#include <unistd.h> // write()
|
||||
|
||||
#define UIRC_IRCV3
|
||||
#define UIRC_HELPERS
|
||||
#include <uirc/uirc.h>
|
||||
#include <uirc/uirc.h> // Tok_mesg()
|
||||
|
||||
ssize_t get_buffer_line(char* buf, IRC_Message* parsed);
|
||||
ssize_t flush_buffer(char* buf, size_t buflen, int fd);
|
||||
|
|
|
@ -19,10 +19,11 @@
|
|||
#include "configuration.h"
|
||||
|
||||
#ifdef UIRCD_FEATURE_LIBCONFIG
|
||||
int parse_configfile(char* config_path, Connection* conn)
|
||||
int
|
||||
parse_configfile(char* config_path, Connection* conn)
|
||||
{
|
||||
config_t conf;
|
||||
int res = -1;
|
||||
config_t conf;
|
||||
int res = -1;
|
||||
config_setting_t* setting;
|
||||
config_init(&conf);
|
||||
if (config_read_file(&conf, config_path)) {
|
||||
|
@ -30,19 +31,19 @@ int parse_configfile(char* config_path, Connection* conn)
|
|||
// TODO: Add all options here
|
||||
struct maps {
|
||||
const char* str;
|
||||
char** save;
|
||||
char** save;
|
||||
} maps[] = {
|
||||
{"address", &conn->data.address},
|
||||
{"service", &conn->data.service},
|
||||
{"quitmsg", &conn->data.quitmsg},
|
||||
{"logdir", &conn->data.path},
|
||||
{ "address", &conn->data.address },
|
||||
{ "service", &conn->data.service },
|
||||
{ "quitmsg", &conn->data.quitmsg },
|
||||
{ "logdir", &conn->data.path },
|
||||
};
|
||||
for (unsigned long i = 0; i < sizeof(maps) / sizeof(*maps); i++) {
|
||||
const char* var = NULL;
|
||||
config_setting_lookup_string(setting, maps[i].str, &var);
|
||||
if (var != NULL) {
|
||||
if (allocate_copy(maps[i].save, var)) {
|
||||
LOG(LOG_DEBUG, "Saved %s to %p", var, (const void*)maps[i].save);
|
||||
LOG(LOG_DEBUG, "Saved %s to %p", var, (const void*) maps[i].save);
|
||||
} else {
|
||||
LOG(LOG_WARN, "Failed to allocate memory to save config variable %s = %s", maps[i].str, var);
|
||||
}
|
||||
|
@ -51,25 +52,31 @@ int parse_configfile(char* config_path, Connection* conn)
|
|||
res = 1;
|
||||
}
|
||||
} else
|
||||
LOG(LOG_WARN, "Encountered a error while reading the config file. %s in %s line %d", config_error_text(&conf),
|
||||
config_error_file(&conf), config_error_line(&conf));
|
||||
LOG(LOG_WARN,
|
||||
"Encountered a error while reading the config file. %s in %s line %d",
|
||||
config_error_text(&conf),
|
||||
config_error_file(&conf),
|
||||
config_error_line(&conf));
|
||||
config_destroy(&conf);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
||||
int set_config_defaults(Connection* conn)
|
||||
int
|
||||
set_config_defaults(Connection* conn)
|
||||
{
|
||||
struct {
|
||||
char** var;
|
||||
char* def;
|
||||
char* def;
|
||||
} defsalloc[] = {
|
||||
{&conn->data.address, UIRCD_DEFAULT_ADDR}, {&conn->data.service, UIRCD_DEFAULT_PORT}, {&conn->data.quitmsg, UIRCD_DEFAULT_QUITMSG},
|
||||
{&conn->user.nickname, UIRCD_DEFAULT_NICK}, {&conn->user.username, UIRCD_DEFAULT_USER}, {&conn->user.realname, UIRCD_DEFAULT_REAL},
|
||||
{ &conn->data.address, UIRCD_DEFAULT_ADDR }, { &conn->data.service, UIRCD_DEFAULT_PORT },
|
||||
{ &conn->data.quitmsg, UIRCD_DEFAULT_QUITMSG }, { &conn->user.nickname, UIRCD_DEFAULT_NICK },
|
||||
{ &conn->user.username, UIRCD_DEFAULT_USER }, { &conn->user.realname, UIRCD_DEFAULT_REAL },
|
||||
};
|
||||
for (unsigned int i = 0; i < sizeof(defsalloc) / sizeof(*defsalloc); i++) {
|
||||
if (!allocate_copy(defsalloc[i].var, defsalloc[i].def)) return 0;
|
||||
}
|
||||
conn->data.timeout = 20;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,20 +16,23 @@
|
|||
* along with uIRCd. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef UIRCD_FEATURE_LIBCONFIG
|
||||
#include <libconfig.h> // config_t config_setting_t config_init() ...
|
||||
|
||||
#endif /* UIRCD_FEATURE_LIBCONFIG */
|
||||
|
||||
#include "connection.h"
|
||||
#include "logging.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include <libconfig.h>
|
||||
|
||||
#ifndef UIRCD_GUARD_CONFIGURATION
|
||||
#define UIRCD_GUARD_CONFIGURATION
|
||||
|
||||
#define UIRCD_DEFAULT_ADDR "localhost"
|
||||
#define UIRCD_DEFAULT_PORT "6667"
|
||||
#define UIRCD_DEFAULT_NICK "uirc-user"
|
||||
#define UIRCD_DEFAULT_USER "uIRC-user"
|
||||
#define UIRCD_DEFAULT_REAL "uIRC user"
|
||||
#define UIRCD_DEFAULT_ADDR "localhost"
|
||||
#define UIRCD_DEFAULT_PORT "6667"
|
||||
#define UIRCD_DEFAULT_NICK "uirc-user"
|
||||
#define UIRCD_DEFAULT_USER "uIRC-user"
|
||||
#define UIRCD_DEFAULT_REAL "uIRC user"
|
||||
#define UIRCD_DEFAULT_QUITMSG "uIRCd " UIRCD_VERSION
|
||||
|
||||
#ifdef UIRCD_FEATURE_LIBCONFIG
|
||||
|
|
|
@ -18,19 +18,28 @@
|
|||
|
||||
#include "connection.h"
|
||||
|
||||
signed int init_connection(Connection* info)
|
||||
signed int
|
||||
init_connection(Connection* info)
|
||||
{
|
||||
int sockfd, getaddrres, connectres;
|
||||
int sockfd, getaddrres, connectres;
|
||||
struct addrinfo* conn;
|
||||
if ((getaddrres = getaddrinfo(info->data.address, info->data.service, NULL, &conn)) != 0) {
|
||||
freeaddrinfo(conn);
|
||||
if (getaddrres != EAI_AGAIN && getaddrres != EAI_NONAME) {
|
||||
LOG(LOG_ERROR, "Failed to get address info for " ADDRFMT ". " ERRNOFMT, info->data.address, info->data.service,
|
||||
gai_strerror(getaddrres), getaddrres);
|
||||
LOG(LOG_ERROR,
|
||||
"Failed to get address info for " ADDRFMT ". " ERRNOFMT,
|
||||
info->data.address,
|
||||
info->data.service,
|
||||
gai_strerror(getaddrres),
|
||||
getaddrres);
|
||||
return INIT_HARDFAIL;
|
||||
} else {
|
||||
LOG(LOG_WARN, "Failed to get address info for " ADDRFMT ". " ERRNOFMT, info->data.address, info->data.service,
|
||||
gai_strerror(getaddrres), getaddrres);
|
||||
LOG(LOG_WARN,
|
||||
"Failed to get address info for " ADDRFMT ". " ERRNOFMT,
|
||||
info->data.address,
|
||||
info->data.service,
|
||||
gai_strerror(getaddrres),
|
||||
getaddrres);
|
||||
return INIT_SOFTFAIL;
|
||||
}
|
||||
}
|
||||
|
@ -43,10 +52,20 @@ signed int init_connection(Connection* info)
|
|||
close(sockfd);
|
||||
freeaddrinfo(conn);
|
||||
if (errno != EADDRNOTAVAIL && errno != ETIMEDOUT && errno != ECONNRESET && errno != ECONNREFUSED) {
|
||||
LOG(LOG_ERROR, "Failed to connect to host " ADDRFMT ". " ERRNOFMT, info->data.address, info->data.service, strerror(errno), errno);
|
||||
LOG(LOG_ERROR,
|
||||
"Failed to connect to host " ADDRFMT ". " ERRNOFMT,
|
||||
info->data.address,
|
||||
info->data.service,
|
||||
strerror(errno),
|
||||
errno);
|
||||
return INIT_HARDFAIL;
|
||||
} else {
|
||||
LOG(LOG_WARN, "Failed to connect to host " ADDRFMT ". " ERRNOFMT, info->data.address, info->data.service, strerror(errno), errno);
|
||||
LOG(LOG_WARN,
|
||||
"Failed to connect to host " ADDRFMT ". " ERRNOFMT,
|
||||
info->data.address,
|
||||
info->data.service,
|
||||
strerror(errno),
|
||||
errno);
|
||||
return INIT_SOFTFAIL;
|
||||
}
|
||||
}
|
||||
|
@ -54,17 +73,22 @@ signed int init_connection(Connection* info)
|
|||
return sockfd;
|
||||
}
|
||||
|
||||
int auto_msg_actions(IRC_Message* message, Connection* connection, Buffer_Info* buf)
|
||||
int
|
||||
auto_msg_actions(IRC_Message* message, Connection* connection, Buffer_Info* buf)
|
||||
{
|
||||
signed long len;
|
||||
time_t ctime = time(NULL);
|
||||
time_t ctime = time(NULL);
|
||||
switch (message->cmd) {
|
||||
case (PING): {
|
||||
LOG(LOG_DEBUG, "Auto-replying to ping \"%s\".", message->args[0]);
|
||||
if ((len = Assm_mesg(buf->buffer, Assm_cmd_PONG(message->args[0], NULL), sizeof(buf->buffer))) > 0)
|
||||
if (flush_buffer(buf->buffer, (size_t)len, buf->fd) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't pong " ADDRFMT ". " ERRNOFMT, connection->data.address, connection->data.service,
|
||||
strerror(errno), errno);
|
||||
if (flush_buffer(buf->buffer, (size_t) len, buf->fd) == -1) {
|
||||
LOG(LOG_WARN,
|
||||
"Couldn't pong " ADDRFMT ". " ERRNOFMT,
|
||||
connection->data.address,
|
||||
connection->data.service,
|
||||
strerror(errno),
|
||||
errno);
|
||||
connection->info.state = CONN_RECONNECTING;
|
||||
return 0;
|
||||
}
|
||||
|
@ -82,12 +106,20 @@ int auto_msg_actions(IRC_Message* message, Connection* connection, Buffer_Info*
|
|||
LOG(LOG_INFO, "Connection established to " ADDRFMT ".", connection->data.address, connection->data.service);
|
||||
connection->info.state = CONN_ACTIVE;
|
||||
for (unsigned int i = 0; connection->data.channels != NULL && connection->data.channels[i] != NULL; i++) {
|
||||
LOG(LOG_VERBOSE, "Auto-joining channel \"%s\" on " ADDRFMT ".", connection->data.channels[i]->name, connection->data.address,
|
||||
LOG(LOG_VERBOSE,
|
||||
"Auto-joining channel \"%s\" on " ADDRFMT ".",
|
||||
connection->data.channels[i]->name,
|
||||
connection->data.address,
|
||||
connection->data.service);
|
||||
if ((len = Assm_mesg(buf->buffer, Assm_cmd_JOIN(connection->data.channels[i]->name, NULL), sizeof(buf->buffer))) > 0) {
|
||||
if (flush_buffer(buf->buffer, (size_t)len, buf->fd) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't auto-join channels \"%s\" " ADDRFMT ". " ERRNOFMT,
|
||||
connection->data.channels[i]->name, connection->data.address, connection->data.service, strerror(errno),
|
||||
if ((len = Assm_mesg(buf->buffer, Assm_cmd_JOIN(connection->data.channels[i]->name, NULL), sizeof(buf->buffer)))
|
||||
> 0) {
|
||||
if (flush_buffer(buf->buffer, (size_t) len, buf->fd) == -1) {
|
||||
LOG(LOG_WARN,
|
||||
"Couldn't auto-join channels \"%s\" " ADDRFMT ". " ERRNOFMT,
|
||||
connection->data.channels[i]->name,
|
||||
connection->data.address,
|
||||
connection->data.service,
|
||||
strerror(errno),
|
||||
errno);
|
||||
connection->info.state = CONN_RECONNECTING;
|
||||
return 0;
|
||||
|
@ -113,10 +145,8 @@ int auto_msg_actions(IRC_Message* message, Connection* connection, Buffer_Info*
|
|||
LOG(LOG_VERBOSE, "Ending capability negotiation on " ADDRFMT ".", connection->data.address, connection->data.port);
|
||||
if ((len = Assm_mesg(buf->buffer, Assm_cmd_CAP_END(), sizeof(buf->buffer))) > 0) {
|
||||
if (flush_buffer(buf->buffer, (size_t)len, buf->fd) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't end capability negotiation on " ADDRFMT ". " ERRNOFMT, connection->data.address,
|
||||
connection->data.port, strerror(errno), errno);
|
||||
connection->state = CONN_RECONNECTING;
|
||||
return 0;
|
||||
LOG(LOG_WARN, "Couldn't end capability negotiation on " ADDRFMT ". " ERRNOFMT,
|
||||
connection->data.address, connection->data.port, strerror(errno), errno); connection->state = CONN_RECONNECTING; return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -124,13 +154,19 @@ int auto_msg_actions(IRC_Message* message, Connection* connection, Buffer_Info*
|
|||
}
|
||||
*/
|
||||
case (ERROR): {
|
||||
LOG(LOG_ERROR, "Received error on connection " ADDRFMT " with the message \"%s\".", connection->data.address,
|
||||
connection->data.service, message->args[0]);
|
||||
LOG(LOG_ERROR,
|
||||
"Received error on connection " ADDRFMT " with the message \"%s\".",
|
||||
connection->data.address,
|
||||
connection->data.service,
|
||||
message->args[0]);
|
||||
break;
|
||||
}
|
||||
case (ERR_NICKNAMEINUSE):
|
||||
case (ERR_NICKCOLLISION): {
|
||||
LOG(LOG_ERROR, "Nickname %s is already taken on " ADDRFMT ".", connection->user.nickname, connection->data.address,
|
||||
LOG(LOG_ERROR,
|
||||
"Nickname %s is already taken on " ADDRFMT ".",
|
||||
connection->user.nickname,
|
||||
connection->data.address,
|
||||
connection->data.service);
|
||||
connection->info.state = CONN_RECONNECTING;
|
||||
break;
|
||||
|
@ -139,7 +175,8 @@ int auto_msg_actions(IRC_Message* message, Connection* connection, Buffer_Info*
|
|||
return 1;
|
||||
}
|
||||
|
||||
ssize_t get_connstr(char* buf, size_t maxlen, Connection* conn)
|
||||
ssize_t
|
||||
get_connstr(char* buf, size_t maxlen, Connection* conn)
|
||||
{
|
||||
int len = 0;
|
||||
LOG(LOG_DEBUG, "Preparing path: " ADDRFMT, conn->data.address, conn->data.service);
|
||||
|
|
|
@ -20,18 +20,19 @@
|
|||
#include "limits.h"
|
||||
#include "logging.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <netdb.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h> // errno
|
||||
#include <limits.h> // ??
|
||||
#include <netdb.h> // getaddrinfo() gai_strerror()
|
||||
#include <stdbool.h> // bool
|
||||
#include <stdio.h> // snprintf()
|
||||
#include <string.h> // strerror()
|
||||
#include <sys/socket.h> // sockaddr connect()
|
||||
#include <sys/types.h> // size_t ssize_t socklen_t
|
||||
#include <unistd.h> // ??
|
||||
|
||||
#define UIRC_IRCV3
|
||||
#define UIRC_HELPERS
|
||||
#include <uirc/uirc.h>
|
||||
#include <uirc/uirc.h> // Assm_mesg Assm_cmd...
|
||||
|
||||
#ifndef UIRCD_GUARD_CONNECTION
|
||||
#define UIRCD_GUARD_CONNECTION
|
||||
|
@ -39,37 +40,39 @@
|
|||
#define INIT_HARDFAIL -1
|
||||
#define INIT_SOFTFAIL -2
|
||||
|
||||
#define CONN_ACTIVE 2
|
||||
#define CONN_REGISTERED 1
|
||||
#define CONN_PENDING 0
|
||||
#define CONN_ACTIVE 2
|
||||
#define CONN_REGISTERED 1
|
||||
#define CONN_PENDING 0
|
||||
#define CONN_RECONNECTING -1
|
||||
#define CONN_CLOSING -2
|
||||
#define CONN_CLOSED -3
|
||||
#define CONN_CLOSING -2
|
||||
#define CONN_CLOSED -3
|
||||
|
||||
typedef struct {
|
||||
char* name;
|
||||
bool joined;
|
||||
bool joined;
|
||||
} Channel;
|
||||
|
||||
typedef struct {
|
||||
char *address, *service, *quitmsg, *path;
|
||||
char * address, *service, *quitmsg, *path;
|
||||
Channel** channels;
|
||||
time_t timeout;
|
||||
} Connection_Data;
|
||||
time_t timeout;
|
||||
} Connection_Data; // Session/Configuration data [persistent]
|
||||
|
||||
typedef struct {
|
||||
char *nickname, *username, *realname, *password;
|
||||
} Connection_User;
|
||||
} Connection_User; // User information [persistent]
|
||||
|
||||
typedef struct {
|
||||
time_t l_ping, l_pong, l_connect, l_message;
|
||||
time_t l_ping, l_pong, l_connect, l_message;
|
||||
signed short state;
|
||||
} Connection_Info;
|
||||
signed int reconinter;
|
||||
bool active;
|
||||
} Connection_Info; // Connection information [temporary]
|
||||
|
||||
typedef struct {
|
||||
char buffer[UIRCD_LIMITS_LINE + 1];
|
||||
char buffer[UIRCD_LIMITS_LINE + 1];
|
||||
size_t append_pos;
|
||||
int fd;
|
||||
int fd;
|
||||
} Buffer_Info;
|
||||
|
||||
typedef struct {
|
||||
|
@ -79,8 +82,8 @@ typedef struct {
|
|||
} Connection;
|
||||
|
||||
signed int init_connection(Connection* info);
|
||||
int auto_msg_actions(IRC_Message* message, Connection* connection, Buffer_Info* buf);
|
||||
ssize_t get_connstr(char* buf, size_t maxlen, Connection* conn);
|
||||
int auto_msg_actions(IRC_Message* message, Connection* connection, Buffer_Info* buf);
|
||||
ssize_t get_connstr(char* buf, size_t maxlen, Connection* conn);
|
||||
|
||||
#endif /* UIRCD_GUARD_CONNECTION */
|
||||
|
||||
|
|
105
src/filesystem.c
105
src/filesystem.c
|
@ -18,13 +18,14 @@
|
|||
|
||||
#include "filesystem.h"
|
||||
|
||||
int mkdir_bottomup(char* path)
|
||||
int
|
||||
mkdir_bottomup(char* path)
|
||||
{
|
||||
if (path == NULL || *path == '\0') return 0;
|
||||
for (char* x = path; x != NULL && *x;) {
|
||||
if ((x = strchr(x, '/')) != NULL) {
|
||||
char save = *(x + 1);
|
||||
*(x + 1) = '\0';
|
||||
*(x + 1) = '\0';
|
||||
if (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
|
||||
if (errno != EEXIST) {
|
||||
*(x + 1) = save;
|
||||
|
@ -40,27 +41,26 @@ int mkdir_bottomup(char* path)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int makeinput(char* path)
|
||||
int
|
||||
makeinput(char* path)
|
||||
{
|
||||
if (path == NULL) return -1;
|
||||
int tmp;
|
||||
LOG(LOG_DEBUG, "Creating FIFO at %s", path);
|
||||
if ((tmp = mkfifo(path, S_IRUSR | S_IWUSR | S_IWGRP)) == 0) {
|
||||
if (mkfifo(path, S_IRUSR | S_IWUSR | S_IWGRP) == 0) {
|
||||
LOG(LOG_VERBOSE, "Created a FIFO pipe for input at %s.", path);
|
||||
} else if (errno != EEXIST) {
|
||||
LOG(LOG_WARN, "Couldn't create FIFO at \"%s\" for input. " ERRNOFMT, path, strerror(errno), errno);
|
||||
return -1;
|
||||
}
|
||||
int fd;
|
||||
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) != -1) {
|
||||
LOG(LOG_DEBUG, "Opened \"%s\" for reading.", path);
|
||||
return fd;
|
||||
} else
|
||||
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't open FIFO pipe \"%s\" for reading. " ERRNOFMT, path, strerror(errno), errno);
|
||||
return -1;
|
||||
} else
|
||||
LOG(LOG_DEBUG, "Opened \"%s\" for reading.", path);
|
||||
return fd;
|
||||
}
|
||||
|
||||
bool write_log(char* path, char* message)
|
||||
bool
|
||||
write_log(char* path, char* message)
|
||||
{
|
||||
FILE* logfile;
|
||||
if ((logfile = fopen(path, "a")) != NULL) {
|
||||
|
@ -72,20 +72,21 @@ bool write_log(char* path, char* message)
|
|||
return 0;
|
||||
}
|
||||
|
||||
bool cleanup_path_names(char* name)
|
||||
bool
|
||||
cleanup_path_names(char* name)
|
||||
{
|
||||
if (name == NULL) return 0;
|
||||
if (!strcmp("..", name) || !strcmp(".", name)) *name = '_';
|
||||
for (; *name; name++) {
|
||||
if (isalpha(*name))
|
||||
*name = (char)tolower(*name);
|
||||
if (isalpha(*name)) *name = (char) tolower(*name);
|
||||
else if (strchr(".+-_#&", *name) == NULL && !isdigit(*name))
|
||||
*name = '_';
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool add_socket_flags(int fd, int flags)
|
||||
bool
|
||||
add_socket_flags(int fd, int flags)
|
||||
{
|
||||
int cflgs;
|
||||
if ((cflgs = fcntl(fd, F_GETFL)) != -1) {
|
||||
|
@ -100,75 +101,3 @@ bool add_socket_flags(int fd, int flags)
|
|||
return 1;
|
||||
}
|
||||
|
||||
ssize_t set_path_elem(char* content, unsigned int nth_elem, bool isdir, PathBuf* buf)
|
||||
{
|
||||
// TODO: Fix this
|
||||
if (buf == NULL || buf->elements[nth_elem].bufpos == NULL) return -1;
|
||||
size_t totallen = 0;
|
||||
for (unsigned long i = 0; i < sizeof(buf->elements) / sizeof(*buf->elements) && buf->elements[i].len > 0; i++) totallen += buf->elements[i].len;
|
||||
ssize_t temp;
|
||||
if ((temp = snprintf(buf->elements[nth_elem].bufpos, sizeof(buf->buf) - totallen, (isdir) ? "%s/" : "%s", content)) == -1) return -2;
|
||||
buf->elements[nth_elem].len = (size_t)temp;
|
||||
if (nth_elem < sizeof(buf->elements) / sizeof(*buf->elements)) {
|
||||
buf->elements[nth_elem + 1].bufpos = buf->elements[nth_elem].bufpos + temp;
|
||||
buf->elements[nth_elem + 1].len = 0;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
signed int prepare_log_path(IRC_Message* message, PathBuf* pathbuffer, Connection* conn)
|
||||
{
|
||||
if (message == NULL || pathbuffer == NULL) return -2;
|
||||
char* reused_strings[] = {"channel", "user", "global"};
|
||||
struct elems {
|
||||
char* name;
|
||||
bool isdir;
|
||||
} elements[] = {{reused_strings[0], true}, {NULL, true}, {"out", false}};
|
||||
char temp[UIRCD_LIMITS_LINE];
|
||||
if (ISCMD(JOIN) || ISCMD(PART) || ISCMD(QUIT) || ISCMD(RPL_USERSSTART) || ISCMD(RPL_USERS) || ISCMD(RPL_NOUSERS) || ISCMD(RPL_ENDOFUSERS)
|
||||
|| ISCMD(RPL_NAMREPLY) || ISCMD(RPL_ENDOFNAMES)) {
|
||||
elements[0].name = reused_strings[2];
|
||||
elements[1].name = "events";
|
||||
elements[1].isdir = false;
|
||||
} else if (ISCMD(MOTD) || ISCMD(RPL_MOTD) || ISCMD(RPL_MOTDSTART) || ISCMD(RPL_ENDOFMOTD) || ISCMD(ERR_NOMOTD)) {
|
||||
elements[0].name = reused_strings[2];
|
||||
elements[1].name = "motd";
|
||||
elements[1].isdir = false;
|
||||
} else if (ISCMD(PING) || ISCMD(PONG)) {
|
||||
elements[0].name = reused_strings[2];
|
||||
elements[1].name = "pings";
|
||||
elements[1].isdir = false;
|
||||
} else if (ISCMD(RPL_LIST) || ISCMD(RPL_LISTEND)) {
|
||||
elements[0].name = reused_strings[2];
|
||||
elements[1].name = "channels";
|
||||
elements[1].isdir = false;
|
||||
} else if (ISCMD(PRIVMSG) || ISCMD(NOTICE)) {
|
||||
if (message->args[0] == NULL) return -2;
|
||||
if (strcmp(conn->user.nickname, message->args[0]) == 0 && message->name.nick != NULL) {
|
||||
elements[0].name = reused_strings[1];
|
||||
strncpy(temp, message->name.nick, sizeof(temp));
|
||||
} else if (*message->args[0] == '#' || *message->args[0] == '&' || *message->args[0] == '+' || *message->args[0] == '!') {
|
||||
strncpy(temp, message->args[0], sizeof(temp));
|
||||
} else
|
||||
return 0; // TODO: Parse patterns as well
|
||||
cleanup_path_names(temp);
|
||||
elements[1].name = temp;
|
||||
} else if (ISCMD(RPL_TOPIC) || ISCMD(RPL_NOTOPIC)) {
|
||||
if (message->args[0] == NULL || message->args[1] == NULL) return -2;
|
||||
if (message->args[2] != NULL)
|
||||
strncpy(temp, message->args[1], sizeof(temp));
|
||||
else
|
||||
strncpy(temp, message->args[0], sizeof(temp));
|
||||
cleanup_path_names(temp);
|
||||
elements[1].name = temp;
|
||||
elements[2].name = "topic";
|
||||
} else
|
||||
return 0;
|
||||
for (unsigned long i = 0; i < sizeof(elements) / sizeof(*elements); i++) {
|
||||
if (set_path_elem(elements[i].name, (unsigned int)i + 1, elements[i].isdir, pathbuffer) <= 0) return -1;
|
||||
if (!elements[i].isdir) return 1;
|
||||
if (!mkdir_bottomup(pathbuffer->buf)) return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,43 +16,28 @@
|
|||
* along with uIRCd. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define UIRC_IRCV3
|
||||
#include <uirc/types.h>
|
||||
|
||||
#include "connection.h"
|
||||
#include "limits.h"
|
||||
#include "logging.h"
|
||||
|
||||
#include <ctype.h> // isalpha() isdigit()
|
||||
#include <errno.h> // errno
|
||||
#include <fcntl.h> // fnctl()
|
||||
#include <limits.h> // ??
|
||||
#include <stdbool.h> // bool
|
||||
#include <stdio.h> // fopen() FILE fprintf()
|
||||
#include <string.h> // strerror()
|
||||
#include <sys/stat.h> // mkfifo() mkdir()
|
||||
#include <sys/types.h> // size_t ssize_t mode_t
|
||||
|
||||
#ifndef UIRCD_GUARD_FILESYSTEM
|
||||
#define UIRCD_GUARD_FILESYSTEM
|
||||
|
||||
#define ISCMD(CMD) (message->cmd == (CMD))
|
||||
|
||||
typedef struct {
|
||||
char* bufpos;
|
||||
size_t len;
|
||||
} PathBufElem;
|
||||
typedef struct {
|
||||
char buf[PATH_MAX];
|
||||
PathBufElem elements[5];
|
||||
} PathBuf;
|
||||
|
||||
int mkdir_bottomup(char* path);
|
||||
int makeinput(char* path);
|
||||
int mkdir_bottomup(char* path);
|
||||
int makeinput(char* path);
|
||||
bool write_log(char* path, char* message);
|
||||
bool cleanup_path_names(char* name);
|
||||
bool add_socket_flags(int fd, int flags);
|
||||
ssize_t set_path_elem(char* content, unsigned int nth_elem, bool isdir, PathBuf* buf);
|
||||
signed int prepare_log_path(IRC_Message* message, PathBuf* pathbuffer, Connection* conn);
|
||||
|
||||
#endif /* UIRCD_GUARD_FILESYSTEM */
|
||||
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
* along with uIRCd. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <limits.h> // HOST_NAME_MAX PATH_MAX ...
|
||||
|
||||
#ifndef UIRCD_GUARD_LIMITS
|
||||
#define UIRCD_GUARD_LIMITS
|
||||
|
||||
|
|
|
@ -18,6 +18,6 @@
|
|||
|
||||
#include "logging.h"
|
||||
|
||||
int loglevel = LOG_FATAL;
|
||||
const char logchars[] = {[LOG_DEBUG] = 'D', [LOG_VERBOSE] = 'V', [LOG_INFO] = 'I', [LOG_WARN] = 'W', [LOG_ERROR] = 'E', [LOG_FATAL] = 'F'};
|
||||
int loglevel = LOG_FATAL;
|
||||
const char logchars[] = { [LOG_DEBUG] = 'D', [LOG_VERBOSE] = 'V', [LOG_INFO] = 'I', [LOG_WARN] = 'W', [LOG_ERROR] = 'E', [LOG_FATAL] = 'F' };
|
||||
|
||||
|
|
|
@ -21,21 +21,21 @@
|
|||
#ifndef UIRCD_GUARD_LOGGING
|
||||
#define UIRCD_GUARD_LOGGING
|
||||
|
||||
#define LOG_FATAL 0
|
||||
#define LOG_ERROR 1
|
||||
#define LOG_WARN 2
|
||||
#define LOG_INFO 3
|
||||
#define LOG_FATAL 0
|
||||
#define LOG_ERROR 1
|
||||
#define LOG_WARN 2
|
||||
#define LOG_INFO 3
|
||||
#define LOG_VERBOSE 4
|
||||
#define LOG_DEBUG 5
|
||||
#define LOG_DEBUG 5
|
||||
|
||||
#define ERRNOFMT "%s (%i)"
|
||||
#define ADDRFMT "%s:%s"
|
||||
#define ADDRFMT "%s:%s"
|
||||
|
||||
#define LOG(LEVEL, FORMAT, ...) \
|
||||
#define LOG(LEVEL, FORMAT, ...) \
|
||||
if (LEVEL <= loglevel) fprintf(stderr, "[%c:L%i] " FORMAT "\n", logchars[LEVEL], __LINE__, __VA_ARGS__)
|
||||
|
||||
extern const char logchars[];
|
||||
extern int loglevel;
|
||||
extern int loglevel;
|
||||
|
||||
#endif /* UIRCD_GUARD_LOGGING */
|
||||
|
||||
|
|
219
src/main.c
219
src/main.c
|
@ -18,10 +18,12 @@
|
|||
|
||||
#include "main.h"
|
||||
|
||||
int parse_args(int argc, char** argv, Connection* conn)
|
||||
int
|
||||
parse_args(int argc, char** argv, Connection* conn)
|
||||
{
|
||||
int c;
|
||||
while ((c = getopt(argc, argv,
|
||||
while ((c = getopt(argc,
|
||||
argv,
|
||||
"V:d:a:p:t:N:U:R:P:q:c:"
|
||||
#ifdef UIRCD_FEATURE_LIBCONFIG
|
||||
"C:"
|
||||
|
@ -33,7 +35,7 @@ int parse_args(int argc, char** argv, Connection* conn)
|
|||
case 'd': allocate_copy(&conn->data.path, optarg); break;
|
||||
case 'a': allocate_copy(&conn->data.address, optarg); break;
|
||||
case 'p': allocate_copy(&conn->data.service, optarg); break;
|
||||
case 't': conn->data.timeout = (unsigned int)atoi(optarg); break;
|
||||
case 't': conn->data.timeout = (unsigned int) atoi(optarg); break;
|
||||
case 'N': allocate_copy(&conn->user.nickname, optarg); break;
|
||||
case 'U': allocate_copy(&conn->user.username, optarg); break;
|
||||
case 'R': allocate_copy(&conn->user.realname, optarg); break;
|
||||
|
@ -53,28 +55,29 @@ int parse_args(int argc, char** argv, Connection* conn)
|
|||
return 1;
|
||||
}
|
||||
|
||||
void print_help(void)
|
||||
void
|
||||
print_help(void)
|
||||
{
|
||||
struct help {
|
||||
char arg;
|
||||
char arg;
|
||||
char* type;
|
||||
char* desc;
|
||||
char* def;
|
||||
} arg_list[] = {{'a', "host", "Host to connect to (IP/Hostname)", UIRCD_DEFAULT_ADDR},
|
||||
{'p', "serv", "Service (port for TCP/UDP)", UIRCD_DEFAULT_PORT},
|
||||
{'N', "str", "Nickname for registration", UIRCD_DEFAULT_NICK},
|
||||
{'U', "str", "Username for registration", UIRCD_DEFAULT_USER},
|
||||
{'R', "str", "Realname for registration", UIRCD_DEFAULT_REAL},
|
||||
{'P', "str", "Password for registration", NULL},
|
||||
{'d', "str", "Directory for logs", "current dir"},
|
||||
{'q', "str", "Quit message", UIRCD_DEFAULT_QUITMSG},
|
||||
{'t', "uint", "Timeout duration", "30 seconds"},
|
||||
{'V', "int", "Log level", "0 [LOG_FATAL]"},
|
||||
} arg_list[] = { { 'a', "host", "Host to connect to (IP/Hostname)", UIRCD_DEFAULT_ADDR },
|
||||
{ 'p', "serv", "Service (port for TCP/UDP)", UIRCD_DEFAULT_PORT },
|
||||
{ 'N', "str", "Nickname for registration", UIRCD_DEFAULT_NICK },
|
||||
{ 'U', "str", "Username for registration", UIRCD_DEFAULT_USER },
|
||||
{ 'R', "str", "Realname for registration", UIRCD_DEFAULT_REAL },
|
||||
{ 'P', "str", "Password for registration", NULL },
|
||||
{ 'd', "str", "Directory for logs", "current dir" },
|
||||
{ 'q', "str", "Quit message", UIRCD_DEFAULT_QUITMSG },
|
||||
{ 't', "uint", "Timeout duration", "20 seconds" },
|
||||
{ 'V', "int", "Log level", "0 [LOG_FATAL]" },
|
||||
#ifdef UIRCD_FEATURE_LIBCONFIG
|
||||
{'C', "path", "Configuration path", "${XDG_CONFIG_HOME:-~/.config}/uircd/main.conf"},
|
||||
{ 'C', "path", "Configuration path", "${XDG_CONFIG_HOME:-~/.config}/uircd/main.conf" },
|
||||
#endif /* UIRCD_FEATURE_LIBCONFIG */
|
||||
{'v', NULL, "Print version information", NULL},
|
||||
{'h', NULL, "Print this help message", NULL}};
|
||||
{ 'v', NULL, "Print version information", NULL },
|
||||
{ 'h', NULL, "Print this help message", NULL } };
|
||||
printf("usage: uircd -a <host> [ <options> ... ]\n");
|
||||
for (size_t i = 0; i < sizeof(arg_list) / sizeof(*arg_list); i++) {
|
||||
printf(" -%c", arg_list[i].arg);
|
||||
|
@ -85,20 +88,18 @@ void print_help(void)
|
|||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
{
|
||||
srand((unsigned int)time(NULL));
|
||||
IRC_Message buffer;
|
||||
struct {
|
||||
Buffer_Info recv, send, fifo;
|
||||
} buf;
|
||||
PathBuf filebuf;
|
||||
signed int reconinter = 0;
|
||||
time_t ctime;
|
||||
bool active = true;
|
||||
struct timespec sleep = {0, 50000000L};
|
||||
Connection connection = {0};
|
||||
Connection connection = { 0 };
|
||||
time_t ctime;
|
||||
struct timespec sleep = { 0, 50000000L };
|
||||
|
||||
srand((unsigned int) time(NULL));
|
||||
if (!set_config_defaults(&connection)) {
|
||||
LOG(LOG_FATAL, "Couldn't allocate memory for configuration variables. " ERRNOFMT, strerror(errno), errno);
|
||||
return EXIT_FAILURE;
|
||||
|
@ -118,17 +119,24 @@ int main(int argc, char* argv[])
|
|||
}
|
||||
for (;;) {
|
||||
ctime = time(NULL);
|
||||
if (!active) nanosleep(&sleep, NULL);
|
||||
active = false;
|
||||
if (!connection.info.active) nanosleep(&sleep, NULL);
|
||||
connection.info.active = false;
|
||||
if (!run || connection.info.state == CONN_CLOSING) {
|
||||
signed long temp;
|
||||
if ((temp = Assm_mesg(buf.send.buffer, Assm_cmd_QUIT(connection.data.quitmsg), sizeof(buf.send.buffer))) > 0) {
|
||||
buf.send.append_pos = (unsigned long)temp;
|
||||
LOG(LOG_VERBOSE, "Sending a QUIT message to " ADDRFMT " containing \"%s\".", connection.data.address,
|
||||
connection.data.service, connection.data.quitmsg);
|
||||
buf.send.append_pos = (unsigned long) temp;
|
||||
LOG(LOG_VERBOSE,
|
||||
"Sending a QUIT message to " ADDRFMT " containing \"%s\".",
|
||||
connection.data.address,
|
||||
connection.data.service,
|
||||
connection.data.quitmsg);
|
||||
if (flush_buffer(buf.send.buffer, buf.send.append_pos, buf.send.fd) == -1)
|
||||
LOG(LOG_WARN, "Couldn't flush send buffer to " ADDRFMT ". " ERRNOFMT, connection.data.address,
|
||||
connection.data.service, strerror(errno), errno);
|
||||
LOG(LOG_WARN,
|
||||
"Couldn't flush send buffer to " ADDRFMT ". " ERRNOFMT,
|
||||
connection.data.address,
|
||||
connection.data.service,
|
||||
strerror(errno),
|
||||
errno);
|
||||
}
|
||||
close(buf.send.fd);
|
||||
close(buf.fifo.fd);
|
||||
|
@ -141,38 +149,21 @@ int main(int argc, char* argv[])
|
|||
close(buf.send.fd);
|
||||
close(buf.fifo.fd);
|
||||
connection.info.state = CONN_PENDING;
|
||||
if (reconinter <= 300) reconinter += 5;
|
||||
if (connection.info.reconinter <= 300) connection.info.reconinter += 5;
|
||||
continue;
|
||||
} else if (connection.info.state == CONN_PENDING) {
|
||||
if (ctime - connection.info.l_connect < reconinter) continue;
|
||||
if (ctime - connection.info.l_connect < connection.info.reconinter) continue;
|
||||
connection.info.l_connect = ctime;
|
||||
filebuf.elements[0].bufpos = filebuf.buf;
|
||||
|
||||
/* Reset all state-dependent values to empty */
|
||||
memset(&buf, '\0', sizeof(buf));
|
||||
buf.recv.fd = -1;
|
||||
buf.send.fd = -1;
|
||||
buf.fifo.fd = -1;
|
||||
connection.info.l_ping = 0;
|
||||
connection.info.l_pong = 0;
|
||||
buf.recv.fd = -1;
|
||||
buf.send.fd = -1;
|
||||
buf.fifo.fd = -1;
|
||||
connection.info.l_ping = 0;
|
||||
connection.info.l_pong = 0;
|
||||
connection.info.l_message = 0;
|
||||
|
||||
/* Prepare first part of path */
|
||||
char tempath[PATH_MAX];
|
||||
if ((get_connstr(tempath, sizeof(tempath), &connection)) > 0) {
|
||||
cleanup_path_names(tempath);
|
||||
if (set_path_elem(tempath, 0, true, &filebuf) > 0 && set_path_elem("global", 1, true, &filebuf) > 0) {
|
||||
if (mkdir_bottomup(filebuf.buf)) {
|
||||
if (set_path_elem("in", 2, false, &filebuf))
|
||||
buf.fifo.fd = makeinput(filebuf.buf);
|
||||
else
|
||||
LOG(LOG_WARN, "%s", "Couldn't append \"in\" to pathname.");
|
||||
}
|
||||
} else
|
||||
LOG(LOG_WARN, "%s", "Couldn't prepare FIFO pathname.");
|
||||
} else
|
||||
LOG(LOG_WARN, "%s", "Couldn't get connection string.");
|
||||
|
||||
if ((buf.send.fd = init_connection(&connection)) > 0) {
|
||||
buf.recv.fd = buf.send.fd;
|
||||
add_socket_flags(buf.send.fd, O_NONBLOCK);
|
||||
|
@ -191,35 +182,54 @@ int main(int argc, char* argv[])
|
|||
*/
|
||||
if (connection.user.password != NULL
|
||||
&& (temp = Assm_mesg(buf.send.buffer, Assm_cmd_PASS(connection.user.password), sizeof(buf.send.buffer))) > 0) {
|
||||
buf.send.append_pos = (size_t)temp;
|
||||
buf.send.append_pos = (size_t) temp;
|
||||
LOG(LOG_VERBOSE, "Sending PASS authentication to " ADDRFMT, connection.data.address, connection.data.service);
|
||||
if (flush_buffer(buf.send.buffer, buf.send.append_pos, buf.send.fd) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't send PASS authentication on " ADDRFMT ". " ERRNOFMT, connection.data.address,
|
||||
connection.data.service, strerror(errno), errno);
|
||||
LOG(LOG_WARN,
|
||||
"Couldn't send PASS authentication on " ADDRFMT ". " ERRNOFMT,
|
||||
connection.data.address,
|
||||
connection.data.service,
|
||||
strerror(errno),
|
||||
errno);
|
||||
connection.info.state = CONN_RECONNECTING;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ((temp = Assm_mesg(buf.send.buffer, Assm_cmd_NICK(connection.user.nickname), sizeof(buf.send.buffer))) > 0) {
|
||||
buf.send.append_pos = (size_t)temp;
|
||||
LOG(LOG_VERBOSE, "Sending a NICK registration to " ADDRFMT " containing \"%s\".", connection.data.address,
|
||||
connection.data.service, connection.user.nickname);
|
||||
buf.send.append_pos = (size_t) temp;
|
||||
LOG(LOG_VERBOSE,
|
||||
"Sending a NICK registration to " ADDRFMT " containing \"%s\".",
|
||||
connection.data.address,
|
||||
connection.data.service,
|
||||
connection.user.nickname);
|
||||
if (flush_buffer(buf.send.buffer, buf.send.append_pos, buf.send.fd) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't register nickname on " ADDRFMT ". " ERRNOFMT, connection.data.address,
|
||||
connection.data.service, strerror(errno), errno);
|
||||
LOG(LOG_WARN,
|
||||
"Couldn't register nickname on " ADDRFMT ". " ERRNOFMT,
|
||||
connection.data.address,
|
||||
connection.data.service,
|
||||
strerror(errno),
|
||||
errno);
|
||||
connection.info.state = CONN_RECONNECTING;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ((temp = Assm_mesg(buf.send.buffer, Assm_cmd_USER(connection.user.username, connection.user.realname, 0),
|
||||
if ((temp = Assm_mesg(buf.send.buffer,
|
||||
Assm_cmd_USER(connection.user.username, connection.user.realname, 0),
|
||||
sizeof(buf.send.buffer)))
|
||||
> 0) {
|
||||
buf.send.append_pos = (size_t)temp;
|
||||
LOG(LOG_VERBOSE, "Sending a USER registration to " ADDRFMT " containing \"%s\".", connection.data.address,
|
||||
connection.data.service, connection.user.realname);
|
||||
buf.send.append_pos = (size_t) temp;
|
||||
LOG(LOG_VERBOSE,
|
||||
"Sending a USER registration to " ADDRFMT " containing \"%s\".",
|
||||
connection.data.address,
|
||||
connection.data.service,
|
||||
connection.user.realname);
|
||||
if (flush_buffer(buf.send.buffer, buf.send.append_pos, buf.send.fd) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't register user and real name on " ADDRFMT ". " ERRNOFMT,
|
||||
connection.data.address, connection.data.service, strerror(errno), errno);
|
||||
LOG(LOG_WARN,
|
||||
"Couldn't register user and real name on " ADDRFMT ". " ERRNOFMT,
|
||||
connection.data.address,
|
||||
connection.data.service,
|
||||
strerror(errno),
|
||||
errno);
|
||||
connection.info.state = CONN_RECONNECTING;
|
||||
continue;
|
||||
}
|
||||
|
@ -233,26 +243,35 @@ int main(int argc, char* argv[])
|
|||
continue;
|
||||
}
|
||||
} else if (connection.info.state == CONN_ACTIVE) {
|
||||
reconinter = 0;
|
||||
connection.info.reconinter = 0;
|
||||
if (connection.info.l_message < ctime - connection.data.timeout
|
||||
&& connection.info.l_pong < connection.info.l_ping - connection.data.timeout) {
|
||||
LOG(LOG_WARN, "Server " ADDRFMT " didn't respond to a PING in time.", connection.data.address,
|
||||
LOG(LOG_WARN,
|
||||
"Server " ADDRFMT " didn't respond to a PING in time.",
|
||||
connection.data.address,
|
||||
connection.data.service);
|
||||
connection.info.state = CONN_RECONNECTING;
|
||||
continue;
|
||||
}
|
||||
if (ctime - connection.info.l_message >= connection.data.timeout / 4
|
||||
&& ctime - connection.info.l_ping >= connection.data.timeout / 4) {
|
||||
char mesg[] = "uIRC PING -- XXXXXX";
|
||||
char mesg[] = "uIRCd PING -- XXXXXX";
|
||||
snprintf(mesg + sizeof(mesg) - 7, 7, "%.6i", rand());
|
||||
signed long temp;
|
||||
if ((temp = Assm_mesg(buf.send.buffer, Assm_cmd_PING(mesg, NULL), sizeof(buf.send.buffer))) > 0) {
|
||||
buf.send.append_pos = (size_t)temp;
|
||||
LOG(LOG_DEBUG, "Sending ping to " ADDRFMT " with message \"%s\"", connection.data.address,
|
||||
connection.data.service, mesg);
|
||||
buf.send.append_pos = (size_t) temp;
|
||||
LOG(LOG_DEBUG,
|
||||
"Sending ping to " ADDRFMT " with message \"%s\"",
|
||||
connection.data.address,
|
||||
connection.data.service,
|
||||
mesg);
|
||||
if (flush_buffer(buf.send.buffer, buf.send.append_pos, buf.send.fd) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't ping " ADDRFMT ". " ERRNOFMT, connection.data.address,
|
||||
connection.data.service, strerror(errno), errno);
|
||||
LOG(LOG_WARN,
|
||||
"Couldn't ping " ADDRFMT ". " ERRNOFMT,
|
||||
connection.data.address,
|
||||
connection.data.service,
|
||||
strerror(errno),
|
||||
errno);
|
||||
connection.info.state = CONN_RECONNECTING;
|
||||
continue;
|
||||
}
|
||||
|
@ -266,32 +285,36 @@ int main(int argc, char* argv[])
|
|||
if ((brd =
|
||||
read(buf.fifo.fd, buf.fifo.buffer + buf.fifo.append_pos, sizeof(buf.fifo.buffer) - buf.fifo.append_pos - 1))
|
||||
> 0) {
|
||||
*(buf.fifo.buffer + (buf.fifo.append_pos += (size_t)brd)) = '\0';
|
||||
*(buf.fifo.buffer + (buf.fifo.append_pos += (size_t) brd)) = '\0';
|
||||
LOG(LOG_DEBUG, "Read %li bytes from FIFO. Now appending at %li", brd, buf.fifo.append_pos);
|
||||
active = true;
|
||||
connection.info.active = true;
|
||||
} else if (brd == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
|
||||
LOG(LOG_ERROR, "Failed to read FIFO input. " ERRNOFMT, strerror(errno), errno);
|
||||
connection.info.state = CONN_RECONNECTING;
|
||||
continue;
|
||||
}
|
||||
|
||||
memset((void*)&buffer, '\0', sizeof(IRC_Message));
|
||||
memset((void*) &buffer, '\0', sizeof(IRC_Message));
|
||||
if ((len = get_buffer_line(buf.fifo.buffer, &buffer)) > 0) {
|
||||
LOG(LOG_DEBUG, "%s", "Tokenized FIFO message successfully.");
|
||||
signed long temp;
|
||||
if ((temp = Assm_mesg(buf.fifo.buffer, &buffer, sizeof(buf.fifo.buffer))) > 0) {
|
||||
if (flush_buffer(buf.fifo.buffer, (size_t)temp, buf.send.fd) == -1) {
|
||||
LOG(LOG_WARN, "Couldn't send FIFO input to " ADDRFMT ". " ERRNOFMT, connection.data.address,
|
||||
connection.data.service, strerror(errno), errno);
|
||||
if (flush_buffer(buf.fifo.buffer, (size_t) temp, buf.send.fd) == -1) {
|
||||
LOG(LOG_WARN,
|
||||
"Couldn't send FIFO input to " ADDRFMT ". " ERRNOFMT,
|
||||
connection.data.address,
|
||||
connection.data.service,
|
||||
strerror(errno),
|
||||
errno);
|
||||
connection.info.state = CONN_RECONNECTING;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
for (long unsigned int x = 0; x < sizeof(buf.fifo.buffer) && *(buf.fifo.buffer + len + x); x++)
|
||||
*(buf.fifo.buffer + x) = *(buf.fifo.buffer + x + len);
|
||||
buf.fifo.append_pos -= (unsigned long)len;
|
||||
buf.fifo.append_pos -= (unsigned long) len;
|
||||
*(buf.fifo.buffer + buf.fifo.append_pos) = '\0';
|
||||
active = true;
|
||||
connection.info.active = true;
|
||||
} else if (len == -1)
|
||||
connection.info.state = CONN_RECONNECTING;
|
||||
}
|
||||
|
@ -300,16 +323,16 @@ int main(int argc, char* argv[])
|
|||
ssize_t brd, len;
|
||||
/* Buffer reader */
|
||||
if ((brd = read(buf.recv.fd, buf.recv.buffer + buf.recv.append_pos, sizeof(buf.recv.buffer) - buf.recv.append_pos - 1)) > 0) {
|
||||
*(buf.recv.buffer + (buf.recv.append_pos += (size_t)brd)) = '\0';
|
||||
*(buf.recv.buffer + (buf.recv.append_pos += (size_t) brd)) = '\0';
|
||||
LOG(LOG_DEBUG, "Read %li bytes from socket buffer. Now appending at %li", brd, buf.recv.append_pos);
|
||||
active = true;
|
||||
connection.info.active = true;
|
||||
} else if (brd == -1 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
|
||||
LOG(LOG_ERROR, "Failed to read inbound traffic. " ERRNOFMT, strerror(errno), errno);
|
||||
connection.info.state = CONN_RECONNECTING;
|
||||
continue;
|
||||
}
|
||||
|
||||
memset((void*)&buffer, '\0', sizeof(IRC_Message));
|
||||
memset((void*) &buffer, '\0', sizeof(IRC_Message));
|
||||
if ((len = get_buffer_line(buf.recv.buffer, &buffer)) > 0) {
|
||||
connection.info.l_message = ctime;
|
||||
char datebuf[25];
|
||||
|
@ -317,24 +340,12 @@ int main(int argc, char* argv[])
|
|||
Assm_tag_timestamp(datebuf, sizeof(datebuf), ctime);
|
||||
buffer.tags.time.value = datebuf;
|
||||
}
|
||||
if (set_path_elem("global", 1, true, &filebuf) > 0 && set_path_elem("out", 2, false, &filebuf) > 0) {
|
||||
LOG(LOG_DEBUG, "Writing message to global path %s.", filebuf.buf);
|
||||
signed long temp;
|
||||
if ((temp = Assm_mesg(buf.send.buffer, &buffer, sizeof(buf.send.buffer))) > 0)
|
||||
write_log(filebuf.buf, buf.send.buffer);
|
||||
}
|
||||
if (prepare_log_path(&buffer, &filebuf, &connection) == 1) {
|
||||
LOG(LOG_DEBUG, "Writing message to path %s.", filebuf.buf);
|
||||
signed long temp;
|
||||
if ((temp = Assm_mesg(buf.send.buffer, &buffer, sizeof(buf.send.buffer))) > 0)
|
||||
write_log(filebuf.buf, buf.send.buffer);
|
||||
}
|
||||
if (!auto_msg_actions(&buffer, &connection, &buf.send)) continue;
|
||||
for (long unsigned int x = 0; x < sizeof(buf.recv.buffer) && *(buf.recv.buffer + len + x); x++)
|
||||
*(buf.recv.buffer + x) = *(buf.recv.buffer + x + len);
|
||||
buf.recv.append_pos -= (unsigned long)len;
|
||||
buf.recv.append_pos -= (unsigned long) len;
|
||||
*(buf.recv.buffer + buf.recv.append_pos) = '\0';
|
||||
active = true;
|
||||
connection.info.active = true;
|
||||
} else if (len == -1)
|
||||
connection.info.state = CONN_RECONNECTING;
|
||||
}
|
||||
|
|
14
src/main.h
14
src/main.h
|
@ -16,11 +16,6 @@
|
|||
* along with uIRCd. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "buffer.h"
|
||||
#include "configuration.h"
|
||||
#include "connection.h"
|
||||
|
@ -29,14 +24,19 @@
|
|||
#include "memory.h"
|
||||
#include "signal.h"
|
||||
|
||||
#include <stdio.h> // printf()
|
||||
#include <stdlib.h> // srand()
|
||||
#include <sys/types.h> // time_t size_t
|
||||
#include <unistd.h> // getopt() chdir() close()
|
||||
|
||||
#define UIRC_IRCV3
|
||||
#define UIRC_HELPERS
|
||||
#include <uirc/uirc.h>
|
||||
#include <uirc/uirc.h> // IRC_Message
|
||||
|
||||
#ifndef UIRCD_GUARD_MAIN
|
||||
#define UIRCD_GUARD_MAIN
|
||||
|
||||
int parse_args(int argc, char** argv, Connection* conn);
|
||||
int parse_args(int argc, char** argv, Connection* conn);
|
||||
void print_help(void);
|
||||
|
||||
#endif /* UIRCD_GUARD_MAIN */
|
||||
|
|
|
@ -25,14 +25,15 @@
|
|||
* If it isn't NULL, the string var is copied to a allocated block stored in *ptr
|
||||
* NOTE: This could be expanded to have uses outside of strings but that's not required (yet)
|
||||
*/
|
||||
int allocate_copy(char** ptr, const char* const var)
|
||||
int
|
||||
allocate_copy(char** ptr, const char* const var)
|
||||
{
|
||||
if (var == NULL) LOG(LOG_DEBUG, "%s.", "Freeing pointer because provided variable has the address of NULL");
|
||||
if (*ptr != NULL) LOG(LOG_DEBUG, "%s.", "Deallocating already allocated pointer for replacement");
|
||||
free(*ptr);
|
||||
*ptr = NULL;
|
||||
if (var == NULL) return 1;
|
||||
if ((*ptr = (char*)malloc(sizeof(char) * (strlen(var) + 1))) != NULL) {
|
||||
if ((*ptr = (char*) malloc(sizeof(char) * (strlen(var) + 1))) != NULL) {
|
||||
strcpy(*ptr, var);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -18,9 +18,8 @@
|
|||
|
||||
#include "logging.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h> // malloc() free()
|
||||
#include <string.h> // strcpy()
|
||||
|
||||
#ifndef UIRCD_GUARD_MEMORY
|
||||
#define UIRCD_GUARD_MEMORY
|
||||
|
|
14
src/signal.c
14
src/signal.c
|
@ -19,11 +19,17 @@
|
|||
#include "signal.h"
|
||||
|
||||
sig_atomic_t volatile run = true;
|
||||
void stop_loop(int sig, siginfo_t* info, void* ucontext) { run = 0; }
|
||||
int setup_signals(void)
|
||||
|
||||
void
|
||||
stop_loop(int sig, siginfo_t* info, void* ucontext)
|
||||
{
|
||||
int res = 1;
|
||||
struct sigaction sa = {.sa_flags = SA_SIGINFO, .sa_sigaction = stop_loop};
|
||||
run = 0;
|
||||
}
|
||||
int
|
||||
setup_signals(void)
|
||||
{
|
||||
int res = 1;
|
||||
struct sigaction sa = { .sa_flags = SA_SIGINFO, .sa_sigaction = stop_loop };
|
||||
sigemptyset(&sa.sa_mask);
|
||||
if (sigaction(SIGINT, &sa, NULL) == -1) {
|
||||
LOG(LOG_WARN, "sigaction() failed. Children won't respond to signal SIGINT. " ERRNOFMT, strerror(errno), errno);
|
||||
|
|
|
@ -16,14 +16,14 @@
|
|||
* along with uIRCd. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "logging.h"
|
||||
|
||||
#include <errno.h> // errno
|
||||
#include <signal.h> // sig_atomic_t
|
||||
#include <stdbool.h> // true, false
|
||||
#include <stdio.h> // NULL
|
||||
#include <errno.h> // errno
|
||||
#include <string.h> // strerror()
|
||||
|
||||
#include "logging.h"
|
||||
|
||||
#ifndef UIRCD_GUARD_SIGNAL
|
||||
#define UIRCD_GUARD_SIGNAL
|
||||
|
||||
|
@ -32,7 +32,7 @@
|
|||
extern sig_atomic_t volatile run;
|
||||
|
||||
void stop_loop(int sig, siginfo_t* info, void* ucontext);
|
||||
int setup_signals(void);
|
||||
int setup_signals(void);
|
||||
|
||||
#endif /* UIRCD_GUARD_SIGNAL */
|
||||
|
||||
|
|
Reference in New Issue