Branch data Line data Source code
1 : : /* 2 : : * Copyright (C) 2011 Collabora Ltd. 3 : : * 4 : : * This program is free software; you can redistribute it and/or modify 5 : : * it under the terms of the GNU Lesser General Public License as 6 : : * published by the Free Software Foundation; either version 2.1 of 7 : : * the License, or (at your option) any later version. 8 : : * 9 : : * This program is distributed in the hope that it will be useful, but 10 : : * WITHOUT ANY WARRANTY; without even the implied warranty of 11 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 : : * Lesser General Public License for more details. 13 : : * 14 : : * You should have received a copy of the GNU Lesser General Public 15 : : * License along with this program; if not, see <http://www.gnu.org/licenses/>. 16 : : * 17 : : * Author: Stef Walter <stefw@collabora.co.uk> 18 : : */ 19 : : 20 : : #include "config.h" 21 : : 22 : : #include "gcr-gnupg-records.h" 23 : : #include "gcr-record.h" 24 : : 25 : : #include "gck/gck.h" 26 : : 27 : : #include <glib/gi18n-lib.h> 28 : : 29 : : /* Copied from GPGME */ 30 : : gboolean 31 : 1 : _gcr_gnupg_records_parse_user_id (const gchar *user_id, 32 : : gchar **rname, 33 : : gchar **remail, 34 : : gchar **rcomment) 35 : : { 36 : : gchar *src, *tail, *x; 37 : 1 : int in_name = 0; 38 : 1 : int in_email = 0; 39 : 1 : int in_comment = 0; 40 : : gboolean anything; 41 : 1 : const gchar *name = NULL; 42 : 1 : const gchar *email = NULL; 43 : 1 : const gchar *comment = NULL; 44 : : 45 : 1 : x = tail = src = g_strdup (user_id); 46 : : 47 [ + + ]: 29 : while (*src) { 48 [ + + ]: 28 : if (in_email) { 49 : : /* Not legal but anyway. */ 50 [ - + ]: 15 : if (*src == '<') 51 : 0 : in_email++; 52 [ + + ]: 15 : else if (*src == '>') { 53 [ + - + - ]: 1 : if (!--in_email && !email) { 54 : 1 : email = tail; 55 : 1 : *src = 0; 56 : 1 : tail = src + 1; 57 : : } 58 : : } 59 [ - + ]: 13 : } else if (in_comment) { 60 [ # # ]: 0 : if (*src == '(') 61 : 0 : in_comment++; 62 [ # # ]: 0 : else if (*src == ')') { 63 [ # # # # ]: 0 : if (!--in_comment && !comment) { 64 : 0 : comment = tail; 65 : 0 : *src = 0; 66 : 0 : tail = src + 1; 67 : : } 68 : : } 69 [ + + ]: 13 : } else if (*src == '<') { 70 [ + - ]: 1 : if (in_name) { 71 [ + - ]: 1 : if (!name) { 72 : 1 : name = tail; 73 : 1 : *src = 0; 74 : 1 : tail = src + 1; 75 : : } 76 : 1 : in_name = 0; 77 : : } else 78 : 0 : tail = src + 1; 79 : : 80 : 1 : in_email = 1; 81 [ - + ]: 12 : } else if (*src == '(') { 82 [ # # ]: 0 : if (in_name) { 83 [ # # ]: 0 : if (!name) { 84 : 0 : name = tail; 85 : 0 : *src = 0; 86 : 0 : tail = src + 1; 87 : : } 88 : 0 : in_name = 0; 89 : : } 90 : 0 : in_comment = 1; 91 [ + + + - : 12 : } else if (!in_name && *src != ' ' && *src != '\t') { + - ] 92 : 1 : in_name = 1; 93 : : } 94 : 28 : src++; 95 : : } 96 : : 97 [ - + ]: 1 : if (in_name) { 98 [ # # ]: 0 : if (!name) { 99 : 0 : name = tail; 100 : 0 : *src = 0; 101 : : } 102 : : } 103 : : 104 : 1 : anything = FALSE; 105 : : 106 [ + - ]: 1 : if (rname) { 107 : 1 : *rname = g_strdup (name); 108 [ + - ]: 1 : if (name) { 109 : 1 : g_strstrip (*rname); 110 : 1 : anything = TRUE; 111 : : } 112 : : } 113 : : 114 [ + - ]: 1 : if (remail) { 115 : 1 : *remail = g_strdup (email); 116 [ + - ]: 1 : if (email) { 117 : 1 : g_strstrip (*remail); 118 : 1 : anything = TRUE; 119 : : } 120 : : } 121 : : 122 [ + - ]: 1 : if (rcomment) { 123 : 1 : *rcomment = g_strdup (comment); 124 [ - + ]: 1 : if (comment) { 125 : 0 : g_strstrip (*rcomment); 126 : 0 : anything = TRUE; 127 : : } 128 : : } 129 : : 130 : 1 : g_free (x); 131 : 1 : return anything; 132 : : } 133 : : 134 : : const gchar * 135 : 10 : _gcr_gnupg_records_get_keyid (GPtrArray *records) 136 : : { 137 : : GcrRecord *record; 138 : : 139 : 10 : record = _gcr_records_find (records, GCR_RECORD_SCHEMA_PUB); 140 [ + + ]: 10 : if (record != NULL) 141 : 8 : return _gcr_record_get_raw (record, GCR_RECORD_KEY_KEYID); 142 : 2 : record = _gcr_records_find (records, GCR_RECORD_SCHEMA_SEC); 143 [ + - ]: 2 : if (record != NULL) 144 : 2 : return _gcr_record_get_raw (record, GCR_RECORD_KEY_KEYID); 145 : 0 : return NULL; 146 : : } 147 : : 148 : : const gchar * 149 : 1 : _gcr_gnupg_records_get_short_keyid (GPtrArray *records) 150 : : { 151 : : const gchar *keyid; 152 : : gsize length; 153 : : 154 : 1 : keyid = _gcr_gnupg_records_get_keyid (records); 155 [ - + ]: 1 : if (keyid == NULL) 156 : 0 : return NULL; 157 : : 158 : 1 : length = strlen (keyid); 159 [ + - ]: 1 : if (length > 8) 160 : 1 : keyid += (length - 8); 161 : : 162 : 1 : return keyid; 163 : : } 164 : : 165 : : gchar * 166 : 0 : _gcr_gnupg_records_get_user_id (GPtrArray *records) 167 : : { 168 : : GcrRecord *record; 169 : : 170 : 0 : record = _gcr_records_find (records, GCR_RECORD_SCHEMA_UID); 171 [ # # ]: 0 : if (record != NULL) 172 : 0 : return _gcr_record_get_string (record, GCR_RECORD_UID_USERID); 173 : 0 : return NULL; 174 : : } 175 : : 176 : : const gchar * 177 : 0 : _gcr_gnupg_records_get_fingerprint (GPtrArray *records) 178 : : { 179 : : GcrRecord *record; 180 : : 181 : 0 : record = _gcr_records_find (records, GCR_RECORD_SCHEMA_FPR); 182 [ # # ]: 0 : if (record != NULL) 183 : 0 : return _gcr_record_get_raw (record, GCR_RECORD_FPR_FINGERPRINT); 184 : 0 : return NULL; 185 : : }