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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
extern crate core;
use self::core::marker::PhantomData;

pub struct KeyIterator {
    ctx : ::gpgme::Context,
}

impl KeyIterator {

    pub fn new() -> KeyIterator {
        let ctx = ::gpgme::Context::new();

        let err = unsafe {
            ::bindings::gpgme::gpgme_op_keylist_start(
                ctx.raw(), ::std::ptr::null(), 0
            )
        };

        assert_eq!(err, ::bindings::gpg_error::GPG_ERR_NO_ERROR);

        let err = unsafe {
            ::bindings::gpgme::gpgme_set_keylist_mode(
                ctx.raw(), 2 + 4 + 8 // ::bindings::gpgme::GPGME_KEYLIST_MODE_SIGS
            )
        };

        assert_eq!(err, ::bindings::gpg_error::GPG_ERR_NO_ERROR);

        KeyIterator{ctx: ctx}
    }

}

impl Drop for KeyIterator {

    fn drop(&mut self) {
        unsafe {
            ::bindings::gpgme::gpgme_op_keylist_end(self.ctx.raw());
        }
    }

}

impl Iterator for KeyIterator {
    type Item = Key;

    fn next(&mut self) -> Option<Key> {
        let mut key : ::bindings::gpgme::gpgme_key_t;
        let err = unsafe {
            key = ::std::mem::uninitialized();
            ::bindings::gpgme::gpgme_op_keylist_next(
                self.ctx.raw(), &mut key
            )
        };

        if err != ::bindings::gpg_error::GPG_ERR_NO_ERROR {
            None
        }
        else {
            Some(Key{raw: key})
        }
    }

}

pub struct Key {
    raw: ::bindings::gpgme::gpgme_key_t,
}

impl Key {

    pub fn subkeys<'a>(&'a self) -> SubKeyIterator<'a> {
        SubKeyIterator{
            current: SubKey{
                raw: unsafe { self.raw.as_ref() }.unwrap().subkeys,
                lifetime: PhantomData,
            }
        }
    }

    pub fn uids<'a>(&'a self) -> UserIdIterator<'a> {
        UserIdIterator{
            current: UserId{
                raw: unsafe { self.raw.as_ref() }.unwrap().uids,
                lifetime: PhantomData,
            }
        }
    }

}

impl Drop for Key {

    fn drop(&mut self) {
        unsafe { ::bindings::gpgme::gpgme_key_release(self.raw) };
    }

}

pub struct SubKeyIterator<'a> {
    current: SubKey<'a>,
}

impl<'a> Iterator for SubKeyIterator<'a> {
    type Item = SubKey<'a>;

    fn next(&mut self) -> Option<SubKey<'a>> {
        let raw = self.current.raw;

        if raw.is_null() {
            None
        }
        else {
            self.current = SubKey{
                raw: unsafe { raw.as_ref() }.unwrap().next,
                lifetime: PhantomData,
            };
            Some(SubKey{raw: raw, lifetime: PhantomData})
        }
    }

}

#[derive(Clone)]
pub struct SubKey<'a> {
    raw: ::bindings::gpgme::gpgme_subkey_t,
    lifetime: PhantomData<&'a Key>,
}

impl<'a> SubKey<'a> {

    pub fn keyid(&self) -> String {
        let keyid = unsafe { ::std::ffi::CStr::from_ptr(self.raw.as_ref().unwrap().keyid) };
        ::std::str::from_utf8(keyid.to_bytes()).unwrap().to_owned()
    }

}

pub struct UserIdIterator<'a> {
    current: UserId<'a>,
}

impl<'a> Iterator for UserIdIterator<'a> {
    type Item = UserId<'a>;

    fn next(&mut self) -> Option<UserId<'a>> {
        let raw = self.current.raw;

        if raw.is_null() {
            None
        }
        else {
            self.current = UserId{
                raw: unsafe { raw.as_ref() }.unwrap().next,
                lifetime: PhantomData,
            };
            Some(UserId{raw: raw, lifetime: PhantomData})
        }
    }

}

pub struct UserId<'a> {
    raw: ::bindings::gpgme::gpgme_user_id_t,
    lifetime: PhantomData<&'a Key>,
}

impl<'a> UserId<'a> {

    pub fn uid(&self) -> String {
        let uid = unsafe { ::std::ffi::CStr::from_ptr(self.raw.as_ref().unwrap().uid) };
        ::std::str::from_utf8(uid.to_bytes()).unwrap().to_owned()
    }

    pub fn signatures(&self) -> KeySignatureIterator {
        let sig = unsafe { self.raw.as_ref() }.unwrap().signatures;
        println!("{:?}", sig);
        KeySignatureIterator{
            current: KeySignature{
                raw: sig,
                lifetime: PhantomData,
            }
        }
    }

}

pub struct KeySignature<'a> {
    raw: ::bindings::gpgme::gpgme_key_sig_t,
    lifetime: PhantomData<&'a Key>,
}

impl<'a> KeySignature<'a> {

    pub fn keyid(&self) -> String {
        let id = unsafe { ::std::ffi::CStr::from_ptr(self.raw.as_ref().unwrap().keyid) };
        ::std::str::from_utf8(id.to_bytes()).unwrap().to_owned()
    }

}

pub struct KeySignatureIterator<'a> {
    current: KeySignature<'a>,
}

impl<'a> Iterator for KeySignatureIterator<'a> {
    type Item = KeySignature<'a>;

    fn next(&mut self) -> Option<KeySignature<'a>> {
        let raw = self.current.raw;

        if raw.is_null() {
            None
        }
        else {
            self.current = KeySignature{
                raw: unsafe { raw.as_ref() }.unwrap().next,
                lifetime: PhantomData,
            };
            Some(KeySignature{raw: raw, lifetime: PhantomData})
        }
    }

}