Merge branch 'mod_chat_history_work'

Conflicts:
	doc/plugins.conf
This commit is contained in:
Jan Vidar Krey
2012-01-03 23:16:08 +01:00
7 changed files with 324 additions and 125 deletions

View File

@@ -689,49 +689,6 @@ static int command_broadcast(struct command_base* cbase, struct hub_user* user,
return 0;
}
static int command_history(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
{
struct cbuffer* buf;
struct linked_list* messages = cbase->hub->chat_history;
char* message;
char* maxlines_str = list_get_first(cmd->args);
int maxlines = 0;
int skiplines = 0;
int total = list_size(messages);
if (total == 0)
return command_status(cbase, user, cmd, cbuf_create_const("No messages."));
buf = cbuf_create(MAX_HELP_MSG);
if (maxlines_str)
maxlines = uhub_atoi(maxlines_str);
if (maxlines <= 0 || maxlines > total)
maxlines = total;
if (maxlines != total)
{
skiplines = total - maxlines;
cbuf_append_format(buf, "*** %s: Displaying %d of %d message%s:", cmd->prefix, maxlines, total, ((total != 1) ? "s" : ""));
}
else
{
cbuf_append_format(buf, "*** %s: Displaying %d message%s:", cmd->prefix, total, ((total != 1) ? "s" : ""));
}
cbuf_append(buf, "\n");
message = (char*) list_get_first(messages);
while (message)
{
if (--skiplines < 0)
cbuf_append(buf, message);
message = (char*) list_get_next(messages);
}
cbuf_append(buf, "\n");
send_message(cbase, user, buf);
return 0;
}
static int command_log(struct command_base* cbase, struct hub_user* user, struct hub_command* cmd)
{
struct cbuffer* buf;
@@ -952,7 +909,6 @@ void commands_builtin_add(struct command_base* cbase)
#endif
ADD_COMMAND("getip", 5, "n", auth_cred_operator, command_getip, "Show IP address for a user" );
ADD_COMMAND("help", 4, "?c",auth_cred_guest, command_help, "Show this help message." );
ADD_COMMAND("history", 7, "?N",auth_cred_guest, command_history, "Show the last chat messages." );
ADD_COMMAND("kick", 4, "n", auth_cred_operator, command_kick, "Kick a user" );
ADD_COMMAND("log", 3, "", auth_cred_operator, command_log, "Display log" );
ADD_COMMAND("mute", 4, "n", auth_cred_operator, command_mute, "Mute user" );

View File

@@ -54,10 +54,10 @@ enum command_parse_status
*/
struct hub_command
{
enum command_parse_status status; /**<<< "Status of the hub_command." */
const char* message; /**<<< "The complete message." */
char* prefix; /**<<< "The prefix extracted from the message." */
struct linked_list* args; /**<<< "List of all parsed arguments from the message. Type depends on expectations." */
enum command_parse_status status; /**<<< "Status of the hub_command." */
command_handler handler; /**<<< "The function handler to call in order to invoke this command." */
const struct hub_user* user; /**<<< "The user who invoked this command." */
void* ptr; /**<<< "A pointer of data which came from struct command_handler" */

View File

@@ -265,6 +265,13 @@ int hub_handle_chat_message(struct hub_info* hub, struct hub_user* u, struct adc
if (!message)
return 0;
message_decoded = adc_msg_unescape(message);
if (!message_decoded)
{
hub_free(message);
return 0;
}
if (!user_is_logged_in(u))
{
hub_free(message);
@@ -290,9 +297,7 @@ int hub_handle_chat_message(struct hub_info* hub, struct hub_user* u, struct adc
}
else
{
message_decoded = adc_msg_unescape(message);
relay = command_invoke(hub->commands, u, message_decoded);
hub_free(message_decoded);
}
}
@@ -307,13 +312,13 @@ int hub_handle_chat_message(struct hub_info* hub, struct hub_user* u, struct adc
plugin_st status = st_default;
if (broadcast)
{
status = plugin_handle_chat_message(hub, u, message, 0);
status = plugin_handle_chat_message(hub, u, message_decoded, 0);
}
else if (private_msg)
{
struct hub_user* target = uman_get_user_by_sid(hub, cmd->target);
if (target)
status = plugin_handle_private_message(hub, u, target, message, 0);
status = plugin_handle_private_message(hub, u, target, message_decoded, 0);
else
relay = 0;
}
@@ -327,39 +332,15 @@ int hub_handle_chat_message(struct hub_info* hub, struct hub_user* u, struct adc
/* adc_msg_remove_named_argument(cmd, "PM"); */
if (broadcast)
{
hub_chat_history_add(hub, u, cmd);
plugin_log_chat_message(hub, u, message, 0);
plugin_log_chat_message(hub, u, message_decoded, 0);
}
ret = route_message(hub, u, cmd);
}
hub_free(message);
hub_free(message_decoded);
return ret;
}
void hub_chat_history_add(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
{
char* msg_esc = adc_msg_get_argument(cmd, 0);
char* message = adc_msg_unescape(msg_esc);
size_t loglen = strlen(message) + strlen(user->id.nick) + 13;
char* log = hub_malloc(loglen + 1);
snprintf(log, loglen, "%s <%s> %s\n", get_timestamp(time(NULL)), user->id.nick, message);
log[loglen] = '\0';
list_append(hub->chat_history, log);
while (list_size(hub->chat_history) > (size_t) hub->config->max_chat_history)
{
char* msg = list_get_first(hub->chat_history);
list_remove(hub->chat_history, msg);
hub_free(msg);
}
hub_free(message);
hub_free(msg_esc);
}
void hub_chat_history_clear(struct hub_info* hub)
{
list_clear(hub->chat_history, &hub_free);
}
void hub_send_support(struct hub_info* hub, struct hub_user* u)
{
if (user_is_connecting(u) || user_is_logged_in(u))
@@ -803,20 +784,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
return 0;
}
hub->chat_history = (struct linked_list*) list_create();
hub->logout_info = (struct linked_list*) list_create();
if (!hub->chat_history)
{
net_con_close(hub->server);
list_destroy(hub->chat_history);
list_destroy(hub->logout_info);
hub_free(hub->recvbuf);
hub_free(hub->sendbuf);
uman_shutdown(hub);
hub_free(hub);
return 0;
}
server_alt_port_start(hub, config);
hub->status = hub_status_running;
@@ -844,8 +812,6 @@ void hub_shutdown_service(struct hub_info* hub)
hub->status = hub_status_stopped;
hub_free(hub->sendbuf);
hub_free(hub->recvbuf);
hub_chat_history_clear(hub);
list_destroy(hub->chat_history);
list_clear(hub->logout_info, &hub_free);
list_destroy(hub->logout_info);
command_shutdown(hub->commands);

View File

@@ -109,7 +109,6 @@ struct hub_info
char* recvbuf; /* Global receive buffer */
char* sendbuf; /* Global send buffer */
struct linked_list* chat_history; /* Chat history */
struct linked_list* logout_info; /* Log of people logging out. */
struct command_base* commands; /* Hub command handler */
@@ -151,16 +150,6 @@ extern int hub_handle_password(struct hub_info* hub, struct hub_user* u, struct
*/
extern int hub_handle_chat_message(struct hub_info* hub, struct hub_user* u, struct adc_message* cmd);
/**
* Add a chat message to the chat history
*/
extern void hub_chat_history_add(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd);
/**
* Clear the chat history.
*/
extern void hub_chat_history_clear(struct hub_info* hub);
/**
* Used internally by hub_handle_info
* @return 1 if nickname is OK, or 0 if nickname is not accepted.