2001-11-21 Marcus Brinkmann <marcus@g10code.de>
[gpgme.git] / gpgme / recipient.c
1 /* recipient.c - mainatin recipient sets
2  *      Copyright (C) 2000 Werner Koch (dd9jn)
3  *      Copyright (C) 2001 g10 Code GmbH
4  *
5  * This file is part of GPGME.
6  *
7  * GPGME is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * GPGME is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #include "util.h"
29 #include "context.h"
30 #include "rungpg.h"
31
32 /**
33  * gpgme_recipients_new:
34  * @r_rset: Returns the new object.
35  * 
36  * Create a new uninitialized Reciepient set Object.
37  * 
38  * Return value: 0 on success or an error code.
39  **/
40 GpgmeError
41 gpgme_recipients_new (GpgmeRecipients *r_rset)
42 {
43     GpgmeRecipients rset;
44
45     rset = xtrycalloc ( 1, sizeof *rset  );
46     if (!rset)
47         return mk_error (Out_Of_Core);
48     *r_rset = rset;
49     return 0;
50 }
51
52 /**
53  * gpgme_recipients_release:
54  * @rset: Recipient Set object
55  * 
56  * Free the given object.
57  **/
58 void
59 gpgme_recipients_release ( GpgmeRecipients rset )
60 {
61     if (rset) {
62         struct user_id_s *u, *u2;
63
64         for (u = rset->list; u; u = u2) {
65             u2 = u->next;
66             xfree(u);
67         }
68     }
69     xfree ( rset );
70 }
71
72
73 /**
74  * gpgme_recipients_add_name:
75  * @rset: Recipient Set object 
76  * @name: user name or keyID
77  * 
78  * Add a name to the recipient Set.
79  * 
80  * Return value: 0 on success or an error code
81  **/
82 GpgmeError
83 gpgme_recipients_add_name (GpgmeRecipients rset, const char *name )
84 {
85     return gpgme_recipients_add_name_with_validity (
86         rset, name, GPGME_VALIDITY_UNKNOWN
87         );
88 }
89
90 /**
91  * gpgme_recipients_add_name_with_validity:
92  * @rset: Recipient Set object
93  * @name: user name or keyID
94  * @val: Validity value 
95  * 
96  * Same as gpgme_recipients_add_name() but with explictly given key
97  * validity.  Use one of the constants 
98  * %GPGME_VALIDITY_UNKNOWN, %GPGME_VALIDITY_UNDEFINED,
99  * %GPGME_VALIDITY_NEVER, %GPGME_VALIDITY_MARGINAL,
100  * %GPGME_VALIDITY_FULL, %GPGME_VALIDITY_ULTIMATE5
101  * for the validity.  %GPGME_VALIDITY_UNKNOWN is implicitly used by
102  * gpgme_recipients_add_name().
103  *
104  * Return value: o on success or an error value.
105  **/
106 GpgmeError
107 gpgme_recipients_add_name_with_validity (GpgmeRecipients rset,
108                                          const char *name,
109                                          GpgmeValidity val )
110 {
111     struct user_id_s *r;
112
113     if (!name || !rset )
114         return mk_error (Invalid_Value);
115     r = xtrymalloc ( sizeof *r + strlen (name) );
116     if (!r)
117         return mk_error (Out_Of_Core);
118     r->validity = val;
119     r->name_part = "";
120     r->email_part = "";
121     r->comment_part = "";
122     strcpy (r->name, name );
123     r->next = rset->list;
124     rset->list = r;
125     return 0;
126 }
127
128
129
130 /**
131  * gpgme_recipients_count:
132  * @rset: Recipient Set object
133  * 
134  * Return value: The number of recipients in the set.
135  **/
136 unsigned int 
137 gpgme_recipients_count ( const GpgmeRecipients rset )
138 {
139     struct user_id_s *r;
140     unsigned int count = 0;
141     
142     if ( rset ) {
143         for (r=rset->list ; r; r = r->next )
144             count++;
145     }
146     return count;
147 }
148
149
150
151 /**
152  * gpgme_recipients_enum_open:
153  * @rset: Recipient Set object
154  * @ctx: Enumerator
155  * 
156  * Start an enumeration on the Recipient Set object.  The caller must pass 
157  * the address of a void pointer which is used as the enumerator object.
158  * 
159  * Return value: 0 on success or an error code.
160  *
161  * See also: gpgme_recipients_enum_read(), gpgme_recipients_enum_close().
162  **/
163 GpgmeError
164 gpgme_recipients_enum_open ( const GpgmeRecipients rset, void **ctx )
165 {
166     if (!rset || !ctx)
167         return mk_error (Invalid_Value);
168
169     *ctx = rset->list;
170     return 0;
171 }
172
173 /**
174  * gpgme_recipients_enum_read:
175  * @rset: Recipient Set object
176  * @ctx: Enumerator 
177  * 
178  * Return the name of the next user name from the given recipient
179  * set. This name is valid as along as the @rset is valid and until
180  * the next call to this function.
181  * 
182  * Return value: name or NULL for no more names.
183  *
184  * See also: gpgme_recipients_enum_read(), gpgme_recipients_enum_close().
185  **/
186 const char *
187 gpgme_recipients_enum_read ( const GpgmeRecipients rset, void **ctx )
188 {
189     struct user_id_s *r;
190
191     if (!rset || !ctx)
192         return NULL; /* oops */
193     
194     r = *ctx;
195     if ( r ) {
196         const char *s = r->name;
197         r = r->next;
198         *ctx = r;
199         return s;
200     }
201
202     return NULL;
203 }
204
205 /**
206  * gpgme_recipients_enum_close:
207  * @rset: Recipient Set object
208  * @ctx: Enumerator
209  * 
210  * Release the enumerator @rset for this object.
211  * 
212  * Return value: 0 on success or %GPGME_Invalid_Value;
213  *
214  * See also: gpgme_recipients_enum_read(), gpgme_recipients_enum_close().
215  **/
216 GpgmeError
217 gpgme_recipients_enum_close ( const GpgmeRecipients rset, void **ctx )
218 {
219     if (!rset || !ctx)
220         return mk_error (Invalid_Value);
221     *ctx = NULL;
222     return 0;
223 }
224
225 int
226 _gpgme_recipients_all_valid ( const GpgmeRecipients rset )
227 {
228     struct user_id_s *r;
229
230     assert (rset);
231     for (r=rset->list ; r; r = r->next ) {
232         if (r->validity != GPGME_VALIDITY_FULL
233             && r->validity != GPGME_VALIDITY_ULTIMATE )
234             return 0; /*no*/
235     }
236     return 1; /*yes*/
237 }
238
239
240