0c814bd262be3b202ed49418451561107378a2ba
[gpgme.git] / gpgme / gpgme.c
1 /* gpgme.c -  GnuPG Made Easy
2  *      Copyright (C) 2000 Werner Koch (dd9jn)
3  *
4  * This file is part of GPGME.
5  *
6  * GPGME is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GPGME is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <assert.h>
25
26 #include "util.h"
27 #include "context.h"
28 #include "ops.h"
29
30 #define my_isdigit(a)  ( (a) >='0' && (a) <= '9' )
31 #define my_isxdigit(a) ( my_isdigit((a))               \
32                          || ((a) >= 'A' && (a) <= 'F') \
33                          || ((a) >= 'f' && (a) <= 'f') )
34
35 /**
36  * gpgme_new:
37  * @r_ctx: Returns the new context
38  * 
39  * Create a new context to be used with most of the other GPGME
40  * functions.  Use gpgme_release_contect() to release all resources
41  *
42  * Return value: An error code 
43  **/
44 GpgmeError
45 gpgme_new (GpgmeCtx *r_ctx)
46 {
47     GpgmeCtx c;
48
49     c = xtrycalloc ( 1, sizeof *c );
50     if (!c)
51         return mk_error (Out_Of_Core);
52     c->verbosity = 1;
53     c->use_armor = 1; /* fixme: reset this to 0 */
54     *r_ctx = c;
55
56     return 0;
57 }
58
59 /**
60  * gpgme_release:
61  * @c: Context to be released. 
62  * 
63  * Release all resources associated with the given context.
64  **/
65 void
66 gpgme_release ( GpgmeCtx c )
67 {
68     if (!c)
69         return;
70     _gpgme_gpg_release ( c->gpg ); 
71     _gpgme_release_result ( c );
72     _gpgme_key_release ( c->tmp_key );
73     gpgme_data_release ( c->help_data_1 );
74     gpgme_data_release ( c->notation );
75     /* fixme: release the key_queue */
76     xfree (c);
77 }
78
79
80 void
81 _gpgme_release_result ( GpgmeCtx c )
82 {
83     switch (c->result_type) {
84       case RESULT_TYPE_NONE:
85         break;
86       case RESULT_TYPE_VERIFY:
87         _gpgme_release_verify_result ( c->result.verify );
88         break;
89       case RESULT_TYPE_DECRYPT:
90         _gpgme_release_decrypt_result ( c->result.decrypt );
91         break;
92       case RESULT_TYPE_SIGN:
93         _gpgme_release_sign_result ( c->result.sign );
94         break;
95     }
96
97     c->result.verify = NULL;
98     c->result_type = RESULT_TYPE_NONE;
99 }
100
101
102 /**
103  * gpgme_cancel:
104  * @c: the context
105  * 
106  * Cancel the current operation.  It is not guaranteed that it will work for
107  * all kinds of operations.  It is especially useful in a passphrase callback
108  * to stop the system from asking another time for the passphrase.
109  **/
110
111 void
112 gpgme_cancel (GpgmeCtx c)
113 {
114     c->cancel = 1;
115 }
116
117 /**
118  * gpgme_get_notation:
119  * @c: the context 
120  * 
121  * If there is notation data available from the last signature check, this
122  * function may be used to return this notation data as a string.  The string
123  * is an XML represantaton of that data embedded in a %<notation> container.
124  * 
125  * Return value: An XML string or NULL if no notation data is available.
126  **/
127 char *
128 gpgme_get_notation ( GpgmeCtx c )
129 {
130     if ( !c->notation )
131         return NULL;
132     return _gpgme_data_get_as_string ( c->notation );
133 }
134
135
136 /**
137  * gpgme_set_armor:
138  * @c: the contect 
139  * @yes: boolean value to set or clear that flag
140  * 
141  * Enable or disable the use of an ascii armor for all output.  
142  **/
143 void
144 gpgme_set_armor ( GpgmeCtx c, int yes )
145 {
146     if ( !c )
147         return; /* oops */
148     c->use_armor = yes;
149 }
150
151 /**
152  * gpgme_set_textmode:
153  * @c: the context 
154  * @yes: boolean flag whether textmode should be enabled
155  * 
156  * Enable or disable the use of the special textmode.  Textmode is for example
157  * used for MIME (RFC2015) signatures
158  **/
159 void
160 gpgme_set_textmode ( GpgmeCtx c, int yes )
161 {
162     if ( !c )
163         return; /* oops */
164     c->use_textmode = yes;
165 }
166
167 /**
168  * gpgme_set_passphrase_cb:
169  * @c: the context 
170  * @cb: A callback function
171  * @cb_value: The value passed to the callback function
172  * 
173  * This function sets a callback function to be used to pass a passphrase
174  * to gpg. The preferred way to handle this is by using the gpg-agent, but
175  * because that beast is not ready for real use, you can use this passphrase
176  * thing.
177  *
178  * The callback function is defined as:
179  * <literal>
180  * typedef const char *(*GpgmePassphraseCb)(void*cb_value,
181  *                                          const char *desc,
182  *                                          void *r_hd);
183  * </literal>
184  * and called whenever gpgme needs a passphrase. DESC will have a nice
185  * text, to be used to prompt for the passphrase and R_HD is just a parameter
186  * to be used by the callback it self.  Becuase the callback returns a const
187  * string, the callback might want to know when it can releae resources
188  * assocated with that returned string; gpgme helps here by calling this
189  * passphrase callback with an DESC of %NULL as soon as it does not need
190  * the returned string anymore.  The callback function might then choose
191  * to release resources depending on R_HD.
192  *
193  **/
194 void
195 gpgme_set_passphrase_cb ( GpgmeCtx c, GpgmePassphraseCb cb, void *cb_value )
196 {
197     c->passphrase_cb = cb;
198     c->passphrase_cb_value = cb_value;
199 }
200
201 /**
202  * gpgme_set_pprogress_cb:
203  * @c: the context 
204  * @cb: A callback function
205  * @cb_value: The value passed to the callback function
206  * 
207  * This function sets a callback function to be used as a progress indicator.
208  *
209  * The callback function is defined as:
210  * <literal>
211  * typedef void (*GpgmeProgressCb) (void*cb_value,
212  *                                  const char *what, int type,
213  *                                  int curretn, int total);
214  * </literal>
215  * For details on the progress events, see the entry for the PROGRESS
216  * status in the file doc/DETAILS of the GnuPG distribution.
217  **/
218 void
219 gpgme_set_progress_cb ( GpgmeCtx c, GpgmeProgressCb cb, void *cb_value )
220 {
221     c->progress_cb = cb;
222     c->progress_cb_value = cb_value;
223 }
224
225