Made sure the logging plugin logs on a format that looks almost the same as we used to have in the past.
This commit is contained in:
parent
0c6a58d35a
commit
c2b7ecd49c
|
@ -255,7 +255,7 @@ $(plugin_auth_sqlite_TARGET): $(plugin_auth_sqlite_SOURCES) $(libutils_OBJECTS)
|
|||
$(plugin_example_TARGET): $(plugin_example_SOURCES)
|
||||
$(MSG_CC) $(CC) -shared -fPIC -o $@ $^ $(CFLAGS)
|
||||
|
||||
$(plugin_logging_TARGET): $(plugin_logging_SOURCES)
|
||||
$(plugin_logging_TARGET): $(plugin_logging_SOURCES) $(libutils_OBJECTS) $(libadc_common_OBJECTS) src/network/network.o
|
||||
$(MSG_CC) $(CC) -shared -fPIC -o $@ $^ $(CFLAGS)
|
||||
|
||||
$(adcrush_BINARY): $(adcrush_OBJECTS) $(libuhub_OBJECTS) $(libutils_OBJECTS) $(libadc_common_OBJECTS) $(libadc_client_OBJECTS)
|
||||
|
|
|
@ -78,6 +78,7 @@ static void convert_user_type(struct plugin_user* puser, struct hub_user* user)
|
|||
puser->sid = user->id.sid;
|
||||
puser->nick = user->id.nick;
|
||||
puser->cid = user->id.cid;
|
||||
puser->user_agent = user->user_agent;
|
||||
puser->addr = user->id.addr;
|
||||
puser->credentials = user->credentials;
|
||||
}
|
||||
|
|
|
@ -46,6 +46,7 @@ struct plugin_user
|
|||
unsigned int sid;
|
||||
const char* nick;
|
||||
const char* cid;
|
||||
const char* user_agent;
|
||||
struct ip_addr_encap addr;
|
||||
enum auth_credentials credentials;
|
||||
};
|
||||
|
|
|
@ -2,32 +2,95 @@
|
|||
* This is a minimal example plugin for uhub.
|
||||
*/
|
||||
|
||||
// #include "uhub.h"
|
||||
#include "system.h"
|
||||
#include "adc/adcconst.h"
|
||||
#include "adc/sid.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/ipcalc.h"
|
||||
#include "plugin_api/handle.h"
|
||||
|
||||
struct ip_addr_encap;
|
||||
|
||||
plugin_st log_connect(struct plugin_handle* plugin, struct ip_addr_encap* addr)
|
||||
struct log_data
|
||||
{
|
||||
return st_default;
|
||||
char* logfile;
|
||||
int fd;
|
||||
};
|
||||
|
||||
|
||||
static void set_error_message(struct plugin_handle* plugin, const char* msg)
|
||||
{
|
||||
plugin->error_msg = msg;
|
||||
}
|
||||
|
||||
void log_user_login(struct plugin_handle* plugin, struct plugin_user* user)
|
||||
|
||||
static struct log_data* log_open(struct plugin_handle* plugin, const char* config)
|
||||
{
|
||||
printf("login: \"%s\"\n", user->nick);
|
||||
struct log_data* data = (struct log_data*) hub_malloc(sizeof(struct log_data));
|
||||
data->logfile = strdup(config);
|
||||
data->fd = open(data->logfile, O_CREAT | O_APPEND | O_NOATIME | O_LARGEFILE | O_WRONLY, 0664);
|
||||
if (data->fd == -1)
|
||||
{
|
||||
set_error_message(plugin, "Unable to open log file!");
|
||||
hub_free(data->logfile);
|
||||
hub_free(data);
|
||||
return NULL;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
void log_user_logout(struct plugin_handle* plugin, struct plugin_user* user)
|
||||
static void log_close(struct log_data* data)
|
||||
{
|
||||
printf("logout: \"%s\"\n", user->nick);
|
||||
hub_free(data->logfile);
|
||||
close(data->fd);
|
||||
hub_free(data);
|
||||
}
|
||||
|
||||
plugin_st log_change_nick(struct plugin_handle* plugin, struct plugin_user* user, const char* new_nick)
|
||||
static void log_message(struct log_data* data, const char *format, ...)
|
||||
{
|
||||
printf("\"%s\" -> \"%s\"\n", user->nick, new_nick);
|
||||
return st_default;
|
||||
static char logmsg[1024];
|
||||
struct tm *tmp;
|
||||
time_t t;
|
||||
va_list args;
|
||||
ssize_t size = 0;
|
||||
|
||||
t = time(NULL);
|
||||
tmp = localtime(&t);
|
||||
strftime(logmsg, 32, "%Y-%m-%d %H:%M:%S ", tmp);
|
||||
|
||||
va_start(args, format);
|
||||
size = vsnprintf(logmsg + 20, 1004, format, args);
|
||||
va_end(args);
|
||||
|
||||
write(data->fd, logmsg, size + 20);
|
||||
fdatasync(data->fd);
|
||||
}
|
||||
|
||||
static void log_user_login(struct plugin_handle* plugin, struct plugin_user* user)
|
||||
{
|
||||
const char* cred = auth_cred_to_string(user->credentials);
|
||||
const char* addr = ip_convert_to_string(&user->addr);
|
||||
|
||||
log_message(plugin->ptr, "LoginOK %s/%s %s \"%s\" (%s) \"%s\"\n", sid_to_string(user->sid), user->cid, addr, user->nick, cred, user->user_agent);
|
||||
}
|
||||
|
||||
static void log_user_login_error(struct plugin_handle* plugin, struct plugin_user* user, const char* reason)
|
||||
{
|
||||
const char* addr = ip_convert_to_string(&user->addr);
|
||||
log_message(plugin->ptr, "LoginError %s/%s %s \"%s\" (%s) \"%s\"\n", sid_to_string(user->sid), user->cid, addr, user->nick, reason, user->user_agent);
|
||||
}
|
||||
|
||||
static void log_user_logout(struct plugin_handle* plugin, struct plugin_user* user, const char* reason)
|
||||
{
|
||||
const char* addr = ip_convert_to_string(&user->addr);
|
||||
log_message(plugin->ptr, "Logout %s/%s %s \"%s\" (%s) \"%s\"\n", sid_to_string(user->sid), user->cid, addr, user->nick, reason, user->user_agent);
|
||||
}
|
||||
|
||||
static void log_change_nick(struct plugin_handle* plugin, struct plugin_user* user, const char* new_nick)
|
||||
{
|
||||
const char* addr = ip_convert_to_string(&user->addr);
|
||||
log_message(plugin->ptr, "NickChange %s/%s %s \"%s\" -> \"%s\"\n", sid_to_string(user->sid), user->cid, addr, user->nick, new_nick);
|
||||
}
|
||||
|
||||
int plugin_register(struct plugin_handle* plugin, const char* config)
|
||||
{
|
||||
|
@ -38,20 +101,22 @@ int plugin_register(struct plugin_handle* plugin, const char* config)
|
|||
plugin->plugin_api_version = PLUGIN_API_VERSION;
|
||||
plugin->plugin_funcs_size = sizeof(struct plugin_funcs);
|
||||
memset(&plugin->funcs, 0, sizeof(struct plugin_funcs));
|
||||
/*
|
||||
plugin->funcs.on_connect = log_connect;
|
||||
|
||||
plugin->funcs.on_user_login = log_user_login;
|
||||
plugin->funcs.on_user_login_error = log_user_login_error;
|
||||
plugin->funcs.on_user_logout = log_user_logout;
|
||||
plugin->funcs.on_user_change_nick = log_change_nick;
|
||||
*/
|
||||
puts("* plugin register");
|
||||
plugin->funcs.on_user_nick_change = log_change_nick;
|
||||
|
||||
plugin->ptr = log_open(plugin, config);
|
||||
if (!plugin->ptr)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int plugin_unregister(struct plugin_handle* plugin)
|
||||
{
|
||||
/* No need to do anything! */
|
||||
puts("* plugin unregister");
|
||||
log_close(plugin->ptr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue