Skip to content
Merged
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
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ if get_option('tests')
'tests/unittests/command/test_cmd_ac.c',
'tests/unittests/command/test_cmd_disconnect.c',
'tests/unittests/command/test_cmd_mucping.c',
'tests/unittests/ui/test_window_list.c',
'tests/unittests/unittests.c',
)

Expand Down
4 changes: 3 additions & 1 deletion src/command/cmd_ac.c
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,8 @@ cmd_ac_reset(ProfWin* window)
win_reset_search_attempts();
win_close_reset_search_attempts();
plugins_reset_autocomplete();

wins_reset_url_ac();
}

void
Expand Down Expand Up @@ -4037,7 +4039,7 @@ _url_autocomplete(ProfWin* window, const gchar* const input, gboolean previous)
return result;
}

if (window->type == WIN_CHAT || window->type == WIN_MUC || window->type == WIN_PRIVATE) {
if (window && window->layout && window->layout->buffer) {
result = autocomplete_param_with_func(input, "/url open", wins_get_url, previous, window);
if (result) {
return result;
Expand Down
3 changes: 2 additions & 1 deletion src/command/cmd_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2521,7 +2521,8 @@ static const struct cmd_t command_defs[] = {
{ "save", cmd_url_save })
CMD_TAGS(
CMD_TAG_CHAT,
CMD_TAG_GROUPCHAT)
CMD_TAG_GROUPCHAT,
CMD_TAG_UI)
CMD_SYN(
"/url open <url>",
"/url save <url> [<path>]")
Expand Down
10 changes: 5 additions & 5 deletions src/command/cmd_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9563,8 +9563,8 @@ _url_external_method(const char* cmd_template, const char* url, gchar* filename)
gboolean
cmd_url_open(ProfWin* window, const gchar* const command, gchar** args)
{
if (window->type != WIN_CHAT && window->type != WIN_MUC && window->type != WIN_PRIVATE) {
cons_show_error("url open not supported in this window");
if (!window || !window->layout || !window->layout->buffer) {
cons_show_error("`/url open` is not supported in this window.");
return TRUE;
}

Expand All @@ -9575,7 +9575,7 @@ cmd_url_open(ProfWin* window, const gchar* const command, gchar** args)
}

// reset autocompletion to start from latest url and not where we left of
autocomplete_reset(window->urls_ac);
wins_reset_url_ac();

auto_gchar gchar* scheme = g_uri_parse_scheme(url);
if (scheme == NULL) {
Expand Down Expand Up @@ -9606,7 +9606,7 @@ cmd_url_open(ProfWin* window, const gchar* const command, gchar** args)
gboolean
cmd_url_save(ProfWin* window, const gchar* const command, gchar** args)
{
if (window->type != WIN_CHAT && window->type != WIN_MUC && window->type != WIN_PRIVATE) {
if (!window || !window->layout || !window->layout->buffer) {
cons_show_error("`/url save` is not supported in this window.");
return TRUE;
}
Expand All @@ -9620,7 +9620,7 @@ cmd_url_save(ProfWin* window, const gchar* const command, gchar** args)
gchar* path = args[2]; // might be NULL, intentionally skip NULL check

// reset autocompletion to start from latest url and not where we left of
autocomplete_reset(window->urls_ac);
wins_reset_url_ac();

auto_gchar gchar* scheme = g_uri_parse_scheme(url);
if (scheme == NULL) {
Expand Down
1 change: 0 additions & 1 deletion src/ui/chatwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ chatwin_incoming_msg(ProfChatWin* chatwin, ProfMessage* message, gboolean win_cr
}

if (!message->is_mam) {
wins_add_urls_ac(window, message, FALSE);
wins_add_quotes_ac(window, message->plain, FALSE);

if (prefs_get_boolean(PREF_BEEP)) {
Expand Down
1 change: 0 additions & 1 deletion src/ui/mucwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,6 @@ mucwin_incoming_msg(ProfMucWin* mucwin, const ProfMessage* const message, GSList
const gchar* ch = get_enc_char(message->enc, mucwin->message_char);

win_insert_last_read_position_marker((ProfWin*)mucwin, mucwin->roomjid);
wins_add_urls_ac(window, message, FALSE);
wins_add_quotes_ac(window, message->plain, FALSE);

if (g_slist_length(mentions) > 0) {
Expand Down
1 change: 0 additions & 1 deletion src/ui/privwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ privwin_incoming_msg(ProfPrivateWin* privatewin, ProfMessage* message)
}
}

wins_add_urls_ac(window, message, FALSE);
wins_add_quotes_ac(window, message->plain, TRUE);

if (prefs_get_boolean(PREF_BEEP)) {
Expand Down
1 change: 0 additions & 1 deletion src/ui/win_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ typedef struct prof_win_t
win_type_t type;
win_scroll_state_t scroll_state;
ProfLayout* layout;
Autocomplete urls_ac;
Autocomplete quotes_ac;
GHashTable* warned_jids;
} ProfWin;
Expand Down
2 changes: 0 additions & 2 deletions src/ui/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,6 @@ win_print_history(ProfWin* window, const ProfMessage* const message)
auto_gchar gchar* display_name = get_display_name(message, &flags);
const gchar* ch = get_show_char(message->enc);

wins_add_urls_ac(window, message, FALSE);
wins_add_quotes_ac(window, message->plain, FALSE);
int y_start_pos = getcury(window->layout->win);
_win_print_internal(window, ch, 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, message->plain, NULL);
Expand All @@ -1633,7 +1632,6 @@ win_print_old_history(ProfWin* window, const ProfMessage* const message)
const gchar* ch = get_show_char(message->enc);

int y_start_pos = getcury(window->layout->win);
wins_add_urls_ac(window, message, TRUE);
wins_add_quotes_ac(window, message->plain, TRUE);
_win_print_internal(window, ch, 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, message->plain, NULL);
buffer_prepend(window->layout->buffer, ch, 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, message->from_jid->barejid, message->plain, NULL, message->id, y_start_pos, getcury(window->layout->win));
Expand Down
87 changes: 52 additions & 35 deletions src/ui/window_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "config/theme.h"
#include "plugins/plugins.h"
#include "ui/ui.h"
#include "ui/buffer.h"
#include "ui/window_list.h"
#include "xmpp/xmpp.h"
#include "xmpp/roster_list.h"
Expand All @@ -35,6 +36,8 @@ static GList *keys, *values;
static int current;
static Autocomplete wins_ac;
static Autocomplete wins_close_ac;
static Autocomplete urls_ac = NULL;
static GRegex* url_regex = NULL;

static int _wins_cmp_num(gconstpointer a, gconstpointer b);
static int _wins_get_next_available_num(GList* used);
Expand Down Expand Up @@ -77,6 +80,8 @@ wins_init(void)
wins_close_ac = autocomplete_new();
autocomplete_add(wins_close_ac, "all");
autocomplete_add(wins_close_ac, "read");

urls_ac = autocomplete_new();
}

ProfWin*
Expand Down Expand Up @@ -509,7 +514,6 @@ wins_close_by_num(int i)
}
}
}
autocomplete_free(window->urls_ac);
autocomplete_free(window->quotes_ac);
break;
}
Expand All @@ -523,7 +527,6 @@ wins_close_by_num(int i)
if (mucwin->last_msg_timestamp) {
g_date_time_unref(mucwin->last_msg_timestamp);
}
autocomplete_free(window->urls_ac);
autocomplete_free(window->quotes_ac);
break;
}
Expand All @@ -533,7 +536,6 @@ wins_close_by_num(int i)
plugins_on_win_close(privwin->fulljid, "private");
autocomplete_remove(wins_ac, privwin->fulljid);
autocomplete_remove(wins_close_ac, privwin->fulljid);
autocomplete_free(window->urls_ac);
autocomplete_free(window->quotes_ac);
break;
}
Expand Down Expand Up @@ -616,7 +618,6 @@ wins_new_chat(const gchar* const barejid)
autocomplete_add(wins_close_ac, nick);
}
}
newwin->urls_ac = autocomplete_new();
newwin->quotes_ac = autocomplete_new();

plugins_on_win_open(barejid, "chat");
Expand All @@ -631,7 +632,6 @@ wins_new_muc(const gchar* const roomjid)
_wins_htable_insert(windows, GINT_TO_POINTER(result), newwin);
autocomplete_add(wins_ac, roomjid);
autocomplete_add(wins_close_ac, roomjid);
newwin->urls_ac = autocomplete_new();
newwin->quotes_ac = autocomplete_new();

plugins_on_win_open(roomjid, "muc");
Expand All @@ -657,7 +657,6 @@ wins_new_private(const gchar* const fulljid)
_wins_htable_insert(windows, GINT_TO_POINTER(result), newwin);
autocomplete_add(wins_ac, fulljid);
autocomplete_add(wins_close_ac, fulljid);
newwin->urls_ac = autocomplete_new();
newwin->quotes_ac = autocomplete_new();

plugins_on_win_open(fulljid, "private");
Expand Down Expand Up @@ -1171,6 +1170,8 @@ wins_destroy(void)
g_clear_pointer(&windows, g_hash_table_destroy);
g_clear_pointer(&wins_ac, autocomplete_free);
g_clear_pointer(&wins_close_ac, autocomplete_free);
g_clear_pointer(&urls_ac, autocomplete_free);
g_clear_pointer(&url_regex, g_regex_unref);
}

ProfWin*
Expand Down Expand Up @@ -1238,34 +1239,6 @@ wins_get_next_attention(void)
return NULL;
}

void
wins_add_urls_ac(const ProfWin* const win, const ProfMessage* const message, const gboolean flip)
{
GRegex* regex;
GMatchInfo* match_info;

// https://stackoverflow.com/questions/43588699/regex-for-matching-any-url-character
regex = g_regex_new("(https?|aesgcm)://[\\w\\-.~:/?#\\[\\]@!$&'()*+,;=%]+", 0, 0, NULL);
g_regex_match(regex, message->plain, 0, &match_info);

while (g_match_info_matches(match_info)) {
auto_gchar gchar* word = g_match_info_fetch(match_info, 0);

if (flip) {
autocomplete_add_unsorted(win->urls_ac, word, FALSE);
} else {
autocomplete_add_unsorted(win->urls_ac, word, TRUE);
}
// for people who run profanity a long time, we don't want to waste a lot of memory
autocomplete_remove_older_than_max_reverse(win->urls_ac, 20);

g_match_info_next(match_info, NULL);
}

g_match_info_free(match_info);
g_regex_unref(regex);
}

void
wins_add_quotes_ac(const ProfWin* const win, const gchar* const message, const gboolean flip)
{
Expand All @@ -1279,10 +1252,54 @@ wins_add_quotes_ac(const ProfWin* const win, const gchar* const message, const g
autocomplete_remove_older_than_max_reverse(win->quotes_ac, 20);
}

void
wins_reset_url_ac(void)
{
if (urls_ac) {
autocomplete_clear(urls_ac);
}
}

char*
wins_get_url(const gchar* const search_str, gboolean previous, void* context)
{
ProfWin* win = (ProfWin*)context;

return autocomplete_complete(win->urls_ac, search_str, FALSE, previous);
if (!win || !win->layout || !win->layout->buffer) {
return NULL;
}

if (!urls_ac) {
urls_ac = autocomplete_new();
}

if (autocomplete_length(urls_ac) == 0) {
if (url_regex == NULL) {
url_regex = g_regex_new("(https?|aesgcm)://[\\w\\-.~:/?#\\[\\]@!$&'()*+,;=%]+", G_REGEX_OPTIMIZE, 0, NULL);
}

unsigned int len = buffer_size(win->layout->buffer);
for (unsigned int i = len; i > 0; i--) {
ProfBuffEntry* entry = buffer_get_entry(win->layout->buffer, i - 1);
if (entry && entry->message) {
GMatchInfo* match_info = NULL;
g_regex_match(url_regex, entry->message, 0, &match_info);

GList* msg_urls = NULL;
while (g_match_info_matches(match_info)) {
gchar* word = g_match_info_fetch(match_info, 0);
msg_urls = g_list_prepend(msg_urls, word);
g_match_info_next(match_info, NULL);
}
g_match_info_free(match_info);

for (GList* u = msg_urls; u != NULL; u = g_list_next(u)) {
autocomplete_add_unsorted(urls_ac, (gchar*)u->data, FALSE);
}
g_list_free_full(msg_urls, g_free);
}
}
}

return autocomplete_complete(urls_ac, search_str, FALSE, previous);
}
2 changes: 1 addition & 1 deletion src/ui/window_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ void win_reset_search_attempts(void);
char* win_close_autocomplete(const gchar* const search_str, gboolean previous, void* context);
void win_close_reset_search_attempts(void);

void wins_add_urls_ac(const ProfWin* const win, const ProfMessage* const message, const gboolean flip);
char* wins_get_url(const gchar* const search_str, gboolean previous, void* context);
void wins_reset_url_ac(void);
void wins_add_quotes_ac(const ProfWin* const win, const gchar* const message, const gboolean flip);

#endif
7 changes: 0 additions & 7 deletions tests/unittests/command/test_cmd_otr.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ test_cmd_otr_theirfp_from_wintype(win_type_t wintype)
ProfWin window;
window.type = wintype;
window.layout = NULL;
window.urls_ac = NULL;
window.quotes_ac = NULL;

will_return(connection_get_status, JABBER_CONNECTED);
Expand Down Expand Up @@ -302,7 +301,6 @@ cmd_otr_theirfp__shows__message_when_non_otr_chat_window(void** state)
ProfWin window;
window.type = WIN_CHAT;
window.layout = NULL;
window.urls_ac = NULL;
window.quotes_ac = NULL;
ProfChatWin chatwin;
chatwin.window = window;
Expand Down Expand Up @@ -332,7 +330,6 @@ cmd_otr_theirfp__shows__fingerprint(void** state)
ProfWin window;
window.type = WIN_CHAT;
window.layout = NULL;
window.urls_ac = NULL;
window.quotes_ac = NULL;
ProfChatWin chatwin;
chatwin.window = window;
Expand Down Expand Up @@ -361,7 +358,6 @@ test_cmd_otr_start_from_wintype(win_type_t wintype)
ProfWin window;
window.type = wintype;
window.layout = NULL;
window.urls_ac = NULL;
window.quotes_ac = NULL;

will_return(connection_get_status, JABBER_CONNECTED);
Expand Down Expand Up @@ -401,7 +397,6 @@ cmd_otr_start__shows__message_when_already_started(void** state)
ProfWin window;
window.type = WIN_CHAT;
window.layout = NULL;
window.urls_ac = NULL;
window.quotes_ac = NULL;
ProfChatWin chatwin;
chatwin.window = window;
Expand All @@ -428,7 +423,6 @@ cmd_otr_start__shows__message_when_no_key(void** state)
ProfWin window;
window.type = WIN_CHAT;
window.layout = NULL;
window.urls_ac = NULL;
window.quotes_ac = NULL;
ProfChatWin chatwin;
chatwin.window = window;
Expand All @@ -453,7 +447,6 @@ cmd_otr_start__tests__sends_otr_query_message_to_current_recipeint(void** state)
ProfWin window;
window.type = WIN_CHAT;
window.layout = NULL;
window.urls_ac = NULL;
window.quotes_ac = NULL;
ProfChatWin chatwin;
chatwin.window = window;
Expand Down
14 changes: 14 additions & 0 deletions tests/unittests/ui/stub_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -1457,14 +1457,28 @@ ui_flash(void)
{
}

static GList* mock_buffer_entries = NULL;

void
stub_ui_set_mock_buffer_entries(GList* entries)
{
mock_buffer_entries = entries;
}

unsigned int
buffer_size(ProfBuff buffer)
{
if (mock_buffer_entries) {
return g_list_length(mock_buffer_entries);
}
return 0;
}

ProfBuffEntry*
buffer_get_entry(ProfBuff buffer, unsigned int index)
{
if (mock_buffer_entries) {
return g_list_nth_data(mock_buffer_entries, index);
}
return NULL;
}
1 change: 1 addition & 0 deletions tests/unittests/ui/stub_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ void expect_any_cons_show(void);
void expect_cons_show_error(char* expected);
void expect_any_cons_show_error(void);
void expect_win_println(char* message);
void stub_ui_set_mock_buffer_entries(GList* entries);
Loading
Loading