Skip to content

Commit

Permalink
Expose new API added in 1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ueno committed Aug 27, 2015
1 parent bd7b965 commit 46533d6
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
109 changes: 109 additions & 0 deletions ext/gpgme/gpgme_n.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,54 @@ rb_s_gpgme_get_passphrase_cb (VALUE dummy, VALUE vctx, VALUE rpassfunc,
return Qnil;
}

#if defined(GPGME_VERSION_NUMBER) && GPGME_VERSION_NUMBER >= 0x010600
static gpgme_error_t
status_cb (void *hook, const char *keyword, const char *args)
{
VALUE vcb = (VALUE)hook, vstatusfunc, vhook_value;

vstatusfunc = RARRAY_PTR(vcb)[0];
vhook_value = RARRAY_PTR(vcb)[1];

rb_funcall (vstatusfunc, rb_intern ("call"), 3,
vhook_value,
keyword ? rb_str_new2 (keyword) : Qnil,
args ? rb_str_new2 (args) : Qnil);
return gpgme_err_make (GPG_ERR_SOURCE_USER_1, GPG_ERR_NO_ERROR);
}

static VALUE
rb_s_gpgme_set_status_cb (VALUE dummy, VALUE vctx, VALUE vstatusfunc,
VALUE vhook_value)
{
gpgme_ctx_t ctx;
VALUE vcb = rb_ary_new ();

rb_ary_push (vcb, vstatusfunc);
rb_ary_push (vcb, vhook_value);
/* Keep a reference to avoid GC. */
rb_iv_set (vctx, "@status_cb", vcb);

UNWRAP_GPGME_CTX(vctx, ctx);
if (!ctx)
rb_raise (rb_eArgError, "released ctx");
gpgme_set_status_cb (ctx, status_cb, (void*)vcb);
return Qnil;
}

static VALUE
rb_s_gpgme_get_status_cb (VALUE dummy, VALUE vctx, VALUE rstatusfunc,
VALUE rhook_value)
{
VALUE vcb = rb_iv_get (vctx, "@status_cb");

/* No need to call gpgme_get_status_cb. */
rb_ary_store (rstatusfunc, 0, RARRAY_PTR(vcb)[0]);
rb_ary_store (rhook_value, 0, RARRAY_PTR(vcb)[1]);
return Qnil;
}
#endif

static void
progress_cb (void *hook, const char *what, int type, int current, int total)
{
Expand Down Expand Up @@ -709,6 +757,35 @@ rb_s_gpgme_get_pinentry_mode (VALUE dummy, VALUE vctx)
}
#endif

#if defined(GPGME_VERSION_NUMBER) && GPGME_VERSION_NUMBER >= 0x010600
static VALUE
rb_s_gpgme_set_offline (VALUE dummy, VALUE vctx, VALUE vyes)
{
gpgme_ctx_t ctx;

UNWRAP_GPGME_CTX(vctx, ctx);
if (!ctx)
rb_raise (rb_eArgError, "released ctx");

gpgme_set_offline (ctx, vyes == Qtrue);
return Qnil;
}

static VALUE
rb_s_gpgme_get_offline (VALUE dummy, VALUE vctx)
{
gpgme_ctx_t ctx;
int yes;

UNWRAP_GPGME_CTX(vctx, ctx);
if (!ctx)
rb_raise (rb_eArgError, "released ctx");

yes = gpgme_get_offline (ctx);
return yes ? Qtrue : Qfalse;
}
#endif

static VALUE
rb_s_gpgme_op_keylist_start (VALUE dummy, VALUE vctx, VALUE vpattern,
VALUE vsecret_only)
Expand Down Expand Up @@ -2246,6 +2323,16 @@ Init_gpgme_n (void)
rb_s_gpgme_get_progress_cb, 3);
rb_define_module_function (mGPGME, "gpgme_set_locale",
rb_s_gpgme_set_locale, 3);
#if defined(GPGME_VERSION_NUMBER) && GPGME_VERSION_NUMBER >= 0x010600
rb_define_module_function (mGPGME, "gpgme_set_offline",
rb_s_gpgme_set_offline, 2);
rb_define_module_function (mGPGME, "gpgme_get_offline",
rb_s_gpgme_get_offline, 1);
rb_define_module_function (mGPGME, "gpgme_set_status_cb",
rb_s_gpgme_set_status_cb, 3);
rb_define_module_function (mGPGME, "gpgme_get_status_cb",
rb_s_gpgme_get_status_cb, 3);
#endif

/* Key Management */
rb_define_module_function (mGPGME, "gpgme_op_keylist_start",
Expand Down Expand Up @@ -2877,4 +2964,26 @@ Init_gpgme_n (void)
rb_define_const (mGPGME, "GPGME_SPAWN_ALLOW_SET_FG",
INT2FIX(GPGME_SPAWN_ALLOW_SET_FG));
#endif

/* This flag was added in 1.2.0. */
#ifdef GPGME_EXPORT_MODE_EXTERN
rb_define_const (mGPGME, "GPGME_EXPORT_MODE_EXTERN",
INT2FIX(GPGME_EXPORT_MODE_EXTERN));
#endif

/* This flag was added in 1.3.0. */
#ifdef GPGME_EXPORT_MODE_MINIMAL
rb_define_const (mGPGME, "GPGME_EXPORT_MODE_MINIMAL",
INT2FIX(GPGME_EXPORT_MODE_MINIMAL));
#endif

/* These flags were added in 1.6.0. */
#if defined(GPGME_VERSION_NUMBER) && GPGME_VERSION_NUMBER >= 0x010600
rb_define_const (mGPGME, "GPGME_EXPORT_MODE_SECRET",
INT2FIX(GPGME_EXPORT_MODE_SECRET));
rb_define_const (mGPGME, "GPGME_EXPORT_MODE_RAW",
INT2FIX(GPGME_EXPORT_MODE_RAW));
rb_define_const (mGPGME, "GPGME_EXPORT_MODE_PKCS12",
INT2FIX(GPGME_EXPORT_MODE_PKCS12));
#endif
}
14 changes: 14 additions & 0 deletions lib/gpgme/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,20 @@ module GPGME
SPAWN_ALLOW_SET_FG = GPGME_SPAWN_ALLOW_SET_FG
end

if defined?(GPGME_EXPORT_MODE_EXTERN)
EXPORT_MODE_EXTERN = GPGME_EXPORT_MODE_EXTERN
end

if defined?(GPGME_EXPORT_MODE_MINIMAL)
EXPORT_MODE_MINIMAL = GPGME_EXPORT_MODE_MINIMAL
end

if defined?(GPGME_EXPORT_MODE_SECRET)
EXPORT_MODE_SECRET = GPGME_EXPORT_MODE_SECRET
EXPORT_MODE_RAW = GPGME_EXPORT_MODE_RAW
EXPORT_MODE_PKCS12 = GPGME_EXPORT_MODE_PKCS12
end

KEYLIST_MODE_NAMES = {
KEYLIST_MODE_LOCAL => :local,
KEYLIST_MODE_EXTERN => :extern,
Expand Down
37 changes: 37 additions & 0 deletions lib/gpgme/ctx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ class Ctx
# * +:pinentry_mode+ One of: +PINENTRY_MODE_DEFAULT+,
# +PINENTRY_MODE_ASK+, +PINENTRY_MODE_CANCEL+,
# +PINENTRY_MODE_ERROR+, or +PINENTRY_MODE_LOOPBACK+.
# * +:offline+ if set to true, dirmngr will not contact external services
# * +:password+ password of the passphrased password being used.
# * +:passphrase_callback+ A callback function. See {#set_passphrase_callback}.
# * +:passphrase_callback_value+ An object passed to passphrase_callback.
# * +:progress_callback+ A callback function. See {#set_progress_callback}.
# * +:progress_callback_value+ An object passed to progress_callback.
# * +:status_callback+ A callback function. See {#set_status_callback}.
# * +:status_callback_value+ An object passed to status_callback.
#
# @example
# ctx = GPGME::Ctx.new
Expand All @@ -51,6 +54,7 @@ def self.new(options = {})
ctx.textmode = options[:textmode] if options[:textmode]
ctx.keylist_mode = options[:keylist_mode] if options[:keylist_mode]
ctx.pinentry_mode = options[:pinentry_mode] if options[:pinentry_mode]
ctx.offline = options[:offline] if options[:offline]

if options[:password]
ctx.set_passphrase_callback GPGME::Ctx.method(:pass_function),
Expand All @@ -65,6 +69,10 @@ def self.new(options = {})
ctx.set_progress_callback options[:progress_callback],
options[:progress_callback_value]
end
if options[:status_callback]
ctx.set_status_callback options[:status_callback],
options[:status_callback_value]
end

if block_given?
begin
Expand Down Expand Up @@ -153,6 +161,18 @@ def pinentry_mode
GPGME::gpgme_get_pinentry_mode(self)
end

# Change the default behaviour of the dirmngr that might require
# connections to external services.
def offline=(mode)
GPGME::gpgme_set_offline(self, mode)
mode
end

# Return the current offline mode.
def offline
GPGME::gpgme_get_offline(self)
end

##
# Passphrase and progress callbacks
##
Expand Down Expand Up @@ -223,6 +243,23 @@ def set_progress_callback(progfunc, hook_value = nil)
end
alias set_progress_cb set_progress_callback

# Set the status callback with given hook value.
# +statusfunc+ should respond to +call+ with 3 arguments.
#
# * +obj+ the parameter +:status_callback_value+ passed when creating
# the {GPGME::Ctx} object.
# * +keyword+ the name of the status message
# * +args+ any arguments for the status message
#
# def status_function(obj, keyword, args)
# $stderr.puts("#{keyword} #{args}")
# return 0
# end
def set_status_callback(statusfunc, hook_value = nil)
GPGME::gpgme_set_status_cb(self, statusfunc, hook_value)
end
alias set_status_cb set_status_callback

##
# Searching and iterating through keys. Used by {GPGME::Key.find}
##
Expand Down

0 comments on commit 46533d6

Please sign in to comment.