Skip to content

Commit 6c22a5b

Browse files
manu3569ueno
authored andcommitted
Add option to delete secret key without confirmation dialog
1 parent c55a80e commit 6c22a5b

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

ext/gpgme/gpgme_n.c

+32
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,27 @@ rb_s_gpgme_op_delete (VALUE dummy, VALUE vctx, VALUE vkey, VALUE vallow_secret)
14841484
return LONG2NUM(err);
14851485
}
14861486

1487+
/* This method was added in 1.9.1. */
1488+
#if defined(GPGME_VERSION_NUMBER) && GPGME_VERSION_NUMBER >= 0x010901
1489+
static VALUE
1490+
rb_s_gpgme_op_delete_ext (VALUE dummy, VALUE vctx, VALUE vkey, VALUE vflags)
1491+
{
1492+
gpgme_ctx_t ctx;
1493+
gpgme_key_t key;
1494+
gpgme_error_t err;
1495+
1496+
CHECK_KEYLIST_NOT_IN_PROGRESS(vctx);
1497+
1498+
UNWRAP_GPGME_CTX(vctx, ctx);
1499+
if (!ctx)
1500+
rb_raise (rb_eArgError, "released ctx");
1501+
UNWRAP_GPGME_KEY(vkey, key);
1502+
1503+
err = gpgme_op_delete_ext (ctx, key, NUM2INT(vflags));
1504+
return LONG2NUM(err);
1505+
}
1506+
#endif
1507+
14871508
static VALUE
14881509
rb_s_gpgme_op_delete_start (VALUE dummy, VALUE vctx, VALUE vkey,
14891510
VALUE vallow_secret)
@@ -2547,6 +2568,8 @@ Init_gpgme_n (void)
25472568
rb_s_gpgme_op_import_result, 1);
25482569
rb_define_module_function (mGPGME, "gpgme_op_delete",
25492570
rb_s_gpgme_op_delete, 3);
2571+
rb_define_module_function (mGPGME, "gpgme_op_delete_ext",
2572+
rb_s_gpgme_op_delete_ext, 3);
25502573
rb_define_module_function (mGPGME, "gpgme_op_delete_start",
25512574
rb_s_gpgme_op_delete_start, 3);
25522575
rb_define_module_function (mGPGME, "gpgme_op_edit",
@@ -3156,4 +3179,13 @@ Init_gpgme_n (void)
31563179
rb_define_const (mGPGME, "GPGME_EXPORT_MODE_PKCS12",
31573180
INT2FIX(GPGME_EXPORT_MODE_PKCS12));
31583181
#endif
3182+
3183+
/* These flags were added in 1.9.1. */
3184+
#if defined(GPGME_VERSION_NUMBER) && GPGME_VERSION_NUMBER >= 0x010901
3185+
rb_define_const (mGPGME, "GPGME_DELETE_ALLOW_SECRET",
3186+
INT2FIX(GPGME_DELETE_ALLOW_SECRET));
3187+
rb_define_const (mGPGME, "GPGME_DELETE_FORCE",
3188+
INT2FIX(GPGME_DELETE_FORCE));
3189+
#endif
31593190
}
3191+

lib/gpgme/constants.rb

+8
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,12 @@ module GPGME
265265
VALIDITY_FULL => :full,
266266
VALIDITY_ULTIMATE => :ultimate
267267
}
268+
269+
if defined?(GPGME_DELETE_ALLOW_SECRET)
270+
DELETE_ALLOW_SECRET = GPGME_DELETE_ALLOW_SECRET
271+
end
272+
273+
if defined?(GPGME_DELETE_FORCE)
274+
DELETE_FORCE = GPGME_DELETE_FORCE
275+
end
268276
end

lib/gpgme/ctx.rb

+11-2
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,17 @@ def import_result
447447
# Delete the key from the key ring.
448448
# If allow_secret is false, only public keys are deleted,
449449
# otherwise secret keys are deleted as well.
450-
def delete_key(key, allow_secret = false)
451-
err = GPGME::gpgme_op_delete(self, key, allow_secret ? 1 : 0)
450+
# If force is true, the confirmation dialog will not be displayed.
451+
def delete_key(key, allow_secret = false, force = false)
452+
err = nil
453+
if defined?(GPGME::gpgme_op_delete_ext)
454+
flag = 0
455+
flag ^= GPGME::DELETE_ALLOW_SECRET if allow_secret
456+
flag ^= GPGME::DELETE_FORCE if force
457+
err = GPGME::gpgme_op_delete_ext(self, key, flag)
458+
else
459+
err = GPGME::gpgme_op_delete(self, key, allow_secret ? 1 : 0)
460+
end
452461
exc = GPGME::error_to_exception(err)
453462
raise exc if exc
454463
end

lib/gpgme/key.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ def export(options = {})
156156

157157
##
158158
# Delete this key. If it's public, and has a secret one it will fail unless
159-
# +allow_secret+ is specified as true.
160-
def delete!(allow_secret = false)
159+
# +allow_secret+ is specified as true. Suppress the confirmation dialog, if
160+
# +force+ is specified as true.
161+
def delete!(allow_secret = false, force = false)
161162
GPGME::Ctx.new do |ctx|
162-
ctx.delete_key self, allow_secret
163+
ctx.delete_key self, allow_secret, force
163164
end
164165
end
165166

0 commit comments

Comments
 (0)