Skip to content

Commit

Permalink
Have proper autocompletion for /software in chat window
Browse files Browse the repository at this point in the history
In console autocomplete from roster.
In muc autocomplete from occupants lits.
In 1:1 regular chat autocomplete from active resources of currently
selected user (new).

Also give a hint (/help resource) how to set the resource should a user choose that way.

Fix #1337
  • Loading branch information
jubalh committed May 14, 2020
1 parent 5cf6ee1 commit 9243655
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
41 changes: 39 additions & 2 deletions src/command/cmd_ac.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ static char* _color_autocomplete(ProfWin *window, const char *const input, gbool
static char* _avatar_autocomplete(ProfWin *window, const char *const input, gboolean previous);
static char* _correction_autocomplete(ProfWin *window, const char *const input, gboolean previous);
static char* _correct_autocomplete(ProfWin *window, const char *const input, gboolean previous);
static char* _software_autocomplete(ProfWin *window, const char *const input, gboolean previous);

static char* _script_autocomplete_func(const char *const prefix, gboolean previous, void *context);

Expand Down Expand Up @@ -1613,7 +1614,7 @@ _cmd_ac_complete_params(ProfWin *window, const char *const input, gboolean previ
assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);
Autocomplete nick_ac = muc_roster_ac(mucwin->roomjid);
if (nick_ac) {
gchar *nick_choices[] = { "/msg", "/info", "/caps", "/software" } ;
gchar *nick_choices[] = { "/msg", "/info", "/caps" } ;

// Remove quote character before and after names when doing autocomplete
char *unquoted = strip_arg_quotes(input);
Expand Down Expand Up @@ -1641,7 +1642,7 @@ _cmd_ac_complete_params(ProfWin *window, const char *const input, gboolean previ
}
free(unquoted);

gchar *resource_choices[] = { "/caps", "/software", "/ping" };
gchar *resource_choices[] = { "/caps", "/ping" };
for (i = 0; i < ARRAY_SIZE(resource_choices); i++) {
result = autocomplete_param_with_func(input, resource_choices[i], roster_fulljid_autocomplete, previous, NULL);
if (result) {
Expand Down Expand Up @@ -1726,6 +1727,7 @@ _cmd_ac_complete_params(ProfWin *window, const char *const input, gboolean previ
g_hash_table_insert(ac_funcs, "/avatar", _avatar_autocomplete);
g_hash_table_insert(ac_funcs, "/correction", _correction_autocomplete);
g_hash_table_insert(ac_funcs, "/correct", _correct_autocomplete);
g_hash_table_insert(ac_funcs, "/software", _software_autocomplete);

int len = strlen(input);
char parsed[len+1];
Expand Down Expand Up @@ -3887,3 +3889,38 @@ _correct_autocomplete(ProfWin *window, const char *const input, gboolean previou

return result;
}

static char*
_software_autocomplete(ProfWin *window, const char *const input, gboolean previous)
{
char *result = NULL;

if (window->type == WIN_CHAT){
ProfChatWin *chatwin = (ProfChatWin*)window;
assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);

GString *search_str = g_string_new("/software ");
g_string_append(search_str, chatwin->barejid);
result = autocomplete_param_with_func(search_str->str, "/software", roster_fulljid_autocomplete, previous, NULL);
g_string_free(search_str, TRUE);
} else if (window->type == WIN_MUC) {
ProfMucWin *mucwin = (ProfMucWin*)window;
assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK);

Autocomplete nick_ac = muc_roster_ac(mucwin->roomjid);

if (nick_ac) {
result = autocomplete_param_with_ac(input, "/software", nick_ac, TRUE, previous);
if (result) {
return result;
}
}
} else {
result = autocomplete_param_with_func(input, "/software", roster_fulljid_autocomplete, previous, NULL);
if (result) {
return result;
}
}

return result;
}
36 changes: 21 additions & 15 deletions src/command/cmd_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3427,6 +3427,22 @@ cmd_caps(ProfWin *window, const char *const command, gchar **args)
return TRUE;
}

static void
_send_software_version_iq_to_fulljid(char *request)
{
Jid *myJid = jid_create(connection_get_fulljid());
Jid *jid = jid_create(request);

if (jid == NULL || jid->fulljid == NULL) {
cons_show("You must provide a full jid to the /software command.");
} else if (g_strcmp0(jid->barejid, myJid->barejid) == 0) {
cons_show("Cannot request software version for yourself.");
} else {
iq_send_software_version(jid->fulljid);
}
jid_destroy(myJid);
jid_destroy(jid);
}

gboolean
cmd_software(ProfWin *window, const char *const command, gchar **args)
Expand Down Expand Up @@ -3458,7 +3474,8 @@ cmd_software(ProfWin *window, const char *const command, gchar **args)
break;
case WIN_CHAT:
if (args[0]) {
cons_show("No parameter needed to /software when in chat.");
_send_software_version_iq_to_fulljid(args[0]);
break;
} else {
ProfChatWin *chatwin = (ProfChatWin*)window;
assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
Expand All @@ -3477,24 +3494,13 @@ cmd_software(ProfWin *window, const char *const command, gchar **args)
iq_send_software_version(fulljid->str);
g_string_free(fulljid, TRUE);
} else {
win_println(window, THEME_DEFAULT, "-", "Unknown resource for /software command.");
win_println(window, THEME_DEFAULT, "-", "Unknown resource for /software command. See /help resource.");
}
break;
}
break;
case WIN_CONSOLE:
if (args[0]) {
Jid *myJid = jid_create(connection_get_fulljid());
Jid *jid = jid_create(args[0]);

if (jid == NULL || jid->fulljid == NULL) {
cons_show("You must provide a full jid to the /software command.");
} else if (g_strcmp0(jid->barejid, myJid->barejid) == 0) {
cons_show("Cannot request software version for yourself.");
} else {
iq_send_software_version(jid->fulljid);
}
jid_destroy(myJid);
jid_destroy(jid);
_send_software_version_iq_to_fulljid(args[0]);
} else {
cons_show("You must provide a jid to the /software command.");
}
Expand Down

0 comments on commit 9243655

Please sign in to comment.