Add optional pgp public key autoimport

Refactor `p_gpg_list_keys`
Add `/pgp autoimport` command,
it's not described in XEP-0027, but used in some clients,
such as PSI, Pidgin. It will autoimport keys received with
`/pgp sendpub`, in plain text as a message, or using features,
provided in other clients. It doesn't autoassign them, but shows
command to assign, letting user to decide.
Improve documentation for some preexisting functions
Add contact argument to `/pgp sendpub`
This commit is contained in:
John Hernandez
2023-06-30 14:33:17 +02:00
parent 349e4cb322
commit 36784738fc
10 changed files with 269 additions and 52 deletions

View File

@@ -238,6 +238,7 @@ static Autocomplete reconnect_ac;
static Autocomplete pgp_ac;
static Autocomplete pgp_log_ac;
static Autocomplete pgp_sendfile_ac;
static Autocomplete pgp_autoimport_ac;
static Autocomplete ox_ac;
static Autocomplete ox_log_ac;
#endif
@@ -911,6 +912,7 @@ cmd_ac_init(void)
autocomplete_add(pgp_ac, "char");
autocomplete_add(pgp_ac, "sendfile");
autocomplete_add(pgp_ac, "sendpub");
autocomplete_add(pgp_ac, "autoimport");
pgp_log_ac = autocomplete_new();
autocomplete_add(pgp_log_ac, "on");
@@ -921,6 +923,10 @@ cmd_ac_init(void)
autocomplete_add(pgp_sendfile_ac, "on");
autocomplete_add(pgp_sendfile_ac, "off");
pgp_autoimport_ac = autocomplete_new();
autocomplete_add(pgp_autoimport_ac, "on");
autocomplete_add(pgp_autoimport_ac, "off");
ox_ac = autocomplete_new();
autocomplete_add(ox_ac, "keys");
autocomplete_add(ox_ac, "contacts");
@@ -1675,6 +1681,7 @@ cmd_ac_reset(ProfWin* window)
autocomplete_reset(pgp_ac);
autocomplete_reset(pgp_log_ac);
autocomplete_reset(pgp_sendfile_ac);
autocomplete_reset(pgp_autoimport_ac);
autocomplete_reset(ox_ac);
autocomplete_reset(ox_log_ac);
#endif
@@ -1857,6 +1864,7 @@ cmd_ac_uninit(void)
autocomplete_free(pgp_ac);
autocomplete_free(pgp_log_ac);
autocomplete_free(pgp_sendfile_ac);
autocomplete_free(pgp_autoimport_ac);
autocomplete_free(ox_ac);
autocomplete_free(ox_log_ac);
#endif
@@ -2737,6 +2745,11 @@ _pgp_autocomplete(ProfWin* window, const char* const input, gboolean previous)
if (found) {
return found;
}
found = autocomplete_param_with_func(input, "/pgp sendpub", roster_contact_autocomplete, previous, NULL);
if (found) {
return found;
}
}
found = autocomplete_param_with_ac(input, "/pgp log", pgp_log_ac, TRUE, previous);
@@ -2749,6 +2762,11 @@ _pgp_autocomplete(ProfWin* window, const char* const input, gboolean previous)
return found;
}
found = autocomplete_param_with_ac(input, "/pgp autoimport", pgp_autoimport_ac, TRUE, previous);
if (found) {
return found;
}
gboolean result;
auto_gcharv gchar** args = parse_args(input, 2, 3, &result);
if ((strncmp(input, "/pgp", 4) == 0) && (result == TRUE)) {

View File

@@ -1706,7 +1706,7 @@ static const struct cmd_t command_defs[] = {
"/pgp log on|off|redact",
"/pgp char <char>",
"/pgp sendfile on|off",
"/pgp sendpub")
"/pgp sendpub [<contact>]")
CMD_DESC(
"Open PGP commands to manage keys, and perform PGP encryption during chat sessions. "
"See the /account command to set your own PGP key.")
@@ -1721,7 +1721,8 @@ static const struct cmd_t command_defs[] = {
{ "log redact", "Log PGP encrypted messages, but replace the contents with [redacted]. This is the default." },
{ "char <char>", "Set the character to be displayed next to PGP encrypted messages." },
{ "sendfile on|off", "Allow /sendfile to send unencrypted files while otherwise using PGP." },
{ "sendpub", "Used in chat. Sends a message to the current recipient with your PGP public key." })
{ "autoimport on|off", "Autoimport PGP keys from messages." },
{ "sendpub [<contact>]", "Sends a message to the current recipient with your PGP public key, current contact will be used if not specified." })
CMD_EXAMPLES(
"/pgp log off",
"/pgp setkey odin@valhalla.edda BA19CACE5A9592C5",

View File

@@ -7361,7 +7361,9 @@ cmd_pgp(ProfWin* window, const char* const command, gchar** args)
}
cons_bad_cmd_usage(command);
return TRUE;
} else if (g_strcmp0(args[0], "log") == 0) {
}
if (g_strcmp0(args[0], "log") == 0) {
char* choice = args[1];
if (g_strcmp0(choice, "on") == 0) {
prefs_set_string(PREF_PGP_LOG, "on");
@@ -7384,6 +7386,11 @@ cmd_pgp(ProfWin* window, const char* const command, gchar** args)
return TRUE;
}
if (g_strcmp0(args[0], "autoimport") == 0) {
_cmd_set_boolean_preference(args[1], "PGP keys autoimport from messages", PREF_PGP_PUBKEY_AUTOIMPORT);
return TRUE;
}
if (g_strcmp0(args[0], "keys") == 0) {
GHashTable* keys = p_gpg_list_keys();
if (!keys || g_hash_table_size(keys) == 0) {
@@ -7494,7 +7501,7 @@ cmd_pgp(ProfWin* window, const char* const command, gchar** args)
}
if (window->type != WIN_CHAT && args[1] == NULL) {
cons_show("You must be in a regular chat window to start PGP encryption.");
cons_show("You must set recipient in an argument or be in a regular chat window to start PGP encryption.");
return TRUE;
}
@@ -7587,11 +7594,17 @@ cmd_pgp(ProfWin* window, const char* const command, gchar** args)
}
if (g_strcmp0(args[0], "sendpub") == 0) {
if (window->type != WIN_CHAT) {
cons_show_error("Please, use this command only in chat windows.");
jabber_conn_status_t conn_status = connection_get_status();
if (conn_status != JABBER_CONNECTED) {
cons_show("You must be connected to share your PGP public key.");
return TRUE;
}
ProfChatWin* chatwin = (ProfChatWin*)window;
if (window->type != WIN_CHAT && args[1] == NULL) {
cons_show("You must set recipient in an argument or use this command in a regular chat window to share your PGP key.");
return TRUE;
}
ProfAccount* account = accounts_get_account(session_get_account_name());
if (account->pgp_keyid == NULL) {
@@ -7605,7 +7618,28 @@ cmd_pgp(ProfWin* window, const char* const command, gchar** args)
account_free(account);
return TRUE;
}
ProfChatWin* chatwin = NULL;
if (args[1]) {
char* contact = args[1];
char* barejid = roster_barejid_from_name(contact);
if (barejid == NULL) {
barejid = contact;
}
chatwin = wins_get_chat(barejid);
if (!chatwin) {
chatwin = chatwin_new(barejid);
}
ui_focus_win((ProfWin*)chatwin);
} else {
chatwin = (ProfChatWin*)window;
assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
}
cl_ev_send_msg(chatwin, pubkey, NULL);
win_update_entry_message((ProfWin*)chatwin, chatwin->last_msg_id, "[you shared your PGP key]");
cons_show("PGP key has been shared with %s.", chatwin->barejid);
account_free(account);
return TRUE;