Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: /ignore command #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/twc-chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <tox/tox.h>
#include <weechat/weechat-plugin.h>

#include "twc-config.h"
#include "twc-list.h"
#include "twc-message-queue.h"
#include "twc-profile.h"
Expand Down Expand Up @@ -57,6 +58,10 @@ twc_chat_new(struct t_twc_profile *profile, const char *name)
chat->profile = profile;
chat->friend_number = chat->group_number = -1;
chat->nicks = NULL;
chat->ids = NULL;
chat->completion = NULL;
chat->last_search = NULL;
chat->prev_comp = NULL;

size_t full_name_size = strlen(profile->name) + 1 + strlen(name) + 1;
char *full_name = malloc(full_name_size);
Expand Down Expand Up @@ -137,6 +142,8 @@ twc_chat_new_group(struct t_twc_profile *profile, int32_t group_number)
chat->nicklist_group =
weechat_nicklist_add_group(chat->buffer, NULL, NULL, NULL, true);
chat->nicks = weechat_list_new();
chat->ids = weechat_list_new();
chat->completion = weechat_list_new();

weechat_buffer_set(chat->buffer, "nicklist", "1");
}
Expand Down Expand Up @@ -274,6 +281,54 @@ twc_chat_search_buffer(struct t_gui_buffer *buffer)
return NULL;
}

/**
* Set a prefix to a nickname in the nicklist of a chat.
*/
void
twc_chat_update_prefix(struct t_twc_chat *chat, const char *id,
const char *prefix, const char *prefix_color)
{
struct t_gui_nick_group *ptr_group = NULL;
struct t_gui_nick *ptr_nick = NULL;

weechat_nicklist_get_next_item(chat->buffer, &ptr_group, &ptr_nick);
while (ptr_group || ptr_nick)
{
if (ptr_nick)
{
const char *name_field = weechat_nicklist_nick_get_string(
chat->buffer, ptr_nick, "name");
size_t short_id_length =
weechat_config_integer(twc_config_short_id_size);
if (!weechat_strncasecmp(id, name_field + sizeof(char),
short_id_length))
{
weechat_nicklist_nick_set(chat->buffer, ptr_nick, "prefix",
prefix);
weechat_nicklist_nick_set(chat->buffer, ptr_nick,
"prefix_color", prefix_color);
weechat_nicklist_nick_set(chat->buffer, ptr_nick, "color",
"default");
return;
}
}
weechat_nicklist_get_next_item(chat->buffer, &ptr_group, &ptr_nick);
}
}

/**
* Update prefix for a certain nickname structure pointer.
*/
void
twc_chat_update_prefix_by_nick(struct t_gui_buffer *buffer,
struct t_gui_nick *nick, const char *prefix,
const char *prefix_color)
{
weechat_nicklist_nick_set(buffer, nick, "prefix", prefix);
weechat_nicklist_nick_set(buffer, nick, "prefix_color", prefix_color);
weechat_nicklist_nick_set(buffer, nick, "color", "default");
}

/**
* Print a chat message to a chat's buffer.
*/
Expand Down Expand Up @@ -395,6 +450,18 @@ twc_chat_free(struct t_twc_chat *chat)
weechat_list_remove_all(chat->nicks);
weechat_list_free(chat->nicks);
}
if (chat->ids)
{
weechat_list_remove_all(chat->ids);
weechat_list_free(chat->ids);
}
if (chat->completion)
{
weechat_list_remove_all(chat->completion);
weechat_list_free(chat->completion);
}
free(chat->last_search);
free(chat->prev_comp);
free(chat);
}

Expand Down
13 changes: 13 additions & 0 deletions src/twc-chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ struct t_twc_chat

struct t_gui_nick_group *nicklist_group;
struct t_weelist *nicks;
struct t_weelist *ids;
struct t_weelist *completion;
char *last_search;
char *prev_comp;
};

struct t_twc_chat *
Expand All @@ -52,6 +56,15 @@ twc_chat_search_group(struct t_twc_profile *profile, int32_t group_number,
struct t_twc_chat *
twc_chat_search_buffer(struct t_gui_buffer *target_buffer);

void
twc_chat_update_prefix(struct t_twc_chat *chat, const char *id,
const char *prefix, const char *prefix_color);

void
twc_chat_update_prefix_by_nick(struct t_gui_buffer *buffer,
struct t_gui_nick *nick, const char *prefix,
const char *prefix_color);

enum t_twc_rc
twc_chat_set_logging(struct t_twc_chat const *const chat, bool logging);

Expand Down
163 changes: 163 additions & 0 deletions src/twc-commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,154 @@ twc_cmd_send(const void *pointer, void *data, struct t_gui_buffer *buffer,
return WEECHAT_RC_OK;
}

/**
* Custom completion for nicknames in groups.
*/
int
twc_input_complete(const void *pointer, void *data, struct t_gui_buffer *buffer,
const char *command)
{
struct t_twc_chat *chat = twc_chat_search_buffer(buffer);
if (chat && chat->group_number >= 0)
{
const char *input = weechat_buffer_get_string(buffer, "input");
if (!strcmp(input, ""))
return WEECHAT_RC_OK;
size_t last_search_len =
chat->last_search ? strlen(chat->last_search) : 0;
int cmp = strncmp(chat->last_search, input, last_search_len);
if (cmp || !last_search_len)
{
free(chat->last_search);
chat->last_search = strdup(input);
free(chat->prev_comp);
chat->prev_comp = NULL;
twc_starts_with(chat->nicks, chat->last_search, chat->completion);
}

const char *comp =
twc_get_next_completion(chat->completion, chat->prev_comp);
if (!comp)
return WEECHAT_RC_OK;

weechat_buffer_set(buffer, "completion_freeze", "1");

char *terminator = ": "; /* Probably put it in a config */
char temp[strlen(comp) + strlen(terminator) + 1];
sprintf(temp, "%s%s", comp, terminator);
weechat_buffer_set(buffer, "input", temp);

int input_pos = weechat_buffer_get_integer(buffer, "input_length");
int input_pos_str_size = snprintf(NULL, 0, "%d", input_pos);
char input_pos_str[input_pos_str_size + 1];
snprintf(input_pos_str, input_pos_str_size + 1, "%d", input_pos);
weechat_buffer_set(buffer, "input_pos", input_pos_str);

weechat_buffer_set(buffer, "completion_freeze", "0");

free(chat->prev_comp);
chat->prev_comp = strdup(comp);
return WEECHAT_RC_OK_EAT;
}
return WEECHAT_RC_OK;
}
/**
* Update a certain nick's prefix in all opened group chats
*/
void
twc_name_prefix_in_groupchats(struct t_twc_profile *profile, const char *id,
const char *prefix, const char *prefix_color)
{
struct t_twc_list_item *item;
struct t_twc_chat *chat;
size_t index;
twc_list_foreach (profile->chats, index, item)
{
chat = (struct t_twc_chat *)(item->chat);
if ((chat->group_number) >= 0)
{
twc_chat_update_prefix(chat, id, prefix, prefix_color);
}
}
}

/**
* Command /ignore callback.
*/
int
twc_cmd_ignore(const void *pointer, void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
struct t_twc_profile *profile = twc_profile_search_buffer(buffer);
TWC_CHECK_PROFILE(profile);
TWC_CHECK_PROFILE_LOADED(profile);

/* list ignores */
if ((argc == 1) ||
((argc == 2) && (weechat_strcasecmp(argv[1], "list") == 0)))
{
if (!weechat_list_size(profile->ignores))
{
weechat_printf(profile->buffer, "ignore list is empty");
return WEECHAT_RC_OK;
}
weechat_printf(profile->buffer, "ignore list:");
size_t i = 0;
struct t_weelist_item *item;
for (item = weechat_list_get(profile->ignores, 0); item;
item = weechat_list_next(item))
{
weechat_printf(profile->buffer, " [%d] %s", i,
weechat_list_string(item));
i++;
}
return WEECHAT_RC_OK;
}
/* add ignore */

const char *id = argv_eol[2];
struct t_weelist_item *item;
item = twc_is_id_ignored(profile, id);

if (weechat_strcasecmp(argv[1], "add") == 0)
{
WEECHAT_COMMAND_MIN_ARGS(3, "add");
if (!item)
{
weechat_list_add(profile->ignores, id, WEECHAT_LIST_POS_END, NULL);
weechat_printf(profile->buffer,
"%sID '%s' has been added to the ignore list",
weechat_prefix("action"), id);
twc_name_prefix_in_groupchats(profile, id, "-", "yellow");
}
else
weechat_printf(profile->buffer,
"%sID '%s' is already in the ignore list",
weechat_prefix("error"), id);
return WEECHAT_RC_OK;
}

/* delete ignore */
if (weechat_strcasecmp(argv[1], "del") == 0)
{
WEECHAT_COMMAND_MIN_ARGS(3, "del");
if (item)
{
weechat_list_remove(profile->ignores, item);
weechat_printf(profile->buffer,
"%sID '%s' has been deleted from the ignore list",
weechat_prefix("action"), id);
twc_name_prefix_in_groupchats(profile, id, " ", "default");
}
else
weechat_printf(profile->buffer,
"%sthere's no ID '%s' in the ignore list",
weechat_prefix("error"), id);
return WEECHAT_RC_OK;
}
return WEECHAT_RC_ERROR;
}

/**
* Register Tox-WeeChat commands.
*/
Expand Down Expand Up @@ -1421,6 +1569,9 @@ twc_commands_init()

weechat_hook_command_run("/save", twc_cmd_save, NULL, NULL);

weechat_hook_command_run("/input complete_next", twc_input_complete, NULL,
NULL);

weechat_hook_command("status", "change your Tox status", "online|busy|away",
"", NULL, twc_cmd_status, NULL, NULL);

Expand Down Expand Up @@ -1464,4 +1615,16 @@ twc_commands_init()
"%(filename)"
" || %(tox_friend_name)|%(tox_friend_tox_id) %(filename)",
twc_cmd_send, NULL, NULL);
weechat_hook_command("ignore",
"ommit messages from people with certain Tox IDs",
"list"
" || add <ID part>"
" || del <ID part>",
"list: show the list of ignores\n"
"add: add an ID to the list\n"
"del: delete an ID from the list\n",
"list"
" || add %*"
" || del %*",
twc_cmd_ignore, NULL, NULL);
}
Loading