summaryrefslogtreecommitdiff
path: root/gcr/gcr-gnupg-records.c
blob: 222f3dd007a615f88b4c77db7a3cf3098dbea5b0 (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
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
/*
 * Copyright (C) 2011 Collabora Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This program 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Stef Walter <stefw@collabora.co.uk>
 */

#include "config.h"

#include "gcr-gnupg-records.h"
#include "gcr-record.h"

#include "gck/gck.h"

#include <glib/gi18n-lib.h>

/* Copied from GPGME */
gboolean
_gcr_gnupg_records_parse_user_id (const gchar *user_id,
                                  gchar **rname,
                                  gchar **remail,
                                  gchar **rcomment)
{
	gchar *src, *tail, *x;
	int in_name = 0;
	int in_email = 0;
	int in_comment = 0;
	gboolean anything;
	const gchar *name = NULL;
	const gchar *email = NULL;
	const gchar *comment = NULL;

	x = tail = src = g_strdup (user_id);

	while (*src) {
		if (in_email) {
			/* Not legal but anyway.  */
			if (*src == '<')
				in_email++;
			else if (*src == '>') {
				if (!--in_email && !email) {
					email = tail;
					*src = 0;
					tail = src + 1;
				}
			}
		} else if (in_comment) {
			if (*src == '(')
				in_comment++;
			else if (*src == ')') {
				if (!--in_comment && !comment) {
					comment = tail;
					*src = 0;
					tail = src + 1;
				}
			}
		} else if (*src == '<') {
			if (in_name) {
				if (!name) {
					name = tail;
					*src = 0;
					tail = src + 1;
				}
				in_name = 0;
			} else
				tail = src + 1;

			in_email = 1;
		} else if (*src == '(') {
			if (in_name) {
				if (!name) {
					name = tail;
					*src = 0;
					tail = src + 1;
				}
				in_name = 0;
			}
			in_comment = 1;
		} else if (!in_name && *src != ' ' && *src != '\t') {
			in_name = 1;
		}
		src++;
	}

	if (in_name) {
		if (!name) {
			name = tail;
			*src = 0;
		}
	}

	anything = FALSE;

	if (rname) {
		*rname = g_strdup (name);
		if (name) {
			g_strstrip (*rname);
			anything = TRUE;
		}
	}

	if (remail) {
		*remail = g_strdup (email);
		if (email) {
			g_strstrip (*remail);
			anything = TRUE;
		}
	}

	if (rcomment) {
		*rcomment = g_strdup (comment);
		if (comment) {
			g_strstrip (*rcomment);
			anything = TRUE;
		}
	}

	g_free (x);
	return anything;
}

const gchar *
_gcr_gnupg_records_get_keyid (GPtrArray *records)
{
	GcrRecord *record;

	record = _gcr_records_find (records, GCR_RECORD_SCHEMA_PUB);
	if (record != NULL)
		return _gcr_record_get_raw (record, GCR_RECORD_KEY_KEYID);
	record = _gcr_records_find (records, GCR_RECORD_SCHEMA_SEC);
	if (record != NULL)
		return _gcr_record_get_raw (record, GCR_RECORD_KEY_KEYID);
	return NULL;
}

const gchar *
_gcr_gnupg_records_get_short_keyid (GPtrArray *records)
{
	const gchar *keyid;
	gsize length;

	keyid = _gcr_gnupg_records_get_keyid (records);
	if (keyid == NULL)
		return NULL;

	length = strlen (keyid);
	if (length > 8)
		keyid += (length - 8);

	return keyid;
}

gchar *
_gcr_gnupg_records_get_user_id (GPtrArray *records)
{
	GcrRecord *record;

	record = _gcr_records_find (records, GCR_RECORD_SCHEMA_UID);
	if (record != NULL)
		return _gcr_record_get_string (record, GCR_RECORD_UID_USERID);
	return NULL;
}

const gchar *
_gcr_gnupg_records_get_fingerprint (GPtrArray *records)
{
	GcrRecord *record;

	record = _gcr_records_find (records, GCR_RECORD_SCHEMA_FPR);
	if (record != NULL)
		return _gcr_record_get_raw (record, GCR_RECORD_FPR_FINGERPRINT);
	return NULL;
}