aboutsummaryrefslogtreecommitdiffstats
path: root/gpgme/edit.c
blob: afc0a355ecb3f08b4482459babb07de02808043c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* edit.c - key edit functions
 *      Copyright (C) 2002 g10 Code GmbH
 *
 * This file is part of GPGME.
 *
 * GPGME is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GPGME is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "util.h"
#include "context.h"
#include "ops.h"


struct edit_result_s
{
  GpgmeEditCb fnc;
  void *fnc_value;
};

void
_gpgme_release_edit_result (EditResult result)
{
  if (!result)
    return;
  xfree (result);
}

void
_gpgme_edit_status_handler (GpgmeCtx ctx, GpgmeStatusCode status, char *args)
{
  _gpgme_passphrase_status_handler (ctx, status, args);
  _gpgme_progress_status_handler (ctx, status, args);

  if (ctx->error)
    return;

  ctx->error = (*ctx->result.edit->fnc) (ctx->result.edit->fnc_value, status, args, NULL);
}

static const char *
command_handler (void *opaque, GpgmeStatusCode status, const char *args)
{
  GpgmeCtx ctx = opaque;
  const char *result;

  result = _gpgme_passphrase_command_handler (ctx, status, args);

  if (!result)
    ctx->error = (*ctx->result.edit->fnc) (ctx->result.edit->fnc_value, status, args, &result);

  return result;
}

static GpgmeError
_gpgme_op_edit_start (GpgmeCtx ctx, int synchronous,
		      GpgmeKey key,
		      GpgmeEditCb fnc, void *fnc_value,
		      GpgmeData out)
{
  GpgmeError err = 0;

  if (!fnc)
    return mk_error (Invalid_Value);

  err = _gpgme_op_reset (ctx, synchronous);
  if (err)
    goto leave;

  assert (!ctx->result.edit);
  ctx->result.edit = xtrymalloc (sizeof *ctx->result.edit);
  if (!ctx->result.edit)
    {
      err = mk_error (Out_Of_Core);
      goto leave;
    }
  ctx->result.edit->fnc = fnc;
  ctx->result.edit->fnc_value = fnc_value;

  /* Check the supplied data.  */
  if (!out || gpgme_data_get_type (out) != GPGME_DATA_TYPE_NONE)
    {
      err = mk_error (Invalid_Value);
      goto leave;
    }
  _gpgme_data_set_mode (out, GPGME_DATA_MODE_IN);

  err = _gpgme_engine_set_command_handler (ctx->engine, command_handler,
					   ctx, out);
  if (err)
    goto leave;

  _gpgme_engine_set_status_handler (ctx->engine, _gpgme_edit_status_handler,
				    ctx);

  _gpgme_engine_set_verbosity (ctx->engine, ctx->verbosity);

  _gpgme_engine_op_edit (ctx->engine, key, out, ctx);

  /* And kick off the process.  */
  err = _gpgme_engine_start (ctx->engine, ctx);
  
 leave:
  if (err)
    {
      ctx->pending = 0; 
      _gpgme_engine_release (ctx->engine);
      ctx->engine = NULL;
    }
  return err;
}

GpgmeError
gpgme_op_edit_start (GpgmeCtx ctx,
		     GpgmeKey key,
		     GpgmeEditCb fnc, void *fnc_value,
		     GpgmeData out)
{
  return _gpgme_op_edit_start (ctx, 0, key, fnc, fnc_value, out);
}

/**
 * gpgme_op_edit:
 * @ctx: The context
 * @key: The key to be edited.
 * @fnc: An edit callback handler.
 * @fnc_value: To be passed to @fnc as first arg.
 * @out: The output.
 * 
 * Return value: 0 on success or an error code.
 **/
GpgmeError
gpgme_op_edit (GpgmeCtx ctx,
	       GpgmeKey key,
	       GpgmeEditCb fnc, void *fnc_value,
	       GpgmeData out)
{
  GpgmeError err = _gpgme_op_edit_start (ctx, 1, key, fnc, fnc_value, out);
  if (!err)
    err = _gpgme_wait_one (ctx);
  return err;
}